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-2,A-4] Add type annotations for paddle/tensor/attribute.py and paddle/tensor/einsum.py #65255

Merged
merged 13 commits into from
Jun 20, 2024

Conversation

zrr1999
Copy link
Member

@zrr1999 zrr1999 commented Jun 18, 2024

PR Category

User Experience

PR Types

Improvements

Description

添加类型提示

Related links

Copy link

paddle-bot bot commented Jun 18, 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 18, 2024
@zrr1999 zrr1999 changed the title Th 3 [Typing][A-2,A-3] Add type annotations for paddle/tensor/attribute.py, paddle/tensor/einsum.py Jun 18, 2024
@zrr1999 zrr1999 requested a review from SigureMo June 18, 2024 07:54
@@ -323,15 +336,26 @@ def plan_reduce(plan, op, reduce_dims, keepdim):
plan.add_step(step)


def plan_scalar_prod(plan, op1, op2):
def plan_scalar_prod(plan: "Plan", op1: int, op2: int) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

使用 PEP 563 这里就不用引号了

range(start, end)[::-1], range(s.start(), s.end())[::-1]
):
inv_map[ax] = dim
assert r is not None, "Invalid equation: missing ellipsis in output labels."
Copy link
Member

Choose a reason for hiding this comment

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

这里能确保 r 不是 None 么?那下面的 if r 的 else 分支是干啥的?

@@ -3361,7 +3361,7 @@ def unique(
return tuple(outs)


def unsqueeze(x, axis, name=None):
def unsqueeze(x, axis, name=None) -> paddle.Tensor:
Copy link
Member

Choose a reason for hiding this comment

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

输入也顺手标一下吧~

另外尽可能用

if TYPE_CHECKING:
    from paddle import Tensor

然后标注直接用 Tensor 吧~

Copy link
Contributor

@megemini megemini left a comment

Choose a reason for hiding this comment

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

我觉得不要为了类型标注而修改代码逻辑 ~ 如果不方便标注,实在不行就用 Any ~

如果最后总结的时候,发现有些地方标注不合适 (如 Any ),再推动修改代码 ~

@SigureMo 帮忙确认一下 ~

Comment on lines 164 to 172
assert r is not None, "Invalid equation: missing ellipsis in output labels."
start, end = r.start(), r.end()
s = re.search(r'\.+', in_labels)
# fill the broadcast dimension indices from right to left.
if s:
for ax, dim in zip(
range(start, end)[::-1], range(s.start(), s.end())[::-1]
):
inv_map[ax] = dim
Copy link
Contributor

Choose a reason for hiding this comment

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

不要改代码逻辑吧?

@@ -460,7 +460,7 @@ def create_parameter(

def create_variable_for_type_inference(
self, dtype, stop_gradient=False, shape=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.

输入哩 ~

Copy link
Member

Choose a reason for hiding this comment

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

这个没啥关系,反正不是公开 API

@@ -323,15 +336,26 @@ def plan_reduce(plan, op, reduce_dims, keepdim):
plan.add_step(step)


def plan_scalar_prod(plan, op1, op2):
def plan_scalar_prod(plan: "Plan", op1: int, op2: int) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

from __future__ import annotations 不用引号 ~

@SigureMo SigureMo changed the title [Typing][A-2,A-3] Add type annotations for paddle/tensor/attribute.py, paddle/tensor/einsum.py [Typing][A-2,A-3] Add type annotations for paddle/tensor/attribute.py and paddle/tensor/einsum.py Jun 18, 2024
@@ -13,6 +13,7 @@
# limitations under the License.

# TODO: define functions to get tensor attributes
Copy link
Member

Choose a reason for hiding this comment

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

TODO 删了吧~

@@ -28,7 +29,7 @@
__all__ = []


def rank(input):
def rank(input: paddle.Tensor) -> paddle.Tensor:
Copy link
Member

Choose a reason for hiding this comment

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

这个文件也一样直接用 Tensor 吧

@SigureMo SigureMo changed the title [Typing][A-2,A-3] Add type annotations for paddle/tensor/attribute.py and paddle/tensor/einsum.py [Typing][A-2,A-4] Add type annotations for paddle/tensor/attribute.py and paddle/tensor/einsum.py Jun 19, 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 SigureMo merged commit ff19b61 into PaddlePaddle:develop Jun 20, 2024
32 of 33 checks passed
@SigureMo SigureMo added the HappyOpenSource 快乐开源活动issue与PR label Jun 21, 2024
co63oc pushed a commit to co63oc/Paddle that referenced this pull request Jun 25, 2024
…y` and `paddle/tensor/einsum.py` (PaddlePaddle#65255)



---------

Co-authored-by: Nyakku Shigure <[email protected]>
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.

3 participants