File size: 3,374 Bytes
8929c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fc11f4
8929c9c
 
 
 
0fc11f4
8929c9c
 
0fc11f4
 
 
8929c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{% macro mrope_cache_index(pair, suffix="") %}
  // Which of the three position streams owns this cache column.
  var stream{{ suffix }} = 0u;
{% if mropeSectioned %}
  // Sectioned (Qwen2-VL, Qwen2.5-VL): three contiguous chunks [T | H | W].
  if ({{ pair }} >= params.section0 && {{ pair }} < params.section0 + params.section1) {
    stream{{ suffix }} = 1u;
  } else if ({{ pair }} >= params.section0 + params.section1) {
    stream{{ suffix }} = 2u;
  }
{% else %}
  // Interleaved (Qwen3-VL, Qwen3.5): T everywhere, then H over every third column
  // from offset 1 and W over every third column from offset 2, each bounded by its
  // own section length.
  if ({{ pair }} % 3u == 1u && {{ pair }} / 3u < params.section1) {
    stream{{ suffix }} = 1u;
  } else if ({{ pair }} % 3u == 2u && {{ pair }} / 3u < params.section2) {
    stream{{ suffix }} = 2u;
  }
{% endif %}

  let pos{{ suffix }} = position_ids[(stream{{ suffix }} * params.batchSize + batch) * params.sequenceLength + token];
  let valid{{ suffix }} = pos{{ suffix }} >= 0 && u32(pos{{ suffix }}) < params.maxSequenceLength;
  let cache{{ suffix }} = u32(pos{{ suffix }}) * params.halfRotaryDim + {{ pair }};
{% endmacro %}
{% macro flat_index_2d(workgroupSize, name="i", bound="params.count") %}
{% 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 bound != "" %}
  if ({{ name }} >= {{ bound }}) {
    return;
  }
{% endif %}
{% endmacro %}
{{ env.wgsl.resourceDeclarations }}
// A contiguous vector owns several split-half rotation pairs. Each component
// gathers its own stream position, including vectors crossing section boundaries.
@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
{{ flat_index_2d(tunables.WORKGROUP_SIZE, "p", "params.pairCount / " ~ pairWidth ~ "u") }}
  let tasksPerHead = params.headSize / {{ 2 * pairWidth }}u;
  let task = p % tasksPerHead;
  let headFlat = p / tasksPerHead;
  let base = headFlat * (params.headSize / {{ pairWidth }}u);
{% if rank == 3 %}
  let token = (headFlat / params.numHeads) % params.sequenceLength;
  let batch = headFlat / (params.numHeads * params.sequenceLength);
{% else %}
  let token = headFlat % params.sequenceLength;
  let batch = (headFlat / params.sequenceLength) / params.numHeads;
{% endif %}
{% for i in range(pairWidth) %}
  let pair{{ i }} = task * {{ pairWidth }}u + {{ i }}u;
{{ mrope_cache_index("pair" ~ i, i) }}
{% endfor %}
  let a = vec{{ pairWidth }}<f32>(x[base + task]);
  let bIndex = base + params.halfRotaryDim / {{ pairWidth }}u + task;
  let b = vec{{ pairWidth }}<f32>(x[bIndex]);
  var outA = x[base + task];
  var outB = x[bIndex];
{% for i in range(pairWidth) %}
  if (valid{{ i }}) {
    // Round scaled coefficients to storage T before evaluating the rotation.
    let cf = f32({{ scalar }}(f32(cos_cache[cache{{ i }}]) * params.scale));
    let sf = f32({{ scalar }}(f32(sin_cache[cache{{ i }}]) * params.scale));
    outA[{{ i }}] = {{ scalar }}(a[{{ i }}] * cf - b[{{ i }}] * sf);
    outB[{{ i }}] = {{ scalar }}(a[{{ i }}] * sf + b[{{ i }}] * cf);
  }
{% endfor %}
  y[base + task] = outA;
  y[bIndex] = outB;
}