Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
d1c8cd26
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d1c8cd26
编写于
6月 01, 2023
作者:
X
xiongkun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
function
上级
7aabdfd9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
42 addition
and
12 deletion
+42
-12
paddle/fluid/pybind/jit.cc
paddle/fluid/pybind/jit.cc
+35
-12
test/dygraph_to_static/test_eval_frame.py
test/dygraph_to_static/test_eval_frame.py
+7
-0
未找到文件。
paddle/fluid/pybind/jit.cc
浏览文件 @
d1c8cd26
...
...
@@ -17,6 +17,7 @@ limitations under the License. */
#include <Python.h>
#include <code.h>
#include <frameobject.h>
#include <internal/pycore_frame.h>
#include <object.h>
#include <pystate.h>
...
...
@@ -36,6 +37,12 @@ namespace py = pybind11;
namespace
paddle
{
namespace
pybind
{
#if PY_VERSION_HEX >= 0x030b0000
typedef
_PyInterpreterFrame
FrameObject
;
#else
typedef
PyFrameObject
FrameObject
;
#endif
#define unlikely(x) __builtin_expect((x), 0)
// Use static variable to save customed eval hook.
...
...
@@ -56,7 +63,7 @@ inline static void eval_frame_callback_set(PyObject *obj) {
// call python default eval frame to interpret current frame.
inline
static
PyObject
*
eval_frame_default
(
PyThreadState
*
tstate
,
Py
FrameObject
*
frame
,
FrameObject
*
frame
,
int
throw_flag
)
{
#if PY_VERSION_HEX >= 0x03090000
if
(
tstate
==
NULL
)
{
...
...
@@ -71,7 +78,7 @@ inline static PyObject *eval_frame_default(PyThreadState *tstate,
// Start a new frame and run code in this frame.
// Execute a piece of code by default frame-hook.
inline
static
PyObject
*
eval_custom_code
(
PyThreadState
*
tstate
,
Py
FrameObject
*
frame
,
FrameObject
*
frame
,
PyCodeObject
*
code
,
int
throw_flag
)
{
Py_ssize_t
ncells
=
0
;
...
...
@@ -79,18 +86,26 @@ inline static PyObject *eval_custom_code(PyThreadState *tstate,
Py_ssize_t
nlocals_new
=
code
->
co_nlocals
;
Py_ssize_t
nlocals_old
=
frame
->
f_code
->
co_nlocals
;
if
((
code
->
co_flags
&
CO_NOFREE
)
==
0
)
{
ncells
=
PyTuple_GET_SIZE
(
code
->
co_cellvars
);
nfrees
=
PyTuple_GET_SIZE
(
code
->
co_freevars
);
}
#if PY_VERSION_HEX >= 0x030b0000
ncells
=
code
->
co_ncellvars
;
nfrees
=
code
->
co_nfreevars
;
#else
ncells
=
PyTuple_GET_SIZE
(
code
->
co_cellvars
);
nfrees
=
PyTuple_GET_SIZE
(
code
->
co_freevars
);
#endif
PyFrameObject
*
shadow
=
PyFrame_New
(
tstate
,
code
,
frame
->
f_globals
,
NULL
);
if
(
shadow
==
NULL
)
{
return
NULL
;
}
#if PY_VERSION_HEX >= 0x030b0000
PyObject
**
fastlocals_old
=
frame
->
localsplus
;
PyObject
**
fastlocals_new
=
shadow
->
f_frame
->
localsplus
;
#else
PyObject
**
fastlocals_old
=
frame
->
f_localsplus
;
PyObject
**
fastlocals_new
=
shadow
->
f_localsplus
;
#endif
for
(
Py_ssize_t
i
=
0
;
i
<
nlocals_old
;
i
++
)
{
Py_XINCREF
(
fastlocals_old
[
i
]);
...
...
@@ -102,18 +117,26 @@ inline static PyObject *eval_custom_code(PyThreadState *tstate,
fastlocals_new
[
nlocals_new
+
i
]
=
fastlocals_old
[
nlocals_old
+
i
];
}
#if PY_VERSION_HEX >= 0x030b0000
PyObject
*
result
=
eval_frame_default
(
tstate
,
shadow
->
f_frame
,
throw_flag
);
#else
PyObject
*
result
=
eval_frame_default
(
tstate
,
shadow
,
throw_flag
);
#endif
Py_DECREF
(
shadow
);
return
result
;
}
static
PyObject
*
_custom_eval_frame
(
PyThreadState
*
tstate
,
Py
FrameObject
*
frame
,
FrameObject
*
frame
,
int
throw_flag
,
PyObject
*
callback
)
{
// https://peps.python.org/pep-0558/#fast-locals-proxy-implementation-details
// https://devguide.python.org/internals/interpreter/#all-sorts-of-variables
// https://peps.python.org/pep-0558/#fast-locals-proxy-implementation-details
// https://devguide.python.org/internals/interpreter/#all-sorts-of-variables
#if PY_VERSION_HEX >= 0x030b0000
if
(
PyFrame_FastToLocalsWithError
(
frame
->
frame_obj
)
<
0
)
{
#else
if
(
PyFrame_FastToLocalsWithError
(
frame
)
<
0
)
{
#endif
return
NULL
;
}
...
...
@@ -167,7 +190,7 @@ static PyObject *_custom_eval_frame(PyThreadState *tstate,
}
static
PyObject
*
_custom_eval_frame_shim
(
PyThreadState
*
tstate
,
Py
FrameObject
*
frame
,
FrameObject
*
frame
,
int
throw_flag
)
{
PyObject
*
callback
=
eval_frame_callback_get
();
...
...
@@ -180,12 +203,12 @@ static PyObject *_custom_eval_frame_shim(PyThreadState *tstate,
#if PY_VERSION_HEX >= 0x03090000
static
PyObject
*
custom_eval_frame_shim
(
PyThreadState
*
tstate
,
Py
FrameObject
*
frame
,
FrameObject
*
frame
,
int
throw_flag
)
{
return
_custom_eval_frame_shim
(
tstate
,
frame
,
throw_flag
);
}
#else
static
PyObject
*
custom_eval_frame_shim
(
Py
FrameObject
*
frame
,
int
throw_flag
)
{
static
PyObject
*
custom_eval_frame_shim
(
FrameObject
*
frame
,
int
throw_flag
)
{
PyThreadState
*
tstate
=
PyThreadState_GET
();
return
_custom_eval_frame_shim
(
tstate
,
frame
,
throw_flag
);
}
...
...
test/dygraph_to_static/test_eval_frame.py
浏览文件 @
d1c8cd26
...
...
@@ -15,6 +15,7 @@
import
collections
import
unittest
from
sys
import
version_info
import
paddle
...
...
@@ -27,6 +28,12 @@ class TestEvalFrame(unittest.TestCase):
pass
def
test_eval_frame
(
self
):
if
version_info
.
major
!=
3
or
(
version_info
.
minor
<=
8
or
version_info
.
minor
>=
11
):
print
(
"skip test_eval_frame, current only support 3.8 - 3.10"
)
return
CustomCode
=
collections
.
namedtuple
(
"CustomCode"
,
[
"code"
,
"disable_eval_frame"
]
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录