From f4b5378f0d4639a90763f94a5decb80aa0769b58 Mon Sep 17 00:00:00 2001 From: liyongchao911 <11_258@163.com> Date: Fri, 14 Jun 2024 17:16:52 +0800 Subject: [PATCH 1/7] Typing_conv.py --- python/paddle/nn/layer/conv.py | 213 ++++++++++++++++++--------------- 1 file changed, 116 insertions(+), 97 deletions(-) diff --git a/python/paddle/nn/layer/conv.py b/python/paddle/nn/layer/conv.py index 2990969ef0503..90f9fc393a4e4 100644 --- a/python/paddle/nn/layer/conv.py +++ b/python/paddle/nn/layer/conv.py @@ -14,7 +14,26 @@ # TODO: define classes of convolutional neural network +from __future__ import annotations + +from paddle import ParamAttr +from typing import Any, Literal, Sequence + +from typing_extensions import TypeAlias + +from ..._typing import ( + DataLayout1D, + DataLayout2D, + DataLayout3D, + IntSequence, + ShapeLike, +) + +PaddingSizeStr: TypeAlias = Literal["valid", "same"] +PaddingMode: TypeAlias = Literal["zeros", "reflect", "replicate", "circular"] + import numpy as np +import paddle from paddle import get_flags @@ -32,13 +51,13 @@ __all__ = [] -def _get_default_param_initializer(num_channels, filter_size): +def _get_default_param_initializer(num_channels: int, filter_size: int) -> paddle.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], n: int) -> list: """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`. @@ -49,21 +68,21 @@ def _reverse_repeat_list(t, n): class _ConvNd(Layer): def __init__( self, - in_channels, - out_channels, - kernel_size, - transposed, - dims, - stride=1, - padding=0, - padding_mode='zeros', - output_padding=0, - dilation=1, - groups=1, - weight_attr=None, - bias_attr=None, - data_format="NCHW", - ): + 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: super().__init__() assert ( weight_attr is not False @@ -142,7 +161,7 @@ def __init__( in_channels // groups, ] + self._kernel_size - def _get_default_param_initializer(): + def _get_default_param_initializer() -> None: if transposed: return None filter_elem_num = np.prod(self._kernel_size) * self._in_channels @@ -186,7 +205,7 @@ def _get_default_param_initializer(): ): self._use_cudnn = False - def extra_repr(self): + def extra_repr(self) -> str: main_str = '{_in_channels}, {_out_channels}, kernel_size={_kernel_size}' if self._stride != [1] * len(self._stride): main_str += ', stride={_stride}' @@ -328,18 +347,18 @@ class Conv1D(_ConvNd): def __init__( self, - in_channels, - out_channels, - kernel_size, - stride=1, - padding=0, - dilation=1, - groups=1, - padding_mode='zeros', - weight_attr=None, - bias_attr=None, - data_format="NCL", - ): + in_channels: int, + out_channels: int, + kernel_size: int | IntSequence, + stride: int | IntSequence = 1, + padding: int | IntSequence | PaddingSizeStr = 0, + dilation: int | IntSequence = 1, + groups: int = 1, + padding_mode: PaddingMode = 'zeros', + weight_attr: ParamAttr | None = None, + bias_attr: ParamAttr | bool | None = None, + data_format: DataLayout1D = "NCL", + ) -> None: super().__init__( in_channels, out_channels, @@ -356,7 +375,7 @@ def __init__( data_format=data_format, ) - def forward(self, x): + def forward(self, x: paddle.Tensor) -> paddle.Tensor: padding = 0 if self._padding_mode != "zeros": x = F.pad( @@ -515,18 +534,18 @@ class Conv1DTranspose(_ConvNd): def __init__( self, - in_channels, - out_channels, - kernel_size, - stride=1, - padding=0, - output_padding=0, - groups=1, - dilation=1, - weight_attr=None, - bias_attr=None, - data_format="NCL", - ): + in_channels: int, + out_channels: int, + kernel_size: int | IntSequence, + stride: int | IntSequence = 1, + padding: int | IntSequence | PaddingSizeStr = 0, + output_padding: int | IntSequence = 0, + groups: int = 1, + dilation: int | IntSequence = 1, + weight_attr: ParamAttr | None = None, + bias_attr: ParamAttr | bool | None = None, + data_format: DataLayout1D = "NCL", + ) -> None: super().__init__( in_channels, out_channels, @@ -543,7 +562,7 @@ def __init__( data_format=data_format, ) - def forward(self, x, output_size=None): + def forward(self, x: paddle.Tensor, output_size: ShapeLike | None = None) -> paddle.Tensor: out = F.conv1d_transpose( x, self.weight, @@ -671,18 +690,18 @@ class Conv2D(_ConvNd): def __init__( self, - in_channels, - out_channels, - kernel_size, - stride=1, - padding=0, - dilation=1, - groups=1, - padding_mode='zeros', - weight_attr=None, - bias_attr=None, - data_format="NCHW", - ): + in_channels: int, + out_channels: int, + kernel_size: int | IntSequence, + stride: int | IntSequence = 1, + padding: int | IntSequence | PaddingSizeStr = 0, + dilation: int | IntSequence = 1, + groups: int = 1, + padding_mode: PaddingMode = 'zeros', + weight_attr: ParamAttr | None = None, + bias_attr: ParamAttr | bool | None = None, + data_format: DataLayout2D = "NCHW", + ) -> None: super().__init__( in_channels, out_channels, @@ -699,7 +718,7 @@ def __init__( data_format=data_format, ) - def forward(self, x): + def forward(self, x: paddle.Tensor) -> paddle.Tensor: if self._padding_mode != 'zeros': x = F.pad( x, @@ -846,18 +865,18 @@ class Conv2DTranspose(_ConvNd): def __init__( self, - in_channels, - out_channels, - kernel_size, - stride=1, - padding=0, - output_padding=0, - dilation=1, - groups=1, - weight_attr=None, - bias_attr=None, - data_format="NCHW", - ): + in_channels: int, + out_channels: int, + kernel_size: int | IntSequence, + stride: int | IntSequence = 1, + padding: int | IntSequence | PaddingSizeStr = 0, + output_padding: int | IntSequence = 0, + dilation: int | IntSequence = 1, + groups: int = 1, + weight_attr: ParamAttr | None = None, + bias_attr: ParamAttr | bool | None = None, + data_format: DataLayout2D = "NCHW", + ) -> None: super().__init__( in_channels, out_channels, @@ -874,7 +893,7 @@ def __init__( data_format=data_format, ) - def forward(self, x, output_size=None): + def forward(self, x: paddle.Tensor, output_size: ShapeLike | None = None) -> paddle.Tensor: if output_size is None: output_padding = self.output_padding else: @@ -1003,18 +1022,18 @@ class Conv3D(_ConvNd): def __init__( self, - in_channels, - out_channels, - kernel_size, - stride=1, - padding=0, - dilation=1, - groups=1, - padding_mode='zeros', - weight_attr=None, - bias_attr=None, - data_format="NCDHW", - ): + in_channels: int, + out_channels: int, + kernel_size: int | IntSequence, + stride: int | IntSequence = 1, + padding: int | IntSequence | PaddingSizeStr = 0, + dilation: int | IntSequence = 1, + groups: int = 1, + padding_mode: PaddingMode = 'zeros', + weight_attr: ParamAttr | None = None, + bias_attr: ParamAttr | bool | None = None, + data_format: DataLayout3D = "NCDHW", + ) -> None: super().__init__( in_channels, out_channels, @@ -1031,7 +1050,7 @@ def __init__( data_format=data_format, ) - def forward(self, x): + def forward(self, x: paddle.Tensor) -> paddle.Tensor: if self._padding_mode != 'zeros': x = F.pad( x, @@ -1182,18 +1201,18 @@ class Conv3DTranspose(_ConvNd): def __init__( self, - in_channels, - out_channels, - kernel_size, - stride=1, - padding=0, - output_padding=0, - dilation=1, - groups=1, - weight_attr=None, - bias_attr=None, - data_format="NCDHW", - ): + in_channels: int, + out_channels: int, + kernel_size: int | IntSequence, + stride: int | IntSequence = 1, + padding: int | IntSequence | PaddingSizeStr = 0, + output_padding: int | IntSequence = 0, + dilation: int | IntSequence = 1, + groups: int = 1, + weight_attr: ParamAttr | None = None, + bias_attr: ParamAttr | bool | None = None, + data_format: DataLayout3D = "NCDHW", + ) -> None: super().__init__( in_channels, out_channels, @@ -1210,7 +1229,7 @@ def __init__( data_format=data_format, ) - def forward(self, x, output_size=None): + def forward(self, x: paddle.Tensor, output_size: ShapeLike | None = None) -> paddle.Tensor: if output_size is None: output_padding = self.output_padding else: From 65452d449a4ae6f6e1354718fa3d4caccd0565ba Mon Sep 17 00:00:00 2001 From: liyongchao911 <11_258@163.com> Date: Mon, 17 Jun 2024 16:39:18 +0800 Subject: [PATCH 2/7] update --- python/paddle/nn/layer/conv.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/python/paddle/nn/layer/conv.py b/python/paddle/nn/layer/conv.py index 90f9fc393a4e4..d9331bd0dd2d3 100644 --- a/python/paddle/nn/layer/conv.py +++ b/python/paddle/nn/layer/conv.py @@ -22,6 +22,7 @@ from typing_extensions import TypeAlias from ..._typing import ( + DataLayoutND, DataLayout1D, DataLayout2D, DataLayout3D, @@ -57,7 +58,7 @@ def _get_default_param_initializer(num_channels: int, filter_size: int) -> paddl return Normal(0.0, std) -def _reverse_repeat_list(t: Sequence[int], n: int) -> list: +def _reverse_repeat_list(t: Sequence[int | str], n: int) -> list: """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`. @@ -68,20 +69,20 @@ def _reverse_repeat_list(t: Sequence[int], n: int) -> list: class _ConvNd(Layer): def __init__( self, - in_channels: Any, - out_channels: Any, - kernel_size: Any, - transposed: Any, - dims: Any, + in_channels: int, + out_channels: int, + kernel_size: int | IntSequence, + transposed: bool, + dims: int, stride: int | IntSequence = 1, padding: int | IntSequence = 0, - padding_mode: str = 'zeros', - output_padding: int | IntSequence = 0, + padding_mode: PaddingMode = 'zeros', + output_padding: int | IntSequence | PaddingSizeStr = 0, dilation: int | IntSequence = 1, groups: int = 1, weight_attr: Any | None = None, bias_attr: Any | None = None, - data_format: str = "NCHW", + data_format: DataLayoutND = "NCHW", ) -> None: super().__init__() assert ( From 06faaf3c906c2dcd7ba56854569d12b214cd0100 Mon Sep 17 00:00:00 2001 From: liyongchao911 <11_258@163.com> Date: Wed, 19 Jun 2024 16:11:23 +0800 Subject: [PATCH 3/7] update --- python/paddle/nn/layer/conv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/nn/layer/conv.py b/python/paddle/nn/layer/conv.py index d9331bd0dd2d3..70ae6277a866c 100644 --- a/python/paddle/nn/layer/conv.py +++ b/python/paddle/nn/layer/conv.py @@ -1033,7 +1033,7 @@ def __init__( padding_mode: PaddingMode = 'zeros', weight_attr: ParamAttr | None = None, bias_attr: ParamAttr | bool | None = None, - data_format: DataLayout3D = "NCDHW", + data_format: DataLayout3D = "NCDHW", ) -> None: super().__init__( in_channels, From cdf0aad82da24477ce92325871500d22cbe47983 Mon Sep 17 00:00:00 2001 From: liyongchao911 <11_258@163.com> Date: Tue, 25 Jun 2024 15:34:14 +0800 Subject: [PATCH 4/7] update --- python/paddle/nn/layer/conv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/nn/layer/conv.py b/python/paddle/nn/layer/conv.py index 70ae6277a866c..562b003bdbe51 100644 --- a/python/paddle/nn/layer/conv.py +++ b/python/paddle/nn/layer/conv.py @@ -16,7 +16,6 @@ from __future__ import annotations -from paddle import ParamAttr from typing import Any, Literal, Sequence from typing_extensions import TypeAlias @@ -37,6 +36,7 @@ import paddle from paddle import get_flags +from paddle import ParamAttr from ...device import ( get_cudnn_version, From 125721f90864c97b88462c64609cf591d0e0c5cc Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 28 Jun 2024 22:54:55 +0800 Subject: [PATCH 5/7] revert all --- python/paddle/nn/layer/conv.py | 214 +++++++++++++++------------------ 1 file changed, 97 insertions(+), 117 deletions(-) diff --git a/python/paddle/nn/layer/conv.py b/python/paddle/nn/layer/conv.py index 562b003bdbe51..2990969ef0503 100644 --- a/python/paddle/nn/layer/conv.py +++ b/python/paddle/nn/layer/conv.py @@ -14,29 +14,9 @@ # TODO: define classes of convolutional neural network -from __future__ import annotations - -from typing import Any, Literal, Sequence - -from typing_extensions import TypeAlias - -from ..._typing import ( - DataLayoutND, - DataLayout1D, - DataLayout2D, - DataLayout3D, - IntSequence, - ShapeLike, -) - -PaddingSizeStr: TypeAlias = Literal["valid", "same"] -PaddingMode: TypeAlias = Literal["zeros", "reflect", "replicate", "circular"] - import numpy as np -import paddle from paddle import get_flags -from paddle import ParamAttr from ...device import ( get_cudnn_version, @@ -52,13 +32,13 @@ __all__ = [] -def _get_default_param_initializer(num_channels: int, filter_size: int) -> paddle.Tensor: +def _get_default_param_initializer(num_channels, filter_size): 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: Sequence[int | str], n: int) -> list: +def _reverse_repeat_list(t, n): """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`. @@ -69,21 +49,21 @@ def _reverse_repeat_list(t: Sequence[int | str], n: int) -> list: class _ConvNd(Layer): def __init__( self, - in_channels: int, - out_channels: int, - kernel_size: int | IntSequence, - transposed: bool, - dims: int, - stride: int | IntSequence = 1, - padding: int | IntSequence = 0, - padding_mode: PaddingMode = 'zeros', - output_padding: int | IntSequence | PaddingSizeStr = 0, - dilation: int | IntSequence = 1, - groups: int = 1, - weight_attr: Any | None = None, - bias_attr: Any | None = None, - data_format: DataLayoutND = "NCHW", - ) -> None: + in_channels, + out_channels, + kernel_size, + transposed, + dims, + stride=1, + padding=0, + padding_mode='zeros', + output_padding=0, + dilation=1, + groups=1, + weight_attr=None, + bias_attr=None, + data_format="NCHW", + ): super().__init__() assert ( weight_attr is not False @@ -162,7 +142,7 @@ def __init__( in_channels // groups, ] + self._kernel_size - def _get_default_param_initializer() -> None: + def _get_default_param_initializer(): if transposed: return None filter_elem_num = np.prod(self._kernel_size) * self._in_channels @@ -206,7 +186,7 @@ def _get_default_param_initializer() -> None: ): self._use_cudnn = False - def extra_repr(self) -> str: + def extra_repr(self): main_str = '{_in_channels}, {_out_channels}, kernel_size={_kernel_size}' if self._stride != [1] * len(self._stride): main_str += ', stride={_stride}' @@ -348,18 +328,18 @@ class Conv1D(_ConvNd): def __init__( self, - in_channels: int, - out_channels: int, - kernel_size: int | IntSequence, - stride: int | IntSequence = 1, - padding: int | IntSequence | PaddingSizeStr = 0, - dilation: int | IntSequence = 1, - groups: int = 1, - padding_mode: PaddingMode = 'zeros', - weight_attr: ParamAttr | None = None, - bias_attr: ParamAttr | bool | None = None, - data_format: DataLayout1D = "NCL", - ) -> None: + in_channels, + out_channels, + kernel_size, + stride=1, + padding=0, + dilation=1, + groups=1, + padding_mode='zeros', + weight_attr=None, + bias_attr=None, + data_format="NCL", + ): super().__init__( in_channels, out_channels, @@ -376,7 +356,7 @@ def __init__( data_format=data_format, ) - def forward(self, x: paddle.Tensor) -> paddle.Tensor: + def forward(self, x): padding = 0 if self._padding_mode != "zeros": x = F.pad( @@ -535,18 +515,18 @@ class Conv1DTranspose(_ConvNd): def __init__( self, - in_channels: int, - out_channels: int, - kernel_size: int | IntSequence, - stride: int | IntSequence = 1, - padding: int | IntSequence | PaddingSizeStr = 0, - output_padding: int | IntSequence = 0, - groups: int = 1, - dilation: int | IntSequence = 1, - weight_attr: ParamAttr | None = None, - bias_attr: ParamAttr | bool | None = None, - data_format: DataLayout1D = "NCL", - ) -> None: + in_channels, + out_channels, + kernel_size, + stride=1, + padding=0, + output_padding=0, + groups=1, + dilation=1, + weight_attr=None, + bias_attr=None, + data_format="NCL", + ): super().__init__( in_channels, out_channels, @@ -563,7 +543,7 @@ def __init__( data_format=data_format, ) - def forward(self, x: paddle.Tensor, output_size: ShapeLike | None = None) -> paddle.Tensor: + def forward(self, x, output_size=None): out = F.conv1d_transpose( x, self.weight, @@ -691,18 +671,18 @@ class Conv2D(_ConvNd): def __init__( self, - in_channels: int, - out_channels: int, - kernel_size: int | IntSequence, - stride: int | IntSequence = 1, - padding: int | IntSequence | PaddingSizeStr = 0, - dilation: int | IntSequence = 1, - groups: int = 1, - padding_mode: PaddingMode = 'zeros', - weight_attr: ParamAttr | None = None, - bias_attr: ParamAttr | bool | None = None, - data_format: DataLayout2D = "NCHW", - ) -> None: + in_channels, + out_channels, + kernel_size, + stride=1, + padding=0, + dilation=1, + groups=1, + padding_mode='zeros', + weight_attr=None, + bias_attr=None, + data_format="NCHW", + ): super().__init__( in_channels, out_channels, @@ -719,7 +699,7 @@ def __init__( data_format=data_format, ) - def forward(self, x: paddle.Tensor) -> paddle.Tensor: + def forward(self, x): if self._padding_mode != 'zeros': x = F.pad( x, @@ -866,18 +846,18 @@ class Conv2DTranspose(_ConvNd): def __init__( self, - in_channels: int, - out_channels: int, - kernel_size: int | IntSequence, - stride: int | IntSequence = 1, - padding: int | IntSequence | PaddingSizeStr = 0, - output_padding: int | IntSequence = 0, - dilation: int | IntSequence = 1, - groups: int = 1, - weight_attr: ParamAttr | None = None, - bias_attr: ParamAttr | bool | None = None, - data_format: DataLayout2D = "NCHW", - ) -> None: + in_channels, + out_channels, + kernel_size, + stride=1, + padding=0, + output_padding=0, + dilation=1, + groups=1, + weight_attr=None, + bias_attr=None, + data_format="NCHW", + ): super().__init__( in_channels, out_channels, @@ -894,7 +874,7 @@ def __init__( data_format=data_format, ) - def forward(self, x: paddle.Tensor, output_size: ShapeLike | None = None) -> paddle.Tensor: + def forward(self, x, output_size=None): if output_size is None: output_padding = self.output_padding else: @@ -1023,18 +1003,18 @@ class Conv3D(_ConvNd): def __init__( self, - in_channels: int, - out_channels: int, - kernel_size: int | IntSequence, - stride: int | IntSequence = 1, - padding: int | IntSequence | PaddingSizeStr = 0, - dilation: int | IntSequence = 1, - groups: int = 1, - padding_mode: PaddingMode = 'zeros', - weight_attr: ParamAttr | None = None, - bias_attr: ParamAttr | bool | None = None, - data_format: DataLayout3D = "NCDHW", - ) -> None: + in_channels, + out_channels, + kernel_size, + stride=1, + padding=0, + dilation=1, + groups=1, + padding_mode='zeros', + weight_attr=None, + bias_attr=None, + data_format="NCDHW", + ): super().__init__( in_channels, out_channels, @@ -1051,7 +1031,7 @@ def __init__( data_format=data_format, ) - def forward(self, x: paddle.Tensor) -> paddle.Tensor: + def forward(self, x): if self._padding_mode != 'zeros': x = F.pad( x, @@ -1202,18 +1182,18 @@ class Conv3DTranspose(_ConvNd): def __init__( self, - in_channels: int, - out_channels: int, - kernel_size: int | IntSequence, - stride: int | IntSequence = 1, - padding: int | IntSequence | PaddingSizeStr = 0, - output_padding: int | IntSequence = 0, - dilation: int | IntSequence = 1, - groups: int = 1, - weight_attr: ParamAttr | None = None, - bias_attr: ParamAttr | bool | None = None, - data_format: DataLayout3D = "NCDHW", - ) -> None: + in_channels, + out_channels, + kernel_size, + stride=1, + padding=0, + output_padding=0, + dilation=1, + groups=1, + weight_attr=None, + bias_attr=None, + data_format="NCDHW", + ): super().__init__( in_channels, out_channels, @@ -1230,7 +1210,7 @@ def __init__( data_format=data_format, ) - def forward(self, x: paddle.Tensor, output_size: ShapeLike | None = None) -> paddle.Tensor: + def forward(self, x, output_size=None): if output_size is None: output_padding = self.output_padding else: From ee8d5d6c755896bfdb4ad2da5c0d101738a3f187 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 29 Jun 2024 00:01:15 +0800 Subject: [PATCH 6/7] annotate conv layer --- python/paddle/nn/functional/common.py | 2 +- python/paddle/nn/functional/conv.py | 31 ++-- python/paddle/nn/layer/conv.py | 244 ++++++++++++++------------ 3 files changed, 152 insertions(+), 125 deletions(-) diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index 99e8c6b4354c5..d6d08c05208ee 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -63,7 +63,7 @@ ] _DropoutMode: TypeAlias = Literal['upscale_in_train', 'downscale_in_infer'] _PaddingTensorMode: TypeAlias = Literal[ - "constant", "reflect", "replicate", "circular" + "zeros", "constant", "reflect", "replicate", "circular" ] _PaddingSizeMode: TypeAlias = Literal[ # noqa: PYI047 'valid', 'same', 'VALID', 'SAME' diff --git a/python/paddle/nn/functional/conv.py b/python/paddle/nn/functional/conv.py index e48cc13ba330b..3a83dbd3965ad 100644 --- a/python/paddle/nn/functional/conv.py +++ b/python/paddle/nn/functional/conv.py @@ -26,6 +26,7 @@ DataLayout1D, DataLayout2D, DataLayout3D, + DataLayoutND, Size1, Size2, Size3, @@ -127,20 +128,20 @@ def _update_padding_nd(padding, channel_last, num_dims): def _conv_nd( - x, - weight, - bias=None, - stride=1, - padding=0, + x: Tensor, + weight: Tensor, + bias: Tensor | None = None, + stride: int | Sequence[int] = 1, + padding: _PaddingSizeMode | int | Sequence[int] | Sequence[Size2] = 0, padding_algorithm=None, - dilation=1, - groups=1, - data_format="NCHW", - channel_dim=1, - op_type="conv2d", - use_cudnn=True, - name=None, -): + dilation: int | Sequence[int] = 1, + groups: int = 1, + data_format: DataLayoutND = "NCHW", + channel_dim: int = 1, + op_type: str = "conv2d", + use_cudnn: bool = True, + name: str | None = None, +) -> Tensor: # Due to the poor performance of NHWC, we transpose the input to NCHW. if in_dynamic_or_pir_mode() and op_type == "conv2d": pre_bias = _C_ops.conv2d( @@ -777,7 +778,7 @@ def conv1d_transpose( weight: Tensor, bias: Tensor | None = None, stride: Size1 = 1, - padding: _PaddingSizeMode | Size1 | Size2 = 0, + padding: _PaddingSizeMode | Size1 | Size2 | Sequence[Size2] = 0, output_padding: Size1 = 0, groups: int = 1, dilation: Size1 = 1, @@ -1356,7 +1357,7 @@ def conv3d( weight: Tensor, bias: Tensor | None = None, stride: Size3 = 1, - padding=0, + padding: _PaddingSizeMode | Size3 | Size6 | Sequence[Size2] = 0, dilation: Size3 = 1, groups: int = 1, data_format: DataLayout3D = "NCDHW", diff --git a/python/paddle/nn/layer/conv.py b/python/paddle/nn/layer/conv.py index 2990969ef0503..4e2fb99d6fcdc 100644 --- a/python/paddle/nn/layer/conv.py +++ b/python/paddle/nn/layer/conv.py @@ -12,11 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -# TODO: define classes of convolutional neural network +from __future__ import annotations + +from typing import TYPE_CHECKING, Sequence import numpy as np from paddle import get_flags +from paddle.tensor.tensor import Tensor from ...device import ( get_cudnn_version, @@ -29,6 +32,24 @@ from ..initializer import Normal from .layers import Layer +if TYPE_CHECKING: + from paddle import Tensor + from paddle._typing import ( + DataLayout1D, + DataLayout2D, + DataLayout3D, + DataLayoutND, + ParamAttrLike, + Size1, + Size2, + Size3, + Size4, + Size6, + ) + + from ..functional.common import _PaddingSizeMode, _PaddingTensorMode + + __all__ = [] @@ -47,23 +68,28 @@ def _reverse_repeat_list(t, n): class _ConvNd(Layer): + weight: Tensor + bias: Tensor + def __init__( self, - in_channels, - out_channels, - kernel_size, - transposed, - dims, - stride=1, - padding=0, - padding_mode='zeros', - output_padding=0, - dilation=1, - groups=1, - weight_attr=None, - bias_attr=None, - data_format="NCHW", - ): + in_channels: int, + out_channels: int, + kernel_size: int | Sequence[int], + transposed: bool, + dims: int, + stride: int | Sequence[int] = 1, + padding: _PaddingSizeMode | int | Sequence[int] | Sequence[Size2] = 0, + padding_mode: _PaddingTensorMode = 'zeros', + output_padding: ( + _PaddingSizeMode | int | Sequence[int] | Sequence[Size2] + ) = 0, + dilation: int | Sequence[int] = 1, + groups: int = 1, + weight_attr: ParamAttrLike | None = None, + bias_attr: ParamAttrLike | None = None, + data_format: DataLayoutND = "NCHW", + ) -> None: super().__init__() assert ( weight_attr is not False @@ -328,18 +354,18 @@ class Conv1D(_ConvNd): def __init__( self, - in_channels, - out_channels, - kernel_size, - stride=1, - padding=0, - dilation=1, - groups=1, - padding_mode='zeros', - weight_attr=None, - bias_attr=None, - data_format="NCL", - ): + in_channels: int, + out_channels: int, + kernel_size: Size1, + stride: Size1 = 1, + padding: _PaddingSizeMode | Size1 | Size2 | Sequence[Size2] = 0, + dilation: Size1 = 1, + groups: int = 1, + padding_mode: _PaddingTensorMode = 'zeros', + weight_attr: ParamAttrLike | None = None, + bias_attr: ParamAttrLike | None = None, + data_format: DataLayout1D = "NCL", + ) -> None: super().__init__( in_channels, out_channels, @@ -356,7 +382,7 @@ def __init__( data_format=data_format, ) - def forward(self, x): + def forward(self, x: Tensor) -> Tensor: padding = 0 if self._padding_mode != "zeros": x = F.pad( @@ -430,12 +456,12 @@ class Conv1DTranspose(_ConvNd): L_{out} &\in [ L^\prime_{out}, L^\prime_{out} + stride ] Note: - The conv1d_transpose can be seen as the backward of the conv1d. For conv1d, - when stride > 1, conv1d maps multiple input shape to the same output shape, - so for conv1d_transpose, when stride > 1, input shape maps multiple output shape. - If output_size is None, :math:`L_{out} = L^\prime_{out}`; - else, the :math:`L_{out}` of the output size must between :math:`L^\prime_{out}` - and :math:`L^\prime_{out} + stride`. + The conv1d_transpose can be seen as the backward of the conv1d. For conv1d, + when stride > 1, conv1d maps multiple input shape to the same output shape, + so for conv1d_transpose, when stride > 1, input shape maps multiple output shape. + If output_size is None, :math:`L_{out} = L^\prime_{out}`; + else, the :math:`L_{out}` of the output size must between :math:`L^\prime_{out}` + and :math:`L^\prime_{out} + stride`. Args: in_channels(int): The number of channels in the input image. @@ -449,12 +475,12 @@ class Conv1DTranspose(_ConvNd): If stride is a tuple/list, it must contain one integer, (stride_size). Default: stride = 1. padding(int|list|str|tuple, optional): The padding size. The padding argument effectively adds - `dilation * (kernel - 1)` amount of zero-padding on both sides of input. If `padding` is a - string, either 'VALID' or 'SAME' supported, which is the padding algorithm. - If `padding` is a tuple or list, it could be in two forms: - `[pad]` or `[pad_left, pad_right]`. Default: padding = 0. + `dilation * (kernel - 1)` amount of zero-padding on both sides of input. If `padding` is a + string, either 'VALID' or 'SAME' supported, which is the padding algorithm. + If `padding` is a tuple or list, it could be in two forms: + `[pad]` or `[pad_left, pad_right]`. Default: padding = 0. output_padding(int|list|tuple, optional): The count of zeros to be added to tail of each dimension. - If it is a tuple/list, it must contain one integer. Default: 0. + If it is a tuple/list, it must contain one integer. Default: 0. groups(int, optional): The groups number of the Conv2D transpose layer. Inspired by grouped convolution in Alex Krizhevsky's Deep CNN paper, in which when group=2, the first half of the filters is only connected to the @@ -488,7 +514,7 @@ class Conv1DTranspose(_ConvNd): - output(Tensor): 3-D tensor with same shape as input x. Examples: - .. code-block:: python + .. code-block:: python >>> import paddle >>> from paddle.nn import Conv1DTranspose @@ -515,18 +541,18 @@ class Conv1DTranspose(_ConvNd): def __init__( self, - in_channels, - out_channels, - kernel_size, - stride=1, - padding=0, - output_padding=0, - groups=1, - dilation=1, - weight_attr=None, - bias_attr=None, - data_format="NCL", - ): + in_channels: int, + out_channels: int, + kernel_size: Size1, + stride: Size1 = 1, + padding: _PaddingSizeMode | Size1 | Size2 | Sequence[Size2] = 0, + output_padding: _PaddingSizeMode | Size1 | Size2 | Sequence[Size2] = 0, + groups: int = 1, + dilation: Size1 = 1, + weight_attr: ParamAttrLike | None = None, + bias_attr: ParamAttrLike | None = None, + data_format: DataLayout1D = "NCL", + ) -> None: super().__init__( in_channels, out_channels, @@ -543,7 +569,7 @@ def __init__( data_format=data_format, ) - def forward(self, x, output_size=None): + def forward(self, x: Tensor, output_size: Size1 | None = None) -> Tensor: out = F.conv1d_transpose( x, self.weight, @@ -671,18 +697,18 @@ class Conv2D(_ConvNd): def __init__( self, - in_channels, - out_channels, - kernel_size, - stride=1, - padding=0, - dilation=1, - groups=1, - padding_mode='zeros', - weight_attr=None, - bias_attr=None, - data_format="NCHW", - ): + in_channels: int, + out_channels: int, + kernel_size: Size2, + stride: Size2 = 1, + padding: _PaddingSizeMode | Size2 | Size4 | Sequence[Size2] = 0, + dilation: Size2 = 1, + groups: int = 1, + padding_mode: _PaddingTensorMode = 'zeros', + weight_attr: ParamAttrLike | None = None, + bias_attr: ParamAttrLike | None = None, + data_format: DataLayout2D = "NCHW", + ) -> None: super().__init__( in_channels, out_channels, @@ -699,7 +725,7 @@ def __init__( data_format=data_format, ) - def forward(self, x): + def forward(self, x: Tensor) -> Tensor: if self._padding_mode != 'zeros': x = F.pad( x, @@ -758,7 +784,7 @@ class Conv2DTranspose(_ConvNd): * :math:`Out`: Output value, a 4-D ``Tensor`` with NCHW or NHWC format, the shape of :math:`Out` and :math:`X` may be different. Note: - If output_size is None, :math:`H_{out}` = :math:`H^\prime_{out}` , :math:`W_{out}` = :math:`W^\prime_{out}`. Otherwise, the specified output_size_height (the height of the output feature layer) :math:`H_{out}` should be between :math:`H^\prime_{out}` and :math:`H^\prime_{out} + strides[0]` (excluding :math:`H^\prime_{out} + strides[0]` ). + If output_size is None, :math:`H_{out}` = :math:`H^\prime_{out}` , :math:`W_{out}` = :math:`W^\prime_{out}`. Otherwise, the specified output_size_height (the height of the output feature layer) :math:`H_{out}` should be between :math:`H^\prime_{out}` and :math:`H^\prime_{out} + strides[0]` (excluding :math:`H^\prime_{out} + strides[0]` ). Parameters: in_channels(int): The number of channels in the input image. @@ -829,7 +855,7 @@ class Conv2DTranspose(_ConvNd): Examples: - .. code-block:: python + .. code-block:: python >>> import paddle >>> import paddle.nn as nn @@ -846,18 +872,18 @@ class Conv2DTranspose(_ConvNd): def __init__( self, - in_channels, - out_channels, - kernel_size, - stride=1, - padding=0, - output_padding=0, - dilation=1, - groups=1, - weight_attr=None, - bias_attr=None, - data_format="NCHW", - ): + in_channels: int, + out_channels: int, + kernel_size: Size2, + stride: Size2 = 1, + padding: _PaddingSizeMode | Size2 | Size4 | Sequence[Size2] = 0, + output_padding: _PaddingSizeMode | Size2 | Size4 | Sequence[Size2] = 0, + dilation: Size2 = 1, + groups: int = 1, + weight_attr: ParamAttrLike | None = None, + bias_attr: ParamAttrLike | None = None, + data_format: DataLayout2D = "NCHW", + ) -> None: super().__init__( in_channels, out_channels, @@ -874,7 +900,7 @@ def __init__( data_format=data_format, ) - def forward(self, x, output_size=None): + def forward(self, x: Tensor, output_size: Size2 | None = None) -> Tensor: if output_size is None: output_padding = self.output_padding else: @@ -1003,18 +1029,18 @@ class Conv3D(_ConvNd): def __init__( self, - in_channels, - out_channels, - kernel_size, - stride=1, - padding=0, - dilation=1, - groups=1, - padding_mode='zeros', - weight_attr=None, - bias_attr=None, - data_format="NCDHW", - ): + in_channels: int, + out_channels: int, + kernel_size: Size3, + stride: Size3 = 1, + padding: _PaddingSizeMode | Size3 | Size6 | Sequence[Size2] = 0, + dilation: Size3 = 1, + groups: int = 1, + padding_mode: _PaddingTensorMode = 'zeros', + weight_attr: ParamAttrLike | None = None, + bias_attr: ParamAttrLike | None = None, + data_format: DataLayout3D = "NCDHW", + ) -> None: super().__init__( in_channels, out_channels, @@ -1031,7 +1057,7 @@ def __init__( data_format=data_format, ) - def forward(self, x): + def forward(self, x: Tensor) -> Tensor: if self._padding_mode != 'zeros': x = F.pad( x, @@ -1165,7 +1191,7 @@ class Conv3DTranspose(_ConvNd): Examples: - .. code-block:: python + .. code-block:: python >>> import paddle >>> import paddle.nn as nn @@ -1182,18 +1208,18 @@ class Conv3DTranspose(_ConvNd): def __init__( self, - in_channels, - out_channels, - kernel_size, - stride=1, - padding=0, - output_padding=0, - dilation=1, - groups=1, - weight_attr=None, - bias_attr=None, - data_format="NCDHW", - ): + in_channels: int, + out_channels: int, + kernel_size: Size3, + stride: Size3 = 1, + padding: _PaddingSizeMode | Size3 | Size6 | Sequence[Size2] = 0, + output_padding: _PaddingSizeMode | Size3 | Size6 | Sequence[Size2] = 0, + dilation: Size3 = 1, + groups: int = 1, + weight_attr: ParamAttrLike | None = None, + bias_attr: ParamAttrLike | None = None, + data_format: DataLayout3D = "NCDHW", + ) -> None: super().__init__( in_channels, out_channels, @@ -1210,7 +1236,7 @@ def __init__( data_format=data_format, ) - def forward(self, x, output_size=None): + def forward(self, x: Tensor, output_size: Size3 | None = None) -> Tensor: if output_size is None: output_padding = self.output_padding else: From f29d247f962041d581f748997464bf163a97edc9 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 29 Jun 2024 00:25:08 +0800 Subject: [PATCH 7/7] remove auto import --- python/paddle/nn/layer/conv.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/nn/layer/conv.py b/python/paddle/nn/layer/conv.py index 4e2fb99d6fcdc..77d5194daadd4 100644 --- a/python/paddle/nn/layer/conv.py +++ b/python/paddle/nn/layer/conv.py @@ -19,7 +19,6 @@ import numpy as np from paddle import get_flags -from paddle.tensor.tensor import Tensor from ...device import ( get_cudnn_version,