{% macro attn_seqlens_count(index) %}(u32(max(seqlens_k[{{ index }}], 0)) + select(0u, 1u, seqlens_k[{{ index }}] >= 0)){% endmacro %} {% macro attn_seqlens_bound(capacity, index, queryFloor="") %}min({{ capacity }}, {{ attn_seqlens_count(index) }}){% endmacro %} {{ env.wgsl.resourceDeclarations }} const HEAD_DIM = {{ headDim }}u; const Q_HEADS = {{ qNumHeads }}u; const KV_HEADS = {{ kvNumHeads }}u; const INVALID_GRID = 0xffffffffu; var normParts: array; var normSquaredMax: atomic; var valueMax: atomic; var normOneMax: atomic; var finestGrid: atomic; @compute @workgroup_size(128) fn main(@builtin(workgroup_id) wg: vec3u, @builtin(local_invocation_index) lane: u32) { let b = wg.z / Q_HEADS; let head = wg.z % Q_HEADS; let isQuery = wg.y == 0u; if (!isQuery && head >= KV_HEADS) { return; } let seq = select(params.kvSeq, params.qSeq, isQuery); let live = select({{ attn_seqlens_bound("params.kvSeq", "b") }}, params.qSeq, isQuery); let first = wg.x * 32u; if (first >= seq) { return; } let row = first + lane / 4u; let heads = select(KV_HEADS, Q_HEADS, isQuery); let base = ((b * seq + row) * heads + head) * HEAD_DIM; var norm = vec2f(0.0); var valueBits = 0u; var gridBits = 0u; if (row < live) { for (var d = lane % 4u; d < HEAD_DIM; d += 4u) { var x: f32; if (isQuery) { x = query[base + d]; } else { x = key[base + d]; valueBits = max(valueBits, bitcast(value[base + d]) & 0x7fffffffu); } let bits = bitcast(x) & 0x7fffffffu; if (bits == 0u) { continue; } // Magnitudes outside [2^-32, 2^32) can underflow or overflow the split. if (bits < 0x2f800000u || bits >= 0x4f800000u) { gridBits = INVALID_GRID; continue; } // The last set significand bit gives the finest grid this entry needs. let significand = (bits & 0x7fffffu) | 0x800000u; gridBits = max(gridBits, 278u - (bits >> 23u) - countTrailingZeros(significand)); norm += vec2f(x * x, abs(x)); } } normParts[lane] = norm; atomicMax(&valueMax, valueBits); atomicMax(&finestGrid, gridBits); workgroupBarrier(); if (lane % 4u == 0u) { let rowSums = (norm + normParts[lane + 1u]) + (normParts[lane + 2u] + normParts[lane + 3u]); atomicMax(&normSquaredMax, bitcast(rowSums.x)); atomicMax(&normOneMax, bitcast(rowSums.y)); } workgroupBarrier(); if (lane == 0u) { let qChunks = (params.qSeq + 31u) / 32u; let kChunks = (params.kvSeq + 31u) / 32u; let headBase = select(Q_HEADS * qChunks + head * kChunks, head * qChunks, isQuery); let index = b * (Q_HEADS * qChunks + KV_HEADS * kChunks) + headBase + wg.x; let maxValue = bitcast(min(atomicLoad(&valueMax), 0x7f7fffffu)); let grid = atomicLoad(&finestGrid); if (grid == INVALID_GRID) { ranges[index] = vec4f(-1.0, 0.0, maxValue, 0.0); return; } // Inflate the reductions before rounding the grid spacing up to a power of two. // Rounding at most 256 components adds at most 8 to the integer L2 norm, // so every leading dot and partial sum fits in the 24-bit significand. let norm2 = sqrt(bitcast(atomicLoad(&normSquaredMax))) * 1.0001; let stepBits = (bitcast(max(norm2 / 4080.0, 0x1p-48)) + 0x7fffffu) & 0x7f800000u; let norm1 = bitcast(atomicLoad(&normOneMax)) * 1.0001; let onGrid = grid <= 255u - (stepBits >> 23u); // The sign of the L1 bound records whether the row block has a zero residual. ranges[index] = vec4f(bitcast(stepBits), norm2, maxValue, select(norm1, -norm1, onGrid)); } }