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

[Typing] bump mypy version to 1.11.2 and fix many typing errors in cinn binding #68372

Merged
merged 12 commits into from
Sep 25, 2024

Conversation

megemini
Copy link
Contributor

PR Category

User Experience

PR Types

Bug fixes

Description

  • 升级 mypy 版本至 1.11.2
  • 打开 CI 的类型检查

关联 #68278

@SigureMo

@megemini
Copy link
Contributor Author

Update 20240923

  • 遍历 _typing.libs.libpaddle 中的目录,并添加到 setup 中的 packages package_data

  • gen_pybind11_stub.py 增加后处理

    • 替换 BAD_ATTR,如 cinn::ir::_paddle.Tensor_。这些字符串 pybind11_stubgen 捕获不到,只能逐个处理,以防止语法错误。以及 None: typing.ClassVar[Type.cpp_type_t] 错误的导出 python 关键字。
    • 插入缺失的 import 。有些模块 pybind11_stubgen 没有正确的添加 import ,这里判断文件中缺失后进行添加。

@paddle-bot paddle-bot bot added the contributor External developers label Sep 23, 2024
@luotao1 luotao1 added the HappyOpenSource 快乐开源活动issue与PR label Sep 24, 2024
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

CI 里貌似还有点因为切 PIR 导致的新问题和这两天因为没监控带来的新问题(比如 from_dlpack 那个我是肉眼没注意到的)~

Comment on lines 323 to 329
def post_process(output_dir: str):
for root, _, files in os.walk(output_dir):
if files:
for f in files:
if f.endswith('.pyi'):
replace_bad_attr(str(Path(root) / f))
insert_import_modules(str(Path(root) / f))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def post_process(output_dir: str):
for root, _, files in os.walk(output_dir):
if files:
for f in files:
if f.endswith('.pyi'):
replace_bad_attr(str(Path(root) / f))
insert_import_modules(str(Path(root) / f))
def post_process(output_dir: str):
for root, _, files in os.walk(output_dir):
if not files:
continue
for f in files:
if not f.endswith('.pyi'):
continue
replace_bad_attr(str(Path(root) / f))
insert_import_modules(str(Path(root) / f))

这样可以有效减少两层缩进

Comment on lines 50 to 53
# python/paddle/_typing/libs/libpaddle/cinn/ir.pyi
'cinn::ir::_paddle.Tensor_': 'typing.Any',
# python/paddle/_typing/libs/libpaddle/cinn/common.pyi
'None: typing.ClassVar[Type.cpp_type_t]': '# None: typing.ClassVar[Type.cpp_type_t]',
Copy link
Member

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.

本 PR 可以暂时这样,因为我目前也没有更好的方案

Copy link
Contributor Author

Choose a reason for hiding this comment

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

确实没啥好办法 ... ... 其实吧,我觉得我们是在给 c++ 接口拓展(如 pybind11)的不规范使用擦屁股 ... ... 不然为啥有的接口就转换的很好,有的就不行 ~

而出现 None 作为 python 成员变量的名称这种问题,应该在接口审核的时候就提出来,不然下游也没法用的 ~

唉 ... ... 俺是人微言轻,只能见招拆招了 ~

我在想,能不能在 post process 的时候做一步语法检查,检查出来语法错误就直接删掉那一行?

Copy link
Member

Choose a reason for hiding this comment

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

c++ 接口拓展(如 pybind11)的不规范使用

这里我觉得是可以规范一下的,只是我现在不清楚,需要怎么修改才能规范化呢?就比如这里的 cinn::ir::_paddle.Tensor_ 是怎么搞出来的呢?要怎么修改呢?(目前还没有去关注相关代码

None 这个问题比较明确,其实这里可以修改的,因为 cinn 的 python 端没啥用了,可以改成 None_,这个感觉是否可以有一个什么拦截机制?(嵌套这么多层还真不是很好检测)

>>> paddle.base.core.cinn.common.Type.cpp_type_t.Const
<cpp_type_t.Const: 1>
>>> paddle.base.core.cinn.common.Type.cpp_type_t.None
  File "<stdin>", line 1
    paddle.base.core.cinn.common.Type.cpp_type_t.None
                                                 ^^^^
SyntaxError: invalid syntax
>>> getattr(paddle.base.core.cinn.common.Type.cpp_type_t, "None")
<cpp_type_t.None: 0>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cinn::ir::paddle.Tensor 是怎么搞出来的呢?要怎么修改呢?

这个确实比较奇怪,pybind11_stubgen 本来是可以拦截这类不合法的类型,比如之前 PYBIND11_ATTR_MAPPING 里面的那一堆东西 ~ 但是这个却拦截不了 ~

需要怎么修改才能规范化呢?

比如:

https://pybind11.readthedocs.io/en/latest/advanced/misc.html#avoiding-cpp-types-in-docstrings

image

导致出现此类问题的原因可能还有很多,只是目前木有精力去一一分析 ... ...

None 这个问题比较明确

确实是这样,本来问题不应该留到 python 这里才发现 ~

@@ -43,6 +43,27 @@
if TYPE_CHECKING:
from collections.abc import Generator

# some invalid attr can NOT be parsed.
# to avoid sytax error, we can only do plain replacement.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# to avoid sytax error, we can only do plain replacement.
# to avoid syntax error, we can only do plain replacement.

@megemini
Copy link
Contributor Author

Update 20240924

  • 增加 check_remove_syntax_error 后处理,删除掉 syntax error 的行
  • 修复一些 type 标注问题

@@ -116,7 +117,9 @@ def to_dlpack(x: Tensor) -> CapsuleType:
return x._to_dlpack()


def from_dlpack(dlpack: SupportDLPack | CapsuleType) -> Tensor:
def from_dlpack(
dlpack: SupportDLPack | CapsuleType | npt.NDArray[Any],
Copy link
Member

Choose a reason for hiding this comment

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

@SigureMo SigureMo changed the title [Typing] 升级 mypy 版本至 1.11.2 [Typing] bump mypy version to 1.11.2 and fix many typing errors in cinn binding Sep 25, 2024
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

LGTMeow 🐾

Copy link
Contributor

@risemeup1 risemeup1 left a comment

Choose a reason for hiding this comment

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

LGTM

@SigureMo SigureMo merged commit b5e30b2 into PaddlePaddle:develop Sep 25, 2024
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource 快乐开源活动issue与PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants