Skip to content
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

Merged
merged 8 commits into from
Jun 24, 2024

Conversation

gouzil
Copy link
Member

@gouzil gouzil commented Jun 19, 2024

PR Category

User Experience

PR Types

Improvements

Description

类型标注:

  • paddle/tensor/linalg.py

Related links

@SigureMo @megemini

Copy link

paddle-bot bot commented Jun 19, 2024

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Jun 19, 2024
@gouzil gouzil requested review from SigureMo and removed request for SigureMo June 19, 2024 05:06
@gouzil gouzil changed the title [Typing][A-5] Add type annotations for paddle/tensor/linalg.py [WIP][Typing][A-5] Add type annotations for paddle/tensor/linalg.py Jun 19, 2024
@gouzil gouzil changed the title [WIP][Typing][A-5] Add type annotations for paddle/tensor/linalg.py [Typing][A-5] Add type annotations for paddle/tensor/linalg.py Jun 19, 2024
@gouzil gouzil requested a review from SigureMo June 19, 2024 05:09
__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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x: Tensor, perm: list[int] | tuple[int, ...], name: str | None = None
x: Tensor, perm: Sequence[int], name: str | None = None

def vector_norm(
x: Tensor,
p: float = 2.0,
axis: int | list[int] | tuple[int, ...] | None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

def norm(x, p=None, axis=None, keepdim=False, name=None):
def norm(
x: paddle.Tensor,
p: float | Literal['fro', 'nuc'] | None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 Literal 出现了多次,是否可以提取成一个 TypeAlias?

@@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

axis 的类型?

def cross(
x: Tensor,
y: Tensor,
axis: int | list[int] | tuple[int, ...] = 9,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用 Sequence

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x 的类型?

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]:
Copy link
Member

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)

if TYPE_CHECKING:
from paddle import Tensor

_Porder: TypeAlias = Literal['fro', 'nuc']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_Porder: TypeAlias = Literal['fro', 'nuc']
_POrder: TypeAlias = Literal['fro', 'nuc']

def matrix_norm(x, p='fro', axis=[-2, -1], keepdim=False, name=None):
def matrix_norm(
x: Tensor,
p: float | str = 'fro',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里也是 _POrder 吧?

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_POrder ?

Comment on lines 984 to 990
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

统一用 Tensor 吧 ~

def pinv(x, rcond=1e-15, hermitian=False, name=None):
def pinv(
x: Tensor,
rcond: Tensor = 1e-15,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float | Tensor

Comment on lines 3058 to 3062
@overload
def lu(
x: Tensor, pivot: bool = ..., get_infos: bool = ..., name: str | None = None
) -> tuple[Tensor, Tensor] | tuple[Tensor, Tensor, Tensor]:
...
Copy link
Contributor

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 就不用再重复标注了 ~

Comment on lines 2969 to 2973
def qr(
x: Tensor,
mode: Literal['reduced', 'complete', 'r'] = "reduced",
name: str | None = None,
) -> tuple[Tensor, Tensor] | Tensor:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里貌似也可以根据 mode 分流?

x: Tensor,
pivot: bool = ...,
get_infos: Literal[False] = ...,
name: str | None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: str | None = None,
name: str | None = ...,

纯类型标注的签名这里用 ...

@SigureMo
Copy link
Member

static-check 需要看下

SigureMo
SigureMo previously approved these changes Jun 21, 2024
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMeow 🐾

@SigureMo
Copy link
Member

看样子给 PR-CI-Paddle-Doc-Preview 搞挂了,得看看

@SigureMo SigureMo added the HappyOpenSource 快乐开源活动issue与PR label Jun 21, 2024
@SigureMo SigureMo self-assigned this Jun 21, 2024
@SigureMo SigureMo merged commit 4c84c13 into PaddlePaddle:develop Jun 24, 2024
32 of 33 checks passed
@gouzil gouzil deleted the typehint/tensor_linalg branch July 20, 2024 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource 快乐开源活动issue与PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants