Archived
rad_drafter_shared_kv(b, pos_offset), an additive builder export: the declared SERIAL head reads the trunk's K and V and writes none (Gemma 4's assistant head has a query projection and nothing else per layer). A plugin that does not call it drafts exactly as before; RAD_ABI_VERSION is untouched (same pattern as rad_kv_group_zero). For such a head the serial driver changes in two places: - No history is owed. The history pass carries round 1 only: one row a drafting sequence, at the committed position, with the token the trunk chose after it and the trunk's hidden state there. A sequence that does not draft gets no row; a step with nothing to draft runs no pass. - Every round runs at that same position, over the same context. seqused ends at the committed position: past it, the trunk's cache holds the K and V of drafts the verify rejected, or nothing. `pos_offset` (0 or 1) is where the rows sit for the rotary, relative to the committed position; it moves nothing else. Card side: ADV_HIST and ADV_ROUND take the shape from AdvanceArgs::shared_kv and rot_shift, which the scheduler fills from the Program. Host side: serial_begin/serial_batch stage one row and a constant ceiling. Tests: build_test checks the export's refusals; sched_test stages one verify step both ways (the old path pinned: five history rows, rounds stepping), checks that a cacheless head runs no pass when nothing drafts, and runs the card's advance rows directly for both shapes.
173 lines
7.4 KiB
C++
173 lines
7.4 KiB
C++
/* advance_impl.h -- one row of an advance, shared by the kernel (advance.hip) and the host backend
|
|
* (advance.cpp). See advance.h for what the modes are and why the card computes them.
|
|
*
|
|
* Every row is independent: it reads its own slot's state and writes only its own positions,
|
|
* tokens and slots, so the kernel runs one thread a row and the host a loop.
|
|
*/
|
|
#pragma once
|
|
#include "advance.h"
|
|
|
|
#if defined(__HIPCC__)
|
|
#define RAD_ADV_FN __host__ __device__ inline
|
|
#else
|
|
#define RAD_ADV_FN inline
|
|
#endif
|
|
|
|
namespace rad {
|
|
|
|
RAD_ADV_FN int32_t* adv_i32(const AdvanceArgs& a, int64_t off) {
|
|
return reinterpret_cast<int32_t*>(a.slab + off);
|
|
}
|
|
|
|
RAD_ADV_FN bool adv_is_eog(const AdvanceArgs& a, int32_t t) {
|
|
int32_t lo = 0, hi = a.n_eog;
|
|
while (lo < hi) {
|
|
const int32_t mid = (lo + hi) / 2;
|
|
if (a.eog[mid] < t) lo = mid + 1; else hi = mid;
|
|
}
|
|
return lo < a.n_eog && a.eog[lo] == t;
|
|
}
|
|
|
|
/* Every group's slots for the row's `n` positions, read from the positions just written, and its
|
|
* length from `base`. A position its block table does not reach gets slot -1, which the kernels
|
|
* read as "no slot" -- the builder sized the tables for the furthest position a pass can reach,
|
|
* so it only happens to a row that is being computed for nothing. */
|
|
RAD_ADV_FN void adv_groups(const AdvanceArgs& a, int i, int32_t k, int32_t base, int32_t n) {
|
|
const AdvGroup* G = reinterpret_cast<const AdvGroup*>(a.slab + a.grp_off);
|
|
const int32_t* pos = adv_i32(a, a.pos_off);
|
|
for (int g = 0; g < a.n_groups; ++g) {
|
|
const AdvGroup& e = G[g];
|
|
const int32_t first = adv_i32(a, e.first_off)[i];
|
|
adv_i32(a, e.used_off)[i] = base + n - first;
|
|
if (!e.paged) continue;
|
|
const int32_t* bt = adv_i32(a, e.bt_off) + (int64_t)i * e.pitch;
|
|
int32_t* slot = adv_i32(a, e.slot_off);
|
|
for (int32_t j = 0; j < n; ++j) {
|
|
const int32_t p = pos[k + j];
|
|
const int32_t bi = (p - first) / e.block_size;
|
|
slot[k + j] = (p >= first && bi < e.pitch && bt[bi] >= 0)
|
|
? bt[bi] * e.block_size + p % e.block_size : -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* A row's position, written, with its rotary components moved by the same amount -- only when it
|
|
* moved, so a row the host placed exactly keeps whatever components the host gave it. */
|
|
RAD_ADV_FN void adv_pos(const AdvanceArgs& a, int32_t* pos, int32_t row, int32_t p) {
|
|
const int32_t was = pos[row];
|
|
pos[row] = p;
|
|
if (a.rope_off < 0 || was == p) return;
|
|
int32_t* r = adv_i32(a, a.rope_off);
|
|
const int32_t shift = r[row] - was;
|
|
r[row] = p + shift;
|
|
r[a.rope_pitch + row] = p + shift;
|
|
r[2 * a.rope_pitch + row] = p + shift;
|
|
}
|
|
|
|
RAD_ADV_FN void adv_row(const AdvanceArgs& a, int i) {
|
|
const int32_t* rows = adv_i32(a, a.row_off);
|
|
const int32_t rtok = rows[a.row_pitch + i];
|
|
const int32_t rout = rows[2 * a.row_pitch + i];
|
|
const int32_t aux = rows[3 * a.row_pitch + i];
|
|
/* Acceptance runs over every row that sampled, whoever staged it; every other mode over the
|
|
* rows the card owns. */
|
|
if (a.mode == ADV_ACCEPT ? rout < 0 : !rows[i]) return;
|
|
|
|
int32_t* tok = adv_i32(a, a.tok_off);
|
|
int32_t* pos = adv_i32(a, a.pos_off);
|
|
int32_t* ctx = adv_i32(a, a.ctx_off);
|
|
const int32_t slot = adv_i32(a, a.sid_off)[i];
|
|
const int32_t k = adv_i32(a, a.cu_off)[i];
|
|
const int32_t n = adv_i32(a, a.q_off)[i];
|
|
StepSlot& S = a.st[slot];
|
|
|
|
switch (a.mode) {
|
|
case ADV_ACCEPT: {
|
|
/* THE RULE accept_greedy states, and then the end-of-generation cut collect_step applies:
|
|
* a draft is accepted while it equals the target's own pick at its position, and the run
|
|
* stops AT an end-of-generation token rather than after it. */
|
|
const int32_t* y = a.src + rout;
|
|
const int32_t* d = tok + rtok + 1;
|
|
int32_t acc = 0;
|
|
while (acc < aux && d[acc] == y[acc]) ++acc;
|
|
for (int32_t j = 0; j <= acc; ++j)
|
|
if (adv_is_eog(a, y[j])) { acc = j; break; }
|
|
const int32_t head = ctx[i] + (rtok - k) + acc;
|
|
S.acc = acc;
|
|
S.head = head;
|
|
S.ctx = head + 1;
|
|
S.n_acc = acc + 1;
|
|
S.tok[0] = y[acc];
|
|
return;
|
|
}
|
|
case ADV_TRUNK: {
|
|
const int32_t base = S.ctx;
|
|
for (int32_t j = 0; j < n; ++j) { tok[k + j] = S.tok[j]; adv_pos(a, pos, k + j, base + j); }
|
|
ctx[i] = base;
|
|
adv_i32(a, a.acc_off)[i] = S.n_acc;
|
|
adv_groups(a, i, k, base, n);
|
|
return;
|
|
}
|
|
case ADV_HIST: {
|
|
if (a.shared_kv) {
|
|
/* A HEAD THAT READS THE TRUNK'S CACHE AND KEEPS NONE (rad_drafter_shared_kv). There is
|
|
* no history to fill, so the pass is round 1 alone and its one row is the last one of
|
|
* the run below: the committed position, the trunk's row there, the token the trunk
|
|
* chose after it. Its context is the committed positions and NOT the verify step's
|
|
* whole run -- the cache past the committed position holds the K and V of the drafts
|
|
* this verify rejected. The host staged the run's first trunk row; the card adds the
|
|
* drafts it accepted. */
|
|
int32_t* out = adv_i32(a, a.out_off);
|
|
tok[k] = a.src[rout + S.acc];
|
|
adv_pos(a, pos, k, S.head + a.rot_shift);
|
|
out[k] += S.acc;
|
|
ctx[i] = S.head;
|
|
adv_groups(a, i, k, S.head, 1);
|
|
return;
|
|
}
|
|
/* `aux` positions the host already knew the tokens of, then the verify step's own
|
|
* positions up to the last one it committed, each with the token committed after it --
|
|
* the sampler's pick there, since the drafts before it agreed with it. The pass has a
|
|
* row for every position the step COULD have committed, and the ones past the last
|
|
* committed repeat it: a position past it would take a rejected draft's hidden state
|
|
* into the head's cache. The repeat is also what makes the run's LAST row draft round
|
|
* 1's -- the committed position, its hidden state and the token after it -- whatever
|
|
* the acceptance was (RadBatch::draft_out_ids). */
|
|
const int32_t base = S.head - S.acc - aux;
|
|
const int32_t last = aux + S.acc;
|
|
int32_t* out = adv_i32(a, a.out_off);
|
|
const int32_t row0 = out[k];
|
|
for (int32_t j = 0; j < n; ++j) {
|
|
const int32_t jj = j < last ? j : last;
|
|
if (jj >= aux) tok[k + j] = a.src[rout + jj - aux];
|
|
adv_pos(a, pos, k + j, base + jj);
|
|
out[k + j] = row0 + jj;
|
|
}
|
|
ctx[i] = base;
|
|
adv_groups(a, i, k, base, n);
|
|
return;
|
|
}
|
|
case ADV_ROUND: {
|
|
/* Round 2 onward: round 1 is the history pass's last row (ADV_HIST above), so every
|
|
* round embeds the proposal of the round before it. A head with no cache of its own does
|
|
* not step forward: it stays at the committed position over the committed context, and
|
|
* what it proposed reaches the next round through its own hidden state. */
|
|
const int32_t base = a.shared_kv ? S.head : S.head + a.round - 1;
|
|
const int32_t t = a.src[(int64_t)i * a.src_pitch];
|
|
S.tok[a.round - 1] = t;
|
|
tok[k] = t;
|
|
adv_pos(a, pos, k, a.shared_kv ? base + a.rot_shift : base);
|
|
ctx[i] = base;
|
|
adv_groups(a, i, k, base, 1);
|
|
return;
|
|
}
|
|
case ADV_TAKE:
|
|
S.tok[a.round] = a.src[(int64_t)i * a.src_pitch];
|
|
return;
|
|
default:
|
|
return;
|
|
}
|
|
}
|
|
|
|
} /* namespace rad */
|