Skip to content

Commit 52f9431

Browse files
authored
Cortex-M: keep the max_pool2d reference off aten's channels-last int8 path (pytorch#21820)
### Summary aten's channels-last max_pool2d buffers each window index in vec::int_same_size_t<opmath_t> and guards it with TORCH_CHECK(input_depth * input_height * input_width <= numeric_limits<integer_t>::max()) (aten/src/ATen/native/cpu/MaxPoolKernel.cpp, cpu_max_pool_channels_last). For int8 that caps an image at 127 spatial elements -- H*W, with channels not counted. Reproduce with: torch.nn.functional.max_pool2d( torch.zeros(1, 1, 12, 12, dtype=torch.int8).to( memory_format=torch.channels_last), 2, 2) The Cortex-M reference implementation called F.max_pool2d on the tensor as given, so a channels-last graph tripped that ceiling and raised before producing a result. That is a limit of the eager kernel and not of arm_max_pool_s8, so it only ever struck the host dialect stage -- but it struck it hard, since the whole model test aborts. Pooling does not depend on the memory format, so the reference now pools a contiguous copy; the existing return already puts the result back in channels-last, and .contiguous() aliases an already-contiguous tensor, so NCHW graphs pay nothing. quantized_max_pool2d_impl is the only reference in this file that pools in the native int8 dtype: avg_pool2d dequantizes to float first and both convolutions promote to int32, so none of them can reach the check. ### Test plan pytest backends/cortex_m/test/ops/test_max_pool2d.py -- 16 passed, 2 xfailed, dialect and Corstone-300; the new case adds ~2.4 s, in line with the existing implementation cases. Full backends/cortex_m/test dialect run: 404 passed. Reverting input.contiguous() fails the new case with the TORCH_CHECK above. Authored with assistance from Claude Code.
1 parent ae2958e commit 52f9431

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

‎backends/cortex_m/ops/operators.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1495,8 +1495,23 @@ def quantized_max_pool2d_impl(
14951495
if ceil_mode:
14961496
raise RuntimeError("quantized_max_pool2d does not support ceil_mode=True")
14971497

1498+
# aten's channels-last max_pool2d caps how large an image it will take, so
1499+
# pool a contiguous copy instead. Pooling is layout-invariant, and the
1500+
# return below puts the result back in channels-last either way.
1501+
#
1502+
# The cap: cpu_max_pool_channels_last buffers each window index in
1503+
# vec::int_same_size_t<opmath_t> and guards it with
1504+
# TORCH_CHECK(input_depth * input_height * input_width <= max), so int8
1505+
# rejects any image with more than 127 spatial elements -- H*W, with
1506+
# channels not counted. int16 hits the same wall at 32767, which a future
1507+
# quantized_max_pool2d_s16 will need to handle the same way.
1508+
#
1509+
# .to(memory_format=...) rather than .contiguous(): for C == 1 the
1510+
# channels-last strides also satisfy plain contiguity, so .contiguous()
1511+
# returns the same tensor while aten still dispatches on the memory-format
1512+
# hint and raises anyway.
14981513
result = F.max_pool2d(
1499-
input,
1514+
input.to(memory_format=torch.contiguous_format),
15001515
kernel,
15011516
stride=stride_vals,
15021517
padding=padding_vals,

‎backends/cortex_m/test/ops/test_max_pool2d.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
13
# Copyright 2026 Arm Limited and/or its affiliates.
24
#
35
# This source code is licensed under the BSD-style license found in the
@@ -34,6 +36,34 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
3436
return self.pool(x)
3537

3638

39+
class CortexMMaxPool2dPermutedView(torch.nn.Module):
40+
"""A single-channel NHWC image permuted to NCHW.
41+
42+
With C == 1 the channels-last strides also satisfy plain contiguity, so
43+
.contiguous() hands the reference back the very tensor it was given while
44+
aten keeps dispatching on the memory-format hint. This case is what pins
45+
the .to(memory_format=...) in quantized_max_pool2d_impl: it fails at the
46+
dialect stage if that is written as .contiguous().
47+
"""
48+
49+
# The permute brings its own quant/dequant pair, so only the pool itself
50+
# is pinned here.
51+
ops_before_transforms = {
52+
"executorch_exir_dialects_edge__ops_aten_max_pool2d_with_indices_default": 1,
53+
}
54+
ops_after_transforms = {
55+
"executorch_exir_dialects_edge__ops_cortex_m_quantized_max_pool2d_default": 1,
56+
"executorch_exir_dialects_edge__ops_aten_max_pool2d_with_indices_default": 0,
57+
}
58+
59+
def __init__(self, *args, **kwargs):
60+
super().__init__()
61+
self.pool = torch.nn.MaxPool2d(*args, **kwargs)
62+
63+
def forward(self, x: torch.Tensor) -> torch.Tensor:
64+
return self.pool(x.permute(0, 3, 1, 2))
65+
66+
3767
class CortexMMaxPool2dIndices(torch.nn.Module):
3868
ops_before_transforms = CortexMMaxPool2d.ops_before_transforms
3969
ops_after_transforms = CortexMMaxPool2d.ops_after_transforms
@@ -68,6 +98,18 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
6898
CortexMMaxPool2dIndices(kernel_size=2, stride=2),
6999
(ramp_tensor(-50, 50, (1, 1, 6, 6)),),
70100
),
101+
# 576 spatial elements (24x24), past the 127 that aten's channels-last int8
102+
# max_pool2d accepts; the reference pools a contiguous copy to avoid it.
103+
# randn rather than a ramp: a ramp over this many elements puts a whole
104+
# pooling window inside one int8 code, so it cannot tell max from min.
105+
"maxpool_2x2_large_channels_last": McuTestCase(
106+
CortexMMaxPool2d(kernel_size=2, stride=2),
107+
((torch.randn(1, 64, 24, 24) * 30).to(memory_format=torch.channels_last),),
108+
),
109+
"maxpool_2x2_single_channel_view": McuTestCase(
110+
CortexMMaxPool2dPermutedView(kernel_size=2, stride=2),
111+
((torch.randn(1, 24, 24, 1) * 30),),
112+
),
71113
}
72114

73115

0 commit comments

Comments
 (0)