-
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-17] Add type annotations for conv layers #65183
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
python/paddle/nn/layer/conv.py
Outdated
filter_elem_num = num_channels * np.prod(filter_size) | ||
std = (2.0 / filter_elem_num) ** 0.5 | ||
return Normal(0.0, std) | ||
|
||
|
||
def _reverse_repeat_list(t, n): | ||
def _reverse_repeat_list(t: Sequence[int], n: int) -> list: |
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.
这里的 t
应该不只限于 int
~ 可以试一下 type var ~
def _reverse_repeat_list(t: Sequence[_T], n: int) -> list[_T]:
python/paddle/nn/layer/conv.py
Outdated
in_channels: Any, | ||
out_channels: Any, | ||
kernel_size: Any, | ||
transposed: Any, | ||
dims: Any, | ||
stride: int | IntSequence = 1, | ||
padding: int | IntSequence = 0, | ||
padding_mode: str = 'zeros', | ||
output_padding: int | IntSequence = 0, | ||
dilation: int | IntSequence = 1, | ||
groups: int = 1, | ||
weight_attr: Any | None = None, | ||
bias_attr: Any | None = None, | ||
data_format: str = "NCHW", | ||
) -> 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.
尽量不要用 Any ~
另外,data_format 在 _typing
模块里面应该有公用类型 ~
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.
不要直接从 paddlepaddle-stubs copy,要思考,_ConvNd
明显没有标注
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.
抱歉 review 拖的时间比较长 ~ 🙏🙏🙏
python/paddle/nn/layer/conv.py
Outdated
from ..._typing import ( | ||
DataLayoutND, | ||
DataLayout1D, | ||
DataLayout2D, | ||
DataLayout3D, | ||
IntSequence, | ||
ShapeLike, | ||
) | ||
|
||
PaddingSizeStr: TypeAlias = Literal["valid", "same"] | ||
PaddingMode: TypeAlias = Literal["zeros", "reflect", "replicate", "circular"] |
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.
以及下面的 from paddle import ParamAttr
放到 if TYPE_CHECKING:
里面吧 ~
2024-06-25 16:49:54 ImportError: cannot import name 'ParamAttr' from partially initialized module 'paddle' (most likely due to a circular import) (/paddle/build/python/paddle/__init__.py)
这里的报错也跟这个有关 ~
另外,这里依赖的 PaddingSizeStr 可以等 #65197 合入后,从 python/paddle/nn/functional/common.py 引入 ~
TYPE_CHECKING
里面再加一个 from paddle import Tensor
吧 ~ 后面就不用 paddle.Tensor
而是用 Tensor
,大家统一一下 ~
python/paddle/nn/layer/conv.py
Outdated
filter_elem_num = num_channels * np.prod(filter_size) | ||
std = (2.0 / filter_elem_num) ** 0.5 | ||
return Normal(0.0, std) | ||
|
||
|
||
def _reverse_repeat_list(t, n): | ||
def _reverse_repeat_list(t: Sequence[int | str], n: int) -> list: |
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.
可以尝试
_T = TypeVar("_T")
def _reverse_repeat_list(t: Sequence[_T], n:int) -> list[_T]:
"""Reverse the order of `t` and repeat each element for `n` times.
This can be used to translate padding arg used by Conv and Pooling modules
to the ones used by `F.pad`.
"""
return [x for x in reversed(t) for _ in range(n)]
python/paddle/nn/layer/conv.py
Outdated
weight_attr: Any | None = None, | ||
bias_attr: Any | 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.
这里可以不用 Any ~
python/paddle/nn/layer/conv.py
Outdated
stride: int | IntSequence = 1, | ||
padding: int | IntSequence | PaddingSizeStr = 0, | ||
dilation: int | IntSequence = 1, |
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.
根据不同的 Conv ,这里的输入可能不一样 ~ 参考 #65191
后面几个也是 ~
这个 PR 后续我来推进 |
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.
@@ -63,7 +63,7 @@ | |||
] | |||
_DropoutMode: TypeAlias = Literal['upscale_in_train', 'downscale_in_infer'] | |||
_PaddingTensorMode: TypeAlias = Literal[ |
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.
为啥要加 zeros
? 我记得当时单独在 conv 里面加了一个
_ConvPaddingMode: TypeAlias = Literal[
"zero", "reflect", "replicate", "circular"
]
_PaddingTensorMode 是 constant ...
,_ConvPaddingMode
是 zeros ...
~
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 Category
User Experience
PR Types
Improvements
Description
类型标注:
Related links
@SigureMo @megemini