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] eval_frame support python 3.13 #69126

Merged
merged 10 commits into from
Nov 2, 2024

Conversation

gouzil
Copy link
Member

@gouzil gouzil commented Nov 1, 2024

PR Category

Execute Infrastructure

PR Types

New features

Description

改名

  • Internal_PyInterpreterFrame_GetLine -> Internal_PyUnstable_InterpreterFrame_GetLine
  • Internal_PyEvalFrameClearAndPop -> Internal_PyEval_FrameClearAndPop

TODO

======================================================================
ERROR: test_numpy_add (__main__.TestNumpyAdd.test_numpy_add)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/gouzi/miniconda3/envs/py313/lib/python3.13/contextlib.py", line 85, in inner
    return func(*args, **kwds)
  File "/Users/gouzi/Documents/git/Paddle/test/sot/test_01_basic.py", line 42, in test_numpy_add
    self.assert_results(numpy_add, x, y)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/Users/gouzi/Documents/git/Paddle/test/sot/test_case_base.py", line 71, in assert_results
    sym_output = symbolic_translate(func)(*inputs)
  File "/Users/gouzi/miniconda3/envs/py313/lib/python3.13/site-packages/paddle/jit/sot/translate.py", line 119, in impl
    return impl_sot(*args, **kwargs)
  File "/Users/gouzi/miniconda3/envs/py313/lib/python3.13/site-packages/paddle/jit/sot/translate.py", line 103, in impl_sot
    raise e
  File "/Users/gouzi/miniconda3/envs/py313/lib/python3.13/site-packages/paddle/jit/sot/translate.py", line 101, in impl_sot
    outs = fn(*args, **kwargs)
  File "/Users/gouzi/miniconda3/envs/py313/lib/python3.13/site-packages/paddle/jit/sot/translate.py", line 91, in callback
    return eval_frame_callback(frame, **kwargs)
  File "/Users/gouzi/miniconda3/envs/py313/lib/python3.13/site-packages/paddle/jit/sot/opcode_translator/eval_frame_callback.py", line 75, in eval_frame_callback
    custom_code = OpcodeExecutorCache()(frame, **kwargs)
  File "/Users/gouzi/miniconda3/envs/py313/lib/python3.13/site-packages/paddle/jit/sot/opcode_translator/executor/executor_cache.py", line 88, in __call__
    new_custom_code, guard_fn = self.translate(frame, **kwargs)
                                ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/Users/gouzi/miniconda3/envs/py313/lib/python3.13/site-packages/paddle/jit/sot/opcode_translator/executor/executor_cache.py", line 170, in translate
    custom_new_code, guard_fn = start_translate(frame, **kwargs)
                                ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/Users/gouzi/miniconda3/envs/py313/lib/python3.13/site-packages/paddle/jit/sot/opcode_translator/executor/executor_cache.py", line 219, in start_translate
    simulator = OpcodeExecutor(frame, **kwargs)
  File "/Users/gouzi/miniconda3/envs/py313/lib/python3.13/site-packages/paddle/jit/sot/opcode_translator/executor/opcode_executor.py", line 1617, in __init__
    super().__init__(frame.f_code, graph)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gouzi/miniconda3/envs/py313/lib/python3.13/site-packages/paddle/jit/sot/opcode_translator/executor/opcode_executor.py", line 372, in __init__
    self._prepare_virtual_env()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/gouzi/miniconda3/envs/py313/lib/python3.13/site-packages/paddle/jit/sot/opcode_translator/executor/opcode_executor.py", line 1663, in _prepare_virtual_env
    for name, value in self._frame.f_locals.items():
                       ^^^^^^^^^^^^^^^^^^^^
SystemError: error return without exception set

Copy link

paddle-bot bot commented Nov 1, 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 1, 2024
@gouzil gouzil changed the title [SOT] eval_frame support py3.13 [WIP][SOT] eval_frame support py3.13 Nov 1, 2024
Comment on lines 46 to 52
if sys.version_info >= (3, 13):
# in 3.13, the frame_obj.f_code is f_executable
if frame_obj.f_executable.co_name == "add":
return code
return CustomCode(
code=frame_obj.f_executable, disable_eval_frame=True
) # do nothing.
Copy link
Member

Choose a reason for hiding this comment

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

uv run -p 3.13 --no-project test.py
> /Users/nyakku/Projects/Paddle/test.py(6)fn()
-> breakpoint()
(Pdb) f = inspect.currentframe()
(Pdb) f.f_code
<code object <module> at 0x10431ca80, file "<stdin>", line 1>
(Pdb) f.f_executable
*** AttributeError: 'frame' object has no attribute 'f_executable'
(Pdb)

