Skip to content
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b8c2201
add type_hints for ci
megemini Apr 25, 2024
5dab33d
add type_hints unittest
megemini Apr 26, 2024
c11b093
tmp test for type hints
megemini Apr 26, 2024
436175f
change mypy version
megemini Apr 26, 2024
f6927c2
from __future__ import annotations
megemini Apr 26, 2024
c4ba2bf
tmp math.py docstring trigger ci
megemini Apr 26, 2024
9fba61a
tmp trigger ci
megemini Apr 26, 2024
36ba294
tmp debug mypy
megemini Apr 26, 2024
68354cb
fix paddle_build.sh
megemini Apr 27, 2024
7c2a715
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
megemini Apr 27, 2024
9654c26
setup with pyi
megemini Apr 27, 2024
732aff8
force reinstall
megemini Apr 27, 2024
458802a
setup.py type hints
megemini Apr 27, 2024
363ec84
restore math.py
megemini May 2, 2024
41aa797
update print_signatures.py for trigger type annotation ci
megemini May 2, 2024
c8775e3
update print_signatures.py member_dict for trigger type annotation ci
megemini May 2, 2024
0dae862
restore print_signatures.py
megemini May 2, 2024
5c996a0
get_api_md5 with ArgSpec & update unittest
megemini May 2, 2024
ce613b2
change math.py type annotation
megemini May 2, 2024
fb6cef6
change math.py type annotation return
megemini May 2, 2024
edc4b23
change math.py type annotation scale & stanh
megemini May 2, 2024
9857d86
update paddle_build.sh
megemini May 5, 2024
a7ed18c
[Update] type checker
megemini May 8, 2024
51fda55
tmp math.py, test=type_checking
megemini May 8, 2024
98dc1df
tmp math.py, test=type_checking
megemini May 9, 2024
eb1f468
tmp math.py, test=type_checking
megemini May 9, 2024
c75c574
tmp math.py, test=type_checking
megemini May 9, 2024
495d0b7
tmp math.py & fix paddle_build.sh, test=type_checking
megemini May 9, 2024
40b66c7
type checking on title
megemini May 10, 2024
0181f23
reduce log
megemini May 10, 2024
e7be07d
change mypy cache dir abspath
megemini May 10, 2024
618c3b9
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
megemini May 20, 2024
72067ed
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
megemini May 29, 2024
37ae0ab
[Change] paddle_build.sh func
megemini May 29, 2024
cf37661
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
megemini May 30, 2024
5cc0c4b
[Update] filter api
megemini May 30, 2024
f9c381e
[Update] pyproject.toml & process pool for run
megemini May 30, 2024
06dee11
[Update] restore math.py
megemini May 31, 2024
ae07a13
[Update] restore math.py
megemini May 31, 2024
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
114 changes: 108 additions & 6 deletions paddle/scripts/paddle_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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`
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
TITLE_CHECK=`curl -s https://github.com/PaddlePaddle/Paddle/pull/${GIT_PR_ID} | grep "<title>" | grep -i "type checking" || true`
TITLE_CHECK=`curl -s https://github.com/PaddlePaddle/Paddle/pull/${GIT_PR_ID} | grep "<title>" | grep -iE "type checking|typing" || true`

加一个 typing 吧,我比较喜欢用 [Typing] 前缀


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=$?
Copy link
Member

Choose a reason for hiding this comment

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

关于这块的mypy_cache有个小问题,如果我根据 shell 脚本来跑,这个路径将会是在tools/.mypy_cache, 而不是${PADDLE_ROOT}/.mypy_cache。是故意这么设计的嘛emmm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个有什么影响吗? pyproject.toml 可以配置

[tool.mypy]
python_version = "3.8"
cache_dir = ".mypy_cache"

另外,刚好有个问题请教一下!

我这里 paddle_build.sh 中的 run_type_checking 参考的 check_run_sot_ci

    # 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

但是,$(git log -1 --pretty=format:"%s" | grep -w "test=type_checking" || true) 没有效果,所以去掉了,只使用了 title 的判断条件 ~

我本地 $(git log -1 --pretty=format:"%s" | grep -w "test=type_checking" || true) 没问题 ~

这是咋回事儿?有啥办法?

Copy link
Member

@gouzil gouzil May 10, 2024

Choose a reason for hiding this comment

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

这个有什么影响吗? pyproject.toml 可以配置

[tool.mypy]
python_version = "3.8"
cache_dir = ".mypy_cache"

影响就是,这样就会生成和维护两份缓存了

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

另外,刚好有个问题请教一下!

可以把git log -1 改成 git log -10或者更大,因为 ci 在运行之前会 git pull upstream develop 所以导致没有命中我们想要的 commit (sot那个我自己来修吧, 感谢大佬发现的问题)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

影响就是,这样就会生成和维护两份缓存了

😅 我草率了 ... ... 当时没想这么多 ... ...

那我在 type_checking.py 里面改为绝对路径吧 ~

感谢!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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:"
Expand All @@ -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
Expand Down Expand Up @@ -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=$?
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
run_tc=$?
type_checking_status=$?

感觉这样语义会更明确些

Copy link
Member

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

if [[ ${run_tc} -eq 0 ]]; then
{ type_checking_info=$(exec_type_checking 2>&1 1>&3 3>/dev/null); } 3>&1
type_checking_code=$?

