sync 6fdf6301e2bb
Browse filesFiles changed (21)
- README.md+5-2
- build/webgpu/attention-rank4-tiled.wgsl.jinja+1-25
- build/webgpu/attn-flash-decode-splitk-merge.wgsl.jinja+2-9
- build/webgpu/attn-flash-decode-splitk.wgsl.jinja+14-115
- build/webgpu/attn-flash-online.wgsl.jinja+8-44
- build/webgpu/attn-flash-prefill-cluster.wgsl.jinja+41-194
- build/webgpu/attn-flash-q32-broadcast.wgsl.jinja+46-11
- build/webgpu/attn-materialized-apply-f32.wgsl.jinja+3-2
- build/webgpu/attn-materialized-rowstats-combine-f32.wgsl.jinja+9-5
- build/webgpu/attn-materialized-score-f32.wgsl.jinja+6-85
- build/webgpu/attn-materialized-sgmat-f32.wgsl.jinja+28-54
- build/webgpu/attn-materialized-softmax-f32.wgsl.jinja+6-41
- build/webgpu/attn-online-scalar.wgsl.jinja+39-28
- build/webgpu/attn-small-head-parallel.wgsl.jinja+3-9
- build/webgpu/attn-small-head-value.wgsl.jinjaAdded+198
- build/webgpu/bench.json
- build/webgpu/manifest.json+224-362
- build/webgpu/metadata.json+24-21
- build/webgpu/mha-small-seq-blocked.wgsl.jinja+5-4
- build/webgpu/mha-small-seq.wgsl.jinja+26-6
- build/webgpu/test.json
51 unmodified lines52535455565742 unmodified lines1001011021031041051067 unmodified lines11411511611711811912012112212312412512651 unmodified linesOne implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.- `qkv_bias_small_seq_blocked` — Blocked whole-head attention for short key sequences with bias and optional causal masking. A workgroup stages one head's K and V once, then assigns each query in a small block to a group of key lanes whose register-resident online-softmax partials are merged in one pass.- `qkv_no_bias_small_seq_blocked` — Blocked whole-head attention for short key sequences without bias and with optional causal masking. A workgroup stages one head's K and V once, then assigns each query in a small block to a group of key lanes whose register-resident online-softmax partials are merged in one pass.- `qkv_no_bias_small_seq` — Whole-head attention for short bidirectional float32 requests without bias. One workgroup stages a head's complete key and value planes, and each participating invocation owns one query row through scoring, normalization, and context accumulation.42 unmodified lines- [`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 cases- [`attention-rank4-tiled.wgsl.jinja`](build/webgpu/attention-rank4-tiled.wgsl.jinja)- [`attn-flash-decode-splitk-merge.wgsl.jinja`](build/webgpu/attn-flash-decode-splitk-merge.wgsl.jinja)- [`attn-flash-decode-splitk.wgsl.jinja`](build/webgpu/attn-flash-decode-splitk.wgsl.jinja)7 unmodified lines- [`attn-materialized-softmax-f32.wgsl.jinja`](build/webgpu/attn-materialized-softmax-f32.wgsl.jinja)- [`attn-online-scalar.wgsl.jinja`](build/webgpu/attn-online-scalar.wgsl.jinja)- [`attn-small-head-parallel.wgsl.jinja`](build/webgpu/attn-small-head-parallel.wgsl.jinja)- [`mha-small-seq-blocked.wgsl.jinja`](build/webgpu/mha-small-seq-blocked.wgsl.jinja)- [`mha-small-seq.wgsl.jinja`](build/webgpu/mha-small-seq.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.51 unmodified lines525354555657585942 unmodified lines1021031041051061071087 unmodified lines11611711811912012112212312412512612712812951 unmodified linesOne implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.- `qkv_no_bias_small_head_value_subgroups` — Cooperatively reduces values across key lanes and reuses keys/values for two queries when device workgroup storage fits. Uses portable-width subgroup collectives with f32 scores, probabilities and accumulation. Short key sequences use the serial value path.- `qkv_no_bias_small_head_value_tree` — Cooperatively reduces values across key lanes and reuses keys/values for two queries when device workgroup storage fits. Uses shared-memory tree reductions with f32 scores, probabilities and accumulation. Short key sequences use the serial value path.- `qkv_bias_small_seq_blocked` — Blocked whole-head attention for short key sequences with bias and optional causal masking. A workgroup stages one head's K and V once, then assigns each query in a small block to a group of key lanes whose register-resident online-softmax partials are merged in one pass.- `qkv_no_bias_small_seq_blocked` — Blocked whole-head attention for short key sequences without bias and with optional causal masking. A workgroup stages one head's K and V once, then assigns each query in a small block to a group of key lanes whose register-resident online-softmax partials are merged in one pass.- `qkv_no_bias_small_seq` — Whole-head attention for short bidirectional float32 requests without bias. One workgroup stages a head's complete key and value planes, and each participating invocation owns one query row through scoring, normalization, and context accumulation.42 unmodified lines- [`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 cases- [`attention-rank4-tiled.wgsl.jinja`](build/webgpu/attention-rank4-tiled.wgsl.jinja)- [`attn-flash-decode-splitk-merge.wgsl.jinja`](build/webgpu/attn-flash-decode-splitk-merge.wgsl.jinja)- [`attn-flash-decode-splitk.wgsl.jinja`](build/webgpu/attn-flash-decode-splitk.wgsl.jinja)7 unmodified lines- [`attn-materialized-softmax-f32.wgsl.jinja`](build/webgpu/attn-materialized-softmax-f32.wgsl.jinja)- [`attn-online-scalar.wgsl.jinja`](build/webgpu/attn-online-scalar.wgsl.jinja)- [`attn-small-head-parallel.wgsl.jinja`](build/webgpu/attn-small-head-parallel.wgsl.jinja)- [`attn-small-head-value.wgsl.jinja`](build/webgpu/attn-small-head-value.wgsl.jinja)- [`mha-small-seq-blocked.wgsl.jinja`](build/webgpu/mha-small-seq-blocked.wgsl.jinja)- [`mha-small-seq.wgsl.jinja`](build/webgpu/mha-small-seq.wgsl.jinja)## Use with `@huggingface/kernels````shnpm install --save-exact @huggingface/kernels@0.0.1-preview.3```Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
123411 unmodified lines1617181920212223242526272822 unmodified lines515253545556575859606162636411 unmodified lines76777879808182838485868788899091929394959613 unmodified lines110111112113114115116117118119120121122123124125126127128129130131{% set layout = layout if layout is defined else "bnsh" %}{{ env.wgsl.resourceDeclarations }}// Thread-per-query online-softmax flash fallback for rank-4 (BNSH) attention with11 unmodified linesvar<workgroup> acc: array<f32, {{ vHeadCap * blockM }}>;{% set ATTN_SCALE_DIM = "params.headSize" %}{% if ATTN_SCALE_DIM is not defined %}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}{% endif %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}fn kv_head(q_head: u32) -> u32 {return q_head / (params.qHeads / params.kvHeads);}22 unmodified linesif (qs >= params.qSeq) { return; }let kh = kv_head(qh);{% if layout == "bsh" %}// Token-major packed QKV: row = (batch*seq + token)*HIDDEN + head*head_dim.let qBase = (batch * params.qSeq + qs) * params.qHidden + qh * params.headSize;{% else %}let qBase = ((batch * params.qHeads + qh) * params.qSeq + qs) * params.headSize;let kBase = (batch * params.kvHeads + kh) * params.kvSeq;let vBase = (batch * params.kvHeads + kh) * params.kvSeq;{% endif %}let scale = scale_value();for (var d: u32 = 0u; d < params.vHeadSize; d = d + 1u) {11 unmodified linesfor (var ks: u32 = 0u; ks < maxKj; ks = ks + 1u) {var masked = false;{% if hasMask and maskIsBool %}let mIdxB = batch * params.maskBatchStride + qh * params.maskHeadStride + qs * params.maskSeqStride + ks;masked = attn_mask[mIdxB] == 0u;{% endif %}// A rejected bool-mask key has zero softmax mass. Skipping it is safe here:// this thread-per-query kernel has no barriers inside the key loop.if (masked) { continue; }// Adjacent query threads load the same K/V row for this key.var score: f32 = -3.4028234663852886e38;if (!masked) {{% if layout == "bsh" %}let kRow = (batch * params.kvSeq + ks) * params.kvHidden + kh * params.headSize;{% else %}let kRow = (kBase + ks) * params.headSize;{% endif %}var dot: f32 = 0.0;for (var d: u32 = 0u; d < params.headSize; d = d + 1u) {dot = dot + f32(q[qBase + d]) * f32(k[kRow + d]);13 unmodified lineslet weight = exp(score - next_max);running_max = next_max;running_denom = running_denom * prev_scale + weight;{% if layout == "bsh" %}let vRow = (batch * params.kvSeq + ks) * params.vHidden + kh * params.vHeadSize;{% else %}let vRow = (vBase + ks) * params.vHeadSize;{% endif %}for (var d: u32 = 0u; d < params.vHeadSize; d = d + 1u) {acc[d * BLOCK_M + tid] = acc[d * BLOCK_M + tid] * prev_scale + weight * f32(v[vRow + d]);}}let inv_denom = select(0.0, 1.0 / running_denom, running_denom > 0.0);{% if layout == "bsh" %}let yBase = (batch * params.qSeq + qs) * (params.qHeads * params.vHeadSize) + qh * params.vHeadSize;{% else %}let yBase = ((batch * params.qHeads + qh) * params.qSeq + qs) * params.vHeadSize;{% endif %}for (var d: u32 = 0u; d < params.vHeadSize; d = d + 1u) {y[yBase + d] = {{ scalar }}(acc[d * BLOCK_M + tid] * inv_denom);}123411 unmodified lines161718192021222324252622 unmodified lines495051525354555611 unmodified lines6869707172737475767778798013 unmodified lines949596979899100101102103104105106107{% set maskIsBool = false %}{{ env.wgsl.resourceDeclarations }}// Thread-per-query online-softmax flash fallback for rank-4 (BNSH) attention with11 unmodified linesvar<workgroup> acc: array<f32, {{ vHeadCap * blockM }}>;{% set ATTN_SCALE_DIM = "params.headSize" %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}fn kv_head(q_head: u32) -> u32 {return q_head / (params.qHeads / params.kvHeads);}22 unmodified linesif (qs >= params.qSeq) { return; }let kh = kv_head(qh);// Token-major packed QKV: row = (batch*seq + token)*HIDDEN + head*head_dim.let qBase = (batch * params.qSeq + qs) * params.qHidden + qh * params.headSize;let scale = scale_value();for (var d: u32 = 0u; d < params.vHeadSize; d = d + 1u) {11 unmodified linesfor (var ks: u32 = 0u; ks < maxKj; ks = ks + 1u) {var masked = false;// A rejected bool-mask key has zero softmax mass. Skipping it is safe here:// this thread-per-query kernel has no barriers inside the key loop.if (masked) { continue; }// Adjacent query threads load the same K/V row for this key.var score: f32 = -3.4028234663852886e38;if (!masked) {let kRow = (batch * params.kvSeq + ks) * params.kvHidden + kh * params.headSize;var dot: f32 = 0.0;for (var d: u32 = 0u; d < params.headSize; d = d + 1u) {dot = dot + f32(q[qBase + d]) * f32(k[kRow + d]);13 unmodified lineslet weight = exp(score - next_max);running_max = next_max;running_denom = running_denom * prev_scale + weight;let vRow = (batch * params.kvSeq + ks) * params.vHidden + kh * params.vHeadSize;for (var d: u32 = 0u; d < params.vHeadSize; d = d + 1u) {acc[d * BLOCK_M + tid] = acc[d * BLOCK_M + tid] * prev_scale + weight * f32(v[vRow + d]);}}let inv_denom = select(0.0, 1.0 / running_denom, running_denom > 0.0);let yBase = (batch * params.qSeq + qs) * (params.qHeads * params.vHeadSize) + qh * params.vHeadSize;for (var d: u32 = 0u; d < params.vHeadSize; d = d + 1u) {y[yBase + d] = {{ scalar }}(acc[d * BLOCK_M + tid] * inv_denom);}
12345678939 unmodified lines49505152535449 unmodified lines104105106107108109110111112113114{% if scalar == "f16" %}enable f16;{% endif %}{% if splitQueries is not defined %}{% set splitQueries = false %}{% endif %}{% if hasGate is not defined %}{% set hasGate = false %}{% endif %}{% set qSeq = qSeq | default(0) %}{{ env.wgsl.resourceDeclarations }}// Split-K flash decode, pass 2 of 2. Combines the per-split un-normalized online39 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}49 unmodified lines// V bias row base: 2 * qHidden (skip the packed Q and K bias blocks) + this head.let vBiasBase = 2u * Q_HIDDEN + h * HEAD_DIM + d4 * 4u;outValue = outValue + vec4<f32>(bias[vBiasBase], bias[vBiasBase + 1u], bias[vBiasBase + 2u], bias[vBiasBase + 3u]);{% endif %}{% if hasGate %}// The gated route multiplies the normalized attention output elementwise by its gate.let gateV = vec4<f32>(gate[qBaseV4 + d4]);outValue = outValue * (vec4<f32>(1.0) / (vec4<f32>(1.0) + exp(-gateV)));{% endif %}output[qBaseV4 + d4] = vec4<{{ scalar }}>(outValue);}12345639 unmodified lines4647484950515249 unmodified lines102103104105106107{% if splitQueries is not defined %}{% set splitQueries = false %}{% endif %}{% set qSeq = qSeq | default(0) %}{% set qHidden = qHidden | default(0) %}{{ env.wgsl.resourceDeclarations }}// Split-K flash decode, pass 2 of 2. Combines the per-split un-normalized online39 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}49 unmodified lines// V bias row base: 2 * qHidden (skip the packed Q and K bias blocks) + this head.let vBiasBase = 2u * Q_HIDDEN + h * HEAD_DIM + d4 * 4u;outValue = outValue + vec4<f32>(bias[vBiasBase], bias[vBiasBase + 1u], bias[vBiasBase + 2u], bias[vBiasBase + 3u]);{% endif %}output[qBaseV4 + d4] = vec4<{{ scalar }}>(outValue);}
12345678910111 unmodified line1314151617181936 unmodified lines5657585960611 unmodified line6364656667686962 unmodified lines13213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117223 unmodified lines1961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482491 unmodified line25125225325425525625725825926026126226326426526623 unmodified lines29029129229329429529629729829930030130214 unmodified lines31731831932032132232333 unmodified lines35735835936036136236312 unmodified lines3763773783793803813828 unmodified lines3913923933943953963973983994004014024034044054064074084094104119 unmodified lines4214224234244254264276 unmodified lines43443543643743843944044144244311 unmodified lines455456457458459460461462463464{% if useSubgroups is not defined %}{% set useSubgroups = true %}{% endif %}{% if splitQueries is not defined %}{% set splitQueries = false %}{% endif %}{% if quantizedCache is not defined %}{% set quantizedCache = false %}{% endif %}{% if cacheSeqlens is not defined %}{% set cacheSeqlens = false %}{% endif %}{% if hasMask is not defined %}{% set hasMask = false %}{% endif %}{% if maskIsBool is not defined %}{% set maskIsBool = false %}{% endif %}{% set scale = scale | default("0.0") %}{% if hasRotary is not defined %}{% set hasRotary = false %}{% endif %}{% set qSeq = qSeq | default(0) %}{% set splitKWorkgroupSize = workgroupSizeSpec if workgroupSizeSpec is defined else tunables.WORKGROUP_SIZE %}{% if useSubgroups %}1 unmodified line{% endif %}{{ env.wgsl.resourceDeclarations }}// Split-K flash attention, pass 1 of 2; the merge pass follows. This geometry// handles decode and short-query, long-context prefill inputs.//// The non-split flash decode launches only `batch * numHeads` workgroups, each36 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}1 unmodified linevar<workgroup> q_shared: array<vec4<f32>, HEAD_DIM_V4>;var<workgroup> running_out: array<vec4<f32>, HEAD_DIM_V4>;var<workgroup> probs: array<f32, WG>;{% set coopQk = useSubgroups and headDimV4 >= 8 and not (usesF16 and headDimV4 <= 32) %}{% set jGroups = (splitKWorkgroupSize / headDimV4)|int %}{% set jSplitV = (splitKWorkgroupSize % headDimV4 == 0) and (jGroups >= 2) %}{% if coopQk %}62 unmodified linesreturn combinedMD;}{% else %}{% set mdStreamed = mdStreams is defined %}{% set mdStreams = mdStreams if mdStreams is defined else 1 %}{% set mdExtent = "WG" if mdStreams == 1 else "WG * " ~ mdStreams ~ "u" %}var<workgroup> partialM: array<f32, {{ mdExtent }}>;var<workgroup> partialD: array<f32, {{ mdExtent }}>;{% if mdStreamed %}// In-place fold of {{ mdStreams }} streams. Input partials occupy// partialM/partialD; stream s returns its merged pair in slot s * WG.fn combine_partials_streams(lidx: u32) {workgroupBarrier();var stride = WG / 2u;loop {if (stride == 0u) {break;}if (lidx < stride) {{% for s in range(mdStreams) %}{let slot = {{ s }}u * WG + lidx;let m1 = partialM[slot];let d1 = partialD[slot];let m2 = partialM[slot + stride];let d2 = partialD[slot + stride];let mNew = max(m1, m2);partialD[slot] = d1 * exp_shift(m1, mNew) + d2 * exp_shift(m2, mNew);partialM[slot] = mNew;}{% endfor %}}workgroupBarrier();stride = stride / 2u;}}{% else %}fn combine_partials(m: f32, d: f32, lidx: u32) -> vec2<f32> {partialM[lidx] = m;23 unmodified linesreturn merged;}{% endif %}{% endif %}{% if ATTN_SCALE_DIM is not defined %}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}{% endif %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}{% if quantizedCache %}{% macro emit_quant_scale4(kind, scaleBuffer) %}fn {{ kind }}scale4(d4: u32, hk: u32) -> vec4<f32> {if (params.perChannel == 0u) {return vec4<f32>({{ scaleBuffer }}[0]);}let base = hk * HEAD_DIM + d4 * 4u;return vec4<f32>({{ scaleBuffer }}[base],{{ scaleBuffer }}[base + 1u],{{ scaleBuffer }}[base + 2u],{{ scaleBuffer }}[base + 3u]);}{%- endmacro %}{%- macro emit_quant_load4(format, kind, buffer, scaleBuffer) %}{{ emit_quant_scale4(kind, scaleBuffer) }}fn load_{{ kind }}4(indexV4: u32, d4: u32, hk: u32) -> vec4<f32> {{%- if format == "int8" %}return vec4<f32>({{ buffer }}[indexV4]) * {{ kind }}scale4(d4, hk);{%- else %}// Two elements cover this vec4: each carries two +8-biased nibbles, low first.let rowBase = indexV4 - d4;let lo = {{ buffer }}[rowBase + d4 * 2u];let hi = {{ buffer }}[rowBase + d4 * 2u + 1u];let nibbles = vec4<i32>(i32(lo & 0xFu), i32((lo >> 4u) & 0xFu),i32(hi & 0xFu), i32((hi >> 4u) & 0xFu));let signed = nibbles - vec4<i32>(8);return vec4<f32>(signed) * {{ kind }}scale4(d4, hk);{%- endif %}}{%- endmacro %}{{ emit_quant_load4("int8", "key", "key", "k_scale") }}{{ emit_quant_load4("int8", "value", "value", "v_scale") }}{% else %}fn load_key4(indexV4: u32) -> vec4<f32> {return vec4<f32>(key[indexV4]);}1 unmodified linefn load_value4(indexV4: u32) -> vec4<f32> {return vec4<f32>(value[indexV4]);}{% endif %}{% if hasBias %}// Packed [Q; K; V] bias rows (token-independent). The Q bias folds into the// query row before the Q.K dots; the K bias adds a constant to every key score// that softmax cancels, so it is skipped; the V bias is token-independent and// is applied once in the merge pass after the final normalize.fn load_bias4(base: u32, d4: u32) -> vec4<f32> {let offset = base + d4 * 4u;return vec4<f32>(bias[offset], bias[offset + 1u], bias[offset + 2u], bias[offset + 3u]);}{% endif %}23 unmodified lineslet tid = lid.x;let hKv = h / (Q_HEADS / KV_HEADS);let cacheSeq = params.kvSeq;{% if cacheSeqlens %}// Buffer-sharing caches retain their capacity in the physical BNSH stride;// seqlens_k supplies the active end independently for each batch.let kvSeq = min(cacheSeq, u32(seqlens_k[b]) + 1u);{% else %}let kvSeq = cacheSeq;{% endif %}// Query row (decode uses token zero; short-query prefill folds the token into wg.x).{% if splitQueries %}14 unmodified linessplitEnd = kvSeq;}{% set hasBias = hasBias is defined and hasBias %}for (var d4 = tid; d4 < HEAD_DIM_V4; d4 = d4 + WG) {var qv = vec4<f32>(query[qBaseV4 + d4]);{% if hasBias %}33 unmodified linesif (j < tileCount) {let kRowV4 = kvBaseV4 + (kjBase + j) * kvTokenStrideV4;for (var d4: u32 = lane; d4 < HEAD_DIM_V4; d4 = d4 + sgSize) {accS = accS + dot(q_shared[d4], load_key4(kRowV4 + d4{% if quantizedCache %}, d4, hKv{% endif %}));}}let sj = subgroupAdd(accS);12 unmodified lineslet kRowV4 = kvBaseV4 + kj * kvTokenStrideV4;var acc: f32 = 0.0;for (var d4: u32 = 0u; d4 < HEAD_DIM_V4; d4 = d4 + 1u) {acc = acc + dot(q_shared[d4], load_key4(kRowV4 + d4{% if quantizedCache %}, d4, hKv{% endif %}));}score = acc * scale;m = score;8 unmodified lineslet maskQuery = 0u;{% endif %}let maskIndex = b * params.maskBatchStride + h * params.maskHeadStride + maskQuery * params.maskSeqStride + kj;{% if maskIsBool %}// A rejected bool-mask key contributes no probability mass. The merge// pass already maps a zero global denominator to an all-zero output row.if (attn_mask[maskIndex] == 0u) {keyAllowed = false;score = -FLT_MAX;dPart = 0.0;}{% else %}score = score + f32(attn_mask[maskIndex]);{% endif %}m = score;}{% endif %}let tile = combine_partials(m, dPart, tid{% if useSubgroups %}, sgSize{% endif %});// Merge one key tile's online-softmax (maximum, denominator) partial into the// running state, then store the per-key probabilities consumed by V accumulation.9 unmodified linesprobs[tid] = prob;workgroupBarrier();{% if jSplitV %}// j-split V accumulation: thread (jg, d4v) sums keys j == jg mod// J_GROUPS for dim block d4v into a register, then the groups combine6 unmodified linesloop {if (jj >= tileCount) { break; }vacc = vacc + probs[jj] * load_value4(kvBaseV4 + (kjBase + jj) * kvTokenStrideV4 + d4v{% if quantizedCache %},d4v,hKv{% endif %});jj = jj + J_GROUPS;}vacc_sh[tid] = vacc;11 unmodified linesvar vSum = vec4<f32>(0.0);for (var i: u32 = 0u; i < tileCount; i = i + 1u) {vSum = vSum + probs[i] * load_value4(kvBaseV4 + (kjBase + i) * kvTokenStrideV4 + d4{% if quantizedCache %},d4,hKv{% endif %});}running_out[d4] = running_out[d4] * correction + vSum;}1234561 unmodified line8910111213141536 unmodified lines525354555657581 unmodified line6061626364656662 unmodified lines12913013113213313413513613723 unmodified lines1611621631641651661671681691701711721 unmodified line17417517617717817918018118218318418518618718818919023 unmodified lines21421521621721821922014 unmodified lines23523623723823924033 unmodified lines27427527627727827928012 unmodified lines2932942952962972982998 unmodified lines3083093103113123133143153163179 unmodified lines3273283293303313326 unmodified lines33934034134234334434511 unmodified lines357358359360361362363{% if useSubgroups is not defined %}{% set useSubgroups = true %}{% endif %}{% if splitQueries is not defined %}{% set splitQueries = false %}{% endif %}{% if hasMask is not defined %}{% set hasMask = false %}{% endif %}{% set qSeq = qSeq | default(0) %}{% set splitKWorkgroupSize = workgroupSizeSpec if workgroupSizeSpec is defined else tunables.WORKGROUP_SIZE %}{% if useSubgroups %}1 unmodified line{% endif %}{{ env.wgsl.resourceDeclarations }}// Split-K flash attention. Single-partition direct output skips the merge pass.// Otherwise this is the first of two passes. This geometry// handles decode and short-query, long-context prefill inputs.//// The non-split flash decode launches only `batch * numHeads` workgroups, each36 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}1 unmodified linevar<workgroup> q_shared: array<vec4<f32>, HEAD_DIM_V4>;var<workgroup> running_out: array<vec4<f32>, HEAD_DIM_V4>;var<workgroup> probs: array<f32, WG>;{% set coopQk = useSubgroups and headDimV4 >= 8 and not (usesF16 and headDimV4 <= 32) and (allowCooperativeQk if allowCooperativeQk is defined else true) %}{% set jGroups = (splitKWorkgroupSize / headDimV4)|int %}{% set jSplitV = (splitKWorkgroupSize % headDimV4 == 0) and (jGroups >= 2) %}{% if coopQk %}62 unmodified linesreturn combinedMD;}{% else %}{% set mdExtent = "WG" %}var<workgroup> partialM: array<f32, {{ mdExtent }}>;var<workgroup> partialD: array<f32, {{ mdExtent }}>;fn combine_partials(m: f32, d: f32, lidx: u32) -> vec2<f32> {partialM[lidx] = m;23 unmodified linesreturn merged;}{% endif %}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}fn load_key4(indexV4: u32) -> vec4<f32> {return vec4<f32>(key[indexV4]);}1 unmodified linefn load_value4(indexV4: u32) -> vec4<f32> {return vec4<f32>(value[indexV4]);}{% if hasBias %}// Packed [Q; K; V] bias rows (token-independent). The Q bias folds into the// query row before the Q.K dots; the K bias adds a constant to every key score// that softmax cancels, so it is skipped; the V bias is token-independent and// is applied once in the merge pass after the final normalize.{% set BW = "" %}{% set BC = "" %}fn load_bias4(base: u32, d4: u32) -> vec4<f32> {let offset = base + d4 * 4u;return vec4<f32>({{ BW }}bias[offset]{{ BC }}, {{ BW }}bias[offset + 1u]{{ BC }}, {{ BW }}bias[offset + 2u]{{ BC }}, {{ BW }}bias[offset + 3u]{{ BC }});}{% endif %}23 unmodified lineslet tid = lid.x;let hKv = h / (Q_HEADS / KV_HEADS);let cacheSeq = params.kvSeq;let kvSeq = cacheSeq;// Query row (decode uses token zero; short-query prefill folds the token into wg.x).{% if splitQueries %}14 unmodified linessplitEnd = kvSeq;}for (var d4 = tid; d4 < HEAD_DIM_V4; d4 = d4 + WG) {var qv = vec4<f32>(query[qBaseV4 + d4]);{% if hasBias %}33 unmodified linesif (j < tileCount) {let kRowV4 = kvBaseV4 + (kjBase + j) * kvTokenStrideV4;for (var d4: u32 = lane; d4 < HEAD_DIM_V4; d4 = d4 + sgSize) {accS = accS + dot(q_shared[d4], load_key4(kRowV4 + d4));}}let sj = subgroupAdd(accS);12 unmodified lineslet kRowV4 = kvBaseV4 + kj * kvTokenStrideV4;var acc: f32 = 0.0;for (var d4: u32 = 0u; d4 < HEAD_DIM_V4; d4 = d4 + 1u) {acc = acc + dot(q_shared[d4], load_key4(kRowV4 + d4));}score = acc * scale;m = score;8 unmodified lineslet maskQuery = 0u;{% endif %}let maskIndex = b * params.maskBatchStride + h * params.maskHeadStride + maskQuery * params.maskSeqStride + kj;score = score + f32(attn_mask[maskIndex]);m = score;}{% endif %} let tile = combine_partials(m, dPart, tid{% if useSubgroups %}, sgSize{% endif %});// Merge one key tile's online-softmax (maximum, denominator) partial into the// running state, then store the per-key probabilities consumed by V accumulation.9 unmodified linesprobs[tid] = prob;workgroupBarrier();{% if jSplitV %}// j-split V accumulation: thread (jg, d4v) sums keys j == jg mod// J_GROUPS for dim block d4v into a register, then the groups combine6 unmodified linesloop {if (jj >= tileCount) { break; }vacc = vacc + probs[jj] * load_value4(kvBaseV4 + (kjBase + jj) * kvTokenStrideV4 + d4v );jj = jj + J_GROUPS;}vacc_sh[tid] = vacc;11 unmodified linesvar vSum = vec4<f32>(0.0);for (var i: u32 = 0u; i < tileCount; i = i + 1u) {vSum = vSum + probs[i] * load_value4(kvBaseV4 + (kjBase + i) * kvTokenStrideV4 + d4 );}running_out[d4] = running_out[d4] * correction + vSum;}
27 unmodified lines282930313233343514 unmodified lines5051525354557 unmodified lines6364656667686946 unmodified lines11611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515623 unmodified lines18018118218318418518618718818919019119219319419519619719819920020119 unmodified lines22122222322422522622769 unmodified lines29729829930030130230327 unmodified linesconst KV_HIDDEN_V4: u32 = {{ kvHiddenV4 }}u;const Q_HEADS: u32 = {{ qNumHeads }}u;const KV_HEADS: u32 = {{ kvNumHeads }}u;{% set qHeads = "params.qHeads" if headsFromParams else "Q_HEADS" %}{% set kvHeads = "params.kvHeads" if headsFromParams else "KV_HEADS" %}const WG: u32 = {{ tunables.WORKGROUP_SIZE }}u;// FLT_MAX, not -inf, as the online (m, d) accumulator init: merges must keep// `m - m` finite so an empty lane / all--inf row contributes the exact14 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}7 unmodified lines// Both the subgroup and portable barrier-tree engines return the same merged// pair to every invocation. Repeated merges require a workgroup barrier between// calls before their shared partial storage is reused.{% set combineSubgroups = combineSubgroups is defined and combineSubgroups %}{% if combineSubgroups %}// Cross-subgroup merge that assumes nothing about which invocations share a// subgroup or how many subgroups there are: each subgroup's elected lane46 unmodified linesreturn combinedMD;}{% else %}{% set mdStreamed = mdStreams is defined %}{% set mdStreams = mdStreams if mdStreams is defined else 1 %}{% set mdExtent = "WG" if mdStreams == 1 else "WG * " ~ mdStreams ~ "u" %}var<workgroup> partialM: array<f32, {{ mdExtent }}>;var<workgroup> partialD: array<f32, {{ mdExtent }}>;{% if mdStreamed %}// In-place fold of {{ mdStreams }} streams. Input partials occupy// partialM/partialD; stream s returns its merged pair in slot s * WG.fn combine_partials_streams(lidx: u32) {workgroupBarrier();var stride = WG / 2u;loop {if (stride == 0u) {break;}if (lidx < stride) {{% for s in range(mdStreams) %}{let slot = {{ s }}u * WG + lidx;let m1 = partialM[slot];let d1 = partialD[slot];let m2 = partialM[slot + stride];let d2 = partialD[slot + stride];let mNew = max(m1, m2);partialD[slot] = d1 * exp_shift(m1, mNew) + d2 * exp_shift(m2, mNew);partialM[slot] = mNew;}{% endfor %}}workgroupBarrier();stride = stride / 2u;}}{% else %}fn combine_partials(m: f32, d: f32, lidx: u32) -> vec2<f32> {partialM[lidx] = m;23 unmodified linesreturn merged;}{% endif %}{% endif %}// An explicit-zero specialization bakes the scale as 0. Otherwise,// params.scale == 0 encodes an omitted scale and selects 1/sqrt(headDim).{% if ATTN_SCALE_DIM is not defined %}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}{% endif %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}{% if hasBias %}fn load_bias4(base: u32, d4: u32) -> vec4<f32> {let offset = base + d4 * 4u;return vec4<f32>(bias[offset], bias[offset + 1u], bias[offset + 2u], bias[offset + 3u]);}{% endif %}19 unmodified lineslet kvTokenStrideV4 = KV_HIDDEN_V4;// Cooperative vec4 Q-row load; init the output accumulator.{% set hasBias = hasBias is defined and hasBias %}for (var d4 = tid; d4 < HEAD_DIM_V4; d4 = d4 + WG) {var qv = vec4<f32>(query[qBaseV4 + d4]);{% if hasBias %}69 unmodified linesprobs[tid] = prob;workgroupBarrier();// running_out[d4] is owned by the same thread (tid ≡ d4 mod WG) across all// tiles, so the rescale-and-accumulate below is race-free; vec4 V loads.let tileCount = min(WG, keyBound - kjBase);27 unmodified lines282930313233343514 unmodified lines505152535455567 unmodified lines64656667686946 unmodified lines11611711811912012112212312423 unmodified lines14814915015115215315415515615715815916016116216316416516616719 unmodified lines18718818919019119269 unmodified lines26226326426526626727 unmodified linesconst KV_HIDDEN_V4: u32 = {{ kvHiddenV4 }}u;const Q_HEADS: u32 = {{ qNumHeads }}u;const KV_HEADS: u32 = {{ kvNumHeads }}u;{% set qHeads = "Q_HEADS" %}{% set kvHeads = "KV_HEADS" %}const WG: u32 = {{ tunables.WORKGROUP_SIZE }}u;// FLT_MAX, not -inf, as the online (m, d) accumulator init: merges must keep// `m - m` finite so an empty lane / all--inf row contributes the exact14 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}7 unmodified lines// Both the subgroup and portable barrier-tree engines return the same merged// pair to every invocation. Repeated merges require a workgroup barrier between// calls before their shared partial storage is reused.{% if combineSubgroups %}// Cross-subgroup merge that assumes nothing about which invocations share a// subgroup or how many subgroups there are: each subgroup's elected lane46 unmodified linesreturn combinedMD;}{% else %}{% set mdExtent = "WG" %}var<workgroup> partialM: array<f32, {{ mdExtent }}>;var<workgroup> partialD: array<f32, {{ mdExtent }}>;fn combine_partials(m: f32, d: f32, lidx: u32) -> vec2<f32> {partialM[lidx] = m;23 unmodified linesreturn merged;}{% endif %}// An explicit-zero specialization bakes the scale as 0. Otherwise,// params.scale == 0 encodes an omitted scale and selects 1/sqrt(headDim).{% set ATTN_SCALE_DIM = "HEAD_DIM" %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}{% if hasBias %}{% set BW = "" %}{% set BC = "" %}fn load_bias4(base: u32, d4: u32) -> vec4<f32> {let offset = base + d4 * 4u;return vec4<f32>({{ BW }}bias[offset]{{ BC }}, {{ BW }}bias[offset + 1u]{{ BC }}, {{ BW }}bias[offset + 2u]{{ BC }}, {{ BW }}bias[offset + 3u]{{ BC }});}{% endif %}19 unmodified lineslet kvTokenStrideV4 = KV_HIDDEN_V4;// Cooperative vec4 Q-row load; init the output accumulator.for (var d4 = tid; d4 < HEAD_DIM_V4; d4 = d4 + WG) {var qv = vec4<f32>(query[qBaseV4 + d4]);{% if hasBias %}69 unmodified linesprobs[tid] = prob;workgroupBarrier();// running_out[d4] is owned by the same thread (tid ≡ d4 mod WG) across all// tiles, so the rescale-and-accumulate below is race-free; vec4 V loads.let tileCount = min(WG, keyBound - kjBase);
12345678910111213141516171819202122232425262728293031323 unmodified lines36373839404142434445464748495013 unmodified lines64656667686970717 unmodified lines7980818283848586878889909192939495969798991001011026 unmodified lines10911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917041 unmodified lines21221321421521621721821922022122222322422522622722831 unmodified lines26026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129236 unmodified lines32933033133233333433533633733833934034134234330 unmodified lines37437537637737837938038138238338438538638738838939039139239339439539639739839940040140231 unmodified lines4344354364374384394404414424434444454464474484494504514524534544554564574584594604614621 unmodified line46446546646746846947047147247347447547647747847948048148248348448548648752 unmodified lines540541542543544545546547548549550551552553554555556557558559560561{% set sourceProfile = sourceProfile if sourceProfile is defined else 0 %}{% set scaling = scaling | default("0.0") %}{% set qkvStrideV4 = qkvStrideV4 | default(0) %}{% set QSEQ = "params.seq_len" if sourceProfile == 1 else "params.qSeq" %}{% set KVSEQ = "(params.past_len + params.seq_len)" if sourceProfile == 1 else "params.kvSeq" %}{% set IS_CAUSAL = "1u" if sourceProfile == 1 else "params.isCausal" %}{% set Q_STRIDE = "QKV_STRIDE_V4" if sourceProfile == 1 else "Q_HIDDEN_V4" %}{% set QUERY = "qkv" if sourceProfile == 1 else "query" %}{% set KEY = "cache_keys" if sourceProfile == 1 else "key" %}{% set VALUE = "cache_values" if sourceProfile == 1 else "value" %}{% set OUTPUT = "attn_out" if sourceProfile == 1 else "output" %}{% if sourceProfile == 1 %}{% set ATTN_SCALE_OVERRIDE = scaling %}{% endif %}{% if useSubgroups is not defined %}{% set useSubgroups = true %}{% endif %}{% if batchNoSgReduction is not defined %}{% set batchNoSgReduction = false %}{% endif %}{% if hasMask is not defined %}{% set hasMask = false %}{% endif %}{% if maskIsKeyKeep is not defined %}{% set maskIsKeyKeep = false %}{% endif %}{% if maskIsBool is not defined %}{% set maskIsBool = false %}{% endif %}{% if hasSoftcap is not defined %}{% set hasSoftcap = false %}{% endif %}{% if hasHeadSink is not defined %}{% set hasHeadSink = false %}{% endif %}{% set Q_HIDDEN = qHidden | default(0) %}{% if quantCacheFormat is not defined %}{% set quantCacheFormat = "" %}{% endif %}{% if useSeqlens is not defined %}{% set useSeqlens = false %}{% endif %}// A windowed cache binds a fixed CAPACITY but keeps only the most recent// min(total, capacity) rows resident. params.kvSeq then names the physical row// count, which is still the right batch stride but the wrong attention bound, so// the bounds read kvActive instead. Modes without seqlens use `params.kvSeq`// in both roles.{% set KVA = "kvActive" if useSeqlens else KVSEQ %}{% macro score_expr(part) %}{% if hasSoftcap %}params.softcap * tanh(clamp(({{ part }} * SCALE) / params.softcap, -30.0, 30.0)){% else %}{{ part }} * SCALE{% endif %}{% endmacro %}{% if useSubgroups %}enable subgroups;{% endif %}3 unmodified lines// workgroup storage; values widen to f32 when read. Other specializations stage// them as f32. Score and weighted-value accumulation remain in f32 throughout.{% set STAGE_MASK = hasMask and useSubgroups and stageMask is defined and stageMask %}{% set MASK_IS_INT = hasMask and (maskIsKeyKeep or maskIsBool) %}{% set MASK_TILE_TYPE = "u32" if MASK_IS_INT else "f32" %}{% set MASK_TILE_LOAD = "attn_mask[maskIndex]" if MASK_IS_INT else "f32(attn_mask[maskIndex])" %}{% set MASK_TILE_ZERO = "0u" if MASK_IS_INT else "0.0" %}{% set MASK_ELEMENT = "maskValue" if STAGE_MASK else "attn_mask[maskIndex]" %}{% set MASK_ADDITIVE = "maskValue" if STAGE_MASK else "f32(attn_mask[maskIndex])" %}{% set SLICE_COUNT = ((headDimV4 / LPQ) | int) %}{% set QPL = (2 if (not usesF16 and SLICE_COUNT <= 4 and TILE_K <= 16 and TILE_Q % 2 == 0) else 1) if useSubgroups else 1 %}{% macro qn(name, qi) %}{{ name }}{% if qi > 0 %}_q{{ qi }}{% endif %}{% endmacro %}// Tiled flash prefill attention with configurable-width query clusters for// token-major [batch, seq, heads*headDim] attention ops. Each workgroup covers13 unmodified linesconst HEAD_DIM: u32 = {{ headDim }}u;const HEAD_DIM_V4: u32 = {{ headDimV4 }}u;const Q_HIDDEN_V4: u32 = {{ qHiddenV4 }}u; // bsh token-major hidden stride{% if sourceProfile == 1 %}const QKV_STRIDE_V4: u32 = {{ qkvStrideV4 }}u; // packed [Q; K; V] input row stride{% endif %}const KV_HIDDEN_V4: u32 = {{ kvHiddenV4 }}u;const Q_HEADS: u32 = {{ qNumHeads }}u;const KV_HEADS: u32 = {{ kvNumHeads }}u;const TILE_Q: u32 = {{ TILE_Q }}u;7 unmodified lines{% else %}const WG: u32 = TILE_Q * LPQ;{% endif %}{% if MASK_IS_INT %}// Key-keep masks in contrib attention use a finite low logit for a rejected// key. Logical ONNX bool masks use the exclusion sentinel instead, so a fully// masked row has zero mass. Keeping one declaration shape for both mask modes// lets both mask modes share the score loop below.const NEG_INF: f32 = -3.4028234663852886e38;const MASK_NEG: f32 = {{ "-1e38" if maskIsKeyKeep else "-3.4028234663852886e38" }};{% else %}const NEG_INF: f32 = -3.4028234663852886e38;{% endif %}var<workgroup> k_tile: array<vec4<{{ ST }}>, TILE_K * (HEAD_DIM / 4u)>;var<workgroup> v_tile: array<vec4<{{ ST }}>, TILE_K * (HEAD_DIM / 4u)>;{% if STAGE_MASK %}// Each LPQ cluster consumes one mask value per (query,key), so stage the// TILE_Q x TILE_K mask tile once instead of issuing LPQ duplicate global loads.var<workgroup> mask_tile: array<{{ MASK_TILE_TYPE }}, TILE_Q * TILE_K>;{% endif %}{% if not useSubgroups %}{% if batchNoSgReduction %}// No-subgroups cluster reduction scratch for a whole K tile. Staging every6 unmodified lines{% endif %}{% endif %}{% if ATTN_SCALE_DIM is not defined %}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}{% endif %}fn scale_value() -> f32 {{% if ATTN_SCALE_OVERRIDE is defined %}return {{ ATTN_SCALE_OVERRIDE }};{% else %}if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));{% endif %}}{% if quantCacheFormat %}// A quantized cache is dequantized once per key into the staged tile, then read// by all TILE_Q queries in the workgroup. The unpack cost is amortized over the// tile height instead of paid once per (query, key).{% macro emit_quant_scale4(kind, scaleBuffer) %}fn {{ kind }}scale4(d4: u32, hk: u32) -> vec4<f32> {if (params.perChannel == 0u) {return vec4<f32>({{ scaleBuffer }}[0]);}let base = hk * HEAD_DIM + d4 * 4u;return vec4<f32>({{ scaleBuffer }}[base],{{ scaleBuffer }}[base + 1u],{{ scaleBuffer }}[base + 2u],{{ scaleBuffer }}[base + 3u]);}{%- endmacro %}{%- macro emit_quant_load4(format, kind, buffer, scaleBuffer) %}{{ emit_quant_scale4(kind, scaleBuffer) }}fn load_{{ kind }}4(indexV4: u32, d4: u32, hk: u32) -> vec4<f32> {{%- if format == "int8" %}return vec4<f32>({{ buffer }}[indexV4]) * {{ kind }}scale4(d4, hk);{%- else %}// Two elements cover this vec4: each carries two +8-biased nibbles, low first.let rowBase = indexV4 - d4;let lo = {{ buffer }}[rowBase + d4 * 2u];let hi = {{ buffer }}[rowBase + d4 * 2u + 1u];let nibbles = vec4<i32>(i32(lo & 0xFu), i32((lo >> 4u) & 0xFu),i32(hi & 0xFu), i32((hi >> 4u) & 0xFu));let signed = nibbles - vec4<i32>(8);return vec4<f32>(signed) * {{ kind }}scale4(d4, hk);{%- endif %}}{%- endmacro %}{{ emit_quant_load4(quantCacheFormat, "key", KEY, "k_scale") }}{{ emit_quant_load4(quantCacheFormat, "value", VALUE, "v_scale") }}{% endif %}{% if hasBias %}fn load_bias4(base: u32, d4: u32) -> vec4<f32> {let offset = base + d4 * 4u;return vec4<f32>(bias[offset], bias[offset + 1u], bias[offset + 2u], bias[offset + 3u]);}{% endif %}41 unmodified linesvar {{ qn("m", qi) }}: f32 = NEG_INF;var {{ qn("l", qi) }}: f32 = 0.0;{% endfor %}{% if useSeqlens %}// Resident rows of a windowed cache: the survivors were shifted down to [0, kvActive),// so query/key DISTANCE is unchanged and every bound below reads as if the cache were// exactly kvActive long. Rotary is excluded from this path (it would need the absolute// position, not the cache-relative one), so pastLenForRope keeps the physical length.// Clamping below by the query count matches the cache-update passes and the scalar// path: a right-padded batch (seqlens_k[b]+1 < qSeq) still appends its whole chunk,// so its queries score against all of it.let kvActive = min({{ KVSEQ }}, max({{ QSEQ }}, u32(seqlens_k[b]) + 1u));{% endif %}// Causal ceiling per query; the key loop runs over the workgroup's union range// (uniform trip count), masking out-of-range (query, key) pairs.// Upper-left causal and/or sliding-window bounds. Query qIdx sits at31 unmodified lineslet kj = kStart + slot;if (kj < wgEnd) {let base4 = kvBatch4 + kj * KV_HIDDEN_V4 + d4;{% if quantCacheFormat %}k_tile[i] = vec4<{{ ST }}>(load_key4(base4, d4, hKv));v_tile[i] = vec4<{{ ST }}>(load_value4(base4, d4, hKv));{% else %}k_tile[i] = vec4<{{ ST }}>({{ KEY }}[base4]);v_tile[i] = vec4<{{ ST }}>({{ VALUE }}[base4]);{% endif %}} else {k_tile[i] = vec4<{{ ST }}>(0.0);v_tile[i] = vec4<{{ ST }}>(0.0);}}{% if STAGE_MASK %}// The K/V load barrier also publishes this compact mask tile.for (var i: u32 = tid; i < TILE_Q * TILE_K; i = i + WG) {let qSlot = i / TILE_K;let kSlot = i % TILE_K;let maskQ = min(wg.x * TILE_Q + qSlot, {{ QSEQ }} - 1u);let maskK = kStart + kSlot;if (maskK < wgEnd) {let maskIndex = b * params.maskBatchStride + h * params.maskHeadStride + maskQ * params.maskSeqStride + maskK;mask_tile[i] = {{ MASK_TILE_LOAD }};} else {mask_tile[i] = {{ MASK_TILE_ZERO }};}}{% endif %}workgroupBarrier();// TILE_K is a small shader constant; the loop updates the named q/o slices in place.{% if not useSubgroups and batchNoSgReduction %}36 unmodified linesif (kj >= minKj && kj < maxKj) {{% if hasMask %}let maskIndex = b * params.maskBatchStride + h * params.maskHeadStride + qClamped * params.maskSeqStride + kj;{% if maskIsBool %}if (attn_mask[maskIndex] != 0u) {s[kk] = {{ score_expr("part") }};} else {s[kk] = MASK_NEG;}{% else %}s[kk] = {{ score_expr("part") }} + f32(attn_mask[maskIndex]);{% endif %}{% else %}s[kk] = {{ score_expr("part") }};{% endif %}30 unmodified lineslet sc = partv[{{ qi }}];if (kj >= {{ qn("minKj", qi) }} && kj < {{ qn("maxKj", qi) }}) {{% if hasMask %}{% if STAGE_MASK %}let maskValue = mask_tile[(qSub + {{ qi }}u) * TILE_K + kk];{% else %}// Broadcast-strided mask address (a 0 stride collapses that axis; rank-2// [q, k] masks set batch/head strides to 0). {{ qn("qClamped", qi) }} keeps the seq index// in-bounds for padding queries in the last tile (their output is dropped).let maskIndex = b * params.maskBatchStride + h * params.maskHeadStride + {{ qn("qClamped", qi) }} * params.maskSeqStride + kj;{% endif %}{% if maskIsKeyKeep %}// A broadcast key mask uses 1 for a retained key and 0 for padding.{{ qn("s", qi) }}[kk] = {{ score_expr("sc") }} + (1.0 - f32({{ MASK_ELEMENT }})) * MASK_NEG;{% elif maskIsBool %}// Logical bool: a rejected key contributes no softmax mass. Leaving// the initialized NEG_INF sentinel in place makes a fully masked row// land on the zero-denominator output guard below.if ({{ MASK_ELEMENT }} != 0u) {{{ qn("s", qi) }}[kk] = {{ score_expr("sc") }};} else {{{ qn("s", qi) }}[kk] = MASK_NEG;}{% else %}{{ qn("s", qi) }}[kk] = {{ score_expr("sc") }} + {{ MASK_ADDITIVE }};{% endif %}{% else %}{{ qn("s", qi) }}[kk] = {{ score_expr("sc") }};{% endif %}31 unmodified lines{% endif %}if (kj >= minKj && kj < maxKj) {{% if hasMask %}{% if STAGE_MASK %}let maskValue = mask_tile[qSub * TILE_K + kk];{% else %}// Broadcast-strided mask address (a 0 stride collapses that axis; rank-2// [q, k] masks set batch/head strides to 0). qClamped keeps the seq index// in-bounds for padding queries in the last tile (their output is dropped).let maskIndex = b * params.maskBatchStride + h * params.maskHeadStride + qClamped * params.maskSeqStride + kj;{% endif %}{% if maskIsKeyKeep %}// A broadcast key mask uses 1 for a retained key and 0 for padding.s[kk] = {{ score_expr("part") }} + (1.0 - f32({{ MASK_ELEMENT }})) * MASK_NEG;{% elif maskIsBool %}// Logical bool: a rejected key contributes no softmax mass. Leaving// the initialized NEG_INF sentinel in place makes a fully masked row// land on the zero-denominator output guard below.if ({{ MASK_ELEMENT }} != 0u) {s[kk] = {{ score_expr("part") }};} else {s[kk] = MASK_NEG;}{% else %}s[kk] = {{ score_expr("part") }} + {{ MASK_ADDITIVE }};{% endif %}{% else %}s[kk] = {{ score_expr("part") }};{% endif %}1 unmodified line}{% endif %}// Per-thread online softmax over the tile. s[kk] is reused to hold the// exponentiated probabilities for the PV accumulation below.{% for qi in range(QPL) %}var {{ qn("tileMax", qi) }}: f32 = {{ qn("s", qi) }}[0];for (var kk: u32 = 1u; kk < TILE_K; kk = kk + 1u) {{{ qn("tileMax", qi) }} = max({{ qn("tileMax", qi) }}, {{ qn("s", qi) }}[kk]);}let {{ qn("newMax", qi) }} = max({{ qn("m", qi) }}, {{ qn("tileMax", qi) }});let {{ qn("corr", qi) }} = select(exp({{ qn("m", qi) }} - {{ qn("newMax", qi) }}), 0.0, {{ qn("m", qi) }} == NEG_INF);var {{ qn("pSum", qi) }}: f32 = 0.0;for (var kk: u32 = 0u; kk < TILE_K; kk = kk + 1u) {let pk = select(0.0, exp({{ qn("s", qi) }}[kk] - {{ qn("newMax", qi) }}), {{ qn("s", qi) }}[kk] != NEG_INF);{{ qn("s", qi) }}[kk] = pk;{{ qn("pSum", qi) }} = {{ qn("pSum", qi) }} + pk;}{{ qn("l", qi) }} = {{ qn("l", qi) }} * {{ qn("corr", qi) }} + {{ qn("pSum", qi) }};{{ qn("m", qi) }} = {{ qn("newMax", qi) }};{% endfor %}// A boundary tile can address V rows outside a query's attended range, and// a dynamic-row producer may leave those rows unwritten. Because 0 * NaN is// NaN, the guarded loop selects the V operand away for range-excluded keys.52 unmodified lines{% for qi in range(QPL) %}if ({{ qn("qValid", qi) }}) {let outBase4 = (b * {{ QSEQ }} + {{ qn("qIdx", qi) }}) * Q_HIDDEN_V4 + h * HEAD_DIM_V4 + lane8 * SLICE;{% if hasHeadSink %}// The head sink is a learned logit that competes with the keys but carries// no value, so it enters the denominator only and the weighted sum above is// untouched. Renormalizing against max({{ qn("m", qi) }}, sink) keeps the exponentials in// range when the sink dominates a fully-masked row.let sink = f32(head_sink[h]);let finalM = max({{ qn("m", qi) }}, sink);let accScale = exp({{ qn("m", qi) }} - finalM);let inv = accScale / (exp(sink - finalM) + {{ qn("l", qi) }} * accScale);{% else %}// {{ qn("l", qi) }} == 0 means this query had no probability-bearing key: either its// causal/window range is empty or its logical bool mask rejects every key.// Emit 0 rather than 0/0. Other attention paths enforce the same empty-row// contract by selecting on positive global mass.let inv = select(0.0, 1.0 / {{ qn("l", qi) }}, {{ qn("l", qi) }} > 0.0);{% endif %}{% for c in range(SLICE_COUNT) %}{{ OUTPUT }}[outBase4 + {{ c }}u] = vec4<{{ scalar }}>({{ attention_value(c, qi) }});{% endfor %}1234567891011121314151617181920212223242526273 unmodified lines31323334353637383940414243444546474849505152535455565758596013 unmodified lines747576777879807 unmodified lines888990919293949596976 unmodified lines10410510610710810911011111211311411511611711811912041 unmodified lines16216316416516616731 unmodified lines19920020120220320420520620720820921021136 unmodified lines24824925025125225325430 unmodified lines28528628728828929029129229329429531 unmodified lines3273283293303313323333343353363371 unmodified line33934034134234334434552 unmodified lines398399400401402403404405406407408{% set QSEQ = "params.qSeq" %}{% set KVSEQ = "params.kvSeq" %}{% set IS_CAUSAL = "params.isCausal" %}{% set Q_STRIDE = "Q_HIDDEN_V4" %}{% set QUERY = "query" %}{% set KEY = "key" %}{% set VALUE = "value" %}{% set OUTPUT = "output" %}{% if useSubgroups is not defined %}{% set useSubgroups = true %}{% endif %}{% if batchNoSgReduction is not defined %}{% set batchNoSgReduction = false %}{% endif %}{% if hasMask is not defined %}{% set hasMask = false %}{% endif %}{% set Q_HIDDEN = qHidden | default(0) %}// A windowed cache binds a fixed CAPACITY but keeps only the most recent// min(total, capacity) rows resident. params.kvSeq then names the physical row// count, which is still the right batch stride but the wrong attention bound, so// the bounds read kvActive instead. Modes without seqlens use `params.kvSeq`// in both roles.{% if hasKeyLimit is not defined %}{% set hasKeyLimit = false %}{% endif %}{% if hasKeyLimit %}{% set KVA = "select(params.kvSeq, min(params.keyLimit, params.kvSeq), params.keyLimit > 0u)" %}{% else %}{% set KVA = KVSEQ %}{% endif %}{% macro score_expr(part) %}{{ part }} * SCALE{% endmacro %}{% if useSubgroups %}enable subgroups;{% endif %}3 unmodified lines// workgroup storage; values widen to f32 when read. Other specializations stage// them as f32. Score and weighted-value accumulation remain in f32 throughout.{% set STAGE_MASK = hasMask and useSubgroups and stageMask is defined and stageMask %}{% set MASK_ADDITIVE = "maskValue" if STAGE_MASK else "f32(attn_mask[maskIndex])" %}{% set SLICE_COUNT = ((headDimV4 / LPQ) | int) %}{% set QPL = (2 if (not usesF16 and SLICE_COUNT <= 4 and TILE_K <= 16 and TILE_Q % 2 == 0) else 1) if useSubgroups else 1 %}{% macro qn(name, qi) %}{{ name }}{% if qi > 0 %}_q{{ qi }}{% endif %}{% endmacro %}{% macro emit_tile_softmax() %}// Per-thread online softmax over the tile. s[kk] is reused to hold the// exponentiated probabilities for the PV accumulation below.{% for qi in range(QPL) %}var {{ qn("tileMax", qi) }}: f32 = {{ qn("s", qi) }}[0];for (var kk: u32 = 1u; kk < TILE_K; kk = kk + 1u) {{{ qn("tileMax", qi) }} = max({{ qn("tileMax", qi) }}, {{ qn("s", qi) }}[kk]);}let {{ qn("newMax", qi) }} = max({{ qn("m", qi) }}, {{ qn("tileMax", qi) }});let {{ qn("corr", qi) }} = select(exp({{ qn("m", qi) }} - {{ qn("newMax", qi) }}), 0.0, {{ qn("m", qi) }} == NEG_INF);var {{ qn("pSum", qi) }}: f32 = 0.0;for (var kk: u32 = 0u; kk < TILE_K; kk = kk + 1u) {let pk = select(0.0, exp({{ qn("s", qi) }}[kk] - {{ qn("newMax", qi) }}), {{ qn("s", qi) }}[kk] != NEG_INF);{{ qn("s", qi) }}[kk] = pk;{{ qn("pSum", qi) }} = {{ qn("pSum", qi) }} + pk;}{{ qn("l", qi) }} = {{ qn("l", qi) }} * {{ qn("corr", qi) }} + {{ qn("pSum", qi) }};{{ qn("m", qi) }} = {{ qn("newMax", qi) }};{% endfor %}{% endmacro %}// Tiled flash prefill attention with configurable-width query clusters for// token-major [batch, seq, heads*headDim] attention ops. Each workgroup covers13 unmodified linesconst HEAD_DIM: u32 = {{ headDim }}u;const HEAD_DIM_V4: u32 = {{ headDimV4 }}u;const Q_HIDDEN_V4: u32 = {{ qHiddenV4 }}u; // bsh token-major hidden strideconst KV_HIDDEN_V4: u32 = {{ kvHiddenV4 }}u;const Q_HEADS: u32 = {{ qNumHeads }}u;const KV_HEADS: u32 = {{ kvNumHeads }}u;const TILE_Q: u32 = {{ TILE_Q }}u;7 unmodified lines{% else %}const WG: u32 = TILE_Q * LPQ;{% endif %}const NEG_INF: f32 = -3.4028234663852886e38;var<workgroup> k_tile: array<vec4<{{ ST }}>, TILE_K * (HEAD_DIM / 4u)>;var<workgroup> v_tile: array<vec4<{{ ST }}>, TILE_K * (HEAD_DIM / 4u)>;{% if not useSubgroups %}{% if batchNoSgReduction %}// No-subgroups cluster reduction scratch for a whole K tile. Staging every6 unmodified lines{% endif %}{% endif %}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}{% if hasBias %}{% set BW = "" %}{% set BC = "" %}fn load_bias4(base: u32, d4: u32) -> vec4<f32> {let offset = base + d4 * 4u;return vec4<f32>({{ BW }}bias[offset]{{ BC }}, {{ BW }}bias[offset + 1u]{{ BC }}, {{ BW }}bias[offset + 2u]{{ BC }}, {{ BW }}bias[offset + 3u]{{ BC }});}{% endif %}41 unmodified linesvar {{ qn("m", qi) }}: f32 = NEG_INF;var {{ qn("l", qi) }}: f32 = 0.0;{% endfor %}// Causal ceiling per query; the key loop runs over the workgroup's union range// (uniform trip count), masking out-of-range (query, key) pairs.// Upper-left causal and/or sliding-window bounds. Query qIdx sits at31 unmodified lineslet kj = kStart + slot;if (kj < wgEnd) {let base4 = kvBatch4 + kj * KV_HIDDEN_V4 + d4;k_tile[i] = vec4<{{ ST }}>({{ KEY }}[base4]);v_tile[i] = vec4<{{ ST }}>({{ VALUE }}[base4]);} else {k_tile[i] = vec4<{{ ST }}>(0.0);v_tile[i] = vec4<{{ ST }}>(0.0);}}workgroupBarrier();// TILE_K is a small shader constant; the loop updates the named q/o slices in place.{% if not useSubgroups and batchNoSgReduction %}36 unmodified linesif (kj >= minKj && kj < maxKj) {{% if hasMask %}let maskIndex = b * params.maskBatchStride + h * params.maskHeadStride + qClamped * params.maskSeqStride + kj;s[kk] = {{ score_expr("part") }} + f32(attn_mask[maskIndex]);{% else %}s[kk] = {{ score_expr("part") }};{% endif %}30 unmodified lineslet sc = partv[{{ qi }}];if (kj >= {{ qn("minKj", qi) }} && kj < {{ qn("maxKj", qi) }}) {{% if hasMask %}// Broadcast-strided mask address (a 0 stride collapses that axis; rank-2// [q, k] masks set batch/head strides to 0). {{ qn("qClamped", qi) }} keeps the seq index// in-bounds for padding queries in the last tile (their output is dropped).let maskIndex = b * params.maskBatchStride + h * params.maskHeadStride + {{ qn("qClamped", qi) }} * params.maskSeqStride + kj;{{ qn("s", qi) }}[kk] = {{ score_expr("sc") }} + {{ MASK_ADDITIVE }};{% else %}{{ qn("s", qi) }}[kk] = {{ score_expr("sc") }};{% endif %}31 unmodified lines{% endif %}if (kj >= minKj && kj < maxKj) {{% if hasMask %}// Broadcast-strided mask address (a 0 stride collapses that axis; rank-2// [q, k] masks set batch/head strides to 0). qClamped keeps the seq index// in-bounds for padding queries in the last tile (their output is dropped).let maskIndex = b * params.maskBatchStride + h * params.maskHeadStride + qClamped * params.maskSeqStride + kj;s[kk] = {{ score_expr("part") }} + {{ MASK_ADDITIVE }};{% else %}s[kk] = {{ score_expr("part") }};{% endif %}1 unmodified line}{% endif %}{{ emit_tile_softmax() }}// A boundary tile can address V rows outside a query's attended range, and// a dynamic-row producer may leave those rows unwritten. Because 0 * NaN is// NaN, the guarded loop selects the V operand away for range-excluded keys.52 unmodified lines{% for qi in range(QPL) %}if ({{ qn("qValid", qi) }}) {let outBase4 = (b * {{ QSEQ }} + {{ qn("qIdx", qi) }}) * Q_HIDDEN_V4 + h * HEAD_DIM_V4 + lane8 * SLICE;// {{ qn("l", qi) }} == 0 means this query had no probability-bearing key: either its// causal/window range is empty or its logical bool mask rejects every key.// Emit 0 rather than 0/0. Other attention paths enforce the same empty-row// contract by selecting on positive global mass.let inv = select(0.0, 1.0 / {{ qn("l", qi) }}, {{ qn("l", qi) }} > 0.0);{% for c in range(SLICE_COUNT) %}{{ OUTPUT }}[outBase4 + {{ c }}u] = vec4<{{ scalar }}>({{ attention_value(c, qi) }});{% endfor %}
12345 unmodified lines101112131415161718192025 unmodified lines46474849505152535455565758596038 unmodified lines9910010110210310410510610785 unmodified lines19319419519619719810 unmodified lines209210211212213214215216217218219220221222223224225226227228229// Register-resident flash prefill uses one 32-lane subgroup per workgroup. Each// lane owns a query row and keeps its Q slice and f32 output in registers. Lanes// cooperatively load K/V and broadcast them with subgroupShuffle, so each query// computes q·k and p·v without cross-lane reductions or workgroup storage.5 unmodified lines{% set ST = "f16" if usesF16 else "f32" %}{% set components = ["x", "y", "z", "w"] %}{% set USE_SUBGROUPS = useSubgroups if useSubgroups is defined else true %}{% set Q_STEP = qStep if qStep is defined else 32 %}{% set CAUSAL = false if (hasCausal is defined and not hasCausal) else true %}{% if USE_SUBGROUPS %}enable subgroups;{% endif %}{{ env.wgsl.resourceDeclarations }}const HEAD_DIM_V4: u32 = {{ headDimV4 }}u;25 unmodified linesreturn inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}{% if hasBias %}fn load_bias4(base: u32, d4: u32) -> vec4<f32> {let offset = base + d4 * 4u;return vec4<f32>(bias[offset], bias[offset + 1u], bias[offset + 2u], bias[offset + 3u]);}{% endif %}@compute @workgroup_size({{ Q_STEP }}, 1, 1)fn main(@builtin(workgroup_id) wg: vec3<u32>,@builtin(local_invocation_id) lid: vec3<u32>{% if USE_SUBGROUPS %},38 unmodified lines// so every lane shares a uniform trip count (subgroup ops stay reconverged);// each lane masks its own keys past myMaxKj to NEG_INF.{% if CAUSAL %}let lastQ = min(wg.x * Q_STEP + Q_STEP - 1u, params.qSeq - 1u);let kvEnd = select(params.kvSeq, min(lastQ + 1u, params.kvSeq), params.isCausal != 0u);let myMaxKj = select(params.kvSeq, min(qi + 1u, params.kvSeq), params.isCausal != 0u);{% else %}let kvEnd = params.kvSeq;let myMaxKj = params.kvSeq;85 unmodified lines{% endfor %}previous_max = new_max;previous_denom = denom;for (var d4: u32 = 0u; d4 < HEAD_DIM_V4; d4 = d4 + 1u) {{% if USE_SUBGROUPS %}10 unmodified lines}{% endif %}{% endif %}var acc: vec4<f32> = vec4<f32>(0.0);{% for g in range(qkGroups) %}{% for lane in range(4) %}{% if USE_SUBGROUPS %}{% if g < 8 %}acc = acc + vec4<f32>(subgroupShuffle(v_local0, {{ g * 4 + lane }}u)) * qk{{ g }}.{{ components[lane] }};{% else %}acc = acc + vec4<f32>(subgroupShuffle(v_local1, {{ (g - 8) * 4 + lane }}u)) * qk{{ g }}.{{ components[lane] }};{% endif %}{% else %}acc = acc + vec4<f32>(valueTile[d4 * K_STEP + {{ g * 4 + lane }}u]) * qk{{ g }}.{{ components[lane] }};{% endif %}{% endfor %}{% endfor %}o_tile[d4] = o_tile[d4] * vec4<f32>(o_ratio) + acc;}{% if not USE_SUBGROUPS %}workgroupBarrier();123455 unmodified lines1112131415161718192021222324252625 unmodified lines5253545556575859606162636465666738 unmodified lines10610710810911011111211311411511611711811985 unmodified lines20520620720820921021121221321421521621710 unmodified lines228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264// Register-resident flash prefill uses one 32-lane subgroup per workgroup (fixed// by the adapter, or pinned where the adapter can compile exactly 32 lanes). Each// lane owns a query row and keeps its Q slice and f32 output in registers. Lanes// cooperatively load K/V and broadcast them with subgroupShuffle, so each query// computes q·k and p·v without cross-lane reductions or workgroup storage.5 unmodified lines{% set ST = "f16" if usesF16 else "f32" %}{% set components = ["x", "y", "z", "w"] %}{% set USE_SUBGROUPS = useSubgroups if useSubgroups is defined else true %}{% set PIN_SUBGROUP_32 = USE_SUBGROUPS and pinSubgroupSize32 is defined and pinSubgroupSize32 %}{% set Q_STEP = qStep if qStep is defined else 32 %}{% set CAUSAL = false if (hasCausal is defined and not hasCausal) else true %}{% set qHidden = qHidden | default(0) %}{% if USE_SUBGROUPS %}enable subgroups;{% endif %}{% if PIN_SUBGROUP_32 %}enable subgroup_size_control;{% endif %}{{ env.wgsl.resourceDeclarations }}const HEAD_DIM_V4: u32 = {{ headDimV4 }}u;25 unmodified linesreturn inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}{% if hasBias %}{% set BW = "" %}{% set BC = "" %}fn load_bias4(base: u32, d4: u32) -> vec4<f32> {let offset = base + d4 * 4u;return vec4<f32>({{ BW }}bias[offset]{{ BC }}, {{ BW }}bias[offset + 1u]{{ BC }}, {{ BW }}bias[offset + 2u]{{ BC }}, {{ BW }}bias[offset + 3u]{{ BC }});}{% endif %}@compute @workgroup_size({{ Q_STEP }}, 1, 1){{ " @subgroup_size(32)" if PIN_SUBGROUP_32 else "" }}fn main(@builtin(workgroup_id) wg: vec3<u32>,@builtin(local_invocation_id) lid: vec3<u32>{% if USE_SUBGROUPS %},38 unmodified lines// so every lane shares a uniform trip count (subgroup ops stay reconverged);// each lane masks its own keys past myMaxKj to NEG_INF.{% if CAUSAL %}{% set FIXED_CAUSAL = false %}{% macro key_ceiling(query) %}{% if FIXED_CAUSAL %}min({{ query }} + 1u, params.kvSeq){% else %}select(params.kvSeq, min({{ query }} + 1u, params.kvSeq), params.isCausal != 0u){% endif %}{% endmacro %}let lastQ = min(wg.x * Q_STEP + Q_STEP - 1u, params.qSeq - 1u);let kvEnd = {{ key_ceiling("lastQ") }};let myMaxKj = {{ key_ceiling("qi") }};{% else %}let kvEnd = params.kvSeq;let myMaxKj = params.kvSeq;85 unmodified lines{% endfor %}previous_max = new_max;previous_denom = denom;{% set PV_HALF = ST == "f16" and not (precisePv | default(false)) %}{% set PV_FLUSH_GROUPS = 4 %}{% if PV_HALF %}{% for g in range(qkGroups) %}let p{{ g }} = vec4<f16>(qk{{ g }});{% endfor %}{% endif %}for (var d4: u32 = 0u; d4 < HEAD_DIM_V4; d4 = d4 + 1u) {{% if USE_SUBGROUPS %}10 unmodified lines}{% endif %}{% endif %}{% if PV_HALF %}var acc_f32: vec4<f32> = vec4<f32>(0.0);var acc: vec4<f16> = vec4<f16>(0.0);{% else %}var acc: vec4<f32> = vec4<f32>(0.0);{% endif %}{% for g in range(qkGroups) %}{% for lane in range(4) %}{% if not USE_SUBGROUPS %}{% set pvValue = "valueTile[d4 * K_STEP + " ~ (g * 4 + lane) ~ "u]" %}{% elif g < 8 %}{% set pvValue = "subgroupShuffle(v_local0, " ~ (g * 4 + lane) ~ "u)" %}{% else %}{% set pvValue = "subgroupShuffle(v_local1, " ~ ((g - 8) * 4 + lane) ~ "u)" %}{% endif %}{% if PV_HALF %}acc = fma({{ pvValue }}, vec4<f16>(p{{ g }}.{{ components[lane] }}), acc);{% else %}acc = acc + vec4<f32>({{ pvValue }}) * qk{{ g }}.{{ components[lane] }};{% endif %}{% endfor %}{% if PV_HALF and (g + 1) % PV_FLUSH_GROUPS == 0 %}acc_f32 = acc_f32 + vec4<f32>(acc);acc = vec4<f16>(0.0);{% endif %}{% endfor %}{% if PV_HALF %}o_tile[d4] = o_tile[d4] * vec4<f32>(o_ratio) + acc_f32;{% else %}o_tile[d4] = o_tile[d4] * vec4<f32>(o_ratio) + acc;{% endif %}}{% if not USE_SUBGROUPS %}workgroupBarrier();
12335 unmodified lines394041424344452 unmodified lines48495051525354{{ env.wgsl.resourceDeclarations }}// Register-blocked normalized-scores @ V GEMM with BHSD and BSH specializations.35 unmodified lines{% endif %}fn v_at(b: u32, h: u32, k: u32, d: u32) -> f32 {return value[(b * params.kvSeq + k) * KV_HIDDEN + h * HEAD_DIM + d];}fn store_y(b: u32, h: u32, q: u32, d: u32, result: f32) {2 unmodified lines{% else %}let biased = result;{% endif %}output[(b * params.qSeq + q) * Q_HIDDEN + h * HEAD_DIM + d] = biased;}@compute @workgroup_size(WG_DIM, WG_DIM, 1)123435 unmodified lines404142434445462 unmodified lines49505152535455{% set STORAGE_F16 = false %}{{ env.wgsl.resourceDeclarations }}// Register-blocked normalized-scores @ V GEMM with BHSD and BSH specializations.35 unmodified lines{% endif %}fn v_at(b: u32, h: u32, k: u32, d: u32) -> f32 {return {{ "f32(" if STORAGE_F16 else "" }}value[(b * params.kvSeq + k) * KV_HIDDEN + h * HEAD_DIM + d]{{ ")" if STORAGE_F16 else "" }};}fn store_y(b: u32, h: u32, q: u32, d: u32, result: f32) {2 unmodified lines{% else %}let biased = result;{% endif %}output[(b * params.qSeq + q) * Q_HIDDEN + h * HEAD_DIM + d] = {{ "f16(" if STORAGE_F16 else "" }}biased{{ ")" if STORAGE_F16 else "" }};}@compute @workgroup_size(WG_DIM, WG_DIM, 1)
12 unmodified lines13141516171822 unmodified lines41424344454647484950515253545556575859606112 unmodified lines// computing the denominator there would double the exp count. Fold maxima and// leave the denominator to the apply pass, which sees every row element anyway.{% set MAX_ONLY = maxOnly is defined and maxOnly %}{{ env.wgsl.resourceDeclarations }}const SLOTS: u32 = {{ statSlots }}u;22 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}{%- endif %}{% if stableUsage == "all" %}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}{%- endif %}@compute @workgroup_size(WG, 1, 1)fn main(@builtin(global_invocation_id) gid: vec3<u32>) {let row = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * WG;if (row >= params.rows) { return; }// `row` already runs over (batch, head, query) together, and the partial// layout puts that same product one axis out from the slot, so the stride12 unmodified lines13141516171819202122232422 unmodified lines4748495051525354555657585960616263646512 unmodified lines// computing the denominator there would double the exp count. Fold maxima and// leave the denominator to the apply pass, which sees every row element anyway.{% set MAX_ONLY = maxOnly is defined and maxOnly %}{% macro flat_index_2d(workgroupSize, name="i", bound="params.count", guardInline=false) %}{% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}// 2D-folded flat index: gid.y carries the high bits past the dispatch's// per-axis workgroup fold width.let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};if ({{ name }} >= {{ bound }}) { return; }{% endmacro %}{{ env.wgsl.resourceDeclarations }}const SLOTS: u32 = {{ statSlots }}u;22 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}{% endif %}{% if stableUsage == "all" %}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}{% endif %}@compute @workgroup_size(WG, 1, 1)fn main(@builtin(global_invocation_id) gid: vec3<u32>) {{{ flat_index_2d("WG", "row", "params.rows", guardInline=true) }}// `row` already runs over (batch, head, query) together, and the partial// layout puts that same product one axis out from the slot, so the stride
1234 unmodified lines89101112131415161718192021222324252627282915 unmodified lines454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889901 unmodified line929394959697988 unmodified lines10710810911011111211311411511611711811912012112212328 unmodified lines1521531541551561571581591601611621631641651661671681691703 unmodified lines1741751761771781791808 unmodified lines1891901911921931941951961971981992002012022032042052062072082092102112122132142152168 unmodified lines225226227228229230231{{ env.wgsl.resourceDeclarations }}// f32 prefill score GEMM with BHSD and packed BSH specializations. It computes4 unmodified lines// for TM * TN * 4 fused multiply-adds. K keeps a one-word shared stride// padding, which changes the transposed workgroup-memory stride and reduces// bank-conflict risk on banked implementations.{% if scalarAccumulators is not defined %}{% set scalarAccumulators = false %}{% endif %}{% if maskIsKeyKeep is not defined %}{% set maskIsKeyKeep = false %}{% endif %}{% if scalarAccumulators %}{% set TM_JINJA = (materializedQueryTile / materializedWorkgroupDim)|int %}{% set TN_JINJA = (materializedKeyTile / materializedWorkgroupDim)|int %}{% endif %}{% set EMIT_ROW_STATS = emitRowStats is defined and emitRowStats %}const Q_HEADS: u32 = {{ qNumHeads }}u;const KV_HEADS: u32 = {{ kvNumHeads }}u;const HEAD_DIM: u32 = {{ headDim }}u;const Q_HIDDEN: u32 = Q_HEADS * HEAD_DIM;const KV_HIDDEN: u32 = KV_HEADS * HEAD_DIM;{% if maskIsKeyKeep %}const NEG_INF: f32 = -3.4028234663852886e38;{% endif %}const BK: u32 = {{ materializedInnerTile }}u;const BM: u32 = {{ materializedQueryTile }}u;15 unmodified linesvar<workgroup> statScratch: array<f32, WG_THREADS>;{% endif %}{% if EMIT_ROW_STATS %}{% set stableHelperUsage = "constant" %}{% set stableUsage = stableHelperUsage if stableHelperUsage is defined else "all" %}// FLT_MAX, not -inf, as the online (m, d) accumulator init: merges must keep// `m - m` finite so an empty lane / all--inf row contributes the exact// accumulator identity (m, d) = (-FLT_MAX, 0). Operator epilogues interpret// a zero final denominator according to their public semantics. Using -inf// here changes +inf-row behavior.const FLT_MAX: f32 = 3.4028234663852886e38;{% if stableUsage != "constant" %}fn is_finite_f32(value: f32) -> bool {return select(false, value <= FLT_MAX, value >= -FLT_MAX);}// x - m that is exactly 0 when x equals a finite m, so exp(shifted) == 1// exactly at the row max. `x - x` on an infinite max is a legal fast-math// fold to 0, which would silently turn +inf rows finite — the explicit// equality test keeps the NaN propagation of the serial kernels.fn shifted_value(value: f32, maxValue: f32) -> f32 {let equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}{%- endif %}{% if stableUsage == "all" %}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}{%- endif %}{% endif %}{% if ATTN_SCALE_DIM is not defined %}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}{% endif %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}fn q_at(b: u32, h: u32, q: u32, d: u32) -> f32 {var value = query[(b * params.qSeq + q) * Q_HIDDEN + h * HEAD_DIM + d];{% if hasBias %}value = value + bias[h * HEAD_DIM + d];{% endif %}1 unmodified line}fn k_at(b: u32, h: u32, k: u32, d: u32) -> f32 {return key[(b * params.kvSeq + k) * KV_HIDDEN + h * HEAD_DIM + d];}@compute @workgroup_size(WG_DIM, WG_DIM, 1)8 unmodified lineslet kBase = wg.x * BN;let li = lid.y * WG_DIM + lid.x;{% if scalarAccumulators %}// Keep every register-tile cell statically addressable.{% for i in range(TM_JINJA) %}{% for j in range(TN_JINJA) %}var acc{{ i }}_{{ j }}: f32 = 0.0;{% endfor %}{% endfor %}{% else %}var acc: array<f32, TM * TN>;for (var i = 0u; i < TM * TN; i = i + 1u) { acc[i] = 0.0; }{% endif %}for (var dBase = 0u; dBase < HEAD_DIM; dBase = dBase + BK) {// Both source tiles are loaded in their physical row-major direction.28 unmodified lineslet qr0 = lid.y * TM;let kc0 = lid.x * TN;for (var dv = 0u; dv < D_VECS; dv = dv + 1u) {{% if scalarAccumulators %}{% for i in range(TM_JINJA) %}let qv{{ i }} = tileQ[qr0 + {{ i }}u][dv];{% endfor %}{% for j in range(TN_JINJA) %}let kv{{ j }} = tileK[kc0 + {{ j }}u][dv];{% endfor %}{% for i in range(TM_JINJA) %}{% for j in range(TN_JINJA) %}acc{{ i }}_{{ j }} = acc{{ i }}_{{ j }} + dot(qv{{ i }}, kv{{ j }});{% endfor %}{% endfor %}{% else %}var qv: array<vec4<f32>, TM>;var kv: array<vec4<f32>, TN>;for (var i = 0u; i < TM; i = i + 1u) { qv[i] = tileQ[qr0 + i][dv]; }3 unmodified linesacc[i * TN + j] = acc[i * TN + j] + dot(qv[i], kv[j]);}}{% endif %}}workgroupBarrier();}8 unmodified linesvar rowM: array<f32, TM>;for (var i = 0u; i < TM; i = i + 1u) { rowM[i] = -FLT_MAX; }{% endif %}{% if scalarAccumulators %}{% for i in range(TM_JINJA) %}{let qi = qi0 + {{ i }}u;if (qi < params.qSeq) {{% for j in range(TN_JINJA) %}let ki{{ j }} = ki0 + {{ j }}u;if (ki{{ j }} < params.kvSeq) {var score = acc{{ i }}_{{ j }} * scale;{% if maskIsKeyKeep %}score = score + (1.0 - f32(attn_mask[b * params.kvSeq + ki{{ j }}])) * NEG_INF;{% endif %}scores[scoreBase + qi * params.kvSeq + ki{{ j }}] = score;{% if EMIT_ROW_STATS %}rowM[{{ i }}u] = max(rowM[{{ i }}u], score);{% endif %}}{% endfor %}}}{% endfor %}{% else %}for (var i = 0u; i < TM; i = i + 1u) {let qi = qi0 + i;if (qi >= params.qSeq) { continue; }8 unmodified lines}}}{% endif %}{% if EMIT_ROW_STATS %}// Fold each row's partial (m, d) across the WG_DIM threads that own its columns. Every lane12344 unmodified lines91011121314151617181920212215 unmodified lines38394041424344454647484950515253545556571 unmodified line596061626364658 unmodified lines747576777879808128 unmodified lines1101111121131141153 unmodified lines1191201211221231248 unmodified lines1331341351361371388 unmodified lines147148149150151152{% set STORAGE_F16 = false %}{{ env.wgsl.resourceDeclarations }}// f32 prefill score GEMM with BHSD and packed BSH specializations. It computes4 unmodified lines// for TM * TN * 4 fused multiply-adds. K keeps a one-word shared stride// padding, which changes the transposed workgroup-memory stride and reduces// bank-conflict risk on banked implementations.{% set EMIT_ROW_STATS = emitRowStats is defined and emitRowStats %}{% set statSlots = statSlots | default(0) %}const Q_HEADS: u32 = {{ qNumHeads }}u;const KV_HEADS: u32 = {{ kvNumHeads }}u;const HEAD_DIM: u32 = {{ headDim }}u;const Q_HIDDEN: u32 = Q_HEADS * HEAD_DIM;const KV_HIDDEN: u32 = KV_HEADS * HEAD_DIM;const BK: u32 = {{ materializedInnerTile }}u;const BM: u32 = {{ materializedQueryTile }}u;15 unmodified linesvar<workgroup> statScratch: array<f32, WG_THREADS>;{% endif %}{% if EMIT_ROW_STATS %}// FLT_MAX, not -inf, as the online (m, d) accumulator init: merges must keep// `m - m` finite so an empty lane / all--inf row contributes the exact// accumulator identity (m, d) = (-FLT_MAX, 0). Operator epilogues interpret// a zero final denominator according to their public semantics. Using -inf// here changes +inf-row behavior.const FLT_MAX: f32 = 3.4028234663852886e38;{% endif %}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}fn q_at(b: u32, h: u32, q: u32, d: u32) -> f32 {var value = {{ "f32(" if STORAGE_F16 else "" }}query[(b * params.qSeq + q) * Q_HIDDEN + h * HEAD_DIM + d]{{ ")" if STORAGE_F16 else "" }};{% if hasBias %}value = value + bias[h * HEAD_DIM + d];{% endif %}1 unmodified line}fn k_at(b: u32, h: u32, k: u32, d: u32) -> f32 {return {{ "f32(" if STORAGE_F16 else "" }}key[(b * params.kvSeq + k) * KV_HIDDEN + h * HEAD_DIM + d]{{ ")" if STORAGE_F16 else "" }};}@compute @workgroup_size(WG_DIM, WG_DIM, 1)8 unmodified lineslet kBase = wg.x * BN;let li = lid.y * WG_DIM + lid.x;var acc: array<f32, TM * TN>;for (var i = 0u; i < TM * TN; i = i + 1u) { acc[i] = 0.0; }for (var dBase = 0u; dBase < HEAD_DIM; dBase = dBase + BK) {// Both source tiles are loaded in their physical row-major direction.28 unmodified lineslet qr0 = lid.y * TM;let kc0 = lid.x * TN;for (var dv = 0u; dv < D_VECS; dv = dv + 1u) {var qv: array<vec4<f32>, TM>;var kv: array<vec4<f32>, TN>;for (var i = 0u; i < TM; i = i + 1u) { qv[i] = tileQ[qr0 + i][dv]; }3 unmodified linesacc[i * TN + j] = acc[i * TN + j] + dot(qv[i], kv[j]);}}}workgroupBarrier();}8 unmodified linesvar rowM: array<f32, TM>;for (var i = 0u; i < TM; i = i + 1u) { rowM[i] = -FLT_MAX; }{% endif %}for (var i = 0u; i < TM; i = i + 1u) {let qi = qi0 + i;if (qi >= params.qSeq) { continue; }8 unmodified lines}}}{% if EMIT_ROW_STATS %}// Fold each row's partial (m, d) across the WG_DIM threads that own its columns. Every lane
12345671 unmodified line91011121314153 unmodified lines1920212223242526272829303132333435363738394041424344451 unmodified line47484950515253545556575859606146 unmodified lines1081091101111121131141151161171181191201217 unmodified lines12913013113213313413513613713813914014114230 unmodified lines1731741751761771781791801811821831841851861871881891901911921931941951961971981992002012023 unmodified lines20620720820921021121243 unmodified lines2562572582592602612628 unmodified lines27127227327427527627727827928028128228328428528628728828929012 unmodified lines3033043053063073083093103113123133143158 unmodified lines32432532632732832933033133233333433546 unmodified lines38238338438538638738859 unmodified lines4484494504514524534547 unmodified lines46246346446546646746821 unmodified lines490491492493494495496{% set MT = "f16" if (operandF16 is defined and operandF16) else "f32" %}{% if MT == "f16" %}enable f16;{% endif %}enable subgroups;{% if pinSubgroupSize32 %}enable subgroup_size_control;1 unmodified lineenable chromium_experimental_subgroup_matrix;diagnostic(off, chromium.subgroup_matrix_uniformity);{{ env.wgsl.resourceDeclarations }}{% set layout = layout | default("bsh") %}3 unmodified lines{% macro kv_index(seq, d) %}{% if kvHeadMajor %}((b * KV_HEADS + h_kv) * params.kvSeq + {{ seq }}) * HEAD_DIM + {{ d }}{% else %}(b * params.kvSeq + {{ seq }}) * KV_HIDDEN + h_kv * HEAD_DIM + {{ d }}{% endif %}{% endmacro %}{% set OUT_ROW_STRIDE = "HEAD_DIM" if headMajor else "HIDDEN" %}{% set KV_ROW_STRIDE = "HEAD_DIM" if kvHeadMajor else "KV_HIDDEN" %}{% set scorePhase = phase == "score" %}{% set FUSED_SOFTMAX = fusedSoftmax is defined and fusedSoftmax %}{% set PRIVATE_ROW_STATS = FUSED_SOFTMAX and (materializedSgmatPrivateRowStats is defined and materializedSgmatPrivateRowStats) %}{% macro score_value(index, guard) %}{% if FUSED_SOFTMAX %}{% if PRIVATE_ROW_STATS %}select(0.0, exp_shift(scores[{{ index }}], private_softmax_m) / private_softmax_d, {{ guard[1] }}){%- else %}select(0.0, exp_shift(scores[{{ index }}], softmax_m[{{ guard[0] }}]) / softmax_d[{{ guard[0] }}], {{ guard[1] }}){%- endif %}{% else %}select(0.0, scores[{{ index }}], {{ guard[1] }}){%- endif %}{% endmacro %}{% set TILE_M_VALUE = materializedSgmatQueryTile %}{% set TILE_N_VALUE = materializedSgmatKeyTile %}{% set TILE_K_VALUE = materializedSgmatInnerTile %}{% set SUB_ROWS_VALUE = materializedSgmatSubgroupTileRows if materializedSgmatSubgroupTileRows is defined else 16 %}{% set SUB_COLS_VALUE = materializedSgmatSubgroupTileCols if materializedSgmatSubgroupTileCols is defined else 32 %}{% set ROW_BLOCKS = (SUB_ROWS_VALUE / 8)|int %}{% set COL_BLOCKS = (SUB_COLS_VALUE / 8)|int %}{% set SUBGROUP_ROWS = (TILE_M_VALUE / SUB_ROWS_VALUE)|int %}{% set SUBGROUP_COLS = (TILE_N_VALUE / SUB_COLS_VALUE)|int %}{% set SUBGROUP_COUNT = SUBGROUP_ROWS * SUBGROUP_COLS %}1 unmodified line{% if hasBias is not defined %}{% set hasBias = false %}{% endif %}{% set EMIT_ROW_STATS = emitRowStats is defined and emitRowStats %}{% set DIRECT_SCORE_STORE = materializedSgmatDirectScoreStore and not EMIT_ROW_STATS %}{% set DIRECT_APPLY_STORE = materializedSgmatDirectApplyStore and not hasBias and MT == "f32" %}{% set DIRECT_OUTPUT_STORE = DIRECT_SCORE_STORE if scorePhase else DIRECT_APPLY_STORE %}{% set RUNTIME_DIRECT_STORE = (not DIRECT_OUTPUT_STORE)and materializedSgmatRuntimeDirectStoreand (scorePhase or (not hasBias and MT == "f32"))and not EMIT_ROW_STATS %}{% set ANY_DIRECT_STORE = DIRECT_OUTPUT_STORE or RUNTIME_DIRECT_STORE %}{% set SCALE_IN_Q = DIRECT_SCORE_STORE or (RUNTIME_DIRECT_STORE and scorePhase) %}{% macro q_tile_value(index) %}{% if hasBias %}(query[{{ index }}] + bias[h * HEAD_DIM + k]){% else %}query[{{ index }}]{% endif %}{% if SCALE_IN_Q %} * score_scale{% endif %}{% endmacro %}const HEADS: u32 = {{ qNumHeads }}u;{% if kvNumHeads is defined %}46 unmodified linesvar<workgroup> softmax_d: array<f32, {{ TILE_M_VALUE }}>;{% endif %}{% if FUSED_SOFTMAX or EMIT_ROW_STATS %}{% set stableUsage = stableHelperUsage if stableHelperUsage is defined else "all" %}// FLT_MAX, not -inf, as the online (m, d) accumulator init: merges must keep// `m - m` finite so an empty lane / all--inf row contributes the exact// accumulator identity (m, d) = (-FLT_MAX, 0). Operator epilogues interpret// a zero final denominator according to their public semantics. Using -inf// here changes +inf-row behavior.const FLT_MAX: f32 = 3.4028234663852886e38;{% if stableUsage != "constant" %}fn is_finite_f32(value: f32) -> bool {return select(false, value <= FLT_MAX, value >= -FLT_MAX);7 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}{%- endif %}{% if stableUsage == "all" %}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}{%- endif %}{% endif %}@compute @workgroup_size({{ WORKGROUP_THREADS }}, 1, 1){{ " @subgroup_size(32)" if pinSubgroupSize32 else "" }}30 unmodified lineslet inner = params.kvSeq;{% endif %}{% if FUSED_SOFTMAX %}{% if PRIVATE_ROW_STATS %}// In the admitted BM64/BN64/BK32/WG256 loader, four adjacent lanes own the// same query row for every reduction tile. Keep that row's constants private:// this removes both 512 bytes of workgroup storage and the initialization// barrier while preserving the exact exp/divide sequence of the shared-memory// row-stats arm.let private_stat_row =(b * HEADS + h) * params.qSeq + min(m_base + li / 4u, params.qSeq - 1u);let private_softmax_m = rowStats[private_stat_row * 2u];let private_softmax_d = rowStats[private_stat_row * 2u + 1u];{% else %}// One row-stats pair per query row of the tile. A query tail clamps to the last// real row rather than reading past the buffer; those lanes are discarded by the// staging guard anyway, and the clamp keeps the denominator non-zero.for (var r = li; r < TILE_M; r += WORKGROUP_THREADS) {let stat_row = (b * HEADS + h) * params.qSeq + min(m_base + r, params.qSeq - 1u);softmax_m[r] = rowStats[stat_row * 2u];softmax_d[r] = rowStats[stat_row * 2u + 1u];}workgroupBarrier();{% endif %}{% endif %}for (var k_base = 0u; k_base < inner; k_base += TILE_K) {{% if phase == "apply" and not FUSED_SOFTMAX and MT == "f32" %}// Full interior PV tiles can be loaded directly from storage. Query,// reduction, and output-dimension tails use the guarded shared path below.if (3 unmodified lines) {for (var step = 0u; step < TILE_K; step += 8u) {{% for row_block in range(ROW_BLOCKS) %}let score_offset{{ row_block }} = (b * HEADS + h) * params.qSeq * params.kvSeq+ (m_base + base_A + {{ row_block * 8 }}u) * params.kvSeq + k_base + step;var matA{{ row_block }}: subgroup_matrix_left<f32, 8, 8> =subgroupMatrixLoad<subgroup_matrix_left<f32, 8, 8>, row_major>(43 unmodified linestile_A[a_row * TILE_K + a_col + i] = loaded;{% endif %}{% else %}let score_base = (b * HEADS + h) * params.qSeq * params.kvSeq;tile_A[a_row * TILE_K + a_col + i] ={{ "f16(" if MT == "f16" else "" }}{{ score_value("score_base + row * params.kvSeq + k", ["a_row", "row < params.qSeq && k < params.kvSeq"]) }}{{ ")" if MT == "f16" else "" }};{% endif %}8 unmodified lines{% if headDim % 32 == 0 %}tile_B[b_row * TILE_K + b_col + i] = select({{ "0.0h" if MT == "f16" else "0.0" }},key[{{ kv_index("col", "k") }}],col < params.kvSeq);{% else %}var loaded = {{ "0.0h" if MT == "f16" else "0.0" }};if (col < params.kvSeq && k < HEAD_DIM) {loaded = key[{{ kv_index("col", "k") }}];}tile_B[b_row * TILE_K + b_col + i] = loaded;{% endif %}{% else %}tile_B[b_row * TILE_K + b_col + i] = select({{ "0.0h" if MT == "f16" else "0.0" }},value[{{ kv_index("k", "col") }}],k < params.kvSeq && col < HEAD_DIM);{% endif %}12 unmodified linesloaded = {{ q_tile_value(q_index("row", "k")) }};}{% elif FUSED_SOFTMAX %}let score_base = (b * HEADS + h) * params.qSeq * params.kvSeq;let loaded ={{ "f16(" if MT == "f16" else "" }}{{ score_value("score_base + row * params.kvSeq + k", ["tile_row", "row < params.qSeq && k < params.kvSeq"]) }}{{ ")" if MT == "f16" else "" }};{% else %}var loaded = 0.0;if (row < params.qSeq && k < params.kvSeq) {let score_base = (b * HEADS + h) * params.qSeq * params.kvSeq;loaded = scores[score_base + row * params.kvSeq + k];}{% endif %}8 unmodified lines{% if scorePhase %}var loaded = {{ "0.0h" if MT == "f16" else "0.0" }};if (col < params.kvSeq && k < HEAD_DIM) {loaded = key[{{ kv_index("col", "k") }}];}{% else %}var loaded = {{ "0.0h" if MT == "f16" else "0.0" }};if (k < params.kvSeq && col < HEAD_DIM) {loaded = value[{{ kv_index("k", "col") }}];}{% endif %}tile_B[idx] = loaded;46 unmodified lines{% for col_block in range(COL_BLOCKS) %}{% if scorePhase %}let output_offset{{ row_block }}{{ col_block }} =(b * HEADS + h) * params.qSeq * params.kvSeq+ (m_base + base_A + {{ row_block * 8 }}u) * params.kvSeq+ n_base + base_B + {{ col_block * 8 }}u;subgroupMatrixStore<row_major>(59 unmodified lines{% endif %}let scored = result{% if not SCALE_IN_Q %} * scale{% endif %};scores[(b * HEADS + h) * params.qSeq * params.kvSeq + row * params.kvSeq + col] = scored;{% if EMIT_ROW_STATS %}// Softmax sees the STORED value, so the statistics have to be taken on it7 unmodified lines{% if hasBias %}// V bias row base: skip the packed Q and K blocks, then index this head.{% endif %}output[{{ q_index("row", "col") }}] = {{ "f16(" if MT == "f16" else "" }}result{{ ")" if MT == "f16" else "" }}{% if hasBias %} + bias[2u * HIDDEN + h * HEAD_DIM + col]{% endif %};{% endif %}}}21 unmodified lines// eight consecutive pairs, and the combine pass reads a slot's whole column// of rows contiguously.let slot = wg.x * {{ SUBGROUP_COLS }}u + subtile_idx;let out_index = (((b * HEADS + h) * STAT_SLOTS + slot) * params.qSeq + stat_row) * 2u;scorePartials[out_index] = stat_m{{ row_block }};scorePartials[out_index + 1u] = stat_d{{ row_block }};}123451 unmodified line7891011123 unmodified lines1617181920212223242526272829303132333435361 unmodified line3839404142434445464748495051525346 unmodified lines1001011021031041051061071081091101117 unmodified lines11912012112212312412512612712830 unmodified lines1591601611621631641651661671681691701711721731741751763 unmodified lines18018118218318418518643 unmodified lines2302312322332342352368 unmodified lines24524624724824925025125225325425525625725825926026126226326412 unmodified lines2772782792802812822832842852862872882898 unmodified lines29829930030130230330430530630730830946 unmodified lines35635735835936036136259 unmodified lines4224234244254264274287 unmodified lines43643743843944044144221 unmodified lines464465466467468469470{% set MT = "f16" if (operandF16 is defined and operandF16) else "f32" %}{% set ST = "f16" if (storageF16 is defined and storageF16) else MT %}enable subgroups;{% if pinSubgroupSize32 %}enable subgroup_size_control;1 unmodified lineenable chromium_experimental_subgroup_matrix;diagnostic(off, chromium.subgroup_matrix_uniformity);{{ env.wgsl.resourceDeclarations }}{% set layout = layout | default("bsh") %}3 unmodified lines{% macro kv_index(seq, d) %}{% if kvHeadMajor %}((b * KV_HEADS + h_kv) * params.kvSeq + {{ seq }}) * HEAD_DIM + {{ d }}{% else %}(b * params.kvSeq + {{ seq }}) * KV_HIDDEN + h_kv * HEAD_DIM + {{ d }}{% endif %}{% endmacro %}{% set OUT_ROW_STRIDE = "HEAD_DIM" if headMajor else "HIDDEN" %}{% set KV_ROW_STRIDE = "HEAD_DIM" if kvHeadMajor else "KV_HIDDEN" %}{% set SCRATCH_HEAD = "(b * HEADS + h)" %}{% set scorePhase = phase == "score" %}{% set FUSED_SOFTMAX = fusedSoftmax is defined and fusedSoftmax %}{% set PRIVATE_ROW_STATS = FUSED_SOFTMAX and (materializedSgmatPrivateRowStats is defined and materializedSgmatPrivateRowStats) %}{% macro score_value(index, guard) %}{% set rowMax = "private_softmax_m" if PRIVATE_ROW_STATS else "softmax_m[" ~ guard[0] ~ "]" %}{% set rowDenom = "private_softmax_d" if PRIVATE_ROW_STATS else "softmax_d[" ~ guard[0] ~ "]" %}select(0.0, {{ "exp_shift(scores[" ~ index ~ "], " ~ rowMax ~ ") / " ~ rowDenom if FUSED_SOFTMAX else "scores[" ~ index ~ "]" }}, {{ guard[1] }}){% endmacro %}{% set TILE_M_VALUE = materializedSgmatQueryTile %}{% set TILE_N_VALUE = materializedSgmatKeyTile %}{% set TILE_K_VALUE = materializedSgmatInnerTile %}{% set SUB_ROWS_VALUE = 16 %}{% set SUB_COLS_VALUE = 32 %}{% set ROW_BLOCKS = 2 %}{% set COL_BLOCKS = 4 %}{% set SUBGROUP_ROWS = (TILE_M_VALUE / SUB_ROWS_VALUE)|int %}{% set SUBGROUP_COLS = (TILE_N_VALUE / SUB_COLS_VALUE)|int %}{% set SUBGROUP_COUNT = SUBGROUP_ROWS * SUBGROUP_COLS %}1 unmodified line{% if hasBias is not defined %}{% set hasBias = false %}{% endif %}{% set EMIT_ROW_STATS = emitRowStats is defined and emitRowStats %}{% set DIRECT_SCORE_STORE = materializedSgmatDirectScoreStore and not EMIT_ROW_STATS %}{% set DIRECT_APPLY_STORE = materializedSgmatDirectApplyStore and not hasBias and MT == "f32" and ST == "f32" %}{% set DIRECT_OUTPUT_STORE = DIRECT_SCORE_STORE if scorePhase else DIRECT_APPLY_STORE %}{% set RUNTIME_DIRECT_STORE = (not DIRECT_OUTPUT_STORE)and materializedSgmatRuntimeDirectStoreand (scorePhase or (not hasBias and MT == "f32" and ST == "f32"))and not EMIT_ROW_STATS %}{% set ANY_DIRECT_STORE = DIRECT_OUTPUT_STORE or RUNTIME_DIRECT_STORE %}{% set SCALE_IN_Q = DIRECT_SCORE_STORE or (RUNTIME_DIRECT_STORE and scorePhase) %}{% macro operand_load(name, index) %}{% if ST != MT %}{{ MT }}({% endif %}{{ name }}[{{ index }}]{% if ST != MT %}){% endif %}{% endmacro %}{% macro q_tile_value(index) %}{% if hasBias %}({{ operand_load("query", index) }} + bias[h * HEAD_DIM + k]){% else %}{{ operand_load("query", index) }}{% endif %}{% if SCALE_IN_Q %} * score_scale{% endif %}{% endmacro %}const HEADS: u32 = {{ qNumHeads }}u;{% if kvNumHeads is defined %}46 unmodified linesvar<workgroup> softmax_d: array<f32, {{ TILE_M_VALUE }}>;{% endif %}{% if FUSED_SOFTMAX or EMIT_ROW_STATS %}// FLT_MAX, not -inf, as the online (m, d) accumulator init: merges must keep// `m - m` finite so an empty lane / all--inf row contributes the exact// accumulator identity (m, d) = (-FLT_MAX, 0). Operator epilogues interpret// a zero final denominator according to their public semantics. Using -inf// here changes +inf-row behavior.const FLT_MAX: f32 = 3.4028234663852886e38;fn is_finite_f32(value: f32) -> bool {return select(false, value <= FLT_MAX, value >= -FLT_MAX);7 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}{% endif %}@compute @workgroup_size({{ WORKGROUP_THREADS }}, 1, 1){{ " @subgroup_size(32)" if pinSubgroupSize32 else "" }}30 unmodified lineslet inner = params.kvSeq;{% endif %}{% if FUSED_SOFTMAX %}// One row-stats pair per query row of the tile. A query tail clamps to the last// real row rather than reading past the buffer; those lanes are discarded by the// staging guard anyway, and the clamp keeps the denominator non-zero.for (var r = li; r < TILE_M; r += WORKGROUP_THREADS) {let stat_row = {{ SCRATCH_HEAD }} * params.qSeq + min(m_base + r, params.qSeq - 1u);softmax_m[r] = rowStats[stat_row * 2u];softmax_d[r] = rowStats[stat_row * 2u + 1u];}workgroupBarrier();{% endif %}for (var k_base = 0u; k_base < inner; k_base += TILE_K) {{% if phase == "apply" and not FUSED_SOFTMAX and MT == "f32" and ST == "f32" %}// Full interior PV tiles can be loaded directly from storage. Query,// reduction, and output-dimension tails use the guarded shared path below.if (3 unmodified lines) {for (var step = 0u; step < TILE_K; step += 8u) {{% for row_block in range(ROW_BLOCKS) %}let score_offset{{ row_block }} = {{ SCRATCH_HEAD }} * params.qSeq * params.kvSeq+ (m_base + base_A + {{ row_block * 8 }}u) * params.kvSeq + k_base + step;var matA{{ row_block }}: subgroup_matrix_left<f32, 8, 8> =subgroupMatrixLoad<subgroup_matrix_left<f32, 8, 8>, row_major>(43 unmodified linestile_A[a_row * TILE_K + a_col + i] = loaded;{% endif %}{% else %}let score_base = {{ SCRATCH_HEAD }} * params.qSeq * params.kvSeq;tile_A[a_row * TILE_K + a_col + i] ={{ "f16(" if MT == "f16" else "" }}{{ score_value("score_base + row * params.kvSeq + k", ["a_row", "row < params.qSeq && k < params.kvSeq"]) }}{{ ")" if MT == "f16" else "" }};{% endif %}8 unmodified lines{% if headDim % 32 == 0 %}tile_B[b_row * TILE_K + b_col + i] = select({{ "0.0h" if MT == "f16" else "0.0" }},{{ operand_load("key", kv_index("col", "k")) }},col < params.kvSeq);{% else %}var loaded = {{ "0.0h" if MT == "f16" else "0.0" }};if (col < params.kvSeq && k < HEAD_DIM) {loaded = {{ operand_load("key", kv_index("col", "k")) }};}tile_B[b_row * TILE_K + b_col + i] = loaded;{% endif %}{% else %}tile_B[b_row * TILE_K + b_col + i] = select({{ "0.0h" if MT == "f16" else "0.0" }},{{ operand_load("value", kv_index("k", "col")) }},k < params.kvSeq && col < HEAD_DIM);{% endif %}12 unmodified linesloaded = {{ q_tile_value(q_index("row", "k")) }};}{% elif FUSED_SOFTMAX %}let score_base = {{ SCRATCH_HEAD }} * params.qSeq * params.kvSeq;let loaded ={{ "f16(" if MT == "f16" else "" }}{{ score_value("score_base + row * params.kvSeq + k", ["tile_row", "row < params.qSeq && k < params.kvSeq"]) }}{{ ")" if MT == "f16" else "" }};{% else %}var loaded = 0.0;if (row < params.qSeq && k < params.kvSeq) {let score_base = {{ SCRATCH_HEAD }} * params.qSeq * params.kvSeq;loaded = scores[score_base + row * params.kvSeq + k];}{% endif %}8 unmodified lines{% if scorePhase %}var loaded = {{ "0.0h" if MT == "f16" else "0.0" }};if (col < params.kvSeq && k < HEAD_DIM) {loaded = {{ operand_load("key", kv_index("col", "k")) }};}{% else %}var loaded = {{ "0.0h" if MT == "f16" else "0.0" }};if (k < params.kvSeq && col < HEAD_DIM) {loaded = {{ operand_load("value", kv_index("k", "col")) }};}{% endif %}tile_B[idx] = loaded;46 unmodified lines{% for col_block in range(COL_BLOCKS) %}{% if scorePhase %}let output_offset{{ row_block }}{{ col_block }} ={{ SCRATCH_HEAD }} * params.qSeq * params.kvSeq+ (m_base + base_A + {{ row_block * 8 }}u) * params.kvSeq+ n_base + base_B + {{ col_block * 8 }}u;subgroupMatrixStore<row_major>(59 unmodified lines{% endif %}let scored = result{% if not SCALE_IN_Q %} * scale{% endif %};scores[{{ SCRATCH_HEAD }} * params.qSeq * params.kvSeq + row * params.kvSeq + col] = scored;{% if EMIT_ROW_STATS %}// Softmax sees the STORED value, so the statistics have to be taken on it7 unmodified lines{% if hasBias %}// V bias row base: skip the packed Q and K blocks, then index this head.{% endif %}output[{{ q_index("row", "col") }}] = {{ "f16(" if ST == "f16" else "" }}result{{ ")" if ST == "f16" else "" }}{% if hasBias %} + bias[2u * HIDDEN + h * HEAD_DIM + col]{% endif %};{% endif %}}}21 unmodified lines// eight consecutive pairs, and the combine pass reads a slot's whole column// of rows contiguously.let slot = wg.x * {{ SUBGROUP_COLS }}u + subtile_idx;let out_index = (({{ SCRATCH_HEAD }} * STAT_SLOTS + slot) * params.qSeq + stat_row) * 2u;scorePartials[out_index] = stat_m{{ row_block }};scorePartials[out_index + 1u] = stat_d{{ row_block }};}
1234567892 unmodified lines1213141516171819 unmodified lines38394041424357 unmodified lines10110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014123 unmodified lines165166167168169170171172{% set CACHE_VEC4 = cacheVec4 if cacheVec4 is defined else false %}{% set CAUSAL_ROWS = causalRows is defined and causalRows %}{% set SCALE_IN = scaleInSoftmax is defined and scaleInSoftmax %}{% set RUNTIME_COLS = colsFromParams is defined and colsFromParams %}{% set COLS_EXPR = "params.keyLen" if RUNTIME_COLS else "COLS" %}{% set ROWS_EXPR = rowsExpr if rowsExpr is defined else "params.rows" %}{% if useSubgroups %}enable subgroups;{% endif %}2 unmodified lines// In-place row softmax for materialized f32 attention. The default path reads// each scalar twice; the register-cached vec4 path eliminates the second read.const WG: u32 = {{ materializedSoftmaxWg }}u;{% set SCALED_SCORE = "scores[base + c] * SCALE" if SCALE_IN else "scores[base + c]" %}{% if CACHE_VEC4 %}const COLS4: u32 = {{ materializedSoftmaxCols4 }}u;const CACHE_VECS: u32 = (COLS4 + WG - 1u) / WG;19 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}57 unmodified linesreturn combinedMD;}{% else %}{% set mdStreamed = mdStreams is defined %}{% set mdStreams = mdStreams if mdStreams is defined else 1 %}{% set mdExtent = "WG" if mdStreams == 1 else "WG * " ~ mdStreams ~ "u" %}var<workgroup> partialM: array<f32, {{ mdExtent }}>;var<workgroup> partialD: array<f32, {{ mdExtent }}>;{% if mdStreamed %}// In-place fold of {{ mdStreams }} streams. Input partials occupy// partialM/partialD; stream s returns its merged pair in slot s * WG.fn combine_partials_streams(lidx: u32) {workgroupBarrier();var stride = WG / 2u;loop {if (stride == 0u) {break;}if (lidx < stride) {{% for s in range(mdStreams) %}{let slot = {{ s }}u * WG + lidx;let m1 = partialM[slot];let d1 = partialD[slot];let m2 = partialM[slot + stride];let d2 = partialD[slot + stride];let mNew = max(m1, m2);partialD[slot] = d1 * exp_shift(m1, mNew) + d2 * exp_shift(m2, mNew);partialM[slot] = mNew;}{% endfor %}}workgroupBarrier();stride = stride / 2u;}}{% else %}fn combine_partials(m: f32, d: f32, lidx: u32) -> vec2<f32> {partialM[lidx] = m;23 unmodified linesreturn merged;}{% endif %}{% endif %}@compute @workgroup_size(WG, 1, 1)fn main(12345672 unmodified lines1011121314151619 unmodified lines3637383940414257 unmodified lines10010110210310410510610710823 unmodified lines132133134135136137{% set CACHE_VEC4 = cacheVec4 if cacheVec4 is defined else false %}{% set CAUSAL_ROWS = false %}{% set COLS_EXPR = "COLS" %}{% set ROWS_EXPR = "params.rows" %}{% if useSubgroups %}enable subgroups;{% endif %}2 unmodified lines// In-place row softmax for materialized f32 attention. The default path reads// each scalar twice; the register-cached vec4 path eliminates the second read.const WG: u32 = {{ materializedSoftmaxWg }}u;{% set SCALED_SCORE = "scores[base + c]" %}{% if CACHE_VEC4 %}const COLS4: u32 = {{ materializedSoftmaxCols4 }}u;const CACHE_VECS: u32 = (COLS4 + WG - 1u) / WG;19 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}57 unmodified linesreturn combinedMD;}{% else %}{% set mdExtent = "WG" %}var<workgroup> partialM: array<f32, {{ mdExtent }}>;var<workgroup> partialD: array<f32, {{ mdExtent }}>;fn combine_partials(m: f32, d: f32, lidx: u32) -> vec2<f32> {partialM[lidx] = m;23 unmodified linesreturn merged;}{% endif %}@compute @workgroup_size(WG, 1, 1)fn main(
12345678910119 unmodified lines2122232425262728293031323334353637383 unmodified lines42434445464748495051525 unmodified lines5859606162636465666768697071727374757677787937 unmodified lines1171181191201211221232 unmodified lines126127128129130131132133134135136137138139140141142{{ env.wgsl.resourceDeclarations }}{% set MASK_BATCH = "batch * params.maskBatchStride + " if layout == "bsh" else "" %}// Online-softmax attention fallback with no feature requirements: one// workgroup per (batch, head, query token) walks the keys serially; the// workgroup cooperates on each q·k dot (tree reduction) and on the running// V accumulator, with the online rescale applied per key. This path requires// no subgroup or subgroup-matrix features.// Layout: rank-3 token-major [batch, seq, heads * headDim].// An omitted scale uses 1/sqrt(headDim); explicit zero remains zero through// the scaleIsExplicitZero specialization.9 unmodified linesconst KV_HIDDEN: u32 = {{ kvHidden }}u;const Q_HEADS: u32 = {{ qNumHeads }}u;const KV_HEADS: u32 = {{ kvNumHeads }}u;{% set qHeads = "params.qHeads" if headsFromParams else "Q_HEADS" %}{% set kvHeads = "params.kvHeads" if headsFromParams else "KV_HEADS" %}{% set scale = scale | default("0.0") %}const WG: u32 = {{ workgroupSize }}u;var<workgroup> partial: array<f32, WG>;var<workgroup> running_max: f32;var<workgroup> running_denom: f32;var<workgroup> running_out: array<f32, HEAD_DIM>;var<workgroup> previous_scale: f32;{% macro wgsl_tree_reduce_f32(name, mode, buffer="partial", wg="WG", trailingBarrier=true) %}fn {{ name }}(value: f32, tid: u32) -> f32 {{{ buffer }}[tid] = value;workgroupBarrier();// Ceil-halving keeps every lane when the workgroup size is not a power of3 unmodified linesloop {let half = (n + 1u) / 2u;if (tid < n - half) {{% if mode == "max" %}{{ buffer }}[tid] = max({{ buffer }}[tid], {{ buffer }}[tid + half]);{% else %}{{ buffer }}[tid] = {{ buffer }}[tid] + {{ buffer }}[tid + half];{% endif %}}workgroupBarrier();n = half;5 unmodified lines// slot 0 here, so the next call's first store must not run until all lanes have read it.// `trailingBarrier=false` is safe only when the buffer is never written again before kernel exit.let reduced = {{ buffer }}[0];{% if trailingBarrier %}workgroupBarrier();{% endif %}return reduced;}{% endmacro %}{{ wgsl_tree_reduce_f32("reduce_sum", "add", "partial", "WG") }}{% if ATTN_SCALE_DIM is not defined %}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}{% endif %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}@compute @workgroup_size(WG, 1, 1)fn main(@builtin(workgroup_id) wg: vec3<u32>,37 unmodified linesfor (var key_token: u32 = minKj; key_token < maxKj; key_token = key_token + 1u) {let kRow = kvBase + key_token * kvTokenStride;var partial_dot = 0.0;for (var d: u32 = tid; d < HEAD_DIM; d = d + WG) {var q_value = f32(query[qBase + d]);var k_value = f32(key[kRow + d]);2 unmodified linesq_value = q_value + f32(bias[channel]);k_value = k_value + f32(bias[Q_HIDDEN + channel]);{% endif %}partial_dot = partial_dot + q_value * k_value;}// reduce_sum returns the same partial[0] to every lane, so `score` is already// workgroup-uniform; tid==0 advances the running max/denom/scale (broadcast// through shared memory by the barrier below).{% if hasMask %}let maskIndex = {{ MASK_BATCH }}h * params.maskHeadStride + query_token * params.maskSeqStride + key_token;let score = reduce_sum(partial_dot, tid) * scale_value() + f32(attn_mask[maskIndex]);{% else %}let score = reduce_sum(partial_dot, tid) * scale_value();{% endif %}if (tid == 0u) {let next_max = max(running_max, score);12345678910119 unmodified lines2122232425262728293031323334353637383940414243444546474849505152535455565758593 unmodified lines636465666768695 unmodified lines7576777879808182838485868788899037 unmodified lines1281291301311321331342 unmodified lines137138139140141142143144145146147148149150151152153{{ env.wgsl.resourceDeclarations }}{% set MASK_BATCH = "batch * params.maskBatchStride + " %}// Online-softmax attention fallback with no feature requirements: one// workgroup per (batch, head, query token) walks the keys serially; the// workgroup cooperates on each q·k dot (compensated tree reduction) and on the// running V accumulator, with the online rescale applied per key. This path// requires no subgroup or subgroup-matrix features.// Layout: rank-3 token-major [batch, seq, heads * headDim].// An omitted scale uses 1/sqrt(headDim); explicit zero remains zero through// the scaleIsExplicitZero specialization.9 unmodified linesconst KV_HIDDEN: u32 = {{ kvHidden }}u;const Q_HEADS: u32 = {{ qNumHeads }}u;const KV_HEADS: u32 = {{ kvNumHeads }}u;{% set qHeads = "Q_HEADS" %}{% set kvHeads = "KV_HEADS" %}const WG: u32 = {{ workgroupSize }}u;{% set dotType = "f32" %}// Retain product and addition residuals across a dot product. Explicit fma// boundaries preserve the addition error transform under reassociation.struct DotAccumulator {hi: {{ dotType }},lo: {{ dotType }},}fn dot_accumulate(acc: DotAccumulator, a: {{ dotType }}, b: {{ dotType }}) -> DotAccumulator {let product = fma(a, b, {{ dotType }}(0.0));let productError = fma(a, b, -product);let sum = fma(acc.hi, {{ dotType }}(1.0), product);let bv = fma({{ dotType }}(-1.0), acc.hi, sum);let av = fma({{ dotType }}(-1.0), bv, sum);let ae = fma({{ dotType }}(-1.0), av, acc.hi);let be = fma({{ dotType }}(-1.0), bv, product);let error = fma(fma(fma(ae, {{ dotType }}(1.0), be), {{ dotType }}(1.0), acc.lo), {{ dotType }}(1.0), productError);let hi = fma(sum, {{ dotType }}(1.0), error);return DotAccumulator(hi, fma({{ dotType }}(-1.0), fma({{ dotType }}(-1.0), sum, hi), error));}fn dot_value(acc: DotAccumulator) -> {{ dotType }} {return fma(acc.hi, {{ dotType }}(1.0), acc.lo);}var<workgroup> partial: array<DotAccumulator, WG>;var<workgroup> running_max: f32;var<workgroup> running_denom: f32;var<workgroup> running_out: array<f32, HEAD_DIM>;var<workgroup> previous_scale: f32;{% macro wgsl_tree_reduce_f32(name, mode, buffer="partial", wg="WG", trailingBarrier=true, valueType="f32") %}fn {{ name }}(value: {{ valueType }}, tid: u32) -> {{ valueType }} {{{ buffer }}[tid] = value;workgroupBarrier();// Ceil-halving keeps every lane when the workgroup size is not a power of3 unmodified linesloop {let half = (n + 1u) / 2u;if (tid < n - half) {{{ buffer }}[tid] = dot_accumulate(dot_accumulate({{ buffer }}[tid], {{ buffer }}[tid + half].hi, 1.0), {{ buffer }}[tid + half].lo, 1.0);}workgroupBarrier();n = half;5 unmodified lines// slot 0 here, so the next call's first store must not run until all lanes have read it.// `trailingBarrier=false` is safe only when the buffer is never written again before kernel exit.let reduced = {{ buffer }}[0];workgroupBarrier();return reduced;}{% endmacro %}{{ wgsl_tree_reduce_f32("reduce_dot", "compensated", "partial", "WG", valueType="DotAccumulator") }}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}@compute @workgroup_size(WG, 1, 1)fn main(@builtin(workgroup_id) wg: vec3<u32>,37 unmodified linesfor (var key_token: u32 = minKj; key_token < maxKj; key_token = key_token + 1u) {let kRow = kvBase + key_token * kvTokenStride;var partial_dot = DotAccumulator(0.0, 0.0);for (var d: u32 = tid; d < HEAD_DIM; d = d + WG) {var q_value = f32(query[qBase + d]);var k_value = f32(key[kRow + d]);2 unmodified linesq_value = q_value + f32(bias[channel]);k_value = k_value + f32(bias[Q_HIDDEN + channel]);{% endif %}partial_dot = dot_accumulate(partial_dot, q_value, k_value);}// reduce_dot returns the same partial[0] to every lane, so `score` is already// workgroup-uniform; tid==0 advances the running max/denom/scale (broadcast// through shared memory by the barrier below).{% if hasMask %}let maskIndex = {{ MASK_BATCH }}h * params.maskHeadStride + query_token * params.maskSeqStride + key_token;let score = dot_value(reduce_dot(partial_dot, tid)) * scale_value() + f32(attn_mask[maskIndex]);{% else %}let score = dot_value(reduce_dot(partial_dot, tid)) * scale_value();{% endif %}if (tid == 0u) {let next_max = max(running_max, score);
8 unmodified lines91011121314151617181920212223242519 unmodified lines454647484950515253545556575859608 unmodified linesconst KV_SEQ: u32 = {{ kvSeq }}u;const WG: u32 = 64u;const NEG_MAX: f32 = -3.4028234663852886e38;{% if ATTN_SCALE_DIM is not defined %}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}{% endif %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}var<workgroup> scores: array<f32, KV_SEQ>;var<workgroup> partial: array<f32, WG>;{% macro wgsl_tree_reduce_f32(name, mode, buffer="partial", wg="WG", trailingBarrier=true) %}fn {{ name }}(value: f32, tid: u32) -> f32 {{{ buffer }}[tid] = value;workgroupBarrier();// Ceil-halving keeps every lane when the workgroup size is not a power of19 unmodified lines// slot 0 here, so the next call's first store must not run until all lanes have read it.// `trailingBarrier=false` is safe only when the buffer is never written again before kernel exit.let reduced = {{ buffer }}[0];{% if trailingBarrier %}workgroupBarrier();{% endif %}return reduced;}{% endmacro %}{{ wgsl_tree_reduce_f32("reduce_max", "max", "partial", "WG") }}{{ wgsl_tree_reduce_f32("reduce_sum", "add", "partial", "WG") }}@compute @workgroup_size(WG, 1, 1)fn main(@builtin(workgroup_id) wg: vec3<u32>,8 unmodified lines9101112131415161718192021222319 unmodified lines4344454647484950515253548 unmodified linesconst KV_SEQ: u32 = {{ kvSeq }}u;const WG: u32 = 64u;const NEG_MAX: f32 = -3.4028234663852886e38;{% set ATTN_SCALE_DIM = "HEAD_DIM" %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}var<workgroup> scores: array<f32, KV_SEQ>;var<workgroup> partial: array<f32, WG>;{% macro wgsl_tree_reduce_f32(name, mode, buffer="partial", wg="WG", trailingBarrier=true, valueType="f32") %}fn {{ name }}(value: {{ valueType }}, tid: u32) -> {{ valueType }} {{{ buffer }}[tid] = value;workgroupBarrier();// Ceil-halving keeps every lane when the workgroup size is not a power of19 unmodified lines// slot 0 here, so the next call's first store must not run until all lanes have read it.// `trailingBarrier=false` is safe only when the buffer is never written again before kernel exit.let reduced = {{ buffer }}[0];workgroupBarrier();return reduced;}{% endmacro %}{{ wgsl_tree_reduce_f32("reduce_max", "max", "partial", "WG") }}{{ wgsl_tree_reduce_f32("reduce_sum", "add", "partial", "WG") }}@compute @workgroup_size(WG, 1, 1)fn main(@builtin(workgroup_id) wg: vec3<u32>,
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198{% if useValueSubgroups %}enable subgroups;{% endif %}{{ env.wgsl.resourceDeclarations }}{% set rowType = "f32" if queryBlock == 1 else "vec" ~ queryBlock ~ "<f32>" %}{% set dotValueType = rowType %}{% set dotType = dotValueType | default("f32") %}// Retain product and addition residuals across a dot product. Explicit fma// boundaries preserve the addition error transform under reassociation.struct DotAccumulator { hi: {{ dotType }}, lo: {{ dotType }},}fn dot_accumulate(acc: DotAccumulator, a: {{ dotType }}, b: {{ dotType }}) -> DotAccumulator { let product = fma(a, b, {{ dotType }}(0.0)); let productError = fma(a, b, -product); let sum = fma(acc.hi, {{ dotType }}(1.0), product); let bv = fma({{ dotType }}(-1.0), acc.hi, sum); let av = fma({{ dotType }}(-1.0), bv, sum); let ae = fma({{ dotType }}(-1.0), av, acc.hi); let be = fma({{ dotType }}(-1.0), bv, product); let error = fma(fma(fma(ae, {{ dotType }}(1.0), be), {{ dotType }}(1.0), acc.lo), {{ dotType }}(1.0), productError); let hi = fma(sum, {{ dotType }}(1.0), error); return DotAccumulator(hi, fma({{ dotType }}(-1.0), fma({{ dotType }}(-1.0), sum, hi), error));}fn dot_value(acc: DotAccumulator) -> {{ dotType }} { return fma(acc.hi, {{ dotType }}(1.0), acc.lo);}const HEAD_DIM: u32 = {{ headDim }}u;const HIDDEN: u32 = {{ qHidden }}u;const KV_SEQ: u32 = {{ kvSeq }}u;const WG: u32 = {{ valueWorkgroupSize }}u;const QUERY_BLOCK: u32 = {{ queryBlock }}u;const NEG_MAX: f32 = -3.4028234663852886e38;{% set ATTN_SCALE_DIM = "HEAD_DIM" %}fn scale_value() -> f32 { if (params.scale != 0.0) { return params.scale; } return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}var<workgroup> scores: array<{{ rowType }}, {{ scoreStorageElements }}u>;var<workgroup> partial: array<{{ rowType }}, WG>;
{% if useValueSubgroups %}{% macro reduce(name, collective, mode, identity) %}fn {{ name }}(value: {{ rowType }}, sgLane: u32, sgId: u32, numSg: u32) -> {{ rowType }} { let sub = {{ collective }}(value); if (sgLane == 0u) { partial[sgId] = sub; } workgroupBarrier(); var total = {{ rowType }}({{ identity }}); for (var i = 0u; i < numSg; i = i + 1u) {{% if mode == "max" %} total = max(total, partial[i]);{% else %} total = total + partial[i];{% endif %} } workgroupBarrier(); return total;}{% endmacro %}{{ reduce("reduce_max", "subgroupMax", "max", "NEG_MAX") }}{{ reduce("reduce_sum", "subgroupAdd", "add", "0.0") }}{% else %}{% macro wgsl_tree_reduce_f32(name, mode, buffer="partial", wg="WG", trailingBarrier=true, valueType="f32") %}fn {{ name }}(value: {{ valueType }}, tid: u32) -> {{ valueType }} { {{ buffer }}[tid] = value; workgroupBarrier(); // Ceil-halving keeps every lane when the workgroup size is not a power of // two. For even n this matches the power-of-two tree order; for odd n, lanes // [0, n-half) fold the upper tail while the middle lane carries forward. var n: u32 = {{ wg }}; loop { let half = (n + 1u) / 2u; if (tid < n - half) {{% if mode == "max" %} {{ buffer }}[tid] = max({{ buffer }}[tid], {{ buffer }}[tid + half]);{% else %} {{ buffer }}[tid] = {{ buffer }}[tid] + {{ buffer }}[tid + half];{% endif %} } workgroupBarrier(); n = half; if (n == 1u) { break; } } // The default trailing barrier makes this helper safe for back-to-back calls: every lane reads // slot 0 here, so the next call's first store must not run until all lanes have read it. // `trailingBarrier=false` is safe only when the buffer is never written again before kernel exit. let reduced = {{ buffer }}[0]; workgroupBarrier(); return reduced;}{% endmacro %}{{ wgsl_tree_reduce_f32("reduce_max", "max", valueType=rowType) }}{{ wgsl_tree_reduce_f32("reduce_sum", "add", valueType=rowType) }}{% endif %}@compute @workgroup_size(WG, 1, 1)fn main( @builtin(workgroup_id) wg: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>{% if useValueSubgroups %} , @builtin(subgroup_invocation_id) sgLane: u32 , @builtin(subgroup_id) sgId: u32 , @builtin(num_subgroups) numSg: u32{% endif %}) { let h = wg.y; let batch = wg.z; let tid = lid.x; let kvBase = batch * KV_SEQ * HIDDEN + h * HEAD_DIM; for (var query_token = wg.x * QUERY_BLOCK; query_token < params.qSeq; query_token = query_token + nwg.x * QUERY_BLOCK) {{% for row in range(queryBlock) %} let qBase_{{ row }} = (batch * params.qSeq + {% if queryTail and row > 0 %}min(query_token + {{ row }}u, params.qSeq - 1u){% else %}query_token{% if row > 0 %} + {{ row }}u{% endif %}{% endif %}) * HIDDEN + h * HEAD_DIM;{% endfor %} // Query components stay private throughout the key scan. Their lifetime // ends before the value accumulators need the same registers. {{% for d in range(headDim) %} let q_{{ d }} = {% if queryBlock > 1 %}{{ rowType }}({% endif %}{% for row in range(queryBlock) %}{% if not loop.first %}, {% endif %}f32(query[qBase_{{ row }} + {{ d }}u]){% endfor %}{% if queryBlock > 1 %}){% endif %};{% endfor %} for (var key_token = tid; key_token < KV_SEQ; key_token = key_token + WG) { let kBase = kvBase + key_token * HIDDEN; var score = DotAccumulator({{ rowType }}(0.0), {{ rowType }}(0.0));{% for d in range(headDim) %} score = dot_accumulate(score, q_{{ d }}, {{ rowType }}(f32(key[kBase + {{ d }}u])));{% endfor %} scores[key_token] = dot_value(score) * scale_value(); } } workgroupBarrier(); var laneMax = {{ rowType }}(NEG_MAX); for (var key_token = tid; key_token < KV_SEQ; key_token = key_token + WG) { laneMax = max(laneMax, scores[key_token]); } let rowMax = reduce_max(laneMax, {% if useValueSubgroups %}sgLane, sgId, numSg{% else %}tid{% endif %}); var laneSum = {{ rowType }}(0.0); for (var key_token = tid; key_token < KV_SEQ; key_token = key_token + WG) { let probability = exp(scores[key_token] - rowMax); scores[key_token] = probability; laneSum = laneSum + probability; } let invDenom = {{ rowType }}(1.0) / reduce_sum(laneSum, {% if useValueSubgroups %}sgLane, sgId, numSg{% else %}tid{% endif %});{% for d in range(headDim) %} var value_acc_{{ d }} = {{ rowType }}(0.0);{% endfor %} for (var key_token = tid; key_token < KV_SEQ; key_token = key_token + WG) { let probability = scores[key_token]; let vBase = kvBase + key_token * HIDDEN;{% for d in range(headDim) %} value_acc_{{ d }} = value_acc_{{ d }} + probability * f32(value[vBase + {{ d }}u]);{% endfor %} }{% if useValueSubgroups %}{% for d in range(headDim) %} let value_sum_{{ d }} = subgroupAdd(value_acc_{{ d }});{% endfor %} workgroupBarrier(); if (sgLane == 0u) {{% for d in range(headDim) %} scores[sgId * HEAD_DIM + {{ d }}u] = value_sum_{{ d }};{% endfor %} } workgroupBarrier(); if (tid < HEAD_DIM) { var total = {{ rowType }}(0.0); for (var i = 0u; i < numSg; i = i + 1u) { total = total + scores[i * HEAD_DIM + tid]; } let result = total * invDenom;{% else %} workgroupBarrier();{% for d in range(headDim) %} scores[{{ d }}u * WG + tid] = value_acc_{{ d }};{% endfor %} workgroupBarrier(); for (var stride = WG / 2u; stride > 0u; stride = stride / 2u) { if (tid < stride) {{% for d in range(headDim) %} scores[{{ d }}u * WG + tid] = scores[{{ d }}u * WG + tid] + scores[{{ d }}u * WG + tid + stride];{% endfor %} } workgroupBarrier(); } if (tid < HEAD_DIM) { let result = scores[tid * WG] * invDenom;{% endif %}{% for row in range(queryBlock) %}{% if queryTail and row > 0 %} if (query_token + {{ row }}u < params.qSeq) {{% endif %} output[qBase_{{ row }} + tid] = {{ outputScalar }}(result{% if queryBlock > 1 %}[{{ row }}]{% endif %});{% if queryTail and row > 0 %} }{% endif %}{% endfor %} } workgroupBarrier(); }}56 unmodified lines575859606162632 unmodified lines6667686970717273747576777879808182838485868788899091929394959697989910010110210310410517 unmodified lines1231241251261271281291301313 unmodified lines1351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751 unmodified line17717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225312 unmodified lines26626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731810 unmodified lines3293303313323333343353363373383393403 unmodified lines34434534634734834935035135235335435535635710 unmodified lines3683693703713723733743753763773783793 unmodified lines3833843853863873883896 unmodified lines39639739839940040140240340440540640740840941041141241341441541641741841942028 unmodified lines4494504514524534544554562 unmodified lines45946046146246346446546646746846947047147211 unmodified lines4844854864874884894904914924934944954962 unmodified lines4995005015025035045055064 unmodified lines5115125135145155165175185195 unmodified lines52552652752852953053153253353453553640 unmodified lines5775785795805815825835849 unmodified lines59459559659759859960060160260312 unmodified lines6166176186196206216226236246256266276286296306316326336345 unmodified lines64064164264364464564612 unmodified lines65966066166266366466566666766812 unmodified lines6816826836846856866876886896906916926936946956966976986995 unmodified lines70570670770870971071112 unmodified lines72472572672772872973073173273315 unmodified lines7497507517527537547557567577587597607617627637647657667675 unmodified lines7737747757767777787795 unmodified lines78578678778878979079179279379479579679779879926 unmodified lines82682782882983083183283383483583683783883984026 unmodified lines86786886987087187287387487587687787887988088128 unmodified lines91091191291391491591691791891992092192292392417 unmodified lines94294394494594694794894995095195295395495595695795895996096196296396417 unmodified lines9829839849859869879889899909919929939949959969979989991000100110021003100419 unmodified lines102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104813 unmodified lines10621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109115 unmodified lines11071108110911101111111211131114111511161117111811193 unmodified lines11231124112511261127112811291130113125 unmodified lines11571158115911601161116211635 unmodified lines11691170117111721173117411754 unmodified lines118011811182118311841185118611871188118925 unmodified lines12151216121712181219122012211 unmodified line12231224122512261227122812295 unmodified lines1235123612371238123912401241124225 unmodified lines126812691270127112721273127412751276127712781279128012811282128312841285128612874 unmodified lines12921293129412951296129712981299130025 unmodified lines13261327132813291330133113321333133413351336133713381339134013416 unmodified lines13481349135013511352135313543 unmodified lines13581359136013611362136313649 unmodified lines13741375137613771378137913805 unmodified lines138613871388138913901391139213931394139513961397139813991400140114021403140414059 unmodified lines14151416141714181419142014213 unmodified lines14251426142714281429143014319 unmodified lines14411442144314441445144614475 unmodified lines1453145414551456145714581459146014611462146314641465146614671468146914701471147210 unmodified lines1483148414851486148714881489149014911492149314941495149612 unmodified lines15091510151115121513151415155 unmodified lines15211522152315241525152615275 unmodified lines15331534153515361537153815399 unmodified lines1549155015511552155315541555155615571558155915601561156212 unmodified lines15751576157715781579158015815 unmodified lines15871588158915901591159215935 unmodified lines15991600160116021603160416055 unmodified lines16111612161316141615161616171618161916201621162214 unmodified lines163716381639164016411642164316445 unmodified lines165016511652165316541655165624 unmodified lines1681168216831684168516861687168816891690169116921693169412 unmodified lines17071708170917101711171217137 unmodified lines1721172217231724172517261727172817291730173117321733173412 unmodified lines17471748174917501751175217537 unmodified lines176117621763176417651766176717681769177017711772177325 unmodified lines179918001801180218031804180518061807180818091810181125 unmodified lines1837183818391840184118421843184418451846184718481849185018511852185310 unmodified lines186418651866186718681869187018712 unmodified lines1874187518761877187818791880188118821883188418851886188718881889189012 unmodified lines190319041905190619071908190919102 unmodified lines1913191419151916191719181919192019211922192319241925192619271928192919 unmodified lines1949195019511952195319541955195619571958195919601961196219631964196521 unmodified lines198719881989199019911992199319941995199619971998199920002001200220039 unmodified lines2013201420152016201720182019202020212 unmodified lines2024202520262027202820292030203120322033203420352036203720382039204011 unmodified lines2052205320542055205620572058205920602 unmodified lines206320642065206620672068206920702071207220732074207520762077207820799 unmodified lines20892090209120922093209420953 unmodified lines2099210021012102210321042105210621072108210921102111211221132114211511 unmodified lines21272128212921302131213221338 unmodified lines21422143214421452146214721482149215021512152215321549 unmodified lines21642165216621672168216921705 unmodified lines2176217721782179218021812182218321842185218621872188218921902191219221932194219512 unmodified lines22082209221022112212221322142215221622172218221922209 unmodified lines22302231223222332234223522365 unmodified lines2242224322442245224622472248224922502251225222532254225522562257225822592260226156 unmodified lines"canPinSubgroupSize32": "device.features.has(\"subgroups\") and device.features.has(\"subgroup-size-control\") and has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize <= 32 and device.adapterInfo.subgroupMaxSize >= 32","pinSubgroupSize32": "canPinSubgroupSize32 and not wave32Adapter","wave32Effective": "wave32Adapter or pinSubgroupSize32","headDimPlan": "dim(shapes.queryT, 2) / attrs.num_heads if (ranks.queryT == 3 and attrs.num_heads > 0) else 0","qkvDtypesOk": "tensorDtypes.keyT == tensorDtypes.queryT and tensorDtypes.valueT == tensorDtypes.queryT and tensorDtypes.outputT == tensorDtypes.queryT","floatDtypeOk": "(tensorDtypes.queryT == \"float32\" or tensorDtypes.queryT == \"float16\") and f16Ok(tensorDtypes.queryT)","qkvShapeOk": "ranks.queryT == 3 and ranks.keyT == 3 and ranks.valueT == 3 and ranks.outputT == 3 and attrs.num_heads > 0 and dim(shapes.queryT, 2) % attrs.num_heads == 0 and dim(shapes.keyT, 2) == dim(shapes.queryT, 2) and dim(shapes.valueT, 2) == dim(shapes.queryT, 2) and dim(shapes.keyT, 1) == dim(shapes.valueT, 1) and dim(shapes.queryT, 0) == dim(shapes.keyT, 0) and dim(shapes.queryT, 0) == dim(shapes.valueT, 0) and dim(shapes.outputT, 0) == dim(shapes.queryT, 0) and dim(shapes.outputT, 1) == dim(shapes.queryT, 1) and dim(shapes.outputT, 2) == dim(shapes.valueT, 2)",2 unmodified lines"qkvContractOk": "qkvShapeOk and qkvDtypesOk and floatDtypeOk and noAttnBias","qkvMaskContractOk": "qkvShapeOk and qkvDtypesOk and floatDtypeOk and attnBiasOk","biasOk": "present.biasT and ranks.biasT == 1 and tensorDtypes.biasT == tensorDtypes.queryT and dim(shapes.biasT, 0) == 3 * dim(shapes.queryT, 2)","q32BroadcastSubgroupLanes": "device.adapterInfo.subgroupMinSize if subgroupsWave32 else 0","q32BroadcastF32HeadVectors": "headDimPlan / 4 if headDimPlan % 4 == 0 else 0","q32BroadcastF32RegisterGeometry": "subgroupsWave32 and q32BroadcastF32HeadVectors == q32BroadcastSubgroupLanes","subgroupCluster4": "not narrowSubgroupRange and device.features.has(\"subgroups\") and has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize >= 4 and device.adapterInfo.subgroupMinSize % 4 == 0 and device.adapterInfo.subgroupMaxSize % 4 == 0","subgroupCluster8": "not narrowSubgroupRange and device.features.has(\"subgroups\") and has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize >= 8 and device.adapterInfo.subgroupMinSize % 8 == 0 and device.adapterInfo.subgroupMaxSize % 8 == 0","attentionDispatchFits": "dim(shapes.queryT, 0) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and dim(shapes.queryT, 1) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and attrs.num_heads <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","flashHeadOk": "headDimPlan % 4 == 0 and headDimPlan >= 32 and headDimPlan <= 256","flashSizeOk": "flashHeadOk and (dim(shapes.queryT, 1) * attrs.num_heads >= tunables.FLASH_MIN_QUERY_HEADS or (dim(shapes.queryT, 1) == 1 and dim(shapes.keyT, 1) >= 512) or (dim(shapes.queryT, 1) > 1 and dim(shapes.keyT, 1) >= 2048)) and attentionDispatchFits","flashShapeOk": "qkvContractOk and flashSizeOk","flashMaskShapeOk": "qkvMaskContractOk and flashSizeOk","noBiasSplitKCount": "min(tunables.DECODE_MAX_SPLITS if dim(shapes.queryT, 1) == 1 else max(1, ceilDiv(tunables.SPLITK_TARGET_WORKGROUPS, dim(shapes.queryT, 0) * dim(shapes.queryT, 1) * attrs.num_heads)), ceilDiv(dim(shapes.keyT, 1), tunables.DECODE_KEYS_PER_SPLIT))","biasSplitKCount": "min(tunables.DECODE_MAX_SPLITS, ceilDiv(dim(shapes.keyT, 1), tunables.DECODE_KEYS_PER_SPLIT))","noBiasPartialOutBytes": "dim(shapes.queryT, 0) * dim(shapes.queryT, 1) * attrs.num_heads * noBiasSplitKCount * headDimPlan * 4","noBiasStatsBytes": "2 * dim(shapes.queryT, 0) * dim(shapes.queryT, 1) * attrs.num_heads * noBiasSplitKCount * 4","noBiasSplitScratchFits": "noBiasPartialOutBytes <= device.limits.maxStorageBufferBindingSize and noBiasPartialOutBytes <= device.limits.maxBufferSize and noBiasStatsBytes <= device.limits.maxStorageBufferBindingSize and noBiasStatsBytes <= device.limits.maxBufferSize","noBiasSplitDispatchFits": "dim(shapes.queryT, 1) * noBiasSplitKCount <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","biasPartialOutBytes": "dim(shapes.queryT, 0) * attrs.num_heads * biasSplitKCount * headDimPlan * 4","biasStatsBytes": "2 * dim(shapes.queryT, 0) * attrs.num_heads * biasSplitKCount * 4","biasSplitScratchFits": "biasPartialOutBytes <= device.limits.maxStorageBufferBindingSize and biasPartialOutBytes <= device.limits.maxBufferSize and biasStatsBytes <= device.limits.maxStorageBufferBindingSize and biasStatsBytes <= device.limits.maxBufferSize","biasSplitDispatchFits": "biasSplitKCount <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","decodeSplitKNoBiasOk": "qkvContractOk and attrs.unidirectional == 0 and dim(shapes.queryT, 1) == 1 and dim(shapes.keyT, 1) >= 512 and flashHeadOk and attentionDispatchFits and noBiasSplitDispatchFits and noBiasSplitScratchFits","shortQuerySplitKNoBiasOk": "qkvContractOk and attrs.unidirectional == 0 and dim(shapes.queryT, 1) >= 2 and dim(shapes.queryT, 1) <= 16 and dim(shapes.keyT, 1) >= 2048 and flashHeadOk and attentionDispatchFits and noBiasSplitDispatchFits and noBiasSplitScratchFits","decodeSplitKBiasOk": "biasOk and qkvContractOk and attrs.unidirectional == 0 and dim(shapes.queryT, 1) == 1 and dim(shapes.keyT, 1) >= 512 and flashHeadOk and attentionDispatchFits and biasSplitDispatchFits and biasSplitScratchFits","decodeSplitKPortablePreferred": "tensorDtypes.queryT == \"float32\" and device.features.has(\"subgroups\") and has(device.adapterInfo, \"subgroupMinSize\") and headDimPlan / 4 < device.adapterInfo.subgroupMinSize","materializedScoreBytes": "dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1) * dim(shapes.keyT, 1) * 4","materializedScoreFits": "materializedScoreBytes <= device.limits.maxStorageBufferBindingSize and materializedScoreBytes <= device.limits.maxBufferSize","materializedWorkgroupSize": "tunables.MATERIALIZED_WORKGROUP_DIM * tunables.MATERIALIZED_WORKGROUP_DIM","materializedScoreStorageBytes": "(tunables.MATERIALIZED_QUERY_TILE * tunables.MATERIALIZED_INNER_TILE + tunables.MATERIALIZED_KEY_TILE * (tunables.MATERIALIZED_INNER_TILE + 4)) * 4","materializedApplyTileN": "tunables.MATERIALIZED_VALUE_TILE_D128 if headDimPlan == 128 else tunables.MATERIALIZED_VALUE_TILE","materializedApplyStorageBytes": "(tunables.MATERIALIZED_QUERY_TILE * tunables.MATERIALIZED_INNER_TILE + tunables.MATERIALIZED_INNER_TILE * materializedApplyTileN) * 4","materializedTileGeometryOk": "tunables.MATERIALIZED_INNER_TILE % 4 == 0 and (materializedApplyTileN / tunables.MATERIALIZED_WORKGROUP_DIM) % 4 == 0 and tunables.MATERIALIZED_INNER_TILE > 0 and tunables.MATERIALIZED_WORKGROUP_DIM > 0 and tunables.MATERIALIZED_QUERY_TILE % tunables.MATERIALIZED_WORKGROUP_DIM == 0 and tunables.MATERIALIZED_KEY_TILE % tunables.MATERIALIZED_WORKGROUP_DIM == 0 and materializedApplyTileN % tunables.MATERIALIZED_WORKGROUP_DIM == 0 and tunables.MATERIALIZED_QUERY_TILE * tunables.MATERIALIZED_INNER_TILE >= materializedWorkgroupSize and tunables.MATERIALIZED_KEY_TILE * tunables.MATERIALIZED_INNER_TILE >= materializedWorkgroupSize and tunables.MATERIALIZED_INNER_TILE * materializedApplyTileN >= materializedWorkgroupSize and (tunables.MATERIALIZED_QUERY_TILE * tunables.MATERIALIZED_INNER_TILE) % materializedWorkgroupSize == 0 and (tunables.MATERIALIZED_KEY_TILE * tunables.MATERIALIZED_INNER_TILE) % materializedWorkgroupSize == 0 and (tunables.MATERIALIZED_INNER_TILE * materializedApplyTileN) % materializedWorkgroupSize == 0","materializedDeviceOk": "materializedTileGeometryOk and materializedWorkgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup and tunables.MATERIALIZED_WORKGROUP_DIM <= device.limits.maxComputeWorkgroupSizeX and tunables.MATERIALIZED_WORKGROUP_DIM <= device.limits.maxComputeWorkgroupSizeY and materializedScoreStorageBytes <= device.limits.maxComputeWorkgroupStorageSize and materializedApplyStorageBytes <= device.limits.maxComputeWorkgroupStorageSize","materializedWideSimdOk": "device.features.has(\"subgroups\") or (has(device.adapterInfo, \"subgroupMinSize\") and device.adapterInfo.subgroupMinSize >= 16)","materializedF32CoreOk": "qkvContractOk and tensorDtypes.queryT == \"float32\" and attrs.unidirectional == 0 and headDimPlan >= 64 and headDimPlan <= 128 and dim(shapes.queryT, 1) >= 512 and dim(shapes.keyT, 1) >= 512 and dim(shapes.queryT, 0) * attrs.num_heads <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and ceilDiv(dim(shapes.keyT, 1), tunables.MATERIALIZED_KEY_TILE) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and materializedScoreFits and materializedDeviceOk","materializedSoftmaxStorageBytes": "tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE * 8 + 8","materializedSoftmaxResourcesFit": "tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE > 0 and pow2ceil(tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE) == tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE and tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE <= deviceWorkgroupCap and materializedSoftmaxStorageBytes <= device.limits.maxComputeWorkgroupStorageSize","materializedSgmatQueryTile": "tunables.MATERIALIZED_SGMAT_QUERY_TILE",17 unmodified lines"materializedSgmatResourcesFit": "materializedSgmatGeometryOk and materializedSgmatWorkgroupSize <= deviceWorkgroupCap and materializedSgmatCompactStorageBytes <= device.limits.maxComputeWorkgroupStorageSize","materializedSgmatDispatchFits": "ceilDiv(dim(shapes.keyT, 1), materializedSgmatKeyTile) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and dim(shapes.queryT, 0) * attrs.num_heads <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and ceilDiv(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), min(device.limits.maxComputeWorkgroupsPerDimension, 65535)) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","materializedSgmatDirectScoreStore": "dim(shapes.queryT, 1) % materializedSgmatQueryTile == 0 and dim(shapes.keyT, 1) % materializedSgmatKeyTile == 0","materializedSgmatDirectApplyStore": "dim(shapes.queryT, 1) % materializedSgmatQueryTile == 0 and headDimPlan % materializedSgmatKeyTile == 0","materializedSgmatRuntimeDirectStore": "dim(shapes.queryT, 1) >= 2 * materializedSgmatQueryTile and dim(shapes.keyT, 1) >= 2 * materializedSgmatKeyTile","materializedSgmatCoreOk": "qkvContractOk and tensorDtypes.queryT == \"float32\" and attrs.unidirectional == 0 and headDimPlan >= 32 and headDimPlan <= 256 and dim(shapes.queryT, 1) >= 512 and dim(shapes.keyT, 1) >= 512 and device.features.has(\"subgroups\") and wave32Effective and device.features.has(\"chromium-experimental-subgroup-matrix\") and materializedSgmatBuffersFit and materializedSgmatResourcesFit and materializedSgmatDispatchFits","materializedCachedSoftmaxWg": "min(tunables.MATERIALIZED_CACHED_SOFTMAX_WORKGROUP_SIZE, device.limits.maxComputeInvocationsPerWorkgroup, device.limits.maxComputeWorkgroupSizeX)","materializedCachedSoftmaxVecsPerLane": "ceilDiv(ceilDiv(dim(shapes.keyT, 1), 4), max(1, materializedCachedSoftmaxWg))","materializedCachedSoftmaxStorageBytes": "materializedCachedSoftmaxWg * 8 + 8",3 unmodified lines"materializedSgmatOk": "materializedSgmatCoreOk and materializedAdaptiveSoftmaxOk","materializedSgmatFusedOk": "materializedSgmatCoreOk","materializedF32Ok": "materializedF32CoreOk and materializedAdaptiveSoftmaxOk","clusterTileKWg64": "max(1, min(tunables.FLASH_MAX_TILE_K, floor(device.limits.maxComputeWorkgroupStorageSize / (headDimPlan * (8 if tensorDtypes.queryT != \"float16\" else 4) + tunables.FLASH_CLUSTER_WG_SMALL * 4))))","clusterTileKWg128": "max(1, min(tunables.FLASH_MAX_TILE_K, floor(device.limits.maxComputeWorkgroupStorageSize / (headDimPlan * (8 if tensorDtypes.queryT != \"float16\" else 4) + tunables.FLASH_CLUSTER_WG_LARGE * 4))))","smallHeadParallelOk": "qkvContractOk and headDimPlan < 32 and dim(shapes.keyT, 1) >= 64 and dim(shapes.keyT, 1) <= 2048 and attrs.unidirectional == 0 and dim(shapes.queryT, 0) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and attrs.num_heads <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","prefillTiledStorageBytes": "headDimPlan * tunables.PREFILL_QUERY_TILE * 4","prefillTiledDeviceOk": "tunables.PREFILL_QUERY_TILE <= deviceWorkgroupCap and prefillTiledStorageBytes <= device.limits.maxComputeWorkgroupStorageSize","portableWorkgroupSize": "min(tunables.WORKGROUP_SIZE, pow2ceil(max(1, headDimPlan)))","portableWorkgroupStorageBytes": "portableWorkgroupSize * 4 + max(1, headDimPlan) * 4 + 16","portableWorkgroupOk": "tunables.WORKGROUP_SIZE > 0 and pow2ceil(tunables.WORKGROUP_SIZE) == tunables.WORKGROUP_SIZE and portableWorkgroupSize <= deviceWorkgroupCap and portableWorkgroupStorageBytes <= device.limits.maxComputeWorkgroupStorageSize","fallbackShapeOk": "qkvContractOk and portableWorkgroupOk","fallbackMaskShapeOk": "qkvMaskContractOk and portableWorkgroupOk","smallSeqShapeOk": "qkvContractOk and tensorDtypes.queryT == \"float32\" and dim(shapes.queryT, 1) >= 1 and dim(shapes.queryT, 1) <= tunables.SMALL_SEQ_MAX and dim(shapes.keyT, 1) >= 1 and dim(shapes.keyT, 1) <= tunables.SMALL_SEQ_MAX and headDimPlan >= 1 and attrs.unidirectional == 0","smallSeqPrivateFloats": "dim(shapes.keyT, 1) + headDimPlan","smallSeqWorkgroupSize": "max(32, pow2ceil(dim(shapes.queryT, 1)))","smallSeqSharedBytes": "dim(shapes.keyT, 1) * headDimPlan * 8","smallSeqResourcesFit": "smallSeqPrivateFloats <= tunables.SMALL_SEQ_MAX_PRIVATE_FLOATS and smallSeqWorkgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup and smallSeqWorkgroupSize <= device.limits.maxComputeWorkgroupSizeX and smallSeqSharedBytes <= device.limits.maxComputeWorkgroupStorageSize","smallSeqBlockedKvBytes": "dim(shapes.keyT, 1) * headDimPlan * 8","smallSeqBlockedLaneBytes": "8 + headDimPlan * 4","smallSeqBlockedKeyLanes": "min(pow2ceil(dim(shapes.keyT, 1)), 16 if smallSeqBlockedKvBytes + tunables.SMALL_SEQ_BLOCKED_QUERY_BLOCK * 16 * smallSeqBlockedLaneBytes <= device.limits.maxComputeWorkgroupStorageSize else (8 if smallSeqBlockedKvBytes + tunables.SMALL_SEQ_BLOCKED_QUERY_BLOCK * 8 * smallSeqBlockedLaneBytes <= device.limits.maxComputeWorkgroupStorageSize else 4))","smallSeqBlockedWorkgroupSize": "tunables.SMALL_SEQ_BLOCKED_QUERY_BLOCK * smallSeqBlockedKeyLanes","smallSeqBlockedSharedBytes": "smallSeqBlockedKvBytes + smallSeqBlockedWorkgroupSize * smallSeqBlockedLaneBytes","smallSeqBlockedShapeOk": "qkvContractOk and tensorDtypes.queryT == \"float32\" and headDimPlan % 4 == 0 and headDimPlan >= 4 and headDimPlan <= tunables.SMALL_SEQ_BLOCKED_MAX_HEAD_DIM and dim(shapes.keyT, 1) >= 1 and dim(shapes.keyT, 1) <= tunables.SMALL_SEQ_BLOCKED_MAX_KV and dim(shapes.queryT, 1) >= 1","smallSeqBlockedFits": "smallSeqBlockedWorkgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup and smallSeqBlockedWorkgroupSize <= device.limits.maxComputeWorkgroupSizeX and smallSeqBlockedSharedBytes <= device.limits.maxComputeWorkgroupStorageSize and ceilDiv(dim(shapes.queryT, 1), tunables.SMALL_SEQ_BLOCKED_QUERY_BLOCK) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and attrs.num_heads <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and dim(shapes.queryT, 0) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","smallSeqDispatchFits": "attrs.num_heads <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and dim(shapes.queryT, 0) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","materializedSgmatCoreF16Ok": "qkvContractOk and tensorDtypes.queryT == \"float16\" and tensorDtypes.keyT == \"float16\" and tensorDtypes.valueT == \"float16\" and device.features.has(\"shader-f16\") and attrs.unidirectional == 0 and headDimPlan >= 32 and headDimPlan <= 256 and dim(shapes.queryT, 1) >= 512 and dim(shapes.keyT, 1) >= 512 and device.features.has(\"subgroups\") and wave32Effective and device.features.has(\"chromium-experimental-subgroup-matrix\") and materializedSgmatBuffersFit and materializedSgmatResourcesFit and materializedSgmatDispatchFits","materializedSgmatFusedF16Ok": "materializedSgmatCoreF16Ok","attentionScaleExpression": "\"select(inverseSqrt(f32(HEAD_DIM)), params.scale, params.scale != 0.0)\""},"bindings": {"query": { "arg": "queryT", "buffer": "read-only-storage", "elementType": "$inputElement" },"key": { "arg": "keyT", "buffer": "read-only-storage", "elementType": "$inputElement" },"value": { "arg": "valueT", "buffer": "read-only-storage", "elementType": "$inputElement" },"bias": { "arg": "biasT", "buffer": "read-only-storage", "elementType": "$inputScalar" },"output": { "arg": "outputT", "buffer": "storage", "elementType": "$outputElement" },"params": {"buffer": "uniform","struct": [{ "name": "qSeq", "type": "u32", "value": "dim(shapes.queryT, 1)" },{ "name": "kvSeq", "type": "u32", "value": "dim(shapes.keyT, 1)" },1 unmodified line{ "name": "isCausal", "type": "u32", "value": "attrs.unidirectional" }]},"params_2": {"name": "params","buffer": "uniform","struct": [{ "name": "qSeq", "type": "u32", "value": "dim(shapes.queryT, 1)" },{ "name": "scale", "type": "f32", "value": "attrs.scale if has(attrs, \"scale\") else 0" }]},"q": { "arg": "queryT", "buffer": "read-only-storage", "elementType": "$scalar" },"k": { "arg": "keyT", "buffer": "read-only-storage", "elementType": "$scalar" },"v": { "arg": "valueT", "buffer": "read-only-storage", "elementType": "$scalar" },"y": { "arg": "outputT", "buffer": "storage", "elementType": "$scalar" },"params_4": {"name": "params","buffer": "uniform","struct": [{ "name": "qSeq", "type": "u32", "value": "dim(shapes.queryT, 1)" },{ "name": "kvSeq", "type": "u32", "value": "dim(shapes.keyT, 1)" },{ "name": "scale", "type": "f32", "value": "attrs.scale if has(attrs, \"scale\") else 0" }]},"query_2": { "arg": "queryT", "name": "query", "buffer": "read-only-storage", "elementType": "f32" },"key_2": { "arg": "keyT", "name": "key", "buffer": "read-only-storage", "elementType": "f32" },"scores": { "scratch": "materializedScores", "buffer": "storage", "elementType": "f32" },"scorePartials": { "scratch": "materializedScorePartials", "buffer": "storage", "elementType": "f32" },"bias_2": { "arg": "biasT", "name": "bias", "buffer": "read-only-storage", "elementType": "f32" },"scorePartials_2": {"scratch": "materializedScorePartials","name": "scorePartials","buffer": "read-only-storage","elementType": "f32"},"rowStats": { "scratch": "materializedRowStats", "buffer": "storage", "elementType": "f32" },"params_6": {"name": "params","buffer": "uniform","struct": [{ "name": "rows", "type": "u32", "value": "dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1)" }]},"scores_2": {"scratch": "materializedScores","name": "scores","buffer": "read-only-storage","elementType": "f32"},"value_2": { "arg": "valueT", "name": "value", "buffer": "read-only-storage", "elementType": "f32" },"output_2": { "arg": "outputT", "name": "output", "buffer": "storage", "elementType": "f32" },"rowStats_2": {"scratch": "materializedRowStats","name": "rowStats","buffer": "read-only-storage","elementType": "f32"},"params_7": {"name": "params","buffer": "uniform","struct": [{ "name": "qSeq", "type": "u32", "value": "dim(shapes.queryT, 1)" },{ "name": "kvSeq", "type": "u32", "value": "dim(shapes.keyT, 1)" }]},"attn_mask_2": {"arg": "attentionBiasT","name": "attn_mask","buffer": "read-only-storage","elementType": "$maskElement"},"params_8": {"name": "params","buffer": "uniform","struct": [{ "name": "qSeq", "type": "u32", "value": "dim(shapes.queryT, 1)" },{ "name": "kvSeq", "type": "u32", "value": "dim(shapes.keyT, 1)" },12 unmodified lines{ "name": "maskSeqStride", "type": "u32", "value": "dim(shapes.attentionBiasT, 3)" }]},"query_3": { "arg": "queryT", "name": "query", "buffer": "read-only-storage", "elementType": "$inputVec4" },"key_3": { "arg": "keyT", "name": "key", "buffer": "read-only-storage", "elementType": "$inputVec4" },"value_3": { "arg": "valueT", "name": "value", "buffer": "read-only-storage", "elementType": "$inputVec4" },"partial_out": { "scratch": "partialOut", "buffer": "storage", "elementType": "vec4<f32>" },"partial_stats": { "scratch": "partialStats", "buffer": "storage", "elementType": "vec2<f32>" },"params_9": {"name": "params","buffer": "uniform","struct": [{ "name": "kvSeq", "type": "u32", "value": "dim(shapes.keyT, 1)" },{ "name": "scale", "type": "f32", "value": "attrs.scale if has(attrs, \"scale\") else 0" }]},"partial_out_2": {"scratch": "partialOut","name": "partial_out","buffer": "read-only-storage","elementType": "vec4<f32>"},"partial_stats_2": {"scratch": "partialStats","name": "partial_stats","buffer": "read-only-storage","elementType": "vec2<f32>"},"output_3": { "arg": "outputT", "name": "output", "buffer": "storage", "elementType": "$inputVec4" },"scores_3": {"scratch": "materializedScores","name": "scores","buffer": "storage","elementType": "$softmaxElementType"}},"variants": [{"id": "qkv_bias_small_seq_blocked","priority": 40,"when": ["biasOk", "smallSeqBlockedShapeOk", "not flashShapeOk", "smallSeqBlockedFits"],"derive": {"usesF16": false,"scalar": "\"f32\"","inputElement": "\"vec4<f32>\"","outputElement": "\"vec4<f32>\"","inputScalar": "\"f32\"","hasBias": true,"headDim": "headDimPlan","headDimV4": "headDimPlan / 4","hidden": "dim(shapes.queryT, 2)","hiddenV4": "dim(shapes.queryT, 2) / 4","kvSeq": "dim(shapes.keyT, 1)",10 unmodified lines"x": "ceilDiv(dim(shapes.queryT, 1), tunables.SMALL_SEQ_BLOCKED_QUERY_BLOCK)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"},"profile": {"op": "\"multi_head_attention\"","variant": "\"qkv_bias_small_seq_blocked\"","numHeads": "attrs.num_heads","headDim": "headDim"}}]3 unmodified lines"priority": 40,"when": ["not present.biasT", "smallSeqBlockedShapeOk", "not flashShapeOk", "smallSeqBlockedFits"],"derive": {"usesF16": false,"scalar": "\"f32\"","inputElement": "\"vec4<f32>\"","outputElement": "\"vec4<f32>\"","inputScalar": "\"f32\"","hasBias": false,"headDim": "headDimPlan","headDimV4": "headDimPlan / 4","hidden": "dim(shapes.queryT, 2)","hiddenV4": "dim(shapes.queryT, 2) / 4","kvSeq": "dim(shapes.keyT, 1)",10 unmodified lines"x": "ceilDiv(dim(shapes.queryT, 1), tunables.SMALL_SEQ_BLOCKED_QUERY_BLOCK)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"},"profile": {"op": "\"multi_head_attention\"","variant": "\"qkv_no_bias_small_seq_blocked\"","numHeads": "attrs.num_heads","headDim": "headDim"}}]3 unmodified lines"priority": 60,"when": ["not present.biasT", "smallSeqShapeOk", "not flashShapeOk", "smallSeqResourcesFit", "smallSeqDispatchFits"],"derive": {"usesF16": false,"inputElement": "\"f32\"","outputElement": "\"f32\"","headDim": "dim(shapes.queryT, 2) / attrs.num_heads",6 unmodified lines"id": "main","name": "MultiHeadAttention","shader": "mha-small-seq.wgsl.jinja","bindings": ["query", "key", "value", "output", "params_2"],"dispatch": { "x": "attrs.num_heads", "y": "dim(shapes.queryT, 0)" },"profile": {"op": "\"multi_head_attention\"","variant": "\"qkv_no_bias_small_seq\"","numHeads": "attrs.num_heads","headDim": "dim(shapes.queryT, 2) / attrs.num_heads"}}]},{"id": "qkv_no_bias_tiled_nosg","priority": 19,"when": ["not present.biasT", "qkvContractOk", "not smallHeadParallelOk", "headDimPlan % 4 == 0", "headDimPlan <= 128", "dim(shapes.queryT, 1) >= 31", "prefillTiledDeviceOk"],"supersededBy": ["qkv_no_bias_flash_cluster_nosg", "qkv_no_bias_flash_cluster_lpq4_nosg"],"derive": {"scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","usesF16": "tensorDtypes.queryT == \"float16\"","blockM": "tunables.PREFILL_QUERY_TILE","vHeadCap": "dim(shapes.queryT, 2) / attrs.num_heads"},28 unmodified lines}],"dispatch": {"x": "min(ceilDiv((dim(shapes.outputT, 0) * attrs.num_heads * ceil(dim(shapes.outputT, 1) / 32) * 32), (32)), 65535)","y": "ceilDiv(ceilDiv((dim(shapes.outputT, 0) * attrs.num_heads * ceil(dim(shapes.outputT, 1) / 32) * 32), (32)), 65535)","z": 1}}2 unmodified lines{"id": "qkv_bias_flash_q32_broadcast_f32_d128","priority": 30,"when": ["tensorDtypes.queryT == \"float32\"", "biasOk", "attrs.unidirectional == 0", "flashShapeOk", "q32BroadcastF32RegisterGeometry", "dim(shapes.queryT, 1) >= 31", "ceilDiv(dim(shapes.queryT, 1), 32) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)", "subgroupsWave32"],"requires": { "features": ["subgroups"] },"derive": {"hasBias": true,"hasCausal": false,"usesF16": false,"scalar": "\"f32\"","inputVec4": "\"vec4<f32>\"","inputElement": "\"vec4<f32>\"","outputElement": "\"vec4<f32>\"","inputScalar": "\"f32\"",11 unmodified lines"name": "MultiHeadAttention.FlashQ32BroadcastF32Bias","shader": "attn-flash-q32-broadcast.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "bias", "output", "params_4"],"dispatch": {"x": "ceilDiv(dim(shapes.queryT, 1), 32)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"},"subgroupCollectivesWidth": 32}]},2 unmodified lines"priority": 10,"when": ["not present.biasT", "smallHeadParallelOk"],"derive": {"usesF16": "tensorDtypes.queryT == \"float16\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",4 unmodified lines"passes": [{"id": "main","name": "MultiHeadAttentionSmallHeadParallel","shader": "attn-small-head-parallel.wgsl.jinja","bindings": ["query", "key", "value", "output", "params_2"],"dispatch": {"x": "min(dim(shapes.queryT, 1), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": "attrs.num_heads",5 unmodified lines{"id": "qkv_no_bias_tiled_attn_bias_nosg","priority": 17,"when": ["not present.biasT", "qkvMaskContractOk", "not smallHeadParallelOk", "headDimPlan % 4 == 0", "headDimPlan <= 128", "dim(shapes.queryT, 1) >= 31", "prefillTiledDeviceOk"],"derive": {"scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","usesF16": "tensorDtypes.queryT == \"float16\"","hasMask": true,"maskIsBool": false,"blockM": "tunables.PREFILL_QUERY_TILE","vHeadCap": "dim(shapes.queryT, 2) / attrs.num_heads"},40 unmodified lines}],"dispatch": {"x": "min(ceilDiv((dim(shapes.outputT, 0) * attrs.num_heads * ceil(dim(shapes.outputT, 1) / 32) * 32), (32)), 65535)","y": "ceilDiv(ceilDiv((dim(shapes.outputT, 0) * attrs.num_heads * ceil(dim(shapes.outputT, 1) / 32) * 32), (32)), 65535)","z": 1}}9 unmodified lines},"derive": {"qNumHeads": "attrs.num_heads","headDim": "headDimPlan","qHidden": "dim(shapes.queryT, 2)","hasBias": false,"useSubgroups": true,"statSlots": "materializedSgmatStatSlots","statQuerySeq": "dim(shapes.queryT, 1)"},12 unmodified lines"name": "MultiHeadAttention.MaterializedScoresSgmat","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"score\"", "emitRowStats": true },"bindings": ["query_2", "key_2", "scores", "scorePartials", "params_4"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)","z": "dim(shapes.queryT, 0) * attrs.num_heads"},"subgroupCollectivesWidth": 32},{"id": "rowstats","name": "MultiHeadAttention.MaterializedRowStatsCombine","shader": "attn-materialized-rowstats-combine-f32.wgsl.jinja","bindings": ["scorePartials_2", "rowStats", "params_6"],"dispatch": {"x": "min(ceilDiv((dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1)), (materializedRowStatsWg)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": 1,5 unmodified lines"name": "MultiHeadAttention.MaterializedApplySgmat","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"apply\"", "fusedSoftmax": true },"bindings": ["scores_2", "value_2", "output_2", "rowStats_2", "params_7"],"dispatch": {"x": "ceilDiv(headDim, materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",12 unmodified lines},"derive": {"qNumHeads": "attrs.num_heads","headDim": "headDimPlan","qHidden": "dim(shapes.queryT, 2)","hasBias": true,"useSubgroups": true,"statSlots": "materializedSgmatStatSlots","statQuerySeq": "dim(shapes.queryT, 1)"},12 unmodified lines"name": "MultiHeadAttention.MaterializedScoresSgmatBias","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"score\"", "emitRowStats": true },"bindings": ["query_2", "key_2", "bias_2", "scores", "scorePartials", "params_4"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)","z": "dim(shapes.queryT, 0) * attrs.num_heads"},"subgroupCollectivesWidth": 32},{"id": "rowstats","name": "MultiHeadAttention.MaterializedRowStatsCombineBias","shader": "attn-materialized-rowstats-combine-f32.wgsl.jinja","bindings": ["scorePartials_2", "rowStats", "params_6"],"dispatch": {"x": "min(ceilDiv((dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1)), (materializedRowStatsWg)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": 1,5 unmodified lines"name": "MultiHeadAttention.MaterializedApplySgmatBias","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"apply\"", "fusedSoftmax": true },"bindings": ["scores_2", "value_2", "bias_2", "output_2", "rowStats_2", "params_7"],"dispatch": {"x": "ceilDiv(headDim, materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",12 unmodified lines},"derive": {"qNumHeads": "attrs.num_heads","headDim": "headDimPlan","qHidden": "dim(shapes.queryT, 2)","hasBias": false,"useSubgroups": true,"statSlots": "materializedSgmatStatSlots","statQuerySeq": "dim(shapes.queryT, 1)","operandF16": true,15 unmodified lines"name": "MultiHeadAttention.MaterializedScoresSgmatF16","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"score\"", "emitRowStats": true },"bindings": ["query", "key", "scores", "scorePartials", "params_4"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)","z": "dim(shapes.queryT, 0) * attrs.num_heads"},"subgroupCollectivesWidth": 32},{"id": "rowstats","name": "MultiHeadAttention.MaterializedRowStatsCombine","shader": "attn-materialized-rowstats-combine-f32.wgsl.jinja","bindings": ["scorePartials_2", "rowStats", "params_6"],"dispatch": {"x": "min(ceilDiv((dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1)), (materializedRowStatsWg)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": 1,5 unmodified lines"name": "MultiHeadAttention.MaterializedApplySgmatF16","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"apply\"", "fusedSoftmax": true },"bindings": ["scores_2", "value", "output", "rowStats_2", "params_7"],"dispatch": {"x": "ceilDiv(headDim, materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",5 unmodified lines{"id": "qkv_no_bias_flash_cluster_lpq4_nosg","priority": 20,"when": ["not present.biasT", "flashShapeOk", "headDimPlan % 16 == 0", "headDimPlan % 32 != 0", "dim(shapes.queryT, 1) >= 31"],"requires": {},"derive": {"hasBias": false,"hasCausal": true,"hasWindow": false,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",26 unmodified lines{"id": "qkv_no_bias_flash_cluster_nosg","priority": 20,"when": ["not present.biasT", "flashShapeOk", "headDimPlan % 32 == 0", "dim(shapes.queryT, 1) >= 31"],"requires": {},"derive": {"hasBias": false,"hasCausal": true,"hasWindow": false,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",26 unmodified lines{"id": "qkv_bias_flash_cluster_nosg","priority": 19,"when": ["biasOk", "flashShapeOk", "headDimPlan % 32 == 0", "dim(shapes.queryT, 1) >= 31"],"requires": {},"derive": {"hasBias": true,"hasCausal": true,"hasWindow": false,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",28 unmodified lines{"id": "qkv_no_bias_flash_cluster_lpq4","priority": 22,"when": ["not present.biasT", "flashShapeOk", "headDimPlan % 16 == 0", "headDimPlan % 32 != 0", "dim(shapes.queryT, 1) >= 31", "subgroupCluster4"],"requires": { "features": ["subgroups"] },"derive": {"hasBias": false,"hasCausal": true,"hasWindow": false,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",17 unmodified lines"x": "ceilDiv(dim(shapes.queryT, 1), TILE_Q)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"},"subgroupCollectivesWidth": "portable"}]},{"id": "qkv_no_bias_flash_cluster","priority": 22,"when": ["not present.biasT", "flashShapeOk", "headDimPlan % 32 == 0", "dim(shapes.queryT, 1) >= 31", "subgroupCluster8"],"requires": { "features": ["subgroups"] },"derive": {"hasBias": false,"hasCausal": true,"hasWindow": false,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",17 unmodified lines"x": "ceilDiv(dim(shapes.queryT, 1), TILE_Q)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"},"subgroupCollectivesWidth": "portable"}]},{"id": "qkv_bias_flash_cluster","priority": 21,"when": ["biasOk", "flashShapeOk", "headDimPlan % 32 == 0", "dim(shapes.queryT, 1) >= 31", "subgroupCluster8"],"requires": { "features": ["subgroups"] },"derive": {"hasBias": true,"hasCausal": true,"hasWindow": false,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",19 unmodified lines"x": "ceilDiv(dim(shapes.queryT, 1), TILE_Q)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"},"subgroupCollectivesWidth": "portable"}]},{"id": "qkv_no_bias_flash_cluster_attn_bias","priority": 22,"when": ["not present.biasT", "flashMaskShapeOk", "headDimPlan % 32 == 0", "dim(shapes.queryT, 1) >= 31", "subgroupCluster8"],"requires": { "features": ["subgroups"] },"derive": {"hasBias": false,"hasCausal": true,"hasWindow": false,"hasMask": true,"maskIsBool": false,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",13 unmodified lines"name": "MultiHeadAttention.Flash","shader": "attn-flash-prefill-cluster.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_2", "output", "params_8"],"dispatch": {"x": "ceilDiv(dim(shapes.queryT, 1), TILE_Q)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"},"subgroupCollectivesWidth": "portable"}]},{"id": "qkv_bias_flash_cluster_attn_bias","priority": 21,"when": ["biasOk", "flashMaskShapeOk", "headDimPlan % 32 == 0", "dim(shapes.queryT, 1) >= 31", "subgroupCluster8"],"requires": { "features": ["subgroups"] },"derive": {"hasBias": true,"hasCausal": true,"hasWindow": false,"hasMask": true,"maskIsBool": false,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",15 unmodified lines"name": "MultiHeadAttention.Flash","shader": "attn-flash-prefill-cluster.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_2", "bias", "output", "params_8"],"dispatch": {"x": "ceilDiv(dim(shapes.queryT, 1), TILE_Q)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"},"subgroupCollectivesWidth": "portable"}]},3 unmodified lines"when": ["not present.biasT", "decodeSplitKNoBiasOk or shortQuerySplitKNoBiasOk"],"requires": {},"derive": {"combineSubgroups": false,"useSubgroups": false,"hasWindow": false,"splitQueries": true,"scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"",25 unmodified lines"name": "MultiHeadAttention.DecodeSplitKNoSg","shader": "attn-flash-decode-splitk.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query_3", "key_3", "value_3", "partial_out", "partial_stats", "params_9"],"dispatch": {"x": "dim(shapes.queryT, 1) * noBiasSplitKCount","y": "attrs.num_heads",5 unmodified lines"name": "MultiHeadAttention.DecodeSplitKMergeNoSg","shader": "attn-flash-decode-splitk-merge.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["partial_out_2", "partial_stats_2", "output_3"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]4 unmodified lines"when": ["biasOk", "decodeSplitKBiasOk"],"requires": {},"derive": {"combineSubgroups": false,"useSubgroups": false,"hasBias": true,"hasWindow": false,"scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",25 unmodified lines"name": "MultiHeadAttention.DecodeSplitKBiasNoSg","shader": "attn-flash-decode-splitk.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query_3", "key_3", "value_3", "bias", "partial_out", "partial_stats", "params_9"],"dispatch": { "x": "biasSplitKCount", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }},{1 unmodified line"name": "MultiHeadAttention.DecodeSplitKMergeBiasNoSg","shader": "attn-flash-decode-splitk-merge.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["partial_out_2", "partial_stats_2", "bias", "output_3"],"dispatch": { "x": 1, "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]5 unmodified lines"demoteWhen": ["decodeSplitKPortablePreferred"],"requires": { "features": ["subgroups"] },"derive": {"combineSubgroups": true,"hasWindow": false,"splitQueries": true,"scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"",25 unmodified lines"name": "MultiHeadAttention.DecodeSplitK","shader": "attn-flash-decode-splitk.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query_3", "key_3", "value_3", "partial_out", "partial_stats", "params_9"],"dispatch": {"x": "dim(shapes.queryT, 1) * noBiasSplitKCount","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"},"subgroupCollectivesWidth": "portable"},{"id": "merge","name": "MultiHeadAttention.DecodeSplitKMerge","shader": "attn-flash-decode-splitk-merge.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["partial_out_2", "partial_stats_2", "output_3"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]4 unmodified lines"when": ["biasOk", "decodeSplitKBiasOk"],"requires": { "features": ["subgroups"] },"derive": {"combineSubgroups": true,"hasBias": true,"hasWindow": false,"scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",25 unmodified lines"name": "MultiHeadAttention.DecodeSplitKBias","shader": "attn-flash-decode-splitk.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query_3", "key_3", "value_3", "bias", "partial_out", "partial_stats", "params_9"],"dispatch": { "x": "biasSplitKCount", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" },"subgroupCollectivesWidth": "portable"},{"id": "merge","name": "MultiHeadAttention.DecodeSplitKMergeBias","shader": "attn-flash-decode-splitk-merge.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["partial_out_2", "partial_stats_2", "bias", "output_3"],"dispatch": { "x": 1, "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]6 unmodified lines"derive": {"qNumHeads": "attrs.num_heads","kvNumHeads": "attrs.num_heads","headDim": "headDimPlan","materializedInnerTile": "tunables.MATERIALIZED_INNER_TILE","materializedQueryTile": "tunables.MATERIALIZED_QUERY_TILE","materializedKeyTile": "tunables.MATERIALIZED_KEY_TILE",3 unmodified lines"materializedSoftmaxCols4": "dim(shapes.keyT, 1) / 4","softmaxElementType": "\"vec4<f32>\" if materializedCachedSoftmaxOk else \"f32\"","applyTileN": "materializedApplyTileN","hasBias": false,"useSubgroups": "device.features.has(\"subgroups\")"},"intermediates": [9 unmodified lines"name": "MultiHeadAttention.MaterializedScores","shader": "attn-materialized-score-f32.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query_2", "key_2", "scores", "params_4"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), tunables.MATERIALIZED_KEY_TILE)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",5 unmodified lines"name": "MultiHeadAttention.MaterializedSoftmax","shader": "attn-materialized-softmax-f32.wgsl.jinja","derive": { "cacheVec4": "materializedCachedSoftmaxOk" },"bindings": ["scores_3", "params_6"],"dispatch": {"x": "min(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","y": "ceilDiv(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","z": 1},"subgroupCollectivesWidth": "portable"},{"id": "apply","name": "MultiHeadAttention.MaterializedApply","shader": "attn-materialized-apply-f32.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["scores_2", "value_2", "output_2", "params_7"],"dispatch": {"x": "ceilDiv(headDim, applyTileN)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",9 unmodified lines"derive": {"qNumHeads": "attrs.num_heads","kvNumHeads": "attrs.num_heads","headDim": "headDimPlan","materializedInnerTile": "tunables.MATERIALIZED_INNER_TILE","materializedQueryTile": "tunables.MATERIALIZED_QUERY_TILE","materializedKeyTile": "tunables.MATERIALIZED_KEY_TILE",3 unmodified lines"materializedSoftmaxCols4": "dim(shapes.keyT, 1) / 4","softmaxElementType": "\"vec4<f32>\" if materializedCachedSoftmaxOk else \"f32\"","applyTileN": "materializedApplyTileN","hasBias": true,"useSubgroups": "device.features.has(\"subgroups\")"},"intermediates": [9 unmodified lines"name": "MultiHeadAttention.MaterializedScoresBias","shader": "attn-materialized-score-f32.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query_2", "key_2", "bias_2", "scores", "params_4"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), tunables.MATERIALIZED_KEY_TILE)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",5 unmodified lines"name": "MultiHeadAttention.MaterializedSoftmaxBias","shader": "attn-materialized-softmax-f32.wgsl.jinja","derive": { "cacheVec4": "materializedCachedSoftmaxOk" },"bindings": ["scores_3", "params_6"],"dispatch": {"x": "min(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","y": "ceilDiv(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","z": 1},"subgroupCollectivesWidth": "portable"},{"id": "apply","name": "MultiHeadAttention.MaterializedApplyBias","shader": "attn-materialized-apply-f32.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["scores_2", "value_2", "bias_2", "output_2", "params_7"],"dispatch": {"x": "ceilDiv(headDim, applyTileN)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",10 unmodified lines"derive": {"qNumHeads": "attrs.num_heads","kvNumHeads": "attrs.num_heads","headDim": "headDimPlan","materializedInnerTile": "tunables.MATERIALIZED_INNER_TILE","materializedQueryTile": "tunables.MATERIALIZED_QUERY_TILE","materializedKeyTile": "tunables.MATERIALIZED_KEY_TILE","materializedWorkgroupDim": "tunables.MATERIALIZED_WORKGROUP_DIM","applyTileN": "materializedApplyTileN","hasBias": false,"useSubgroups": "device.features.has(\"subgroups\")","statSlots": "materializedGemmStatSlots","statQuerySeq": "dim(shapes.queryT, 1)"},12 unmodified lines"name": "MultiHeadAttention.MaterializedScoresFused","shader": "attn-materialized-score-f32.wgsl.jinja","derive": { "layout": "\"bsh\"", "emitRowStats": true },"bindings": ["query_2", "key_2", "scores", "scorePartials", "params_4"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), tunables.MATERIALIZED_KEY_TILE)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",5 unmodified lines"name": "MultiHeadAttention.MaterializedGemmRowStatsCombine","shader": "attn-materialized-rowstats-combine-f32.wgsl.jinja","derive": { "maxOnly": true },"bindings": ["scorePartials_2", "rowStats", "params_6"],"dispatch": {"x": "min(ceilDiv((dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1)), (materializedRowStatsWg)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": 1,5 unmodified lines"name": "MultiHeadAttention.MaterializedApplyFused","shader": "attn-materialized-apply-f32.wgsl.jinja","derive": { "layout": "\"bsh\"", "fusedSoftmax": true },"bindings": ["scores_2", "value_2", "output_2", "rowStats_2", "params_7"],"dispatch": {"x": "ceilDiv(headDim, applyTileN)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",9 unmodified lines"derive": {"qNumHeads": "attrs.num_heads","kvNumHeads": "attrs.num_heads","headDim": "headDimPlan","materializedInnerTile": "tunables.MATERIALIZED_INNER_TILE","materializedQueryTile": "tunables.MATERIALIZED_QUERY_TILE","materializedKeyTile": "tunables.MATERIALIZED_KEY_TILE","materializedWorkgroupDim": "tunables.MATERIALIZED_WORKGROUP_DIM","applyTileN": "materializedApplyTileN","hasBias": true,"useSubgroups": "device.features.has(\"subgroups\")","statSlots": "materializedGemmStatSlots","statQuerySeq": "dim(shapes.queryT, 1)"},12 unmodified lines"name": "MultiHeadAttention.MaterializedScoresBiasFused","shader": "attn-materialized-score-f32.wgsl.jinja","derive": { "layout": "\"bsh\"", "emitRowStats": true },"bindings": ["query_2", "key_2", "bias_2", "scores", "scorePartials", "params_4"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), tunables.MATERIALIZED_KEY_TILE)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",5 unmodified lines"name": "MultiHeadAttention.MaterializedGemmRowStatsCombineBias","shader": "attn-materialized-rowstats-combine-f32.wgsl.jinja","derive": { "maxOnly": true },"bindings": ["scorePartials_2", "rowStats", "params_6"],"dispatch": {"x": "min(ceilDiv((dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1)), (materializedRowStatsWg)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": 1,5 unmodified lines"name": "MultiHeadAttention.MaterializedApplyBiasFused","shader": "attn-materialized-apply-f32.wgsl.jinja","derive": { "layout": "\"bsh\"", "fusedSoftmax": true },"bindings": ["scores_2", "value_2", "bias_2", "output_2", "rowStats_2", "params_7"],"dispatch": {"x": "ceilDiv(headDim, applyTileN)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",5 unmodified lines{"id": "qkv_no_bias_flash_q32_broadcast","priority": 30,"when": ["tensorDtypes.queryT == \"float16\"", "not present.biasT", "flashShapeOk", "(dim(shapes.queryT, 2) / attrs.num_heads) % 4 == 0", "(dim(shapes.queryT, 2) / attrs.num_heads) >= 64", "(dim(shapes.queryT, 2) / attrs.num_heads) <= 256", "dim(shapes.queryT, 1) >= 31", "subgroupsWave32"],"requires": { "features": ["subgroups", "shader-f16"] },"derive": {"usesF16": true,"scalar": "\"f16\"","inputVec4": "\"vec4<f16>\"","inputElement": "\"vec4<f16>\"","outputElement": "\"vec4<f16>\"","qNumHeads": "attrs.num_heads",14 unmodified lines"x": "ceilDiv(dim(shapes.queryT, 1), 32)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"},"subgroupCollectivesWidth": 32}]},5 unmodified lines"derive": {"usesF16": true,"scalar": "\"f16\"","inputVec4": "\"vec4<f16>\"","inputElement": "\"vec4<f16>\"","outputElement": "\"vec4<f16>\"","qNumHeads": "attrs.num_heads",24 unmodified lines"priority": 0,"when": ["not present.biasT and fallbackMaskShapeOk"],"derive": {"headsFromParams": false,"hasBias": false,"hasCausal": true,"hasWindow": false,"hasKeyLimit": false,"hasMask": true,"scaleFallbackRsqrt": true,"usesF16": "tensorDtypes.queryT == \"float16\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",12 unmodified lines"name": "MultiHeadAttention","shader": "attn-online-scalar.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_2", "output", "params_8"],"dispatch": {"x": "min(dim(shapes.queryT, 1), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": "attrs.num_heads",7 unmodified lines"priority": 0,"when": ["biasOk and fallbackMaskShapeOk"],"derive": {"headsFromParams": false,"hasBias": true,"hasCausal": true,"hasWindow": false,"hasKeyLimit": false,"hasMask": true,"scaleFallbackRsqrt": true,"usesF16": "tensorDtypes.queryT == \"float16\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",12 unmodified lines"name": "MultiHeadAttention","shader": "attn-online-scalar.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_2", "bias", "output", "params_8"],"dispatch": {"x": "min(dim(shapes.queryT, 1), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": "attrs.num_heads",7 unmodified lines"priority": 0,"when": ["not present.biasT and fallbackShapeOk and not flashShapeOk"],"derive": {"headsFromParams": false,"hasBias": false,"hasCausal": true,"hasWindow": false,"hasKeyLimit": false,"scaleFallbackRsqrt": true,"usesF16": "tensorDtypes.queryT == \"float16\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",25 unmodified lines"priority": 0,"when": ["biasOk and fallbackShapeOk and not flashShapeOk"],"derive": {"headsFromParams": false,"hasBias": true,"hasCausal": true,"hasWindow": false,"hasKeyLimit": false,"scaleFallbackRsqrt": true,"usesF16": "tensorDtypes.queryT == \"float16\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",25 unmodified lines"priority": 20,"when": ["device.features.has(\"subgroups\")", "not present.biasT", "flashShapeOk"],"derive": {"headsFromParams": false,"hasBias": false,"hasCausal": true,"hasWindow": false,"combineSubgroups": true,"hasMask": false,"maskIsBool": false,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",10 unmodified lines"shader": "attn-flash-online.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "output", "params"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" },"subgroupCollectivesWidth": "portable"}]},2 unmodified lines"priority": 20,"when": ["device.features.has(\"subgroups\")", "biasOk", "flashShapeOk"],"derive": {"headsFromParams": false,"hasBias": true,"hasCausal": true,"hasWindow": false,"combineSubgroups": true,"hasMask": false,"maskIsBool": false,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",12 unmodified lines"shader": "attn-flash-online.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "bias", "output", "params"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" },"subgroupCollectivesWidth": "portable"}]},2 unmodified lines"priority": 18,"when": ["true", "not present.biasT", "flashShapeOk"],"derive": {"headsFromParams": false,"hasBias": false,"hasCausal": true,"hasWindow": false,"combineSubgroups": false,"hasMask": false,"maskIsBool": false,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",19 unmodified lines"priority": 17,"when": ["true", "biasOk", "flashShapeOk"],"derive": {"headsFromParams": false,"hasBias": true,"hasCausal": true,"hasWindow": false,"combineSubgroups": false,"hasMask": false,"maskIsBool": false,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",21 unmodified lines"priority": 20,"when": ["device.features.has(\"subgroups\")", "not present.biasT", "flashMaskShapeOk"],"derive": {"headsFromParams": false,"hasBias": false,"hasCausal": true,"hasWindow": false,"combineSubgroups": true,"hasMask": true,"maskIsBool": false,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",9 unmodified lines"name": "MultiHeadAttention.Flash","shader": "attn-flash-online.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_2", "output", "params_8"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" },"subgroupCollectivesWidth": "portable"}]},2 unmodified lines"priority": 20,"when": ["device.features.has(\"subgroups\")", "biasOk", "flashMaskShapeOk"],"derive": {"headsFromParams": false,"hasBias": true,"hasCausal": true,"hasWindow": false,"combineSubgroups": true,"hasMask": true,"maskIsBool": false,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",11 unmodified lines"name": "MultiHeadAttention.Flash","shader": "attn-flash-online.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_2", "bias", "output", "params_8"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" },"subgroupCollectivesWidth": "portable"}]},2 unmodified lines"priority": 18,"when": ["true", "not present.biasT", "flashMaskShapeOk"],"derive": {"headsFromParams": false,"hasBias": false,"hasCausal": true,"hasWindow": false,"combineSubgroups": false,"hasMask": true,"maskIsBool": false,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",9 unmodified lines"name": "MultiHeadAttention.NoBiasOnlineFlashNoSg","shader": "attn-flash-online.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_2", "output", "params_8"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]3 unmodified lines"priority": 17,"when": ["true", "biasOk", "flashMaskShapeOk"],"derive": {"headsFromParams": false,"hasBias": true,"hasCausal": true,"hasWindow": false,"combineSubgroups": false,"hasMask": true,"maskIsBool": false,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",11 unmodified lines"name": "MultiHeadAttention.BiasOnlineFlashNoSg","shader": "attn-flash-online.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_2", "bias", "output", "params_8"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]8 unmodified lines},"derive": {"qNumHeads": "attrs.num_heads","headDim": "headDimPlan","qHidden": "dim(shapes.queryT, 2)","materializedSoftmaxWg": "materializedCachedSoftmaxWg if materializedCachedSoftmaxOk else tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE","materializedSoftmaxCols": "dim(shapes.keyT, 1)","materializedSoftmaxCols4": "dim(shapes.keyT, 1) / 4","softmaxElementType": "\"vec4<f32>\" if materializedCachedSoftmaxOk else \"f32\"","hasBias": false,"useSubgroups": true},"intermediates": [9 unmodified lines"name": "MultiHeadAttention.MaterializedScoresSgmat","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"score\"" },"bindings": ["query_2", "key_2", "scores", "params_4"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",5 unmodified lines"name": "MultiHeadAttention.MaterializedSoftmaxSgmat","shader": "attn-materialized-softmax-f32.wgsl.jinja","derive": { "cacheVec4": "materializedCachedSoftmaxOk" },"bindings": ["scores_3", "params_6"],"dispatch": {"x": "min(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","y": "ceilDiv(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","z": 1},"subgroupCollectivesWidth": "portable"},{"id": "apply","name": "MultiHeadAttention.MaterializedApplySgmat","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"apply\"" },"bindings": ["scores_2", "value_2", "output_2", "params_7"],"dispatch": {"x": "ceilDiv(headDim, materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",12 unmodified lines},"derive": {"qNumHeads": "attrs.num_heads","headDim": "headDimPlan","qHidden": "dim(shapes.queryT, 2)","materializedSoftmaxWg": "materializedCachedSoftmaxWg if materializedCachedSoftmaxOk else tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE","materializedSoftmaxCols": "dim(shapes.keyT, 1)","materializedSoftmaxCols4": "dim(shapes.keyT, 1) / 4","softmaxElementType": "\"vec4<f32>\" if materializedCachedSoftmaxOk else \"f32\"","hasBias": true,"useSubgroups": true},"intermediates": [9 unmodified lines"name": "MultiHeadAttention.MaterializedScoresSgmatBias","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"score\"" },"bindings": ["query_2", "key_2", "bias_2", "scores", "params_4"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",5 unmodified lines"name": "MultiHeadAttention.MaterializedSoftmaxSgmatBias","shader": "attn-materialized-softmax-f32.wgsl.jinja","derive": { "cacheVec4": "materializedCachedSoftmaxOk" },"bindings": ["scores_3", "params_6"],"dispatch": {"x": "min(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","y": "ceilDiv(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","z": 1},"subgroupCollectivesWidth": "portable"},{"id": "apply","name": "MultiHeadAttention.MaterializedApplySgmatBias","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"apply\"" },"bindings": ["scores_2", "value_2", "bias_2", "output_2", "params_7"],"dispatch": {"x": "ceilDiv(headDim, materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",56 unmodified lines57585960616263642 unmodified lines67686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710817 unmodified lines1261271281291301311321331343 unmodified lines1381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841 unmodified line18618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125212 unmodified lines26526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237310 unmodified lines3843853863873883893 unmodified lines39339439539639739839940040110 unmodified lines4124134144154164173 unmodified lines4214224234244254266 unmodified lines43343443543643743843944044144244344444544644744844945028 unmodified lines4794804814824834844854862 unmodified lines48949049149249349449549649749849950011 unmodified lines5125135145155165175185195205215225232 unmodified lines5265275285295305314 unmodified lines5365375385395405415425435445 unmodified lines55055155255355455555655755840 unmodified lines5996006016026036046056069 unmodified lines61661761861962062162212 unmodified lines6356366376386396406416426436446456466476486496506516525 unmodified lines65865966066166266366412 unmodified lines67767867968068168268312 unmodified lines6966976986997007017027037047057067077087097107117127135 unmodified lines71972072172272372472512 unmodified lines73873974074174274374415 unmodified lines7607617627637647657667677687697707717727737747757767775 unmodified lines7837847857867877887895 unmodified lines79579679779879980080180280380480580626 unmodified lines83383483583683783883984084184284384426 unmodified lines87187287387487587687787887988088188228 unmodified lines91191291391491591691791891992092192217 unmodified lines94094194294394494594694794894995095195295395495595695795817 unmodified lines97697797897998098198298398498598698798898999099199299399419 unmodified lines101410151016101710181019102010211022102310241025102610271028102910301031103213 unmodified lines10461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106915 unmodified lines1085108610871088108910901091109210931094109510963 unmodified lines110011011102110311041105110625 unmodified lines11321133113411351136113711385 unmodified lines11441145114611471148114911504 unmodified lines115511561157115811591160116125 unmodified lines11871188118911901191119211931 unmodified line11951196119711981199120012015 unmodified lines12071208120912101211121225 unmodified lines12381239124012411242124312441245124612471248124912501251125212531254125512564 unmodified lines12611262126312641265126625 unmodified lines1292129312941295129612971298129913001301130213031304130513066 unmodified lines1313131413151316131713183 unmodified lines1322132313241325132613279 unmodified lines13371338133913401341134213435 unmodified lines13491350135113521353135413551356135713581359136013611362136313641365136613679 unmodified lines1377137813791380138113823 unmodified lines1386138713881389139013919 unmodified lines14011402140314041405140614075 unmodified lines141314141415141614171418141914201421142214231424142514261427142814291430143110 unmodified lines1442144314441445144614471448144914501451145212 unmodified lines14651466146714681469147014715 unmodified lines14771478147914801481148214835 unmodified lines14891490149114921493149414959 unmodified lines1505150615071508150915101511151215131514151512 unmodified lines15281529153015311532153315345 unmodified lines15401541154215431544154515465 unmodified lines15521553155415551556155715585 unmodified lines1564156515661567156815691570157115721573157414 unmodified lines15891590159115921593159415955 unmodified lines16011602160316041605160624 unmodified lines1631163216331634163516361637163812 unmodified lines16511652165316541655165616577 unmodified lines1665166616671668166916701671167212 unmodified lines16851686168716881689169016917 unmodified lines1699170017011702170317041705170625 unmodified lines1732173317341735173617371738173925 unmodified lines176517661767176817691770177117721773177410 unmodified lines17851786178717881789179017912 unmodified lines179417951796179717981799180018011802180312 unmodified lines18161817181818191820182118222 unmodified lines182518261827182818291830183118321833183419 unmodified lines185418551856185718581859186018611862186321 unmodified lines18851886188718881889189018911892189318949 unmodified lines190419051906190719081909191019112 unmodified lines191419151916191719181919192019211922192311 unmodified lines193519361937193819391940194119422 unmodified lines19451946194719481949195019511952195319549 unmodified lines19641965196619671968196919703 unmodified lines197419751976197719781979198019811982198311 unmodified lines19951996199719981999200020018 unmodified lines201020112012201320142015201620172018201920209 unmodified lines20302031203220332034203520365 unmodified lines204220432044204520462047204820492050205120522053205420552056205720582059206012 unmodified lines207320742075207620772078207920802081208220839 unmodified lines20932094209520962097209820995 unmodified lines210521062107210821092110211121122113211421152116211721182119212021212122212356 unmodified lines"canPinSubgroupSize32": "device.features.has(\"subgroups\") and device.features.has(\"subgroup-size-control\") and has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize <= 32 and device.adapterInfo.subgroupMaxSize >= 32","pinSubgroupSize32": "canPinSubgroupSize32 and not wave32Adapter","wave32Effective": "wave32Adapter or pinSubgroupSize32","wave32SubgroupsUsable": "subgroupsWave32 or pinSubgroupSize32","headDim": "dim(shapes.queryT, 2) / attrs.num_heads if (ranks.queryT == 3 and attrs.num_heads > 0) else 0","qkvDtypesOk": "tensorDtypes.keyT == tensorDtypes.queryT and tensorDtypes.valueT == tensorDtypes.queryT and tensorDtypes.outputT == tensorDtypes.queryT","floatDtypeOk": "(tensorDtypes.queryT == \"float32\" or tensorDtypes.queryT == \"float16\") and f16Ok(tensorDtypes.queryT)","qkvShapeOk": "ranks.queryT == 3 and ranks.keyT == 3 and ranks.valueT == 3 and ranks.outputT == 3 and attrs.num_heads > 0 and dim(shapes.queryT, 2) % attrs.num_heads == 0 and dim(shapes.keyT, 2) == dim(shapes.queryT, 2) and dim(shapes.valueT, 2) == dim(shapes.queryT, 2) and dim(shapes.keyT, 1) == dim(shapes.valueT, 1) and dim(shapes.queryT, 0) == dim(shapes.keyT, 0) and dim(shapes.queryT, 0) == dim(shapes.valueT, 0) and dim(shapes.outputT, 0) == dim(shapes.queryT, 0) and dim(shapes.outputT, 1) == dim(shapes.queryT, 1) and dim(shapes.outputT, 2) == dim(shapes.valueT, 2)",2 unmodified lines"qkvContractOk": "qkvShapeOk and qkvDtypesOk and floatDtypeOk and noAttnBias","qkvMaskContractOk": "qkvShapeOk and qkvDtypesOk and floatDtypeOk and attnBiasOk","biasOk": "present.biasT and ranks.biasT == 1 and tensorDtypes.biasT == tensorDtypes.queryT and dim(shapes.biasT, 0) == 3 * dim(shapes.queryT, 2)","hasBias": "present.biasT","hasMask": "present.attentionBiasT","q32BroadcastSubgroupLanes": "32 if wave32SubgroupsUsable else 0","q32BroadcastF32HeadVectors": "headDim / 4 if headDim % 4 == 0 else 0","q32BroadcastF32RegisterGeometry": "wave32SubgroupsUsable and q32BroadcastF32HeadVectors == q32BroadcastSubgroupLanes","subgroupCluster4": "not narrowSubgroupRange and device.features.has(\"subgroups\") and has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize >= 4 and device.adapterInfo.subgroupMinSize % 4 == 0 and device.adapterInfo.subgroupMaxSize % 4 == 0","subgroupCluster8": "not narrowSubgroupRange and device.features.has(\"subgroups\") and has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize >= 8 and device.adapterInfo.subgroupMinSize % 8 == 0 and device.adapterInfo.subgroupMaxSize % 8 == 0","attentionDispatchFits": "dim(shapes.queryT, 0) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and dim(shapes.queryT, 1) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and attrs.num_heads <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","flashHeadOk": "headDim % 4 == 0 and headDim >= 32 and headDim <= 256","flashSizeOk": "flashHeadOk and (dim(shapes.queryT, 1) * attrs.num_heads >= tunables.FLASH_MIN_QUERY_HEADS or (dim(shapes.queryT, 1) == 1 and dim(shapes.keyT, 1) >= 512) or (dim(shapes.queryT, 1) > 1 and dim(shapes.keyT, 1) >= 2048)) and attentionDispatchFits","flashShapeOk": "qkvContractOk and flashSizeOk","flashMaskShapeOk": "qkvMaskContractOk and flashSizeOk","noBiasSplitKCount": "min(tunables.DECODE_MAX_SPLITS if dim(shapes.queryT, 1) == 1 else max(1, ceilDiv(tunables.SPLITK_TARGET_WORKGROUPS, dim(shapes.queryT, 0) * dim(shapes.queryT, 1) * attrs.num_heads)), ceilDiv(dim(shapes.keyT, 1), tunables.DECODE_KEYS_PER_SPLIT))","biasSplitKCount": "min(tunables.DECODE_MAX_SPLITS, ceilDiv(dim(shapes.keyT, 1), tunables.DECODE_KEYS_PER_SPLIT))","noBiasPartialOutBytes": "dim(shapes.queryT, 0) * dim(shapes.queryT, 1) * attrs.num_heads * noBiasSplitKCount * headDim * 4","noBiasStatsBytes": "2 * dim(shapes.queryT, 0) * dim(shapes.queryT, 1) * attrs.num_heads * noBiasSplitKCount * 4","noBiasSplitScratchFits": "noBiasPartialOutBytes <= device.limits.maxStorageBufferBindingSize and noBiasPartialOutBytes <= device.limits.maxBufferSize and noBiasStatsBytes <= device.limits.maxStorageBufferBindingSize and noBiasStatsBytes <= device.limits.maxBufferSize","noBiasSplitDispatchFits": "dim(shapes.queryT, 1) * noBiasSplitKCount <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","biasPartialOutBytes": "dim(shapes.queryT, 0) * attrs.num_heads * biasSplitKCount * headDim * 4","biasStatsBytes": "2 * dim(shapes.queryT, 0) * attrs.num_heads * biasSplitKCount * 4","biasSplitScratchFits": "biasPartialOutBytes <= device.limits.maxStorageBufferBindingSize and biasPartialOutBytes <= device.limits.maxBufferSize and biasStatsBytes <= device.limits.maxStorageBufferBindingSize and biasStatsBytes <= device.limits.maxBufferSize","biasSplitDispatchFits": "biasSplitKCount <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","decodeSplitKNoBiasOk": "qkvContractOk and attrs.unidirectional == 0 and dim(shapes.queryT, 1) == 1 and dim(shapes.keyT, 1) >= 512 and flashHeadOk and attentionDispatchFits and noBiasSplitDispatchFits and noBiasSplitScratchFits","shortQuerySplitKNoBiasOk": "qkvContractOk and attrs.unidirectional == 0 and dim(shapes.queryT, 1) >= 2 and dim(shapes.queryT, 1) <= 16 and dim(shapes.keyT, 1) >= 2048 and flashHeadOk and attentionDispatchFits and noBiasSplitDispatchFits and noBiasSplitScratchFits","decodeSplitKBiasOk": "biasOk and qkvContractOk and attrs.unidirectional == 0 and dim(shapes.queryT, 1) == 1 and dim(shapes.keyT, 1) >= 512 and flashHeadOk and attentionDispatchFits and biasSplitDispatchFits and biasSplitScratchFits","decodeSplitKPortablePreferred": "tensorDtypes.queryT == \"float32\" and device.features.has(\"subgroups\") and has(device.adapterInfo, \"subgroupMinSize\") and headDim / 4 < device.adapterInfo.subgroupMinSize","materializedScoreBytes": "dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1) * dim(shapes.keyT, 1) * 4","materializedScoreFits": "materializedScoreBytes <= device.limits.maxStorageBufferBindingSize and materializedScoreBytes <= device.limits.maxBufferSize","materializedWorkgroupSize": "tunables.MATERIALIZED_WORKGROUP_DIM * tunables.MATERIALIZED_WORKGROUP_DIM","materializedScoreStorageBytes": "(tunables.MATERIALIZED_QUERY_TILE * tunables.MATERIALIZED_INNER_TILE + tunables.MATERIALIZED_KEY_TILE * (tunables.MATERIALIZED_INNER_TILE + 4)) * 4","materializedApplyTileN": "tunables.MATERIALIZED_VALUE_TILE_D128 if headDim == 128 else tunables.MATERIALIZED_VALUE_TILE","materializedApplyStorageBytes": "(tunables.MATERIALIZED_QUERY_TILE * tunables.MATERIALIZED_INNER_TILE + tunables.MATERIALIZED_INNER_TILE * materializedApplyTileN) * 4","materializedTileGeometryOk": "tunables.MATERIALIZED_INNER_TILE % 4 == 0 and (materializedApplyTileN / tunables.MATERIALIZED_WORKGROUP_DIM) % 4 == 0 and tunables.MATERIALIZED_INNER_TILE > 0 and tunables.MATERIALIZED_WORKGROUP_DIM > 0 and tunables.MATERIALIZED_QUERY_TILE % tunables.MATERIALIZED_WORKGROUP_DIM == 0 and tunables.MATERIALIZED_KEY_TILE % tunables.MATERIALIZED_WORKGROUP_DIM == 0 and materializedApplyTileN % tunables.MATERIALIZED_WORKGROUP_DIM == 0 and tunables.MATERIALIZED_QUERY_TILE * tunables.MATERIALIZED_INNER_TILE >= materializedWorkgroupSize and tunables.MATERIALIZED_KEY_TILE * tunables.MATERIALIZED_INNER_TILE >= materializedWorkgroupSize and tunables.MATERIALIZED_INNER_TILE * materializedApplyTileN >= materializedWorkgroupSize and (tunables.MATERIALIZED_QUERY_TILE * tunables.MATERIALIZED_INNER_TILE) % materializedWorkgroupSize == 0 and (tunables.MATERIALIZED_KEY_TILE * tunables.MATERIALIZED_INNER_TILE) % materializedWorkgroupSize == 0 and (tunables.MATERIALIZED_INNER_TILE * materializedApplyTileN) % materializedWorkgroupSize == 0","materializedDeviceOk": "materializedTileGeometryOk and materializedWorkgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup and tunables.MATERIALIZED_WORKGROUP_DIM <= device.limits.maxComputeWorkgroupSizeX and tunables.MATERIALIZED_WORKGROUP_DIM <= device.limits.maxComputeWorkgroupSizeY and materializedScoreStorageBytes <= device.limits.maxComputeWorkgroupStorageSize and materializedApplyStorageBytes <= device.limits.maxComputeWorkgroupStorageSize","materializedWideSimdOk": "device.features.has(\"subgroups\") or (has(device.adapterInfo, \"subgroupMinSize\") and device.adapterInfo.subgroupMinSize >= 16)","materializedF32CoreOk": "qkvContractOk and tensorDtypes.queryT == \"float32\" and attrs.unidirectional == 0 and headDim >= 64 and headDim <= 128 and dim(shapes.queryT, 1) >= 512 and dim(shapes.keyT, 1) >= 512 and dim(shapes.queryT, 0) * attrs.num_heads <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and ceilDiv(dim(shapes.keyT, 1), tunables.MATERIALIZED_KEY_TILE) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and materializedScoreFits and materializedDeviceOk","materializedSoftmaxStorageBytes": "tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE * 8 + 8","materializedSoftmaxResourcesFit": "tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE > 0 and pow2ceil(tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE) == tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE and tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE <= deviceWorkgroupCap and materializedSoftmaxStorageBytes <= device.limits.maxComputeWorkgroupStorageSize","materializedSgmatQueryTile": "tunables.MATERIALIZED_SGMAT_QUERY_TILE",17 unmodified lines"materializedSgmatResourcesFit": "materializedSgmatGeometryOk and materializedSgmatWorkgroupSize <= deviceWorkgroupCap and materializedSgmatCompactStorageBytes <= device.limits.maxComputeWorkgroupStorageSize","materializedSgmatDispatchFits": "ceilDiv(dim(shapes.keyT, 1), materializedSgmatKeyTile) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and dim(shapes.queryT, 0) * attrs.num_heads <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and ceilDiv(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), min(device.limits.maxComputeWorkgroupsPerDimension, 65535)) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","materializedSgmatDirectScoreStore": "dim(shapes.queryT, 1) % materializedSgmatQueryTile == 0 and dim(shapes.keyT, 1) % materializedSgmatKeyTile == 0","materializedSgmatDirectApplyStore": "dim(shapes.queryT, 1) % materializedSgmatQueryTile == 0 and headDim % materializedSgmatKeyTile == 0","materializedSgmatRuntimeDirectStore": "dim(shapes.queryT, 1) >= 2 * materializedSgmatQueryTile and dim(shapes.keyT, 1) >= 2 * materializedSgmatKeyTile","materializedSgmatCoreOk": "qkvContractOk and tensorDtypes.queryT == \"float32\" and attrs.unidirectional == 0 and headDim >= 32 and headDim <= 256 and dim(shapes.queryT, 1) >= 512 and dim(shapes.keyT, 1) >= 512 and device.features.has(\"subgroups\") and wave32Effective and device.features.has(\"chromium-experimental-subgroup-matrix\") and materializedSgmatBuffersFit and materializedSgmatResourcesFit and materializedSgmatDispatchFits","materializedCachedSoftmaxWg": "min(tunables.MATERIALIZED_CACHED_SOFTMAX_WORKGROUP_SIZE, device.limits.maxComputeInvocationsPerWorkgroup, device.limits.maxComputeWorkgroupSizeX)","materializedCachedSoftmaxVecsPerLane": "ceilDiv(ceilDiv(dim(shapes.keyT, 1), 4), max(1, materializedCachedSoftmaxWg))","materializedCachedSoftmaxStorageBytes": "materializedCachedSoftmaxWg * 8 + 8",3 unmodified lines"materializedSgmatOk": "materializedSgmatCoreOk and materializedAdaptiveSoftmaxOk","materializedSgmatFusedOk": "materializedSgmatCoreOk","materializedF32Ok": "materializedF32CoreOk and materializedAdaptiveSoftmaxOk","clusterTileKWg64": "max(1, min(tunables.FLASH_MAX_TILE_K, floor(device.limits.maxComputeWorkgroupStorageSize / (headDim * (8 if tensorDtypes.queryT != \"float16\" else 4) + tunables.FLASH_CLUSTER_WG_SMALL * 4))))","clusterTileKWg128": "max(1, min(tunables.FLASH_MAX_TILE_K, floor(device.limits.maxComputeWorkgroupStorageSize / (headDim * (8 if tensorDtypes.queryT != \"float16\" else 4) + tunables.FLASH_CLUSTER_WG_LARGE * 4))))","smallHeadShapeOk": "qkvContractOk and headDim < 32 and dim(shapes.keyT, 1) >= 64 and attrs.unidirectional == 0 and dim(shapes.queryT, 0) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and attrs.num_heads <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","smallHeadParallelOk": "smallHeadShapeOk and dim(shapes.keyT, 1) <= 2048","prefillTiledStorageBytes": "headDim * tunables.PREFILL_QUERY_TILE * 4","prefillTiledDeviceOk": "tunables.PREFILL_QUERY_TILE <= deviceWorkgroupCap and prefillTiledStorageBytes <= device.limits.maxComputeWorkgroupStorageSize","portableWorkgroupSize": "min(tunables.WORKGROUP_SIZE, pow2ceil(max(1, headDim)))","portableWorkgroupStorageBytes": "portableWorkgroupSize * 8 + max(1, headDim) * 4 + 16","portableWorkgroupOk": "tunables.WORKGROUP_SIZE > 0 and pow2ceil(tunables.WORKGROUP_SIZE) == tunables.WORKGROUP_SIZE and portableWorkgroupSize <= deviceWorkgroupCap and portableWorkgroupStorageBytes <= device.limits.maxComputeWorkgroupStorageSize","fallbackShapeOk": "qkvContractOk and portableWorkgroupOk","fallbackMaskShapeOk": "qkvMaskContractOk and portableWorkgroupOk","smallSeqShapeOk": "qkvContractOk and tensorDtypes.queryT == \"float32\" and dim(shapes.queryT, 1) >= 1 and dim(shapes.queryT, 1) <= tunables.SMALL_SEQ_MAX and dim(shapes.keyT, 1) >= 1 and dim(shapes.keyT, 1) <= tunables.SMALL_SEQ_MAX and headDim >= 1 and attrs.unidirectional == 0","smallSeqPrivateFloats": "dim(shapes.keyT, 1) + headDim","smallSeqWorkgroupSize": "max(32, pow2ceil(dim(shapes.queryT, 1)))","smallSeqSharedBytes": "dim(shapes.keyT, 1) * headDim * 8","smallSeqResourcesFit": "smallSeqPrivateFloats <= tunables.SMALL_SEQ_MAX_PRIVATE_FLOATS and smallSeqWorkgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup and smallSeqWorkgroupSize <= device.limits.maxComputeWorkgroupSizeX and smallSeqSharedBytes <= device.limits.maxComputeWorkgroupStorageSize","smallSeqBlockedKvBytes": "dim(shapes.keyT, 1) * headDim * 8","smallSeqBlockedLaneBytes": "8 + headDim * 4","smallSeqBlockedKeyLanes": "min(pow2ceil(dim(shapes.keyT, 1)), 16 if smallSeqBlockedKvBytes + tunables.SMALL_SEQ_BLOCKED_QUERY_BLOCK * 16 * smallSeqBlockedLaneBytes <= device.limits.maxComputeWorkgroupStorageSize else (8 if smallSeqBlockedKvBytes + tunables.SMALL_SEQ_BLOCKED_QUERY_BLOCK * 8 * smallSeqBlockedLaneBytes <= device.limits.maxComputeWorkgroupStorageSize else 4))","smallSeqBlockedWorkgroupSize": "tunables.SMALL_SEQ_BLOCKED_QUERY_BLOCK * smallSeqBlockedKeyLanes","smallSeqBlockedSharedBytes": "smallSeqBlockedKvBytes + smallSeqBlockedWorkgroupSize * smallSeqBlockedLaneBytes","smallSeqBlockedShapeOk": "qkvContractOk and tensorDtypes.queryT == \"float32\" and headDim % 4 == 0 and headDim >= 4 and headDim <= tunables.SMALL_SEQ_BLOCKED_MAX_HEAD_DIM and dim(shapes.keyT, 1) >= 1 and dim(shapes.keyT, 1) <= tunables.SMALL_SEQ_BLOCKED_MAX_KV and dim(shapes.queryT, 1) >= 1","smallSeqBlockedFits": "smallSeqBlockedWorkgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup and smallSeqBlockedWorkgroupSize <= device.limits.maxComputeWorkgroupSizeX and smallSeqBlockedSharedBytes <= device.limits.maxComputeWorkgroupStorageSize and ceilDiv(dim(shapes.queryT, 1), tunables.SMALL_SEQ_BLOCKED_QUERY_BLOCK) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and attrs.num_heads <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and dim(shapes.queryT, 0) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","smallSeqDispatchFits": "attrs.num_heads <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and dim(shapes.queryT, 0) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","materializedSgmatCoreF16Ok": "qkvContractOk and tensorDtypes.queryT == \"float16\" and tensorDtypes.keyT == \"float16\" and tensorDtypes.valueT == \"float16\" and device.features.has(\"shader-f16\") and attrs.unidirectional == 0 and headDim >= 32 and headDim <= 256 and dim(shapes.queryT, 1) >= 512 and dim(shapes.keyT, 1) >= 512 and device.features.has(\"subgroups\") and wave32Effective and device.features.has(\"chromium-experimental-subgroup-matrix\") and materializedSgmatBuffersFit and materializedSgmatResourcesFit and materializedSgmatDispatchFits","materializedSgmatFusedF16Ok": "materializedSgmatCoreF16Ok","attentionScaleExpression": "\"select(inverseSqrt(f32(HEAD_DIM)), params.scale, params.scale != 0.0)\"","smallHeadValueWg": "pow(2, log2ceil(min(64, device.limits.maxComputeInvocationsPerWorkgroup, device.limits.maxComputeWorkgroupSizeX) + 1) - 1)","smallHeadValueStorage": "max(dim(shapes.keyT, 1), smallHeadValueWg * headDim)","smallHeadValueQueryBlock": "2 if dim(shapes.queryT, 1) >= 2 and (smallHeadValueStorage + smallHeadValueWg) * 8 <= device.limits.maxComputeWorkgroupStorageSize else 1","smallHeadValueEstimatedSteps": "headDim * (ceilDiv(dim(shapes.keyT, 1), smallHeadValueWg) + 2 * log2ceil(smallHeadValueWg) * (2 if tensorDtypes.queryT == \"float16\" else 1) + 2)","smallHeadValueSgReductionSteps": "ceilDiv(smallHeadValueWg, max(1, device.adapterInfo.subgroupMinSize)) if has(device.adapterInfo, \"subgroupMinSize\") else log2ceil(smallHeadValueWg)","smallHeadValueSgEstimatedSteps": "headDim * (ceilDiv(dim(shapes.keyT, 1), smallHeadValueWg) + 2 * smallHeadValueSgReductionSteps * (2 if tensorDtypes.queryT == \"float16\" else 1) + 2)"},"bindings": {"query": { "arg": "queryT", "elementType": "$inputElement" },"key": { "arg": "keyT", "elementType": "$inputElement" },"value": { "arg": "valueT", "elementType": "$inputElement" },"bias": { "arg": "biasT", "elementType": "$inputScalar" },"output": { "arg": "outputT", "elementType": "$outputElement" },"params": {"struct": [{ "name": "qSeq", "type": "u32", "value": "dim(shapes.queryT, 1)" },{ "name": "kvSeq", "type": "u32", "value": "dim(shapes.keyT, 1)" },1 unmodified line{ "name": "isCausal", "type": "u32", "value": "attrs.unidirectional" }]},"params_main": {"name": "params","struct": [{ "name": "qSeq", "type": "u32", "value": "dim(shapes.queryT, 1)" },{ "name": "scale", "type": "f32", "value": "attrs.scale if has(attrs, \"scale\") else 0" }]},"q": { "arg": "queryT", "elementType": "$scalar" },"k": { "arg": "keyT", "elementType": "$scalar" },"v": { "arg": "valueT", "elementType": "$scalar" },"y": { "arg": "outputT", "elementType": "$scalar" },"params_scores": {"name": "params","struct": [{ "name": "qSeq", "type": "u32", "value": "dim(shapes.queryT, 1)" },{ "name": "kvSeq", "type": "u32", "value": "dim(shapes.keyT, 1)" },{ "name": "scale", "type": "f32", "value": "attrs.scale if has(attrs, \"scale\") else 0" }]},"query_f32": { "arg": "queryT", "name": "query", "elementType": "f32" },"key_f32": { "arg": "keyT", "name": "key", "elementType": "f32" },"scores": { "scratch": "materializedScores", "elementType": "f32" },"scorePartials": { "scratch": "materializedScorePartials", "elementType": "f32" },"bias_f32": { "arg": "biasT", "name": "bias", "elementType": "f32" },"scorePartials_f32": {"scratch": "materializedScorePartials","name": "scorePartials","buffer": "read-only-storage","elementType": "f32"},"rowStats": { "scratch": "materializedRowStats", "elementType": "f32" },"params_rows": {"name": "params","struct": [{ "name": "rows", "type": "u32", "value": "dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1)" }]},"scores_f32": {"scratch": "materializedScores","name": "scores","buffer": "read-only-storage","elementType": "f32"},"value_f32": { "arg": "valueT", "name": "value", "elementType": "f32" },"output_f32": { "arg": "outputT", "name": "output", "elementType": "f32" },"rowStats_f32": {"scratch": "materializedRowStats","name": "rowStats","buffer": "read-only-storage","elementType": "f32"},"params_apply": {"name": "params","struct": [{ "name": "qSeq", "type": "u32", "value": "dim(shapes.queryT, 1)" },{ "name": "kvSeq", "type": "u32", "value": "dim(shapes.keyT, 1)" }]},"attn_mask_main": { "arg": "attentionBiasT", "name": "attn_mask", "elementType": "$maskElement" },"params__uniform": {"name": "params","struct": [{ "name": "qSeq", "type": "u32", "value": "dim(shapes.queryT, 1)" },{ "name": "kvSeq", "type": "u32", "value": "dim(shapes.keyT, 1)" },12 unmodified lines{ "name": "maskSeqStride", "type": "u32", "value": "dim(shapes.attentionBiasT, 3)" }]},"query_query_t": { "arg": "queryT", "name": "query", "elementType": "$inputVec4" },"key_key_t": { "arg": "keyT", "name": "key", "elementType": "$inputVec4" },"value_value_t": { "arg": "valueT", "name": "value", "elementType": "$inputVec4" },"partial_out": { "scratch": "partialOut", "elementType": "vec4<f32>" },"partial_stats": { "scratch": "partialStats", "elementType": "vec2<f32>" },"params_kv_seq_scale": {"name": "params","struct": [{ "name": "kvSeq", "type": "u32", "value": "dim(shapes.keyT, 1)" },{ "name": "scale", "type": "f32", "value": "attrs.scale if has(attrs, \"scale\") else 0" }]},"partial_out_merge": {"scratch": "partialOut","name": "partial_out","buffer": "read-only-storage","elementType": "vec4<f32>"},"partial_stats_merge": {"scratch": "partialStats","name": "partial_stats","buffer": "read-only-storage","elementType": "vec2<f32>"},"output_merge": { "arg": "outputT", "name": "output", "elementType": "$inputVec4" },"scores_softmax": { "scratch": "materializedScores", "name": "scores", "elementType": "$softmaxElementType" }},"variants": [{"id": "qkv_no_bias_small_head_value_subgroups","priority": 12,"when": ["not present.biasT", "smallHeadShapeOk", "headDim > 0", "headDim <= smallHeadValueWg", "(smallHeadValueStorage + smallHeadValueWg) * 4 * smallHeadValueQueryBlock <= device.limits.maxComputeWorkgroupStorageSize", "device.wgslLanguageFeatures.has(\"subgroup_id\")"],"derive": {"outputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","qHidden": "dim(shapes.queryT, 2)","headDim": "dim(shapes.queryT, 2) / attrs.num_heads","kvSeq": "dim(shapes.keyT, 1)","valueWorkgroupSize": "smallHeadValueWg","scoreStorageElements": "smallHeadValueStorage","queryBlock": "smallHeadValueQueryBlock","queryTail": "dim(shapes.queryT, 1) % smallHeadValueQueryBlock != 0","useValueSubgroups": true},"passes": [{"id": "main","name": "MultiHeadAttention.SmallHeadValueSubgroups","shader": "attn-small-head-value.wgsl.jinja","bindings": ["query", "key", "value", "output", "params_main"],"dispatch": {"x": "min(ceilDiv(dim(shapes.queryT, 1), smallHeadValueQueryBlock), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"}}],"requires": { "features": ["subgroups"] },"demoteWhen": ["dim(shapes.keyT, 1) <= smallHeadValueSgEstimatedSteps"]},{"id": "qkv_no_bias_small_head_value_tree","priority": 11,"when": ["not present.biasT", "smallHeadShapeOk", "headDim > 0", "headDim <= smallHeadValueWg", "(smallHeadValueStorage + smallHeadValueWg) * 4 * smallHeadValueQueryBlock <= device.limits.maxComputeWorkgroupStorageSize", "true"],"derive": {"outputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","qHidden": "dim(shapes.queryT, 2)","headDim": "dim(shapes.queryT, 2) / attrs.num_heads","kvSeq": "dim(shapes.keyT, 1)","valueWorkgroupSize": "smallHeadValueWg","scoreStorageElements": "smallHeadValueStorage","queryBlock": "smallHeadValueQueryBlock","queryTail": "dim(shapes.queryT, 1) % smallHeadValueQueryBlock != 0","useValueSubgroups": false},"passes": [{"id": "main","name": "MultiHeadAttention.SmallHeadValueTree","shader": "attn-small-head-value.wgsl.jinja","bindings": ["query", "key", "value", "output", "params_main"],"dispatch": {"x": "min(ceilDiv(dim(shapes.queryT, 1), smallHeadValueQueryBlock), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"}}],"requires": { "features": [] },"demoteWhen": ["dim(shapes.keyT, 1) <= smallHeadValueEstimatedSteps"]},{"id": "qkv_bias_small_seq_blocked","priority": 40,"when": ["biasOk", "smallSeqBlockedShapeOk", "not flashShapeOk", "smallSeqBlockedFits"],"derive": {"inputElement": "\"vec4<f32>\"","outputElement": "\"vec4<f32>\"","inputScalar": "\"f32\"","headDimV4": "headDim / 4","hidden": "dim(shapes.queryT, 2)","hiddenV4": "dim(shapes.queryT, 2) / 4","kvSeq": "dim(shapes.keyT, 1)",10 unmodified lines"x": "ceilDiv(dim(shapes.queryT, 1), tunables.SMALL_SEQ_BLOCKED_QUERY_BLOCK)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"}}]3 unmodified lines"priority": 40,"when": ["not present.biasT", "smallSeqBlockedShapeOk", "not flashShapeOk", "smallSeqBlockedFits"],"derive": {"inputElement": "\"vec4<f32>\"","outputElement": "\"vec4<f32>\"","headDimV4": "headDim / 4","hidden": "dim(shapes.queryT, 2)","hiddenV4": "dim(shapes.queryT, 2) / 4","kvSeq": "dim(shapes.keyT, 1)",10 unmodified lines"x": "ceilDiv(dim(shapes.queryT, 1), tunables.SMALL_SEQ_BLOCKED_QUERY_BLOCK)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"}}]3 unmodified lines"priority": 60,"when": ["not present.biasT", "smallSeqShapeOk", "not flashShapeOk", "smallSeqResourcesFit", "smallSeqDispatchFits"],"derive": {"inputElement": "\"f32\"","outputElement": "\"f32\"","headDim": "dim(shapes.queryT, 2) / attrs.num_heads",6 unmodified lines"id": "main","name": "MultiHeadAttention","shader": "mha-small-seq.wgsl.jinja","bindings": ["query", "key", "value", "output", "params_main"],"dispatch": { "x": "attrs.num_heads", "y": "dim(shapes.queryT, 0)" }}]},{"id": "qkv_no_bias_tiled_nosg","priority": 19,"when": ["not present.biasT", "qkvContractOk", "not smallHeadParallelOk", "headDim % 4 == 0", "headDim <= 128", "dim(shapes.queryT, 1) >= 31", "prefillTiledDeviceOk"],"supersededBy": ["qkv_no_bias_flash_cluster_nosg", "qkv_no_bias_flash_cluster_lpq4_nosg"],"derive": {"scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","blockM": "tunables.PREFILL_QUERY_TILE","vHeadCap": "dim(shapes.queryT, 2) / attrs.num_heads"},28 unmodified lines}],"dispatch": {"x": "min(ceilDiv((dim(shapes.outputT, 0) * attrs.num_heads * ceil(dim(shapes.outputT, 1) / blockM) * blockM), (blockM)), 65535)","y": "ceilDiv(ceilDiv((dim(shapes.outputT, 0) * attrs.num_heads * ceil(dim(shapes.outputT, 1) / blockM) * blockM), (blockM)), 65535)","z": 1}}2 unmodified lines{"id": "qkv_bias_flash_q32_broadcast_f32_d128","priority": 30,"when": ["tensorDtypes.queryT == \"float32\"", "biasOk", "attrs.unidirectional == 0", "flashShapeOk", "q32BroadcastF32RegisterGeometry", "dim(shapes.queryT, 1) >= 31", "ceilDiv(dim(shapes.queryT, 1), 32) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)", "wave32SubgroupsUsable"],"requires": { "features": ["subgroups"] },"derive": {"hasCausal": false,"usesF16": false,"scalar": "\"f32\"","inputElement": "\"vec4<f32>\"","outputElement": "\"vec4<f32>\"","inputScalar": "\"f32\"",11 unmodified lines"name": "MultiHeadAttention.FlashQ32BroadcastF32Bias","shader": "attn-flash-q32-broadcast.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "bias", "output", "params_scores"],"dispatch": {"x": "ceilDiv(dim(shapes.queryT, 1), 32)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"}}]},2 unmodified lines"priority": 10,"when": ["not present.biasT", "smallHeadParallelOk"],"derive": {"outputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",4 unmodified lines"passes": [{"id": "main","name": "MultiHeadAttention.SmallHeadParallel","shader": "attn-small-head-parallel.wgsl.jinja","bindings": ["query", "key", "value", "output", "params_main"],"dispatch": {"x": "min(dim(shapes.queryT, 1), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": "attrs.num_heads",5 unmodified lines{"id": "qkv_no_bias_tiled_attn_bias_nosg","priority": 17,"when": ["not present.biasT", "qkvMaskContractOk", "not smallHeadParallelOk", "headDim % 4 == 0", "headDim <= 128", "dim(shapes.queryT, 1) >= 31", "prefillTiledDeviceOk"],"derive": {"scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","blockM": "tunables.PREFILL_QUERY_TILE","vHeadCap": "dim(shapes.queryT, 2) / attrs.num_heads"},40 unmodified lines}],"dispatch": {"x": "min(ceilDiv((dim(shapes.outputT, 0) * attrs.num_heads * ceil(dim(shapes.outputT, 1) / blockM) * blockM), (blockM)), 65535)","y": "ceilDiv(ceilDiv((dim(shapes.outputT, 0) * attrs.num_heads * ceil(dim(shapes.outputT, 1) / blockM) * blockM), (blockM)), 65535)","z": 1}}9 unmodified lines},"derive": {"qNumHeads": "attrs.num_heads","qHidden": "dim(shapes.queryT, 2)","statSlots": "materializedSgmatStatSlots","statQuerySeq": "dim(shapes.queryT, 1)"},12 unmodified lines"name": "MultiHeadAttention.MaterializedScoresSgmat","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"score\"", "emitRowStats": true },"bindings": ["query_f32", "key_f32", "scores", "scorePartials", "params_scores"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)","z": "dim(shapes.queryT, 0) * attrs.num_heads"}},{"id": "rowstats","name": "MultiHeadAttention.MaterializedRowStatsCombine","shader": "attn-materialized-rowstats-combine-f32.wgsl.jinja","bindings": ["scorePartials_f32", "rowStats", "params_rows"],"dispatch": {"x": "min(ceilDiv((dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1)), (materializedRowStatsWg)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": 1,5 unmodified lines"name": "MultiHeadAttention.MaterializedApplySgmat","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"apply\"", "fusedSoftmax": true },"bindings": ["scores_f32", "value_f32", "output_f32", "rowStats_f32", "params_apply"],"dispatch": {"x": "ceilDiv(headDim, materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",12 unmodified lines},"derive": {"qNumHeads": "attrs.num_heads","qHidden": "dim(shapes.queryT, 2)","statSlots": "materializedSgmatStatSlots","statQuerySeq": "dim(shapes.queryT, 1)"},12 unmodified lines"name": "MultiHeadAttention.MaterializedScoresSgmatBias","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"score\"", "emitRowStats": true },"bindings": ["query_f32", "key_f32", "bias_f32", "scores", "scorePartials", "params_scores"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)","z": "dim(shapes.queryT, 0) * attrs.num_heads"}},{"id": "rowstats","name": "MultiHeadAttention.MaterializedRowStatsCombineBias","shader": "attn-materialized-rowstats-combine-f32.wgsl.jinja","bindings": ["scorePartials_f32", "rowStats", "params_rows"],"dispatch": {"x": "min(ceilDiv((dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1)), (materializedRowStatsWg)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": 1,5 unmodified lines"name": "MultiHeadAttention.MaterializedApplySgmatBias","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"apply\"", "fusedSoftmax": true },"bindings": ["scores_f32", "value_f32", "bias_f32", "output_f32", "rowStats_f32", "params_apply"],"dispatch": {"x": "ceilDiv(headDim, materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",12 unmodified lines},"derive": {"qNumHeads": "attrs.num_heads","qHidden": "dim(shapes.queryT, 2)","statSlots": "materializedSgmatStatSlots","statQuerySeq": "dim(shapes.queryT, 1)","operandF16": true,15 unmodified lines"name": "MultiHeadAttention.MaterializedScoresSgmatF16","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"score\"", "emitRowStats": true },"bindings": ["query", "key", "scores", "scorePartials", "params_scores"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)","z": "dim(shapes.queryT, 0) * attrs.num_heads"}},{"id": "rowstats","name": "MultiHeadAttention.MaterializedRowStatsCombine","shader": "attn-materialized-rowstats-combine-f32.wgsl.jinja","bindings": ["scorePartials_f32", "rowStats", "params_rows"],"dispatch": {"x": "min(ceilDiv((dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1)), (materializedRowStatsWg)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": 1,5 unmodified lines"name": "MultiHeadAttention.MaterializedApplySgmatF16","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"apply\"", "fusedSoftmax": true },"bindings": ["scores_f32", "value", "output", "rowStats_f32", "params_apply"],"dispatch": {"x": "ceilDiv(headDim, materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",5 unmodified lines{"id": "qkv_no_bias_flash_cluster_lpq4_nosg","priority": 20,"when": ["not present.biasT", "flashShapeOk", "headDim % 16 == 0", "headDim % 32 != 0", "dim(shapes.queryT, 1) >= 31"],"requires": {},"derive": {"hasCausal": true,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",26 unmodified lines{"id": "qkv_no_bias_flash_cluster_nosg","priority": 20,"when": ["not present.biasT", "flashShapeOk", "headDim % 32 == 0", "dim(shapes.queryT, 1) >= 31"],"requires": {},"derive": {"hasCausal": true,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",26 unmodified lines{"id": "qkv_bias_flash_cluster_nosg","priority": 19,"when": ["biasOk", "flashShapeOk", "headDim % 32 == 0", "dim(shapes.queryT, 1) >= 31"],"requires": {},"derive": {"hasCausal": true,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",28 unmodified lines{"id": "qkv_no_bias_flash_cluster_lpq4","priority": 22,"when": ["not present.biasT", "flashShapeOk", "headDim % 16 == 0", "headDim % 32 != 0", "dim(shapes.queryT, 1) >= 31", "subgroupCluster4"],"requires": { "features": ["subgroups"] },"derive": {"hasCausal": true,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",17 unmodified lines"x": "ceilDiv(dim(shapes.queryT, 1), TILE_Q)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"}}]},{"id": "qkv_no_bias_flash_cluster","priority": 22,"when": ["not present.biasT", "flashShapeOk", "headDim % 32 == 0", "dim(shapes.queryT, 1) >= 31", "subgroupCluster8"],"requires": { "features": ["subgroups"] },"derive": {"hasCausal": true,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",17 unmodified lines"x": "ceilDiv(dim(shapes.queryT, 1), TILE_Q)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"}}]},{"id": "qkv_bias_flash_cluster","priority": 21,"when": ["biasOk", "flashShapeOk", "headDim % 32 == 0", "dim(shapes.queryT, 1) >= 31", "subgroupCluster8"],"requires": { "features": ["subgroups"] },"derive": {"hasCausal": true,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",19 unmodified lines"x": "ceilDiv(dim(shapes.queryT, 1), TILE_Q)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"}}]},{"id": "qkv_no_bias_flash_cluster_attn_bias","priority": 22,"when": ["not present.biasT", "flashMaskShapeOk", "headDim % 32 == 0", "dim(shapes.queryT, 1) >= 31", "subgroupCluster8"],"requires": { "features": ["subgroups"] },"derive": {"hasCausal": true,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",13 unmodified lines"name": "MultiHeadAttention.Flash","shader": "attn-flash-prefill-cluster.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_main", "output", "params__uniform"],"dispatch": {"x": "ceilDiv(dim(shapes.queryT, 1), TILE_Q)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"}}]},{"id": "qkv_bias_flash_cluster_attn_bias","priority": 21,"when": ["biasOk", "flashMaskShapeOk", "headDim % 32 == 0", "dim(shapes.queryT, 1) >= 31", "subgroupCluster8"],"requires": { "features": ["subgroups"] },"derive": {"hasCausal": true,"usesF16": "tensorDtypes.queryT == \"float16\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",15 unmodified lines"name": "MultiHeadAttention.Flash","shader": "attn-flash-prefill-cluster.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_main", "bias", "output", "params__uniform"],"dispatch": {"x": "ceilDiv(dim(shapes.queryT, 1), TILE_Q)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"}}]},3 unmodified lines"when": ["not present.biasT", "decodeSplitKNoBiasOk or shortQuerySplitKNoBiasOk"],"requires": {},"derive": {"useSubgroups": false,"splitQueries": true,"scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"",25 unmodified lines"name": "MultiHeadAttention.DecodeSplitKNoSg","shader": "attn-flash-decode-splitk.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query_query_t", "key_key_t", "value_value_t", "partial_out", "partial_stats", "params_kv_seq_scale"],"dispatch": {"x": "dim(shapes.queryT, 1) * noBiasSplitKCount","y": "attrs.num_heads",5 unmodified lines"name": "MultiHeadAttention.DecodeSplitKMergeNoSg","shader": "attn-flash-decode-splitk-merge.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["partial_out_merge", "partial_stats_merge", "output_merge"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]4 unmodified lines"when": ["biasOk", "decodeSplitKBiasOk"],"requires": {},"derive": {"useSubgroups": false,"scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",25 unmodified lines"name": "MultiHeadAttention.DecodeSplitKBiasNoSg","shader": "attn-flash-decode-splitk.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query_query_t", "key_key_t", "value_value_t", "bias", "partial_out", "partial_stats", "params_kv_seq_scale"],"dispatch": { "x": "biasSplitKCount", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }},{1 unmodified line"name": "MultiHeadAttention.DecodeSplitKMergeBiasNoSg","shader": "attn-flash-decode-splitk-merge.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["partial_out_merge", "partial_stats_merge", "bias", "output_merge"],"dispatch": { "x": 1, "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]5 unmodified lines"demoteWhen": ["decodeSplitKPortablePreferred"],"requires": { "features": ["subgroups"] },"derive": {"splitQueries": true,"scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"",25 unmodified lines"name": "MultiHeadAttention.DecodeSplitK","shader": "attn-flash-decode-splitk.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query_query_t", "key_key_t", "value_value_t", "partial_out", "partial_stats", "params_kv_seq_scale"],"dispatch": {"x": "dim(shapes.queryT, 1) * noBiasSplitKCount","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"}},{"id": "merge","name": "MultiHeadAttention.DecodeSplitKMerge","shader": "attn-flash-decode-splitk-merge.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["partial_out_merge", "partial_stats_merge", "output_merge"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]4 unmodified lines"when": ["biasOk", "decodeSplitKBiasOk"],"requires": { "features": ["subgroups"] },"derive": {"scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputVec4": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",25 unmodified lines"name": "MultiHeadAttention.DecodeSplitKBias","shader": "attn-flash-decode-splitk.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query_query_t", "key_key_t", "value_value_t", "bias", "partial_out", "partial_stats", "params_kv_seq_scale"],"dispatch": { "x": "biasSplitKCount", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }},{"id": "merge","name": "MultiHeadAttention.DecodeSplitKMergeBias","shader": "attn-flash-decode-splitk-merge.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["partial_out_merge", "partial_stats_merge", "bias", "output_merge"],"dispatch": { "x": 1, "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]6 unmodified lines"derive": {"qNumHeads": "attrs.num_heads","kvNumHeads": "attrs.num_heads","materializedInnerTile": "tunables.MATERIALIZED_INNER_TILE","materializedQueryTile": "tunables.MATERIALIZED_QUERY_TILE","materializedKeyTile": "tunables.MATERIALIZED_KEY_TILE",3 unmodified lines"materializedSoftmaxCols4": "dim(shapes.keyT, 1) / 4","softmaxElementType": "\"vec4<f32>\" if materializedCachedSoftmaxOk else \"f32\"","applyTileN": "materializedApplyTileN","useSubgroups": "device.features.has(\"subgroups\")"},"intermediates": [9 unmodified lines"name": "MultiHeadAttention.MaterializedScores","shader": "attn-materialized-score-f32.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query_f32", "key_f32", "scores", "params_scores"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), tunables.MATERIALIZED_KEY_TILE)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",5 unmodified lines"name": "MultiHeadAttention.MaterializedSoftmax","shader": "attn-materialized-softmax-f32.wgsl.jinja","derive": { "cacheVec4": "materializedCachedSoftmaxOk" },"bindings": ["scores_softmax", "params_rows"],"dispatch": {"x": "min(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","y": "ceilDiv(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","z": 1}},{"id": "apply","name": "MultiHeadAttention.MaterializedApply","shader": "attn-materialized-apply-f32.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["scores_f32", "value_f32", "output_f32", "params_apply"],"dispatch": {"x": "ceilDiv(headDim, applyTileN)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",9 unmodified lines"derive": {"qNumHeads": "attrs.num_heads","kvNumHeads": "attrs.num_heads","materializedInnerTile": "tunables.MATERIALIZED_INNER_TILE","materializedQueryTile": "tunables.MATERIALIZED_QUERY_TILE","materializedKeyTile": "tunables.MATERIALIZED_KEY_TILE",3 unmodified lines"materializedSoftmaxCols4": "dim(shapes.keyT, 1) / 4","softmaxElementType": "\"vec4<f32>\" if materializedCachedSoftmaxOk else \"f32\"","applyTileN": "materializedApplyTileN","useSubgroups": "device.features.has(\"subgroups\")"},"intermediates": [9 unmodified lines"name": "MultiHeadAttention.MaterializedScoresBias","shader": "attn-materialized-score-f32.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query_f32", "key_f32", "bias_f32", "scores", "params_scores"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), tunables.MATERIALIZED_KEY_TILE)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",5 unmodified lines"name": "MultiHeadAttention.MaterializedSoftmaxBias","shader": "attn-materialized-softmax-f32.wgsl.jinja","derive": { "cacheVec4": "materializedCachedSoftmaxOk" },"bindings": ["scores_softmax", "params_rows"],"dispatch": {"x": "min(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","y": "ceilDiv(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","z": 1}},{"id": "apply","name": "MultiHeadAttention.MaterializedApplyBias","shader": "attn-materialized-apply-f32.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["scores_f32", "value_f32", "bias_f32", "output_f32", "params_apply"],"dispatch": {"x": "ceilDiv(headDim, applyTileN)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",10 unmodified lines"derive": {"qNumHeads": "attrs.num_heads","kvNumHeads": "attrs.num_heads","materializedInnerTile": "tunables.MATERIALIZED_INNER_TILE","materializedQueryTile": "tunables.MATERIALIZED_QUERY_TILE","materializedKeyTile": "tunables.MATERIALIZED_KEY_TILE","materializedWorkgroupDim": "tunables.MATERIALIZED_WORKGROUP_DIM","applyTileN": "materializedApplyTileN","statSlots": "materializedGemmStatSlots","statQuerySeq": "dim(shapes.queryT, 1)"},12 unmodified lines"name": "MultiHeadAttention.MaterializedScoresFused","shader": "attn-materialized-score-f32.wgsl.jinja","derive": { "layout": "\"bsh\"", "emitRowStats": true },"bindings": ["query_f32", "key_f32", "scores", "scorePartials", "params_scores"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), tunables.MATERIALIZED_KEY_TILE)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",5 unmodified lines"name": "MultiHeadAttention.MaterializedGemmRowStatsCombine","shader": "attn-materialized-rowstats-combine-f32.wgsl.jinja","derive": { "maxOnly": true },"bindings": ["scorePartials_f32", "rowStats", "params_rows"],"dispatch": {"x": "min(ceilDiv((dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1)), (materializedRowStatsWg)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": 1,5 unmodified lines"name": "MultiHeadAttention.MaterializedApplyFused","shader": "attn-materialized-apply-f32.wgsl.jinja","derive": { "layout": "\"bsh\"", "fusedSoftmax": true },"bindings": ["scores_f32", "value_f32", "output_f32", "rowStats_f32", "params_apply"],"dispatch": {"x": "ceilDiv(headDim, applyTileN)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",9 unmodified lines"derive": {"qNumHeads": "attrs.num_heads","kvNumHeads": "attrs.num_heads","materializedInnerTile": "tunables.MATERIALIZED_INNER_TILE","materializedQueryTile": "tunables.MATERIALIZED_QUERY_TILE","materializedKeyTile": "tunables.MATERIALIZED_KEY_TILE","materializedWorkgroupDim": "tunables.MATERIALIZED_WORKGROUP_DIM","applyTileN": "materializedApplyTileN","statSlots": "materializedGemmStatSlots","statQuerySeq": "dim(shapes.queryT, 1)"},12 unmodified lines"name": "MultiHeadAttention.MaterializedScoresBiasFused","shader": "attn-materialized-score-f32.wgsl.jinja","derive": { "layout": "\"bsh\"", "emitRowStats": true },"bindings": ["query_f32", "key_f32", "bias_f32", "scores", "scorePartials", "params_scores"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), tunables.MATERIALIZED_KEY_TILE)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",5 unmodified lines"name": "MultiHeadAttention.MaterializedGemmRowStatsCombineBias","shader": "attn-materialized-rowstats-combine-f32.wgsl.jinja","derive": { "maxOnly": true },"bindings": ["scorePartials_f32", "rowStats", "params_rows"],"dispatch": {"x": "min(ceilDiv((dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1)), (materializedRowStatsWg)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": 1,5 unmodified lines"name": "MultiHeadAttention.MaterializedApplyBiasFused","shader": "attn-materialized-apply-f32.wgsl.jinja","derive": { "layout": "\"bsh\"", "fusedSoftmax": true },"bindings": ["scores_f32", "value_f32", "bias_f32", "output_f32", "rowStats_f32", "params_apply"],"dispatch": {"x": "ceilDiv(headDim, applyTileN)","y": "ceilDiv(dim(shapes.queryT, 1), tunables.MATERIALIZED_QUERY_TILE)",5 unmodified lines{"id": "qkv_no_bias_flash_q32_broadcast","priority": 30,"when": ["tensorDtypes.queryT == \"float16\"", "not present.biasT", "flashShapeOk", "(dim(shapes.queryT, 2) / attrs.num_heads) % 4 == 0", "(dim(shapes.queryT, 2) / attrs.num_heads) >= 64", "(dim(shapes.queryT, 2) / attrs.num_heads) <= 256", "dim(shapes.queryT, 1) >= 31", "wave32SubgroupsUsable"],"requires": { "features": ["subgroups", "shader-f16"] },"derive": {"usesF16": true,"scalar": "\"f16\"","inputElement": "\"vec4<f16>\"","outputElement": "\"vec4<f16>\"","qNumHeads": "attrs.num_heads",14 unmodified lines"x": "ceilDiv(dim(shapes.queryT, 1), 32)","y": "attrs.num_heads","z": "dim(shapes.queryT, 0)"}}]},5 unmodified lines"derive": {"usesF16": true,"scalar": "\"f16\"","inputElement": "\"vec4<f16>\"","outputElement": "\"vec4<f16>\"","qNumHeads": "attrs.num_heads",24 unmodified lines"priority": 0,"when": ["not present.biasT and fallbackMaskShapeOk"],"derive": {"hasCausal": true,"hasKeyLimit": false,"inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",12 unmodified lines"name": "MultiHeadAttention","shader": "attn-online-scalar.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_main", "output", "params__uniform"],"dispatch": {"x": "min(dim(shapes.queryT, 1), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": "attrs.num_heads",7 unmodified lines"priority": 0,"when": ["biasOk and fallbackMaskShapeOk"],"derive": {"hasCausal": true,"hasKeyLimit": false,"inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",12 unmodified lines"name": "MultiHeadAttention","shader": "attn-online-scalar.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_main", "bias", "output", "params__uniform"],"dispatch": {"x": "min(dim(shapes.queryT, 1), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))","y": "attrs.num_heads",7 unmodified lines"priority": 0,"when": ["not present.biasT and fallbackShapeOk and not flashShapeOk"],"derive": {"hasCausal": true,"hasKeyLimit": false,"inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",25 unmodified lines"priority": 0,"when": ["biasOk and fallbackShapeOk and not flashShapeOk"],"derive": {"hasCausal": true,"hasKeyLimit": false,"inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","outputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",25 unmodified lines"priority": 20,"when": ["device.features.has(\"subgroups\")", "not present.biasT", "flashShapeOk"],"derive": {"hasCausal": true,"combineSubgroups": true,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",10 unmodified lines"shader": "attn-flash-online.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "output", "params"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]},2 unmodified lines"priority": 20,"when": ["device.features.has(\"subgroups\")", "biasOk", "flashShapeOk"],"derive": {"hasCausal": true,"combineSubgroups": true,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",12 unmodified lines"shader": "attn-flash-online.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "bias", "output", "params"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]},2 unmodified lines"priority": 18,"when": ["true", "not present.biasT", "flashShapeOk"],"derive": {"hasCausal": true,"combineSubgroups": false,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",19 unmodified lines"priority": 17,"when": ["true", "biasOk", "flashShapeOk"],"derive": {"hasCausal": true,"combineSubgroups": false,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",21 unmodified lines"priority": 20,"when": ["device.features.has(\"subgroups\")", "not present.biasT", "flashMaskShapeOk"],"derive": {"hasCausal": true,"combineSubgroups": true,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",9 unmodified lines"name": "MultiHeadAttention.Flash","shader": "attn-flash-online.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_main", "output", "params__uniform"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]},2 unmodified lines"priority": 20,"when": ["device.features.has(\"subgroups\")", "biasOk", "flashMaskShapeOk"],"derive": {"hasCausal": true,"combineSubgroups": true,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",11 unmodified lines"name": "MultiHeadAttention.Flash","shader": "attn-flash-online.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_main", "bias", "output", "params__uniform"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]},2 unmodified lines"priority": 18,"when": ["true", "not present.biasT", "flashMaskShapeOk"],"derive": {"hasCausal": true,"combineSubgroups": false,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","qNumHeads": "attrs.num_heads",9 unmodified lines"name": "MultiHeadAttention.NoBiasOnlineFlashNoSg","shader": "attn-flash-online.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_main", "output", "params__uniform"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]3 unmodified lines"priority": 17,"when": ["true", "biasOk", "flashMaskShapeOk"],"derive": {"hasCausal": true,"combineSubgroups": false,"maskElement": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","scalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"","inputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","outputElement": "\"vec4<f16>\" if tensorDtypes.queryT == \"float16\" else \"vec4<f32>\"","inputScalar": "\"f16\" if tensorDtypes.queryT == \"float16\" else \"f32\"",11 unmodified lines"name": "MultiHeadAttention.BiasOnlineFlashNoSg","shader": "attn-flash-online.wgsl.jinja","derive": { "layout": "\"bsh\"" },"bindings": ["query", "key", "value", "attn_mask_main", "bias", "output", "params__uniform"],"dispatch": { "x": "dim(shapes.queryT, 1)", "y": "attrs.num_heads", "z": "dim(shapes.queryT, 0)" }}]8 unmodified lines},"derive": {"qNumHeads": "attrs.num_heads","qHidden": "dim(shapes.queryT, 2)","materializedSoftmaxWg": "materializedCachedSoftmaxWg if materializedCachedSoftmaxOk else tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE","materializedSoftmaxCols": "dim(shapes.keyT, 1)","materializedSoftmaxCols4": "dim(shapes.keyT, 1) / 4","softmaxElementType": "\"vec4<f32>\" if materializedCachedSoftmaxOk else \"f32\"","useSubgroups": true},"intermediates": [9 unmodified lines"name": "MultiHeadAttention.MaterializedScoresSgmat","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"score\"" },"bindings": ["query_f32", "key_f32", "scores", "params_scores"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",5 unmodified lines"name": "MultiHeadAttention.MaterializedSoftmaxSgmat","shader": "attn-materialized-softmax-f32.wgsl.jinja","derive": { "cacheVec4": "materializedCachedSoftmaxOk" },"bindings": ["scores_softmax", "params_rows"],"dispatch": {"x": "min(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","y": "ceilDiv(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","z": 1}},{"id": "apply","name": "MultiHeadAttention.MaterializedApplySgmat","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"apply\"" },"bindings": ["scores_f32", "value_f32", "output_f32", "params_apply"],"dispatch": {"x": "ceilDiv(headDim, materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",12 unmodified lines},"derive": {"qNumHeads": "attrs.num_heads","qHidden": "dim(shapes.queryT, 2)","materializedSoftmaxWg": "materializedCachedSoftmaxWg if materializedCachedSoftmaxOk else tunables.MATERIALIZED_SOFTMAX_WORKGROUP_SIZE","materializedSoftmaxCols": "dim(shapes.keyT, 1)","materializedSoftmaxCols4": "dim(shapes.keyT, 1) / 4","softmaxElementType": "\"vec4<f32>\" if materializedCachedSoftmaxOk else \"f32\"","useSubgroups": true},"intermediates": [9 unmodified lines"name": "MultiHeadAttention.MaterializedScoresSgmatBias","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"score\"" },"bindings": ["query_f32", "key_f32", "bias_f32", "scores", "params_scores"],"dispatch": {"x": "ceilDiv(dim(shapes.keyT, 1), materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",5 unmodified lines"name": "MultiHeadAttention.MaterializedSoftmaxSgmatBias","shader": "attn-materialized-softmax-f32.wgsl.jinja","derive": { "cacheVec4": "materializedCachedSoftmaxOk" },"bindings": ["scores_softmax", "params_rows"],"dispatch": {"x": "min(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","y": "ceilDiv(dim(shapes.queryT, 0) * attrs.num_heads * dim(shapes.queryT, 1), 65535)","z": 1}},{"id": "apply","name": "MultiHeadAttention.MaterializedApplySgmatBias","shader": "attn-materialized-sgmat-f32.wgsl.jinja","derive": { "phase": "\"apply\"" },"bindings": ["scores_f32", "value_f32", "bias_f32", "output_f32", "params_apply"],"dispatch": {"x": "ceilDiv(headDim, materializedSgmatKeyTile)","y": "ceilDiv(dim(shapes.queryT, 1), materializedSgmatQueryTile)",
123456789101112131415161718192021222324252627282930313233343536{"name": "com.microsoft.MultiHeadAttention","id": "_com_microsoft_multiheadattention_webgpu_5144476","version": 1,"license": "Apache-2.0","backend": { "type": "webgpu" },"digest": {"algorithm": "sha256","files": {"attention-rank4-tiled.wgsl.jinja": "gYl58ecIVTxJJkvnK/6lNcbPKaRT24cfz5Yx0MxxxxE=","attn-flash-decode-splitk-merge.wgsl.jinja": "XSAMF/uzQKJg9pZZDeqmViaVXuj47Vd7Pk404Z1J48I=","attn-flash-decode-splitk.wgsl.jinja": "dqxX8taw1tpo/QQkFr2zJ7TEEtQtOspTYEYHADXtESs=","attn-flash-online.wgsl.jinja": "hCxO/6VGkyixwyoT/kuYeHgcEkFoRwk/ZmiLlnwa0jM=","attn-flash-prefill-cluster.wgsl.jinja": "igbsg5xRuFz7alnwmL74hhh1JUqfDB5PfUslsEz5rjA=","attn-flash-q32-broadcast.wgsl.jinja": "8qgKXydwiH2shac/fkdHi+FR+c8pOMDFbNp9Zs8KAnc=","attn-materialized-apply-f32.wgsl.jinja": "aF7JSJj8rQN8wGTV7IWmxkqMDbXOnGBSI3tsI1CxZNk=","attn-materialized-rowstats-combine-f32.wgsl.jinja": "+/5m03sN44zTn9q7cquDea2UNhEO1ujzm1J7yPai4OY=","attn-materialized-score-f32.wgsl.jinja": "gCvBC1T1CLEmNxcpo04CLBQdFBkEVMJ9Xhv1wbQxCsI=","attn-materialized-sgmat-f32.wgsl.jinja": "Ez9UJggPXMZ6OyzUQu8xLtg8bZd+lTCoEGDwLkNHNL0=","attn-materialized-softmax-f32.wgsl.jinja": "DCuz4gFKXDCdR9UMnfD+mYFjBghnv12GxtEsPKH52mE=","attn-online-scalar.wgsl.jinja": "6+hU7MzwHz69fjciv94xsPxN+o9rwm42u9Cin2C+AL8=","attn-small-head-parallel.wgsl.jinja": "oxFE6hQZKTRugz9hwuS+cob00PskT8iYqP4e+YWtHKA=","bench.json": "b/WdaAhEP5NpRXNB5w1mivRNTfALrBDJisDONxTf4Yo=","manifest.json": "+/bNCAjkG5OoutFq7O4vQJqb2yMxzICsKvFXsRE50bA=","mha-small-seq-blocked.wgsl.jinja": "zznGERrPco5uH9tKiQ2QE+ArO3vho/nEv69wT8IgY1U=","mha-small-seq.wgsl.jinja": "UsbUIkvgWL/RgOBSm7lhPGIatA36yTQR4Z2gpHVLqx4=","test.json": "NlMA/wlmGSpogAuhSZ/kcQ01dd3M+CvkPF0d/VBmIpM="}},"provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } },"webgpu": {"manifestSpec": "2.0","variants": {"qkv_bias_small_seq_blocked": ["mha-small-seq-blocked.wgsl.jinja"],"qkv_no_bias_small_seq_blocked": ["mha-small-seq-blocked.wgsl.jinja"],"qkv_no_bias_small_seq": ["mha-small-seq.wgsl.jinja"],123456789101112131415161718192021222324252627282930313233343536373839{"name": "com.microsoft.MultiHeadAttention","id": "_com_microsoft_multiheadattention_webgpu_e942a19","version": 1,"license": "Apache-2.0","backend": { "type": "webgpu" },"digest": {"algorithm": "sha256","files": {"attention-rank4-tiled.wgsl.jinja": "084x5bbpxeFR95/Q4nGzLrLnk9JJrd9XA1KJHxwU9CY=","attn-flash-decode-splitk-merge.wgsl.jinja": "JIhoYhGkuat/Wrvix6ddD19+bmzHnc0yJdwWbW9o+nw=","attn-flash-decode-splitk.wgsl.jinja": "tDj5VlltkM0N99vjpj6z5wtlIv00benlaAboa4e5vWI=","attn-flash-online.wgsl.jinja": "1DGpp4kzd/v4gzAviJKnYGCT7BwDomzXK3He6CIT+1E=","attn-flash-prefill-cluster.wgsl.jinja": "pjDvKLZ3CSluSAPNfb9j3U7yKEy+wVOfOAyZ1jdrjv8=","attn-flash-q32-broadcast.wgsl.jinja": "puOnD1vGGeVg6HmaUi8k4JAZJviJM1dXWJyMZ2xuURM=","attn-materialized-apply-f32.wgsl.jinja": "E2Vas8y/zcjHQb4Ks5TPnC4/lAhVRawXR5S1OwJB1Tc=","attn-materialized-rowstats-combine-f32.wgsl.jinja": "CBltqk0CN9vvscb6D29o1T43vHWZYlU9fFpO9av2Sa8=","attn-materialized-score-f32.wgsl.jinja": "bxW7HdJcF47NTbyXVHcaWrtCO0xCJ8pW5GW9/FTuGGg=","attn-materialized-sgmat-f32.wgsl.jinja": "3e4ZOgVD5XSnYjVldRVq5QL72KiloCkr91OZj/VU0ZU=","attn-materialized-softmax-f32.wgsl.jinja": "bpHIAAuUa+nrniTKxXEScXT5rJr5nObXY/sJ81hby5A=","attn-online-scalar.wgsl.jinja": "AbpGvmzka15nPEj3QZPyMaW48Lo/9TqtndP0SdyqV/U=","attn-small-head-parallel.wgsl.jinja": "Y0otPUGeAa4DzQUi+LnWLRTATc1i/awpIipRqbGDoQ0=","attn-small-head-value.wgsl.jinja": "+6NLq7AJT3lRkAwsW2AvjGE0nhkD7dkBLqh6SpOFjy4=","bench.json": "ateXBEoOVVMHNfiuXPmDW6sKzU1NRVRA9ndzslgsD2A=","manifest.json": "VPWToEdcoP2Mr6k2oQuf/BhwUZdbcPl9J5YLZsMBMaE=","mha-small-seq-blocked.wgsl.jinja": "WudxiAnGnY7dSEnlhHtwvQKvm4AN3ngQ9cbBfI6OUBM=","mha-small-seq.wgsl.jinja": "1MR+JuEbffrS+QkuAZzFzWCLPX4VX9RJ5lmNIOm0/o0=","test.json": "IEMAo5OqrMxRC7xc5Y/8xmjWObXuWw1SJhJeGGzDhwc="}},"provenance": { "kernel": { "sha": "6fdf6301e2bbcc2f03bf1eaf493b7ad55ef33afc", "dirty": false } },"webgpu": {"manifestSpec": "2.1","variants": {"qkv_no_bias_small_head_value_subgroups": ["attn-small-head-value.wgsl.jinja"],"qkv_no_bias_small_head_value_tree": ["attn-small-head-value.wgsl.jinja"],"qkv_bias_small_seq_blocked": ["mha-small-seq-blocked.wgsl.jinja"],"qkv_no_bias_small_seq_blocked": ["mha-small-seq-blocked.wgsl.jinja"],"qkv_no_bias_small_seq": ["mha-small-seq.wgsl.jinja"],
36 unmodified lines3738394041424344454647484950515253545536 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}{% if ATTN_SCALE_DIM is not defined %}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}{% endif %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}{% if hasBias %}fn load_bias4(base: u32, d4: u32) -> vec4<f32> {let offset = base + d4 * 4u;return vec4<f32>(bias[offset], bias[offset + 1u], bias[offset + 2u], bias[offset + 3u]);}{% endif %}36 unmodified lines373839404142434445464748495051525354555636 unmodified lineslet equalFiniteMax = select(false, value == maxValue, is_finite_f32(maxValue));return select(value - maxValue, 0.0, equalFiniteMax);}fn exp_shift(value: f32, maxValue: f32) -> f32 {return exp(shifted_value(value, maxValue));}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}{% if hasBias %}{% set BW = "" %}{% set BC = "" %}fn load_bias4(base: u32, d4: u32) -> vec4<f32> {let offset = base + d4 * 4u;return vec4<f32>({{ BW }}bias[offset]{{ BC }}, {{ BW }}bias[offset + 1u]{{ BC }}, {{ BW }}bias[offset + 2u]{{ BC }}, {{ BW }}bias[offset + 3u]{{ BC }});}{% endif %}
12344 unmodified lines910111213141516171819202134 unmodified lines5657585960616263646566{{ env.wgsl.resourceDeclarations }}// Whole-head attention for tiny bidirectional sequences without bias or mask.// One workgroup owns one (head, batch) and stages the complete K and V planes in4 unmodified linesconst KV_SEQ: u32 = {{ kvSeq }}u;const HIDDEN: u32 = {{ hidden }}u;const WG: u32 = {{ workgroupSize }}u;{% if ATTN_SCALE_DIM is not defined %}{% set ATTN_SCALE_DIM = "HEAD_DIM" %}{% endif %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}var<workgroup> kShared: array<f32, KV_SEQ * HEAD_DIM>;var<workgroup> vShared: array<f32, KV_SEQ * HEAD_DIM>;34 unmodified linesvar scores: array<f32, KV_SEQ>;var maxScore = -3.4028234663852886e38;for (var k = 0u; k < KV_SEQ; k++) {var dot = 0.0;for (var d = 0u; d < HEAD_DIM; d++) {dot += qRow[d] * kShared[k * HEAD_DIM + d];}let s = dot * scale;scores[k] = s;maxScore = max(maxScore, s);}12345678910111213141516171819202122232425264 unmodified lines313233343536373839404134 unmodified lines7677787980818283848586{{ env.wgsl.resourceDeclarations }}{% set dotType = "f32" %}// Retain product and addition residuals across a dot product. Explicit fma// boundaries preserve the addition error transform under reassociation.struct DotAccumulator {hi: {{ dotType }},lo: {{ dotType }},}fn dot_accumulate(acc: DotAccumulator, a: {{ dotType }}, b: {{ dotType }}) -> DotAccumulator {let product = fma(a, b, {{ dotType }}(0.0));let productError = fma(a, b, -product);let sum = fma(acc.hi, {{ dotType }}(1.0), product);let bv = fma({{ dotType }}(-1.0), acc.hi, sum);let av = fma({{ dotType }}(-1.0), bv, sum);let ae = fma({{ dotType }}(-1.0), av, acc.hi);let be = fma({{ dotType }}(-1.0), bv, product);let error = fma(fma(fma(ae, {{ dotType }}(1.0), be), {{ dotType }}(1.0), acc.lo), {{ dotType }}(1.0), productError);let hi = fma(sum, {{ dotType }}(1.0), error);return DotAccumulator(hi, fma({{ dotType }}(-1.0), fma({{ dotType }}(-1.0), sum, hi), error));}fn dot_value(acc: DotAccumulator) -> {{ dotType }} {return fma(acc.hi, {{ dotType }}(1.0), acc.lo);}// Whole-head attention for tiny bidirectional sequences without bias or mask.// One workgroup owns one (head, batch) and stages the complete K and V planes in4 unmodified linesconst KV_SEQ: u32 = {{ kvSeq }}u;const HIDDEN: u32 = {{ hidden }}u;const WG: u32 = {{ workgroupSize }}u;{% set ATTN_SCALE_DIM = "HEAD_DIM" %}fn scale_value() -> f32 {if (params.scale != 0.0) { return params.scale; }return inverseSqrt(f32({{ ATTN_SCALE_DIM }}));}var<workgroup> kShared: array<f32, KV_SEQ * HEAD_DIM>;var<workgroup> vShared: array<f32, KV_SEQ * HEAD_DIM>;34 unmodified linesvar scores: array<f32, KV_SEQ>;var maxScore = -3.4028234663852886e38;for (var k = 0u; k < KV_SEQ; k++) {var dot = DotAccumulator(0.0, 0.0);for (var d = 0u; d < HEAD_DIM; d++) {dot = dot_accumulate(dot, qRow[d], kShared[k * HEAD_DIM + d]);}let s = dot_value(dot) * scale;scores[k] = s;maxScore = max(maxScore, s);}
README.md+5-2
build/webgpu/attention-rank4-tiled.wgsl.jinja+1-25
build/webgpu/attn-flash-decode-splitk-merge.wgsl.jinja+2-9
build/webgpu/attn-flash-decode-splitk.wgsl.jinja+14-115
build/webgpu/attn-flash-online.wgsl.jinja+8-44
build/webgpu/attn-flash-prefill-cluster.wgsl.jinja+41-194
build/webgpu/attn-flash-q32-broadcast.wgsl.jinja+46-11
build/webgpu/attn-materialized-apply-f32.wgsl.jinja+3-2
build/webgpu/attn-materialized-rowstats-combine-f32.wgsl.jinja+9-5
build/webgpu/attn-materialized-score-f32.wgsl.jinja+6-85
build/webgpu/attn-materialized-sgmat-f32.wgsl.jinja+28-54
build/webgpu/attn-materialized-softmax-f32.wgsl.jinja+6-41
build/webgpu/attn-online-scalar.wgsl.jinja+39-28
build/webgpu/attn-small-head-parallel.wgsl.jinja+3-9
build/webgpu/attn-small-head-value.wgsl.jinjaAdded+198
build/webgpu/bench.json
build/webgpu/manifest.json+224-362
build/webgpu/metadata.json+24-21
build/webgpu/mha-small-seq-blocked.wgsl.jinja+5-4
build/webgpu/mha-small-seq.wgsl.jinja+26-6
build/webgpu/test.json