python 端界面上好像没改?好像还有 f_code

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.

2024-11-02 01:49:05   File "/usr/local/lib/python3.12/dist-packages/paddle/jit/sot/opcode_translator/executor/opcode_executor.py", line 1663, in _prepare_virtual_env
2024-11-02 01:49:05     for name, value in self._frame.f_locals.items():
2024-11-02 01:49:05                        ^^^^^^^^^^^^^^^^^^^^
2024-11-02 01:49:05 SystemError: error return without exception set

没太看出来,为啥会影响到 3.12

@SigureMo SigureMo changed the title [WIP][SOT] eval_frame support py3.13 [WIP][Typing][SOT] eval_frame support py3.13 Nov 1, 2024
#define PY_3_10_PLUS (PY_VERSION_HEX >= PY_3_10_0_HEX)
#define PY_3_11_PLUS (PY_VERSION_HEX >= PY_3_11_0_HEX)
#define PY_3_12_PLUS (PY_VERSION_HEX >= PY_3_12_0_HEX)
#define PY_3_13_PLUS (PY_VERSION_HEX >= PY_3_13_0_HEX)
Copy link
Member

Choose a reason for hiding this comment

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

没太看出来,为啥会影响到 3.12

得加括号,不然 !PY_3_13_PLUS 会有问题

PyObject *locals = Internal_PyFrame_GetLocals(frame, 0);
if (locals == NULL) {
return -1;
}
Py_DECREF(locals);
#endif
return 0;
}
Copy link
Member

@SigureMo SigureMo Nov 1, 2024

Choose a reason for hiding this comment

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

注意 3.13 是将 PyFrame_FastToLocalsWithError 变为无操作,因为这个接口会被弃用并删除,而我们这里在 3.11+ 以来对齐的是 _PyFrame_FastToLocalsWithError,这个接口在 python/cpython#115153 被直接删除,但并不是不需要了,而是使用了替代方案,主要是 _PyFrame_GetLocals

可以参考 python/cpython#115153 中的替换 _PyFrame_FastToLocalsWithError 的使用方式,我们可以也直接替换下,3.13 我们就不用编 Internal_PyFrame_FastToLocalsWithError 这个接口了

debug 版本的报错可能是因为这个原因,可以试下

改动动机可以看看 PEP 667

非 debug 版本 Linux 下验证通过,这个问题解决后感觉就可以合入了

Copy link
Member

Choose a reason for hiding this comment

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

现在没有使用 GetLocals 基本 case 能过,但后续需要注意下是否有问题

SigureMo
SigureMo previously approved these changes Nov 2, 2024
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 🐾

@PaddlePaddle PaddlePaddle locked and limited conversation to collaborators Nov 2, 2024
@PaddlePaddle PaddlePaddle unlocked this conversation Nov 2, 2024
@PaddlePaddle PaddlePaddle locked and limited conversation to collaborators Nov 2, 2024
@PaddlePaddle PaddlePaddle unlocked this conversation Nov 2, 2024
@gouzil gouzil changed the title [WIP][Typing][SOT] eval_frame support py3.13 [Typing][SOT] eval_frame support py3.13 Nov 2, 2024
@SigureMo
Copy link
Member

SigureMo commented Nov 2, 2024

现在使用 debug 版 python import paddle 后(与本 PR 无关),在程序退出时会挂掉

Debug memory block at address p=0x1066e44e0: API 'o'
    1 bytes originally requested
    The 7 pad bytes at p-7 are FORBIDDENBYTE, as expected.
    The 8 pad bytes at tail=0x1066e44e1 are FORBIDDENBYTE, as expected.
    Data at p: 00

Enable tracemalloc to get the memory block allocation traceback

Fatal Python error: _PyMem_DebugRawFree: bad ID: Allocated using API 'o', verified using API 'm'
Python runtime state: finalizing (tstate=0x000000010075cc00)

Current thread 0x00000001e9b77840 (most recent call first):
  Garbage-collecting
  <no Python frame>

问题确认升级 pybind11 到 2.13.6 可解

@SigureMo SigureMo changed the title [Typing][SOT] eval_frame support py3.13 [SOT][3.13] eval_frame support python 3.13 Nov 2, 2024
@SigureMo SigureMo merged commit 651e66b into PaddlePaddle:develop Nov 2, 2024
28 checks passed
@gouzil gouzil deleted the sot/support_py_3_13 branch November 22, 2024 15:05
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