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

【Hackathon 6th Fundable Projects 1 1-1】Add _typing module to paddle #63604

Merged
merged 21 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 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",
Asthestarsfalll marked this conversation as resolved.
Show resolved Hide resolved
"PLC3002",
"PLR0206",
"PLR0402",
Expand Down
64 changes: 64 additions & 0 deletions python/paddle/_typing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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,
)

# Device
from .device_like import (
CPUPlace as CPUPlace,
CUDAPlace as CUDAPlace,
CustomPlace as CustomPlace,
IPUPlace as IPUPlace,
MLUPlace as MLUPlace,
NPUPlace as NPUPlace,
PlaceLike as PlaceLike,
XPUPlace as XPUPlace,
)

# 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,
DataLayoutSparseCOO as DataLayoutSparseCOO,
DataLayoutSparseCSR as DataLayoutSparseCSR,
DataLayoutStrided as DataLayoutStrided,
)

# 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,
)
50 changes: 50 additions & 0 deletions python/paddle/_typing/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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 typing import Sequence, TypeVar, Union

import numpy as np
from typing_extensions import TypeAlias
Asthestarsfalll marked this conversation as resolved.
Show resolved Hide resolved

from .. 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]]
48 changes: 48 additions & 0 deletions python/paddle/_typing/device_like.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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 typing import Protocol, Union

from typing_extensions import TypeAlias

from paddle import (
CPUPlace,
CUDAPinnedPlace,
CUDAPlace,
CustomPlace,
IPUPlace,
XPUPlace,
Asthestarsfalll marked this conversation as resolved.
Show resolved Hide resolved
)


class _Place(Protocol):
def __init__(self, id: int) -> None:
...


NPUPlace = _Place
MLUPlace = _Place
Copy link
Contributor

@megemini megemini May 9, 2024

Choose a reason for hiding this comment

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

xxx = _xxx 这样写有点问题,比如:

from __future__ import annotations

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

NPUPlace = _Place
MLUPlace = _Place

def test(place: NPUPlace) -> None:
    return

a = MLUPlace(0)
test(a)

这里面 a 类型实际是错的,但是 mypy 检查 pass ~

直接使用 = 会使两个类型没啥区别 ~

如果要写的话,还是单独列一个类 ~

不过,建议 NPUPlace MLUPlace 暂时不要了,因为 paddle 目前还没有暴露出来这俩个东西,只在 pyi 里面写可能会有异议 ~ 尽量不要增加 _typing 之外属于 api 的东西吧 ~

PlaceLike 支持 str 应该目前用足够 ~ 这样的话,init.py 和 init.pyi 也不需要这两个东西 ~

@SigureMo 看看行不?

Copy link
Member

@SigureMo SigureMo May 9, 2024

Choose a reason for hiding this comment

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

protocol 应该继承,(或者说是实现,这是组合的概念),而不是直接 assign,NPU 和 MLU 很早之前就在框架内退场了吧大概一年前左右的开源任务

Copy link
Contributor

Choose a reason for hiding this comment

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

不需要了是吧?~

Copy link
Member

Choose a reason for hiding this comment

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

对的,没有就删掉好了


PlaceLike: TypeAlias = Union[
CPUPlace,
CUDAPlace,
CUDAPinnedPlace,
NPUPlace,
IPUPlace,
CustomPlace,
MLUPlace,
XPUPlace,
# It seems that we cannot define the literal for dev:id in nowadays python type-hinting.
str,
]
56 changes: 56 additions & 0 deletions python/paddle/_typing/dtype_like.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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 typing import Literal, Type, Union

import numpy as np
from typing_extensions import TypeAlias

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]
40 changes: 40 additions & 0 deletions python/paddle/_typing/layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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 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"]
DataLayoutSparseCOO: TypeAlias = Literal["SPARSE_COO"]
DataLayoutSparseCSR: TypeAlias = Literal["SPARSE_CSR"]
DataLayoutStrided: TypeAlias = Literal["STRIDED"]
Asthestarsfalll marked this conversation as resolved.
Show resolved Hide resolved

DataLayoutSparse: TypeAlias = Union[DataLayoutSparseCOO, DataLayoutSparseCSR]
DataLayoutND: TypeAlias = Union[
DataLayout0D,
DataLayout1D,
DataLayout2D,
DataLayout3D,
DataLayoutSparseCSR,
DataLayoutSparseCOO,
DataLayoutStrided,
]

DataLayout1DVariant: TypeAlias = Literal["NCW", "NWC"]
DataLayoutImage: TypeAlias = Literal["HWC", "CHW"]
56 changes: 56 additions & 0 deletions python/paddle/_typing/shape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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 typing import List, Optional, Tuple, Union

from typing_extensions import TypeAlias

from .. import Tensor

DynamicShapeLike: TypeAlias = Union[
Tuple[Optional[int]], list[Optional[int]], Tensor
Asthestarsfalll marked this conversation as resolved.
Show resolved Hide resolved
]

# Note: Do not confrom to predefined naming style in pylint.
Shape0D: TypeAlias = Tuple[None]
Shape1D: TypeAlias = Tuple[int]
Shape2D: TypeAlias = Tuple[int, int]
Shape3D: TypeAlias = Tuple[int, int, int]
Shape4D: TypeAlias = Tuple[int, int, int, int]
Shape5D: TypeAlias = Tuple[int, int, int, int, int]
Shape6D: TypeAlias = Tuple[int, int, int, int, int, int]
Asthestarsfalll marked this conversation as resolved.
Show resolved Hide resolved
ShapeND: TypeAlias = Tuple[int, ...]


ShapeLike: TypeAlias = Union[
Shape0D,
Shape1D,
Shape2D,
Shape3D,
Shape4D,
Shape5D,
Shape6D,
List[int],
Tensor,
]


# for size parameters, eg, kernel_size, stride ...
Size1: TypeAlias = Union[int, Shape1D]
Size2: TypeAlias = Union[int, Shape2D]
Size3: TypeAlias = Union[int, Shape3D]
Size4: TypeAlias = Union[int, Shape4D]
Size5: TypeAlias = Union[int, Shape5D]
Size6: TypeAlias = Union[int, Shape6D]
SizeN: TypeAlias = Union[int, ShapeND]
Asthestarsfalll marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 25 additions & 0 deletions python/paddle/framework/dtype.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from enum import Enum

class dtype(Enum):
"""
Paddle data type class.
"""

uint8 = ...
int8 = ...
int16 = ...
int32 = ...
int64 = ...

float32 = ...
float64 = ...
float16 = ...
bfloat16 = ...

complex64 = ...
complex128 = ...

bool = ...
Asthestarsfalll marked this conversation as resolved.
Show resolved Hide resolved

def iinfo(dtype: dtype): ...
def finfo(dtype: dtype): ...
Asthestarsfalll marked this conversation as resolved.
Show resolved Hide resolved