File size: 3,676 Bytes
772a078
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{% 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<workgroup> normParts: array<vec2f, 128>;
var<workgroup> normSquaredMax: atomic<u32>;
var<workgroup> valueMax: atomic<u32>;
var<workgroup> normOneMax: atomic<u32>;
var<workgroup> finestGrid: atomic<u32>;

@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<u32>(value[base + d]) & 0x7fffffffu);
      }
      let bits = bitcast<u32>(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<u32>(rowSums.x));
    atomicMax(&normOneMax, bitcast<u32>(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<f32>(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<f32>(atomicLoad(&normSquaredMax))) * 1.0001;
    let stepBits = (bitcast<u32>(max(norm2 / 4080.0, 0x1p-48)) + 0x7fffffu) & 0x7f800000u;
    let norm1 = bitcast<f32>(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<f32>(stepBits), norm2, maxValue, select(norm1, -norm1, onGrid));
  }
}