Skip to content

Commit c491d01

Browse files
authored
Support max pooling in the CPU XNNPACK provider (#24236)
Summary: Add XNNPACK execution for static batched FP32 `max_pool2d_with_indices` when its indices have no consumers and are not graph outputs. The serialized graph retains the original semantic operator, preserving Reference fallback for integer tensors, empty batches, unbatched inputs, consumed indices, and Reference-only runtime compositions. XNNPACK skips allocating unused indices. The provider lowers pooling through NCHW/NHWC transposes with ATen argument broadcasting, padding validation, dilation, and ceil-mode handling. When a spatial input dimension is smaller than its dilation, explicit `-inf` padding prevents invalid reads and gives all-padding windows the correct result. `MaxPool1d` uses its existing 2D decomposition. Authored with assistance from Codex. Reviewed By: digantdesai Differential Revision: D123143945 Pull Request resolved: #24236
1 parent 616a0b4 commit c491d01

5 files changed

Lines changed: 735 additions & 3 deletions

File tree

‎backends/cpu/runtime/providers/xnnpack/XNNPACKProvider.cpp‎

Lines changed: 261 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,134 @@ bool fp32(const Kernel& node, const ptn::Graph& graph) {
6666
});
6767
}
6868

69+
struct MaxPool2d {
70+
ptn::ValueId input;
71+
ptn::ValueId output;
72+
int64_t kh, kw, sh, sw, ph, pw, dh, dw;
73+
bool ceil_mode;
74+
};
75+
76+
const std::vector<int64_t>* static_pool_list(const ptn::Argument& argument) {
77+
if (argument.kind() != ptn::ArgKind::IntList) {
78+
return nullptr;
79+
}
80+
const auto& list = argument.as_int_list();
81+
return std::all_of(
82+
list.ids.begin(),
83+
list.ids.end(),
84+
[](auto id) { return id == ptn::kInvalid; })
85+
? &list.values
86+
: nullptr;
87+
}
88+
89+
// Broadcasts an aten int[2] argument: empty uses the fallback, one element
90+
// broadcasts, two elements pass through; anything else is malformed.
91+
bool pool_pair(
92+
const std::vector<int64_t>& list,
93+
int64_t fallback,
94+
int64_t& first,
95+
int64_t& second) {
96+
if (list.empty()) {
97+
first = fallback;
98+
second = fallback;
99+
return true;
100+
}
101+
if (list.size() == 1) {
102+
first = list[0];
103+
second = list[0];
104+
return true;
105+
}
106+
if (list.size() == 2) {
107+
first = list[0];
108+
second = list[1];
109+
return true;
110+
}
111+
return false;
112+
}
113+
114+
int64_t pooling_output_size(
115+
int64_t in,
116+
int64_t k,
117+
int64_t s,
118+
int64_t p,
119+
int64_t d,
120+
bool ceil_mode) {
121+
const int64_t numerator = in + 2 * p - d * (k - 1) - 1;
122+
int64_t out = ceil_mode ? (numerator + s - 1) / s + 1 : numerator / s + 1;
123+
if (ceil_mode && (out - 1) * s >= in + p) {
124+
out -= 1;
125+
}
126+
return out;
127+
}
128+
129+
// Extra trailing padding so floor-only pooling produces aten ceil-mode output
130+
// sizes. Ports the ceil handling of the main XNNPACK backend: pad just enough
131+
// for the final partial window, unless aten's adjustment drops that window.
132+
int64_t ceil_pad_amount(
133+
int64_t in_size,
134+
int64_t kernel_size,
135+
int64_t stride,
136+
int64_t padding,
137+
int64_t dilation) {
138+
const int64_t no_ceil =
139+
in_size + 2 * padding - dilation * (kernel_size - 1) - 1;
140+
const int64_t output_size = (no_ceil + stride - 1) / stride + 1;
141+
const bool needs_adjust = (output_size - 1) * stride >= in_size + padding;
142+
const int64_t partial_stride = no_ceil % stride;
143+
return (partial_stride > 0 && !needs_adjust) ? stride - partial_stride : 0;
144+
}
145+
146+
std::optional<MaxPool2d> parse_max_pool2d(const Kernel& node) {
147+
if (node.target != "torch.ops.aten.max_pool2d_with_indices.default" ||
148+
node.inputs.size() != 6 || node.outputs.size() != 2 ||
149+
node.outputs[0].kind != ptn::OutputValueKind::Tensor ||
150+
node.outputs[1].kind != ptn::OutputValueKind::Tensor ||
151+
std::any_of(node.inputs.begin(), node.inputs.end(), [](const auto& in) {
152+
return in.mutated;
153+
})) {
154+
return std::nullopt;
155+
}
156+
const auto& input = node.inputs[0].arg;
157+
const auto* kernel = static_pool_list(node.inputs[1].arg);
158+
const auto* stride = static_pool_list(node.inputs[2].arg);
159+
const auto* padding = static_pool_list(node.inputs[3].arg);
160+
const auto* dilation = static_pool_list(node.inputs[4].arg);
161+
const auto& ceil = node.inputs[5].arg;
162+
if (input.kind() != ptn::ArgKind::Tensor || !kernel || !stride || !padding ||
163+
!dilation || ceil.kind() != ptn::ArgKind::Bool ||
164+
ceil.as_bool().id != ptn::kInvalid) {
165+
return std::nullopt;
166+
}
167+
MaxPool2d parsed;
168+
parsed.input = input.as_tensor().id;
169+
parsed.output = node.outputs[0].value_id;
170+
if (!pool_pair(*kernel, 0, parsed.kh, parsed.kw) || parsed.kh < 1 ||
171+
parsed.kw < 1) {
172+
return std::nullopt;
173+
}
174+
if (stride->empty()) {
175+
parsed.sh = parsed.kh;
176+
parsed.sw = parsed.kw;
177+
} else if (
178+
!pool_pair(*stride, 0, parsed.sh, parsed.sw) || parsed.sh < 1 ||
179+
parsed.sw < 1) {
180+
return std::nullopt;
181+
}
182+
if (!pool_pair(*padding, 0, parsed.ph, parsed.pw) || parsed.ph < 0 ||
183+
parsed.pw < 0) {
184+
return std::nullopt;
185+
}
186+
if (!pool_pair(*dilation, 1, parsed.dh, parsed.dw) || parsed.dh < 1 ||
187+
parsed.dw < 1) {
188+
return std::nullopt;
189+
}
190+
if (parsed.ph > parsed.kh / 2 || parsed.pw > parsed.kw / 2) {
191+
return std::nullopt;
192+
}
193+
parsed.ceil_mode = ceil.as_bool().value;
194+
return parsed;
195+
}
196+
69197
class XNNExecutable;
70198

