You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Ops with included input dim order conversion fail at runtime #11086
Certain ops in pytorch can internally convert their inputs to contiguous format. Linear, for example, seems to do this. These dim order conversions are reflected in the node metadata (and thus ET dim order), but not in the graph. Our kernels don't include the input dim order conversion and thus fail at runtime.
A slightly contrived example is as follows, though I expect that this is reproable with any op with an ATen kernel that includes an implicit conversion to contiguous, of which there are quite a few.
[tensor_util_portable.cpp:132] Check failed (all_contiguous || all_channels_last): 2 input tensors have different dim orders
[op_expand_copy.cpp:89] Check failed (tensors_have_same_dim_order(self, out)):
[method.cpp:1322] KernelCall failed at instruction 0:1 in operator aten::expand_copy.out: 0x12
[method.cpp:1328] arg 0 with type id 1
[method.cpp:1328] arg 1 with type id 8
[method.cpp:1328] arg 2 with type id 5
[method.cpp:1328] arg 3 with type id 1
[method.cpp:1328] arg 4 with type id 1
Note that the failure actually occurs in expand_copy due to the weird decomp in this case.
changed the title [-]Ops with implicit dim order conversion fail at runtime[/-][+]Ops with included input dim order conversion fail at runtime[/+]on May 23, 2025
These dim order conversions are reflected in the node metadata (and thus ET dim order), but not in the graph.
Seems like eager mode will work OK. And some delegate trying to match the graph will also work OK. Portable Linear with NHWC input may not. So the action here is to insert an explicit convert when running into such ops? How about decomposition handling this at least for Linear?
This still reproduces on executorch 1.3.1 (torch 2.12, CPU). A channels_last input through nn.Linear fails at runtime in the portable op_expand_copy on tensors_have_same_dim_order(self, out), KernelCall 0x12. The node metadata and specs show a dim-order change to contiguous, but no explicit dim-order conversion is materialized before the decomposed expand_copy, so the kernel receives mismatched dim orders. The expand_copy invariant itself looks correct, so this reads as a lowering gap rather than a kernel bug.
Happy to take this. Should the fix be an EXIR lowering pass that inserts the dim-order conversion after the decomposition and before out-var and memory planning? I would rather match the intended design before I build.
Still hitting this on main. Smaller repro that doesn't involve Linear or conv - mean/sum reduce to contiguous internally, so the other operand of the add is still channels_last:
Check failed (all_contiguous || all_channels_last): 3 input tensors have different dim orders
KernelCall failed at instruction 0:2 in operator aten::add.out: 0x12
Same thing with mul/sub/div. It's not specific to add - 106 files under kernels/portable/cpu call tensors_have_same_dim_order.
I did try extending LegalizePortableDimOrderPass to insert a _to_dim_order_copy on whichever operand disagrees, and both obvious versions are wrong, so flagging that before someone else burns time on it:
converge the operands on the op's output dim order: runs fine, silently wrong values for add/mul/sub
converge on contiguous: values are right, but it adds a second copy in the expand_copy path that test_channels_last_linear_inserts_copy_before_expand_copy counts, and two cases in test_issue_11086_runtime_regressions start failing with 0x12
Looks like the op's own output keeps its original dim order in metadata either way, so the mismatch just moves downstream instead of going away. Probably needs the new order propagated to the output spec, which is more than a local change in that pass, so I stopped there.
I hit this through yolo26 (#21621), since newer ultralytics versions convert models to channels-last on x86 CPUs.
I tried adding aten.convolution to LegalizePortableDimOrderPass and making the output match the input's dim order. On main @939015a3a1:
Conv alone: 64+ cases (dense, depthwise, single-channel, 1x1, transposed, no-bias, chained convs, batch 1/2, and all layout combinations). 16 fail before the change, 0 after. All outputs match eager, so for conv, using the output dim order this way didn't produce incorrect values. The existing dim-order and exir pass/emit/memory-planning tests also pass.
Real models: this doesn't get very far. With conv followed by add/mul/cat using mixed layouts, 8/12 cases fail before and 7/12 after. In yolo26, the next failure is split_with_sizes_copy (same input/output dim-order check).
So I agree with the conclusion above: adding ops to the pass one at a time just moves the failure downstream, and this probably needs dim order to be propagated through the graph.
Is there a preferred design for this? @Lawson-Darrow asked the same back in June. I'm happy to help implement it, but I'd rather not open a conv-only PR if that's not the direction you want.
Activity
digantdesai commentedon May 23, 2025
print(module.forward(*inputs).dim_order())what does this print?[-]Ops with implicit dim order conversion fail at runtime[/-][+]Ops with included input dim order conversion fail at runtime[/+]GregoryComer commentedon May 23, 2025
It prints
(0, 1, 2, 3).digantdesai commentedon Jun 25, 2025
Seems like eager mode will work OK. And some delegate trying to match the graph will also work OK. Portable Linear with NHWC input may not. So the action here is to insert an explicit convert when running into such ops? How about decomposition handling this at least for Linear?
Lawson-Darrow commentedon Jun 4, 2026
This still reproduces on executorch 1.3.1 (torch 2.12, CPU). A channels_last input through
nn.Linearfails at runtime in the portableop_expand_copyontensors_have_same_dim_order(self, out), KernelCall 0x12. The node metadata and specs show a dim-order change to contiguous, but no explicit dim-order conversion is materialized before the decomposedexpand_copy, so the kernel receives mismatched dim orders. Theexpand_copyinvariant itself looks correct, so this reads as a lowering gap rather than a kernel bug.Happy to take this. Should the fix be an EXIR lowering pass that inserts the dim-order conversion after the decomposition and before out-var and memory planning? I would rather match the intended design before I build.
Fix channels-last portable fallback by inserting explicit dim-order l…
akashjainn commentedon Sep 3, 2026
Still hitting this on main. Smaller repro that doesn't involve Linear or conv - mean/sum reduce to contiguous internally, so the other operand of the add is still channels_last:
Same thing with mul/sub/div. It's not specific to add - 106 files under kernels/portable/cpu call tensors_have_same_dim_order.
I did try extending LegalizePortableDimOrderPass to insert a _to_dim_order_copy on whichever operand disagrees, and both obvious versions are wrong, so flagging that before someone else burns time on it:
Looks like the op's own output keeps its original dim order in metadata either way, so the mismatch just moves downstream instead of going away. Probably needs the new order propagated to the output spec, which is more than a local change in that pass, so I stopped there.
Not working on this, just leaving the repro.
HiroForYou commentedon Oct 5, 2026
Adding another example, this time with convolution, along with some data on the approach @akashjainn described.
A channels-last weight with a contiguous input is valid in eager (the output is channels-last) and exports fine, but fails on portable:
I hit this through yolo26 (#21621), since newer ultralytics versions convert models to channels-last on x86 CPUs.
I tried adding
aten.convolutiontoLegalizePortableDimOrderPassand making the output match the input's dim order. On main @939015a3a1:split_with_sizes_copy(same input/output dim-order check).So I agree with the conclusion above: adding ops to the pass one at a time just moves the failure downstream, and this probably needs dim order to be propagated through the graph.
Is there a preferred design for this? @Lawson-Darrow asked the same back in June. I'm happy to help implement it, but I'd rather not open a conv-only PR if that's not the direction you want.