[ExecuTorch][WebGPU] Register-tile the q4gsw quantized-linear kernel#20490
Closed
JulianCloudNTH wants to merge 1 commit into
Closed
[ExecuTorch][WebGPU] Register-tile the q4gsw quantized-linear kernel#20490JulianCloudNTH wants to merge 1 commit into
JulianCloudNTH wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/20490
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):
Register-tile the
et_vk.linear_q4gswGEMM — up to 3.4x faster prefill (M4 Pro, M=128).Problem:
et_vk.linear_q4gsw(4-bit weight-only, W4A16) computesout[m,n] = bias[n] + sum_k input[m,k] * (nibble(weight,n,k)-8) * scale[k/group_size, n]in a single dispatch over a raw[N, K/2]4-bit weight (2 nibbles/byte, +8-shifted symmetric, groupwise scales). The shipped kernel was naive: one workgroup per output rowm, threads stridingN, a scalar K-loop per(m,n). For an M-row (prefill) input it re-extracts every dequantized weightMtimes (once per row) and re-reads each input value once per output column — redundant memory traffic that dominates the prefill GEMM.Solution: a register-tiled GEMM where each thread owns a
TM x TN = 4x4output tile, so both weights and inputs are loaded once per tile instead of once per element.(n,k)dequantized once per(m,n)(extractedMx for prefill);input[m,k]re-read once per output columnn.(n,k)dequantized ONCE and reused across theTMrows of the tile (weight reads drop ~TMx); eachinput[m,k]loaded once perkinto a register and reused across theTNcolumns (input reads drop ~TNx).Implementation:
q4gsw_linear.wgsl: perk, hoist theTMinput values into registers, then for each of theTNcolumns dequantize the weight once and accumulate into the4x4register tile.Mworkgroups toceil(M/TM)*ceil(N/TN)tiles overwg_sizethreads;wg_sizeis computed before the count so the dispatch is still validated against device limits before any allocation.n0+nl >= Norm0+ml >= M) clamp their weight/scale/input index to the last valid element (the never-stored overhang is harmless), since WGSL out-of-bounds reads are implementation-defined. Mirrors the Vulkan tiled GEMMq4gsw_linear_gemm__w_4x8.glsl'smin(..., N-1)clamp.4x4tile (vs Vulkan4M x 8N) for a conservative register budget; the RAW[N,K/2]layout with scalar nibble unpack and NOW_4X8prepack / NO widevec4<u32>loads (prior on-device measurement found wide loads regress on this GPU); a 1D-flattened tile index (the backend is 1D-dispatch only).Constraints: bindings,
Params, the weight layout, and the single-dispatch structure are unchanged; the dequant index math is copied verbatim from the naive kernel, so the result is a floating-point accumulation reorder equal to the naive output to fp-rounding. TheM=1decode GEMV path and host M-based routing are a separate follow-up.Authored with assistance from Claude Code.
Differential Revision: D109250327