Skip to content

Commit

Permalink
Migrate to training IR in executorch, part 2 (#5950)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #5950

as title

Reviewed By: mergennachin, tugsbayasgalan

Differential Revision: D64004508

fbshipit-source-id: f605312c57c92b7f9e81e1283748a25466cbbc84
  • Loading branch information
yushangdi authored and facebook-github-bot committed Oct 8, 2024
1 parent 085193e commit 4629f3a
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions backends/apple/coreml/test/test_coreml_quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
)

from executorch.backends.apple.coreml.quantizer import CoreMLQuantizer
from torch._export import capture_pre_autograd_graph
from torch.ao.quantization.quantize_pt2e import (
convert_pt2e,
prepare_pt2e,
prepare_qat_pt2e,
)
from torch.export import export_for_training


class TestCoreMLQuantizer:
Expand All @@ -32,7 +32,7 @@ def quantize_and_compare(
) -> None:
assert quantization_type in {"PTQ", "QAT"}

pre_autograd_aten_dialect = capture_pre_autograd_graph(model, example_inputs)
pre_autograd_aten_dialect = export_for_training(model, example_inputs).module()

quantization_config = LinearQuantizerConfig.from_dict(
{
Expand Down
4 changes: 2 additions & 2 deletions backends/apple/mps/test/test_mps_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ def lower_module_and_test_output(

expected_output = model(*sample_inputs)

model = torch._export.capture_pre_autograd_graph(
model = torch.export.export_for_training(
model, sample_inputs, dynamic_shapes=dynamic_shapes
)
).module()

edge_program = export_to_edge(
model,
Expand Down
6 changes: 3 additions & 3 deletions backends/mediatek/quantizer/annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from typing import Callable, List

import torch

from torch._export import capture_pre_autograd_graph
from torch._ops import OpOverload
from torch._subclasses import FakeTensor

Expand All @@ -17,6 +15,8 @@
_annotate_input_qspec_map,
_annotate_output_qspec,
)

from torch.export import export_for_training
from torch.fx import Graph, Node
from torch.fx.passes.utils.matcher_with_name_node_map_utils import (
SubgraphMatcherWithNameNodeMap,
Expand Down Expand Up @@ -159,7 +159,7 @@ def forward(self, x):
return norm, {}

for pattern_cls in (ExecuTorchPattern, MTKPattern):
pattern_gm = capture_pre_autograd_graph(pattern_cls(), (torch.randn(3, 3),))
pattern_gm = export_for_training(pattern_cls(), (torch.randn(3, 3),)).module()
matcher = SubgraphMatcherWithNameNodeMap(
pattern_gm, ignore_literals=True, remove_overlapping_matches=False
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import unittest

import torch
import torch._export as export
from executorch.backends.transforms.duplicate_dynamic_quant_chain import (
DuplicateDynamicQuantChainPass,
)
Expand Down Expand Up @@ -59,10 +58,10 @@ def _test_duplicate_chain(

# program capture
m = copy.deepcopy(m_eager)
m = export.capture_pre_autograd_graph(
m = torch.export.export_for_training(
m,
example_inputs,
)
).module()

m = prepare_pt2e(m, quantizer)
# Calibrate
Expand Down
7 changes: 4 additions & 3 deletions examples/llm_manual/export_nanogpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
from executorch.exir import to_edge

from model import GPT
from torch._export import capture_pre_autograd_graph
from torch.export import export
from torch.export import export, export_for_training
from torch.nn.attention import sdpa_kernel, SDPBackend

model = GPT.from_pretrained("gpt2") # use gpt2 weight as pretrained weight
Expand All @@ -28,7 +27,9 @@
# Trace the model, converting it to a portable intermediate representation.
# The torch.no_grad() call tells PyTorch to exclude training-specific logic.
with sdpa_kernel([SDPBackend.MATH]), torch.no_grad():
m = capture_pre_autograd_graph(model, example_inputs, dynamic_shapes=dynamic_shape)
m = export_for_training(
model, example_inputs, dynamic_shapes=dynamic_shape
).module()
traced_model = export(m, example_inputs, dynamic_shapes=dynamic_shape)

# Convert the model into a runnable ExecuTorch program.
Expand Down
2 changes: 1 addition & 1 deletion examples/mediatek/aot_utils/oss_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def build_executorch_binary(
if quant_dtype not in Precision:
raise AssertionError(f"No support for Precision {quant_dtype}.")

captured_model = torch._export.capture_pre_autograd_graph(model, inputs)
captured_model = torch.export.export_for_training(model, inputs).module()
annotated_model = prepare_pt2e(captured_model, quantizer)
print("Quantizing the model...")
# calibration
Expand Down
4 changes: 2 additions & 2 deletions examples/mediatek/model_export_scripts/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ def export_to_et_ir(
max_num_token, max_cache_size, True
)
print("Getting pre autograd ATen Dialect Graph")
pre_autograd_aten_dialect = torch._export.capture_pre_autograd_graph(
pre_autograd_aten_dialect = torch.export.export_for_training(
model, example_inputs, dynamic_shapes=dynamic_shapes
) # NOTE: Will be replaced with export
).module() # NOTE: Will be replaced with export
quantizer = NeuropilotQuantizer()
quantizer.setup_precision(getattr(Precision, precision))
prepared_graph = prepare_pt2e(pre_autograd_aten_dialect, quantizer)
Expand Down
4 changes: 2 additions & 2 deletions exir/tests/test_quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def test_resnet(self) -> None:
m = torchvision.models.resnet18().eval()
m_copy = copy.deepcopy(m)
# program capture
m = torch._export.capture_pre_autograd_graph(
m = torch.export.export_for_training(
m, copy.deepcopy(example_inputs)
)
).module()

quantizer = XNNPACKQuantizer()
operator_config = get_symmetric_quantization_config(is_per_channel=True)
Expand Down
6 changes: 3 additions & 3 deletions extension/llm/export/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

from executorch.extension.export_util.utils import export_to_edge, save_pte_program
from executorch.extension.llm.tokenizer.utils import get_tokenizer
from torch._export import capture_pre_autograd_graph
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
from torch.ao.quantization.quantizer import Quantizer
from torch.ao.quantization.quantizer.composable_quantizer import ComposableQuantizer
from torch.export import export_for_training
from torch.nn.attention import SDPBackend

FORMAT = "[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s"
Expand Down Expand Up @@ -190,9 +190,9 @@ def capture_pre_autograd_graph(self) -> "LLMEdgeManager":
strict=True,
).module()
else:
self.pre_autograd_graph_module = capture_pre_autograd_graph(
self.pre_autograd_graph_module = export_for_training(
self.model, self.example_inputs, dynamic_shapes=dynamic_shape
)
).module()

return self

Expand Down

0 comments on commit 4629f3a

Please sign in to comment.