Skip to content

Commit a6390b4

Browse files
authored
Implement the portable kernel for avg_pool1d (#24122)
### Summary Fixes [#11325](#11325) ExecuTorch provides a portable average-pooling kernel for 2D inputs but lacks a portable kernel for `avg_pool1d.out`. This adds one-dimensional average pooling for batched `(N, C, L)` and unbatched `(C, L)` inputs. The implementation supports configurable kernel size, stride, padding, `ceil_mode`, and `count_include_pad`. It handles empty batches and dynamically resized outputs, with support for Float, Double, Long, Half, and BFloat16. Half and BFloat16 values accumulate in float. ### Algorithm ```text Validate input and output shapes, dtypes, layout, and pooling parameters. Use kernel_size as the stride when stride is omitted. Calculate the output length. In ceil mode, discard a final window that starts entirely in right padding. Resize the output tensor. For each batch/channel row: For each output position: Determine the pooling window and its valid input range. Sum the valid input values. If count_include_pad: Divide by the window size, including applicable padding. Otherwise: Divide by the number of valid input values. Store the result in the output tensor. ``` ### File changes - `kernels/portable/cpu/op_avg_pool1d.cpp`: implements the portable kernel and parameter validation. - `kernels/portable/functions.yaml`: registers `avg_pool1d.out`. - `kernels/test/op_avg_pool1d_test.cpp`: adds 33 tests covering batched and unbatched inputs, padding, ceil mode, empty batches, dtypes, dynamic outputs, and invalid arguments. - `shim_et/xplat/executorch/build/build_variables.bzl` and `shim_et/xplat/executorch/kernels/portable/op_registration_util.bzl`: include and register the kernel in the build. ### Test plan Regenerated bindings and compiled the new kernel and tests against an existing CMake runtime/GTest cache on Linux: ```sh GTEST_FILTER='OpAvgPool1DOutTest.*' \ ctest --test-dir cmake-out \ -R '^portable_kernels_test$' \ --output-on-failure ``` Result: portable_kernels_test passed; CTest reported 1/1 test target passed, 0 failed. cc @larryliu0820 @manuelcandales @JakeStevens @nil-is-all
1 parent d5e8362 commit a6390b4

10 files changed

Lines changed: 581 additions & 3 deletions

File tree

‎kernels/aten/functions.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484

8585
- op: atanh.out
8686

87+
- op: avg_pool1d.out
88+
8789
- op: avg_pool2d.out
8890

8991
- op: baddbmm.out
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#include <type_traits>
10+
11+
#include <executorch/kernels/portable/cpu/util/kernel_ops_util.h>
12+
#include <executorch/runtime/kernel/kernel_includes.h>
13+
14+
namespace torch {
15+
namespace executor {
16+
namespace native {
17+
18+
using Tensor = executorch::aten::Tensor;
19+
using ScalarType = executorch::aten::ScalarType;
20+
using IntArrayRef = executorch::aten::ArrayRef<int64_t>;
21+
22+
Tensor& avg_pool1d_out(
23+
KernelRuntimeContext& ctx,
24+
const Tensor& in,
25+
IntArrayRef kernel_size,
26+
IntArrayRef stride,
27+
IntArrayRef padding,
28+
bool ceil_mode,
29+
bool count_include_pad,
30+
Tensor& out) {
31+
ET_KERNEL_CHECK(
32+
ctx,
33+
check_avg_pool1d_args(in, kernel_size, stride, padding, ceil_mode, out),
34+
InvalidArgument,
35+
out);
36+
37+
size_t output_ndim = 0;
38+
executorch::aten::SizesType output_sizes[kTensorDimensionLimit];
39+
get_avg_pool1d_out_target_size(
40+
in, kernel_size, stride, padding, ceil_mode, output_sizes, &output_ndim);
41+
42+
ET_KERNEL_CHECK(
43+
ctx,
44+
output_size_is_valid({output_sizes, output_ndim}, 1),
45+
InvalidArgument,
46+
out);
47+
48+
ET_KERNEL_CHECK(
49+
ctx,
50+
resize_tensor(out, {output_sizes, output_ndim}) == Error::Ok,
51+
InvalidArgument,
52+
out);
53+
54+
ScalarType in_type = in.scalar_type();
55+
56+
// @lint-ignore CLANGTIDY facebook-hte-CArray
57+
static constexpr const char op_name[] = "avg_pool1d.out";
58+
59+
ET_SWITCH_FLOATHBF16_TYPES_AND(Long, in_type, ctx, op_name, CTYPE, [&]() {
60+
using ACC = std::conditional_t<
61+
std::is_same_v<CTYPE, executorch::aten::Half> ||
62+
std::is_same_v<CTYPE, executorch::aten::BFloat16>,
63+
float,
64+
CTYPE>;
65+
apply_kernel_1d_reduce_then_map_fn<CTYPE, ACC>(
66+
[](const CTYPE in_val, const ACC accum) {
67+
return accum + static_cast<ACC>(in_val);
68+
},
69+
[](const int64_t count, const ACC accum) {
70+
return static_cast<CTYPE>(accum / static_cast<ACC>(count));
71+
},
72+
count_include_pad,
73+
in,
74+
kernel_size,
75+
stride,
76+
padding,
77+
out);
78+
});
79+
80+
return out;
81+
}
82+
83+
} // namespace native
84+
} // namespace executor
85+
} // namespace torch

‎kernels/portable/cpu/util/kernel_ops_util.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,64 @@ void get_adaptive_avg_pool2d_out_target_size(
311311
out_sizes[*out_ndim - 1] = output_size[1];
312312
}
313313

314+
bool check_avg_pool1d_args(
315+
const Tensor& in,
316+
const IntArrayRef kernel_size,
317+
const IntArrayRef stride,
318+
const IntArrayRef padding,
319+
const bool ceil_mode,
320+
const Tensor& out) {
321+
ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(in, out));
322+
ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dim_order(in, out));
323+
ET_LOG_AND_RETURN_IF_FALSE(tensor_is_default_dim_order(in));
324+
325+
ET_CHECK_OR_RETURN_FALSE(
326+
(in.dim() == 2 && in.size(0) > 0 && in.size(1) > 0) ||
327+
(in.dim() == 3 && in.size(1) > 0 && in.size(2) > 0),
328+
"Expected 2D or 3D (batch mode) tensor with optional 0 dim batch size for input; in.dim() = %" ET_PRI_TENSOR_DIM,
329+
in.dim());
330+
331+
ET_LOG_AND_RETURN_IF_FALSE(
332+
kernel_size_is_valid(kernel_size, /*kernel_ndim=*/1));
333+
ET_LOG_AND_RETURN_IF_FALSE(
334+
stride_is_valid(stride, /*kernel_ndim=*/1, /*allow_empty=*/true));
335+
ET_LOG_AND_RETURN_IF_FALSE(padding_is_valid(
336+
padding, kernel_size, /*kernel_ndim=*/1, /*enforce_half_kernel=*/true));
337+
338+
// Guarantees a non-negative numerator in _kernel_output_size_helper, whose
339+
// truncating division would otherwise round up instead of down.
340+
const int64_t padded_len = in.size(in.dim() - 1) + 2 * padding[0];
341+
const int64_t effective_stride =
342+
val_at(stride, 0, /*default_value=*/kernel_size[0]);
343+
ET_CHECK_OR_RETURN_FALSE(
344+
padded_len - kernel_size[0] + (ceil_mode ? effective_stride - 1 : 0) >= 0,
345+
"Pooling parameters produce a non-positive output length");
346+
347+
return true;
348+
}
349+
350+
void get_avg_pool1d_out_target_size(
351+
const Tensor& in,
352+
const IntArrayRef kernel_size,
353+
const IntArrayRef stride,
354+
const IntArrayRef padding,
355+
const bool ceil_mode,
356+
executorch::aten::SizesType* const out_sizes,
357+
size_t* const out_ndim) {
358+
*out_ndim = in.dim();
359+
360+
// Batch dim is optional, so in can be either 2 or 3 dim.
361+
if (in.dim() == 3) {
362+
out_sizes[0] = in.size(0);
363+
out_sizes[1] = in.size(1);
364+
} else {
365+
out_sizes[0] = in.size(0);
366+
}
367+
368+
calculate_kernel_output_sizes(
369+
in, 1, kernel_size, stride, padding, {}, out_sizes, ceil_mode);
370+
}
371+
314372
bool check_avg_pool2d_args(
315373
const Tensor& in,
316374
const IntArrayRef kernel_size,

‎kernels/portable/cpu/util/kernel_ops_util.h‎

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,6 @@ void kernel_reduction_then_map_2d(
298298
* T map_fn(const int64_t count, const T accum);
299299
* ```
300300
*
301-
* TODO(ssjia) Allow this to handle 1-D kernels as well by unsqueezing
302-
* appropriately.
303-
*
304301
* @param[in] reduce_fn The reduction function used to update accumulator
305302
* values.
306303
* @param[in] map_fn The map function used to post-process the accumulated
@@ -382,6 +379,72 @@ void apply_kernel_2d_reduce_then_map_fn(
382379
}
383380
}
384381

382+
/**
383+
* 1-D counterpart of apply_kernel_2d_reduce_then_map_fn for contiguous inputs
384+
* of shape (C, L) or (N, C, L). Values are accumulated in ACC so that reduced
385+
* precision types can accumulate in a wider type.
386+
*
387+
* reduce_fn should have the following signature:
388+
*
389+
* ```
390+
* ACC reduce_fn(const CTYPE in_val, const ACC accum);
391+
* ```
392+
*
393+
* map_fn should have the following signature:
394+
*
395+
* ```
396+
* CTYPE map_fn(const int64_t count, const ACC accum);
397+
* ```
398+
*
399+
* @param[in] reduce_fn The reduction function used to update the accumulator
400+
* value.
401+
* @param[in] map_fn The map function used to post-process the accumulated
402+
* value before writing to out.
403+
* @param[in] include_pad Indicates if padded elements should be included in
404+
* the count passed to map_fn.
405+
* @param[in] in The input tensor.
406+
* @param[in] kernel_size 1D array describing the length of the kernel region.
407+
* @param[in] stride 1D array describing how the kernel region "traverses" over
408+
* the input tensor. Defaults to kernel_size when empty.
409+
* @param[in] padding 1D array describing padding to apply to the input tensor.
410+
* @param[in] out The output tensor.
411+
*/
412+
template <typename CTYPE, typename ACC, typename ReduceOp, typename MapOp>
413+
void apply_kernel_1d_reduce_then_map_fn(
414+
const ReduceOp& reduce_fn,
415+
const MapOp& map_fn,
416+
const bool include_pad,
417+
const Tensor& in,
418+
const IntArrayRef kernel_size,
419+
const IntArrayRef stride,
420+
const IntArrayRef padding,
421+
Tensor& out) {
422+
const int64_t kernel = kernel_size[0];
423+
const int64_t step = val_at(stride, 0, /*default_value=*/kernel);
424+
const int64_t pad = padding[0];
425+
const int64_t in_len = in.size(in.dim() - 1);
426+
const int64_t out_len = out.size(out.dim() - 1);
427+
428+
const CTYPE* const in_ptr = in.const_data_ptr<CTYPE>();
429+
CTYPE* const out_ptr = out.mutable_data_ptr<CTYPE>();
430+
431+
for (const auto row : c10::irange(in.numel() / in_len)) {
432+
const CTYPE* const in_row = in_ptr + row * in_len;
433+
CTYPE* const out_row = out_ptr + row * out_len;
434+
for (const auto o : c10::irange(out_len)) {
435+
const int64_t start = o * step - pad;
436+
const int64_t end = std::min(start + kernel, in_len + pad);
437+
const int64_t begin = std::max<int64_t>(start, 0);
438+
const int64_t stop = std::min(end, in_len);
439+
ACC accum = 0;
440+
for (const auto i : c10::irange(begin, stop)) {
441+
accum = reduce_fn(in_row[i], accum);
442+
}
443+
out_row[o] = map_fn(include_pad ? end - start : stop - begin, accum);
444+
}
445+
}
446+
}
447+
385448
//
386449
// Operator specific utility functions
387450
//
@@ -399,6 +462,23 @@ void get_adaptive_avg_pool2d_out_target_size(
399462

400463
bool check_arange_args(double start, double end, double step, Tensor& out);
401464

465+
bool check_avg_pool1d_args(
466+
const Tensor& in,
467+
const IntArrayRef kernel_size,
468+
const IntArrayRef stride,
469+
const IntArrayRef padding,
470+
const bool ceil_mode,
471+
const Tensor& out);
472+
473+
void get_avg_pool1d_out_target_size(
474+
const Tensor& in,
475+
const IntArrayRef kernel_size,
476+
const IntArrayRef stride,
477+
const IntArrayRef padding,
478+
const bool ceil_mode,
479+
executorch::aten::SizesType* const out_sizes,
480+
size_t* const out_ndim);
481+
402482
bool check_avg_pool2d_args(
403483
const Tensor& in,
404484
const IntArrayRef kernel_size,

‎kernels/portable/functions.yaml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@
182182
- arg_meta: null
183183
kernel_name: torch::executor::atanh_out
184184

185+
- op: avg_pool1d.out
186+
kernels:
187+
- arg_meta: null
188+
kernel_name: torch::executor::avg_pool1d_out
189+
185190
- op: avg_pool2d.out
186191
kernels:
187192
- arg_meta: null

‎kernels/test/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ set(all_test_sources
176176
"op_atan_test.cpp"
177177
"op_atan2_test.cpp"
178178
"op_atanh_test.cpp"
179+
"op_avg_pool1d_test.cpp"
179180
"op_avg_pool2d_test.cpp"
180181
"op_bitwise_and_test.cpp"
181182
"op_bitwise_left_shift_test.cpp"

0 commit comments

Comments
 (0)