Skip to content
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
2 changes: 1 addition & 1 deletion thunder/dynamo/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __init__(self, **thunder_options):
self._torch_compile = torch.compile

def __call__(self, gm: torch.fx.GraphModule, sample_args: list[torch.SymInt, torch.Tensor]):
gm = remove_empty_autocast(gm)
remove_empty_autocast(gm)

# Dynamo uses lazy generation of the underlying Python code, so we need to
# force recompilation of the GraphModule before passing it to Thunder.
Expand Down
3 changes: 2 additions & 1 deletion thunder/dynamo/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,8 +1094,9 @@ def foo(x):
from thunder.dynamo.splitter import _splitter
from thunder import jit

gm = copy.deepcopy(report.graph)
# Splits the FX graph module using Thunder splitter
gm = remove_empty_autocast(report.graph)
remove_empty_autocast(gm)
# Dynamo uses lazy generation of the underlying Python code, so we need to
# force recompilation of the GraphModule before passing it to Thunder.
recompile_graph(gm)
Expand Down
13 changes: 8 additions & 5 deletions thunder/dynamo/splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,20 @@ def callback(node) -> int:
gm.recompile()

# `split_module` iterates over nodes and determines the partition to place them based on the callback.
original_split_gm: torch.fx.GraphModule = split_module(
split_gm: torch.fx.GraphModule = split_module(
gm, root_m=None, split_callback=callback, keep_original_order=True, keep_original_node_name=True
)

# Workaround for the Torch bug https://github.com/pytorch/pytorch/pull/139275
for submodule in original_split_gm.children():
for submodule in split_gm.children():
if not submodule.graph.find_nodes(op="output"):
submodule.graph.output(())
if not original_split_gm.graph.find_nodes(op="output"):
original_split_gm.graph.output(())
split_gm = copy.deepcopy(original_split_gm)
if not split_gm.graph.find_nodes(op="output"):
split_gm.graph.output(())

# If split_gm contains Parameters or Tensors then deepcopy would also create their copies.
# TODO: Eliminate deepcopy
original_split_gm = copy.deepcopy(split_gm)
Comment thread
shino16 marked this conversation as resolved.

def is_thunder_supported_partition(node: torch.fx.Node) -> bool:
return node.name.startswith("submod") and int(node.name.replace("submod_", "")) in supported_partitions
Expand Down
9 changes: 3 additions & 6 deletions thunder/dynamo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,13 +697,10 @@ def remove_empty_autocast(graph_module: torch.fx.GraphModule) -> torch.fx.GraphM
graph_module: Graph module to which this pass is applied.

"""

empty_autocast_removed_graph_module = copy.deepcopy(graph_module)

# Dummy init node.
prev_node = torch.fx.node.Node(graph_module.graph, "start_node", "call_function", lambda: None, None, None)
nodes_to_erase = []
for node in empty_autocast_removed_graph_module.graph.nodes:
for node in graph_module.graph.nodes:
# As _enter_autocast and _exit_autocast functions map the regions created by context manager,
# previous `_enter_autocast` will always correspond with current `_exit_autocast`.
if (
Expand All @@ -721,9 +718,9 @@ def remove_empty_autocast(graph_module: torch.fx.GraphModule) -> torch.fx.GraphM

# Erase the marked nodes.
for node in nodes_to_erase:
empty_autocast_removed_graph_module.graph.erase_node(node)
graph_module.graph.erase_node(node)

return empty_autocast_removed_graph_module
return graph_module


def arg_like_tensor(arg: torch.Tensor | ExampleInputMetaData):
Expand Down
1 change: 0 additions & 1 deletion thunder/tests/test_dynamo.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,6 @@ def func(x):
dtypes=(dtypes.float32,),
executors=(DynamoThunderExecutor,),
decorators=(
pytest.mark.skip(reason="https://github.com/Lightning-AI/lightning-thunder/issues/1821"),
pytest.mark.parametrize(
"optim",
(
Expand Down
Loading