Skip to content

Commit 5c2b693

Browse files
authored
executorch/backends/xnnpack/test/ops
Differential Revision: D72436120 Pull Request resolved: #9956
1 parent 0ee74ff commit 5c2b693

File tree

18 files changed

+57
-37
lines changed

18 files changed

+57
-37
lines changed

backends/xnnpack/test/ops/test_check_quant_params.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _test_check_quant_message(self, ep_modifier, expected_message):
5252
torch._dynamo.reset()
5353
mod = torch.nn.Linear(10, 10)
5454
quantizer = XNNPACKQuantizer()
55-
captured = export_for_training(mod, (torch.randn(1, 10),)).module()
55+
captured = export_for_training(mod, (torch.randn(1, 10),), strict=True).module()
5656
quantizer.set_global(get_symmetric_quantization_config(is_per_channel=True))
5757
prepared = prepare_pt2e(captured, quantizer)
5858

@@ -68,7 +68,6 @@ def _test_check_quant_message(self, ep_modifier, expected_message):
6868
self.assertEquals(str(context.exception), expected_message)
6969

7070
def test_in_per_tensor_quant(self):
71-
7271
for invalid_scale in [
7372
float("nan"),
7473
float("inf"),

examples/llm_manual/export_nanogpt.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# The torch.no_grad() call tells PyTorch to exclude training-specific logic.
2929
with sdpa_kernel([SDPBackend.MATH]), torch.no_grad():
3030
m = export_for_training(
31-
model, example_inputs, dynamic_shapes=dynamic_shape
31+
model, example_inputs, dynamic_shapes=dynamic_shape, strict=True
3232
).module()
3333
traced_model = export(m, example_inputs, dynamic_shapes=dynamic_shape, strict=True)
3434

examples/mediatek/aot_utils/oss_utils/utils.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def build_executorch_binary(
3030
if quant_dtype not in Precision:
3131
raise AssertionError(f"No support for Precision {quant_dtype}.")
3232

33-
captured_model = torch.export.export_for_training(model, inputs).module()
33+
captured_model = torch.export.export_for_training(
34+
model, inputs, strict=True
35+
).module()
3436
annotated_model = prepare_pt2e(captured_model, quantizer)
3537
print("Quantizing the model...")
3638
# calibration

examples/mediatek/model_export_scripts/llama.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def export_to_et_ir(
319319
)
320320
print("Getting pre autograd ATen Dialect Graph")
321321
pre_autograd_aten_dialect = torch.export.export_for_training(
322-
model, example_inputs, dynamic_shapes=dynamic_shapes
322+
model, example_inputs, dynamic_shapes=dynamic_shapes, strict=True
323323
).module() # NOTE: Will be replaced with export
324324
quantizer = NeuropilotQuantizer()
325325
quantizer.setup_precision(getattr(Precision, precision))

examples/models/phi-3-mini/export_phi-3-mini.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def export(args) -> None:
6565
xnnpack_quantizer.set_global(xnnpack_quant_config)
6666

6767
model = export_for_training(
68-
model, example_inputs, dynamic_shapes=dynamic_shapes
68+
model, example_inputs, dynamic_shapes=dynamic_shapes, strict=True
6969
).module()
7070
model = prepare_pt2e(model, xnnpack_quantizer) # pyre-fixme[6]
7171
model(*example_inputs)

examples/models/test/test_export.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def collect_executorch_and_eager_outputs(
2929
Returns a tuple containing the outputs of the eager mode model and the executorch mode model.
3030
"""
3131
eager_model = eager_model.eval()
32-
model = torch.export.export_for_training(eager_model, example_inputs).module()
32+
model = torch.export.export_for_training(
33+
eager_model, example_inputs, strict=True
34+
).module()
3335
edge_model = export_to_edge(model, example_inputs)
3436

3537
executorch_prog = edge_model.to_executorch()

examples/portable/scripts/export_and_delegate.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def export_composite_module_with_lower_graph():
6161
m_compile_spec = m.get_compile_spec()
6262

6363
# pre-autograd export. eventually this will become torch.export
64-
m = torch.export.export_for_training(m, m_inputs).module()
64+
m = torch.export.export_for_training(m, m_inputs, strict=True).module()
6565
edge = export_to_edge(m, m_inputs)
6666
logging.info(f"Exported graph:\n{edge.exported_program().graph}")
6767

@@ -84,7 +84,7 @@ def forward(self, *args):
8484
m = CompositeModule()
8585
m = m.eval()
8686
# pre-autograd export. eventually this will become torch.export
87-
m = torch.export.export_for_training(m, m_inputs).module()
87+
m = torch.export.export_for_training(m, m_inputs, strict=True).module()
8888
composited_edge = export_to_edge(m, m_inputs)
8989

9090
# The graph module is still runnerable
@@ -134,7 +134,7 @@ def get_example_inputs(self):
134134
m = Model()
135135
m_inputs = m.get_example_inputs()
136136
# pre-autograd export. eventually this will become torch.export
137-
m = torch.export.export_for_training(m, m_inputs).module()
137+
m = torch.export.export_for_training(m, m_inputs, strict=True).module()
138138
edge = export_to_edge(m, m_inputs)
139139
logging.info(f"Exported graph:\n{edge.exported_program().graph}")
140140

@@ -171,7 +171,7 @@ def export_and_lower_the_whole_graph():
171171

172172
m_inputs = m.get_example_inputs()
173173
# pre-autograd export. eventually this will become torch.export
174-
m = torch.export.export_for_training(m, m_inputs).module()
174+
m = torch.export.export_for_training(m, m_inputs, strict=True).module()
175175
edge = export_to_edge(m, m_inputs)
176176
logging.info(f"Exported graph:\n{edge.exported_program().graph}")
177177

examples/xnnpack/aot_compiler.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@
8787

8888
model = model.eval()
8989
# pre-autograd export. eventually this will become torch.export
90-
ep = torch.export.export_for_training(model, example_inputs)
90+
ep = torch.export.export_for_training(model, example_inputs, strict=True)
9191
model = ep.module()
9292

9393
if args.quantize:
9494
logging.info("Quantizing Model...")
9595
# TODO(T165162973): This pass shall eventually be folded into quantizer
9696
model = quantize(model, example_inputs, quant_type)
97-
ep = torch.export.export_for_training(model, example_inputs)
97+
ep = torch.export.export_for_training(model, example_inputs, strict=True)
9898

9999
edge = to_edge_transform_and_lower(
100100
ep,

examples/xnnpack/quantization/example.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ def verify_xnnpack_quantizer_matching_fx_quant_model(model_name, model, example_
6060
m = model
6161

6262
# 1. pytorch 2.0 export quantization flow (recommended/default flow)
63-
m = torch.export.export_for_training(m, copy.deepcopy(example_inputs)).module()
63+
m = torch.export.export_for_training(
64+
m, copy.deepcopy(example_inputs), strict=True
65+
).module()
6466
quantizer = XNNPACKQuantizer()
6567
quantization_config = get_symmetric_quantization_config(is_per_channel=True)
6668
quantizer.set_global(quantization_config)
@@ -177,7 +179,9 @@ def main() -> None:
177179

178180
model = model.eval()
179181
# pre-autograd export. eventually this will become torch.export
180-
model = torch.export.export_for_training(model, example_inputs).module()
182+
model = torch.export.export_for_training(
183+
model, example_inputs, strict=True
184+
).module()
181185
start = time.perf_counter()
182186
quantized_model = quantize(model, example_inputs)
183187
end = time.perf_counter()

exir/backend/test/test_partitioner.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def partition(
7676

7777
mlp = MLP()
7878
example_inputs = mlp.get_random_inputs()
79-
model = export_for_training(mlp, example_inputs).module()
79+
model = export_for_training(mlp, example_inputs, strict=True).module()
8080
aten = export(model, example_inputs, strict=True)
8181
spec_key = "path"
8282
spec_value = "/a/b/c/d"
@@ -137,7 +137,7 @@ def partition(
137137

138138
mlp = MLP()
139139
example_inputs = mlp.get_random_inputs()
140-
model = export_for_training(mlp, example_inputs).module()
140+
model = export_for_training(mlp, example_inputs, strict=True).module()
141141
aten = export(model, example_inputs, strict=True)
142142
edge = exir.to_edge(aten)
143143

@@ -177,7 +177,7 @@ def partition(
177177

178178
mlp = MLP()
179179
example_inputs = mlp.get_random_inputs()
180-
model = export_for_training(mlp, example_inputs).module()
180+
model = export_for_training(mlp, example_inputs, strict=True).module()
181181
edge = exir.to_edge(export(model, example_inputs, strict=True))
182182

183183
with self.assertRaisesRegex(
@@ -229,7 +229,9 @@ def partition(
229229
partition_tags=partition_tags,
230230
)
231231

232-
model = export_for_training(self.AddConst(), (torch.ones(2, 2),)).module()
232+
model = export_for_training(
233+
self.AddConst(), (torch.ones(2, 2),), strict=True
234+
).module()
233235
edge = exir.to_edge(export(model, (torch.ones(2, 2),), strict=True))
234236
delegated = edge.to_backend(PartitionerNoTagData())
235237

@@ -308,7 +310,9 @@ def partition(
308310
partition_tags=partition_tags,
309311
)
310312

311-
model = export_for_training(self.AddConst(), (torch.ones(2, 2),)).module()
313+
model = export_for_training(
314+
self.AddConst(), (torch.ones(2, 2),), strict=True
315+
).module()
312316
edge = exir.to_edge(export(model, (torch.ones(2, 2),), strict=True))
313317
delegated = edge.to_backend(PartitionerTagData())
314318

@@ -383,7 +387,9 @@ def partition(
383387
partition_tags=partition_tags,
384388
)
385389

386-
model = export_for_training(self.AddConst(), (torch.ones(2, 2),)).module()
390+
model = export_for_training(
391+
self.AddConst(), (torch.ones(2, 2),), strict=True
392+
).module()
387393
edge = exir.to_edge(export(model, (torch.ones(2, 2),), strict=True))
388394
delegated = edge.to_backend(PartitionerTagData())
389395

@@ -471,7 +477,9 @@ def partition(
471477
)
472478

473479
inputs = (torch.ones(2, 2),)
474-
model = export_for_training(ReuseConstData(), (torch.ones(2, 2),)).module()
480+
model = export_for_training(
481+
ReuseConstData(), (torch.ones(2, 2),), strict=True
482+
).module()
475483
edge = exir.to_edge(export(model, (torch.ones(2, 2),), strict=True))
476484
exec_prog = edge.to_backend(PartitionerTagData()).to_executorch()
477485
executorch_module = _load_for_executorch_from_buffer(exec_prog.buffer)
@@ -531,7 +539,9 @@ def partition(
531539
partition_tags=partition_tags,
532540
)
533541

534-
model = export_for_training(ReuseConstData(), (torch.ones(2, 2),)).module()
542+
model = export_for_training(
543+
ReuseConstData(), (torch.ones(2, 2),), strict=True
544+
).module()
535545
edge = exir.to_edge(export(model, (torch.ones(2, 2),), strict=True))
536546
with self.assertRaises(RuntimeError) as error:
537547
_ = edge.to_backend(PartitionerTagData())

exir/backend/test/test_passes.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ def forward(self, x):
2828
z = x - self.const
2929
return y, z
3030

31-
model = export_for_training(ReuseConstData(), (torch.ones(2, 2),)).module()
31+
model = export_for_training(
32+
ReuseConstData(), (torch.ones(2, 2),), strict=True
33+
).module()
3234
edge = exir.to_edge(
3335
torch.export.export(model, (torch.ones(2, 2),), strict=True)
3436
)

exir/emit/test/test_emit.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1751,8 +1751,8 @@ def forward(self, x):
17511751
module_1(*example_inputs)
17521752
module_2(*example_inputs)
17531753

1754-
ep1 = export_for_training(module_1, example_inputs)
1755-
ep2 = export_for_training(module_2, example_inputs)
1754+
ep1 = export_for_training(module_1, example_inputs, strict=True)
1755+
ep2 = export_for_training(module_2, example_inputs, strict=True)
17561756

17571757
edge_program_manager = exir.to_edge(
17581758
{"forward1": ep1, "forward2": ep2},

exir/tests/test_passes.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,9 @@ def forward(self, query, key, value):
11641164
value = torch.randn(32, 32, 32, 32)
11651165

11661166
# Capture the model
1167-
m = torch.export.export_for_training(M(32), (query, key, value)).module()
1167+
m = torch.export.export_for_training(
1168+
M(32), (query, key, value), strict=True
1169+
).module()
11681170

11691171
# 8w16a quantization
11701172
from torch.ao.quantization.observer import (
@@ -1405,8 +1407,7 @@ def quantize_model(
14051407
) -> Tuple[EdgeProgramManager, int, int]:
14061408
# program capture
14071409
m = torch.export.export_for_training(
1408-
m_eager,
1409-
example_inputs,
1410+
m_eager, example_inputs, strict=True
14101411
).module()
14111412

14121413
quantizer = XNNPACKQuantizer()

exir/tests/test_quantization.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_resnet(self) -> None:
5252
m_copy = copy.deepcopy(m)
5353
# program capture
5454
m = torch.export.export_for_training(
55-
m, copy.deepcopy(example_inputs)
55+
m, copy.deepcopy(example_inputs), strict=True
5656
).module()
5757

5858
quantizer = XNNPACKQuantizer()

exir/tests/test_quantize_io_pass.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ def _quantize(self, mod, example_inputs):
3939
operator_config = get_symmetric_quantization_config()
4040
quantizer.set_global(operator_config)
4141
m = torch.export.export_for_training(
42-
mod, copy.deepcopy(example_inputs)
42+
mod, copy.deepcopy(example_inputs), strict=True
4343
).module()
4444
m = prepare_pt2e(m, quantizer)
4545
_ = m(*example_inputs)
4646
m = convert_pt2e(m)
47-
exported_program = torch.export.export_for_training(m, example_inputs)
47+
exported_program = torch.export.export_for_training(
48+
m, example_inputs, strict=True
49+
)
4850
return exported_program
4951

5052
def _check_count(self, op, count, epm):

extension/export_util/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def export_to_exec_prog(
108108
) -> ExecutorchProgramManager:
109109
m = model.eval()
110110
# pre-autograd export. eventually this will become torch.export
111-
m = export_for_training(m, example_inputs).module()
111+
m = export_for_training(m, example_inputs, strict=True).module()
112112

113113
core_aten_ep = _to_core_aten(
114114
m,

extension/llm/export/builder.py

+1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def _export(self, module: Optional[torch.nn.Module] = None) -> ExportedProgram:
234234
self.example_inputs,
235235
kwargs=self.example_kwarg_inputs,
236236
dynamic_shapes=dynamic_shape,
237+
strict=True,
237238
)
238239
return exported_module
239240

extension/llm/export/test_export_passes.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
class RemoveRedundantTransposesPassTest(unittest.TestCase):
1212
def _export(self, model, example_inputs):
13-
exported_module = export_for_training(
14-
model,
15-
example_inputs,
16-
)
13+
exported_module = export_for_training(model, example_inputs, strict=True)
1714
return exported_module.module()
1815

1916
def _check(self, model, example_inputs, key, before_count, after_count):

0 commit comments

Comments
 (0)