Skip to content

Commit

Permalink
fix export
Browse files Browse the repository at this point in the history
Signed-off-by: youkaichao <youkaichao@gmail.com>
  • Loading branch information
youkaichao committed Dec 6, 2024
1 parent d1c71d6 commit f03fd0e
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions depyf/explain/patched_lazy_format_graph_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def patched_lazy_format_graph_code(name, gm, maybe_id=None, **kwargs):
src = "from __future__ import annotations\nimport torch\n" + \
gm.print_readable(print_output=False)
src = src.replace("<lambda>", "GraphModule")
print(src)
try:
compile(src, "noname", "exec")
except Exception as e:
Expand All @@ -50,7 +49,11 @@ def patched_lazy_format_graph_code(name, gm, maybe_id=None, **kwargs):
scope = fn.__globals__
exec(compile(src, filename=new_filepath, mode="exec"), scope)
if use_gm:
fn.__code__ = getattr(scope["GraphModule"], fn.__name__).__code__
import torch
classes = [v for v in scope.values() if isinstance(v, type) and issubclass(v, torch.nn.Module)]
assert len(classes) == 1
module_class = classes[0]
fn.__code__ = getattr(module_class, fn.__name__).__code__
else:
fn.__code__ = scope[fn.__name__].__code__
del scope[fn.__name__]
Expand Down

0 comments on commit f03fd0e

Please sign in to comment.