summary_type_checking_problems $type_checking_code "$type_checking_info"
fi
Copy link
Member

Choose a reason for hiding this comment

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

示例代码和类型检查是否可以分别抽一个函数呢?CI 入口一般只做分发,不适合写太多逻辑~


assert_api_spec_approvals
;;
build_and_check_cpu)
Expand All @@ -4294,7 +4375,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=$?
if [[ ${run_tc} -eq 0 ]]; then
{ type_checking_info=$(exec_type_checking 2>&1 1>&3 3>/dev/null); } 3>&1
type_checking_code=$?

summary_type_checking_problems $type_checking_code "$type_checking_info"
fi

assert_api_spec_approvals
;;
check_whl_size)
Expand Down Expand Up @@ -4538,7 +4630,17 @@ function main() {
api_example)
Copy link
Member

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_cpubuild_and_check_gpu,这个入口没用了,直接删掉就好

{ example_info=$(exec_samplecode_test cpu 2>&1 1>&3 3>/dev/null); } 3>&1
example_code=$?
summary_check_problems $example_code "$example_info"

summary_check_example_code_problems $example_code "$example_info"

run_type_checking
run_tc=$?
if [[ ${run_tc} -eq 0 ]]; then
{ type_checking_info=$(exec_type_checking 2>&1 1>&3 3>/dev/null); } 3>&1
type_checking_code=$?

summary_type_checking_problems $type_checking_code "$type_checking_info"
fi
;;
test_op_benchmark)
test_op_benchmark
Expand Down
20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,23 @@ known-first-party = ["paddle"]
"test/dygraph_to_static/test_loop.py" = ["C416", "F821"]
# Ignore unnecessary lambda in dy2st unittest test_lambda
"test/dygraph_to_static/test_lambda.py" = ["PLC3002"]

[tool.mypy]
python_version = "3.8"
cache_dir = ".mypy_cache"
# Miscellaneous strictness flags
allow_redefinition = true
local_partial_types = true
strict = false
# Untyped definitions and calls
check_untyped_defs = true
# Import discovery
follow_imports = "normal"
# Miscellaneous
warn_unused_configs = true
# Configuring warnings
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
# Configuring error messages
show_column_numbers = true
Empty file added python/paddle/py.typed
Empty file.
9 changes: 7 additions & 2 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
math functions
"""

from __future__ import annotations

import math
import warnings

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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):
Copy link
Member

Choose a reason for hiding this comment

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

image

这里正常报错,没问题

我删掉了标题里的 typing,再触发一次看看是否可以按照预期没有检查

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.

image

话说这里能做到只修改类型提示的情况下,由我来 review 么?不然不利于后续 review 推进

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
Member

Choose a reason for hiding this comment

The 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.
Expand Down
3 changes: 3 additions & 0 deletions python/setup.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,9 @@ if '${WITH_CPP_DIST}' == 'ON':
paddle_lib_test_dir = '${PADDLE_LIB_TEST_DIR}'
install_cpp_dist_and_build_test(paddle_install_dir, paddle_lib_test_dir)

# type hints
package_data['paddle'] = package_data.get('paddle', []) + ['py.typed']
package_data['paddle.tensor'] = package_data.get('paddle.tensor', []) + ['*.pyi']

with redirect_stdout():
setup(name='${PACKAGE_NAME}',
Expand Down
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==1.10.0
7 changes: 7 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,13 @@ def get_package_data_and_package_dir():
ext_modules = []
elif sys.platform == 'darwin':
ext_modules = []

# type hints
package_data['paddle'] = package_data.get('paddle', []) + ['py.typed']
package_data['paddle.tensor'] = package_data.get('paddle.tensor', []) + [
'*.pyi'
]

return package_data, package_dir, ext_modules


Expand Down
20 changes: 13 additions & 7 deletions tools/sampcd_processor_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
API_DIFF_SPEC_FN = 'dev_pr_diff_api.spec'
TEST_TIMEOUT = 10

PAT_API_SPEC_MEMBER = re.compile(r'\((paddle[^,]+)\W*document\W*([0-9a-z]{32})')
# insert ArgSpec for changing the API's type annotation can trigger the CI
PAT_API_SPEC_SIGNATURE = re.compile(
r'^(paddle[^,]+)\s+\((ArgSpec.*),.*document\W*([0-9a-z]{32})'
)


class Result:
# name/key for result
Expand Down Expand Up @@ -333,17 +339,17 @@ def get_api_md5(path):
API_spec = os.path.abspath(os.path.join(os.getcwd(), "..", path))
if not os.path.isfile(API_spec):
return api_md5
pat = re.compile(r'\((paddle[^,]+)\W*document\W*([0-9a-z]{32})')
patArgSpec = re.compile(
r'^(paddle[^,]+)\s+\(ArgSpec.*document\W*([0-9a-z]{32})'
)

with open(API_spec) as f:
for line in f.readlines():
mo = pat.search(line)
if not mo:
mo = patArgSpec.search(line)
mo = PAT_API_SPEC_MEMBER.search(line)

if mo:
api_md5[mo.group(1)] = mo.group(2)
else:
mo = PAT_API_SPEC_SIGNATURE.search(line)
api_md5[mo.group(1)] = f'{mo.group(2)}, {mo.group(3)}'

return api_md5


Expand Down
Loading