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-17] Add type annotations for conv layers #65183

Merged
merged 8 commits into from
Jun 28, 2024

Conversation

liyongchao911
Copy link
Contributor

PR Category

User Experience

PR Types

Improvements

Description

类型标注:

  • paddle/nn/layer/conv.py

Related links

@SigureMo @megemini

Copy link

paddle-bot bot commented Jun 14, 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 14, 2024
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:
Copy link
Contributor

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]:

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

Choose a reason for hiding this comment

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

尽量不要用 Any ~

另外,data_format 在 _typing 模块里面应该有公用类型 ~

Copy link
Member

Choose a reason for hiding this comment

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

不要直接从 paddlepaddle-stubs copy,要思考,_ConvNd 明显没有标注

@SigureMo SigureMo added the HappyOpenSource 快乐开源活动issue与PR label Jun 17, 2024
@SigureMo SigureMo self-assigned this Jun 21, 2024
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.

抱歉 review 拖的时间比较长 ~ 🙏🙏🙏

Comment on lines 23 to 33
from ..._typing import (
DataLayoutND,
DataLayout1D,
DataLayout2D,
DataLayout3D,
IntSequence,
ShapeLike,
)

PaddingSizeStr: TypeAlias = Literal["valid", "same"]
PaddingMode: TypeAlias = Literal["zeros", "reflect", "replicate", "circular"]
Copy link
Contributor

@megemini megemini Jun 26, 2024

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 ,大家统一一下 ~

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

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)]

Comment on lines 83 to 84
weight_attr: Any | None = None,
bias_attr: Any | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

这里可以不用 Any ~

Comment on lines 354 to 356
stride: int | IntSequence = 1,
padding: int | IntSequence | PaddingSizeStr = 0,
dilation: int | IntSequence = 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

根据不同的 Conv ,这里的输入可能不一样 ~ 参考 #65191

后面几个也是 ~

@SigureMo
Copy link
Member

这个 PR 后续我来推进

@SigureMo SigureMo changed the title [Typing][A-17] Add type annotations for paddle/nn/layer/conv.py [Typing][A-17] Add type annotations for conv layers Jun 28, 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 4eb608d into PaddlePaddle:develop Jun 28, 2024
30 of 32 checks passed
@@ -63,7 +63,7 @@
]
_DropoutMode: TypeAlias = Literal['upscale_in_train', 'downscale_in_infer']
_PaddingTensorMode: TypeAlias = Literal[
Copy link
Contributor

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 ..._ConvPaddingModezeros ...

Copy link
Member

Choose a reason for hiding this comment

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

没必要区分到这种程度,而且这名字也很奇怪

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