com.microsoft.GroupQueryAttention / build /webgpu /gqa-norm-rotate-rows.wgsl.jinja
Xenova's picture
Xenova HF Staff
sync 4385228f788d
772a078 verified
Raw History Blame
3.51 kB
{% macro attn_seqlens_count(index) %}(u32(max(seqlens_k[{{ index }}], 0)) + select(0u, 1u, seqlens_k[{{ index }}] >= 0)){% endmacro %}
{{ env.wgsl.resourceDeclarations }}
// Per-head RMS normalization and NeoX half-split rotation of new rows for a buffer-sharing
// cache, one workgroup per head row: the lanes share the head's sum of squares through
// workgroup memory, then each rotates whole (d, d + HALF) pairs at the row's absolute position,
// the end of its batch's live range `seqlens_k[b] + 1`. A position past the rotary tables uses
// cos = sin = 0, so the row is written as zeros.
{% if rowKind == "key" %}
// Key rows: each (batch, kvHead, token) row is written, with its raw value row, into the
// shared-capacity BNSH caches at the batch's append offset. Rows outside the append window are
// copied from the past cache by a separate pass (or are the same buffer).
{% else %}
// Query rows: each (batch, token, head) row is written to the BSH scratch buffer that attention reads.
{% endif %}
const HEAD_DIM: u32 = {{ headDim }}u;
const HALF: u32 = {{ half }}u;
{% if rowKind == "key" %}
const HEADS: u32 = {{ kvHeads }}u;
{% else %}
const HEADS: u32 = {{ qHeads }}u;
{% endif %}
const WG: u32 = {{ copyWorkgroupSize }}u;
const QK_EPS: f32 = {{ qkEps }};
var<workgroup> partial: array<f32, WG>;
@compute @workgroup_size(WG)
fn main(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
let row = wid.x + wid.y * {{ DISPATCH_FOLD_WIDTH }}u;
{% if rowKind == "key" %}
if (row >= params.count) {
return;
}
let j = row % params.keySeq;
let rest = row / params.keySeq;
let h = rest % HEADS;
let b = rest / HEADS;
// Clamping below by keySeq maps a right-padded first prompt
// (seqlens_k[b]+1 < keySeq) to append offset 0.
let activeEnd = min(params.seq, max(params.keySeq, {{ attn_seqlens_count("b") }}));
let t = activeEnd - params.keySeq + j;
let src = (b * params.keySeq + j) * HEADS * HEAD_DIM + h * HEAD_DIM;
let dst = ((b * HEADS + h) * params.seq + t) * HEAD_DIM;
{% else %}
if (row >= params.batch * params.qSeq * HEADS) {
return;
}
let h = row % HEADS;
let s = (row / HEADS) % params.qSeq;
let b = row / (HEADS * params.qSeq);
let t = min(params.totalSeq, max(params.qSeq, {{ attn_seqlens_count("b") }})) - params.qSeq + s;
let src = (b * params.qSeq + s) * HEADS * HEAD_DIM + h * HEAD_DIM;
let dst = src;
{% endif %}
var ss = 0.0;
for (var d = lid.x; d < HEAD_DIM; d = d + WG) {
let x = f32({{ rowSource }}[src + d]);
ss = ss + x * x;
}
partial[lid.x] = ss;
workgroupBarrier();
for (var stride = WG / 2u; stride > 0u; stride = stride / 2u) {
if (lid.x < stride) {
partial[lid.x] = partial[lid.x] + partial[lid.x + stride];
}
workgroupBarrier();
}
let invRms = inverseSqrt(partial[0] / f32(HEAD_DIM) + QK_EPS);
for (var d = lid.x; d < HALF; d = d + WG) {
var cs = 0.0;
var sn = 0.0;
if (t < {{ gqaRotaryCacheRows }}u) {
cs = f32(cos_cache[t * HALF + d]);
sn = f32(sin_cache[t * HALF + d]);
}
let x0 = f32({{ rowSource }}[src + d]) * invRms * f32({{ rowNormWeight }}[d]);
let x1 = f32({{ rowSource }}[src + d + HALF]) * invRms * f32({{ rowNormWeight }}[d + HALF]);
{{ rowDestination }}[dst + d] = x0 * cs - x1 * sn;
{{ rowDestination }}[dst + d + HALF] = x1 * cs + x0 * sn;
}
{% if rowKind == "key" %}
for (var d = lid.x; d < HEAD_DIM; d = d + WG) {
present_value[dst + d] = new_v[src + d];
}
{% endif %}
}