71199
struct XNNOp {
@@ -131,11 +259,14 @@ class XNNExecutable final : public Executable {
131259
region.outputs.begin(), region.outputs.end());
132260
std::unordered_set<ptn::ValueId> used;
133261
for (auto id : region.nodes) {
134-
for (auto value : context_.graph.node(id).input_value_ids()) {
262+
const auto& node = context_.graph.node(id);
263+
for (auto value : node.input_value_ids()) {
135264
used.insert(value);
136265
}
137-
for (const auto& output : context_.graph.node(id).outputs) {
138-
used.insert(output.value_id);
266+
const auto output_count =
267+
parse_max_pool2d(node) ? 1 : node.outputs.size();
268+
for (size_t i = 0; i < output_count; ++i) {
269+
used.insert(node.outputs[i].value_id);
139270
}
140271
}
141272
bindings_.reserve(inputs.size() + outputs.size());
@@ -610,6 +741,130 @@ class XNNExecutable final : public Executable {
610741
(shape.size() == 1 && (shape[0] == 1 || shape[0] == filter[0])));
611742
}
612743

744+
static bool supports_max_pool(const Kernel& node, const ptn::Graph& graph) {
745+
const auto parsed = parse_max_pool2d(node);
746+
if (!parsed) {
747+
return false;
748+
}
749+
const auto& input = graph.value(parsed->input).tensor_meta();
750+
const auto& output = graph.value(parsed->output).tensor_meta();
751+
const auto indices_id = node.outputs[1].value_id;
752+
const auto& indices = graph.value(indices_id);
753+
if (!indices.consumer_ids.empty() ||
754+
std::find(
755+
graph.output_ids.begin(), graph.output_ids.end(), indices_id) !=
756+
graph.output_ids.end() ||
757+
indices.tensor_meta().dtype != ptn::ScalarType::Long ||
758+
indices.tensor_meta().sizes != output.sizes ||
759+
input.dtype != ptn::ScalarType::Float ||
760+
output.dtype != ptn::ScalarType::Float || input.sizes.size() != 4 ||
761+
output.sizes.size() != 4 || input.quant || output.quant ||
762+
input.sizes[0] != output.sizes[0] ||
763+
input.sizes[1] != output.sizes[1]) {
764+
return false;
765+
}
766+
return output.sizes[2] ==
767+
pooling_output_size(
768+
input.sizes[2],
769+
parsed->kh,
770+
parsed->sh,
771+
parsed->ph,
772+
parsed->dh,
773+
parsed->ceil_mode) &&
774+
output.sizes[3] ==
775+
pooling_output_size(
776+
input.sizes[3],
777+
parsed->kw,
778+
parsed->sw,
779+
parsed->pw,
780+
parsed->dw,
781+
parsed->ceil_mode);
782+
}
783+
784+
Error max_pool(const Kernel& node) {
785+
const auto parsed = parse_max_pool2d(node);
786+
ET_CHECK_OR_RETURN_ERROR(
787+
parsed, InvalidProgram, "XNNPACK max_pool2d schema mismatch");
788+
const auto input = parsed->input;
789+
const auto output = parsed->output;
790+
const auto in = dimensions(input);
791+
const auto out = dimensions(output);
792+
int64_t pad_top = parsed->ph, pad_left = parsed->pw;
793+
int64_t pad_bottom = parsed->ph, pad_right = parsed->pw;
794+
if (parsed->ceil_mode) {
795+
pad_bottom += ceil_pad_amount(
796+
in[2], parsed->kh, parsed->sh, parsed->ph, parsed->dh);
797+
pad_right += ceil_pad_amount(
798+
in[3], parsed->kw, parsed->sw, parsed->pw, parsed->dw);
799+
}
800+
uint32_t nhwc_in, nhwc_out;
801+
auto error = tensor({in[0], in[2], in[3], in[1]}, nhwc_in);
802+
if (error != Error::Ok) {
803+
return error;
804+
}
805+
error = tensor({out[0], out[2], out[3], out[1]}, nhwc_out);
806+
if (error != Error::Ok) {
807+
return error;
808+
}
809+
const size_t to_nhwc[] = {0, 2, 3, 1};
810+
const size_t to_nchw[] = {0, 3, 1, 2};
811+
CPU_XNN_CHECK(
812+
xnn_define_static_transpose(subgraph_, 4, to_nhwc, input, nhwc_in, 0));
813+
const auto infinity = std::numeric_limits<float>::infinity();
814+
// XNNPACK's implicit padding assumes every window samples the input.
815+
if (in[2] < static_cast<size_t>(parsed->dh) ||
816+
in[3] < static_cast<size_t>(parsed->dw)) {
817+
pad_bottom = std::max<int64_t>(
818+
pad_bottom,
819+
(static_cast<int64_t>(out[2]) - 1) * parsed->sh +
820+
(parsed->kh - 1) * parsed->dh + 1 - static_cast<int64_t>(in[2]) -
821+
pad_top);
822+
pad_right = std::max<int64_t>(
823+
pad_right,
824+
(static_cast<int64_t>(out[3]) - 1) * parsed->sw +
825+
(parsed->kw - 1) * parsed->dw + 1 - static_cast<int64_t>(in[3]) -
826+
pad_left);
827+
const size_t pre[] = {
828+
0, static_cast<size_t>(pad_top), static_cast<size_t>(pad_left), 0};
829+
const size_t post[] = {
830+
0,
831+
static_cast<size_t>(pad_bottom),
832+
static_cast<size_t>(pad_right),
833+
0};
834+
uint32_t padded_input;
835+
error = tensor(
836+
{in[0], in[2] + pre[1] + post[1], in[3] + pre[2] + post[2], in[1]},
837+
padded_input);
838+
if (error != Error::Ok) {
839+
return error;
840+
}
841+
CPU_XNN_CHECK(xnn_define_static_constant_pad_v2(
842+
subgraph_, 4, pre, post, -infinity, nhwc_in, padded_input, 0));
843+
nhwc_in = padded_input;
844+
pad_top = pad_left = pad_bottom = pad_right = 0;
845+
}
846+
CPU_XNN_CHECK(xnn_define_max_pooling_2d(
847+
subgraph_,
848+
static_cast<uint32_t>(pad_top),
849+
static_cast<uint32_t>(pad_right),
850+
static_cast<uint32_t>(pad_bottom),
851+
static_cast<uint32_t>(pad_left),
852+
static_cast<uint32_t>(parsed->kh),
853+
static_cast<uint32_t>(parsed->kw),
854+
static_cast<uint32_t>(parsed->sh),
855+
static_cast<uint32_t>(parsed->sw),
856+
static_cast<uint32_t>(parsed->dh),
857+
static_cast<uint32_t>(parsed->dw),
858+
-infinity,
859+
infinity,
860+
nhwc_in,
861+
nhwc_out,
862+
0));
863+
CPU_XNN_CHECK(xnn_define_static_transpose(
864+
subgraph_, 4, to_nchw, nhwc_out, output, 0));
865+
return Error::Ok;
866+
}
867+
613868
static bool supports_bmm(const Kernel& node, const ptn::Graph& graph) {
614869
if (!fp32(node, graph) || node.inputs.size() != 2 ||
615870
node.outputs.size() != 1 ||
@@ -869,6 +1124,9 @@ const XNNOp* find_op(std::string_view target) {
8691124
&E::supports_gelu,
8701125
&E::unary<xnn_unary_gelu>},
8711126
{"torch.ops.aten.linear.default", &E::supports_linear, &E::linear},
1127+
{"torch.ops.aten.max_pool2d_with_indices.default",
1128+
&E::supports_max_pool,
1129+
&E::max_pool},
8721130
{"torch.ops.aten.mean.dim", &E::supports_mean, &E::mean},
8731131
{"torch.ops.aten.mul.Tensor",
8741132
&E::supports_mul,

‎backends/cpu/runtime/test/CPUPlanTest.cpp‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,53 @@ class CPUPlanTest : public ::testing::Test {
8585

8686
// Cppcheck cannot expand GTest fixture macros without build headers.
8787
// cppcheck-suppress-begin syntaxError
88+
TEST_F(CPUPlanTest, MaxPool2dPreservesReferenceFallbackAndSkipsUnusedIndices) {
89+
graph.values.emplace_back(
90+
"input", ptn::ScalarType::Float, std::vector<int64_t>{1, 1, 4, 4});
91+
graph.values[0].role = ptn::ValueRole::UserInput;
92+
graph.values.emplace_back(
93+
"output", ptn::ScalarType::Float, std::vector<int64_t>{1, 1, 2, 2});
94+
graph.values.emplace_back(
95+
"indices", ptn::ScalarType::Long, std::vector<int64_t>{1, 1, 2, 2});
96+
ptn::Node node;
97+
node.name = "pool";
98+
node.target = "torch.ops.aten.max_pool2d_with_indices.default";
99+
node.inputs = {
100+
{"self", ptn::TensorArg{0}},
101+
{"kernel_size", ptn::IntListArg{{2, 2}, {}}},
102+
{"stride", ptn::IntListArg{{2, 2}, {}}},
103+
{"padding", ptn::IntListArg{{0, 0}, {}}},
104+
{"dilation", ptn::IntListArg{{1, 1}, {}}},
105+
{"ceil_mode", ptn::BoolArg{false}}};
106+
node.outputs = {
107+
{ptn::OutputValueKind::Tensor, 1}, {ptn::OutputValueKind::Tensor, 2}};
108+
graph.nodes.push_back(std::move(node));
109+
graph.input_ids = {0};
110+
finish({1});
111+
for (const bool reference_only : {false, true}) {
112+
if (reference_only) {
113+
configuration.providers = {factories.data() + 1, 1};
114+
}
115+
buffers.assign(graph.values.size(), Buffer{});
116+
CPUPlan plan(graph, buffers, configuration, execution);
117+
ASSERT_EQ(plan.select(), Error::Ok);
118+
ASSERT_EQ(plan.steps().size(), 1);
119+
EXPECT_EQ(
120+
plan.steps()[0].provider->name(),
121+
reference_only ? "Reference" : "XNNPACK");
122+
ASSERT_EQ(plan.prepare(allocator), Error::Ok);
123+
EXPECT_EQ(buffers[2].data != nullptr, reference_only);
124+
for (int i = 0; i < 16; ++i) {
125+
static_cast<float*>(buffers[0].data)[i] = i;
126+
}
127+
ASSERT_EQ(plan.execute(execution), Error::Ok);
128+
const std::array<float, 4> expected{5, 7, 13, 15};
129+
for (size_t i = 0; i < expected.size(); ++i) {
130+
EXPECT_FLOAT_EQ(static_cast<float*>(buffers[1].data)[i], expected[i]);
131+
}
132+
}
133+
}
134+
88135
TEST_F(
89136
CPUPlanTest,
90137
MixedRegionsPreserveFanoutOutputsAndReuseArenaAcrossInvocations) {

0 commit comments

Comments
 (0)