8000 Only print dde partial fx graph for export by StrongerXi · Pull Request #153218 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

Only print dde partial fx graph for export #153218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2025
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
26 changes: 26 additions & 0 deletions test/dynamo/test_repros.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import itertools
import os
import random
import sys
import types
import typing
import unittest
Expand Down Expand Up @@ -6870,6 +6871,31 @@ def fn(x):
x = torch.randn(4)
self.assertEqual(fn(x), opt_fn(x))

def test_data_dependent_error_log_no_print(self):
# This is a regression test case for
# https://github.com/pytorch/pytorch/pull/149831
from io import StringIO

capturedOutput = StringIO()
sys.stderr = capturedOutput

@torch.compile(fullgraph=True)
def func(a):
if a.sum() > 0:
return a + 1
return a + 2

a = torch.rand(10, 10)
try:
func(a)
except Exception:
pass
sys.stderr = sys.__stderr__

# Make sure we don't _print_ out the graph module.
output = capturedOutput.getvalue()
self.assertNotIn("class GraphModule", output)


instantiate_parametrized_tests(ReproTests)

Expand Down
14 changes: 6 additions & 8 deletions torch/_dynamo/symbolic_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,15 +1342,13 @@ def run(self):
raise
except RuntimeError as e:
if hasattr(e, "msg") and "Data-dependent" in e.msg:
print(
"\n"
+ torch.fx.GraphModule(
self.output.nn_modules, self.output.graph
).print_readable(
print_output=False, include_stride=True, include_device=True
),
file=sys.stderr,
readable_graph = torch.fx.GraphModule(
self.output.nn_modules, self.output.graph
).print_readable(
print_output=False, include_stride=True, include_device=True
)
e.partial_fx_graph = readable_graph # type: ignore[attr-defined]
raise

raise
except Exception as e:
Expand Down
8 changes: 8 additions & 0 deletions torch/export/_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import inspect
import logging
import re
import sys
import time
import warnings
from contextlib import contextmanager, nullcontext
Expand Down Expand Up @@ -1089,6 +1090,13 @@ def wrapper(*args, **kwargs):
message=str(e),
flags=_EXPORT_FLAGS,
)

if hasattr(e, "partial_fx_graph"):
print(
e.partial_fx_graph,
file=sys.stderr,
)

raise e
finally:
_EXPORT_FLAGS = None
Expand Down
15 changes: 6 additions & 9 deletions torch/fx/_symbolic_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import inspect
import math
import os
import sys
import warnings
from itertools import chain
from types import CodeType, FunctionType, ModuleType
Expand Down Expand Up @@ -843,14 +842,12 @@ def forward(*args, **kwargs):
self.submodule_paths = None
except RuntimeError as e:
if isinstance(e.args[0], str) and "data-dependent" in e.args[0]:
print(
"\n"
+ self.graph.python_code(
root_module="self",
verbose=True,
).src,
file=sys.stderr,
)
partial_fx_graph = self.graph.python_code(
root_module="self",
verbose=True,
).src
e.partial_fx_graph = partial_fx_graph # type: ignore[attr-defined]
raise

raise
finally:
Expand Down
Loading
0