-
Notifications
You must be signed in to change notification settings - Fork 1.2k
NXP backend: MLPerf Tiny Anomaly detection enable test model #22831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
roman-janik-nxp
merged 1 commit into
pytorch:main
from
nxp-upstream:feature/nxg11066/EIEX-1028-Enable-MLPerfTiny-Anomaly-detection
Sep 18, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
backends/nxp/tests/models/test_mlperf_tiny_anomaly_detection.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # Copyright 2026 NXP | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import os | ||
| from functools import partial | ||
|
|
||
| import numpy as np | ||
|
|
||
| # noinspection PyUnusedImports | ||
| import pytest | ||
| import torch | ||
|
|
||
| from executorch.backends.nxp.tests.dataset_creator import ( | ||
| FromCalibrationDataDatasetCreator, | ||
| ) | ||
| from executorch.backends.nxp.tests.executorch_pipeline import ModelInputSpec | ||
| from executorch.backends.nxp.tests.graph_verifier import BaseGraphVerifier | ||
| from executorch.backends.nxp.tests.model_output_comparator import ( | ||
| ClassificationAccuracyOutputComparator, | ||
| NumericalStatsOutputComparator, | ||
| ) | ||
| from executorch.backends.nxp.tests.nsys_testing import ( | ||
| get_test_name, | ||
| lower_run_compare, | ||
| lower_run_compare_ptq_qat, | ||
| OUTPUTS_DIR, | ||
| ) | ||
| from executorch.backends.nxp.tests.use_qat import * # noqa F403 | ||
| from executorch.examples.nxp.models.mlperf_tiny.anomaly_detection.mlperf_tiny_anomaly_detection import ( | ||
| MLPerfTinyAnomalyDetection, | ||
| ) | ||
|
|
||
| BOUNDS_MSE = { | ||
| "PTQ": 1.4e-08, | ||
| "QAT": 5.205e-06, | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reseed_model_per_test_run(): | ||
| torch.manual_seed(23) | ||
| np.random.seed(23) | ||
|
|
||
|
|
||
| def test_mlperf_tiny_anomaly_detection_mse_cpu_vs_npu( | ||
| mocker, | ||
| request, | ||
| use_qat, | ||
| ): | ||
| num_samples = 60 | ||
|
|
||
| anomaly_detection = MLPerfTinyAnomalyDetection( | ||
| num_samples=num_samples, use_random_dataset=True | ||
| ) | ||
| model = anomaly_detection.get_eager_model() | ||
| dataset = anomaly_detection.dataset | ||
| labels = anomaly_detection.labels | ||
|
|
||
| dataset_creator = FromCalibrationDataDatasetCreator( | ||
| dataset, num_examples=num_samples, idx_to_label=labels | ||
| ) | ||
|
|
||
| input_spec = ModelInputSpec(anomaly_detection.input_shape) | ||
| quant_type_key = "QAT" if use_qat else "PTQ" | ||
|
|
||
| mse = BOUNDS_MSE[quant_type_key] | ||
| comparator = NumericalStatsOutputComparator(max_mse_error=mse) | ||
| model_verifier = BaseGraphVerifier(1, []) | ||
| train_fn = anomaly_detection.train_model_fn if use_qat else None | ||
|
|
||
| lower_run_compare( | ||
| model, | ||
| [input_spec], | ||
| model_verifier, | ||
| request, | ||
| dataset_creator=dataset_creator, | ||
| output_comparator=comparator, | ||
| mocker=mocker, | ||
| use_qat=use_qat, | ||
| train_fn=train_fn, | ||
| ) | ||
|
|
||
|
|
||
| def test_mlperf_tiny_anomaly_detection_ptq_qat_equivalence(request): | ||
| num_samples = 60 | ||
|
|
||
| anomaly_detection = MLPerfTinyAnomalyDetection( | ||
| num_samples=num_samples, use_random_dataset=True | ||
| ) | ||
|
|
||
| model = anomaly_detection.get_eager_model() | ||
| dataset = anomaly_detection.dataset | ||
| labels = anomaly_detection.labels | ||
|
|
||
| dataset_creator = FromCalibrationDataDatasetCreator( | ||
| dataset, num_examples=num_samples, idx_to_label=labels | ||
| ) | ||
|
|
||
| test_name = get_test_name(request) | ||
| input_parent_path = os.path.join( | ||
| OUTPUTS_DIR, | ||
| test_name, | ||
| "dataset/calibration/", | ||
| ) | ||
|
|
||
| comparator = ClassificationAccuracyOutputComparator( | ||
| class_dict=labels, | ||
| postprocess_fn=partial( | ||
| anomaly_detection.get_class_from_reconstruction_error, | ||
| input_parent_path=input_parent_path, | ||
| ), | ||
| ) | ||
|
|
||
| input_spec = ModelInputSpec(anomaly_detection.input_shape) | ||
| model_verifier = BaseGraphVerifier(1, []) | ||
|
|
||
| lower_run_compare_ptq_qat( | ||
| model, | ||
| [input_spec], | ||
| model_verifier, | ||
| request, | ||
| train_fn=anomaly_detection.train_model_fn, | ||
| dataset_creator=dataset_creator, | ||
| output_comparator=comparator, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
examples/nxp/models/mlperf_tiny/anomaly_detection/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright 2026 NXP | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.