dsa_score: row_part -- score one run of a step's query rows, and rebase cu for the selection after it

With `row_part` p, `score` holds R rows and row i is query row p * R + i; `q`, `q_scale` and
`gate` stay the whole step's, so a run is the unsplit op's rows bit for bit. A row past the last
sequence scores -inf, so every rank's run can be the same length whatever the step's. The optional
output `cu_part` receives cu - p * R, under which qsa_select runs over the run's own rows (pos
offset by p * R) and finds each row's sequence unchanged; qsa_select itself is untouched.

This is what an indexer split over tensor-parallel ranks by query rows needs from the op: the
scoring is the only per-row work in GLM-5.3's indexer that grows with the context.

libr4d offsets the query operands and compares row0 + r against cu; libref and libavx index the
same way. Without `row_part` nothing changes. Tests: the selftest scores each case's rows in equal
runs and holds every run to the unsplit rows' bytes and cu_part to the rebased bounds; ref_test
checks a run, a run past the step and a short query plane; r4d_emu_dsa (r4d_dsa.hip now runs on
the emulator) splits decode rows beside a prefill chunk at 2, 4 and 8 ranks, selects with the
rebased bounds and joins the runs as the all_gather does, byte for byte against the unsplit
scorer and selection, ties and signed gates included.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
NunzeandClaude Fable 5.1 authored and StillDeadcode committed 2026-10-21 15:24:05 +02:00
1 parent 5e8bae8ae6
commit afe5318259
13 files changed
+451 -47

No files matched your search

+21 -7
View File
@@ -67,17 +67,19 @@ int ref_dsa_score(const RadArgs* a, RadStream) {
k.stride[1] = 1;
}
const auto &bt = a->t[5], &cu = a->t[6], &nc = a->t[7], &out = a->t[8];
const RadTensor* cu_part = rad_arg_in(a, 9);
const int64_t n = p_int(a, "n", 0), h = p_int(a, "heads", 0);
if (h > 0 && q.rank == 2 && qs.rank == 2 && qs.shape[1] == 1 &&
q.shape[0] == out.shape[0] * h && qs.shape[0] == q.shape[0]) {
const int64_t part = p_int(a, "row_part", -1);
if (h > 0 && q.rank == 2 && qs.rank == 2 && qs.shape[1] == 1 && gate.rank == 2 &&
q.shape[0] == gate.shape[0] * h && qs.shape[0] == q.shape[0]) {
q.rank = 3;
q.shape[0] = out.shape[0];
q.shape[0] = gate.shape[0];
q.shape[2] = q.shape[1];
q.shape[1] = h;
q.stride[2] = q.stride[1];
q.stride[1] = q.stride[0];
q.stride[0] *= h;
qs.shape[0] = out.shape[0];
qs.shape[0] = gate.shape[0];
qs.shape[1] = h;
qs.stride[1] = qs.stride[0];
qs.stride[0] *= h;
@@ -87,9 +89,14 @@ int ref_dsa_score(const RadArgs* a, RadStream) {
gate.shape[0] != q.shape[0] || qs.shape[1] != h || gate.shape[1] != h || k.rank != 2 ||
k.shape[1] != n || ks.rank != 1 || ks.shape[0] != k.shape[0] || bt.rank != 2 ||
cu.rank != 1 || nc.rank != 1 || cu.shape[0] != bt.shape[0] + 1 ||
nc.shape[0] != bt.shape[0] || out.rank != 2 || out.shape[0] != q.shape[0] ||
nc.shape[0] != bt.shape[0] || out.rank != 2 ||
(part < 0 ? out.shape[0] != q.shape[0]
: out.shape[0] < 1 || q.shape[0] < (part + 1) * out.shape[0]) ||
out.shape[1] > bt.shape[1])
return RAD_E_SHAPE;
if (cu_part && (part < 0 || cu_part->rank != 1 || cu_part->shape[0] != cu.shape[0]))
return part < 0 ? RAD_E_INVAL : RAD_E_SHAPE;
if (cu_part && cu_part->dtype != RAD_I32) return RAD_E_DTYPE;
if (q.dtype != RAD_F8E4M3 || k.dtype != RAD_F8E4M3 || qs.dtype != RAD_F32 ||
ks.dtype != RAD_F32 || gate.dtype != RAD_F32 || bt.dtype != RAD_I32 ||
cu.dtype != RAD_I32 || nc.dtype != RAD_I32 || out.dtype != RAD_F32)
@@ -97,8 +104,15 @@ int ref_dsa_score(const RadArgs* a, RadStream) {
for (int i = 0; i < 9; ++i)
if (!(packed && i == 4) && !a->t[i].data) return RAD_E_INVAL;
const float norm = 1.0f / std::sqrt((float)(h * n));
/* A part scores out's rows from step row part * rows; the query operands span the step. */
const int64_t row0 = part > 0 ? part * out.shape[0] : 0;
if (cu_part)
for (int64_t s = 0; s < cu.shape[0]; ++s)
st_int(RAD_I32, cu_part->data, s * cu_part->stride[0],
ld_int(cu.dtype, cu.data, s * cu.stride[0]) - row0);
#pragma omp parallel for schedule(static)
for (int64_t r = 0; r < q.shape[0]; ++r) {
for (int64_t o = 0; o < out.shape[0]; ++o) {
const int64_t r = row0 + o;
int64_t seq = -1;
for (int64_t s = 0; s < bt.shape[0]; ++s)
if (r >= ld_int(cu.dtype, cu.data, s * cu.stride[0]) &&
@@ -126,7 +140,7 @@ int ref_dsa_score(const RadArgs* a, RadStream) {
}
result *= ldt(&ks, page * ks.stride[0]) * norm;
}
stt(&out, r * out.stride[0] + b * out.stride[1], result);
stt(&out, o * out.stride[0] + b * out.stride[1], result);
}
}
return RAD_OK;