sync 91d990483a17
Browse filesFiles changed (11)
- README.md+15-10
- build/webgpu/average-pool2d-nchw-horizontal-reuse.wgsl.jinja+21-24
- build/webgpu/average-pool2d-nchw-w3s1-reuse.wgsl.jinja+12-15
- build/webgpu/bench.json+33-3
- build/webgpu/manifest.json+366-339
- build/webgpu/metadata.json+27-11
- build/webgpu/pool-global-reduction.wgsl.jinja+38-37
- build/webgpu/pool-window-nd.wgsl.jinja+17-20
- build/webgpu/pool-window-unroll.wgsl.jinja+28-29
- build/webgpu/pool2d-nchw-separable.wgsl.jinjaAdded+48
- build/webgpu/test.json
17 unmodified lines1819202122232425262728293031322 unmodified lines3536373839404142434445463 unmodified lines505152535455562 unmodified lines5960616263646566676869707117 unmodified lines## Inputs| Name | Bind key | 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 | Bind key | Logical dtype | Rank | Shape | Description | Presence || --- | --- | --- | --- | --- | --- | --- || `Y` | `y` | `T` | same as `X` | derived; see description | Output tensor with pooled values; spatial dimensions vary based on kernel, stride, dilation, and pad settings. | required |## Attributes2 unmodified lines| 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). || `count_include_pad` | `0` | When non-zero, pad pixels are counted in the divisor when computing the average; defaults to 0 (exclude pad). || `ceil_mode` | `0` | When non-zero, uses ceiling instead of floor when computing the output spatial shape; defaults to 0. || `kernel_shape` | — | Required kernel shape, with one positive value per spatial axis. || `strides` | — | Stride along each spatial axis. When omitted, every stride is 1. || `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. || `dilations` | — | Dilation along each spatial axis. When omitted, every dilation is 1. |## Type constraints3 unmodified lines## Files- [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, provenance)- [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)- [`test.json`](build/webgpu/test.json) — correctness cases- [`bench.json`](build/webgpu/bench.json) — benchmark + tuning cases2 unmodified lines- [`pool-global-reduction.wgsl.jinja`](build/webgpu/pool-global-reduction.wgsl.jinja)- [`pool-window-nd.wgsl.jinja`](build/webgpu/pool-window-nd.wgsl.jinja)- [`pool-window-unroll.wgsl.jinja`](build/webgpu/pool-window-unroll.wgsl.jinja)## Use with `@huggingface/kernels`The loader derives every required output's shape and logical dtype from the manifest contract and this call.It then allocates the result tensors automatically.The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version.Replace each `*Data` placeholder with a typed array containing the corresponding input data.17 unmodified lines1819202122232425262728293031322 unmodified lines3536373839404142434445463 unmodified lines505152535455562 unmodified lines59606162636465666768697071727374757617 unmodified lines## 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 |## Attributes2 unmodified lines| 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 constraints3 unmodified lines## Files- [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, per-variant templates, provenance)- [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)- [`test.json`](build/webgpu/test.json) — correctness cases- [`bench.json`](build/webgpu/bench.json) — benchmark + tuning cases2 unmodified lines- [`pool-global-reduction.wgsl.jinja`](build/webgpu/pool-global-reduction.wgsl.jinja)- [`pool-window-nd.wgsl.jinja`](build/webgpu/pool-window-nd.wgsl.jinja)- [`pool-window-unroll.wgsl.jinja`](build/webgpu/pool-window-unroll.wgsl.jinja)- [`pool2d-nchw-separable.wgsl.jinja`](build/webgpu/pool2d-nchw-separable.wgsl.jinja)## Use with `@huggingface/kernels````shnpm install --save-exact @huggingface/kernels@0.0.1-preview.2```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`.Replace each `*Data` placeholder with a typed array containing the corresponding input data.
1234567891011121314151617181920212 unmodified lines24252627282930311 unmodified line333435363738392 unmodified lines4243444546474849505152535455565758596061626364656667682 unmodified lines71727374757677{% if usesF16 %}enable f16;{% endif %}{{ env.wgsl.resourceDeclarations }}// Four adjacent windows share source columns. For a K-wide, dilation-D window// at stride S, their union spans (K-1)*D+1+3*S columns per row. Each output// retains its own accumulation order.const COUNT4: u32 = {{ source.count4 }}u;const OUT_W4: u32 = max(1u, {{ source.outW4 }}u);const OUT_H: u32 = max(1u, {{ source.outH }}u);{% if source.scalarOutput is defined and source.scalarOutput %}const OUT_W: u32 = {{ source.outW }}u;{% endif %}const IN_H: i32 = {{ source.inH }};const IN_W: i32 = {{ source.inW }};const IN_W_U: u32 = {{ source.inW }}u;const IN_PLANE: u32 = {{ source.inPlane }}u;const WG: u32 = {{ tunables.WORKGROUP_SIZE }}u;fn load_value(validRow: bool, base: u32, col: i32) -> f32 {2 unmodified lines}@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {let q = gid.x + gid.y * nwg.x * WG;if (q >= COUNT4) { return; }let ow0 = (q % OUT_W4) * 4u;1 unmodified linelet oh = t % OUT_H;let plane = t / OUT_H;let planeBase = plane * IN_PLANE;let startW = i32(ow0 * {{ source.strideW }}u) - {{ source.padLeft }};var acc0 = 0.0; var acc1 = 0.0; var acc2 = 0.0; var acc3 = 0.0;// Window validity is separable: every valid kernel row combines with every2 unmodified linesvar validRows = 0u;{% for lane in range(4) %}var validCols{{ lane }} = 0u;{% for kw in range(source.kernelW) %}validCols{{ lane }} += select(0u,1u,startW + {{ lane * source.strideW + kw * source.dilationW }} >= 0 &&startW + {{ lane * source.strideW + kw * source.dilationW }} < IN_W,);{% endfor %}{% endfor %}{% for kh in range(source.kernelH) %}{let ih = i32(oh * {{ source.strideH }}u) + {{ kh * source.dilationH - source.padTop }};let validRow = ih >= 0 && ih < IN_H;validRows += select(0u, 1u, validRow);let rowBase = planeBase + u32(max(ih, 0)) * IN_W_U;{% for j in range((source.kernelW - 1) * source.dilationW + 1 + 3 * source.strideW) %}let v{{ kh }}_{{ j }} = load_value(validRow, rowBase, startW + {{ j }});{% endfor %}{% for lane in range(4) %}{% for kw in range(source.kernelW) %}acc{{ lane }} += v{{ kh }}_{{ lane * source.strideW + kw * source.dilationW }};{% endfor %}{% endfor %}}2 unmodified lineslet cnt{{ lane }} = validRows * validCols{{ lane }};{% endfor %}{% if source.scalarOutput is defined and source.scalarOutput %}let outBase = (plane * OUT_H + oh) * OUT_W + ow0;y[outBase] = {{ scalar }}(select(0.0, acc0 / f32(cnt0), cnt0 > 0u));if (ow0 + 1u < OUT_W) { y[outBase + 1u] = {{ scalar }}(select(0.0, acc1 / f32(cnt1), cnt1 > 0u)); }1234567891011121314151617182 unmodified lines21222324252627281 unmodified line303132333435362 unmodified lines3940414243444546474849505152535455565758596061626364652 unmodified lines68697071727374{{ env.wgsl.resourceDeclarations }}// Four adjacent windows share source columns. For a K-wide, dilation-D window// at stride S, their union spans (K-1)*D+1+3*S columns per row. Each output// retains its own accumulation order.const COUNT4: u32 = {{ count4 }}u;const OUT_W4: u32 = max(1u, {{ outW4 }}u);const OUT_H: u32 = max(1u, {{ outH }}u);{% if scalarOutput is defined and scalarOutput %}const OUT_W: u32 = {{ outW }}u;{% endif %}const IN_H: i32 = {{ inH }};const IN_W: i32 = {{ inW }};const IN_W_U: u32 = {{ inW }}u;const IN_PLANE: u32 = {{ inPlane }}u;const WG: u32 = {{ tunables.WORKGROUP_SIZE }}u;fn load_value(validRow: bool, base: u32, col: i32) -> f32 {2 unmodified lines}@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})fn main(@builtin(global_invocation_id) gid: vec3<u32>) {let q = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * WG;if (q >= COUNT4) { return; }let ow0 = (q % OUT_W4) * 4u;1 unmodified linelet oh = t % OUT_H;let plane = t / OUT_H;let planeBase = plane * IN_PLANE;let startW = i32(ow0 * {{ strideWSpec }}u) - {{ padLeftSpec }};var acc0 = 0.0; var acc1 = 0.0; var acc2 = 0.0; var acc3 = 0.0;// Window validity is separable: every valid kernel row combines with every2 unmodified linesvar validRows = 0u;{% for lane in range(4) %}var validCols{{ lane }} = 0u;{% for kw in range(kernelWSpec) %}validCols{{ lane }} += select(0u,1u,startW + {{ lane * strideWSpec + kw * dilationWSpec }} >= 0 &&startW + {{ lane * strideWSpec + kw * dilationWSpec }} < IN_W,);{% endfor %}{% endfor %}{% for kh in range(kernelHSpec) %}{let ih = i32(oh * {{ strideHSpec }}u) + {{ kh * dilationHSpec - padTopSpec }};let validRow = ih >= 0 && ih < IN_H;validRows += select(0u, 1u, validRow);let rowBase = planeBase + u32(max(ih, 0)) * IN_W_U;{% for j in range((kernelWSpec - 1) * dilationWSpec + 1 + 3 * strideWSpec) %}let v{{ kh }}_{{ j }} = load_value(validRow, rowBase, startW + {{ j }});{% endfor %}{% for lane in range(4) %}{% for kw in range(kernelWSpec) %}acc{{ lane }} += v{{ kh }}_{{ lane * strideWSpec + kw * dilationWSpec }};{% endfor %}{% endfor %}}2 unmodified lineslet cnt{{ lane }} = validRows * validCols{{ lane }};{% endfor %}{% if scalarOutput is defined and scalarOutput %}let outBase = (plane * OUT_H + oh) * OUT_W + ow0;y[outBase] = {{ scalar }}(select(0.0, acc0 / f32(cnt0), cnt0 > 0u));if (ow0 + 1u < OUT_W) { y[outBase + 1u] = {{ scalar }}(select(0.0, acc1 / f32(cnt1), cnt1 > 0u)); }
1234563 unmodified lines101112131415161718192021222310 unmodified lines343536373839404111 unmodified lines535455565758596061{% if usesF16 %}enable f16;{% endif %}{{ env.wgsl.resourceDeclarations }}// 2D NCHW AveragePool specialised for a 3-wide / stride-1 / pad-1 / dilation-13 unmodified lines// instead loads six contiguous columns per kernel row and slides the window in// registers. Accumulation remains kh-major then kw-major, and excludes padding// from the denominator.const OUT_W: u32 = {{ source.outW }}u;const OUT_H: u32 = {{ source.outH }}u;const OUT_W_Q: u32 = {{ source.outWQuarter }}u; // OUT_W / 4 (columns-of-four)const IN_H: i32 = {{ source.inH }};const IN_W: i32 = {{ source.inW }};const IN_W_U: u32 = {{ source.inW }}u;const IN_PLANE: u32 = {{ source.inPlane }}u;const PAD_TOP: i32 = {{ source.padTop }};// Load input element at (rowBase, col), returning 0 outside [0, IN_W). The row// is already gated, so only the horizontal bound is checked here.10 unmodified lines}@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {let tile = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u;if (tile >= params.tileCount) {return;}11 unmodified linesvar acc0 = 0.0; var acc1 = 0.0; var acc2 = 0.0; var acc3 = 0.0;var cnt0 = 0u; var cnt1 = 0u; var cnt2 = 0u; var cnt3 = 0u;{% for kh in range(source.kernelH) %}{let ih = startH + {{ kh * source.dilationH }};let rowValid = ih >= 0 && ih < IN_H;let rowBase = planeBase + u32(max(ih, 0)) * IN_W_U;// Six contiguous input columns feeding the four 3-wide windows.1233 unmodified lines789101112131415161718192010 unmodified lines313233343536373811 unmodified lines505152535455565758{{ env.wgsl.resourceDeclarations }}// 2D NCHW AveragePool specialised for a 3-wide / stride-1 / pad-1 / dilation-13 unmodified lines// instead loads six contiguous columns per kernel row and slides the window in// registers. Accumulation remains kh-major then kw-major, and excludes padding// from the denominator.const OUT_W: u32 = {{ outW }}u;const OUT_H: u32 = {{ outH }}u;const OUT_W_Q: u32 = {{ outWQuarter }}u; // OUT_W / 4 (columns-of-four)const IN_H: i32 = {{ inH }};const IN_W: i32 = {{ inW }};const IN_W_U: u32 = {{ inW }}u;const IN_PLANE: u32 = {{ inPlane }}u;const PAD_TOP: i32 = {{ padTopSpec }};// Load input element at (rowBase, col), returning 0 outside [0, IN_W). The row// is already gated, so only the horizontal bound is checked here.10 unmodified lines}@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})fn main(@builtin(global_invocation_id) gid: vec3<u32>) {let tile = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u;if (tile >= params.tileCount) {return;}11 unmodified linesvar acc0 = 0.0; var acc1 = 0.0; var acc2 = 0.0; var acc3 = 0.0;var cnt0 = 0u; var cnt1 = 0u; var cnt2 = 0u; var cnt3 = 0u;{% for kh in range(kernelHSpec) %}{let ih = startH + {{ kh * dilationHSpec }};let rowValid = ih >= 0 && ih < IN_H;let rowBase = planeBase + u32(max(ih, 0)) * IN_W_U;// Six contiguous input columns feeding the four 3-wide windows.
12345145 unmodified lines1511521531541551561571589 unmodified lines168169170171172173{"op": "ai.onnx.AveragePool","tunableSpace": { "WORKGROUP_SIZE": [64, 128, 256] },"cases": [{145 unmodified lines"name": "global-averagepool-rank5-serial-reduction-pathology","preset": "stress","provenance": {"source": "authored for variant coverage","notes": "Rank-5 global pooling over 131072 spatial values per plane verifies global_window_parallel coverage and its feature-tier fallbacks."},"vars": { "batch": 1, "channels": 32, "inD": 32, "inH": 64, "inW": 64 },"attrs": {9 unmodified lines"bench": {"metrics": [{ "type": "bandwidth", "value": "args.batch * args.channels * args.inD * args.inH * args.inW * 4" }]}}]}1234145 unmodified lines1501511521531541551561579 unmodified lines167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203{"tunableSpace": { "WORKGROUP_SIZE": [64, 128, 256] },"cases": [{145 unmodified lines"name": "global-averagepool-rank5-serial-reduction-pathology","preset": "stress","provenance": {"source": "synthetic benchmark","notes": "Rank-5 global pooling over 131,072 spatial values per plane exercises the parallel global-window reduction and its feature-tier alternatives."},"vars": { "batch": 1, "channels": 32, "inD": 32, "inH": 64, "inW": 64 },"attrs": {9 unmodified lines"bench": {"metrics": [{ "type": "bandwidth", "value": "args.batch * args.channels * args.inD * args.inH * args.inW * 4" }]}},{"name": "nchw_8x64x112x112_k3s1_same_upper_spelling","preset": "smoke","vars": { "batch": 8, "channels": 64, "inH": 112, "inW": 112 },"attrs": { "kernel_shape": [3, 3], "strides": [1, 1], "auto_pad": "SAME_UPPER" },"inputs": { "x": { "dtype": "float32", "shape": [8, 64, 112, 112], "dist": "normal", "seed": 205 } },"outputs": { "y": { "dtype": "float32", "shape": [8, 64, 112, 112], "dist": "empty" } },"bench": {"primary": true,"metrics": [{ "type": "bandwidth", "value": "args.batch * args.channels * args.inH * args.inW * 4 * 2" }]},"provenance": {"notes": "SAME_UPPER resolves to symmetric padding for this shape, so every pooling window has the same geometry as the explicit-padding form."}},{"name": "average-pool-f32-b8c64-56x56-k7s1-valid-no-pad","preset": "edge","attrs": { "kernel_shape": [7, 7], "strides": [1, 1] },"inputs": { "x": { "dtype": "float32", "shape": [8, 64, 56, 56] } },"outputs": { "y": { "dtype": "float32", "shape": [8, 64, 50, 50] } },"bench": { "metrics": [{ "type": "bandwidth", "value": "(numel(shapes.x) + numel(shapes.y)) * 4" }] }},{"name": "average-pool-f32-b8c64-56x56-k7s7-valid-no-pad","preset": "edge","attrs": { "kernel_shape": [7, 7], "strides": [7, 7] },"inputs": { "x": { "dtype": "float32", "shape": [8, 64, 56, 56] } },"outputs": { "y": { "dtype": "float32", "shape": [8, 64, 8, 8] } },"bench": { "metrics": [{ "type": "bandwidth", "value": "(numel(shapes.x) + numel(shapes.y)) * 4" }] }}]}
1 unmodified line234567891011121314151617181920212223242526272829303132332 unmodified lines36373839404142434445464714 unmodified lines62636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772781 unmodified line2802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433441 unmodified line3463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884891 unmodified line"domain": "ai.onnx","name": "AveragePool","sinceVersion": 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.","inputs": [{"role": "X","dtype": "T","description": "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."}],"outputs": [{"role": "Y","dtype": "T","description": "Output tensor with pooled values; spatial dimensions vary based on kernel, stride, dilation, and pad settings.","rank": "ranks.X","shape": "poolOutputShape"}],"attributes": { "auto_pad": "NOTSET", "count_include_pad": 0, "ceil_mode": 0 },"attributeDescriptions": {"auto_pad": "Deprecated auto-padding mode: `NOTSET` (use explicit pads), `SAME_UPPER`, `SAME_LOWER` (pad so output size is `ceil(input / stride)`), or `VALID` (no padding).","kernel_shape": "Required kernel shape, with one positive value per spatial axis.","strides": "Stride along each spatial axis. When omitted, every stride is 1.","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.","dilations": "Dilation along each spatial axis. When omitted, every dilation is 1.","count_include_pad": "When non-zero, pad pixels are counted in the divisor when computing the average; defaults to 0 (exclude pad).","ceil_mode": "When non-zero, uses ceiling instead of floor when computing the output spatial shape; defaults to 0."},"attributeConstraints": {"auto_pad": { "values": ["NOTSET", "SAME_UPPER", "SAME_LOWER", "VALID"] },2 unmodified lines"kernel_shape": { "required": true }},"typeConstraints": { "T": ["float32", "float16"] },"args": {"x": { "kind": "tensor", "semantic": "X", "role": "input" },"y": { "kind": "tensor", "semantic": "Y", "role": "output" }},"derive": {"spatialRank": "ranks.X - 2","narrowSubgroupRange": "has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize < device.adapterInfo.subgroupMaxSize and device.adapterInfo.subgroupMaxSize <= 16","kernelShapeLengthOk": "not has(attrs, \"kernel_shape\") or (attrs.kernel_shape | length) == spatialRank","stridesLengthOk": "not has(attrs, \"strides\") or (attrs.strides | length) == spatialRank",14 unmodified lines"padBack": "attrs.pads[spatialRank] if padsLengthOk and has(attrs, \"pads\") and spatialRank == 3 else 0","padBottom": "attrs.pads[2 * spatialRank - 2] if padsLengthOk and has(attrs, \"pads\") and spatialRank >= 2 else 0","padRight": "attrs.pads[2 * spatialRank - 1] if padsLengthOk and has(attrs, \"pads\") and spatialRank >= 1 else 0","poolBaseOk": "f16Ok(dtypes.T) and ranks.X >= 3 and ranks.X <= 5 and ranks.Y == ranks.X and has(attrs, \"kernel_shape\") and kernelShapeLengthOk and stridesLengthOk and dilationsLengthOk and padsLengthOk and kernelD > 0 and kernelH > 0 and kernelW > 0 and strideD > 0 and strideH > 0 and strideW > 0 and dilationD > 0 and dilationH > 0 and dilationW > 0 and padFront >= 0 and padTop >= 0 and padLeft >= 0 and padBack >= 0 and padBottom >= 0 and padRight >= 0 and (attrs.auto_pad == \"NOTSET\" or not has(attrs, \"pads\")) and dim(shapes.Y, 0) == dim(shapes.X, 0) and dim(shapes.Y, 1) == dim(shapes.X, 1)","explicitOutW": "min(ceil((dim(shapes.X, ranks.X - 1) + padLeft + padRight - ((kernelW - 1) * dilationW + 1)) / strideW) + 1, ceil((dim(shapes.X, ranks.X - 1) + padLeft) / strideW)) if attrs.ceil_mode != 0 else floor((dim(shapes.X, ranks.X - 1) + padLeft + padRight - ((kernelW - 1) * dilationW + 1)) / strideW) + 1","explicitOutH": "min(ceil((dim(shapes.X, ranks.X - 2) + padTop + padBottom - ((kernelH - 1) * dilationH + 1)) / strideH) + 1, ceil((dim(shapes.X, ranks.X - 2) + padTop) / strideH)) if attrs.ceil_mode != 0 else floor((dim(shapes.X, ranks.X - 2) + padTop + padBottom - ((kernelH - 1) * dilationH + 1)) / strideH) + 1","explicitOutD": "(min(ceil((dim(shapes.X, 2) + padFront + padBack - ((kernelD - 1) * dilationD + 1)) / strideD) + 1, ceil((dim(shapes.X, 2) + padFront) / strideD)) if attrs.ceil_mode != 0 else floor((dim(shapes.X, 2) + padFront + padBack - ((kernelD - 1) * dilationD + 1)) / strideD) + 1) if ranks.X == 5 else 0","inferredOutW": "ceil(dim(shapes.X, ranks.X - 1) / strideW) if attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\" else explicitOutW","inferredOutH": "ceil(dim(shapes.X, ranks.X - 2) / strideH) if attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\" else explicitOutH","inferredOutD": "ceil(dim(shapes.X, 2) / strideD) if attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\" else explicitOutD","poolOutputShape": "[dim(shapes.X, 0), dim(shapes.X, 1), inferredOutW] if ranks.X == 3 else ([dim(shapes.X, 0), dim(shapes.X, 1), inferredOutH, inferredOutW] if ranks.X == 4 else [dim(shapes.X, 0), dim(shapes.X, 1), inferredOutD, inferredOutH, inferredOutW])","autoPadTotalH": "max(0, (dim(shapes.Y, 2) - 1) * strideH + ((kernelH - 1) * dilationH + 1) - dim(shapes.X, 2)) if ranks.X == 4 else 0","autoPadTotalW": "max(0, (dim(shapes.Y, 3) - 1) * strideW + ((kernelW - 1) * dilationW + 1) - dim(shapes.X, 3)) if ranks.X == 4 else 0","effectivePadTop": "(floor(autoPadTotalH / 2) if attrs.auto_pad == \"SAME_UPPER\" else autoPadTotalH - floor(autoPadTotalH / 2)) if ranks.X == 4 and (attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\") else padTop","effectivePadLeft": "(floor(autoPadTotalW / 2) if attrs.auto_pad == \"SAME_UPPER\" else autoPadTotalW - floor(autoPadTotalW / 2)) if ranks.X == 4 and (attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\") else padLeft","effectivePadBottom": "autoPadTotalH - effectivePadTop if ranks.X == 4 and (attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\") else padBottom","effectivePadRight": "autoPadTotalW - effectivePadLeft if ranks.X == 4 and (attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\") else padRight","narrowF32W3UnrolledPreferred": "narrowSubgroupRange and dtypes.T == \"f32\"","globalWindowParallelCovered": "poolBaseOk and numel(shapes.Y) == dim(shapes.X, 0) * dim(shapes.X, 1) and numel(shapes.Y) <= device.limits.maxComputeWorkgroupsPerDimension and attrs.auto_pad == \"NOTSET\" and strideD == 1 and strideH == 1 and strideW == 1 and dilationD == 1 and dilationH == 1 and dilationW == 1 and padFront == 0 and padTop == 0 and padLeft == 0 and padBack == 0 and padBottom == 0 and padRight == 0 and ((ranks.X == 3 and kernelW == dim(shapes.X, 2)) or (ranks.X == 4 and kernelH == dim(shapes.X, 2) and kernelW == dim(shapes.X, 3)) or (ranks.X == 5 and kernelD == dim(shapes.X, 2) and kernelH == dim(shapes.X, 3) and kernelW == dim(shapes.X, 4))) and inner(shapes.X, 1) >= 256"},"constants": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"" },"bindingSets": {"globalReduction": [{ "name": "x", "arg": "x", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$xElem" },{ "name": "y", "arg": "y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$scalar" },{"name": "params","semantic": "kernel.params","buffer": { "type": "uniform" },"struct": { "name": "Params", "fields": [{ "name": "spatial", "type": "u32", "value": "inner(shapes.X, 1)" }] }}],"horizontal": [{ "name": "x", "arg": "x", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$T" },{ "name": "y", "arg": "y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$outputElement" }],"w3Reuse": [{ "name": "x", "arg": "x", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$T" },{ "name": "y", "arg": "y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$T" },{"name": "params","semantic": "kernel.params","buffer": { "type": "uniform" },"struct": {"name": "Params","fields": [{ "name": "tileCount", "type": "u32", "value": "numel(shapes.Y) / 4" }]}}],"flatDirect": [{ "name": "x", "arg": "x", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$T" },{ "name": "y", "arg": "y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$T" },{"name": "params","semantic": "kernel.params","buffer": { "type": "uniform" },"struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.Y)" }] }}],"rank1Direct": [{ "name": "x", "arg": "x", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$T" },{ "name": "y", "arg": "y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$T" },{"name": "params","semantic": "kernel.params","buffer": { "type": "uniform" },"struct": {"name": "Params","fields": [{ "name": "channels", "type": "u32", "value": "dim(shapes.X, 1)" },{ "name": "inW", "type": "u32", "value": "dim(shapes.X, 2)" },{ "name": "outW", "type": "u32", "value": "dim(shapes.Y, 2)" },{ "name": "kernelW", "type": "u32", "value": "kernelW" },{ "name": "strideW", "type": "u32", "value": "strideW" },{ "name": "dilationW", "type": "u32", "value": "dilationW" },{ "name": "padLeft", "type": "i32", "value": "padLeft" },{ "name": "padRight", "type": "i32", "value": "padRight" },{ "name": "countIncludePad", "type": "u32", "value": "attrs.count_include_pad" },{ "name": "count", "type": "u32", "value": "numel(shapes.Y)" }]}}],"rank3Direct": [{ "name": "x", "arg": "x", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$T" },{ "name": "y", "arg": "y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$T" },{"name": "params","semantic": "kernel.params","buffer": { "type": "uniform" },"struct": {"name": "Params","fields": [{ "name": "channels", "type": "u32", "value": "dim(shapes.X, 1)" },{ "name": "inD", "type": "u32", "value": "dim(shapes.X, 2)" },{ "name": "inH", "type": "u32", "value": "dim(shapes.X, 3)" },{ "name": "inW", "type": "u32", "value": "dim(shapes.X, 4)" },{ "name": "outD", "type": "u32", "value": "dim(shapes.Y, 2)" },{ "name": "outH", "type": "u32", "value": "dim(shapes.Y, 3)" },{ "name": "outW", "type": "u32", "value": "dim(shapes.Y, 4)" },{ "name": "kernelD", "type": "u32", "value": "kernelD" },{ "name": "kernelH", "type": "u32", "value": "kernelH" },{ "name": "kernelW", "type": "u32", "value": "kernelW" },{ "name": "strideD", "type": "u32", "value": "strideD" },{ "name": "strideH", "type": "u32", "value": "strideH" },{ "name": "strideW", "type": "u32", "value": "strideW" },{ "name": "dilationD", "type": "u32", "value": "dilationD" },{ "name": "dilationH", "type": "u32", "value": "dilationH" },{ "name": "dilationW", "type": "u32", "value": "dilationW" },{ "name": "padFront", "type": "i32", "value": "padFront" },{ "name": "padTop", "type": "i32", "value": "padTop" },{ "name": "padLeft", "type": "i32", "value": "padLeft" },{ "name": "padBack", "type": "i32", "value": "padBack" },{ "name": "padBottom", "type": "i32", "value": "padBottom" },{ "name": "padRight", "type": "i32", "value": "padRight" },{ "name": "countIncludePad", "type": "u32", "value": "attrs.count_include_pad" },{ "name": "count", "type": "u32", "value": "numel(shapes.Y)" }]}}],"rank2Direct": [{ "name": "x", "arg": "x", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$T" },{ "name": "y", "arg": "y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$T" },{"name": "params","semantic": "kernel.params","buffer": { "type": "uniform" },"struct": {"name": "Params","fields": [{ "name": "channels", "type": "u32", "value": "dim(shapes.X, 1)" },{ "name": "inH", "type": "u32", "value": "dim(shapes.X, 2)" },{ "name": "inW", "type": "u32", "value": "dim(shapes.X, 3)" },{ "name": "outH", "type": "u32", "value": "dim(shapes.Y, 2)" },{ "name": "outW", "type": "u32", "value": "dim(shapes.Y, 3)" },{ "name": "kernelH", "type": "u32", "value": "kernelH" },{ "name": "kernelW", "type": "u32", "value": "kernelW" },{ "name": "strideH", "type": "u32", "value": "strideH" },{ "name": "strideW", "type": "u32", "value": "strideW" },{ "name": "dilationH", "type": "u32", "value": "dilationH" },{ "name": "dilationW", "type": "u32", "value": "dilationW" },{ "name": "padTop", "type": "i32", "value": "effectivePadTop" },{ "name": "padLeft", "type": "i32", "value": "effectivePadLeft" },{ "name": "padBottom", "type": "i32", "value": "effectivePadBottom" },{ "name": "padRight", "type": "i32", "value": "effectivePadRight" },{ "name": "countIncludePad", "type": "u32", "value": "attrs.count_include_pad" },{ "name": "count", "type": "u32", "value": "numel(shapes.Y)" }]}}]},"tunables": { "WORKGROUP_SIZE": 256 },"variants": [{"id": "global_window_parallel","priority": 50,"when": ["globalWindowParallelCovered"],"constants": {"xElem": "(\"vec4<f16>\" if dtypes.T == \"f16\" else \"vec4<f32>\") if inner(shapes.X, 1) % 4 == 0 else dtypes.T"},"passes": [{"id": "main","name": "AveragePool.GlobalWindowParallel","source": {"shader": "pool-global-reduction.wgsl.jinja","inputs": {"op": "\"average\"","vec4": "inner(shapes.X, 1) % 4 == 0","workgroupSize": "tunables.WORKGROUP_SIZE","useSubgroups": "device.features.has(\"subgroups\")"}},"bindings": "globalReduction","dispatch": { "x": "numel(shapes.Y)" }}]},{"id": "nchw2d_k7s1_reuse_tail","priority": 34,"when": ["poolBaseOk", "ranks.X == 4", "kernelH == 7", "kernelW == 7", "strideH == 1", "strideW == 1", "dilationH == 1", "dilationW == 1", "padTop == 3", "padBottom == 3", "padLeft == 3", "padRight == 3", "attrs.count_include_pad == 0", "attrs.ceil_mode == 0", "dim(shapes.X, 3) % 4 != 0", "dim(shapes.Y, 2) == dim(shapes.X, 2)", "dim(shapes.Y, 3) == dim(shapes.X, 3)", "dim(shapes.Y, 2) > 0", "dim(shapes.Y, 3) > 0"],"constants": { "outputElement": "dtypes.T" },"passes": [{"id": "main","name": "AveragePool.Nchw2dK7s1ReuseTail","source": {"shader": "average-pool2d-nchw-horizontal-reuse.wgsl.jinja","inputs": {"kernelH": 7,"kernelW": 7,"strideH": 1,"strideW": 1,"dilationH": 1,"dilationW": 1,"padTop": 3,"padLeft": 3,"inH": "dim(shapes.X, 2)","inW": "dim(shapes.X, 3)","inPlane": "dim(shapes.X, 2) * dim(shapes.X, 3)","outH": "dim(shapes.Y, 2)","outW": "dim(shapes.Y, 3)","outW4": "ceilDiv(dim(shapes.Y, 3), 4)","count4": "dim(shapes.Y, 0) * dim(shapes.Y, 1) * dim(shapes.Y, 2) * ceilDiv(dim(shapes.Y, 3), 4)","scalarOutput": true}},"bindings": "horizontal","dispatch": {"threads": "dim(shapes.Y, 0) * dim(shapes.Y, 1) * dim(shapes.Y, 2) * ceilDiv(dim(shapes.Y, 3), 4)","workgroupSize": "tunables.WORKGROUP_SIZE"}}]1 unmodified line{"id": "nchw2d_w3s1_reuse","priority": 30,"demoteWhen": ["narrowF32W3UnrolledPreferred"],"when": ["poolBaseOk", "ranks.X == 4", "kernelW == 3", "strideW == 1", "dilationW == 1", "padLeft == 1", "padRight == 1", "kernelH >= 1", "kernelH <= 3", "strideH == 1", "dilationH >= 1", "attrs.count_include_pad == 0", "attrs.ceil_mode == 0", "dim(shapes.X, 3) % 4 == 0", "dim(shapes.Y, 2) == dim(shapes.X, 2) + padTop + padBottom - ((kernelH - 1) * dilationH + 1) + 1", "dim(shapes.Y, 3) == dim(shapes.X, 3)", "dim(shapes.Y, 2) > 0", "dim(shapes.Y, 3) > 0"],"passes": [{"id": "main","name": "AveragePool.Nchw2dW3s1Reuse","source": {"shader": "average-pool2d-nchw-w3s1-reuse.wgsl.jinja","inputs": {"kernelH": "kernelH","dilationH": "dilationH","padTop": "padTop","inH": "dim(shapes.X, 2)","inW": "dim(shapes.X, 3)","inPlane": "dim(shapes.X, 2) * dim(shapes.X, 3)","outH": "dim(shapes.Y, 2)","outW": "dim(shapes.Y, 3)","outWQuarter": "floor(dim(shapes.Y, 3) / 4)"}},"bindings": "w3Reuse","dispatch": { "threads": "numel(shapes.Y) / 4", "workgroupSize": "tunables.WORKGROUP_SIZE" }}]},{"id": "nchw2d_w3s1_reuse_tail","priority": 29,"when": ["poolBaseOk", "ranks.X == 4", "kernelW == 3", "strideW == 1", "dilationW == 1", "padLeft == 1", "padRight == 1", "kernelH >= 1", "kernelH <= 3", "strideH == 1", "dilationH >= 1", "attrs.count_include_pad == 0", "attrs.ceil_mode == 0", "dim(shapes.X, 3) % 4 != 0", "dim(shapes.Y, 2) == dim(shapes.X, 2) + padTop + padBottom - ((kernelH - 1) * dilationH + 1) + 1", "dim(shapes.Y, 3) == dim(shapes.X, 3)", "dim(shapes.Y, 2) > 0", "dim(shapes.Y, 3) > 0"],"constants": { "outputElement": "dtypes.T" },"passes": [{"id": "main","name": "AveragePool.Nchw2dW3s1ReuseTail","source": {"shader": "average-pool2d-nchw-horizontal-reuse.wgsl.jinja","inputs": {"kernelH": "kernelH","kernelW": 3,"strideH": 1,"strideW": 1,"dilationH": "dilationH","dilationW": 1,"padTop": "padTop","padLeft": 1,"inH": "dim(shapes.X, 2)","inW": "dim(shapes.X, 3)","inPlane": "dim(shapes.X, 2) * dim(shapes.X, 3)","outH": "dim(shapes.Y, 2)","outW": "dim(shapes.Y, 3)","outW4": "ceilDiv(dim(shapes.Y, 3), 4)","count4": "dim(shapes.Y, 0) * dim(shapes.Y, 1) * dim(shapes.Y, 2) * ceilDiv(dim(shapes.Y, 3), 4)","scalarOutput": true}},"bindings": "horizontal","dispatch": {"threads": "dim(shapes.Y, 0) * dim(shapes.Y, 1) * dim(shapes.Y, 2) * ceilDiv(dim(shapes.Y, 3), 4)","workgroupSize": "tunables.WORKGROUP_SIZE"}}]1 unmodified line{"id": "nchw2d_unrolled","priority": 25,"when": ["poolBaseOk", "ranks.X == 4", "dim(shapes.Y, 2) == explicitOutH", "dim(shapes.Y, 3) == explicitOutW", "kernelH >= 1", "kernelH <= 3", "kernelW >= 1", "kernelW <= 3", "strideH >= 1", "strideW >= 1", "dilationH >= 1", "dilationW >= 1", "dim(shapes.Y, 2) > 0", "dim(shapes.Y, 3) > 0"],"passes": [{"id": "main","name": "AveragePool.Nchw2dUnrolled","source": {"shader": "pool-window-unroll.wgsl.jinja","inputs": {"op": "\"average\"","kernelH": "kernelH","kernelW": "kernelW","strideH": "strideH","strideW": "strideW","dilationH": "dilationH","dilationW": "dilationW","padTop": "padTop","padLeft": "padLeft","inH": "dim(shapes.X, 2)","inW": "dim(shapes.X, 3)","outH": "dim(shapes.Y, 2)","outW": "dim(shapes.Y, 3)","padBottom": "padBottom","padRight": "padRight","countIncludePad": "attrs.count_include_pad != 0"}},"bindings": "flatDirect","dispatch": { "threads": "numel(shapes.Y)", "workgroupSize": "tunables.WORKGROUP_SIZE" }}]},{"id": "ncl1d","priority": 10,"when": ["poolBaseOk", "ranks.X == 3", "dim(shapes.Y, 2) == explicitOutW"],"passes": [{"id": "main","name": "AveragePool.Ncl1d","source": { "shader": "pool-window-nd.wgsl.jinja", "inputs": { "op": "\"average\"", "spatialRank": 1 } },"bindings": "rank1Direct","dispatch": { "threads": "numel(shapes.Y)", "workgroupSize": "tunables.WORKGROUP_SIZE" }}]},{"id": "nchw2d","when": ["poolBaseOk", "ranks.X == 4", "((dim(shapes.Y, 2) == explicitOutH and dim(shapes.Y, 3) == explicitOutW) or ((attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\") and dim(shapes.Y, 2) == ceil(dim(shapes.X, 2) / strideH) and dim(shapes.Y, 3) == ceil(dim(shapes.X, 3) / strideW)))"],"passes": [{"id": "main","name": "nchw2d","source": { "shader": "pool-window-nd.wgsl.jinja", "inputs": { "op": "\"average\"", "spatialRank": 2 } },"bindings": "rank2Direct","dispatch": { "threads": "numel(shapes.Y)", "workgroupSize": "tunables.WORKGROUP_SIZE" }}]},{"id": "ncdhw3d","priority": 10,"supersededBy": ["global_window_parallel"],"when": ["poolBaseOk", "ranks.X == 5", "dim(shapes.Y, 2) == explicitOutD", "dim(shapes.Y, 3) == explicitOutH", "dim(shapes.Y, 4) == explicitOutW"],"passes": [{"id": "main","name": "AveragePool.Ncdhw3d","source": { "shader": "pool-window-nd.wgsl.jinja", "inputs": { "op": "\"average\"", "spatialRank": 3 } },"bindings": "rank3Direct","dispatch": { "threads": "numel(shapes.Y)", "workgroupSize": "tunables.WORKGROUP_SIZE" }}]},{"id": "nchw2d_k7s2_reuse","priority": 36,"when": ["poolBaseOk", "ranks.X == 4", "kernelH == 7", "kernelW == 7", "strideH == 2", "strideW == 2", "dilationH == 1", "dilationW == 1", "padTop == 3", "padBottom == 3", "padLeft == 3", "padRight == 3", "attrs.count_include_pad == 0", "attrs.ceil_mode == 0", "dim(shapes.Y, 2) == explicitOutH", "dim(shapes.Y, 3) == explicitOutW", "dim(shapes.Y, 3) % 4 == 0", "dim(shapes.Y, 2) > 0", "dim(shapes.Y, 3) > 0"],"constants": { "outputElement": "\"vec4<\" ~ dtypes.T ~ \">\"", "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" },"passes": [{"id": "main","name": "AveragePool.Nchw2dK7s2Reuse","source": {"shader": "average-pool2d-nchw-horizontal-reuse.wgsl.jinja","inputs": {"kernelH": 7,"kernelW": 7,"strideH": 2,"strideW": 2,"dilationH": 1,"dilationW": 1,"padTop": 3,"padLeft": 3,"inH": "dim(shapes.X, 2)","inW": "dim(shapes.X, 3)","inPlane": "dim(shapes.X, 2) * dim(shapes.X, 3)","outH": "dim(shapes.Y, 2)","outW": "dim(shapes.Y, 3)","outW4": "dim(shapes.Y, 3) / 4","count4": "numel(shapes.Y) / 4"}},"bindings": "horizontal","dispatch": { "threads": "numel(shapes.Y) / 4", "workgroupSize": "tunables.WORKGROUP_SIZE" }}]},{"id": "nchw2d_k7s1_reuse","priority": 35,"when": ["poolBaseOk", "ranks.X == 4", "kernelH == 7", "kernelW == 7", "strideH == 1", "strideW == 1", "dilationH == 1", "dilationW == 1", "padTop == 3", "padBottom == 3", "padLeft == 3", "padRight == 3", "attrs.count_include_pad == 0", "attrs.ceil_mode == 0", "dim(shapes.X, 3) % 4 == 0", "dim(shapes.Y, 2) == dim(shapes.X, 2)", "dim(shapes.Y, 3) == dim(shapes.X, 3)", "dim(shapes.Y, 2) > 0", "dim(shapes.Y, 3) > 0"],"constants": { "outputElement": "\"vec4<\" ~ dtypes.T ~ \">\"", "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" },"passes": [{"id": "main","name": "AveragePool.Nchw2dK7s1Reuse","source": {"shader": "average-pool2d-nchw-horizontal-reuse.wgsl.jinja","inputs": {"kernelH": 7,"kernelW": 7,"strideH": 1,"strideW": 1,"dilationH": 1,"dilationW": 1,"padTop": 3,"padLeft": 3,"inH": "dim(shapes.X, 2)","inW": "dim(shapes.X, 3)","inPlane": "dim(shapes.X, 2) * dim(shapes.X, 3)","outH": "dim(shapes.Y, 2)","outW": "dim(shapes.Y, 3)","outW4": "dim(shapes.Y, 3) / 4","count4": "numel(shapes.Y) / 4"}},"bindings": "horizontal","dispatch": { "threads": "numel(shapes.Y) / 4", "workgroupSize": "tunables.WORKGROUP_SIZE" }}]}1 unmodified line2345678910111213141516172 unmodified lines20212223242526272814 unmodified lines4344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351 unmodified line1371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702711 unmodified line2732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155161 unmodified line"domain": "ai.onnx","name": "AveragePool","sinceVersion": 19,"inputs": { "x": { "onnx": "X", "dtype": "T" } },"outputs": { "y": { "onnx": "Y", "dtype": "T", "rank": "ranks.x", "shape": "poolOutputShape" } },"attributes": {"auto_pad": { "default": "NOTSET" },"count_include_pad": { "default": 0 },"ceil_mode": { "default": 0 },"kernel_shape": {},"strides": {},"pads": {},"dilations": {}},"attributeConstraints": {"auto_pad": { "values": ["NOTSET", "SAME_UPPER", "SAME_LOWER", "VALID"] },2 unmodified lines"kernel_shape": { "required": true }},"typeConstraints": { "T": ["float32", "float16"] },"tunables": { "WORKGROUP_SIZE": { "default": 256 }, "SEPARABLE_MIN_WINDOW": { "default": 25 } },"derive": {"spatialRank": "ranks.x - 2","narrowSubgroupRange": "has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize < device.adapterInfo.subgroupMaxSize and device.adapterInfo.subgroupMaxSize <= 16","kernelShapeLengthOk": "not has(attrs, \"kernel_shape\") or (attrs.kernel_shape | length) == spatialRank","stridesLengthOk": "not has(attrs, \"strides\") or (attrs.strides | length) == spatialRank",14 unmodified lines"padBack": "attrs.pads[spatialRank] if padsLengthOk and has(attrs, \"pads\") and spatialRank == 3 else 0","padBottom": "attrs.pads[2 * spatialRank - 2] if padsLengthOk and has(attrs, \"pads\") and spatialRank >= 2 else 0","padRight": "attrs.pads[2 * spatialRank - 1] if padsLengthOk and has(attrs, \"pads\") and spatialRank >= 1 else 0","poolBaseOk": "f16Ok(dtypes.T) and ranks.x >= 3 and ranks.x <= 5 and ranks.y == ranks.x and has(attrs, \"kernel_shape\") and kernelShapeLengthOk and stridesLengthOk and dilationsLengthOk and padsLengthOk and kernelD > 0 and kernelH > 0 and kernelW > 0 and strideD > 0 and strideH > 0 and strideW > 0 and dilationD > 0 and dilationH > 0 and dilationW > 0 and padFront >= 0 and padTop >= 0 and padLeft >= 0 and padBack >= 0 and padBottom >= 0 and padRight >= 0 and (attrs.auto_pad == \"NOTSET\" or not has(attrs, \"pads\")) and dim(shapes.y, 0) == dim(shapes.x, 0) and dim(shapes.y, 1) == dim(shapes.x, 1)","explicitOutW": "min(ceil((dim(shapes.x, ranks.x - 1) + padLeft + padRight - ((kernelW - 1) * dilationW + 1)) / strideW) + 1, ceil((dim(shapes.x, ranks.x - 1) + padLeft) / strideW)) if attrs.ceil_mode != 0 else floor((dim(shapes.x, ranks.x - 1) + padLeft + padRight - ((kernelW - 1) * dilationW + 1)) / strideW) + 1","explicitOutH": "min(ceil((dim(shapes.x, ranks.x - 2) + padTop + padBottom - ((kernelH - 1) * dilationH + 1)) / strideH) + 1, ceil((dim(shapes.x, ranks.x - 2) + padTop) / strideH)) if attrs.ceil_mode != 0 else floor((dim(shapes.x, ranks.x - 2) + padTop + padBottom - ((kernelH - 1) * dilationH + 1)) / strideH) + 1","explicitOutD": "(min(ceil((dim(shapes.x, 2) + padFront + padBack - ((kernelD - 1) * dilationD + 1)) / strideD) + 1, ceil((dim(shapes.x, 2) + padFront) / strideD)) if attrs.ceil_mode != 0 else floor((dim(shapes.x, 2) + padFront + padBack - ((kernelD - 1) * dilationD + 1)) / strideD) + 1) if ranks.x == 5 else 0","inferredOutW": "ceil(dim(shapes.x, ranks.x - 1) / strideW) if attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\" else explicitOutW","inferredOutH": "ceil(dim(shapes.x, ranks.x - 2) / strideH) if attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\" else explicitOutH","inferredOutD": "ceil(dim(shapes.x, 2) / strideD) if attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\" else explicitOutD","poolOutputShape": "[dim(shapes.x, 0), dim(shapes.x, 1), inferredOutW] if ranks.x == 3 else ([dim(shapes.x, 0), dim(shapes.x, 1), inferredOutH, inferredOutW] if ranks.x == 4 else [dim(shapes.x, 0), dim(shapes.x, 1), inferredOutD, inferredOutH, inferredOutW])","autoPadTotalD": "max(0, (dim(shapes.y, 2) - 1) * strideD + ((kernelD - 1) * dilationD + 1) - dim(shapes.x, 2)) if ranks.x == 5 else 0","autoPadTotalH": "max(0, (dim(shapes.y, ranks.x - 2) - 1) * strideH + ((kernelH - 1) * dilationH + 1) - dim(shapes.x, ranks.x - 2)) if ranks.x >= 4 else 0","autoPadTotalW": "max(0, (dim(shapes.y, ranks.x - 1) - 1) * strideW + ((kernelW - 1) * dilationW + 1) - dim(shapes.x, ranks.x - 1))","effectivePadFront": "(floor(autoPadTotalD / 2) if attrs.auto_pad == \"SAME_UPPER\" else autoPadTotalD - floor(autoPadTotalD / 2)) if (attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\") else padFront","effectivePadBack": "autoPadTotalD - effectivePadFront if (attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\") else padBack","effectivePadTop": "(floor(autoPadTotalH / 2) if attrs.auto_pad == \"SAME_UPPER\" else autoPadTotalH - floor(autoPadTotalH / 2)) if (attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\") else padTop","effectivePadLeft": "(floor(autoPadTotalW / 2) if attrs.auto_pad == \"SAME_UPPER\" else autoPadTotalW - floor(autoPadTotalW / 2)) if (attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\") else padLeft","effectivePadBottom": "autoPadTotalH - effectivePadTop if (attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\") else padBottom","effectivePadRight": "autoPadTotalW - effectivePadLeft if (attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\") else padRight","narrowF32W3UnrolledPreferred": "narrowSubgroupRange and dtypes.T == \"f32\"","globalWindowParallelCovered": "poolBaseOk and numel(shapes.y) == dim(shapes.x, 0) * dim(shapes.x, 1) and numel(shapes.y) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and attrs.auto_pad == \"NOTSET\" and strideD == 1 and strideH == 1 and strideW == 1 and dilationD == 1 and dilationH == 1 and dilationW == 1 and padFront == 0 and padTop == 0 and padLeft == 0 and padBack == 0 and padBottom == 0 and padRight == 0 and ((ranks.x == 3 and kernelW == dim(shapes.x, 2)) or (ranks.x == 4 and kernelH == dim(shapes.x, 2) and kernelW == dim(shapes.x, 3)) or (ranks.x == 5 and kernelD == dim(shapes.x, 2) and kernelH == dim(shapes.x, 3) and kernelW == dim(shapes.x, 4))) and inner(shapes.x, 1) >= 256","separableTmpElements": "dim(shapes.x, 0) * dim(shapes.x, 1) * dim(shapes.x, 2) * dim(shapes.y, 3)","separableFits": "separableTmpElements * 4 <= min(device.limits.maxStorageBufferBindingSize, device.limits.maxBufferSize) and ceilDiv(separableTmpElements, tunables.WORKGROUP_SIZE) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) * min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","wholeWindowDenominator": "attrs.ceil_mode == 0 and (attrs.count_include_pad == 1 or (effectivePadTop == 0 and effectivePadLeft == 0 and effectivePadBottom == 0 and effectivePadRight == 0))","scalar": "dtypes.T"},"bindings": { "y_2": { "name": "y", "buffer": "storage", "elementType": "$outputElement" } },"variants": [{"id": "global_window_parallel","priority": 50,"when": ["globalWindowParallelCovered"],"derive": {"xElem": "(\"vec4<f16>\" if dtypes.T == \"f16\" else \"vec4<f32>\") if inner(shapes.x, 1) % 4 == 0 else dtypes.T"},"passes": [{"id": "main","name": "AveragePool.GlobalWindowParallel","shader": "pool-global-reduction.wgsl.jinja","subgroupCollectivesWidth": "portable","derive": {"op": "\"average\"","vec4": "inner(shapes.x, 1) % 4 == 0","workgroupSizeSpec": "tunables.WORKGROUP_SIZE","useSubgroups": "device.features.has(\"subgroups\")"},"bindings": [{ "arg": "x", "elementType": "$xElem" },{ "arg": "y", "elementType": "$scalar" },{ "name": "params", "struct": [{ "name": "spatial", "type": "u32", "value": "inner(shapes.x, 1)" }] }],"dispatch": { "x": "numel(shapes.y)" }}]},{"id": "nchw2d_k7s1_reuse_tail","priority": 34,"when": ["poolBaseOk", "ranks.x == 4", "kernelH == 7", "kernelW == 7", "strideH == 1", "strideW == 1", "dilationH == 1", "dilationW == 1", "effectivePadTop == 3", "effectivePadBottom == 3", "effectivePadLeft == 3", "effectivePadRight == 3", "attrs.count_include_pad == 0", "attrs.ceil_mode == 0", "dim(shapes.x, 3) % 4 != 0", "dim(shapes.y, 2) == dim(shapes.x, 2)", "dim(shapes.y, 3) == dim(shapes.x, 3)", "dim(shapes.y, 2) > 0", "dim(shapes.y, 3) > 0"],"derive": { "outputElement": "dtypes.T" },"passes": [{"id": "main","name": "AveragePool.Nchw2dK7s1ReuseTail","shader": "average-pool2d-nchw-horizontal-reuse.wgsl.jinja","derive": {"kernelHSpec": 7,"kernelWSpec": 7,"strideHSpec": 1,"strideWSpec": 1,"dilationHSpec": 1,"dilationWSpec": 1,"padTopSpec": 3,"padLeftSpec": 3,"inH": "dim(shapes.x, 2)","inW": "dim(shapes.x, 3)","inPlane": "dim(shapes.x, 2) * dim(shapes.x, 3)","outH": "dim(shapes.y, 2)","outW": "dim(shapes.y, 3)","outW4": "ceilDiv(dim(shapes.y, 3), 4)","count4": "dim(shapes.y, 0) * dim(shapes.y, 1) * dim(shapes.y, 2) * ceilDiv(dim(shapes.y, 3), 4)","scalarOutput": true},"bindings": ["x", "y_2"],"dispatch": {"x": "min(ceilDiv((dim(shapes.y, 0) * dim(shapes.y, 1) * dim(shapes.y, 2) * ceilDiv(dim(shapes.y, 3), 4)), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((dim(shapes.y, 0) * dim(shapes.y, 1) * dim(shapes.y, 2) * ceilDiv(dim(shapes.y, 3), 4)), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]1 unmodified line{"id": "nchw2d_w3s1_reuse","priority": 30,"when": ["poolBaseOk", "ranks.x == 4", "kernelW == 3", "strideW == 1", "dilationW == 1", "effectivePadLeft == 1", "effectivePadRight == 1", "kernelH >= 1", "kernelH <= 3", "strideH == 1", "dilationH >= 1", "attrs.count_include_pad == 0", "attrs.ceil_mode == 0", "dim(shapes.x, 3) % 4 == 0", "dim(shapes.y, 2) == dim(shapes.x, 2) + effectivePadTop + effectivePadBottom - ((kernelH - 1) * dilationH + 1) + 1", "dim(shapes.y, 3) == dim(shapes.x, 3)", "dim(shapes.y, 2) > 0", "dim(shapes.y, 3) > 0"],"demoteWhen": ["narrowF32W3UnrolledPreferred"],"passes": [{"id": "main","name": "AveragePool.Nchw2dW3s1Reuse","shader": "average-pool2d-nchw-w3s1-reuse.wgsl.jinja","derive": {"kernelHSpec": "kernelH","dilationHSpec": "dilationH","padTopSpec": "effectivePadTop","inH": "dim(shapes.x, 2)","inW": "dim(shapes.x, 3)","inPlane": "dim(shapes.x, 2) * dim(shapes.x, 3)","outH": "dim(shapes.y, 2)","outW": "dim(shapes.y, 3)","outWQuarter": "floor(dim(shapes.y, 3) / 4)"},"bindings": ["x","y",{ "name": "params", "struct": [{ "name": "tileCount", "type": "u32", "value": "numel(shapes.y) / 4" }] }],"dispatch": {"x": "min(ceilDiv((numel(shapes.y) / 4), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((numel(shapes.y) / 4), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]},{"id": "nchw2d_w3s1_reuse_tail","priority": 29,"when": ["poolBaseOk", "ranks.x == 4", "kernelW == 3", "strideW == 1", "dilationW == 1", "effectivePadLeft == 1", "effectivePadRight == 1", "kernelH >= 1", "kernelH <= 3", "strideH == 1", "dilationH >= 1", "attrs.count_include_pad == 0", "attrs.ceil_mode == 0", "dim(shapes.x, 3) % 4 != 0", "dim(shapes.y, 2) == dim(shapes.x, 2) + effectivePadTop + effectivePadBottom - ((kernelH - 1) * dilationH + 1) + 1", "dim(shapes.y, 3) == dim(shapes.x, 3)", "dim(shapes.y, 2) > 0", "dim(shapes.y, 3) > 0"],"derive": { "outputElement": "dtypes.T" },"passes": [{"id": "main","name": "AveragePool.Nchw2dW3s1ReuseTail","shader": "average-pool2d-nchw-horizontal-reuse.wgsl.jinja","derive": {"kernelHSpec": "kernelH","kernelWSpec": 3,"strideHSpec": 1,"strideWSpec": 1,"dilationHSpec": "dilationH","dilationWSpec": 1,"padTopSpec": "effectivePadTop","padLeftSpec": 1,"inH": "dim(shapes.x, 2)","inW": "dim(shapes.x, 3)","inPlane": "dim(shapes.x, 2) * dim(shapes.x, 3)","outH": "dim(shapes.y, 2)","outW": "dim(shapes.y, 3)","outW4": "ceilDiv(dim(shapes.y, 3), 4)","count4": "dim(shapes.y, 0) * dim(shapes.y, 1) * dim(shapes.y, 2) * ceilDiv(dim(shapes.y, 3), 4)","scalarOutput": true},"bindings": ["x", "y_2"],"dispatch": {"x": "min(ceilDiv((dim(shapes.y, 0) * dim(shapes.y, 1) * dim(shapes.y, 2) * ceilDiv(dim(shapes.y, 3), 4)), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((dim(shapes.y, 0) * dim(shapes.y, 1) * dim(shapes.y, 2) * ceilDiv(dim(shapes.y, 3), 4)), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]},{"id": "nchw2d_separable","priority": 22,"when": ["poolBaseOk", "ranks.x == 4", "ranks.y == 4", "dim(shapes.y, 2) == inferredOutH", "dim(shapes.y, 3) == inferredOutW", "dim(shapes.y, 2) > 0", "dim(shapes.y, 3) > 0", "kernelH > 1", "kernelW > 1", "kernelH * kernelW >= tunables.SEPARABLE_MIN_WINDOW", "strideH < kernelH", "wholeWindowDenominator", "separableFits"],"derive": { "scalar": "dtypes.T", "pIs1": false, "pIs2": false },"intermediates": [{ "id": "tmp", "dtype": "float32", "shape": "[separableTmpElements]" }],"passes": [{"id": "reduce_w","name": "AveragePool.SeparableW","shader": "pool2d-nchw-separable.wgsl.jinja","derive": { "axisSpec": "\"w\"", "op": "\"avg\"", "windowArea": "kernelH * kernelW" },"bindings": ["x",{ "name": "tmp", "buffer": "storage", "elementType": "f32" },{"name": "params","struct": [{ "name": "inH", "type": "u32", "value": "dim(shapes.x, 2)" },{ "name": "inW", "type": "u32", "value": "dim(shapes.x, 3)" },{ "name": "outW", "type": "u32", "value": "dim(shapes.y, 3)" },{ "name": "kernelW", "type": "u32", "value": "kernelW" },{ "name": "strideW", "type": "u32", "value": "strideW" },{ "name": "dilationW", "type": "u32", "value": "dilationW" },{ "name": "padW", "type": "i32", "value": "effectivePadLeft" },{ "name": "tmpCount", "type": "u32", "value": "separableTmpElements" }]}],"dispatch": {"x": "min(ceilDiv((separableTmpElements), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((separableTmpElements), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}},{"id": "reduce_h","name": "AveragePool.SeparableH","shader": "pool2d-nchw-separable.wgsl.jinja","derive": { "axisSpec": "\"h\"", "op": "\"avg\"", "windowArea": "kernelH * kernelW" },"bindings": [{ "name": "tmp", "buffer": "read-only-storage", "elementType": "f32" },"y",{"name": "params","struct": [{ "name": "inH", "type": "u32", "value": "dim(shapes.x, 2)" },{ "name": "outH", "type": "u32", "value": "dim(shapes.y, 2)" },{ "name": "outW", "type": "u32", "value": "dim(shapes.y, 3)" },{ "name": "kernelH", "type": "u32", "value": "kernelH" },{ "name": "strideH", "type": "u32", "value": "strideH" },{ "name": "dilationH", "type": "u32", "value": "dilationH" },{ "name": "padH", "type": "i32", "value": "effectivePadTop" },{ "name": "count", "type": "u32", "value": "numel(shapes.y)" }]}],"dispatch": {"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]1 unmodified line{"id": "nchw2d_unrolled","priority": 25,"when": ["poolBaseOk", "ranks.x == 4", "dim(shapes.y, 2) == inferredOutH", "dim(shapes.y, 3) == inferredOutW", "kernelH >= 1", "kernelH <= 3", "kernelW >= 1", "kernelW <= 3", "strideH >= 1", "strideW >= 1", "dilationH >= 1", "dilationW >= 1", "dim(shapes.y, 2) > 0", "dim(shapes.y, 3) > 0"],"passes": [{"id": "main","name": "AveragePool.Nchw2dUnrolled","shader": "pool-window-unroll.wgsl.jinja","derive": {"op": "\"average\"","kernelHSpec": "kernelH","kernelWSpec": "kernelW","strideHSpec": "strideH","strideWSpec": "strideW","dilationHSpec": "dilationH","dilationWSpec": "dilationW","padTopSpec": "effectivePadTop","padLeftSpec": "effectivePadLeft","inH": "dim(shapes.x, 2)","inW": "dim(shapes.x, 3)","outH": "dim(shapes.y, 2)","outW": "dim(shapes.y, 3)","padBottomSpec": "effectivePadBottom","padRightSpec": "effectivePadRight","countIncludePad": "attrs.count_include_pad != 0"},"bindings": ["x","y",{ "name": "params", "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.y)" }] }],"dispatch": {"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]},{"id": "ncl1d","priority": 10,"when": ["poolBaseOk", "ranks.x == 3", "dim(shapes.y, 2) == inferredOutW"],"passes": [{"id": "main","name": "AveragePool.Ncl1d","shader": "pool-window-nd.wgsl.jinja","derive": { "op": "\"average\"", "spatialRankSpec": 1 },"bindings": ["x","y",{"name": "params","struct": [{ "name": "channels", "type": "u32", "value": "dim(shapes.x, 1)" },{ "name": "inW", "type": "u32", "value": "dim(shapes.x, 2)" },{ "name": "outW", "type": "u32", "value": "dim(shapes.y, 2)" },{ "name": "kernelW", "type": "u32", "value": "kernelW" },{ "name": "strideW", "type": "u32", "value": "strideW" },{ "name": "dilationW", "type": "u32", "value": "dilationW" },{ "name": "padLeft", "type": "i32", "value": "effectivePadLeft" },{ "name": "padRight", "type": "i32", "value": "effectivePadRight" },{ "name": "countIncludePad", "type": "u32", "value": "attrs.count_include_pad" },{ "name": "count", "type": "u32", "value": "numel(shapes.y)" }]}],"dispatch": {"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]},{"id": "nchw2d","when": ["poolBaseOk", "ranks.x == 4", "((dim(shapes.y, 2) == explicitOutH and dim(shapes.y, 3) == explicitOutW) or ((attrs.auto_pad == \"SAME_UPPER\" or attrs.auto_pad == \"SAME_LOWER\") and dim(shapes.y, 2) == ceil(dim(shapes.x, 2) / strideH) and dim(shapes.y, 3) == ceil(dim(shapes.x, 3) / strideW)))"],"passes": [{"id": "main","name": "nchw2d","shader": "pool-window-nd.wgsl.jinja","derive": { "op": "\"average\"", "spatialRankSpec": 2 },"bindings": ["x","y",{"name": "params","struct": [{ "name": "channels", "type": "u32", "value": "dim(shapes.x, 1)" },{ "name": "inH", "type": "u32", "value": "dim(shapes.x, 2)" },{ "name": "inW", "type": "u32", "value": "dim(shapes.x, 3)" },{ "name": "outH", "type": "u32", "value": "dim(shapes.y, 2)" },{ "name": "outW", "type": "u32", "value": "dim(shapes.y, 3)" },{ "name": "kernelH", "type": "u32", "value": "kernelH" },{ "name": "kernelW", "type": "u32", "value": "kernelW" },{ "name": "strideH", "type": "u32", "value": "strideH" },{ "name": "strideW", "type": "u32", "value": "strideW" },{ "name": "dilationH", "type": "u32", "value": "dilationH" },{ "name": "dilationW", "type": "u32", "value": "dilationW" },{ "name": "padTop", "type": "i32", "value": "effectivePadTop" },{ "name": "padLeft", "type": "i32", "value": "effectivePadLeft" },{ "name": "padBottom", "type": "i32", "value": "effectivePadBottom" },{ "name": "padRight", "type": "i32", "value": "effectivePadRight" },{ "name": "countIncludePad", "type": "u32", "value": "attrs.count_include_pad" },{ "name": "count", "type": "u32", "value": "numel(shapes.y)" }]}],"dispatch": {"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]},{"id": "ncdhw3d","priority": 10,"when": ["poolBaseOk", "ranks.x == 5", "dim(shapes.y, 2) == inferredOutD", "dim(shapes.y, 3) == inferredOutH", "dim(shapes.y, 4) == inferredOutW"],"supersededBy": ["global_window_parallel"],"passes": [{"id": "main","name": "AveragePool.Ncdhw3d","shader": "pool-window-nd.wgsl.jinja","derive": { "op": "\"average\"", "spatialRankSpec": 3 },"bindings": ["x","y",{"name": "params","struct": [{ "name": "channels", "type": "u32", "value": "dim(shapes.x, 1)" },{ "name": "inD", "type": "u32", "value": "dim(shapes.x, 2)" },{ "name": "inH", "type": "u32", "value": "dim(shapes.x, 3)" },{ "name": "inW", "type": "u32", "value": "dim(shapes.x, 4)" },{ "name": "outD", "type": "u32", "value": "dim(shapes.y, 2)" },{ "name": "outH", "type": "u32", "value": "dim(shapes.y, 3)" },{ "name": "outW", "type": "u32", "value": "dim(shapes.y, 4)" },{ "name": "kernelD", "type": "u32", "value": "kernelD" },{ "name": "kernelH", "type": "u32", "value": "kernelH" },{ "name": "kernelW", "type": "u32", "value": "kernelW" },{ "name": "strideD", "type": "u32", "value": "strideD" },{ "name": "strideH", "type": "u32", "value": "strideH" },{ "name": "strideW", "type": "u32", "value": "strideW" },{ "name": "dilationD", "type": "u32", "value": "dilationD" },{ "name": "dilationH", "type": "u32", "value": "dilationH" },{ "name": "dilationW", "type": "u32", "value": "dilationW" },{ "name": "padFront", "type": "i32", "value": "effectivePadFront" },{ "name": "padTop", "type": "i32", "value": "effectivePadTop" },{ "name": "padLeft", "type": "i32", "value": "effectivePadLeft" },{ "name": "padBack", "type": "i32", "value": "effectivePadBack" },{ "name": "padBottom", "type": "i32", "value": "effectivePadBottom" },{ "name": "padRight", "type": "i32", "value": "effectivePadRight" },{ "name": "countIncludePad", "type": "u32", "value": "attrs.count_include_pad" },{ "name": "count", "type": "u32", "value": "numel(shapes.y)" }]}],"dispatch": {"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]},{"id": "nchw2d_k7s2_reuse","priority": 36,"when": ["poolBaseOk", "ranks.x == 4", "kernelH == 7", "kernelW == 7", "strideH == 2", "strideW == 2", "dilationH == 1", "dilationW == 1", "effectivePadTop == 3", "effectivePadBottom == 3", "effectivePadLeft == 3", "effectivePadRight == 3", "attrs.count_include_pad == 0", "attrs.ceil_mode == 0", "dim(shapes.y, 2) == inferredOutH", "dim(shapes.y, 3) == inferredOutW", "dim(shapes.y, 3) % 4 == 0", "dim(shapes.y, 2) > 0", "dim(shapes.y, 3) > 0"],"derive": { "outputElement": "\"vec4<\" ~ dtypes.T ~ \">\"", "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" },"passes": [{"id": "main","name": "AveragePool.Nchw2dK7s2Reuse","shader": "average-pool2d-nchw-horizontal-reuse.wgsl.jinja","derive": {"kernelHSpec": 7,"kernelWSpec": 7,"strideHSpec": 2,"strideWSpec": 2,"dilationHSpec": 1,"dilationWSpec": 1,"padTopSpec": 3,"padLeftSpec": 3,"inH": "dim(shapes.x, 2)","inW": "dim(shapes.x, 3)","inPlane": "dim(shapes.x, 2) * dim(shapes.x, 3)","outH": "dim(shapes.y, 2)","outW": "dim(shapes.y, 3)","outW4": "dim(shapes.y, 3) / 4","count4": "numel(shapes.y) / 4"},"bindings": ["x", "y_2"],"dispatch": {"x": "min(ceilDiv((numel(shapes.y) / 4), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((numel(shapes.y) / 4), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]},{"id": "nchw2d_k7s1_reuse","priority": 35,"when": ["poolBaseOk", "ranks.x == 4", "kernelH == 7", "kernelW == 7", "strideH == 1", "strideW == 1", "dilationH == 1", "dilationW == 1", "effectivePadTop == 3", "effectivePadBottom == 3", "effectivePadLeft == 3", "effectivePadRight == 3", "attrs.count_include_pad == 0", "attrs.ceil_mode == 0", "dim(shapes.x, 3) % 4 == 0", "dim(shapes.y, 2) == dim(shapes.x, 2)", "dim(shapes.y, 3) == dim(shapes.x, 3)", "dim(shapes.y, 2) > 0", "dim(shapes.y, 3) > 0"],"derive": { "outputElement": "\"vec4<\" ~ dtypes.T ~ \">\"", "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" },"passes": [{"id": "main","name": "AveragePool.Nchw2dK7s1Reuse","shader": "average-pool2d-nchw-horizontal-reuse.wgsl.jinja","derive": {"kernelHSpec": 7,"kernelWSpec": 7,"strideHSpec": 1,"strideWSpec": 1,"dilationHSpec": 1,"dilationWSpec": 1,"padTopSpec": 3,"padLeftSpec": 3,"inH": "dim(shapes.x, 2)","inW": "dim(shapes.x, 3)","inPlane": "dim(shapes.x, 2) * dim(shapes.x, 3)","outH": "dim(shapes.y, 2)","outW": "dim(shapes.y, 3)","outW4": "dim(shapes.y, 3) / 4","count4": "numel(shapes.y) / 4"},"bindings": ["x", "y_2"],"dispatch": {"x": "min(ceilDiv((numel(shapes.y) / 4), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((numel(shapes.y) / 4), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]}
12345678910111213141516171819202122{"name": "ai.onnx.AveragePool","id": "_ai_onnx_averagepool_webgpu_208ddea","version": 1,"license": "Apache-2.0","backend": { "type": "webgpu" },"digest": {"algorithm": "sha256","files": {"average-pool2d-nchw-horizontal-reuse.wgsl.jinja": "MuniH46O1hxMO9h44Pke8TPe9LdxPJxSfkWzpzH7ORA=","average-pool2d-nchw-w3s1-reuse.wgsl.jinja": "zPPcxTvNNFow8AZiF6f6aA528J72OfqiLD4r8DTNVVs=","bench.json": "YSXPXL9DWp91TDTmMZrcq81UYOnXi2L5BKFjPk+8MEU=","manifest.json": "6q/856PdHb80O+6DlOVhpkyox/A+DIvIcgNaiZBCN6E=","pool-global-reduction.wgsl.jinja": "kFAmHLC4tjXVW1gTRXNZHVeLo9o4BdkmCVhTIyB9doA=","pool-window-nd.wgsl.jinja": "WlKw9CY8+OR7l8jGO2lyJ72P0twJi3CIvechFVjC7jA=","pool-window-unroll.wgsl.jinja": "65NfjRJtzKQBVh23bYGyuwr7KUKKvdWsHnsBnGHSw7k=","test.json": "voBL9bHxxb4Sgoc7TC6W8uyXVsC/wEElhUg3BklfbL8="}},"provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } },"webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/ai.onnx.AveragePool" }}1234567891011121314151617181920212223242526272829303132333435363738{"name": "ai.onnx.AveragePool","id": "_ai_onnx_averagepool_webgpu_1a52fa9","version": 1,"license": "Apache-2.0","backend": { "type": "webgpu" },"digest": {"algorithm": "sha256","files": {"average-pool2d-nchw-horizontal-reuse.wgsl.jinja": "WhpEal2kC3XudqPetAZeL6v/IGK1olc+Fb4HzzDj890=","average-pool2d-nchw-w3s1-reuse.wgsl.jinja": "IsI2gMcZbY0eemYs5z0pTC86cgaDw605Xfn2iQDBJkw=","bench.json": "7vXmd7ZA5xBCZjqXJYtMzO7vq/0qgzf5gNZ1S6J5Zps=","manifest.json": "a0sXaOaq8613yIAtQBB1R+xrbvA48WTKgkPo14ruXhA=","pool-global-reduction.wgsl.jinja": "SKnTSUq9nU838U0t7VyvC7hgz4h5L+Ndv1rQCM9Vks8=","pool-window-nd.wgsl.jinja": "2shuAamdoFjQCOq2LK2XC6pKsa10zad6bDhqY1h2zBI=","pool-window-unroll.wgsl.jinja": "iSbZ3lQkW4r/CZzsBitQBpHgBLRJ83MekQxyVqbInwI=","pool2d-nchw-separable.wgsl.jinja": "GgafQWV6Pn5SqSn5gMns0EJEHGqk2WkdCdVurt3MgA8=","test.json": "2Xnd9YqkiREFwBJdjyqFIbqnT2KGPNdXYpCezSLDXkA="}},"provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } },"webgpu": {"manifestSpec": "2.0","variants": {"global_window_parallel": ["pool-global-reduction.wgsl.jinja"],"nchw2d_k7s1_reuse_tail": ["average-pool2d-nchw-horizontal-reuse.wgsl.jinja"],"nchw2d_w3s1_reuse": ["average-pool2d-nchw-w3s1-reuse.wgsl.jinja"],"nchw2d_w3s1_reuse_tail": ["average-pool2d-nchw-horizontal-reuse.wgsl.jinja"],"nchw2d_separable": ["pool2d-nchw-separable.wgsl.jinja"],"nchw2d_unrolled": ["pool-window-unroll.wgsl.jinja"],"ncl1d": ["pool-window-nd.wgsl.jinja"],"nchw2d": ["pool-window-nd.wgsl.jinja"],"ncdhw3d": ["pool-window-nd.wgsl.jinja"],"nchw2d_k7s2_reuse": ["average-pool2d-nchw-horizontal-reuse.wgsl.jinja"],"nchw2d_k7s1_reuse": ["average-pool2d-nchw-horizontal-reuse.wgsl.jinja"]}}}
12345671 unmodified line9101112131415161718192021222324252 unmodified lines282930313233343536373839404142434445464748495010 unmodified lines616263646566676869707172737475767778{% if usesF16 %}enable f16;{% endif %}{% if source.useSubgroups %}enable subgroups;{% endif %}{{ env.wgsl.resourceDeclarations }}1 unmodified line// Workgroup-parallel global pooling: one workgroup reduces one contiguous// (n, c) spatial plane (NC[D]HW layout makes the plane contiguous).// Threads stride the plane (vec4 loads when the plane size is divisible by 4),// then combine the per-thread partials. With subgroups: a subgroup collective// + a shared-memory fold of the per-subgroup partials. Without subgroups, a// full workgroup tree keeps workgroup-per-plane parallelism// instead of falling to the one-thread-per-plane serial scalar kernel. The// epilogue finalizes average / max / Lp. Sum reassociates;// max is exact.const WG: u32 = {{ source.workgroupSize }}u;{% if source.useSubgroups %}// WebGPU subgroups are at least 4 wide, so a workgroup holds at most WG / 4 subgroups.const MAX_SUBGROUPS: u32 = WG / 4u;var<workgroup> wg_partials: array<f32, MAX_SUBGROUPS>;{% else %}var<workgroup> wg_tree: array<f32, WG>;{% endif %}2 unmodified lines@compute @workgroup_size(WG)fn main(@builtin(workgroup_id) wid: vec3<u32>,@builtin(local_invocation_id) lid: vec3<u32>{%- if source.useSubgroups %},@builtin(subgroup_size) sg_size: u32{%- endif %}) {let plane = wid.x;let tid = lid.x;{% if source.useSubgroups %}// Seed cross-subgroup slots with the reduction identity so unwritten slots// (when the workgroup has fewer than MAX_SUBGROUPS subgroups) are harmless.if (tid < MAX_SUBGROUPS) {wg_partials[tid] = 0.0;}workgroupBarrier();{% endif %}var partial = 0.0;{% if source.vec4 %}let spatialVec = params.spatial / 4u;let base = plane * spatialVec;var accv = vec4<f32>(0.0);10 unmodified lines}{% endif %}{% if source.useSubgroups %}let sg_sum = subgroupAdd(partial);if (subgroupElect()) {wg_partials[tid / sg_size] = sg_sum;}workgroupBarrier();if (tid == 0u) {let nsg = min((WG + sg_size - 1u) / sg_size, MAX_SUBGROUPS);var total = 0.0;for (var i = 0u; i < nsg; i = i + 1u) {total = total + wg_partials[i];}y[plane] = {{ scalar }}(total / f32(params.spatial));}{% else %}12341 unmodified line678910111213141516171819202122232 unmodified lines2627282930313233343536373810 unmodified lines49505152535455565758596061626364656667686970717273747576777879{% if useSubgroups %}enable subgroups;{% endif %}{{ env.wgsl.resourceDeclarations }}1 unmodified line// Workgroup-parallel global pooling: one workgroup reduces one contiguous// (n, c) spatial plane (NC[D]HW layout makes the plane contiguous).// Threads stride the plane (vec4 loads when the plane size is divisible by 4),// then combine the per-thread partials. With subgroups: a subgroup collective,// then one shared-memory slot per invocation (the subgroup partial from its// elected lane, the identity from every other lane) that every subgroup folds.// Without subgroups, a full workgroup tree combines the per-thread partials.// The epilogue divides the f32 sum by the spatial extent. The tree changes the// association of additions.const WG: u32 = {{ workgroupSizeSpec }}u;{% if useSubgroups %}// One slot per invocation: the cross-subgroup fold below indexes by// local_invocation_id, never by a subgroup ordinal, so no subgroup width or// partition can overflow or alias it.var<workgroup> wg_partials: array<f32, WG>;{% else %}var<workgroup> wg_tree: array<f32, WG>;{% endif %}2 unmodified lines@compute @workgroup_size(WG)fn main(@builtin(workgroup_id) wid: vec3<u32>,@builtin(local_invocation_id) lid: vec3<u32>{%- if useSubgroups %},@builtin(subgroup_size) sg_size: u32{%- endif %}) {let plane = wid.x;let tid = lid.x;var partial = 0.0;{% if vec4 %}let spatialVec = params.spatial / 4u;let base = plane * spatialVec;var accv = vec4<f32>(0.0);10 unmodified lines}{% endif %}{% if useSubgroups %}var total = subgroupAdd(partial);// Cross-subgroup fold that assumes nothing about which invocations share a// subgroup, how many subgroups there are, or which of a subgroup's lanes are// active: every invocation owns the slot at its own index, the elected lane// publishes its subgroup partial there and every other lane publishes the// reduction identity. Each subgroup then folds all WG slots — lane `rank`,// its dense position among the active lanes, walks slots rank, rank + count,// ... — and one more collective merges the lane partials, so every slot is// merged exactly once at any legal width and partition. A one-subgroup// workgroup skips shared memory entirely; that test reads the `subgroup_size`// builtin, which is uniform, because a collective's result is not uniform to// WGSL's analysis and may not guard a barrier.if (sg_size != WG) {let rank = subgroupExclusiveAdd(1u);let count = subgroupAdd(1u);wg_partials[tid] = select(0.0, total, rank == 0u);workgroupBarrier();var acc = 0.0;for (var i = rank; i < WG; i = i + count) {acc = acc + wg_partials[i];}total = subgroupAdd(acc);}if (tid == 0u) {y[plane] = {{ scalar }}(total / f32(params.spatial));}{% else %}
123456789101112131415161718192021222324252627282930313233343536373839404142431 unmodified line4546474849505152531 unmodified line555657585960616263646566676869707172737475763 unmodified lines80818283848586878889{% if usesF16 %}enable f16;{% endif %}{{ env.wgsl.resourceDeclarations }}// Direct {{ source.spatialRank }}D {{ source.op }}-pool traversal for a channels-first tensor.@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {let index = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u;if (index >= params.count) { return; }let ow = index % params.outW;var t = index / params.outW;{% if source.spatialRank >= 2 %}let oh = t % params.outH;t /= params.outH;{% endif %}{% if source.spatialRank == 3 %}let od = t % params.outD;t /= params.outD;{% endif %}let channel = t % params.channels;let batch = t / params.channels;{% if source.spatialRank == 3 %}let startD = i32(od * params.strideD) - params.padFront;{% endif %}{% if source.spatialRank >= 2 %}let startH = i32(oh * params.strideH) - params.padTop;{% endif %}let startW = i32(ow * params.strideW) - params.padLeft;var acc = 0.0;var seen = 0u;{% if source.spatialRank == 3 %}for (var kd = 0u; kd < params.kernelD; kd += 1u) {let id = startD + i32(kd * params.dilationD);if (id < 0 || id >= i32(params.inD)) { continue; }{% endif %}{% if source.spatialRank >= 2 %}for (var kh = 0u; kh < params.kernelH; kh += 1u) {let ih = startH + i32(kh * params.dilationH);if (ih < 0 || ih >= i32(params.inH)) { continue; }1 unmodified linefor (var kw = 0u; kw < params.kernelW; kw += 1u) {let iw = startW + i32(kw * params.dilationW);if (iw < 0 || iw >= i32(params.inW)) { continue; }{% if source.spatialRank == 1 %}let xIndex = (batch * params.channels + channel) * params.inW + u32(iw);{% elif source.spatialRank == 2 %}let xIndex = ((batch * params.channels + channel) * params.inH + u32(ih)) * params.inW + u32(iw);{% else %}let xIndex = (((batch * params.channels + channel) * params.inD + u32(id)) * params.inH + u32(ih)) * params.inW + u32(iw);1 unmodified lineacc += f32(x[xIndex]);seen += 1u;}{% if source.spatialRank >= 2 %}}{% endif %}{% if source.spatialRank == 3 %}}{% endif %}var denom = seen;if (params.countIncludePad != 0u) {denom = 0u;{% if source.spatialRank == 3 %}for (var kd = 0u; kd < params.kernelD; kd += 1u) {let id = startD + i32(kd * params.dilationD);if (id < -params.padFront || id >= i32(params.inD) + params.padBack) { continue; }{% endif %}{% if source.spatialRank >= 2 %}for (var kh = 0u; kh < params.kernelH; kh += 1u) {let ih = startH + i32(kh * params.dilationH);if (ih < -params.padTop || ih >= i32(params.inH) + params.padBottom) { continue; }3 unmodified linesif (iw < -params.padLeft || iw >= i32(params.inW) + params.padRight) { continue; }denom += 1u;}{% if source.spatialRank >= 2 %}}{% endif %}{% if source.spatialRank == 3 %}}{% endif %}}123456789101112131415161718192021222324252627282930313233343536373839401 unmodified line4243444546474849501 unmodified line525354555657585960616263646566676869707172733 unmodified lines77787980818283848586{{ env.wgsl.resourceDeclarations }}// Direct spatial-pooling traversal for a channels-first tensor.@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})fn main(@builtin(global_invocation_id) gid: vec3<u32>) {let index = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u;if (index >= params.count) { return; }let ow = index % params.outW;var t = index / params.outW;{% if spatialRankSpec >= 2 %}let oh = t % params.outH;t /= params.outH;{% endif %}{% if spatialRankSpec == 3 %}let od = t % params.outD;t /= params.outD;{% endif %}let channel = t % params.channels;let batch = t / params.channels;{% if spatialRankSpec == 3 %}let startD = i32(od * params.strideD) - params.padFront;{% endif %}{% if spatialRankSpec >= 2 %}let startH = i32(oh * params.strideH) - params.padTop;{% endif %}let startW = i32(ow * params.strideW) - params.padLeft;var acc = 0.0;var seen = 0u;{% if spatialRankSpec == 3 %}for (var kd = 0u; kd < params.kernelD; kd += 1u) {let id = startD + i32(kd * params.dilationD);if (id < 0 || id >= i32(params.inD)) { continue; }{% endif %}{% if spatialRankSpec >= 2 %}for (var kh = 0u; kh < params.kernelH; kh += 1u) {let ih = startH + i32(kh * params.dilationH);if (ih < 0 || ih >= i32(params.inH)) { continue; }1 unmodified linefor (var kw = 0u; kw < params.kernelW; kw += 1u) {let iw = startW + i32(kw * params.dilationW);if (iw < 0 || iw >= i32(params.inW)) { continue; }{% if spatialRankSpec == 1 %}let xIndex = (batch * params.channels + channel) * params.inW + u32(iw);{% elif spatialRankSpec == 2 %}let xIndex = ((batch * params.channels + channel) * params.inH + u32(ih)) * params.inW + u32(iw);{% else %}let xIndex = (((batch * params.channels + channel) * params.inD + u32(id)) * params.inH + u32(ih)) * params.inW + u32(iw);1 unmodified lineacc += f32(x[xIndex]);seen += 1u;}{% if spatialRankSpec >= 2 %}}{% endif %}{% if spatialRankSpec == 3 %}}{% endif %}var denom = seen;if (params.countIncludePad != 0u) {denom = 0u;{% if spatialRankSpec == 3 %}for (var kd = 0u; kd < params.kernelD; kd += 1u) {let id = startD + i32(kd * params.dilationD);if (id < -params.padFront || id >= i32(params.inD) + params.padBack) { continue; }{% endif %}{% if spatialRankSpec >= 2 %}for (var kh = 0u; kh < params.kernelH; kh += 1u) {let ih = startH + i32(kh * params.dilationH);if (ih < -params.padTop || ih >= i32(params.inH) + params.padBottom) { continue; }3 unmodified linesif (iw < -params.padLeft || iw >= i32(params.inW) + params.padRight) { continue; }denom += 1u;}{% if spatialRankSpec >= 2 %}}{% endif %}{% if spatialRankSpec == 3 %}}{% endif %}}
12345678910111213141516171819202122232425262728293031325 unmodified lines383940414243444546474849505152535455563 unmodified lines6061626364656667686970// Compile-time-unrolled 2D NCHW pooling for small windows. Accumulation remains// kh-major/kw-minor, matching the runtime-loop fallbacks.{% if usesF16 %}enable f16;{% endif %}{{ env.wgsl.resourceDeclarations }}const OUT_W: u32 = {{ source.outW }}u;const OUT_H: u32 = {{ source.outH }}u;const IN_H: i32 = {{ source.inH }};const IN_W: i32 = {{ source.inW }};const IN_W_U: u32 = {{ source.inW }}u;const IN_PLANE: u32 = {{ source.inH * source.inW }}u;const STRIDE_H: u32 = {{ source.strideH }}u;const STRIDE_W: u32 = {{ source.strideW }}u;const PAD_TOP: i32 = {{ source.padTop }};const PAD_LEFT: i32 = {{ source.padLeft }};{% if source.op == "average" and source.countIncludePad %}const PAD_BOTTOM: i32 = {{ source.padBottom }};const PAD_RIGHT: i32 = {{ source.padRight }};{% endif %}@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})fn main(@builtin(global_invocation_id) gid: vec3<u32>,@builtin(num_workgroups) nwg: vec3<u32>) {// Fold very large flat outputs into the dispatch's second dimension.let index = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u;if (index >= params.count) {return;}5 unmodified lineslet planeBase = plane * IN_PLANE;let startH = i32(oh * STRIDE_H) - PAD_TOP;let startW = i32(ow * STRIDE_W) - PAD_LEFT;{% for kh in range(source.kernelH) %}let ih_{{ kh }} = startH + {{ kh * source.dilationH }};{% endfor %}{% for kw in range(source.kernelW) %}let iw_{{ kw }} = startW + {{ kw * source.dilationW }};{% endfor %}var result = 0.0;var seen = 0u;{% for kh in range(source.kernelH) %}if (ih_{{ kh }} >= 0 && ih_{{ kh }} < IN_H) {let rowBase{{ kh }} = planeBase + u32(ih_{{ kh }}) * IN_W_U;{% for kw in range(source.kernelW) %}if (iw_{{ kw }} >= 0 && iw_{{ kw }} < IN_W) {let value_{{ kh }}_{{ kw }} = f32(x[rowBase{{ kh }} + u32(iw_{{ kw }})]);result += value_{{ kh }}_{{ kw }};3 unmodified lines}{% endfor %}{% if source.op == "average" and source.countIncludePad %}var denominator = 0u;{% for kh in range(source.kernelH) %}if (ih_{{ kh }} >= -PAD_TOP && ih_{{ kh }} < IN_H + PAD_BOTTOM) {{% for kw in range(source.kernelW) %}if (iw_{{ kw }} >= -PAD_LEFT && iw_{{ kw }} < IN_W + PAD_RIGHT) {denominator += 1u;}123456789101112131415161718192021222324252627282930315 unmodified lines373839404142434445464748495051525354553 unmodified lines5960616263646566676869// Compile-time-unrolled 2D NCHW pooling for small windows. Accumulation remains// kh-major/kw-minor, matching the loop-based implementations.{% set countIncludePad = countIncludePad is defined and countIncludePad %}{% set padBottomSpec = padBottomSpec | default(0) %}{% set padRightSpec = padRightSpec | default(0) %}{{ env.wgsl.resourceDeclarations }}const OUT_W: u32 = {{ outW }}u;const OUT_H: u32 = {{ outH }}u;const IN_H: i32 = {{ inH }};const IN_W: i32 = {{ inW }};const IN_W_U: u32 = {{ inW }}u;const IN_PLANE: u32 = {{ inH * inW }}u;const STRIDE_H: u32 = {{ strideHSpec }}u;const STRIDE_W: u32 = {{ strideWSpec }}u;const PAD_TOP: i32 = {{ padTopSpec }};const PAD_LEFT: i32 = {{ padLeftSpec }};{% if op == "average" and countIncludePad %}const PAD_BOTTOM: i32 = {{ padBottomSpec }};const PAD_RIGHT: i32 = {{ padRightSpec }};{% endif %}@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})fn main(@builtin(global_invocation_id) gid: vec3<u32>) {// Fold very large flat outputs into the dispatch's second dimension.let index = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u;if (index >= params.count) {return;}5 unmodified lineslet planeBase = plane * IN_PLANE;let startH = i32(oh * STRIDE_H) - PAD_TOP;let startW = i32(ow * STRIDE_W) - PAD_LEFT;{% for kh in range(kernelHSpec) %}let ih_{{ kh }} = startH + {{ kh * dilationHSpec }};{% endfor %}{% for kw in range(kernelWSpec) %}let iw_{{ kw }} = startW + {{ kw * dilationWSpec }};{% endfor %}var result = 0.0;var seen = 0u;{% for kh in range(kernelHSpec) %}if (ih_{{ kh }} >= 0 && ih_{{ kh }} < IN_H) {let rowBase{{ kh }} = planeBase + u32(ih_{{ kh }}) * IN_W_U;{% for kw in range(kernelWSpec) %}if (iw_{{ kw }} >= 0 && iw_{{ kw }} < IN_W) {let value_{{ kh }}_{{ kw }} = f32(x[rowBase{{ kh }} + u32(iw_{{ kw }})]);result += value_{{ kh }}_{{ kw }};3 unmodified lines}{% endfor %}{% if op == "average" and countIncludePad %}var denominator = 0u;{% for kh in range(kernelHSpec) %}if (ih_{{ kh }} >= -PAD_TOP && ih_{{ kh }} < IN_H + PAD_BOTTOM) {{% for kw in range(kernelWSpec) %}if (iw_{{ kw }} >= -PAD_LEFT && iw_{{ kw }} < IN_W + PAD_RIGHT) {denominator += 1u;}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748{{ env.wgsl.resourceDeclarations }}
// Separable NCHW pooling: one spatial axis per pass.//// A kernelH x kernelW window factorizes into a width reduction followed by a// height reduction. The width pass writes one f32 intermediate per input row// and output column; the height pass completes the selected pooling operation.// `axis == "w"` writes `tmp`, and `axis == "h"` writes `y`.// Both passes sum in f32. This specialization requires the whole window as the// denominator, whose baked area is applied once after the height fold.
@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})fn main(@builtin(global_invocation_id) gid: vec3<u32>) { let index = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u;{% if axisSpec == "w" %} if (index >= params.tmpCount) { return; } // One cell per (batch, channel, input row, output column). let ow = index % params.outW; let t = index / params.outW; let ih = t % params.inH; let nc = t / params.inH;
var acc = 0.0; for (var kw = 0u; kw < params.kernelW; kw += 1u) { let iw = i32(ow * params.strideW + kw * params.dilationW) - params.padW; if (iw < 0 || iw >= i32(params.inW)) { continue; } let value = f32(x[(nc * params.inH + ih) * params.inW + u32(iw)]); acc += value; } tmp[index] = acc;{% else %} if (index >= params.count) { return; } let ow = index % params.outW; let t = index / params.outW; let oh = t % params.outH; let nc = t / params.outH;
var acc = 0.0; for (var kh = 0u; kh < params.kernelH; kh += 1u) { let ih = i32(oh * params.strideH + kh * params.dilationH) - params.padH; if (ih < 0 || ih >= i32(params.inH)) { continue; } let partial = tmp[(nc * params.inH + u32(ih)) * params.outW + ow]; acc += partial; }
y[index] = {{ scalar }}(acc / {{ windowArea }}.0);{% endif %}}README.md+15-10
build/webgpu/average-pool2d-nchw-horizontal-reuse.wgsl.jinja+21-24
build/webgpu/average-pool2d-nchw-w3s1-reuse.wgsl.jinja+12-15
build/webgpu/bench.json+33-3
build/webgpu/manifest.json+366-339
build/webgpu/metadata.json+27-11
build/webgpu/pool-global-reduction.wgsl.jinja+38-37
build/webgpu/pool-window-nd.wgsl.jinja+17-20
build/webgpu/pool-window-unroll.wgsl.jinja+28-29
build/webgpu/pool2d-nchw-separable.wgsl.jinjaAdded+48
build/webgpu/test.json