#version 450
#extension GL_EXT_control_flow_attributes : enable
#extension GL_EXT_shader_16bit_storage : require
#if USE_SUBGROUPS
#extension GL_KHR_shader_subgroup_basic : enable
#extension GL_KHR_shader_subgroup_arithmetic : enable
#endif
#include "types.glsl"
layout(constant_id = 0) const int BLOCK_SIZE = 1024;
layout(constant_id = 1) const int QSA = 0; // 1: fuse the qwen4 QSA indexer gather + f16 mask
#if USE_SUBGROUPS
layout(constant_id = 2) const int SUBGROUP_SIZE = 32;
const uint NUM_SUBGROUPS = BLOCK_SIZE / SUBGROUP_SIZE;
#endif
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
layout (binding = 0) readonly buffer A {float data_a[];}; // input values, or QSA block scores [n_tps, n_blocks, n_stream]
layout (binding = 1) writeonly buffer D {int data_d[];}; // [k, ...]
layout (binding = 2) readonly buffer CB {int cell_blk[];}; // QSA: cell->block map [n_kv, n_stream]
layout (binding = 3) readonly buffer M {float16_t mask[];}; // QSA: raw f16 kq_mask [n_kv, n_tps, n_stream]
layout (binding = 4) buffer S {float scratch[];}; // QSA: [nrows, n_kv] gathered inputs
layout (push_constant) uniform parameter {
uint ncols;
uint k;
uint nrows;
uint n_tps; // QSA only
uint n_blocks; // QSA only
uint n_stream; // QSA only
} p;
#define RADIX_BITS 8
#define UNROLL 4
#define RADIX_SIZE (1 << RADIX_BITS)
shared uint histo[RADIX_SIZE];
shared uint sh_bucket;
shared uint sh_above;
shared uint sh_tied;
shared uint out_count;
#if USE_SUBGROUPS
shared uint sg_tied[NUM_SUBGROUPS];
#else
shared uint tie_scan[2 * BLOCK_SIZE];
#endif
// order-preserving float -> uint mapping
uint f2ui(float x) {
uint y = floatBitsToUint(x);
if ((y & 0x80000000u) != 0u) {
y ^= 0xFFFFFFFFu;
} else {
y |= 0x80000000u;
}
return y;
}
// QSA element i of row (t,s): score[cell_blk[i,s], t, s] + mask[i,t,s]
float gather(uint row, uint i) {
const uint t = row % p.n_tps;
const uint s = row / p.n_tps;
const uint block = uint(cell_blk[s * p.ncols + i]);
const float a = data_a[(s * p.n_blocks + block) * p.n_tps + t];
const float m = float(mask[(s * p.n_tps + t) * p.ncols + i]);
return a + m;
}
float load(uint row, uint i, bool first) {
if (QSA == 0) {
return data_a[row * p.ncols + i];
}
// materialize the scattered gather on the first pass and reuse it after; each
// invocation only touches its own scratch entries, so no barrier is needed
const uint off = row * p.ncols + i;
if (first) {
const float v = gather(row, i);
scratch[off] = v;
return v;
}
return scratch[off];
}
// one workgroup per row: radix-select the K-th largest, then compact it plus enough ties
void topk(const uint row) {
const uint tid = gl_LocalInvocationID.x;
const uint ncols = p.ncols;
const uint row_out = row * p.k;
uint prefix = 0; // fixed high bits of the threshold key
uint desired = p.k; // count still needed from the candidate range
[[unroll]] for (int shift = 32 - RADIX_BITS; shift >= 0; shift -= RADIX_BITS) {
for (uint i = tid; i < RADIX_SIZE; i += BLOCK_SIZE) {
histo[i] = 0;
}
barrier();
const bool first = (shift == 32 - RADIX_BITS);
const uint hi_mask = (shift + RADIX_BITS >= 32) ? 0u : (0xFFFFFFFFu << uint(shift + RADIX_BITS));
const uint prefix_hi = prefix & hi_mask;
uint run_b = RADIX_SIZE;
uint run_n = 0;
for (uint i0 = tid; i0 < ncols; i0 += UNROLL * BLOCK_SIZE) {
uint keys[UNROLL];
[[unroll]] for (uint u = 0; u < UNROLL; ++u) {
const uint i = i0 + u * BLOCK_SIZE;
keys[u] = f2ui(load(row, min(i, ncols - 1), first));
}
[[unroll]] for (uint u = 0; u < UNROLL; ++u) {
const uint key = keys[u];
if (i0 + u * BLOCK_SIZE < ncols && (key & hi_mask) == prefix_hi) {
const uint b = (key >> uint(shift)) & (RADIX_SIZE - 1);
if (b != run_b) {
if (run_n != 0) {
atomicAdd(histo[run_b], run_n);
}
run_b = b;
run_n = 0;
}
run_n++;
}
}
}
if (run_n != 0) {
atomicAdd(histo[run_b], run_n);
}
barrier();
// top-down scan for the bucket holding the K-th value
#if USE_SUBGROUPS
uint c = 0, incl = 0;
if (tid < RADIX_SIZE) {
c = histo[RADIX_SIZE - 1 - tid];
incl = subgroupInclusiveAdd(c);
if (gl_SubgroupInvocationID == gl_SubgroupSize - 1) {
sg_tied[gl_SubgroupID] = incl;
}
}
barrier();
if (tid < RADIX_SIZE) {
for (uint s = 0; s < gl_SubgroupID; ++s) {
incl += sg_tied[s];
}
if (incl - c < desired && incl >= desired) {
sh_bucket = RADIX_SIZE - 1 - tid;
sh_above = incl - c;
sh_tied = c;
}
}
barrier();
#else
uint c = 0;
if (tid < RADIX_SIZE) {
c = histo[RADIX_SIZE - 1 - tid];
tie_scan[tid] = c;
}
barrier();
uint src = 0;
[[unroll]] for (uint off = 1; off < RADIX_SIZE; off <<= 1) {
if (tid < RADIX_SIZE) {
const uint v = tie_scan[src + tid];
tie_scan[(RADIX_SIZE - src) + tid] = tid >= off ? v + tie_scan[src + tid - off] : v;
}
src = RADIX_SIZE - src;
barrier();
}
if (tid < RADIX_SIZE) {
const uint incl = tie_scan[src + tid];
if (incl - c < desired && incl >= desired) {
sh_bucket = RADIX_SIZE - 1 - tid;
sh_above = incl - c;
sh_tied = c;
}
}
barrier();
#endif
prefix |= sh_bucket << uint(shift);
desired -= sh_above;
barrier();
}
const uint threshold = prefix;
const bool all_ties = sh_tied == desired;
if (tid == 0) {
out_count = 0;
}
barrier();
if (all_ties) {
for (uint i = tid; i < ncols; i += BLOCK_SIZE) {
if (f2ui(load(row, i, false)) >= threshold) {
data_d[row_out + atomicAdd(out_count, 1u)] = int(i);
}
}
return;
}
// more ties than slots: rank them in invocation order, which does not depend on the scheduling
uint ct = 0;
for (uint i = tid; i < ncols; i += BLOCK_SIZE) {
const uint key = f2ui(load(row, i, false));
if (key > threshold) {
data_d[row_out + atomicAdd(out_count, 1u)] = int(i);
}
ct += key == threshold ? 1u : 0u;
}
#if USE_SUBGROUPS
const uint sg = gl_SubgroupID;
const uint st = subgroupAdd(ct);
if (subgroupElect()) {
sg_tied[sg] = st;
}
barrier();
if (sg == 0) {
const uint lane = gl_SubgroupInvocationID;
uint acc = 0;
for (uint c0 = 0; c0 < NUM_SUBGROUPS; c0 += SUBGROUP_SIZE) {
const uint c = c0 + lane;
const uint t = c < NUM_SUBGROUPS ? sg_tied[c] : 0u;
const uint e = subgroupExclusiveAdd(t);
if (c < NUM_SUBGROUPS) {
sg_tied[c] = acc + e;
}
acc += subgroupAdd(t);
}
}
barrier();
uint pos = p.k - desired + sg_tied[sg] + subgroupExclusiveAdd(ct);
#else
// inclusive Hillis-Steele scan, ping-ponging between the two halves
uint src = 0;
tie_scan[tid] = ct;
barrier();
for (uint off = 1; off < BLOCK_SIZE; off <<= 1) {
const uint v = tie_scan[src + tid];
tie_scan[(BLOCK_SIZE - src) + tid] = tid >= off ? v + tie_scan[src + tid - off] : v;
src = BLOCK_SIZE - src;
barrier();
}
uint pos = p.k - desired + tie_scan[src + tid] - ct;
#endif
const uint pos_end = min(p.k, pos + ct);
for (uint i = tid; i < ncols && pos < pos_end; i += BLOCK_SIZE) {
if (f2ui(load(row, i, false)) == threshold) {
data_d[row_out + pos++] = int(i);
}
}
}
void main() {
for (uint row = gl_WorkGroupID.y; row < p.nrows; row += gl_NumWorkGroups.y) {
topk(row);
}
}