-
Notifications
You must be signed in to change notification settings - Fork 282
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】为 Paddle 框架 API 添加类型提示(Type Hints) #858
Conversation
@Asthestarsfalll @gsq7474741 请帮忙看看 ~~~ |
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
非常棒的 RFC!不过有一些问题想要确认下:
- 是否考虑引入
typing_extension
,是否使用 PEP 563?根据个人 type hints 最佳开发实践,typing_extension
、PEP 563、if TYPE_CHECKING
的结合可以大大减少版本兼容性考虑,允许在低版本部分场景使用高版本的类型提示特性,虽然 PEP 563 终将在 Python 3.13 被 PEP 649 所替代,但距离 Python 3.13 成为最低支持版本还有很多年呢,PEP 563 是一个不错的选择 - 在前期将一些主要的模块标记完之后,是否考虑组织一次分享,向社区其他同学分享类型提示标注的经验,因为了解类型提示的人非常少,真正掌握一些高级用法的人更是基本没有
- 需要考虑在类型提示标注时的一些原则,虽然现在可能没有太多经验考虑不到,但在初期标注积累经验后应当整理成文档,如:
- 对于 dict 参数,如可能,应标记为
TypedDict
,以提供更精准的类型信息 - 等等,其实我暂时也想不到太多,这些可以考虑记录在文档里
- 对于 dict 参数,如可能,应标记为
唔,暂时也想不到太多,感觉稍微修改下就可以准备开工了~
|
||
## 1、相关背景 | ||
|
||
Python 在 3.5 版本通过 [PEP 484 – Type Hints](https://peps.python.org/pep-0484) 正式规范了 `类型提示` 功能,以帮助开发者提高代码质量,Python 目前 (`3.12` 版本) 已经完成的相关 `PEP` 有 `11` 个,具体可以参考 [Typing PEPs](https://peps.python.org/topic/typing/) 。经过前期的几个版本迭代,Python 的 `类型提示` 功能已经受到开发者的广泛认可。Paddle 目前支持的 Python 版本 `3.8` 已经可以较好的支持 `类型提示`,本文旨在阐述 Paddle 引入 `类型提示` 的可行性与具体方案。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python 目前 (
3.12
版本)
严谨地说,「目前」Python 正在开发的是 3.13,应指出「最新发布版本」
已经完成的相关
PEP
有11
个
11 个是如何得来的呢?Finished PEPs
也不止 11 个呀
Paddle 目前支持的 Python 版本
3.8
已经可以较好的支持类型提示
目前最低支持
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
写错了,是 21
个 ... ...
https://peps.python.org/topic/typing/ 中 Finished PEPs (done, with a stable interface)
|
||
## 2、功能目标 | ||
|
||
正确完成 Paddle 开放 API 的类型标注,但不声明 Paddle 类型标注的完备性。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
正确完成 Paddle 开放 API 的类型标注,但不声明 Paddle 类型标注的完备性。 | |
正确完成 Paddle 公开 API 的类型标注,但不声明 Paddle 类型标注的完备性。 |
|
||
### 2.1 typing 模块 | ||
|
||
此目标为 Paddle 添加 `typing` 模块,并作为开放 API 的一部分。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
公开 API 是一件很大的事情,一个 API 一旦公开,那么就需要保证其稳定性,用户不会因为之前版本 import 了一个 API 而在之后版本突然删掉 / 修改行为而挂掉,而我们的 typing 模块明显不具备这一点,我建议目前不考虑公开,并修改命名为 _typing
,公开应该在全 Paddle 类型提示全部覆盖后才考虑
而且公开 API 需要考虑到很多事情,比如 Tensor 是否是泛型等等……
因此 _typing
仅限于内部标注使用我觉得是目前更合适的方式
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯,可以 ~ 这样的话,那可以把这部分必要的文档,放到开发指南里面,做为 《Paddle 中的类型提示》 的一部分 ~
功能特性类似: | ||
|
||
- PyTorch 中的 `torch/types.py` | ||
- Numpy 中的 [Typing (numpy.typing)](https://numpy.org/devdocs/reference/typing.html) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NumPy 的 Typing 做的真的不错,paddlepaddle-stubs 也是有意参考 NumPy 的实现,之后实现时候可以多学习和参考 NumPy 的实现
- PyTorch 中的 `torch/types.py` | ||
- Numpy 中的 [Typing (numpy.typing)](https://numpy.org/devdocs/reference/typing.html) | ||
|
||
`typing` 模块包含 Paddle 中用到的特殊类型,如 `dtype`,`device` 等,具体实现可参考 @SigureMo 的 [paddle-stubs/_typing](https://github.com/cattidea/paddlepaddle-stubs/tree/main/paddle-stubs/_typing)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
paddle 运行时有自己的 dtype,这一点是与 paddlepaddle-stubs 不同的,要实现的只是类似 DTypeLike
这种的便于标注的类型
另外,Paddle 目前 dtype 底层实现正处于 VarType
向 DataType
的过渡期,此时可以使用 if TYPE_CHECKING
guard 下定义专属于类型检查的类型,但在未来消除掉这两者的差异后,直接使用 DataType
应该就可以了
- `paddle.fft.*` | ||
|
||
- `P4` | ||
- `paddle.incubate.*` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
非常冷门,建议 P5
- `paddle.sysconfig.*` | ||
- `paddle.utils.*` | ||
- `paddle.text.*` | ||
- `paddle.tensor.*` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
非常常用,建议 P2
- `paddle.profiler.*` | ||
- `paddle.nn.quant.*` | ||
- `paddle.nn.utils.*` | ||
- `paddle.jit.*` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议 P3
- `P5` | ||
- `paddle.inference.*` | ||
- `paddle.proto.*` | ||
- `paddle.common_ops_import.*` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里没有函数
- `paddle.reader.*` | ||
- `paddle.hapi.*` | ||
- `paddle.framework.*` | ||
- `paddle.dataset.*` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不要标注了,本就是废弃的
必须的 ~ 有一个原则看看可行不:在不违背 Paddle 最低支持版本 比如,annotation 中用
可以有 ~
可以有 ~ 参考上面提到的原则 ~ 只是,type hint 的东西太多了,best practice 可能得慢慢积累 ~ |
嗯,TypeAlias 从语法角度上只是赋值,PEP 563 只是对类型注释进行延迟计算,不会对赋值节点进行延迟计算,因此低版本语法就不支持的,那就无能为力了,PEP 695 也一样 另外,在类型注释中就算写
这个确实 |
Update 20240414
另外,关于 @SigureMo @zrr1999 @Asthestarsfalll @gsq7474741 @sunzhongkai588 @luotao1 请评审 ~ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
> **说明**: 类型标注是个循序渐进的过程,且存在较多私有 API 与 c++ 接口,此次任务无法保证完成以上所有接口的类型标注,故此,不做 Paddle 类型标注的完备性说明。 | ||
|
||
### 2.1 _typing 模块 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### 2.1 _typing 模块 | |
### 2.1 `_typing` 模块 |
- PyTorch 中的 `torch/types.py` | ||
- Numpy 中的 [Typing (numpy.typing)](https://numpy.org/devdocs/reference/typing.html) | ||
|
||
`_typing` 模块包含 Paddle 中用到的特殊类型,如 `dtype`,`device` 等,具体实现可参考 @SigureMo 的 [paddle-stubs/_typing](https://github.com/cattidea/paddlepaddle-stubs/tree/main/paddle-stubs/_typing)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`_typing` 模块包含 Paddle 中用到的特殊类型,如 `dtype`,`device` 等,具体实现可参考 @SigureMo 的 [paddle-stubs/_typing](https://github.com/cattidea/paddlepaddle-stubs/tree/main/paddle-stubs/_typing)。 | |
`_typing` 模块包含 Paddle 中用到的特殊类型,如 `dtype`、`device` 等,具体实现可参考 @SigureMo 的 [paddle-stubs/_typing](https://github.com/cattidea/paddlepaddle-stubs/tree/main/paddle-stubs/_typing)。 |
|
||
1. Paddle 中引入 `_typing` 模块 | ||
|
||
**对用户的影响:** 用户可以使用 `_typing` 模块提供的接口 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不推荐用户用吧,我觉得可以认为对用户无影响
|
||
**对开发者的影响:** 开发者可以使用 `_typing` 模块标注 Paddle 内部模块 | ||
|
||
**对框架架构的影响:** Paddle 框架中增加 `_typing` 模块与相应接口 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**对框架架构的影响:** Paddle 框架中增加 `_typing` 模块与相应接口 | |
**对框架架构的影响:** Paddle 框架中增加 `_typing` 模块 |
已修改 ~ 看看大家还有啥建议?~ |
comment by @jeff41404 整体修改完后,需要进行一个部门评审 1. “意义”(效果/收益)这一章的描述过于概括和抽象,需要清楚和直观一些,比如我们对 公开API 都加上类型提示后,“开发体验”上直观的改进效果是什么,比如开发者在什么场景下有更好的感受,最好有一些截图说明优化前和优化后的效果对比,让其它人能够get到这项工作的价值和意义 2. 下面分阶段的工作中,每个阶段要加一句话说明里程碑的意义,并和整体意义(效果/收益)联动,比如 第1阶段是完成机制基础,第2阶段 就能够达到xxx效果 3. 公开API的数量。2.6版本已经有1800+,develop接近2000 4. “2.3 CI 流水线” 一章中,旧/新 API的意思是“PR中修改的公开API”? 5. “2.4 文档建设”还需要更新 API开发指南,在推进存量修改的同时,确保增量也都有类型提示 |
Understood, but does the priority of |
https://github.com/orgs/cattidea/projects/3/views/1 与本任务无关,只是本任务的一个参考项而已 |
PR types
Others
PR changes
Docs
Description
【Hackathon 6th】为 Paddle 框架 API 添加类型提示(Type Hints)
请评审!
@SigureMo @zrr1999 @sunzhongkai588 @luotao1