Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

update skip files #408

Merged
merged 14 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions sot/opcode_translator/executor/guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]

Expand Down
4 changes: 2 additions & 2 deletions sot/opcode_translator/executor/opcode_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand Down
63 changes: 33 additions & 30 deletions sot/opcode_translator/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,44 +47,47 @@ def eval_frame_callback(frame, **kwargs) -> CustomCode:
return CustomCode(None, True)

if need_skip(frame):
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",
)
used_code = frame.f_code
log(3, f"[eval_frame_callback] skip {frame.f_code}\n")
custom_code = CustomCode(None, False)
new_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",
)
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))
new_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(new_code)
):
log(
3,
"[transform] Code has found no graph, block it.",
"[eval_frame_callback] Code has no graph, block it.",
)
return CustomCode(None, True)

Expand Down
42 changes: 31 additions & 11 deletions sot/utils/code_status.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import inspect
from enum import Enum

from .utils import Singleton
import paddle

from .utils import Singleton, log


class CodeState(Enum):
Expand All @@ -21,13 +23,26 @@ 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):
def is_code_without_graph(self, code):
if code not in self.code_map:
info = CodeInfo()
self.code_map[code] = info
Expand All @@ -36,21 +51,26 @@ 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.state == CodeState.UNKNOW and info.counter > 10:
info.state = CodeState.WITHOUT_GRAPH

def trace_back_frames(self):
frame = inspect.currentframe()
while frame.f_back is not None:
frame = frame.f_back
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}\n",
)
info.state = CodeState.WITH_GRAPH
79 changes: 0 additions & 79 deletions tests/test_code_info.py

This file was deleted.

Loading
Loading