Commit 4c20ef1
fix(MemoryFormatOpsPass): preserve input dim_order for clone/to_copy with no memory_format kwarg (#17611)
## Summary
Fixes #16032
This PR fixes `MemoryFormatOpsPass` to correctly handle
`torch.preserve_format` semantics for `clone()` and `_to_copy.default`
operations.
**Root cause:** When `clone()` or `_to_copy` is called without an
explicit `memory_format` kwarg, the pass was defaulting to
`torch.contiguous_format`, causing the output `dim_order` to be
`[0,1,2,3]` (contiguous) even when the input was channels-last
`[0,2,3,1]`. This caused runtime assertion failures:
```
Code=18 InvalidArgument: tensors_have_same_dim_order(self, out)
```
**Fix:** Change the default from `torch.contiguous_format` to
`torch.preserve_format`, and derive `dim_order` from the input tensor's
`dim_order()` when preserve_format is used.
This is a minimal, focused fix following the guidance from @GregoryComer
in the discussion on PR #17463.
## Changes
- **`exir/passes/memory_format_ops_pass.py`** (+29/-5 lines):
- Default `memory_format` to `torch.preserve_format` instead of
`torch.contiguous_format`
- When preserve_format, derive `dim_order` from
`input_tensor.dim_order()`
- Fallback to contiguous if no input tensor available (e.g., `empty()`)
- **`exir/tests/test_passes.py`** (+130 lines):
- `test_clone_no_kwarg_preserves_channels_last_dim_order`: Core repro
case for #16032
- `test_clone_contiguous_format_kwarg_stays_contiguous`: Regression
guard
- `test_to_copy_no_kwarg_preserves_channels_last_dim_order`: Verifies
`_to_copy.default` path
## Standalone Reproduction
```python
import torch
from torch.export import export
from executorch.exir import to_edge, EdgeCompileConfig
class ConvClone(torch.nn.Module):
def __init__(self):
super().__init__()
self.conv = torch.nn.Conv2d(3, 16, kernel_size=3, padding=1)
def forward(self, x):
return self.conv(x).clone()
model = ConvClone().to(memory_format=torch.channels_last)
x = torch.randn(1, 3, 8, 8).to(memory_format=torch.channels_last)
exported = export(model, (x,))
edge = to_edge(exported, compile_config=EdgeCompileConfig(_skip_dim_order=False))
# Before fix: clone node has dim_order=(0,1,2,3) - BUG
# After fix: clone node has dim_order=(0,2,3,1) - CORRECT
for node in edge.exported_program().graph_module.graph.nodes:
if "_clone_dim_order" in str(node.target):
print(f"clone dim_order: {tuple(node.meta['val'].dim_order())}")
```
## Test Plan
- [x] All 3 new tests pass
- [x] Verified fix with standalone reproduction script
- [x] No changes to existing tests required
## Related
- Fixes #16032
- Supersedes #17463 (this is the minimal fix extracted from that PR per
reviewer feedback)
---------
Co-authored-by: NefAI <info@nefai.nl>1 parent 584ef68 commit 4c20ef1
2 files changed
Lines changed: 181 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
43 | | - | |
44 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
45 | 48 | | |
46 | | - | |
| 49 | + | |
| 50 | + | |
47 | 51 | | |
48 | | - | |
| 52 | + | |
| 53 | + | |
49 | 54 | | |
50 | | - | |
| 55 | + | |
| 56 | + | |
51 | 57 | | |
52 | 58 | | |
53 | 59 | | |
54 | 60 | | |
55 | 61 | | |
56 | 62 | | |
57 | 63 | | |
58 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
59 | 79 | | |
60 | 80 | | |
61 | 81 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2502 | 2502 | | |
2503 | 2503 | | |
2504 | 2504 | | |
| 2505 | + | |
| 2506 | + | |
| 2507 | + | |
| 2508 | + | |
| 2509 | + | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
| 2513 | + | |
| 2514 | + | |
| 2515 | + | |
| 2516 | + | |
| 2517 | + | |
| 2518 | + | |
| 2519 | + | |
| 2520 | + | |
| 2521 | + | |
| 2522 | + | |
| 2523 | + | |
| 2524 | + | |
| 2525 | + | |
| 2526 | + | |
| 2527 | + | |
| 2528 | + | |
| 2529 | + | |
| 2530 | + | |
| 2531 | + | |
| 2532 | + | |
| 2533 | + | |
| 2534 | + | |
| 2535 | + | |
| 2536 | + | |
| 2537 | + | |
| 2538 | + | |
| 2539 | + | |
| 2540 | + | |
| 2541 | + | |
| 2542 | + | |
| 2543 | + | |
| 2544 | + | |
| 2545 | + | |
| 2546 | + | |
| 2547 | + | |
| 2548 | + | |
| 2549 | + | |
| 2550 | + | |
| 2551 | + | |
| 2552 | + | |
| 2553 | + | |
| 2554 | + | |
| 2555 | + | |
| 2556 | + | |
| 2557 | + | |
| 2558 | + | |
| 2559 | + | |
| 2560 | + | |
| 2561 | + | |
| 2562 | + | |
| 2563 | + | |
| 2564 | + | |
| 2565 | + | |
| 2566 | + | |
| 2567 | + | |
| 2568 | + | |
| 2569 | + | |
| 2570 | + | |
| 2571 | + | |
| 2572 | + | |
| 2573 | + | |
| 2574 | + | |
| 2575 | + | |
| 2576 | + | |
| 2577 | + | |
| 2578 | + | |
| 2579 | + | |
| 2580 | + | |
| 2581 | + | |
| 2582 | + | |
| 2583 | + | |
| 2584 | + | |
| 2585 | + | |
| 2586 | + | |
| 2587 | + | |
| 2588 | + | |
| 2589 | + | |
| 2590 | + | |
| 2591 | + | |
| 2592 | + | |
| 2593 | + | |
| 2594 | + | |
| 2595 | + | |
| 2596 | + | |
| 2597 | + | |
| 2598 | + | |
| 2599 | + | |
| 2600 | + | |
| 2601 | + | |
| 2602 | + | |
| 2603 | + | |
| 2604 | + | |
| 2605 | + | |
| 2606 | + | |
| 2607 | + | |
| 2608 | + | |
| 2609 | + | |
| 2610 | + | |
| 2611 | + | |
| 2612 | + | |
| 2613 | + | |
| 2614 | + | |
| 2615 | + | |
| 2616 | + | |
| 2617 | + | |
| 2618 | + | |
| 2619 | + | |
| 2620 | + | |
| 2621 | + | |
| 2622 | + | |
| 2623 | + | |
| 2624 | + | |
| 2625 | + | |
| 2626 | + | |
| 2627 | + | |
| 2628 | + | |
| 2629 | + | |
| 2630 | + | |
| 2631 | + | |
| 2632 | + | |
| 2633 | + | |
| 2634 | + | |
| 2635 | + | |
| 2636 | + | |
| 2637 | + | |
| 2638 | + | |
| 2639 | + | |
| 2640 | + | |
| 2641 | + | |
| 2642 | + | |
| 2643 | + | |
| 2644 | + | |
| 2645 | + | |
| 2646 | + | |
| 2647 | + | |
| 2648 | + | |
| 2649 | + | |
| 2650 | + | |
| 2651 | + | |
| 2652 | + | |
| 2653 | + | |
| 2654 | + | |
| 2655 | + | |
| 2656 | + | |
| 2657 | + | |
| 2658 | + | |
| 2659 | + | |
2505 | 2660 | | |
2506 | 2661 | | |
2507 | 2662 | | |
| |||
0 commit comments