-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Typing][A-5] Add type annotations for paddle/tensor/linalg.py
#65274
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
paddle/tensor/linalg.py
paddle/tensor/linalg.py
paddle/tensor/linalg.py
paddle/tensor/linalg.py
python/paddle/tensor/linalg.py
Outdated
__all__ = [] | ||
|
||
|
||
# Consistent with kDefaultDim from C++ Backend | ||
K_DEFAULT_DIM = 9 | ||
|
||
|
||
def transpose(x, perm, name=None): | ||
def transpose( | ||
x: Tensor, perm: list[int] | tuple[int, ...], name: str | None = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
x: Tensor, perm: list[int] | tuple[int, ...], name: str | None = None | |
x: Tensor, perm: Sequence[int], name: str | None = None |
python/paddle/tensor/linalg.py
Outdated
def vector_norm( | ||
x: Tensor, | ||
p: float = 2.0, | ||
axis: int | list[int] | tuple[int, ...] | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
python/paddle/tensor/linalg.py
Outdated
def norm(x, p=None, axis=None, keepdim=False, name=None): | ||
def norm( | ||
x: paddle.Tensor, | ||
p: float | Literal['fro', 'nuc'] | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 Literal 出现了多次,是否可以提取成一个 TypeAlias?
python/paddle/tensor/linalg.py
Outdated
@@ -1389,7 +1442,7 @@ def mat_norm(input, porder=1.0, axis=None): | |||
) | |||
return out | |||
|
|||
def fro_norm(input, porder=2, axis=[-1]): | |||
def fro_norm(input: Tensor, porder: float = 2, axis=[-1]) -> Tensor: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
axis 的类型?
python/paddle/tensor/linalg.py
Outdated
def cross( | ||
x: Tensor, | ||
y: Tensor, | ||
axis: int | list[int] | tuple[int, ...] = 9, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用 Sequence
python/paddle/tensor/linalg.py
Outdated
@@ -2927,7 +3030,9 @@ def qr(x, mode="reduced", name=None): | |||
return q, r | |||
|
|||
|
|||
def lu(x, pivot=True, get_infos=False, name=None): | |||
def lu( | |||
x, pivot: bool = True, get_infos: bool = False, name: str | None = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
x 的类型?
python/paddle/tensor/linalg.py
Outdated
def lu(x, pivot=True, get_infos=False, name=None): | ||
def lu( | ||
x, pivot: bool = True, get_infos: bool = False, name: str | None = None | ||
) -> Tensor | tuple[Tensor, Tensor] | tuple[Tensor, Tensor, Tensor]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
输出元素个数是静态可区分的吗?比如是否可以根据输入签名确定输出的元素个数,如果可以的话,使用 @overload
,因为输出是 Union 对于下游来说是非常不友好的(往往需要 assert/if isinstance check)
python/paddle/tensor/linalg.py
Outdated
if TYPE_CHECKING: | ||
from paddle import Tensor | ||
|
||
_Porder: TypeAlias = Literal['fro', 'nuc'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_Porder: TypeAlias = Literal['fro', 'nuc'] | |
_POrder: TypeAlias = Literal['fro', 'nuc'] |
python/paddle/tensor/linalg.py
Outdated
def matrix_norm(x, p='fro', axis=[-2, -1], keepdim=False, name=None): | ||
def matrix_norm( | ||
x: Tensor, | ||
p: float | str = 'fro', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里也是 _POrder
吧?
python/paddle/tensor/linalg.py
Outdated
def p_matrix_norm(input, porder=1.0, axis=axis, keepdim=False, name=None): | ||
def p_matrix_norm( | ||
input: Tensor, | ||
porder: float | str = 1.0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_POrder
?
python/paddle/tensor/linalg.py
Outdated
def norm( | ||
x: paddle.Tensor, | ||
p: float | _Porder | None = None, | ||
axis: int | list[int] | tuple[int, int] | None = None, | ||
keepdim: bool = False, | ||
name: str | None = None, | ||
) -> paddle.Tensor: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
统一用 Tensor
吧 ~
python/paddle/tensor/linalg.py
Outdated
def pinv(x, rcond=1e-15, hermitian=False, name=None): | ||
def pinv( | ||
x: Tensor, | ||
rcond: Tensor = 1e-15, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
float | Tensor
python/paddle/tensor/linalg.py
Outdated
@overload | ||
def lu( | ||
x: Tensor, pivot: bool = ..., get_infos: bool = ..., name: str | None = None | ||
) -> tuple[Tensor, Tensor] | tuple[Tensor, Tensor, Tensor]: | ||
... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这一段是不是没啥必要? 通过 get_infos
分流,这里相当于上面两个的 union,就重复了?
另外,如果使用了 @overload
的话,下面具体 def lu
就不用再重复标注了 ~
python/paddle/tensor/linalg.py
Outdated
def qr( | ||
x: Tensor, | ||
mode: Literal['reduced', 'complete', 'r'] = "reduced", | ||
name: str | None = None, | ||
) -> tuple[Tensor, Tensor] | Tensor: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里貌似也可以根据 mode 分流?
python/paddle/tensor/linalg.py
Outdated
x: Tensor, | ||
pivot: bool = ..., | ||
get_infos: Literal[False] = ..., | ||
name: str | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name: str | None = None, | |
name: str | None = ..., |
纯类型标注的签名这里用 ...
static-check 需要看下 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看样子给 PR-CI-Paddle-Doc-Preview 搞挂了,得看看 |
PR Category
User Experience
PR Types
Improvements
Description
类型标注:
Related links
@SigureMo @megemini