-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[Typing] Paddle 的 CI 中引入 mypy 对于 API 中 docstring 的示例代码的类型检查 #63901
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
Changes from 30 commits
b8c2201
5dab33d
c11b093
436175f
f6927c2
c4ba2bf
9fba61a
36ba294
68354cb
7c2a715
9654c26
732aff8
458802a
363ec84
41aa797
c8775e3
0dae862
5c996a0
ce613b2
fb6cef6
edc4b23
9857d86
a7ed18c
51fda55
98dc1df
eb1f468
c75c574
495d0b7
40b66c7
0181f23
e7be07d
618c3b9
72067ed
37ae0ab
cf37661
5cc0c4b
f9c381e
06dee11
ae07a13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3514,17 +3514,59 @@ function exec_samplecode_test() { | |||||
|
|
||||||
| cd ${PADDLE_ROOT}/tools | ||||||
| if [ "$1" = "cpu" ] ; then | ||||||
| python sampcd_processor.py --debug --mode cpu; example_error=$? | ||||||
| python sampcd_processor.py --mode cpu; example_error=$? | ||||||
| elif [ "$1" = "gpu" ] ; then | ||||||
| SAMPLE_CODE_EXEC_THREADS=${SAMPLE_CODE_EXEC_THREADS:-2} | ||||||
| python sampcd_processor.py --threads=${SAMPLE_CODE_EXEC_THREADS} --debug --mode gpu; example_error=$? | ||||||
| python sampcd_processor.py --threads=${SAMPLE_CODE_EXEC_THREADS} --mode gpu; example_error=$? | ||||||
| fi | ||||||
| if [ "$example_error" != "0" ];then | ||||||
| echo "Code instance execution failed" >&2 | ||||||
| exit 5 | ||||||
| fi | ||||||
| } | ||||||
|
|
||||||
| function run_type_checking() { | ||||||
| set +x | ||||||
|
|
||||||
| # check pr title | ||||||
| TITLE_CHECK=`curl -s https://github.com/PaddlePaddle/Paddle/pull/${GIT_PR_ID} | grep "<title>" | grep -i "type checking" || true` | ||||||
|
|
||||||
| if [[ ${TITLE_CHECK} ]]; then | ||||||
| set -x | ||||||
| return 0 | ||||||
| else | ||||||
| set -x | ||||||
| return 1 | ||||||
| fi | ||||||
| } | ||||||
|
|
||||||
| function exec_type_checking() { | ||||||
| if [ -d "${PADDLE_ROOT}/build/pr_whl" ];then | ||||||
| pip install ${PADDLE_ROOT}/build/pr_whl/*.whl | ||||||
| else | ||||||
| echo "WARNING: PR wheel is not found. Use develop wheel !!!" | ||||||
| pip install ${PADDLE_ROOT}/build/python/dist/*.whl | ||||||
| fi | ||||||
|
|
||||||
| python -c "import paddle;print(paddle.__version__);paddle.version.show()" | ||||||
|
|
||||||
| cd ${PADDLE_ROOT}/tools | ||||||
|
|
||||||
| # check all sample code | ||||||
| TITLE_CHECK_ALL=`curl -s https://github.com/PaddlePaddle/Paddle/pull/${GIT_PR_ID} | grep "<title>" | grep -i "type checking all" || true` | ||||||
|
|
||||||
| if [[ ${TITLE_CHECK_ALL} ]]; then | ||||||
| python type_checking.py --full-test; type_checking_error=$? | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 关于这块的
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个有什么影响吗? [tool.mypy]
python_version = "3.8"
cache_dir = ".mypy_cache"另外,刚好有个问题请教一下! 我这里 # use "git commit -m 'message, test=sot'" to force ci to run
COMMIT_RUN_CI=$(git log -1 --pretty=format:"%s" | grep -w "test=sot" || true)
# check pr title
TITLE_RUN_CI=$(curl -s https://github.com/PaddlePaddle/Paddle/pull/${GIT_PR_ID} | grep "<title>" | grep -i "sot" || true)
if [[ ${COMMIT_RUN_CI} || ${TITLE_RUN_CI} ]]; then
set -x
return
fi但是, 我本地 这是咋回事儿?有啥办法?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
影响就是,这样就会生成和维护两份缓存了 mypy python/paddle/tensor/math.py # cache 位置: ${PADDLE_ROOT}/.mypy_cache
cd tools/
python type_checking.py --full-test # cache 位置: ${PADDLE_ROOT}/tools/.mypy_cache
可以把
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
😅 我草率了 ... ... 当时没想这么多 ... ... 那我在 感谢!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gouzil 已修改 ~ |
||||||
| else | ||||||
| python type_checking.py; type_checking_error=$? | ||||||
| fi | ||||||
|
|
||||||
| if [ "$type_checking_error" != "0" ];then | ||||||
| echo "Example code type checking failed" >&2 | ||||||
| exit 5 | ||||||
| fi | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| function collect_ccache_hits() { | ||||||
| ccache -s | ||||||
|
|
@@ -3565,10 +3607,11 @@ function test_model_benchmark() { | |||||
| bash ${PADDLE_ROOT}/tools/test_model_benchmark.sh | ||||||
| } | ||||||
|
|
||||||
| function summary_check_problems() { | ||||||
| function summary_check_example_code_problems() { | ||||||
| set +x | ||||||
| local example_code=$1 | ||||||
| local example_info=$2 | ||||||
|
|
||||||
| if [ $example_code -ne 0 ];then | ||||||
| echo "===============================================================================" | ||||||
| echo "*****Example code error***** Please fix the error listed in the information:" | ||||||
|
|
@@ -3591,6 +3634,33 @@ function summary_check_problems() { | |||||
| } | ||||||
|
|
||||||
|
|
||||||
| function summary_type_checking_problems() { | ||||||
| set +x | ||||||
| local type_checking_code=$1 | ||||||
| local type_checking_info=$2 | ||||||
|
|
||||||
| if [ $type_checking_code -ne 0 ];then | ||||||
| echo "===============================================================================" | ||||||
| echo "*****Example code type checking error***** Please fix the error listed in the information:" | ||||||
| echo "===============================================================================" | ||||||
| echo "$type_checking_info" | ||||||
| echo "===============================================================================" | ||||||
| echo "*****Example code type checking FAIL*****" | ||||||
| echo "===============================================================================" | ||||||
| exit $type_checking_code | ||||||
| else | ||||||
| echo "===============================================================================" | ||||||
| echo "*****Example code type checking info*****" | ||||||
| echo "===============================================================================" | ||||||
| echo "$type_checking_info" | ||||||
| echo "===============================================================================" | ||||||
| echo "*****Example code type checking PASS*****" | ||||||
| echo "===============================================================================" | ||||||
| fi | ||||||
| set -x | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| function reuse_so_cache() { | ||||||
| get_html="https://api.github.com/repos/PaddlePaddle/Paddle" | ||||||
| curl -X GET ${get_html}/commits -H "authorization: token ${GITHUB_API_TOKEN}" >tmp.txt | ||||||
|
|
@@ -4274,7 +4344,18 @@ function main() { | |||||
| fi | ||||||
| { example_info=$(exec_samplecode_test cpu 2>&1 1>&3 3>/dev/null); } 3>&1 | ||||||
| example_code=$? | ||||||
| summary_check_problems $[${example_code_gpu} + ${example_code}] "${example_info_gpu}\n${example_info}" | ||||||
|
|
||||||
| summary_check_example_code_problems $[${example_code_gpu} + ${example_code}] "${example_info_gpu}\n${example_info}" | ||||||
|
|
||||||
| run_type_checking | ||||||
| run_tc=$? | ||||||
|
||||||
| run_tc=$? | |
| type_checking_status=$? |
感觉这样语义会更明确些
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.
啊,不对,我才发现是上面的 run_type_checking 的语义不对,上面的 run_type_checking 乍一看是已经开始跑类型检查了,但实际上只是检查了标题,是有点奇怪的,可以改成 check_need_type_checking 其结果为 need_type_checking
Outdated
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.
示例代码和类型检查是否可以分别抽一个函数呢?CI 入口一般只做分发,不适合写太多逻辑~
Outdated
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_example 应该是很久以前的 CI 流水线入口,我们现在检查示例代码在 Static-Check 流水线,对应上面 build_and_check_cpu 和 build_and_check_gpu,这个入口没用了,直接删掉就好
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| math functions | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import math | ||
| import warnings | ||
|
|
||
|
|
@@ -212,7 +214,10 @@ def log_(x, name=None): | |
| return _C_ops.log_(x) | ||
|
|
||
|
|
||
| def scale(x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None): | ||
| # TODO(megemini): type checking | ||
| def scale( | ||
| x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None | ||
| ) -> int: | ||
| """ | ||
| Scale operator. | ||
|
|
||
|
|
@@ -322,7 +327,7 @@ def scale(x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None): | |
| return helper.append_activation(out) | ||
|
|
||
|
|
||
| def stanh(x, scale_a=0.67, scale_b=1.7159, name=None): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 类型检查跳过了,符合预期~ 这里可以恢复下了~
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已经确认,可以在下个 PR 这么搞下
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| def stanh(x, scale_a: int = 1, scale_b=1.7159, name=None): | ||
| r""" | ||
|
|
||
| stanh activation. | ||
|
|
||


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吧,我比较喜欢用[Typing]前缀