ai.onnx.AveragePool

ai.onnx · standard ONNX operator · ONNX opset ≥ 19

Description

Applies average pooling over a sliding kernel window on input tensor X, computing the mean of values within each window position and writing results to Y. Output spatial dimensions are determined by kernel_shape, strides, dilations, pads, and ceil_mode; padded positions are excluded from the average by default unless count_include_pad is set.

See the ONNX AveragePool spec for the reference semantics.

Inputs

Name Upstream name Logical dtype Rank Shape Description Presence
x X T — — Input data tensor of shape (N x C x D1 x D2 ... Dn), where N is the batch size and C is the number of channels. required

Outputs

Name Upstream name Logical dtype Rank Shape Description Presence
y Y T same as x derived Output tensor with pooled values; spatial dimensions vary based on kernel, stride, dilation, and pad settings. required

Attributes

Attributes and default values (overridable per request):

Attribute Default Description
auto_pad "NOTSET" Deprecated auto-padding mode: NOTSET (use explicit pads), SAME_UPPER, SAME_LOWER (pad so output size is ceil(input / stride)), or VALID (no padding).
ceil_mode 0 When non-zero, uses ceiling instead of floor when computing the output spatial shape; defaults to 0.
count_include_pad 0 When non-zero, pad pixels are counted in the divisor when computing the average; defaults to 0 (exclude pad).
dilations — Dilation along each spatial axis. When omitted, every dilation is 1.
kernel_shape — Required kernel shape, with one positive value per spatial axis.
pads — Padding at the beginning and end of each spatial axis, ordered as [begin_0, ..., begin_n, end_0, ..., end_n]. When omitted, every pad is 0.
strides — Stride along each spatial axis. When omitted, every stride is 1.

Type constraints

Variable Allowed dtypes
T float32, float16

Implementation variants

One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.

  • window_parallel — Workgroup-cooperative average pooling over a 1-D, 2-D or 3-D window: one workgroup reduces a single output element's kernel window, its invocations striding the flattened taps. Chosen when the one-invocation-per-output variant cannot fill the device and the window is long. Padded taps leave the divisor unless count_include_pad is set.
  • nchw2d_k7_reuse — Average pooling that reuses each loaded input row across four horizontally adjacent outputs: 7x7 windows at stride 1 or 2, and 3-wide stride-1 windows whose input width is not four-aligned. Output widths that are a multiple of four store vec4 values; other outputs store scalars and guard the last quad.

Files

Use with @huggingface/kernels

npm install --save-exact @huggingface/kernels@0.0.1-preview.4

Paste the example into a browser app that resolves npm imports. Serve the app over HTTPS or localhost with WebGPU available.

Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.

The version: 1 option selects the published kernel contract; it is independent of any operator opset, contrib since_version, or model version. It follows the v1 branch as fixes land. To pin exact artifact bytes, pass a 40-character commit revision instead of version.

import { getKernel } from "@huggingface/kernels";

const kernel = await getKernel("webgpu-kernels/ai.onnx.AveragePool", { version: 1 });

const xData = Float32Array.of(
  1.7640524, 0.4001572, 0.978738, 2.2408931, 1.867558, -0.9772779, 0.95008844,
  -0.1513572, -0.10321885, 0.41059852, 0.14404356, 1.4542735, 0.7610377, 0.121675014,
  0.44386324, 0.33367434, 1.4940791, -0.20515826, 0.3130677, -0.85409576, -2.5529897,
  0.6536186, 0.8644362, -0.742165, 2.2697546, -1.4543657, 0.045758516, -0.18718386,
  1.5327792, 1.4693588, 0.15494743, 0.37816253, -0.88778573, -1.9807965, -0.34791216,
  0.15634897, 1.2302907, 1.2023798, -0.3873268, -0.30230275, -1.048553, -1.420018,
  -1.7062702, 1.9507754, -0.5096522, -0.4380743, -1.2527953, 0.7774904, -1.6138978,
  -0.21274029, -0.89546657, 0.3869025, -0.51080513, -1.1806322, -0.028182229, 0.42833188,
  0.06651722, 0.3024719, -0.6343221, -0.36274117, -0.67246044, -0.35955316, -0.8131463,
  -1.7262826, 0.17742614, -0.40178093, -1.6301984, 0.46278226, -0.9072984, 0.051945396,
  0.7290906, 0.12898292, 1.1394007, -1.2348258, 0.40234163, -0.6848101, -0.87079716,
  -0.5788497, -0.31155252, 0.05616534, -1.1651498, 0.9008265, 0.46566245, -1.5362437,
  1.4882522, 1.8958892, 1.1787796, -0.17992483, -1.0707526, 1.0544517, -0.40317693,
  1.222445, 0.20827498, 0.97663903, 0.3563664, 0.7065732,
);

const { y } = await kernel({ x: { data: xData, shape: [1, 3, 32] } }, {
  attrs: { kernel_shape: [2] },
});
console.log({ y });
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.