Skip to content

Commit 8b19229

Browse files
【Hackathon 6th Fundable Projects 1 1-1】Add _typing module to paddle (PaddlePaddle#63604)
--------- Co-authored-by: SigureMo <[email protected]>
1 parent c41571b commit 8b19229

16 files changed

+384
-10
lines changed

.github/CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ python/paddle/distributed/fleet/launch.py @sneaxiy @raindrops2sea
6767
python/paddle/distributed/__init__.py @sneaxiy @raindrops2sea
6868
python/paddle/incubate/autograd/composite_rules.py @cyber-pioneer @xiaoguoguo626807 @Charles-hit @JiabinYang
6969
python/paddle/incubate/autograd/primitives.py @cyber-pioneer @xiaoguoguo626807 @Charles-hit @JiabinYang
70+
python/paddle/_typing @SigureMo @zrr1999 @gouzil
7071
python/requirements.txt @phlrain @jzhang533 @kolinwei
7172
test/dygraph_to_static @SigureMo @Aurelius84 @gouzil
7273
test/sot @SigureMo @Aurelius84 @gouzil

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ repos:
4444
name: copyright_checker
4545
entry: python ./tools/codestyle/copyright.py
4646
language: system
47-
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|xpu|kps|py|sh)$
47+
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|xpu|kps|py|pyi|sh)$
4848
exclude: |
4949
(?x)^(
5050
paddle/utils/.*|

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ select = [
7171

7272
# Pylint
7373
"PLE",
74-
"PLC0414",
7574
"PLC3002",
7675
"PLR0206",
7776
"PLR0402",
@@ -119,6 +118,8 @@ combine-as-imports = true
119118
known-first-party = ["paddle"]
120119

121120
[tool.ruff.lint.per-file-ignores]
121+
# Ignore for re-export in __init__ files
122+
"__init__.py" = ["PLC0414"]
122123
# Ignore compare with True in sot unittest
123124
"test/sot/test_dup_top.py" = ["E712"]
124125
# Ignore undefined variables in CMake config and some dygraph_to_static tests

python/paddle/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
# high-level api
105105
from . import ( # noqa: F401
106106
_pir_ops,
107+
_typing as _typing,
107108
callbacks,
108109
fft,
109110
hub,

python/paddle/_typing/__init__.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Basic
16+
from .basic import (
17+
IntSequence as IntSequence,
18+
NestedNumbericSequence as NestedNumbericSequence,
19+
NestedSequence as NestedSequence,
20+
Numberic as Numberic,
21+
NumbericSequence as NumbericSequence,
22+
TensorOrTensors as TensorOrTensors,
23+
)
24+
25+
# Device
26+
from .device_like import (
27+
PlaceLike as PlaceLike,
28+
)
29+
30+
# DType
31+
from .dtype_like import DTypeLike as DTypeLike
32+
33+
# DataLayout
34+
from .layout import (
35+
DataLayout0D as DataLayout0D,
36+
DataLayout1D as DataLayout1D,
37+
DataLayout1DVariant as DataLayout1DVariant,
38+
DataLayout2D as DataLayout2D,
39+
DataLayout3D as DataLayout3D,
40+
DataLayoutImage as DataLayoutImage,
41+
DataLayoutND as DataLayoutND,
42+
)
43+
44+
# Shape
45+
from .shape import (
46+
DynamicShapeLike as DynamicShapeLike,
47+
ShapeLike as ShapeLike,
48+
Size1 as Size1,
49+
Size2 as Size2,
50+
Size3 as Size3,
51+
Size4 as Size4,
52+
Size5 as Size5,
53+
Size6 as Size6,
54+
SizeN as SizeN,
55+
)

python/paddle/_typing/basic.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
from typing import TYPE_CHECKING, Sequence, TypeVar, Union
17+
18+
import numpy as np
19+
from typing_extensions import TypeAlias
20+
21+
if TYPE_CHECKING:
22+
from paddle import Tensor
23+
24+
Numberic: TypeAlias = Union[int, float, complex, np.number, "Tensor"]
25+
26+
_T = TypeVar("_T", bound=Numberic)
27+
_SeqLevel1: TypeAlias = Sequence[_T]
28+
_SeqLevel2: TypeAlias = Sequence[Sequence[_T]]
29+
_SeqLevel3: TypeAlias = Sequence[Sequence[Sequence[_T]]]
30+
_SeqLevel4: TypeAlias = Sequence[Sequence[Sequence[Sequence[_T]]]]
31+
_SeqLevel5: TypeAlias = Sequence[Sequence[Sequence[Sequence[Sequence[_T]]]]]
32+
_SeqLevel6: TypeAlias = Sequence[
33+
Sequence[Sequence[Sequence[Sequence[Sequence[_T]]]]]
34+
]
35+
36+
IntSequence: TypeAlias = _SeqLevel1[int]
37+
38+
NumbericSequence: TypeAlias = _SeqLevel1[Numberic]
39+
40+
NestedSequence: TypeAlias = Union[
41+
_T,
42+
_SeqLevel1[_T],
43+
_SeqLevel2[_T],
44+
_SeqLevel3[_T],
45+
_SeqLevel4[_T],
46+
_SeqLevel5[_T],
47+
_SeqLevel6[_T],
48+
]
49+
50+
NestedNumbericSequence: TypeAlias = NestedSequence[Numberic]
51+
52+
TensorOrTensors: TypeAlias = Union["Tensor", Sequence["Tensor"]]

python/paddle/_typing/device_like.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
from typing import TYPE_CHECKING, Union
17+
18+
from typing_extensions import TypeAlias
19+
20+
if TYPE_CHECKING:
21+
from paddle import (
22+
CPUPlace,
23+
CUDAPinnedPlace,
24+
CUDAPlace,
25+
CustomPlace,
26+
IPUPlace,
27+
XPUPlace,
28+
)
29+
30+
PlaceLike: TypeAlias = Union[
31+
"CPUPlace",
32+
"CUDAPlace",
33+
"CUDAPinnedPlace",
34+
"IPUPlace",
35+
"CustomPlace",
36+
"XPUPlace",
37+
str, # some string like "cpu", "gpu:0", etc.
38+
]

python/paddle/_typing/dtype_like.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
from typing import TYPE_CHECKING, Literal, Type, Union
17+
18+
import numpy as np
19+
from typing_extensions import TypeAlias
20+
21+
if TYPE_CHECKING:
22+
from paddle import dtype
23+
24+
_DTypeLiteral: TypeAlias = Literal[
25+
"uint8",
26+
"int8",
27+
"int16",
28+
"int32",
29+
"int64",
30+
"float32",
31+
"float64",
32+
"float16",
33+
"bfloat16",
34+
"complex64",
35+
"complex128",
36+
"bool",
37+
]
38+
39+
_DTypeNumpy: TypeAlias = Union[
40+
Type[
41+
Union[
42+
np.uint8,
43+
np.int8,
44+
np.int16,
45+
np.int32,
46+
np.int64,
47+
np.float16,
48+
np.float32,
49+
np.float64,
50+
np.complex64,
51+
np.complex128,
52+
np.bool_,
53+
]
54+
],
55+
np.dtype,
56+
]
57+
58+
59+
DTypeLike: TypeAlias = Union["dtype", _DTypeNumpy, _DTypeLiteral]

python/paddle/_typing/layout.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
from typing import Literal, Union
17+
18+
from typing_extensions import TypeAlias
19+
20+
# Note: Do not confrom to predefined naming style in pylint.
21+
DataLayout0D: TypeAlias = Literal["NC"]
22+
DataLayout1D: TypeAlias = Literal["NCL", "NLC"]
23+
DataLayout2D: TypeAlias = Literal["NCHW", "NHCW"]
24+
DataLayout3D: TypeAlias = Literal["NCDHW", "NDHWC"]
25+
26+
DataLayoutND: TypeAlias = Union[
27+
DataLayout0D,
28+
DataLayout1D,
29+
DataLayout2D,
30+
DataLayout3D,
31+
]
32+
33+
DataLayout1DVariant: TypeAlias = Literal["NCW", "NWC"]
34+
DataLayoutImage: TypeAlias = Literal["HWC", "CHW"]

python/paddle/_typing/shape.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
from typing import List, Tuple, Union
17+
18+
from typing_extensions import TypeAlias
19+
20+
from .. import Tensor
21+
22+
DynamicShapeLike: TypeAlias = Union[
23+
Tuple[Union[int, Tensor, None], ...],
24+
List[Union[int, Tensor, None]],
25+
Tensor,
26+
]
27+
28+
29+
ShapeLike: TypeAlias = Union[
30+
Tuple[int, ...],
31+
List[int],
32+
Tensor,
33+
]
34+
35+
36+
# for size parameters, eg, kernel_size, stride ...
37+
Size1: TypeAlias = Union[int, Tuple[int], List[int]]
38+
Size2: TypeAlias = Union[int, Tuple[int, int], List[int]]
39+
Size3: TypeAlias = Union[int, Tuple[int, int, int], List[int]]
40+
Size4: TypeAlias = Union[int, Tuple[int, int, int, int], List[int]]
41+
Size5: TypeAlias = Union[int, Tuple[int, int, int, int, int], List[int]]
42+
Size6: TypeAlias = Union[int, Tuple[int, int, int, int, int, int], List[int]]
43+
SizeN: TypeAlias = Union[int, Tuple[int, ...], List[int]]

python/paddle/base/core.pyi

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
class CPUPlace: ...
16+
17+
class CUDAPlace:
18+
def __init__(self, id: int, /) -> None: ...
19+
20+
class CUDAPinnedPlace: ...
21+
22+
class NPUPlace:
23+
def __init__(self, id: int, /) -> None: ...
24+
25+
class IPUPlace: ...
26+
27+
class CustomPlace:
28+
def __init__(self, name: str, id: int, /) -> None: ...
29+
30+
class MLUPlace:
31+
def __init__(self, id: int, /) -> None: ...
32+
33+
class XPUPlace:
34+
def __init__(self, id: int, /) -> None: ...

python/paddle/framework/dtype.pyi

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
class dtype: ...
16+
17+
uint8: dtype
18+
int8: dtype
19+
int16: dtype
20+
int32: dtype
21+
int64: dtype
22+
23+
float32: dtype
24+
float64: dtype
25+
float16: dtype
26+
bfloat16: dtype
27+
28+
complex64: dtype
29+
complex128: dtype
30+
31+
bool: dtype

python/paddle/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)