From f8383c09033f36b68037679ad85772dca25cd658 Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Thu, 28 Sep 2023 03:35:39 +0000 Subject: [PATCH 01/14] update --- sot/opcode_translator/skip_files.py | 2 +- sot/opcode_translator/transform.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sot/opcode_translator/skip_files.py b/sot/opcode_translator/skip_files.py index fd38b4253..58053b611 100644 --- a/sot/opcode_translator/skip_files.py +++ b/sot/opcode_translator/skip_files.py @@ -119,7 +119,7 @@ def _module_dir(m: types.ModuleType): customed_skip_code = set() -no_skip_code = {paddle.nn.Sequential.forward.__code__} +no_skip_code = {paddle.nn.Sequential.forward.__code__, paddle.nn.Layer.__call__} def need_skip_path(filepath: str) -> bool: diff --git a/sot/opcode_translator/transform.py b/sot/opcode_translator/transform.py index b17d6bbb2..3a9606f44 100644 --- a/sot/opcode_translator/transform.py +++ b/sot/opcode_translator/transform.py @@ -47,7 +47,7 @@ def eval_frame_callback(frame, **kwargs) -> CustomCode: return CustomCode(None, True) if need_skip(frame): - return CustomCode(None, False) + return CustomCode(None, True) log( 2, From ac469c03e60bd03fb22f9968474c854ac6d278d1 Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Thu, 28 Sep 2023 03:56:23 +0000 Subject: [PATCH 02/14] update --- sot/opcode_translator/executor/guard.py | 6 +++--- sot/opcode_translator/skip_files.py | 6 +++--- sot/opcode_translator/transform.py | 5 +++-- sot/utils/code_status.py | 9 +++++++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/sot/opcode_translator/executor/guard.py b/sot/opcode_translator/executor/guard.py index f0f8106c2..ad719b6e3 100644 --- a/sot/opcode_translator/executor/guard.py +++ b/sot/opcode_translator/executor/guard.py @@ -33,11 +33,11 @@ class StringifyExpression: Used to store string based expressions for generating Guard. """ - def __init__(self, str_expr, format_args, free_vars): - expr = str_expr.format(*[arg.expr for arg in format_args]) + def __init__(self, str_expr, sub_exprs, free_vars): + expr = str_expr.format(*[arg.expr for arg in sub_exprs]) self.expr = current_tmp_name_records().add_tmp_var(expr) self.debug_expr = str_expr.format( - *[arg.debug_expr for arg in format_args] + *[arg.debug_expr for arg in sub_exprs] ) self.free_vars = free_vars diff --git a/sot/opcode_translator/skip_files.py b/sot/opcode_translator/skip_files.py index 58053b611..0998e543f 100644 --- a/sot/opcode_translator/skip_files.py +++ b/sot/opcode_translator/skip_files.py @@ -145,10 +145,10 @@ def skip_function(function): def need_skip(frame): pycode = frame.f_code if pycode in no_skip_code: - return False + return False, False if pycode in customed_skip_code: log(3, f"Skip frame by code: {pycode}\n") - return True + return True, False filename = pycode.co_filename if sys.version_info >= (3, 11) and filename.startswith(" CustomCode: if frame.f_code.co_flags & 0x20 > 0: return CustomCode(None, True) - if need_skip(frame): - return CustomCode(None, True) + skip, enable_eval_frame = need_skip(frame) + if skip: + return CustomCode(None, enable_eval_frame) log( 2, diff --git a/sot/utils/code_status.py b/sot/utils/code_status.py index aa498de17..fd8180299 100644 --- a/sot/utils/code_status.py +++ b/sot/utils/code_status.py @@ -1,7 +1,7 @@ import inspect from enum import Enum -from .utils import Singleton +from .utils import Singleton, log class CodeState(Enum): @@ -44,6 +44,7 @@ def visit(self, code): info = self.code_map[code] info.counter += 1 if info.state == CodeState.UNKNOW and info.counter > 10: + log(3, f"[CodeStatus] Switch state to WITHOUT_GRAPH for {code}") info.state = CodeState.WITHOUT_GRAPH def trace_back_frames(self): @@ -53,4 +54,8 @@ def trace_back_frames(self): code = frame.f_code if code in self.code_map: info = self.code_map[code] - info.state = CodeState.WITH_GRAPH + if info.state != CodeState.WITH_GRAPH: + log( + 3, f"[CodeStatus] Switch state to WITH_GRAPH for {code}" + ) + info.state = CodeState.WITH_GRAPH From fb48fa7a1e526977c013a85bcbe75c29832cfedf Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Thu, 28 Sep 2023 04:11:04 +0000 Subject: [PATCH 03/14] update --- sot/opcode_translator/executor/guard.py | 2 +- sot/opcode_translator/executor/opcode_executor.py | 4 ++-- sot/opcode_translator/skip_files.py | 5 ++++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/sot/opcode_translator/executor/guard.py b/sot/opcode_translator/executor/guard.py index ad719b6e3..1766dc17d 100644 --- a/sot/opcode_translator/executor/guard.py +++ b/sot/opcode_translator/executor/guard.py @@ -92,7 +92,7 @@ def analyse_expresions(stringify_exprs, tmp_names): lambda_string += str_expr.debug_expr + " and " free_vars = union_free_vars(free_vars, str_expr.free_vars) - func_string += f" return {func_result[:-5]}\n" + func_string += f" return {func_result[:-5]}" return func_string, free_vars, lambda_string[:-5] diff --git a/sot/opcode_translator/executor/opcode_executor.py b/sot/opcode_translator/executor/opcode_executor.py index 324db5672..f887a6c0b 100644 --- a/sot/opcode_translator/executor/opcode_executor.py +++ b/sot/opcode_translator/executor/opcode_executor.py @@ -174,7 +174,7 @@ def lookup( if guard_result: log( 2, - f"[Cache]: Cache hit, Guard is {getattr(guard_fn, 'expr', 'None')}\n", + f"[Cache]: Cache hit, Guard is \n{getattr(guard_fn, 'expr', 'None')}\n", ) return custom_code else: @@ -184,7 +184,7 @@ def lookup( ) log( 2, - f"[Cache]: Cache miss, Guard is {getattr(guard_fn, 'expr', 'None')}\n", + f"[Cache]: Cache miss, Guard is \n{getattr(guard_fn, 'expr', 'None')}\n", ) log_do( 2, diff --git a/sot/opcode_translator/skip_files.py b/sot/opcode_translator/skip_files.py index 0998e543f..d71cfcb4d 100644 --- a/sot/opcode_translator/skip_files.py +++ b/sot/opcode_translator/skip_files.py @@ -119,7 +119,10 @@ def _module_dir(m: types.ModuleType): customed_skip_code = set() -no_skip_code = {paddle.nn.Sequential.forward.__code__, paddle.nn.Layer.__call__} +no_skip_code = { + paddle.nn.Sequential.forward.__code__, + paddle.nn.Layer.__call__.__code__, +} def need_skip_path(filepath: str) -> bool: From 8afee5957413efd4ed30206a1e196883532bada7 Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Thu, 28 Sep 2023 04:16:31 +0000 Subject: [PATCH 04/14] update --- sot/opcode_translator/skip_files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sot/opcode_translator/skip_files.py b/sot/opcode_translator/skip_files.py index d71cfcb4d..6c4217c3a 100644 --- a/sot/opcode_translator/skip_files.py +++ b/sot/opcode_translator/skip_files.py @@ -148,7 +148,7 @@ def skip_function(function): def need_skip(frame): pycode = frame.f_code if pycode in no_skip_code: - return False, False + return True, False if pycode in customed_skip_code: log(3, f"Skip frame by code: {pycode}\n") return True, False From 4e6a7596d21e9da5f848defe0e1f7b6c9d970fcb Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Thu, 28 Sep 2023 05:26:32 +0000 Subject: [PATCH 05/14] update --- sot/opcode_translator/skip_files.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sot/opcode_translator/skip_files.py b/sot/opcode_translator/skip_files.py index 6c4217c3a..268adc9f6 100644 --- a/sot/opcode_translator/skip_files.py +++ b/sot/opcode_translator/skip_files.py @@ -121,6 +121,9 @@ def _module_dir(m: types.ModuleType): no_skip_code = { paddle.nn.Sequential.forward.__code__, +} + +paddle_api_with_call_user_code = { paddle.nn.Layer.__call__.__code__, } @@ -148,6 +151,8 @@ def skip_function(function): def need_skip(frame): pycode = frame.f_code if pycode in no_skip_code: + return False, False + if pycode in paddle_api_with_call_user_code: return True, False if pycode in customed_skip_code: log(3, f"Skip frame by code: {pycode}\n") From 84fde8da017da13cd5b9b590e3cf99a36b9152aa Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Thu, 28 Sep 2023 06:17:36 +0000 Subject: [PATCH 06/14] update --- sot/opcode_translator/skip_files.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sot/opcode_translator/skip_files.py b/sot/opcode_translator/skip_files.py index 268adc9f6..bc9facc1c 100644 --- a/sot/opcode_translator/skip_files.py +++ b/sot/opcode_translator/skip_files.py @@ -121,6 +121,7 @@ def _module_dir(m: types.ModuleType): no_skip_code = { paddle.nn.Sequential.forward.__code__, + paddle.nn.Layer._dygraph_call_func.__code__, } paddle_api_with_call_user_code = { From 763d8f0e52013c96a1db658e59c313f8b0246d9e Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Thu, 28 Sep 2023 06:19:07 +0000 Subject: [PATCH 07/14] update --- sot/opcode_translator/skip_files.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sot/opcode_translator/skip_files.py b/sot/opcode_translator/skip_files.py index bc9facc1c..2ff25f30b 100644 --- a/sot/opcode_translator/skip_files.py +++ b/sot/opcode_translator/skip_files.py @@ -119,12 +119,12 @@ def _module_dir(m: types.ModuleType): customed_skip_code = set() -no_skip_code = { +paddle_api_simulate_whole_function = { paddle.nn.Sequential.forward.__code__, paddle.nn.Layer._dygraph_call_func.__code__, } -paddle_api_with_call_user_code = { +paddle_api_skip_and_open_eval_frame = { paddle.nn.Layer.__call__.__code__, } @@ -151,9 +151,9 @@ def skip_function(function): def need_skip(frame): pycode = frame.f_code - if pycode in no_skip_code: + if pycode in paddle_api_simulate_whole_function: return False, False - if pycode in paddle_api_with_call_user_code: + if pycode in paddle_api_skip_and_open_eval_frame: return True, False if pycode in customed_skip_code: log(3, f"Skip frame by code: {pycode}\n") From 9f8a2e2fa4ffaae4eaa7b4b8caab6265a257fb83 Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Thu, 28 Sep 2023 06:37:41 +0000 Subject: [PATCH 08/14] update --- sot/opcode_translator/skip_files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sot/opcode_translator/skip_files.py b/sot/opcode_translator/skip_files.py index 2ff25f30b..80e2862e9 100644 --- a/sot/opcode_translator/skip_files.py +++ b/sot/opcode_translator/skip_files.py @@ -121,11 +121,11 @@ def _module_dir(m: types.ModuleType): paddle_api_simulate_whole_function = { paddle.nn.Sequential.forward.__code__, - paddle.nn.Layer._dygraph_call_func.__code__, } paddle_api_skip_and_open_eval_frame = { paddle.nn.Layer.__call__.__code__, + paddle.nn.Layer._dygraph_call_func.__code__, } From 57626b2257df7d68a1b028656ce34260b9fcd51c Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Sun, 8 Oct 2023 03:34:44 +0000 Subject: [PATCH 09/14] update --- sot/opcode_translator/skip_files.py | 19 +++++-------------- sot/opcode_translator/transform.py | 5 ++--- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/sot/opcode_translator/skip_files.py b/sot/opcode_translator/skip_files.py index 80e2862e9..fd38b4253 100644 --- a/sot/opcode_translator/skip_files.py +++ b/sot/opcode_translator/skip_files.py @@ -119,14 +119,7 @@ def _module_dir(m: types.ModuleType): customed_skip_code = set() -paddle_api_simulate_whole_function = { - paddle.nn.Sequential.forward.__code__, -} - -paddle_api_skip_and_open_eval_frame = { - paddle.nn.Layer.__call__.__code__, - paddle.nn.Layer._dygraph_call_func.__code__, -} +no_skip_code = {paddle.nn.Sequential.forward.__code__} def need_skip_path(filepath: str) -> bool: @@ -151,13 +144,11 @@ def skip_function(function): def need_skip(frame): pycode = frame.f_code - if pycode in paddle_api_simulate_whole_function: - return False, False - if pycode in paddle_api_skip_and_open_eval_frame: - return True, False + if pycode in no_skip_code: + return False if pycode in customed_skip_code: log(3, f"Skip frame by code: {pycode}\n") - return True, False + return True filename = pycode.co_filename if sys.version_info >= (3, 11) and filename.startswith(" CustomCode: if frame.f_code.co_flags & 0x20 > 0: return CustomCode(None, True) - skip, enable_eval_frame = need_skip(frame) - if skip: - return CustomCode(None, enable_eval_frame) + if need_skip(frame): + return CustomCode(None, False) log( 2, From fc51069e9aceffad42321ad2a65acd936145d5a3 Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Sun, 8 Oct 2023 03:41:12 +0000 Subject: [PATCH 10/14] update --- sot/opcode_translator/transform.py | 5 ++++- sot/utils/code_status.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/sot/opcode_translator/transform.py b/sot/opcode_translator/transform.py index b17d6bbb2..ca2df232c 100644 --- a/sot/opcode_translator/transform.py +++ b/sot/opcode_translator/transform.py @@ -47,7 +47,10 @@ def eval_frame_callback(frame, **kwargs) -> CustomCode: return CustomCode(None, True) if need_skip(frame): - return CustomCode(None, False) + if CodeStatus().check_code(frame.f_code): + return CustomCode(None, True) + else: + return CustomCode(None, False) log( 2, diff --git a/sot/utils/code_status.py b/sot/utils/code_status.py index fd8180299..69278f653 100644 --- a/sot/utils/code_status.py +++ b/sot/utils/code_status.py @@ -43,7 +43,7 @@ def check_code(self, code): def visit(self, code): info = self.code_map[code] info.counter += 1 - if info.state == CodeState.UNKNOW and info.counter > 10: + if info.counter > 10: log(3, f"[CodeStatus] Switch state to WITHOUT_GRAPH for {code}") info.state = CodeState.WITHOUT_GRAPH From c5197ffddf16f1365d5d129b44c673f59f1757d8 Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Sun, 8 Oct 2023 07:46:30 +0000 Subject: [PATCH 11/14] update --- sot/opcode_translator/transform.py | 59 +++++++++++---------- sot/utils/code_status.py | 17 +++++- tests/test_code_info.py | 83 ++++++++++++++++++++++++++---- 3 files changed, 117 insertions(+), 42 deletions(-) diff --git a/sot/opcode_translator/transform.py b/sot/opcode_translator/transform.py index ca2df232c..11e6bac01 100644 --- a/sot/opcode_translator/transform.py +++ b/sot/opcode_translator/transform.py @@ -47,39 +47,38 @@ def eval_frame_callback(frame, **kwargs) -> CustomCode: return CustomCode(None, True) if need_skip(frame): - if CodeStatus().check_code(frame.f_code): - return CustomCode(None, True) - else: - return CustomCode(None, False) - - log( - 2, - "[eval_frame_callback] start to translate: " - + str(frame.f_code) - + "\n", - ) - log_do(4, partial(print_locals, frame)) - - log(3, f"[transform] OriginCode: {frame.f_code.co_name}\n") - log_do(3, lambda: dis.dis(frame.f_code)) - - custom_code = InstructionTranslatorCache()(frame, **kwargs) - - if custom_code.code is None: - log( - 3, - "[transform] NewCode (same as origin code): " - + frame.f_code.co_name - + "\n", - ) + log(3, f"[eval_frame_callback] skip {frame.f_code}\n") + custom_code = CustomCode(None, False) used_code = frame.f_code else: log( - 3, - "[transform] NewCode: " + custom_code.code.co_name + "\n", + 2, + "[eval_frame_callback] start to translate: " + + str(frame.f_code) + + "\n", ) - log_do(3, lambda: dis.dis(custom_code.code)) - used_code = custom_code.code + log_do(4, partial(print_locals, frame)) + + log(3, f"[transform] OriginCode: {frame.f_code.co_name}\n") + log_do(3, lambda: dis.dis(frame.f_code)) + + custom_code = InstructionTranslatorCache()(frame, **kwargs) + + if custom_code.code is None: + log( + 3, + "[transform] NewCode (same as origin code): " + + frame.f_code.co_name + + "\n", + ) + used_code = frame.f_code + else: + log( + 3, + "[transform] NewCode: " + custom_code.code.co_name + "\n", + ) + log_do(3, lambda: dis.dis(custom_code.code)) + used_code = custom_code.code # just check those codes which need open eval_frame if custom_code.disable_eval_frame is False and CodeStatus().check_code( @@ -87,7 +86,7 @@ def eval_frame_callback(frame, **kwargs) -> CustomCode: ): log( 3, - "[transform] Code has found no graph, block it.", + "[eval_frame_callback] Code has no graph, block it.", ) return CustomCode(None, True) diff --git a/sot/utils/code_status.py b/sot/utils/code_status.py index 69278f653..556fd9be8 100644 --- a/sot/utils/code_status.py +++ b/sot/utils/code_status.py @@ -1,6 +1,8 @@ import inspect from enum import Enum +import paddle + from .utils import Singleton, log @@ -21,11 +23,24 @@ def __repr__(self): @Singleton class CodeStatus: + WITH_GRAPH_API = [ + paddle.nn.Layer.__call__.__code__, + paddle.nn.Layer._dygraph_call_func.__code__, + ] + def __init__(self): self.code_map = {} + self.setup_code_map() + + def setup_code_map(self): + for code in self.WITH_GRAPH_API: + info = CodeInfo() + info.state = CodeState.WITH_GRAPH + self.code_map[code] = info def clear(self): self.code_map.clear() + self.setup_code_map() def check_code(self, code): if code not in self.code_map: @@ -43,7 +58,7 @@ def check_code(self, code): def visit(self, code): info = self.code_map[code] info.counter += 1 - if info.counter > 10: + if info.counter >= 10: log(3, f"[CodeStatus] Switch state to WITHOUT_GRAPH for {code}") info.state = CodeState.WITHOUT_GRAPH diff --git a/tests/test_code_info.py b/tests/test_code_info.py index 52cc58769..4a24f3050 100644 --- a/tests/test_code_info.py +++ b/tests/test_code_info.py @@ -4,7 +4,8 @@ import paddle import sot -from sot.utils import CodeStatus +from sot.opcode_translator.skip_files import skip_function +from sot.utils.code_status import CodeState, CodeStatus class SimpleNet1(paddle.nn.Layer): @@ -48,20 +49,26 @@ def run_net(net, x): class TestCodeInfo(TestCaseBase): - def _analyse_code_info(self, code_map): - return {k.co_name: str(v.state) for k, v in code_map.items()} - def test_case_1(self): CodeStatus().clear() net = SimpleNet1() inp = paddle.rand((10, 10)) self.assert_results(run_net, net, inp) - code_infos = self._analyse_code_info(CodeStatus().code_map) - states = list(code_infos.values()) + code_map = CodeStatus().code_map + states = [] + for k, v in code_map.items(): + if k.co_name.startswith("#") or k.co_name.startswith("$"): + states.append(v) + elif k in CodeStatus().WITH_GRAPH_API: + assert v.state == CodeState.WITH_GRAPH + else: + assert v.state == CodeState.WITHOUT_GRAPH # run_net, forward, loop body, resumed part2 in loop body - assert len([v for v in states if v == "CodeState.WITH_GRAPH"]) == 4 + assert len([v for v in states if v.state == CodeState.WITH_GRAPH]) == 4 # resumed part1 in loop body - assert len([v for v in states if v == "CodeState.WITHOUT_GRAPH"]) == 1 + assert ( + len([v for v in states if v.state == CodeState.WITHOUT_GRAPH]) == 1 + ) def test_case_2(self): with strict_mode_guard(0): @@ -69,10 +76,64 @@ def test_case_2(self): net = SimpleNet2() inp = paddle.rand((10, 10)) self.assert_results(run_net, net, inp) - code_infos = self._analyse_code_info(CodeStatus().code_map) - states = list(code_infos.values()) + code_map = CodeStatus().code_map + states = [] + for k, v in code_map.items(): + if k.co_name.startswith("#") or k.co_name.startswith("$"): + states.append(v) + elif k in CodeStatus().WITH_GRAPH_API: + assert v.state == CodeState.WITH_GRAPH + else: + assert v.state == CodeState.WITHOUT_GRAPH # no graph found because fallback (paddle api will not enter simulate) - assert len([v for v in states if v == "CodeState.WITH_GRAPH"]) == 0 + assert ( + len([v for v in states if v.state == CodeState.WITH_GRAPH]) == 0 + ) + + +def no_skip_func_0(x): + return x + 1 + + +def skipped_func_0(): + pass + + +def skipped_func_1(x): + return x + 1 + + +def skipped_func_2(x): + return no_skip_func_0(x) + + +def call_skipped_func_0(x): + for i in range(15): + skipped_func_0() + x = skipped_func_1(x) + x = skipped_func_2(x) + return x + + +skip_function(skipped_func_0) +skip_function(skipped_func_1) +skip_function(skipped_func_2) +skip_function(call_skipped_func_0) + + +class TestDisableSkippedFrame(TestCaseBase): + def test_case_0(self): + CodeStatus().clear() + x = paddle.to_tensor([1]) + self.assert_results(call_skipped_func_0, x) + code_map = CodeStatus().code_map + assert ( + code_map[skipped_func_0.__code__].state == CodeState.WITHOUT_GRAPH + ) + assert ( + code_map[skipped_func_1.__code__].state == CodeState.WITHOUT_GRAPH + ) + assert code_map[skipped_func_2.__code__].state == CodeState.WITH_GRAPH if __name__ == "__main__": From e2effa4410430470431eeffc748bf769598f73bb Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Sun, 8 Oct 2023 07:49:45 +0000 Subject: [PATCH 12/14] update --- sot/utils/code_status.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sot/utils/code_status.py b/sot/utils/code_status.py index 556fd9be8..fe65d5128 100644 --- a/sot/utils/code_status.py +++ b/sot/utils/code_status.py @@ -59,7 +59,7 @@ def visit(self, code): info = self.code_map[code] info.counter += 1 if info.counter >= 10: - log(3, f"[CodeStatus] Switch state to WITHOUT_GRAPH for {code}") + log(3, f"[CodeStatus] Switch state to WITHOUT_GRAPH for {code}\n") info.state = CodeState.WITHOUT_GRAPH def trace_back_frames(self): @@ -71,6 +71,7 @@ def trace_back_frames(self): info = self.code_map[code] if info.state != CodeState.WITH_GRAPH: log( - 3, f"[CodeStatus] Switch state to WITH_GRAPH for {code}" + 3, + f"[CodeStatus] Switch state to WITH_GRAPH for {code}\n", ) info.state = CodeState.WITH_GRAPH From 9f0cd4bf461702228c24a32a8e9b512be4345bc9 Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Sun, 8 Oct 2023 08:25:38 +0000 Subject: [PATCH 13/14] update --- sot/opcode_translator/transform.py | 5 +++-- sot/utils/code_status.py | 19 +++++++++---------- ...{test_code_info.py => test_code_status.py} | 0 3 files changed, 12 insertions(+), 12 deletions(-) rename tests/{test_code_info.py => test_code_status.py} (100%) diff --git a/sot/opcode_translator/transform.py b/sot/opcode_translator/transform.py index 11e6bac01..b79cb8985 100644 --- a/sot/opcode_translator/transform.py +++ b/sot/opcode_translator/transform.py @@ -81,8 +81,9 @@ def eval_frame_callback(frame, **kwargs) -> CustomCode: used_code = custom_code.code # just check those codes which need open eval_frame - if custom_code.disable_eval_frame is False and CodeStatus().check_code( - used_code + if ( + custom_code.disable_eval_frame is False + and CodeStatus().is_code_without_graph(used_code) ): log( 3, diff --git a/sot/utils/code_status.py b/sot/utils/code_status.py index fe65d5128..9b4438288 100644 --- a/sot/utils/code_status.py +++ b/sot/utils/code_status.py @@ -42,7 +42,7 @@ def clear(self): self.code_map.clear() self.setup_code_map() - def check_code(self, code): + def is_code_without_graph(self, code): if code not in self.code_map: info = CodeInfo() self.code_map[code] = info @@ -51,17 +51,16 @@ def check_code(self, code): if info.state == CodeState.WITHOUT_GRAPH: return True - elif info.state == CodeState.UNKNOW: - self.visit(code) + if info.state == CodeState.UNKNOW: + info.counter += 1 + if info.counter >= 10: + log( + 3, + f"[CodeStatus] Switch state to WITHOUT_GRAPH for {code}\n", + ) + info.state = CodeState.WITHOUT_GRAPH return False - def visit(self, code): - info = self.code_map[code] - info.counter += 1 - if info.counter >= 10: - log(3, f"[CodeStatus] Switch state to WITHOUT_GRAPH for {code}\n") - info.state = CodeState.WITHOUT_GRAPH - def trace_back_frames(self): frame = inspect.currentframe() while frame.f_back is not None: diff --git a/tests/test_code_info.py b/tests/test_code_status.py similarity index 100% rename from tests/test_code_info.py rename to tests/test_code_status.py From fe6ad7eac155ea257c7056a2529e37e3d5cff99b Mon Sep 17 00:00:00 2001 From: feifei-111 <2364819892@qq.com> Date: Sun, 8 Oct 2023 08:27:17 +0000 Subject: [PATCH 14/14] update --- sot/opcode_translator/transform.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sot/opcode_translator/transform.py b/sot/opcode_translator/transform.py index b79cb8985..70b55f16f 100644 --- a/sot/opcode_translator/transform.py +++ b/sot/opcode_translator/transform.py @@ -49,7 +49,7 @@ def eval_frame_callback(frame, **kwargs) -> CustomCode: if need_skip(frame): log(3, f"[eval_frame_callback] skip {frame.f_code}\n") custom_code = CustomCode(None, False) - used_code = frame.f_code + new_code = frame.f_code else: log( 2, @@ -71,19 +71,19 @@ def eval_frame_callback(frame, **kwargs) -> CustomCode: + frame.f_code.co_name + "\n", ) - used_code = frame.f_code + new_code = frame.f_code else: log( 3, "[transform] NewCode: " + custom_code.code.co_name + "\n", ) log_do(3, lambda: dis.dis(custom_code.code)) - used_code = custom_code.code + new_code = custom_code.code # just check those codes which need open eval_frame if ( custom_code.disable_eval_frame is False - and CodeStatus().is_code_without_graph(used_code) + and CodeStatus().is_code_without_graph(new_code) ): log( 3,