Skip to content

Commit 74f1ce8

Browse files
Keep DS-CNN example inputs portable-safe (#21619)
Fixes the test-models-linux (ds_cnn, portable) failure by returning plain NCHW inputs from the example and doing the channels_last conversion in the Cortex-M path instead.
1 parent bcceab1 commit 74f1ce8

2 files changed

Lines changed: 13 additions & 11 deletions

File tree

backends/arm/scripts/aot_arm_compiler.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -925,12 +925,16 @@ def _to_edge_cortex_m(
925925

926926
def _to_channels_last(x):
927927
if isinstance(x, torch.Tensor):
928-
if x.dim() == 4 and not x.is_contiguous(memory_format=torch.channels_last):
929-
logging.warning(
930-
"Converting input tensor with shape %s to channels_last",
931-
list(x.shape),
932-
)
933-
return x.to(memory_format=torch.channels_last)
928+
if x.dim() == 4:
929+
# Singleton channels can satisfy both contiguity checks while
930+
# retaining NCHW strides, so always request the target format.
931+
channels_last = x.to(memory_format=torch.channels_last)
932+
if channels_last.stride() != x.stride():
933+
logging.warning(
934+
"Converting input tensor with shape %s to channels_last",
935+
list(x.shape),
936+
)
937+
return channels_last
934938
return x
935939
elif isinstance(x, tuple):
936940
return tuple(_to_channels_last(t) for t in x)
@@ -979,7 +983,7 @@ def _to_channels_last(x):
979983
)
980984
edge._edge_programs["forward"] = pass_manager.transform()
981985

982-
return model_quant, edge
986+
return model_quant, edge, example_inputs
983987

984988

985989
def _to_edge_no_delegate(
@@ -1078,7 +1082,7 @@ def main() -> None: # noqa: C901
10781082
"(this target does not use delegated ops)."
10791083
)
10801084
args.delegate = False
1081-
model_quant, edge = _to_edge_cortex_m(
1085+
model_quant, edge, example_inputs = _to_edge_cortex_m(
10821086
exported_program,
10831087
args,
10841088
model,

examples/models/mlperf_tiny/ds_cnn.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,4 @@ def get_eager_model(self) -> torch.nn.Module:
8383
return DSCNNKWS().eval()
8484

8585
def get_example_inputs(self):
86-
return (
87-
(torch.rand(1, 1, 49, 10) * 2 - 1).to(memory_format=torch.channels_last),
88-
)
86+
return (torch.rand(1, 1, 49, 10) * 2 - 1,)

0 commit comments

Comments
 (0)