com.microsoft.FusedGemm

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

Description

Gemm with a fused activation: Y = act(alpha * A' * B' + beta * C), where A' and B' are optionally transposed and C is broadcastable to (M, N). The activation runs in the f32 accumulator before the single output narrowing. This package supports Relu, LeakyRelu, Sigmoid, Tanh and HardSigmoid; the other activation strings and numeric types allowed by the open schema are not implemented. Omitting activation gives plain Gemm.

See the ONNX Runtime FusedGemm contrib-operator spec for the reference semantics.

Differences from the ONNX Runtime spec

  • Attribute activation_gamma: The supported activation subset does not include Selu, the only upstream fused activation that consumes gamma.

Inputs

Name Logical dtype Rank Shape Description Presence
A T 2 — Left operand, (M, K) when transA is 0 and (K, M) otherwise. required
B T 2 — Right operand, (K, N) when transB is 0 and (N, K) otherwise. required
C T — — Optional additive term, unidirectionally broadcastable to (M, N): a scalar, a row (N), a column (M, 1), or the full matrix. optional

Outputs

Name Logical dtype Rank Shape Description Presence
Y T 2 derived act(alpha * A' * B' + beta * C) with shape (M, N). required

Attributes

Attributes and default values (overridable per request):

Attribute Default Description
activation — Optional fused activation name. Supported modes are Relu, LeakyRelu, Sigmoid, Tanh and HardSigmoid; omission applies none.
activation_alpha — First activation parameter: the slope for LeakyRelu or alpha for HardSigmoid.
activation_beta — Second activation parameter: beta for HardSigmoid.
alpha 1 Scalar multiplier for the product A' * B'; the standard default is 1.
beta 1 Scalar multiplier for C; the standard default is 1.
transA 0 Whether A is stored transposed. The standard default is 0.
transB 0 Whether B is stored transposed. The standard default is 0.

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_64x64, native16_packed_128x128, native16_packed_64x128 — Uses packed half operands and padded shared tiles with f32 accumulation. Product scaling, the optional broadcast bias and the optional activation execute before the single half output conversion.
  • gemv — Reduces a single output row in f32 and applies scaling, broadcast bias and activation before narrowing. Transposed weights stream contiguous rows. K-by-N f16 weights share input values across adjacent columns. Wide unaligned rows mask the final output group.
  • sgmat — Uses supported subgroup matrices with f32 accumulation and a fused epilogue. Complete non-transposed f32 A tiles load directly from storage; long reductions on that path merge two-tile sums with f32 compensation. Staged A clamps row tails. Workgroup memory is checked against device limits.
  • vec4_tiled — Float32 128x64 or 64x64 register tiles with four-wide operand loads for every transpose combination; a transposed operand is staged through a 4x4 register transpose. Alpha, the optional broadcast beta-scaled bias and the activation run on the f32 result before the store.
  • vec4_splitk — Cuts K of the float32 four-wide register tile into power-of-two slices across dispatch.z when its output tiles alone cannot fill the device. Each slice writes alpha-scaled f32 partials; a second pass sums them in slice order, adds the optional broadcast beta-scaled bias and applies the activation before the single store.
  • subgroup_matrix_16x16x16, subgroup_matrix_16x16x16_m_bias — Experimental 16x16x16 half-precision subgroup matrices with f32 accumulation. The alpha scale, any beta-scaled bias and the fused 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; otherwise the kernel computes every output element without them.

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

Use with @huggingface/kernels

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

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

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

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

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

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

const AData = Float32Array.from({ length: 7168 }, (_, i) => Math.sin((i + 1) * 0.017) * 0.2 + Math.cos((i + 3) * 0.037) * (0.2 * 0.67));
const BData = Float32Array.from({ length: 14336 }, (_, i) => Math.sin((i + 1) * 0.029) * 0.2 + Math.cos((i + 3) * 0.043) * (0.2 * 0.67));

const { Y } = await kernel({
  A: { data: AData, shape: [32, 224] },
  B: { data: BData, shape: [224, 64] },
});
console.log({ Y });
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.