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 8 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
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
44 changes: 44 additions & 0 deletions python/paddle/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import annotations

from .framework.dtype import (
bfloat16 as bfloat16,
bool as bool,
complex64 as complex64,
complex128 as complex128,
dtype as dtype,
finfo as finfo,
float16 as float16,
float32 as float32,
float64 as float64,
iinfo as iinfo,
int8 as int8,
int16 as int16,
int32 as int32,
int64 as int64,
uint8 as uint8,
)

class Tensor: ...

def to_tensor(data, dtype=None, place=None, stop_gradient=True) -> Tensor: ...
Copy link
Contributor Author

Choose a reason for hiding this comment

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

不在pyi里加的话,mypy会找不到,我本地的lsp也找不到,但是我搜索到的资料是优先从pyi中找,其次是py。但这样看起来直接不从py找了

Copy link
Contributor

Choose a reason for hiding this comment

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

是的 ~ 这个 init.pyi 文件现在暂时应该不需要 ~

当时 RFC 里面需要,是因为 Tensor 本来是想加在这里 ~

后面 @SigureMo 建议 Tensor 的 stub 文件 tensor.pyi 直接加在 tensor 目录里面,那么这个 init.pyi 应该暂时不需要了 ~

不然,mypy 优先从 pyi 查找,而这个 init.pyi 又不完整,就会导致 paddle.xxx 都找不到 ~

参考 #63953

Copy link
Contributor

Choose a reason for hiding this comment

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

不过,_typing 模块,貌似需要在 init.py 中引入

import paddle._typing as _typing

Copy link
Member

Choose a reason for hiding this comment

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

一般使用 from . import _typing as _typing


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: ...
62 changes: 62 additions & 0 deletions python/paddle/_typing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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 (
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,
)

# 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

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


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]
33 changes: 33 additions & 0 deletions python/paddle/_typing/layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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"]

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

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

from typing_extensions import TypeAlias

from .. import Tensor

DynamicShapeLike: TypeAlias = Union[
Tuple[Union[int, Tensor, None], ...],
List[Union[int, Tensor, None]],
Comment on lines +23 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

使用 tuplelist 吧 ~

另外,需要 from __future__ import annotations

这里手误写错了,不过我在本地测试过,这些代码都没问题,3.8.0可以使用list作为类型提示,现在添加了mypy进行测试

python 3.8 应该不能直接用 tuplelist,我这里是 3.8.17

----> 1 a:list[int] = [1,2]

TypeError: 'type' object is not subscriptable

In [2]: from __future__ import annotations

In [3]: a:list[int] = [1,2]

你那边测试可能是 mypy 指定版本有问题?

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]]
17 changes: 17 additions & 0 deletions python/paddle/framework/dtype.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
1 change: 1 addition & 0 deletions python/unittest_py/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ wandb>=0.13 ; python_version<"3.12"
xlsxwriter==3.0.9
xdoctest==1.1.1
ubelt==1.3.3 # just for xdoctest
mypy
Copy link
Contributor

Choose a reason for hiding this comment

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

统一一下版本号吧 mypy==1.10.0

Loading