Skip to content

Commit

Permalink
【Hackathon 6th Fundable Projects 1 1-1】Add _typing module to paddle (
Browse files Browse the repository at this point in the history
…PaddlePaddle#63604)


---------

Co-authored-by: SigureMo <[email protected]>
  • Loading branch information
2 people authored and co63oc committed May 13, 2024
1 parent 7f7fac6 commit 2040178
Show file tree
Hide file tree
Showing 16 changed files with 384 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ python/paddle/distributed/fleet/launch.py @sneaxiy @raindrops2sea
python/paddle/distributed/__init__.py @sneaxiy @raindrops2sea
python/paddle/incubate/autograd/composite_rules.py @cyber-pioneer @xiaoguoguo626807 @Charles-hit @JiabinYang
python/paddle/incubate/autograd/primitives.py @cyber-pioneer @xiaoguoguo626807 @Charles-hit @JiabinYang
python/paddle/_typing @SigureMo @zrr1999 @gouzil
python/requirements.txt @phlrain @jzhang533 @kolinwei
test/dygraph_to_static @SigureMo @Aurelius84 @gouzil
test/sot @SigureMo @Aurelius84 @gouzil
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ repos:
name: copyright_checker
entry: python ./tools/codestyle/copyright.py
language: system
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|xpu|kps|py|sh)$
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|xpu|kps|py|pyi|sh)$
exclude: |
(?x)^(
paddle/utils/.*|
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ select = [

# Pylint
"PLE",
"PLC0414",
"PLC3002",
"PLR0206",
"PLR0402",
Expand Down Expand Up @@ -119,6 +118,8 @@ combine-as-imports = true
known-first-party = ["paddle"]

[tool.ruff.lint.per-file-ignores]
# Ignore for re-export in __init__ files
"__init__.py" = ["PLC0414"]
# Ignore compare with True in sot unittest
"test/sot/test_dup_top.py" = ["E712"]
# Ignore undefined variables in CMake config and some dygraph_to_static tests
Expand Down
1 change: 1 addition & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
# high-level api
from . import ( # noqa: F401
_pir_ops,
_typing as _typing,
callbacks,
fft,
hub,
Expand Down
55 changes: 55 additions & 0 deletions python/paddle/_typing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Basic
from .basic import (
IntSequence as IntSequence,
NestedNumbericSequence as NestedNumbericSequence,
NestedSequence as NestedSequence,
Numberic as Numberic,
NumbericSequence as NumbericSequence,
TensorOrTensors as TensorOrTensors,
)

# Device
from .device_like import (
PlaceLike as PlaceLike,
)

# DType
from .dtype_like import DTypeLike as DTypeLike

# DataLayout
from .layout import (
DataLayout0D as DataLayout0D,
DataLayout1D as DataLayout1D,
DataLayout1DVariant as DataLayout1DVariant,
DataLayout2D as DataLayout2D,
DataLayout3D as DataLayout3D,
DataLayoutImage as DataLayoutImage,
DataLayoutND as DataLayoutND,
)

# Shape
from .shape import (
DynamicShapeLike as DynamicShapeLike,
ShapeLike as ShapeLike,
Size1 as Size1,
Size2 as Size2,
Size3 as Size3,
Size4 as Size4,
Size5 as Size5,
Size6 as Size6,
SizeN as SizeN,
)
52 changes: 52 additions & 0 deletions python/paddle/_typing/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

from typing import TYPE_CHECKING, Sequence, TypeVar, Union

import numpy as np
from typing_extensions import TypeAlias

if TYPE_CHECKING:
from paddle import Tensor

Numberic: TypeAlias = Union[int, float, complex, np.number, "Tensor"]

_T = TypeVar("_T", bound=Numberic)
_SeqLevel1: TypeAlias = Sequence[_T]
_SeqLevel2: TypeAlias = Sequence[Sequence[_T]]
_SeqLevel3: TypeAlias = Sequence[Sequence[Sequence[_T]]]
_SeqLevel4: TypeAlias = Sequence[Sequence[Sequence[Sequence[_T]]]]
_SeqLevel5: TypeAlias = Sequence[Sequence[Sequence[Sequence[Sequence[_T]]]]]
_SeqLevel6: TypeAlias = Sequence[
Sequence[Sequence[Sequence[Sequence[Sequence[_T]]]]]
]

IntSequence: TypeAlias = _SeqLevel1[int]

NumbericSequence: TypeAlias = _SeqLevel1[Numberic]

NestedSequence: TypeAlias = Union[
_T,
_SeqLevel1[_T],
_SeqLevel2[_T],
_SeqLevel3[_T],
_SeqLevel4[_T],
_SeqLevel5[_T],
_SeqLevel6[_T],
]

NestedNumbericSequence: TypeAlias = NestedSequence[Numberic]

TensorOrTensors: TypeAlias = Union["Tensor", Sequence["Tensor"]]
38 changes: 38 additions & 0 deletions python/paddle/_typing/device_like.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

from typing import TYPE_CHECKING, Union

from typing_extensions import TypeAlias

if TYPE_CHECKING:
from paddle import (
CPUPlace,
CUDAPinnedPlace,
CUDAPlace,
CustomPlace,
IPUPlace,
XPUPlace,
)

PlaceLike: TypeAlias = Union[
"CPUPlace",
"CUDAPlace",
"CUDAPinnedPlace",
"IPUPlace",
"CustomPlace",
"XPUPlace",
str, # some string like "cpu", "gpu:0", etc.
]
59 changes: 59 additions & 0 deletions python/paddle/_typing/dtype_like.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

from typing import TYPE_CHECKING, Literal, Type, Union

import numpy as np
from typing_extensions import TypeAlias

if TYPE_CHECKING:
from paddle import dtype

_DTypeLiteral: TypeAlias = Literal[
"uint8",
"int8",
"int16",
"int32",
"int64",
"float32",
"float64",
"float16",
"bfloat16",
"complex64",
"complex128",
"bool",
]

_DTypeNumpy: TypeAlias = Union[
Type[
Union[
np.uint8,
np.int8,
np.int16,
np.int32,
np.int64,
np.float16,
np.float32,
np.float64,
np.complex64,
np.complex128,
np.bool_,
]
],
np.dtype,
]


DTypeLike: TypeAlias = Union["dtype", _DTypeNumpy, _DTypeLiteral]
34 changes: 34 additions & 0 deletions python/paddle/_typing/layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

from typing import Literal, Union

from typing_extensions import TypeAlias

# Note: Do not confrom to predefined naming style in pylint.
DataLayout0D: TypeAlias = Literal["NC"]
DataLayout1D: TypeAlias = Literal["NCL", "NLC"]
DataLayout2D: TypeAlias = Literal["NCHW", "NHCW"]
DataLayout3D: TypeAlias = Literal["NCDHW", "NDHWC"]

DataLayoutND: TypeAlias = Union[
DataLayout0D,
DataLayout1D,
DataLayout2D,
DataLayout3D,
]

DataLayout1DVariant: TypeAlias = Literal["NCW", "NWC"]
DataLayoutImage: TypeAlias = Literal["HWC", "CHW"]
43 changes: 43 additions & 0 deletions python/paddle/_typing/shape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

from typing import List, Tuple, Union

from typing_extensions import TypeAlias

from .. import Tensor

DynamicShapeLike: TypeAlias = Union[
Tuple[Union[int, Tensor, None], ...],
List[Union[int, Tensor, None]],
Tensor,
]


ShapeLike: TypeAlias = Union[
Tuple[int, ...],
List[int],
Tensor,
]


# for size parameters, eg, kernel_size, stride ...
Size1: TypeAlias = Union[int, Tuple[int], List[int]]
Size2: TypeAlias = Union[int, Tuple[int, int], List[int]]
Size3: TypeAlias = Union[int, Tuple[int, int, int], List[int]]
Size4: TypeAlias = Union[int, Tuple[int, int, int, int], List[int]]
Size5: TypeAlias = Union[int, Tuple[int, int, int, int, int], List[int]]
Size6: TypeAlias = Union[int, Tuple[int, int, int, int, int, int], List[int]]
SizeN: TypeAlias = Union[int, Tuple[int, ...], List[int]]
34 changes: 34 additions & 0 deletions python/paddle/base/core.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

class CPUPlace: ...

class CUDAPlace:
def __init__(self, id: int, /) -> None: ...

class CUDAPinnedPlace: ...

class NPUPlace:
def __init__(self, id: int, /) -> None: ...

class IPUPlace: ...

class CustomPlace:
def __init__(self, name: str, id: int, /) -> None: ...

class MLUPlace:
def __init__(self, id: int, /) -> None: ...

class XPUPlace:
def __init__(self, id: int, /) -> None: ...
31 changes: 31 additions & 0 deletions python/paddle/framework/dtype.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

class dtype: ...

uint8: dtype
int8: dtype
int16: dtype
int32: dtype
int64: dtype

float32: dtype
float64: dtype
float16: dtype
bfloat16: dtype

complex64: dtype
complex128: dtype

bool: dtype
Empty file added python/paddle/py.typed
Empty file.
Loading

0 comments on commit 2040178

Please sign in to comment.