Skip to content

Conversation

megemini
Copy link
Contributor

@megemini megemini commented Jul 22, 2024

PR Category

User Experience

PR Types

Improvements

Description

添加 libpaddle 的 stub 文件生成脚本。

使用工具 pybind11-stubgen 生成 libpaddle 的 stub 文件。

有几点需要注意的:

  • 没有在 python/paddle/base 下直接生成 libpaddle 文件夹(内含 stub 文件)

    主要原因是,一旦这个文件夹被污染,则极有可能程序中无法正常导入 libpaddle 模块。
    因此,将生成的文件统一放到了 python/paddle/_typing/libs 目录下,这里以后也可以放置其他 pybind11 的 C++ API 相应的 stub 文件。

  • patch 了 pybind11-stubgenPrinter 的方法,主要目的是,使 pyi 文件消除语法错误,以可以正常进行 mypy 检查

    主要改动为:

    • 对于 Printer 中的 print_xxx 方法,如果输入参数中的 name 不合法,则会在 name 后添加 _

      这里主要应对 C++ 中有参数用的 in 作为输入参数,因此修改了输入参数名,如 in 改为 in_ 。影响是,使用 kwargs 的方式调用的时候可能会发现输入参数名不对。另外,发现有个函数名是 assert ,也改为了 assert_ ,这个函数是没法直接调用了(静态检查的时候)~ 整体上来说感觉影响不大 ~

    • 对于 Printer 中的 print_invalid_exp 方法,由直接输出不合法的名称,改为通过引号引用。

      def dist_config(self) -> paddle::DistConfig 改为了 def dist_config(self) -> "paddle::DistConfig": 。改动前的代码是存在语法错误的,改动后的语法正常,而在类型检查时转为了 Any 。这类错误主要是 pybind11 使用不当引起的 ~

  • Tensor.pyi 一样,会在 paddle_binary_dirpaddle_source_dir 中同时添加。

大体看了一下生成的文件,出问题的地方基本都是 pybind11 使用不当导致的,整体来看可用性还行?!~

关联

@SigureMo

Copy link

paddle-bot bot commented Jul 22, 2024

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

Pillow
decorator
astor
opt_einsum==3.3.0
networkx
typing_extensions
pybind11-stubgen==2.5.1 # generate stub file from pybind11 C++ APIs
Copy link
Contributor

Choose a reason for hiding this comment

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

这个库,只是在运行 setup.py 时需要,在 paddlepaddle package 的运行时,并不需要吧?

Copy link
Member

@SigureMo SigureMo Jul 23, 2024

Choose a reason for hiding this comment

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

运行时不需要,现在还只在测试,之后会考虑在用户的依赖里过滤掉,目前考虑的是直接在 requirements.txt 里加个 [build] 标记,在 setup 里过滤掉,以免影响各种流水线和开发流程什么的

比如

Pillow
decorator
astor
opt_einsum==3.3.0
networkx
typing_extensions
pybind11-stubgen==2.5.1 # [build]

长期来看,我们应该有 requirements-build.txtrequirements-dev.txtrequirements-test.txt 等多个依赖清单

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, 明白了。
等 ready 后 ping 我一下,我再给 approve。

Copy link
Contributor Author

@megemini megemini Jul 23, 2024

Choose a reason for hiding this comment

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

是的 ~

目前 paddle 有三个 requirements.txt (我找到的 ... ...

  • paddle 打包安装时的依赖,也就是这个 python/requirements.txt (直接面对用户的,与编译测试无关)
  • 单测时的依赖,python/unittest_py/requirements.txt (类似 requirements-test.txt
  • 编译时的依赖,paddle/scripts/compile_requirements.txt (类似 requirements-build.txt

感觉 pybind11-stubgen 应该放在 paddle/scripts/compile_requirements.txt 中,但是,之前试过,无法触发安装(windows 下可以,mac 和 linux 下不行)。

setup.py 中检查 build 依赖,只会检查 python/requirements.txt ,也就导致 pybind11-stubgen 无法在编译时安装。(当然,可以放在 CMakeLists 中,直接在编译时安装,但个人感觉不太合适)

所以,这里先放在 python/requirements.txt 验证一下功能。

另外,单独提一下,后续如果区分 requirements.txtrequirements-build.txt 等依赖关系的时候,需要注意一下: requirements-build.txt >= requirements.txt

即,编译时的依赖需要包含运行时依赖,因为:

  • 至少从 typing 特性的角度来说,需要在编译后、安装 打包 前,能够正常导入 paddle 以及 libpaddle
  • 目前的编译打包逻辑也是如此验证环境的

Copy link
Member

Choose a reason for hiding this comment

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

目前考虑的是直接在 requirements.txt 里加个 [build] 标记,在 setup 里过滤掉

记得合入前需要实现这个机制,并验证下(unzip wheel 包查看 dist-info 里的 METADATA 就可以看到依赖项了)

@megemini
Copy link
Contributor Author

Update 20240724

  • 增加 libpaddle.pyi ,使其指向 paddle._typing.libs.libpaddle

    由于 paddle.__init__.py 本身没有导入 base ,而生成的 pyi 里面会用到 import paddle.base.libpaddle ,所以,增加 libpaddle.pyi 文件,这样逻辑上也清楚一点吧 ~

  • 增加/更新 core.pyi _C_ops.pyi ,使其指向 paddle._typing.libs.libpaddle 及子目录

    貌似这两个文件基本上就是抽取了 libpaddle 里面的东西?

另外,计划通过解析 ops.yaml 完善 libpaddleops 的标注,也就是 _C_ops 中的标注。目前 ops 还都是 def abs(*args, **kwargs): 的形式。

@megemini
Copy link
Contributor Author

Update 20240731

  • 修改 setup 中对于 requirements 的过滤行为

记得合入前需要实现这个机制,并验证下(unzip wheel 包查看 dist-info 里的 METADATA 就可以看到依赖项了)

image

安装包中已无 pybind11-stubgen

@megemini megemini changed the title [Typing all] 添加 libpaddle 的 stub 文件生成脚本 [Typing] 添加 libpaddle 的 stub 文件生成脚本 Aug 1, 2024
@megemini megemini changed the title [Typing] 添加 libpaddle 的 stub 文件生成脚本 [Typing all] 添加 libpaddle 的 stub 文件生成脚本 Aug 1, 2024
@SigureMo SigureMo changed the title [Typing all] 添加 libpaddle 的 stub 文件生成脚本 [Typing] 添加 libpaddle 的 stub 文件生成脚本 Aug 7, 2024
Copy link

paddle-ci-bot bot commented Aug 8, 2024

Sorry to inform you that 7175f55's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

Comment on lines 1294 to 1298
package_data['paddle'] = [
*package_data.get('paddle', []),
'py.typed',
'*.pyi',
]
Copy link
Member

Choose a reason for hiding this comment

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

这块封装个函数吧,比如

extend_package_data(
    package_data,
    {
        "paddle": ['py.typed', '*.pyi'],
        "paddle.framework": ["*.pyi"],
        ...
    }
)

@megemini
Copy link
Contributor Author

megemini commented Aug 30, 2024

Update 20240830

在本 PR 中修复此问题 ~

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 🐾

@SigureMo SigureMo requested a review from jzhang533 September 4, 2024 07:02
@luotao1 luotao1 merged commit 478d7cb into PaddlePaddle:develop Sep 5, 2024
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants