Skip to content
Merged
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: 20 additions & 6 deletions autoparallel/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import warnings
from contextlib import ExitStack, contextmanager
from types import MethodType
from typing import Optional, Union
from typing import Any, Optional, Tuple, Union

import torch
from torch._dynamo.functional_export import _dynamo_graph_capture_for_export
from torch._functorch.aot_autograd import (
aot_compile_joint_with_descriptors,
aot_export_joint_with_descriptors,
Expand All @@ -22,6 +23,7 @@
from torch._subclasses import FakeTensorMode
from torch.distributed.fsdp import MixedPrecisionPolicy
from torch.distributed.tensor import DeviceMesh
from torch.export._trace import _restore_state_dict
from torch.export._unlift import _assign_attr
from torch.export.unflatten import _AttrKind

Expand Down Expand Up @@ -163,6 +165,21 @@ def enable_local_map_wrapping():
yield


def _export(model: torch.nn.Module, inputs: Tuple[Any]) -> torch.nn.Module:
"""
Thin wrapper around graph capture output that restores the
original calling convention and attribute fqn. TODO:
1) Use bytecode for calling convention instead of pytree for more
seamless UX.
2) Attach guards
3) Be more careful about tensor constants names.
"""
with torch._dynamo.config.patch(install_free_tensors=True):
gm = _dynamo_graph_capture_for_export(model)(*inputs)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is used for new strict export and it adds implicitly sets up right calling convention using pytree. We might need to change this a bit to rely on bytecode instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_restore_state_dict(model, gm)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autoparallel wants the original fqn to be stored.

return gm


class AutoParallel:
"""
Args:
Expand Down Expand Up @@ -279,13 +296,10 @@ def build_model_graph(self):
with set_dtype_cast(
True
), enable_local_map_wrapping(), torch._dynamo.utils._disable_saved_tensors_hooks_during_tracing():
with torch._dynamo.config.patch(
install_free_tensors=True
), monkey_patch_export_verifier():
ep = torch.export.export(self.model, inputs, strict=True)
torch_ir_with_fqn = _export(self.model, inputs)
self.joint_with_descriptors = aot_export_joint_with_descriptors(
self.stack,
ep.module(),
torch_ir_with_fqn,
inputs,
decompositions=decomp_table,
fw_compiler=self.compiler_fn,
Expand Down