Skip to content

Commit d3ab9c6

Browse files
generatedunixname499836121meta-codesync[bot]
authored andcommitted
Fix: Dynamo log always emits ANSI color codes into torch_compile_debug/torchdynamo/debug.log due to colored=True in lazy_format_graph_code (#167823)
Summary: Added ANSI escape sequence handling and a custom logging formatter. Please refer to pytorch/pytorch#167812 for detailed background explanation. This PR adds a format for log_file_handler in dynamo logger to filter ANSI codes. Before this change, log in debug.log: ``` def forward(self, L_x_: "�[31mi64�[0m�[34m[]�[0m�[2m�[34m[]�[0m�[2m�[32mcpu�[0m"): l_x_ = L_x_ �[2m# File: /Users/bytedance/Downloads/Repo/pytorch/mydebug1.py:11 in forward, code: a = torch.ones(2, x.item())�[0m item: "Sym(s20 + 5)" = l_x_.item(); �[2ml_x_ = None�[0m a: "�[31mf32�[0m�[34m[2, s20 + 5]�[0m�[2m�[34m[Max(1, s20 + 5), 1]�[0m�[2m�[32mcpu�[0m" = torch.ones(�[34m2�[0m, item) �[2m# File: /Users/bytedance/Downloads/Repo/pytorch/mydebug1.py:12 in forward, code: b = torch.ones(3, y.item() + 5)�[0m b: "�[31mf32�[0m�[34m[3, s20 + 5]�[0m�[2m�[34m[Max(1, s20 + 5), 1]�[0m�[2m�[32mcpu�[0m" = torch.ones(�[34m3�[0m, item); �[2mitem = None�[0m �[2m# File: /Users/bytedance/Downloads/Repo/pytorch/mydebug1.py:13 in forward, code: res = torch.cat([a, b], dim=0)�[0m res: "�[31mf32�[0m�[34m[5, s20 + 5]�[0m�[2m�[34m[Max(1, s20 + 5), 1]�[0m�[2m�[32mcpu�[0m" = torch.cat([a, b], dim = �[34m0�[0m); �[2ma = b = None�[0m �[2m# File: /Users/bytedance/Downloads/Repo/pytorch/mydebug1.py:14 in forward, code: return res.sum()�[0m sum_1: "�[31mf32�[0m�[34m[]�[0m�[2m�[34m[]�[0m�[2m�[32mcpu�[0m" = res.sum(); �[2mres = None�[0m return (sum_1,) ``` After this change, log in debug.log: ``` def forward(self, L_x_: "i64[][]cpu"): l_x_ = L_x_ # File: /Users/bytedance/Downloads/Repo/pytorch/mydebug1.py:11 in forward, code: a = torch.ones(2, x.item()) item: "Sym(s20 + 5)" = l_x_.item(); l_x_ = None a: "f32[2, s20 + 5][Max(1, s20 + 5), 1]cpu" = torch.ones(2, item) # File: /Users/bytedance/Downloads/Repo/pytorch/mydebug1.py:12 in forward, code: b = torch.ones(3, y.item() + 5) b: "f32[3, s20 + 5][Max(1, s20 + 5), 1]cpu" = torch.ones(3, item); item = None # File: /Users/bytedance/Downloads/Repo/pytorch/mydebug1.py:13 in forward, code: res = torch.cat([a, b], dim=0) res: "f32[5, s20 + 5][Max(1, s20 + 5), 1]cpu" = torch.cat([a, b], dim = 0); a = b = None # File: /Users/bytedance/Downloads/Repo/pytorch/mydebug1.py:14 in forward, code: return res.sum() sum_1: "f32[][]cpu" = res.sum(); res = None return (sum_1,) ``` X-link: pytorch/pytorch#167823 Approved by: https://github.com/angelayi Reviewed By: yangw-dev Differential Revision: D87297668 fbshipit-source-id: 1d2b6d08cc2a068954107515b564bf7f1c9f6e42
1 parent 2b7a1a4 commit d3ab9c6

File tree

1 file changed

+22
-0
lines changed
  • userbenchmark/dynamo/dynamobench/_dynamo

1 file changed

+22
-0
lines changed

userbenchmark/dynamo/dynamobench/_dynamo/utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,11 +903,33 @@ def reset_graph_break_dup_checker() -> None:
903903
graph_break_dup_warning_checker.reset()
904904

905905

906+
# Matches ANSI escape sequences (CSI)
907+
ANSI_ESCAPE_PATTERN = re.compile(
908+
r"""
909+
\x1B # ESC
910+
\[ # [
911+
[0-?]* # Parameter bytes
912+
[ -/]* # Intermediate bytes
913+
[@-~] # Final byte
914+
""",
915+
re.VERBOSE,
916+
)
917+
918+
919+
class StripAnsiFormatter(logging.Formatter):
920+
"""Logging formatter that strips ANSI escape codes."""
921+
922+
def format(self, record):
923+
msg = super().format(record)
924+
return ANSI_ESCAPE_PATTERN.sub("", msg)
925+
926+
906927
def add_file_handler() -> contextlib.ExitStack:
907928
log_path = os.path.join(get_debug_dir(), "torchdynamo")
908929
os.makedirs(log_path, exist_ok=True)
909930

910931
log_file_handler = logging.FileHandler(os.path.join(log_path, "debug.log"))
932+
log_file_handler.setFormatter(StripAnsiFormatter("%(message)s"))
911933
logger = logging.getLogger("torch._dynamo")
912934
logger.addHandler(log_file_handler)
913935

0 commit comments

Comments
 (0)