diff --git a/paddle/fluid/pybind/jit.cc b/paddle/fluid/pybind/jit.cc index 274c4d795e29efd320b6391fb96fe28f436b3501..b0de61b1172852c6568bb125366b7c80c0abff07 100644 --- a/paddle/fluid/pybind/jit.cc +++ b/paddle/fluid/pybind/jit.cc @@ -146,9 +146,19 @@ static PyObject *_custom_eval_frame(PyThreadState *tstate, // NOTE: Cache is not supported now PyCodeObject *code = reinterpret_cast( PyObject_GetAttrString(result, "code")); - // Re-enable custom behavior - eval_frame_callback_set(callback); - return eval_custom_code(tstate, frame, code, throw_flag); + PyObject *disable_eval_frame = + PyObject_GetAttrString(result, "disable_eval_frame"); + if (disable_eval_frame != Py_True) { + // Re-enable custom behavior + eval_frame_callback_set(callback); + auto out = eval_custom_code(tstate, frame, code, throw_flag); + return out; + } else { + auto out = eval_custom_code(tstate, frame, code, throw_flag); + // Re-enable custom behavior + eval_frame_callback_set(callback); + return out; + } } else { // Re-enable custom behavior eval_frame_callback_set(callback); diff --git a/test/dygraph_to_static/test_eval_frame.py b/test/dygraph_to_static/test_eval_frame.py index 5c63517ee2feaf53cfef66579312fdd38a1445d2..66cb3404d6cbbc2b88a7b945ffdcc41bf333a16d 100644 --- a/test/dygraph_to_static/test_eval_frame.py +++ b/test/dygraph_to_static/test_eval_frame.py @@ -27,18 +27,22 @@ class TestEvalFrame(unittest.TestCase): pass def test_eval_frame(self): - CustomCode = collections.namedtuple("CustomCode", ["code"]) + CustomCode = collections.namedtuple( + "CustomCode", ["code", "disable_eval_frame"] + ) def mul(a, b): return a * b - code = CustomCode(mul.__code__) + code = CustomCode(mul.__code__, True) def callback(frame_obj): # Do your callback function here and return a object with `.code` if frame_obj.f_code.co_name == "add": return code - return CustomCode(code=frame_obj.f_code) # do nothing. + return CustomCode( + code=frame_obj.f_code, disable_eval_frame=True + ) # do nothing. def add(a, b): return a + b