#version 450 //! Softmax over the **last** axis, one invocation per row. //! //! Two uses, both on an attention score map in PP-OCRv5 recognition. //! //! # Why the last axis, and why that needs no indexing //! //! The scores are `[heads, T, T]` with the key index innermost, so a row — one query's //! distribution over every key — is a contiguous run of `out_w` elements. The row a //! given invocation owns is therefore just `row * out_w`, and `p.count` is the number of //! rows (`out_c * out_h`) rather than the element count. No `unpack` and no stride. //! //! That is only true because `nets::Kind::AttnScores` chose to emit the key index last. //! A softmax over any other axis of this runtime's tensors would be a different shader, //! and there is no use for one. //! //! # The maximum is subtracted, and it is not optional //! //! `exp` here is fp32, which overflows to infinity a little past 88. Scores are scaled //! by `1 / sqrt(head_dim)` before they arrive so they are usually well inside that, but //! "usually" is doing no work in a shader: one saturated activation upstream turns the //! whole row into `inf / inf` and every probability into a NaN. Subtracting the row //! maximum first makes the largest term exactly `exp(0)`, which also means the //! denominator is at least 1 and the division cannot blow up. //! //! The result is unchanged in exact arithmetic — `exp(x - m) / sum(exp(x - m))` is the //! same function — so this costs one extra pass over a row of `T` and buys the property //! that the output is always a distribution. //! //! # Three passes over the row //! //! Maximum, then sum, then store, re-evaluating `exp` in the third rather than keeping //! the row in registers: `T` is around 80 for a line of text, which is far too large for //! a fixed-size local array and too small for the bandwidth to matter. //! //! # Row mode //! //! With `p.kw == 1` one workgroup owns one row and its lanes split the keys, for the few //! long rows a decode step has (whisper's cross-attention is 1,500 keys). See `reduce.glsl`. //! //! # Key bound //! //! A non-zero `p.kh` normalises only the leading `p.kh` keys of each row and stores zeros past //! them, for a sequence padded to a multiple of eight positions (Gemma 4's vision tower): the //! padded keys then add exact zeros in `attn_apply`. #extension GL_KHR_shader_subgroup_basic : require #extension GL_KHR_shader_subgroup_arithmetic : require #include "common.glsl" #include "reduce.glsl" void row_mode() { uint row = reduce_row(); if (row >= p.count) { return; } uint lane = gl_LocalInvocationID.x; uint lanes = gl_WorkGroupSize.x; uint at = p.in0 + row * p.out_w; uint keys = p.kh == 0u ? p.out_w : p.kh; float peak = -65504.0; for (uint i = lane; i < keys; i += lanes) { peak = max(peak, float(arena[at + i])); } peak = workgroup_max(peak); float total = 0.0; for (uint i = lane; i < keys; i += lanes) { total += exp(float(arena[at + i]) - peak); } float inverse = 1.0 / workgroup_sum(total); uint out_row = p.out_at + row * p.out_w; for (uint i = lane; i < p.out_w; i += lanes) { arena[out_row + i] = i < keys ? float16_t(exp(float(arena[at + i]) - peak) * inverse) : float16_t(0.0); } } void main() { if (reduce_row_mode()) { row_mode(); return; } uint row = global_index(); if (row >= p.count) { return; } uint at = p.in0 + row * p.out_w; uint keys = p.kh == 0u ? p.out_w : p.kh; // The most negative fp16 rather than -inf, matching `maxpool.comp`: every row here // is non-empty, so the seed is never the answer. float peak = -65504.0; for (uint i = 0u; i < keys; i++) { peak = max(peak, float(arena[at + i])); } float total = 0.0; for (uint i = 0u; i < keys; i++) { total += exp(float(arena[at + i]) - peak); } float inverse = 1.0 / total; uint out_row = p.out_at + row * p.out_w; for (uint i = 0u; i < keys; i++) { arena[out_row + i] = float16_t(exp(float(arena[at + i]) - peak) * inverse); } for (uint i = keys; i < p.out_w; i++) { arena[out_row + i] = float16_t(0.0); } }