Skip to content

Commit 74f553e

Browse files
authored
Cortex-M: preserve calibration input layout (pytorch#22572)
### Summary Calibration observers use logical tensor values, so avoid converting calibration samples to channels-last. Cover the behavior in the MobileNet tests and clarify the documentation. Authored with Codex. cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell
1 parent f1adb8b commit 74f553e

4 files changed

Lines changed: 5 additions & 10 deletions

File tree

‎backends/arm/scripts/aot_arm_compiler.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,8 +981,6 @@ def _to_channels_last(x):
981981
calibration_samples = [example_inputs]
982982

983983
for sample in calibration_samples:
984-
if not args.cortex_m_explicit_layout:
985-
sample = tuple(_to_channels_last(x) for x in sample)
986984
prepared(*sample)
987985

988986
model_quant = convert_pt2e(prepared)

‎backends/cortex_m/test/models/test_mobilenet_v2.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@
3737
}
3838

3939
# Use larger sample set for calibration to get better quantization
40-
calibration_samples = [
41-
(torch.randn(1, 3, 224, 224).to(memory_format=torch.channels_last),)
42-
for _ in range(100)
43-
]
40+
calibration_samples = [(torch.randn(1, 3, 224, 224),) for _ in range(100)]
4441

4542
test_cases = {
4643
"mobilenet_v2": McuTestCase(

‎backends/cortex_m/test/models/test_mobilenet_v3.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@
3939
}
4040

4141
# Use bigger sample set for calibration.
42-
calibration_samples = [
43-
(torch.randn(1, 3, 232, 232).to(memory_format=torch.channels_last),)
44-
for i in (range(100))
45-
]
42+
calibration_samples = [(torch.randn(1, 3, 232, 232),) for _ in range(100)]
4643

4744
test_cases = {
4845
"mobilenet_v3_small": McuTestCase(

‎docs/source/backends/arm-cortex-m/arm-cortex-m-overview.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ quantized = convert_pt2e(prepared)
100100
quantized_exported_program = torch.export.export(quantized, (example_input,))
101101
```
102102

103+
Calibration observes logical tensor values, so calibration inputs do not need
104+
to use the same memory format as the export example.
105+
103106
### 2. Lower to edge and apply Cortex-M passes
104107

105108
Lower to the edge dialect with the backend's `EdgeCompileConfig`, then run the `CortexMPassManager` to replace quantized subgraphs with CMSIS-NN operator implementations:

0 commit comments

Comments
 (0)