Commit 193574d
Cortex-M backend: dispatch quantized_linear AOT layout on target ISA (#19676)
### Summary
CMSIS-NN's `arm_fully_connected_s8` has three runtime paths, gated by
compile-time `ARM_MATH_MVEI` / `ARM_MATH_DSP`. They split the bias and
input_offset×sum(weight) offset term between two inputs, in incompatible
conventions:
* MVE: reads `ctx.buf` as a precomputed kernel_sum that must already
include `input_offset × sum(weight)` and the bias contribution. The
`bias` argument is `(void)bias;` — ignored.
* DSP / scalar: read the `bias` argument directly and fold the
input_offset contribution at runtime. `ctx.buf` (kernel_sum) is
`(void)kernel_sum;` — ignored.
`ConvertToCortexMPass._get_linear_replacement` previously emitted only
the MVE shape (kernel_sum populated, bias=None). On any non-MVE build
the DSP/scalar path started the int32 accumulator at 0 instead of at
`bias + input_offset × sum(weight)`, dropping both the bias and the
offset contribution. The accumulator wound up much smaller than
intended, requantization collapsed it to the output zero point, and
every classifier with a deep, narrow tail produced essentially uniform
near-zero outputs on non-MVE Cortex-M builds.
Use the target-ISA plumbing added by the CortexMTargetConfig PR (#19470)
to dispatch the right input shape at AOT time: on MVE targets emit
kernel_sum with bias folded in (bias=None); on DSP and scalar targets
emit the raw int32 bias directly (kernel_sum=None). The CMSIS-NN runtime
then matches exactly what it expects.
Update `quantized_linear_impl` in `operators.py` to mirror the same
contract: dispatch off whichever of kernel_sum / bias is non-None.
Threading happens automatically via `CortexMPassManager`'s signature
injection of `target_config` into the pass's `__init__`.
### Test Plan
Add
`backends/cortex_m/test/misc/test_quantized_linear_small_magnitude.py`
as a regression. A tiny `nn.Linear(512, 10)` on uniform[0, 0.002] input
is the minimal reproducer for the small-magnitude regime where the
missing offset terms dominate. The dialect test parametrizes over
MVE/DSP/scalar target configs; the implementation test runs against
whatever path the runner build matches.
The DSP & Scalar tests will need #19520 for CI testing.
Authored with Claude.
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent b771dab commit 193574d
5 files changed
Lines changed: 231 additions & 49 deletions
File tree
- backends
- cortex_m
- ops
- passes
- test/ops
- transforms
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
467 | 467 | | |
468 | 468 | | |
469 | 469 | | |
470 | | - | |
471 | | - | |
| 470 | + | |
| 471 | + | |
472 | 472 | | |
473 | 473 | | |
474 | 474 | | |
| |||
481 | 481 | | |
482 | 482 | | |
483 | 483 | | |
484 | | - | |
485 | | - | |
486 | | - | |
487 | | - | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
488 | 489 | | |
489 | 490 | | |
490 | 491 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
146 | 147 | | |
147 | 148 | | |
148 | 149 | | |
149 | | - | |
| 150 | + | |
150 | 151 | | |
151 | 152 | | |
152 | 153 | | |
| |||
156 | 157 | | |
157 | 158 | | |
158 | 159 | | |
| 160 | + | |
159 | 161 | | |
160 | 162 | | |
161 | 163 | | |
| |||
187 | 189 | | |
188 | 190 | | |
189 | 191 | | |
190 | | - | |
| 192 | + | |
191 | 193 | | |
192 | 194 | | |
193 | 195 | | |
| |||
209 | 211 | | |
210 | 212 | | |
211 | 213 | | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
212 | 218 | | |
213 | 219 | | |
214 | 220 | | |
| |||
218 | 224 | | |
219 | 225 | | |
220 | 226 | | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
221 | 233 | | |
222 | 234 | | |
223 | 235 | | |
224 | 236 | | |
225 | | - | |
226 | | - | |
227 | | - | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
228 | 243 | | |
229 | 244 | | |
230 | 245 | | |
231 | 246 | | |
232 | 247 | | |
233 | 248 | | |
234 | 249 | | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
245 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
246 | 267 | | |
247 | 268 | | |
248 | 269 | | |
249 | 270 | | |
250 | | - | |
251 | | - | |
| 271 | + | |
| 272 | + | |
252 | 273 | | |
253 | 274 | | |
254 | 275 | | |
| |||
263 | 284 | | |
264 | 285 | | |
265 | 286 | | |
266 | | - | |
| 287 | + | |
267 | 288 | | |
268 | 289 | | |
269 | 290 | | |
270 | 291 | | |
| 292 | + | |
271 | 293 | | |
272 | 294 | | |
273 | 295 | | |
| |||
292 | 314 | | |
293 | 315 | | |
294 | 316 | | |
295 | | - | |
| 317 | + | |
296 | 318 | | |
297 | 319 | | |
298 | 320 | | |
| |||
437 | 459 | | |
438 | 460 | | |
439 | 461 | | |
440 | | - | |
| 462 | + | |
441 | 463 | | |
442 | 464 | | |
443 | 465 | | |
444 | 466 | | |
445 | 467 | | |
446 | 468 | | |
447 | 469 | | |
| 470 | + | |
448 | 471 | | |
449 | 472 | | |
450 | 473 | | |
| |||
562 | 585 | | |
563 | 586 | | |
564 | 587 | | |
565 | | - | |
| 588 | + | |
566 | 589 | | |
567 | 590 | | |
568 | 591 | | |
569 | 592 | | |
| 593 | + | |
570 | 594 | | |
571 | 595 | | |
572 | 596 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
| 11 | + | |
9 | 12 | | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
| 17 | + | |
| 18 | + | |
14 | 19 | | |
15 | 20 | | |
16 | 21 | | |
| |||
128 | 133 | | |
129 | 134 | | |
130 | 135 | | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
0 commit comments