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.13] support closure #69753

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

gouzil
Copy link
Member

@gouzil gouzil commented Nov 27, 2024

PR Category

Execute Infrastructure

PR Types

Others

Description

MAKE_FUNCTION 需要支持下面几种情况, 当前方案并不完美,没有与 cpython 保持一致

  • MAKE_FUNCTION 后跟随 SET_FUNCTION_ATTRIBUTE. 由于 closure 信息在此刻并不完整,所以无法使用 types.FunctionType 创建 func, 为此创建了一个中间层 UserCodeVariable 用于处理这类情况
 33           RESUME                   0

 34           LOAD_CONST               6 ((5, 3))
              LOAD_CONST               2 (<code object fn at 0x1034d25d0, file "/Users/gouzi/Documents/git/Paddle/test/sot/test_13_make_function.py", line 34>)
              MAKE_FUNCTION
              SET_FUNCTION_ATTRIBUTE   1 (defaults)
              STORE_FAST               1 (fn)

 37           LOAD_FAST                1 (fn)
              PUSH_NULL
              LOAD_CONST               3 (1)
              LOAD_CONST               4 (2)
              CALL                     2
              LOAD_FAST                1 (fn)
              PUSH_NULL
              LOAD_CONST               3 (1)
              LOAD_CONST               4 (2)
              LOAD_CONST               1 (3)
              LOAD_CONST               5 (('c',))
              CALL_KW                  3
              BINARY_OP                0 (+)
              LOAD_FAST                0 (x)
              BINARY_OP                0 (+)
              RETURN_VALUE

Disassembly of <code object fn at 0x1034d25d0, file "/Users/gouzi/Documents/git/Paddle/test/sot/test_13_make_function.py", line 34>:
 34           RESUME                   0

 35           LOAD_FAST_LOAD_FAST      1 (a, b)
              BINARY_OP                0 (+)
              LOAD_FAST                2 (c)
              BINARY_OP                0 (+)
              LOAD_FAST                3 (d)
              BINARY_OP                0 (+)
              RETURN_VALUE
  • MAKE_FUNCTION 后不跟随 SET_FUNCTION_ATTRIBUTE
 26           RESUME                   0

 27           LOAD_CONST               1 (<code object fn at 0x10167e790, file "/Users/gouzi/Documents/git/Paddle/test/sot/test_13_make_function.py", line 27>)
              MAKE_FUNCTION
              STORE_FAST               1 (fn)

 30           LOAD_FAST                1 (fn)
              PUSH_NULL
              LOAD_CONST               2 (1)
              LOAD_CONST               3 (2)
              LOAD_CONST               4 (3)
              LOAD_CONST               5 (4)
              CALL                     4
              LOAD_FAST                0 (x)
              BINARY_OP                0 (+)
              RETURN_VALUE

Disassembly of <code object fn at 0x10167e790, file "/Users/gouzi/Documents/git/Paddle/test/sot/test_13_make_function.py", line 27>:
 27           RESUME                   0

 28           LOAD_FAST_LOAD_FAST      1 (a, b)
              BINARY_OP                0 (+)
              LOAD_FAST                2 (c)
              BINARY_OP                0 (+)
              LOAD_FAST                3 (d)
              BINARY_OP                0 (+)
              RETURN_VALUE
  • SET_FUNCTION_ATTRIBUTE 后接着跟随 SET_FUNCTION_ATTRIBUTE
  --           MAKE_CELL                0 (y)

  62           RESUME                   0

  67           LOAD_CONST               3 ((5,))
               LOAD_FAST                0 (y)
               BUILD_TUPLE              1
               LOAD_CONST               1 (<code object local at 0x104d9aa30, file "/Users/gouzi/Documents/git/Paddle/test/sot/test_19_closure.py", line 67>)
               MAKE_FUNCTION
               SET_FUNCTION_ATTRIBUTE   8 (closure)
               SET_FUNCTION_ATTRIBUTE   1 (defaults)
               STORE_FAST               1 (local)

  72           LOAD_FAST                1 (local)
               PUSH_NULL
               LOAD_CONST               2 (1)
               CALL                     1
               RETURN_VALUE

Disassembly of <code object local at 0x104d9aa30, file "/Users/gouzi/Documents/git/Paddle/test/sot/test_19_closure.py", line 67>:
  --           COPY_FREE_VARS           1

  67           RESUME                   0

  69           LOAD_GLOBAL              0 (global_z)
               LOAD_CONST               1 (1)
               BINARY_OP               13 (+=)
               STORE_GLOBAL             0 (global_z)

  70           LOAD_FAST                0 (a)
               LOAD_GLOBAL              0 (global_z)
               BINARY_OP                0 (+)
               LOAD_FAST                1 (b)
               BINARY_OP                0 (+)
               LOAD_DEREF               2 (y)
               BINARY_OP                0 (+)
               RETURN_VALUE

TODO

  • 打开 test_19_closure

@gouzil gouzil requested a review from Copilot November 27, 2024 05:59
Copy link

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

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no suggestions.

Comments skipped due to low confidence (1)

python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py:1419

  • The phrase 'can not' should be corrected to 'cannot'.
# can not set annotation in python env, skip it
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 🐾

flag = 0
else:
flag = instr.arg
if len(codeobj.get_py_value().co_freevars) > 0:
Copy link
Member

Choose a reason for hiding this comment

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

后续考虑对 FunctionVariable 体系重构,以更加合适的方式来构造 function

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.

2 participants