Skip to content

Commit 61b9289

Browse files
sirakiincopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 974198930
1 parent 6d4c622 commit 61b9289

8 files changed

Lines changed: 1935 additions & 9 deletions

File tree

litert_torch/_convert/core.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from __future__ import annotations
1818

1919
import logging
20-
from typing import Literal
20+
from typing import Any, Literal
2121

2222
from litert_torch import fx_infra
2323
from litert_torch import model
@@ -39,12 +39,17 @@
3939

4040
def _run_convert_passes(
4141
exported_program: torch.export.ExportedProgram,
42+
litert_converter_flags: dict[str, Any] | None = None,
4243
) -> torch.export.ExportedProgram:
4344
exported_program = generative_fx_passes.run_generative_passes(
4445
exported_program
4546
)
4647

48+
flags = litert_converter_flags or {}
49+
force_drq = bool(flags.get("_experimental_weight_only_as_drq", False))
50+
4751
passes = [
52+
fx_passes.LowerTorchAOPass(force_weight_only_as_drq=force_drq),
4853
fx_passes.EliminateDeadCodePass(),
4954
fx_passes.OptimizeLayoutTransposesPass(),
5055
fx_passes.CanonicalizePass(),
@@ -86,6 +91,7 @@ def convert_signatures(
8691
lightweight_conversion: bool = False,
8792
enable_x64: bool = True,
8893
runtime_constant_folding: bool | None = None,
94+
_litert_converter_flags: dict[str, Any] | None = None,
8995
) -> model.LiteRTModel:
9096
"""Converts a list of `signature.Signature`s and embeds them into one `model.LiteRTModel`.
9197
@@ -155,7 +161,19 @@ def export(**kwargs):
155161

156162
# Apply default fx passes
157163
with progress.task("Run FX Passes"):
158-
exported_programs = list(map(_run_convert_passes, exported_programs))
164+
exported_programs = [
165+
_run_convert_passes(ep, _litert_converter_flags)
166+
for ep in exported_programs
167+
]
168+
169+
if _litert_converter_flags is None:
170+
_litert_converter_flags = {}
171+
else:
172+
_litert_converter_flags = dict(_litert_converter_flags)
173+
174+
for ep in exported_programs:
175+
if ep.graph_module.meta.get("strict_qdq_mode", False):
176+
_litert_converter_flags["strict_qdq_mode"] = True
159177

160178
exporter = litert_converter.exported_programs_to_flatbuffer(
161179
exported_programs,
@@ -164,6 +182,7 @@ def export(**kwargs):
164182
quant_config=quant_config,
165183
lightweight_conversion=lightweight_conversion,
166184
runtime_constant_folding=runtime_constant_folding,
185+
_litert_converter_flags=_litert_converter_flags,
167186
)
168187

169188
return model.LiteRTModel(exporter)

litert_torch/_convert/fx_passes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from litert_torch._convert.fx_passes.build_aten_composite_pass import BuildAtenCompositePass
1919
from litert_torch._convert.fx_passes.cast_inputs_bf16_to_f32_pass import CastInputsBf16ToF32Pass
2020
from litert_torch._convert.fx_passes.eliminate_dead_code_pass import EliminateDeadCodePass
21+
from litert_torch._convert.fx_passes.lower_torchao_pass import LowerTorchAOPass
2122
from litert_torch._convert.fx_passes.optimize_layout_transposes_pass import OptimizeLayoutTransposesPass
2223
from litert_torch._convert.fx_passes.reduce_view_rank_pass import ReduceViewRankPass
2324
from litert_torch._convert.fx_passes.remove_non_user_outputs_pass import RemoveNonUserOutputsPass

0 commit comments

Comments
 (0)