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][3.12] fix codegen out of range about generating LOAD_ATTR in Python 3.12 #62176

Merged
merged 6 commits into from
Feb 29, 2024

Conversation

diadestiny
Copy link
Contributor

@diadestiny diadestiny commented Feb 28, 2024

PR types

Others

PR changes

Others

Description

python 3.12中,test_21_global.py单测codegen超出范围报错:ValueError: bytes must be in range(0, 256)
排查发现是因为side_effect文件的dict默认调用gen_load_method,对应的opcode是262 所以出错;3.12中dict.update/clear函数调用不会生成LOAD_METHOD字节码,所以改成生成LOAD_ATTR,而dict的update/clear方法需要生成LOAD_ATTR额外push NULL;

TODO: test_inplace_api.py存在类似codegen问题和POP_JUMP_FORWARD_IF_FALSE不支持生成问题

Copy link

paddle-bot bot commented Feb 28, 2024

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

@paddle-bot paddle-bot bot added the contributor External developers label Feb 28, 2024
@SigureMo SigureMo self-assigned this Feb 28, 2024
Comment on lines 745 to 752
def gen_load_attr(self, name: str, push_null=False):
if name not in self._code_options["co_names"]:
self._code_options["co_names"].append(name)
idx = self._code_options["co_names"].index(name)
if sys.version_info >= (3, 12):
idx <<= 1
if push_null:
idx |= 1
Copy link
Member

Choose a reason for hiding this comment

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

LOAD_GLOBAL 不同,LOAD_ATTR 在 3.12 的最后一位用于表示 is_method,所以变量名需要修改下

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 127 to 130
if sys.version_info >= (3, 12):
codegen.gen_load_attr("update", True)
else:
codegen.gen_load_method("update")
Copy link
Member

Choose a reason for hiding this comment

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

不建议在这里做版本分发,可以考虑在 gen_load_method 中将 3.12+ 行为分发到 gen_load_attr(name, is_method)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

idx = self._code_options["co_names"].index(name)
return self.add_instr("LOAD_METHOD", arg=idx, argval=name)
if sys.version_info >= (3, 12):
self.gen_load_attr(name, True)
Copy link
Member

Choose a reason for hiding this comment

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

这里也加一个 return 吧,不然两个分支返回类型是不一致的

另外,加 return 的话,可以改写成 Early Return 的形式,去掉 else 及缩进

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

LGTMeow

@SigureMo SigureMo merged commit 473f7ba into PaddlePaddle:develop Feb 29, 2024
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants