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

[SOT] Fix LOAD_METHOD stack layout when bound instance override the __getattr__ #58956

Conversation

SigureMo
Copy link
Member

@SigureMo SigureMo commented Nov 13, 2023

PR types

Bug fixes

PR changes

Others

Description

修复在 LOAD_METHOD 模拟执行时与 Python 行为不一致的问题

LOAD_METHOD 的实例对象类上重载了 __getattr__ 时,method 并不会按照 func | self | arg1 | arg2 | ... | argN(左侧为栈底)的「method」布局,而是按照 NULL | method | arg1 | arg2 | ... | argN 的「function」布局,因此模拟执行与真实执行会有一点 diff,在生成代码将栈上 Variable 恢复的时候可能出问题

Python 3.8 的处理代码见:

https://github.com/python/cpython/blob/c1c6bedfd3ee66ad208e0cd9cdd732374c95b83d/Objects/object.c#L1149-L1153

Layer 本身重载了 __getattr__,因此用户继承的自定义类上的方法都会有这个问题,比如

class MyLayer(paddle.nn.Layer):
    def __init__(self):
        ...

    def forward_features(self, x):
        return x.numpy()

    def forward(self, x):
        return self.forward_features(x)

这里的 self.forward_features(x) 的 LOAD_METHOD 会因为 Layer 重载了 __getattr__ 而变成 NULL | self.forward_features 而不是 MyLayer.forward_features | self,此时发生 Fallback 而生成的代码就有可能出问题(最新的 fallback 会生成代码),因为模拟执行并不知道有一个 NULL,因此恢复时候将该 NULL STORE_FAST 到一个 local var,之后 LOAD 时候就会报错

因此按照类似 Python 的处理方式,当用户重载了 __getattr__ 时,同样认为它是「function」layout

PCard-66972

Copy link

paddle-bot bot commented Nov 13, 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

@2742195759 2742195759 left a comment

Choose a reason for hiding this comment

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

LGTM

@2742195759 2742195759 merged commit fdfd63b into PaddlePaddle:develop Nov 14, 2023
28 checks passed
@2742195759 2742195759 deleted the sot/fix-simulation-load-method-override-getattr branch November 14, 2023 06:15
danleifeng pushed a commit to danleifeng/Paddle that referenced this pull request Nov 14, 2023
… `__getattr__` (PaddlePaddle#58956)

* [SOT] Fix LOAD_METHOD stack layout when bound instance override the `__getattr__`

* add unittest
SecretXV pushed a commit to SecretXV/Paddle that referenced this pull request Nov 28, 2023
… `__getattr__` (PaddlePaddle#58956)

* [SOT] Fix LOAD_METHOD stack layout when bound instance override the `__getattr__`

* add unittest
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.

None yet

2 participants