{% 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) { {{ 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 }}(x[base + task]); let bIndex = base + params.halfRotaryDim / {{ pairWidth }}u + task; let b = vec{{ pairWidth }}(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; }