Skip to content

[WebGPU] Add a subgroup-size-16 MatMul/Gemm kernel - #34074

Open
xhcao wants to merge 1 commit into
microsoft:mainfrom
xhcao:webgpu-intel-subgroup-matmul
Open

xhcao wants to merge 1 commit into
microsoft:mainfrom
xhcao:webgpu-intel-subgroup-matmul

Conversation

@xhcao

@xhcao xhcao commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

The subgroup MatMul kernel runs with subgroup size 32 and 8 rows per invocation, which spills registers. Pointwise Conv, which reaches the kernel, became 8-15x slower than the packed MatMul.

Add a second subgroup kernel, used only on architectures where it was measured to be faster. Other GPUs keep the default kernel, subgroup size selection and selection rules.

The subgroup-16 kernel:

  • Uses a 16x8 workgroup (128 invocations) where each row is one subgroup of 16 lanes, a 64-column output tile and a 16-deep K tile.
  • Double-buffers the B tile in workgroup memory and prefetches the next tile into registers, so each K tile needs one barrier.
  • Loads A cooperatively as vec4 values along K when K % 4 == 0, with one vec4 subgroupBroadcast per row serving four K steps, addressed by subgroup_invocation_id.
  • Picks rows per invocation from the output size: the most rows (up to 8) that still launch 320 workgroups, at least 4 with f32 accumulators and 2 with f16, and at most 4 for f32 accumulators with scalar A loads.
  • Leaves shapes that use split-K to the packed path, for MatMul and Gemm.
  • Serves Gemm without transposes, including alpha and empty inputs.

UseMatMulSubgroup16Kernel() selects the kernel; on those architectures subgroup size 16 is preferred. The planner, its plan validation, MatMul and Gemm all derive the kernel, tile sizes and rows per invocation from the same facts.

On an Arc A770 versus the packed MatMul, over 420 shapes each (M 64-4096, N 512-5120, K 64-4096): MatMul fp32 1.54x / fp16 1.77x geomean where the subgroup kernel is selected, Gemm fp32 1.49x / fp16 1.74x, no shape below 0.95x. The pointwise Conv from #33856 (384x2560x1280 fp32) goes from 7043 us to 343 us (packed: 543 us).

Fixes #33856.

Description

Motivation and Context

The subgroup MatMul kernel runs with subgroup size 32 and 8 rows per
invocation, which spills registers. Pointwise Conv, which reaches the
kernel, became 8-15x slower than the packed MatMul.

Add a second subgroup kernel, used only on architectures where it was
measured to be faster. Other GPUs keep the default kernel, subgroup
size selection and selection rules.

The subgroup-16 kernel:
- Uses a 16x8 workgroup (128 invocations) where each row is one subgroup of 16
  lanes, a 64-column output tile and a 16-deep K tile.
- Double-buffers the B tile in workgroup memory and prefetches the next tile
  into registers, so each K tile needs one barrier.
- Loads A cooperatively as vec4 values along K when K % 4 == 0, with one vec4
  subgroupBroadcast per row serving four K steps, addressed by
  subgroup_invocation_id.
- Picks rows per invocation from the output size: the most rows (up to 8)
  that still launch 320 workgroups, at least 4 with f32 accumulators and 2
  with f16, and at most 4 for f32 accumulators with scalar A loads.
- Leaves shapes that use split-K to the packed path, for MatMul and Gemm.
- Serves Gemm without transposes, including alpha and empty inputs.

UseMatMulSubgroup16Kernel() selects the kernel; on those architectures
subgroup size 16 is preferred. The planner, its plan validation, MatMul and
Gemm all derive the kernel, tile sizes and rows per invocation from the same
facts.

On an Arc A770 versus the packed MatMul, over 420 shapes each (M 64-4096,
N 512-5120, K 64-4096): MatMul fp32 1.54x / fp16 1.77x geomean where the
subgroup kernel is selected, Gemm fp32 1.49x / fp16 1.74x, no shape below
0.95x. The pointwise Conv from microsoft#33856 (384x2560x1280 fp32) goes from 7043 us
to 343 us (packed: 543 us).

Fixes microsoft#33856.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 10, 2026 08:14
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The new architecture-specific routing, adaptive planning, and subgroup-16 kernel paths lack regression coverage for key fallback, boundary, unaligned, and batched cases.

No open issues 🎉

What changed in this PR

Adds an Intel Alchemist–specific subgroup-size-16 MatMul/Gemm kernel to address the WebGPU pointwise Conv slowdown reported in #33856, while retaining the default kernel on other architectures.

Changes:

  • Introduces a 64-column kernel with double-buffered B tiles, cooperative A loads, and adaptive rows per invocation.
  • Integrates architecture-aware selection into MatMul and Gemm, preserving the packed path for split-K shapes.
  • Aligns execution-plan validation with the new kernel’s tile sizes and accumulation settings.
File Description
onnxruntime/​core/​providers/​webgpu/​vendor/​intel/​math/​matmul.h Adds the subgroup-16 program flag.
onnxruntime/​core/​providers/​webgpu/​vendor/​intel/​math/​matmul.cc Selects and dispatches the architecture-specific kernel.
onnxruntime/​core/​providers/​webgpu/​vendor/​intel/​math/​matmul_execution_planner.cc Preserves split-K selection for eligible shapes.
onnxruntime/​core/​providers/​webgpu/​vendor/​intel/​math/​gemm.h Adds subgroup-16 configuration to Gemm.
onnxruntime/​core/​providers/​webgpu/​vendor/​intel/​math/​gemm.cc Integrates subgroup-16 shader generation and dispatch.
onnxruntime/​core/​providers/​webgpu/​vendor/​intel/​math/​gemm_subgroup16.h Declares the new kernel interface and workgroup dimensions.
onnxruntime/​core/​providers/​webgpu/​vendor/​intel/​math/​gemm_subgroup16.cc Implements the shader, architecture gate, and row tuning.
onnxruntime/​core/​providers/​webgpu/​vendor/​intel/​math/​gemm_subgroup.h Updates the eligibility-check context signature.
onnxruntime/​core/​providers/​webgpu/​vendor/​intel/​math/​gemm_subgroup.cc Adds split-K-aware Gemm eligibility checks.
onnxruntime/​core/​providers/​webgpu/​vendor/​intel/​math/​gemm_subgroup_utils.h Declares architecture-aware selection and tuning helpers.
onnxruntime/​core/​providers/​webgpu/​math/​matmul.cc Passes accumulation precision into planning.
onnxruntime/​core/​providers/​webgpu/​math/​matmul_execution_planner.h Extends selection parameters and exposes the split-K predicate to derived planners.
onnxruntime/​core/​providers/​webgpu/​math/​matmul_execution_planner.cc Validates dispatch using the selected kernel’s tile geometry.

Review effort: Balanced

@xhcao

xhcao commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Jie Chen (@jchen10) PTAL

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants