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

[Dy2St] skip compare between func and module attribute to fix NumPy 1.25 error #55482

Merged
merged 2 commits into from
Jul 18, 2023

Conversation

SigureMo
Copy link
Member

@SigureMo SigureMo commented Jul 17, 2023

PR types

Others

PR changes

Others

Description

在 NumPy 1.25 后在一些错误的比较时会直接报错1,比如

[[1, 2], 3] == np.bool_(False)
# NumPy 1.24
# <stdin>:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.

# NumPy 1.25
# ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.

原来在 NumPy 1.24 会报 Warning 的比较现在直接报错了

我们动转静的 is_unsupported 用于判断一个函数是否不需要转写,其实现是将所有跳过的模块所有成员与当前函数进行比较(== 也即 __eq__),而 NumPy 中包含成员 np.False_,由于 np.False_ 重载了 __eq__,因此其比较行为是可能报错的

目前报错是发生在嵌套 Sequentialnp.False_ 比较时出现的问题,比如

Sequential(
  (0): Conv2d_BN(
    (c): Conv2D(3, 16, kernel_size=[3, 3], stride=[2, 2], padding=1, data_format=NCHW)
    (bn): BatchNorm2D(num_features=16, momentum=0.9, epsilon=1e-05)
  )
  (1): Hardswish()
)

layer[0] 是两层的,layer[1] 是一层的,其实和 [[1, 2], 3] 是同样的结构,在 Python 1.25 就会报错

Workaround

跳过与属性的比较,只和函数比较,按理说传入的是 callable 的,那么也没有必要和非 callable 进行比较,函数比较直接 is 即可,下面的 if isinstance(func_in_dict, (list, numpy.ndarray)) 看起来也是没有必要的了(和 #27631#34246 相关)

PCard-66972

Footnotes

  1. NumPy 1.25 Release Notes - Expired deprecations

@paddle-bot
Copy link

paddle-bot bot commented Jul 17, 2023

你的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.

Copy link
Contributor

@Aurelius84 Aurelius84 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 2dcb0eb into PaddlePaddle:develop Jul 18, 2023
@SigureMo SigureMo deleted the dy2st/np125-eq branch July 18, 2023 03:54
cqulilujia pushed a commit to cqulilujia/Paddle that referenced this pull request Jul 24, 2023
wz1qqx pushed a commit to wz1qqx/Paddle that referenced this pull request Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants