File size: 18,657 Bytes
0ef25e5
6d3894e
0ef25e5
6d3894e
 
 
 
0ef25e5
6d3894e
 
 
 
 
 
9a9da83
6d3894e
 
 
 
 
9008499
6d3894e
9008499
 
9a9da83
9008499
6d3894e
 
 
9008499
6d3894e
9008499
6d3894e
 
 
 
 
 
 
9a9da83
21ba9b4
9008499
6d3894e
9008499
6d3894e
 
 
 
 
 
 
 
 
 
9008499
 
 
 
9a9da83
 
 
 
 
 
 
 
 
 
 
 
 
 
9008499
9a9da83
9008499
9a9da83
 
 
 
 
 
 
 
 
 
 
9008499
6d3894e
 
9a9da83
6d3894e
 
 
9a9da83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d3894e
 
 
9008499
9a9da83
9008499
 
9a9da83
 
9008499
6d3894e
 
9008499
6d3894e
 
 
 
 
9a9da83
 
 
 
6d3894e
9a9da83
 
6d3894e
9a9da83
6d3894e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
---
library_name: kernels
license: apache-2.0
tags:
- kernel
- webgpu
- wgsl
---
# com.microsoft.FusedConv

`com.microsoft`  ·  ONNX Runtime contrib operator  ·  contrib since_version 1

## Description

Applies an N-dimensional convolution with optional bias `B` and residual `Z`, followed by an optional fused activation. Omitting `activation` leaves the convolution result unchanged. Supported activations are `Relu`, `LeakyRelu`, `Sigmoid`, `Tanh`, `HardSigmoid`, `HardSwish`, `Clip`, `QuickGelu`, `Elu`, `Gelu`, `FastGelu`, `Softplus`, `ThresholdedRelu`, and `Erf`; other schema-permitted activation strings are not implemented. The implementation supports one to three spatial dimensions and float16 or float32; higher spatial ranks and float64 are not implemented.

See the [ONNX Runtime `FusedConv` contrib-operator spec](https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#com.microsoft.FusedConv) for the reference semantics.

## Inputs

| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
| --- | --- | --- | --- | --- | --- | --- |
| `x` | `X` | `T` | — | — | Input data tensor of shape `(N, C, D1, ..., Dn)` for one to three spatial dimensions. | required |
| `w` | `W` | `T` | — | — | Convolution filter tensor of shape `(M, C/group, k1, ..., kn)`, with the same spatial rank as `X`. | required |
| `b` | `B` | `T` | `1` | — | Optional 1-D bias tensor of length `out_channels`, broadcast-added to each output channel. | optional |
| `zResidual` | `Z` | `T` | same as `x` | — | Optional residual tensor with the same shape as the output `Y`, added before the activation. | optional |

## Outputs

| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
| --- | --- | --- | --- | --- | --- | --- |
| `y` | `Y` | `T` | same as `x` | derived | Output feature map tensor after convolution, optional bias/residual addition, and the fused activation. | required |

## Attributes

Attributes and default values (overridable per request):

| Attribute | Default | Description |
| --- | --- | --- |
| `activation` | — | Optional fused activation name: `Relu`, `LeakyRelu`, `Sigmoid`, `Tanh`, `HardSigmoid`, `HardSwish`, `Clip`, `QuickGelu` (x * sigmoid(alpha * x); alpha 1 is SiLU), `Elu`, `Gelu` (erf form, or the tanh approximation when `activation_params[0]` is nonzero), `FastGelu` (always the tanh approximation), `Softplus`, `ThresholdedRelu`, or `Erf`. Omission applies no activation. The last seven extend the activations that onnxruntime's CPU provider accepts. |
| `activation_params` | — | Positional parameters for the fused activation: exactly `[alpha]` is required for `LeakyRelu`, and exactly `[alpha, beta]` or `[min, max]` is required for `HardSigmoid` or `Clip`, respectively. `QuickGelu`, `Elu` and `ThresholdedRelu` take an optional `[alpha]` (defaults 1.702, 1.0 and 1.0); `Gelu` takes an optional `[approximate]` flag (0 = erf, nonzero = tanh). Parameter-free activations ignore this attribute. |
| `auto_pad` | `"NOTSET"` | Automatic padding mode. `NOTSET` uses `pads`; `SAME_UPPER` and `SAME_LOWER` choose padding so each output spatial size is `ceil(input / stride)`; `VALID` uses no padding. |
| `dilations` | — | Optional dilation factors, one positive integer per spatial axis. Omission means all ones. |
| `group` | `1` | Number of groups that input and output channels are split into; defaults to 1. |
| `kernel_shape` | — | Optional kernel shape, one positive integer per spatial axis. When present, it must match the spatial dimensions of the weight tensor; omission infers the shape from the weights. |
| `pads` | — | Optional explicit padding in ONNX order `[begin_axis_0, ..., begin_axis_n, end_axis_0, ..., end_axis_n]`. Omission means all zeros; it cannot be combined with an automatic padding mode. |
| `strides` | — | Optional stride factors, one positive integer per spatial axis. Omission means all ones. |

## 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.

- `native16_packed_pointwise_64x64`, `native16_packed_pointwise_128x128`, `native16_packed_pointwise_64x128` — Views an unpadded single-batch pointwise convolution as contiguous half matrix operands, with float32 accumulation and a fused float32 epilogue before half output.
- `im2col_native16_packed_64x64` — Materializes single-batch half convolution columns, then multiplies the weights by them as packed half matrix operands, with float32 accumulation and a fused float32 epilogue before half output.
- `im2col_native16_packed_64x64_exceptional` — Materializes single-batch half convolution columns, then multiplies the weights by them as packed half matrix operands, with float32 accumulation and a fused float32 epilogue before half output. Weight rows that contain Inf or NaN are flagged, and a final pass recomputes those rows in float32 from the original operands.
- `im2col_native16_packed_128x128` — Materializes single-batch half convolution columns, then multiplies the weights by them as packed half matrix operands, with float32 accumulation and a fused float32 epilogue before half output.
- `im2col_native16_packed_128x128_exceptional` — Materializes single-batch half convolution columns, then multiplies the weights by them as packed half matrix operands, with float32 accumulation and a fused float32 epilogue before half output. Weight rows that contain Inf or NaN are flagged, and a final pass recomputes those rows in float32 from the original operands.
- `im2col_native16_packed_vector4_64x64` — Materializes single-batch half convolution columns, then multiplies the weights by them as packed half matrix operands, with float32 accumulation and a fused float32 epilogue before half output.
- `im2col_native16_packed_vector4_64x64_exceptional` — Materializes single-batch half convolution columns, then multiplies the weights by them as packed half matrix operands, with float32 accumulation and a fused float32 epilogue before half output. Weight rows that contain Inf or NaN are flagged, and a final pass recomputes those rows in float32 from the original operands.
- `im2col_native16_packed_vector4_128x128` — Materializes single-batch half convolution columns, then multiplies the weights by them as packed half matrix operands, with float32 accumulation and a fused float32 epilogue before half output.
- `im2col_native16_packed_vector4_128x128_exceptional` — Materializes single-batch half convolution columns, then multiplies the weights by them as packed half matrix operands, with float32 accumulation and a fused float32 epilogue before half output. Weight rows that contain Inf or NaN are flagged, and a final pass recomputes those rows in float32 from the original operands.
- `winograd_f32` — Uses F(2x2,3x3) transforms, component matrix products and accumulation in float32 for half-precision tensors. Stores all transform components in a temporary buffer of bounded size. Tiles whose transformed values contain Inf or NaN use the direct spatial sum instead.
- `winograd_f32_striped` — Uses F(2x2,3x3) transforms, component matrix products and accumulation in float32 for half-precision tensors. Reuses one temporary buffer across four component rows to bound memory. Tiles whose transformed values contain Inf or NaN use the direct spatial sum instead.
- `implicit_im2col_tiled_reg_splitk`, `implicit_im2col_tiled_reg_splitk_preferred` — Partitions a long implicit-im2col reduction across workgroups, then sums the partials and applies bias and activation once; this changes the floating-point addition order. On compact spatial grids the partials fit in the bytes an im2col buffer would use. `_preferred` is the same kernel, tried first for f32 with a small output grid and a deep reduction.
- `implicit_im2col_tiled_reg` — Gathers logical im2col elements directly from `X` into register-blocked GEMM tiles and applies the optional bias and the fused activation in the epilogue, without a temporary column buffer.
- `gemm_1x1_subgroup_matrix` — Multiplies 1x1 weights directly by NCHW input, then applies the optional bias, residual and activation epilogue. Complete output-channel tiles bypass input staging and reduction-loop barriers; partial tiles retain guarded staging. Tile geometry follows the device's workgroup limits.
- `im2col_gemm_subgroup_matrix` — Multiply aligned materialized columns through workgroup-staged subgroup matrices with f32 accumulation and the fused epilogue.
- `im2col_gemm_subgroup_matrix_f16` — Multiply aligned materialized columns through workgroup-staged subgroup matrices with f32 accumulation and the fused epilogue. Weight rows that contain Inf or NaN are flagged, and a final pass recomputes those rows in float32 from the original operands.
- `im2col_direct_inputs_subgroup_matrix` — Multiply aligned materialized f32 columns through direct subgroup-matrix loads, retaining the f32 accumulation order and fused epilogue while omitting operand staging and K-loop barriers.
- `im2col_half_direct_subgroup_matrix` — Materialize aligned f16 convolution columns, then load weights and columns directly into subgroup matrices without operand staging or K-loop barriers. Accumulate in f32, apply the optional bias, residual and activation, and round the output to f16.
- `im2col_half_direct_subgroup_matrix_f16` — Materialize aligned f16 convolution columns, then load weights and columns directly into subgroup matrices without operand staging or K-loop barriers. Accumulate in f32, apply the optional bias, residual and activation, and round the output to f16. Weight rows that contain Inf or NaN are flagged, and a final pass recomputes those rows in float32.
- `im2col_gemm_tiled_reg` — Materialize f32 columns and run a register-tiled GEMM with f32 accumulation. Add the optional bias before activation.
- `im2col_gemm_tiled_reg_f16_columns` — Materialize f16 inputs directly into f16 columns, then run the register-tiled GEMM with f32 accumulation. This preserves the input values exactly and halves column-buffer traffic and storage; it is used when the half-precision column buffer fits WebGPU buffer limits. Add the optional bias before activation.
- `conv1d_residual` — Portable channels-first one-dimensional convolution with residual addition before the optional activation. Supports grouped filters and positive kernel widths beyond those of the unrolled variant; operands accumulate in float32 and narrow only at the output store.
- `grouped_large_kernel_w4`, `grouped_large_kernel_w4_dilated_lanes` — Shares input windows across channels and columns with f32 accumulation. Filter area, channel bytes and activation presence choose row looping or unrolling. Bias seeds the accumulators; activation follows the reduction. Small workloads, and large unrolled kernels on adapters whose subgroup size is not fixed at 32, still run here but only after other variants.
- `implicit_im2col_subgroup_matrix` — Gathers convolution input tiles directly into subgroup-matrix operands and applies the bias, residual, and activation epilogue, avoiding a materialized column matrix when output-channel reuse permits or both column layouts exceed device allocation limits.
- `subgroup_matrix_16x16x16` — Experimental `16x16x16` half-precision subgroup matrices with `f32` accumulation. Bias, residual add and activation are applied in `f32` before the output cast. The matrix path runs only when the workgroup's subgroups are complete and of the expected width and the weights are finite; otherwise the kernel computes every output element without them.
- `implicit_im2col_tiled_reg_wide_gather` — Gathers ragged convolution rows directly into a device-bounded register tile, then applies the fused epilogue without materialized columns.
- `gemm_1x1_tiled_reg_micro` — Gives each lane fewer output rows, with more lanes per workgroup, for pointwise convolution while retaining the same shared tiles and fused epilogue.
- `nhwc_native16_64x64`, `nhwc_native16_128x128`, `nhwc_native16_128x64` — Transposes half activations to channel-last order and packs half weights, then runs an implicit-GEMM half convolution on 16x16x16 subgroup matrices with float32 accumulation. The weight-packing pass flags weight rows that contain Inf or NaN; a final pass recomputes those rows in float32 from the original operands, skipping padded taps.

## Device requirements

Some implementation variants require `subgroup-matrix`, `shader-f16`, and `subgroups`. Other variants run without them; which variant runs also depends on the request's shapes and dtypes.

## Files

- [`metadata.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/metadata.json) — kernel metadata (id, file digests, manifest spec, provenance)
- [`manifest.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/manifest.json) — the op contract (source of truth)
- [`test.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/test.json) — correctness cases
- [`bench.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/bench.json) — benchmark cases
- [`conv-1x1-gemm-tiled-reg.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-1x1-gemm-tiled-reg.wgsl.jinja)
- [`conv-1x1-gemm-tiled.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-1x1-gemm-tiled.wgsl.jinja)
- [`conv-1x1-subgroup-matrix.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-1x1-subgroup-matrix.wgsl.jinja)
- [`conv-direct-nd.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-direct-nd.wgsl.jinja)
- [`conv-direct-unrolled.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-direct-unrolled.wgsl.jinja)
- [`conv-im2col-nchw.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-im2col-nchw.wgsl.jinja)
- [`conv-portable-exceptional-repair.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-portable-exceptional-repair.wgsl.jinja)
- [`conv-splitk-reduce.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-splitk-reduce.wgsl.jinja)
- [`conv-winograd-gemm.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-winograd-gemm.wgsl.jinja)
- [`conv-winograd-input.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-winograd-input.wgsl.jinja)
- [`conv-winograd-merge.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-winograd-merge.wgsl.jinja)
- [`conv-winograd-output.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-winograd-output.wgsl.jinja)
- [`conv-winograd-weight.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv-winograd-weight.wgsl.jinja)
- [`conv1d-tiled-reg.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv1d-tiled-reg.wgsl.jinja)
- [`conv2d-grouped-large-w4.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/conv2d-grouped-large-w4.wgsl.jinja)
- [`datamove-transpose-2d-tiled.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/datamove-transpose-2d-tiled.wgsl.jinja)
- [`matrix-shaped-projection-gemm.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/matrix-shaped-projection-gemm.wgsl.jinja)
- [`nhwc-native16-packed.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/nhwc-native16-packed.wgsl.jinja)
- [`nhwc-weight-pack.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/nhwc-weight-pack.wgsl.jinja)
- [`pointwise-native16-packed.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/pointwise-native16-packed.wgsl.jinja)
- [`spatial-exceptional-repair.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/spatial-exceptional-repair.wgsl.jinja)
- [`spatial-im2col-nf-producer.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/spatial-im2col-nf-producer.wgsl.jinja)
- [`spatial-materialized-generic-repair.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/spatial-materialized-generic-repair.wgsl.jinja)
- [`spatial-materialized-subgroup-matrix-repair.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/spatial-materialized-subgroup-matrix-repair.wgsl.jinja)
- [`spatial-weight-row-flags.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedConv/blob/v1/build/webgpu/spatial-weight-row-flags.wgsl.jinja)

## Use with `@huggingface/kernels`

```sh
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`.

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

const kernel = await getKernel("webgpu-kernels/com.microsoft.FusedConv", { version: 1 });

const xData = Float32Array.of(65536);
const wData = Float32Array.of(1);

const { y } = await kernel({
  x: { data: xData, shape: [1, 1, 1] },
  w: { data: wData, shape: [1, 1, 1] },
});
console.log({ y });
```