From 7cc12094681dbfd7f6465d6b8de738780fdca041 Mon Sep 17 00:00:00 2001 From: Martin Pavella Date: Mon, 25 Aug 2025 11:56:49 +0200 Subject: [PATCH 1/2] NXP backend: Disable training mode for converted models. --- backends/nxp/tests/executorch_pipeline.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/backends/nxp/tests/executorch_pipeline.py b/backends/nxp/tests/executorch_pipeline.py index a20559c8cf6..2ea53b2c520 100644 --- a/backends/nxp/tests/executorch_pipeline.py +++ b/backends/nxp/tests/executorch_pipeline.py @@ -7,6 +7,8 @@ from typing import Callable import torch +from torch import nn +from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e from executorch import exir from executorch.backends.nxp.backend.custom_delegation_options import ( @@ -28,8 +30,6 @@ ExecutorchProgramManager, ) from executorch.extension.export_util.utils import export_to_edge -from torch import nn -from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e @dataclass @@ -96,6 +96,9 @@ def to_quantized_edge_program( example_input = calibration_inputs[0] + # Make sure the model is in the evaluation mode. + model.eval() + exir_program_aten = torch.export.export_for_training( model, example_input, strict=True ) @@ -147,5 +150,9 @@ def to_edge_program( calibration_inputs = get_random_calibration_inputs(to_model_input_spec(input_spec)) example_input = calibration_inputs[0] + + # Make sure the model is in the evaluation mode. + model.eval() + exir_program = torch.export.export(model, example_input) return exir.to_edge(exir_program) From d901c9919950b083f3b7de2a08dc9b0d72040556 Mon Sep 17 00:00:00 2001 From: Martin Pavella Date: Mon, 25 Aug 2025 12:01:39 +0200 Subject: [PATCH 2/2] NXP backend: Update deprecated export call. --- backends/nxp/tests/executorch_pipeline.py | 8 ++-- backends/nxp/tests/test_batch_norm_fusion.py | 4 +- backends/nxp/tests/test_quantizer.py | 40 +++++--------------- examples/nxp/aot_neutron_compile.py | 7 +--- 4 files changed, 16 insertions(+), 43 deletions(-) diff --git a/backends/nxp/tests/executorch_pipeline.py b/backends/nxp/tests/executorch_pipeline.py index 2ea53b2c520..4e297c54dbd 100644 --- a/backends/nxp/tests/executorch_pipeline.py +++ b/backends/nxp/tests/executorch_pipeline.py @@ -7,8 +7,6 @@ from typing import Callable import torch -from torch import nn -from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e from executorch import exir from executorch.backends.nxp.backend.custom_delegation_options import ( @@ -30,6 +28,8 @@ ExecutorchProgramManager, ) from executorch.extension.export_util.utils import export_to_edge +from torch import nn +from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e @dataclass @@ -99,9 +99,7 @@ def to_quantized_edge_program( # Make sure the model is in the evaluation mode. model.eval() - exir_program_aten = torch.export.export_for_training( - model, example_input, strict=True - ) + exir_program_aten = torch.export.export(model, example_input, strict=True) exir_program_aten__module_quant = _quantize_model( exir_program_aten.module(), calibration_inputs diff --git a/backends/nxp/tests/test_batch_norm_fusion.py b/backends/nxp/tests/test_batch_norm_fusion.py index a9c868b7d4f..3f1106c6d24 100644 --- a/backends/nxp/tests/test_batch_norm_fusion.py +++ b/backends/nxp/tests/test_batch_norm_fusion.py @@ -95,7 +95,7 @@ def test_batch_norm_conv_fusing(bias: bool, input_shape: list[int]): example_input = (torch.ones(*input_shape),) module = ConvBatchNormModule(bias, len(input_shape), 4) - program = torch.export.export_for_training(module, example_input, strict=True) + program = torch.export.export(module, example_input, strict=True) og_module = program.module() pm = NeutronAtenPassManager() @@ -129,7 +129,7 @@ def test_batch_norm_linear_fusing(bias: bool): example_input = (torch.ones(*input_shape),) module = LinearBatchNormModule(bias, 4, input_shape[-1], input_shape[1]) - program = torch.export.export_for_training(module, example_input, strict=True) + program = torch.export.export(module, example_input, strict=True) og_module = program.module() pm = NeutronAtenPassManager() diff --git a/backends/nxp/tests/test_quantizer.py b/backends/nxp/tests/test_quantizer.py index e97889e09a2..ef5fbb0cbca 100644 --- a/backends/nxp/tests/test_quantizer.py +++ b/backends/nxp/tests/test_quantizer.py @@ -23,9 +23,7 @@ def test_quantizer_conv2d(): example_input = (torch.ones(1, 4, 32, 32),) quantizer = NeutronQuantizer() - graph_module = torch.export.export_for_training( - model, example_input, strict=True - ).module() + graph_module = torch.export.export(model, example_input, strict=True).module() # noinspection PyTypeChecker m = prepare_pt2e(graph_module, quantizer) @@ -64,9 +62,7 @@ def test_quantizer_linear(): example_input = (torch.ones(10, 32),) quantizer = NeutronQuantizer() - graph_module = torch.export.export_for_training( - model, example_input, strict=True - ).module() + graph_module = torch.export.export(model, example_input, strict=True).module() # noinspection PyTypeChecker m = prepare_pt2e(graph_module, quantizer) @@ -105,9 +101,7 @@ def test_quantizer_maxpool2d(): example_input = (torch.ones(1, 8, 32, 32),) quantizer = NeutronQuantizer() - graph_module = torch.export.export_for_training( - model, example_input, strict=True - ).module() + graph_module = torch.export.export(model, example_input, strict=True).module() # noinspection PyTypeChecker m = prepare_pt2e(graph_module, quantizer) @@ -143,9 +137,7 @@ def test_quantizer_softmax(): example_input = (torch.ones(1, 10),) quantizer = NeutronQuantizer() - graph_module = torch.export.export_for_training( - model, example_input, strict=True - ).module() + graph_module = torch.export.export(model, example_input, strict=True).module() # noinspection PyTypeChecker m = prepare_pt2e(graph_module, quantizer) @@ -182,9 +174,7 @@ def test_quantizer_single_maxpool2d(): example_input = (torch.ones(1, 4, 32, 32),) quantizer = NeutronQuantizer() - graph_module = torch.export.export_for_training( - model, example_input, strict=True - ).module() + graph_module = torch.export.export(model, example_input, strict=True).module() # noinspection PyTypeChecker m = prepare_pt2e(graph_module, quantizer) @@ -206,9 +196,7 @@ def test_quantizer_conv2d_relu(): example_input = (torch.ones(1, 4, 32, 32),) quantizer = NeutronQuantizer() - graph_module = torch.export.export_for_training( - model, example_input, strict=True - ).module() + graph_module = torch.export.export(model, example_input, strict=True).module() # noinspection PyTypeChecker m = prepare_pt2e(graph_module, quantizer) @@ -231,9 +219,7 @@ def test_quantizer_conv2d_avg_pool2d(): example_input = (torch.ones(1, 4, 16, 16),) quantizer = NeutronQuantizer() - graph_module = torch.export.export_for_training( - model, example_input, strict=True - ).module() + graph_module = torch.export.export(model, example_input, strict=True).module() # noinspection PyTypeChecker m = prepare_pt2e(graph_module, quantizer) @@ -256,9 +242,7 @@ def test_quantizer_conv2d_permute(): example_input = (torch.ones(1, 4, 16, 16),) quantizer = NeutronQuantizer() - graph_module = torch.export.export_for_training( - model, example_input, strict=True - ).module() + graph_module = torch.export.export(model, example_input, strict=True).module() # noinspection PyTypeChecker m = prepare_pt2e(graph_module, quantizer) @@ -285,9 +269,7 @@ def test_multiple_shared_spec_ops_in_row(): example_input = (torch.ones(1, 3, 64, 64),) quantizer = NeutronQuantizer() - graph_module = torch.export.export_for_training( - model, example_input, strict=True - ).module() + graph_module = torch.export.export(model, example_input, strict=True).module() # noinspection PyTypeChecker m = prepare_pt2e(graph_module, quantizer) @@ -321,9 +303,7 @@ def test_quantizers_order_invariance(): example_input = (torch.ones(1, 4, 64, 64),) quantizer = NeutronQuantizer() - graph_module = torch.export.export_for_training( - model, example_input, strict=True - ).module() + graph_module = torch.export.export(model, example_input, strict=True).module() m = prepare_pt2e(deepcopy(graph_module), quantizer) m(*example_input) diff --git a/examples/nxp/aot_neutron_compile.py b/examples/nxp/aot_neutron_compile.py index dba3db60071..2986c5a54dc 100644 --- a/examples/nxp/aot_neutron_compile.py +++ b/examples/nxp/aot_neutron_compile.py @@ -15,7 +15,6 @@ import executorch.kernels.quantized # noqa F401 import torch - from executorch.backends.nxp.backend.ir.edge_passes.remove_io_quant_ops_pass import ( RemoveIOQuantOpsPass, ) @@ -24,14 +23,12 @@ from executorch.backends.nxp.quantizer.neutron_quantizer import NeutronQuantizer from executorch.examples.models import MODEL_NAME_TO_MODEL from executorch.examples.models.model_factory import EagerModelFactory - from executorch.exir import ( EdgeCompileConfig, ExecutorchBackendConfig, to_edge_transform_and_lower, ) from executorch.extension.export_util import save_pte_program - from torch.export import export from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e @@ -227,9 +224,7 @@ def _get_batch_size(data): model = model.eval() # 2. Export the model to ATEN - exported_program = torch.export.export_for_training( - model, example_inputs, strict=True - ) + exported_program = torch.export.export(model, example_inputs, strict=True) module = exported_program.module()