Commit fd3656d
Remove permutes around fused_quant elementwise ops (#21481)
Summary:
`ConvToChannelsLast` wraps every conv in `permute(NCHW->NHWC) -> conv ->
permute(NHWC->NCHW)`. When convs are joined by elementwise fused_quant ops
(residual add/mul, activations), permutes end up threaded through the
surrounding region and were never removed: the existing Cadence
`RemovePermutesAroundElementwiseOps` only recognizes aten/cadence elementwise
ops, not the SAS fused_quant ops -- and it would also wrongly treat their
lifted scale/zero_point operands as constants to be permuted.
This adds a fused_quant-aware permute-removal pass and wires it into the edge
optimization group:
- Extend the shared ExecuTorch `RemovePermutesAroundElementwiseOps` with a
small overridable seam (`_permute_relevant_inputs`) so a subclass can hide
operands from layout propagation. Behavior-preserving for existing users.
- New `RemovePermutesAroundFusedQuantElementwiseOps` (SAS) subclasses it,
adding `fused_quant.add`/`mul` and the activation ops as permutable and
exposing only their tensor operands, so the lifted scale/zero_point
placeholders are never permuted/compensated (which would break lowering).
Ops with per-channel qparams are skipped (not permutation-invariant).
- Replace the two Cadence permute passes (which no-op pre-Lower) in the
optimization group with this single pass.
- Teach the shared subgraph engine about "permutation-sink" flattens: a
`view_copy` whose input has <=1 non-unit dim (e.g. the `[1, C, 1, 1] -> [1, C]`
after a global pool) is layout-invariant, so a permutation flowing into it
simply dies. The region can terminate cleanly there with no compensating
permute -- which lets the residual-block permutes collapse across the
avgpool -> flatten -> classifier head instead of being stranded by it.
Note: fused_quant is currently SAS-specific, NOT yet a generic cross-backend
dialect, so the fused_quant knowledge deliberately stays in the SAS subclass
rather than the shared ExecuTorch pass. When fused_quant graduates to a shared
dialect, this can fold into the base pass via `extra_permutable_ops` + the
seam. (The permutation-sink flatten handling is generic and correctly lives in
the shared pass.)
On resnet18 the optimized graph goes from 83 permutes down to a single one (the
model-input boundary); every permute around the residual add/relu blocks and
across the global-pool flatten is removed.
Reviewed By: DrJessop
Differential Revision: D1134241911 parent e8feb9e commit fd3656d
4 files changed
Lines changed: 107 additions & 1 deletion
File tree
- backends
- arm/test/passes
- transforms
- test
Lines changed: 50 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
| |||
47 | 50 | | |
48 | 51 | | |
49 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
50 | 96 | | |
51 | 97 | | |
52 | 98 | | |
| |||
140 | 186 | | |
141 | 187 | | |
142 | 188 | | |
143 | | - | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
144 | 193 | | |
145 | 194 | | |
146 | 195 | | |
| |||
Lines changed: 34 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
119 | 139 | | |
120 | 140 | | |
121 | 141 | | |
| |||
321 | 341 | | |
322 | 342 | | |
323 | 343 | | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
324 | 351 | | |
325 | 352 | | |
326 | 353 | | |
| |||
332 | 359 | | |
333 | 360 | | |
334 | 361 | | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
335 | 369 | | |
336 | 370 | | |
337 | 371 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
409 | 409 | | |
410 | 410 | | |
411 | 411 | | |
| 412 | + | |
412 | 413 | | |
413 | 414 | | |
414 | 415 | | |
| |||
Lines changed: 22 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1097 | 1097 | | |
1098 | 1098 | | |
1099 | 1099 | | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
0 commit comments