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-3] Add type annotations for paddle/tensor/creation.py #65082

Merged
merged 18 commits into from
Jun 17, 2024

Conversation

zrr1999
Copy link
Member

@zrr1999 zrr1999 commented Jun 12, 2024

PR Category

User Experience

PR Types

Improvements

Description

@megemini

Related links

Copy link

paddle-bot bot commented Jun 12, 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 12, 2024
@zrr1999 zrr1999 requested a review from SigureMo June 12, 2024 13:35
"""
This function creates a new tensor variable with value in the global block(block 0).

Args:
shape (list[int]|tuple[int]): Shape of the variable
shape (ShapeLike): Shape of the variable
Copy link
Member

Choose a reason for hiding this comment

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

这里 ShapeLike 不暴露给用户吧

Copy link
Member Author

Choose a reason for hiding this comment

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

done

python/paddle/tensor/creation.py Outdated Show resolved Hide resolved
python/paddle/tensor/creation.py Outdated Show resolved Hide resolved
python/paddle/tensor/creation.py Outdated Show resolved Hide resolved
@@ -2921,14 +3034,16 @@ def polar(abs, angle, name=None):


@dygraph_only
def cauchy_(x, loc=0, scale=1, name=None):
def cauchy_(
x: paddle.Tensor, loc=0, scale=1, 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.

是不是漏了俩 :doge:

@@ -2955,14 +3070,16 @@ def cauchy_(x, loc=0, scale=1, name=None):


@dygraph_only
def geometric_(x, probs, name=None):
def geometric_(
x: paddle.Tensor, probs, 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.

probs 也漏了?

@@ -1079,7 +1134,9 @@ def ones_like(x, dtype=None, name=None):
return full_like(x=x, fill_value=1, dtype=dtype, name=name)


def zeros(shape, dtype=None, name=None):
def zeros(
shape: ShapeLike, dtype: DTypeLike | None = None, name: str | None = None
Copy link
Member

@SigureMo SigureMo Jun 12, 2024

Choose a reason for hiding this comment

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

2024-06-13 00:06:49 paddle.zeros:1
2024-06-13 00:06:49 <string>:8:28: error: Argument "shape" to "zeros" has incompatible type "List[Tensor]"; expected "Union[Tuple[int, ...], List[int], Tensor]"  [arg-type]

看样子是动态 shape 挂了,这里可以不改,直接这样就好,我们改一下 ShapeLike

-DynamicShapeLike: TypeAlias = Union[
+_DynamicShapeLike: TypeAlias = Union[
-    Tuple[Union[int, "Tensor", None], ...],
-    List[Union[int, "Tensor", None]],
+    Sequence[Union[int, "Tensor", None]],
     "Tensor",
 ]


-ShapeLike: TypeAlias = Union[
+_StaticShapeLike: TypeAlias = Union[
-    Tuple[int, ...],
-    List[int],
+    Sequence[int],
     "Tensor",
 ]

+ShapeLike: TypeAlias = Union[_DynamicShapeLike, _StaticShapeLike]

非 dynamic 的情况基本是没有意义的

@luotao1 luotao1 added the HappyOpenSource 快乐开源活动issue与PR label Jun 13, 2024
@zrr1999 zrr1999 requested a review from SigureMo June 13, 2024 04:06
@@ -1136,7 +1137,9 @@ def ones_like(


def zeros(
shape: ShapeLike, dtype: DTypeLike | None = None, name: str | None = None
shape: DynamicShapeLike,
Copy link
Member

Choose a reason for hiding this comment

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

这咋和说的不一样 😂

@zrr1999 zrr1999 requested a review from gouzil as a code owner June 13, 2024 07:03
SigureMo
SigureMo previously approved these changes Jun 13, 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

Static Check 结果需要看一下,stop_gradient #65119 会解决,numpy 问题可以暂时在该行加 # type: ignore,其他问题需要再看看是不是类型不对

>>> array = np.array([[1, 1],
... [3, 4],
... [1, 3]]).astype(np.int64)
>>> array: npt.NDArray[Any] = np.array([[1, 1], [3, 4], [1, 3]]).astype(
Copy link
Contributor

Choose a reason for hiding this comment

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

这里还是加 # type: ignore 吧 ~ 不然一个简单的例子搞这么复杂,不太友好 ~

@SigureMo 看看行不?后面可以一块儿清理 ~

PaddlePaddle/docs#6680 用来处理渲染的问题 ~

Copy link
Member

Choose a reason for hiding this comment

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

嗯嗯,可以~

SigureMo
SigureMo previously approved these changes Jun 14, 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 🐾

This reverts commit aec0894.
SigureMo
SigureMo previously approved these changes Jun 14, 2024
@SigureMo
Copy link
Member

@megemini 有一个奇怪的问题,使用 type_checking 检查 paddle.clone 的时候,大概率会发生段错误

python tools/type_checking.py paddle.clone
----------------Codeblock Type Checking Start--------------------
>>> Get docstring from api ...
API_PR is diff from API_DEV: dict_keys(['paddle.clone'])
Total api: 1
>>> Running type checker ...


--------------------------------------
C++ Traceback (most recent call last):
--------------------------------------
No stack trace in paddle, may be caused by external reasons.

----------------------
Error Message Summary:
----------------------
FatalError: `Termination signal` is detected by the operating system.
  [TimeInfo: *** Aborted at 1718390619 (unix time) try "date -d @1718390619" if you are using GNU date ***]
  [SignalInfo: *** SIGTERM (@0x3e800008a48) received by PID 35464 (TID 0x7fb86244b740) from PID 35400 ***]

>>> Print summary ...
----------------Check results--------------------
----------------Check results--------------------
>>> Type checking is successful!
>>> Type checking is successful!
----------------End of the Check--------------------
----------------End of the Check--------------------

确认只有多进程才会有这个问题,感觉有点奇怪,目前只有这个 API 遇到了,可以低优看看

@megemini
Copy link
Contributor

确认只有多进程才会有这个问题,感觉有点奇怪,目前只有这个 API 遇到了,可以低优看看

我这里确实没遇到 type checking 的时候抛段错误 ~

我先记一下吧,关联 #65190 (comment)

@SigureMo
Copy link
Member

啊……还是冲突了,可以解决一下,应该恢复相关文件的修改就可以了~

@SigureMo
Copy link
Member

啊 我来搞一下吧

@SigureMo SigureMo merged commit a6cbbe8 into PaddlePaddle:develop Jun 17, 2024
32 of 33 checks passed
co63oc pushed a commit to co63oc/Paddle that referenced this pull request Jun 25, 2024
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.

5 participants