kv tiers: rad_kv_group_replicated -- a group every rank holds whole is kept once a host slot

Under tensor parallelism a host slot held every rank's shard of an entry end to end, so a
group every rank holds whole (an MLA latent cache computed from a replicated projection) was
stored once a rank: on GLM-5.3-Flash at eight ranks 396.75 KiB a 4-token block for 49.59 KiB
of distinct bytes. The plugin now says which paged groups are replicated (additive export,
RAD_ABI_VERSION does not move), and the slot holds those once, in one window at its front
that rank 0 writes and every rank reads back on a restore; the sharded groups follow, one
window a rank, as before. Without the declaration nothing changes.

TierExec::slot_layout is the one place the slot arithmetic lives: bringup takes the windows
from it, and it refuses a group replicated on one rank but zero, unflagged or another width
on another. A follower moves only its own window on a copy out (nothing, when every cached
group is replicated) and both windows on a restore. The startup log names what is held once
and the tokens the tier holds either way.

RADIANCE_DEBUG_TIER_VERIFY now covers the claim: each follower gathers the replicated groups
into a pinned scratch beside the copy and hashes them once landed, and the engine, once every
rank has landed, has the leader compare its window with every follower's sum.

mem_test moves a chain through the real TierExec with a replicated and a sharded group at 1,
2 and 8 ranks, the ranks' copies differing on purpose, and checks every rank gets the leader's
bytes back; beside a zero group; the verify path alike and differing; and the layout's refusal.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
NunzeandClaude Fable 5.1 committed 2026-10-22 09:08:44 +00:00
1 parent 17d004d0d0
commit 66a1d5aa15
15 files changed
+490 -87

No files matched your search

+122 -23
View File
@@ -3930,15 +3930,19 @@ static unsigned char pat(int rank, int64_t layer, int32_t block, int64_t i) {
return (unsigned char)(z ^ (z >> 31));
}
static void fill(Fixture& f, int rank) {
/* `alike`: a replicated group holds rank 0's bytes on every rank, as rad_kv_group_replicated
* promises. Without it the ranks differ there on purpose, so a follower restored from its own
* gather rather than the leader's window would show. */
static void fill(Fixture& f, int rank, bool alike = false) {
for (int32_t g : f.pc.cached_groups()) {
const KVGroupPlan* p = f.kv.plan(g);
if (p->zero) continue;
const int r = alike && p->replicated ? 0 : rank;
const int64_t frag = p->layer_bytes_per_block();
for (int64_t l = 0; l < p->n_layers; ++l)
for (int32_t b = 0; b < (int32_t)p->n_blocks; ++b) {
unsigned char* row = (unsigned char*)p->base + l * p->layer_stride() + b * frag;
for (int64_t i = 0; i < frag; ++i) row[i] = pat(rank, g * 16 + l, b, i);
for (int64_t i = 0; i < frag; ++i) row[i] = pat(r, g * 16 + l, b, i);
}
}
}
@@ -3953,7 +3957,8 @@ static void scribble(Fixture& f) {
}
/* The number of entries whose blocks in `now` hold, in every cached group and layer, exactly what
* their blocks in `was` held before the move. */
* their blocks in `was` held before the move: this rank's own bytes, and for a replicated group
* rank 0's, the one copy the slot holds. */
static int64_t same_bytes(Fixture& f, int rank, const std::vector<std::vector<int32_t>>& was,
const std::vector<std::vector<int32_t>>& now) {
const std::vector<int32_t>& cg = f.pc.cached_groups();
@@ -3963,12 +3968,13 @@ static int64_t same_bytes(Fixture& f, int rank, const std::vector<std::vector<in
for (size_t gi = 0; gi < cg.size() && ok; ++gi) {
const KVGroupPlan* p = f.kv.plan(cg[gi]);
if (p->zero) continue;
const int r = p->replicated ? 0 : rank;
const int64_t frag = p->layer_bytes_per_block();
for (int64_t l = 0; l < p->n_layers && ok; ++l) {
const unsigned char* row =
(const unsigned char*)p->base + l * p->layer_stride() + now[gi][k] * frag;
for (int64_t i = 0; i < frag && ok; ++i)
ok = row[i] == pat(rank, cg[gi] * 16 + l, was[gi][k], i);
ok = row[i] == pat(r, cg[gi] * 16 + l, was[gi][k], i);
}
}
good += ok;
@@ -4005,8 +4011,24 @@ static std::vector<KVGroupInfo> two_paged_groups() {
/* `zero_last`: the last rank keeps no attention cache (rad_kv_group_zero), so its window of a slot
* is the narrower group alone and the windows are unequal; every rank still takes part in every
* job. */
static void run_transfer(int world, bool fragment, bool zero_last = false) {
* job. `replicated`: the attention group is the same on every rank (rad_kv_group_replicated), so
* the slot holds it once at its front, written by rank 0 and read back by all. `verify`: under
* RADIANCE_DEBUG_TIER_VERIFY each follower's gather of the replicated group is compared with the
* leader's window; `alike` fills the replicated group the same on every rank, as the flag
* promises, so the comparison passes, and without it every entry is caught. */
struct TransferCase {
int world = 1;
bool fragment = false;
bool zero_last = false;
bool replicated = false;
bool alike = false;
bool verify = false;
};
static void run_transfer(const TransferCase& tc) {
const int world = tc.world;
const bool fragment = tc.fragment, zero_last = tc.zero_last;
if (tc.verify) setenv("RADIANCE_DEBUG_TIER_VERIFY", "1", 1);
else unsetenv("RADIANCE_DEBUG_TIER_VERIFY");
/* Host backend only: the stand-in kernels copy rows on the CPU, where host-backend "device"
* memory lives; on a card the staging buffer is VRAM and production kernels apply. */
if (!device_is_host()) {
@@ -4023,20 +4045,33 @@ static void run_transfer(int world, bool fragment, bool zero_last = false) {
fx.push_back(std::make_unique<Fixture>());
fx.back()->cfg.checkpoint_slots = 0;
std::vector<KVGroupInfo> groups = two_paged_groups();
if (zero_last && r == world - 1) groups[1].zero = true;
/* The zero group is the one not replicated: attention, or the key cache beside a
* replicated attention group, which leaves that rank's own window empty. */
if (zero_last && r == world - 1) groups[tc.replicated ? 2 : 1].zero = true;
if (tc.replicated) groups[1].replicated = true;
REQUIRE_EQ(fx.back()->build_groups(groups), RAD_OK);
}
Fixture& f = *fx[0];
const std::vector<int32_t>& cg = f.pc.cached_groups();
REQUIRE_EQ(cg.size(), 2u);
/* A slot is the sum of the ranks' entries, each rank's window following the previous ones
* (Engine::configure_kv). */
std::vector<int64_t> win_off((size_t)world, 0);
int64_t slot = 0;
for (int r = 0; r < world; ++r) {
win_off[(size_t)r] = slot;
for (int32_t g : cg) slot += fx[(size_t)r]->kv.plan(g)->bytes_per_block;
}
/* The slot as bringup lays it out: a replicated group once at the front, then each rank's own
* window following the previous ones. Checked against the plain sums here. */
std::vector<const KVManager*> kvs;
for (auto& fp : fx) kvs.push_back(&fp->kv);
TierExec::SlotLayout L;
REQUIRE_EQ(TierExec::slot_layout(kvs, cg, &L), RAD_OK);
const int64_t attn = f.kv.plan(cg[0])->bytes_per_block;
int64_t sum = 0;
for (int r = 0; r < world; ++r)
for (int32_t g : cg) sum += fx[(size_t)r]->kv.plan(g)->bytes_per_block;
CHECK_EQ(L.rep_bytes, tc.replicated ? attn : 0);
CHECK_EQ(L.slot_bytes, tc.replicated ? sum - (world - 1) * attn : sum);
CHECK_EQ(L.own_off[0], L.rep_bytes);
for (int r = 1; r < world; ++r)
CHECK_EQ(L.own_off[(size_t)r], L.own_off[(size_t)r - 1] + L.own_bytes[(size_t)r - 1]);
CHECK_EQ(L.own_off[(size_t)world - 1] + L.own_bytes[(size_t)world - 1], L.slot_bytes);
const std::vector<int64_t>& win_off = L.own_off;
const int64_t slot = L.slot_bytes;
/* Forty slots. Fragmented, every other one is taken and the copy goes out as twenty runs of
* one; whole, as one run of twenty, where a rank's pitch through the shared slots decides where
@@ -4075,7 +4110,7 @@ static void run_transfer(int world, bool fragment, bool zero_last = false) {
REQUIRE_EQ(f.pc.lookup(prompt, {}, &hit).n_attn_tokens, 320);
const std::vector<std::vector<int32_t>> was = hit;
CHECK(was[0][0] != was[1][0]);
for (int r = 0; r < world; ++r) fill(*fx[(size_t)r], r);
for (int r = 0; r < world; ++r) fill(*fx[(size_t)r], r, tc.alike);
/* ---- out ---- */
std::vector<TierJob> jobs;
@@ -4089,6 +4124,22 @@ static void run_transfer(int world, bool fragment, bool zero_last = false) {
usleep(100);
CHECK_EQ(st, 1);
}
/* Every rank landed: the follower's hash of the replicated group against the leader's window
* (Engine::tier_tick's order). Bytes out: a follower writes only its own window. */
if (tc.verify && tc.replicated) {
for (int r = 1; r < world; ++r)
for (const TierJob& j : jobs) {
uint64_t sum = 0;
REQUIRE(x[(size_t)r]->take_replicated_sum(j.key, &sum));
x[0]->verify_replicated(j, r, sum);
CHECK(!x[(size_t)r]->take_replicated_sum(j.key, &sum));
}
CHECK_EQ(x[0]->stats().rep_checked, 20ll * (world - 1));
CHECK_EQ(x[0]->stats().rep_mismatch, tc.alike ? 0ll : 20ll * (world - 1));
}
for (int r = 0; r < world; ++r)
CHECK_EQ(x[(size_t)r]->stats().bytes_out,
20 * (r == 0 ? L.rep_bytes + L.own_bytes[0] : L.own_bytes[(size_t)r]));
x[0]->report_pass(jobs, std::vector<char>(jobs.size(), 1), 0);
CHECK_EQ(t.stats().to_host, 20);
@@ -4135,19 +4186,67 @@ static void run_transfer(int world, bool fragment, bool zero_last = false) {
for (auto& xp : x) xp->close();
for (RadStream s : compute) rad_stream_destroy(s);
f.pc.set_tiers(nullptr);
unsetenv("RADIANCE_DEBUG_TIER_VERIFY");
}
TEST(a_conversation_restored_from_host_is_byte_identical_in_new_blocks) {
run_transfer(1, /*fragment=*/false);
run_transfer(1, /*fragment=*/true);
run_transfer({ 1, /*fragment=*/false });
run_transfer({ 1, /*fragment=*/true });
}
TEST(each_rank_restores_its_own_window_of_a_shared_slot) {
run_transfer(2, /*fragment=*/false);
run_transfer(2, /*fragment=*/true);
run_transfer({ 2, /*fragment=*/false });
run_transfer({ 2, /*fragment=*/true });
}
TEST(a_rank_with_a_zero_group_moves_only_its_own_groups) {
run_transfer(3, /*fragment=*/false, /*zero_last=*/true);
run_transfer(3, /*fragment=*/true, /*zero_last=*/true);
run_transfer({ 3, /*fragment=*/false, /*zero_last=*/true });
run_transfer({ 3, /*fragment=*/true, /*zero_last=*/true });
}
/* The replicated group is in the slot once: the leader writes it, every rank reads it back
* beside its own shard of the other group, and the slot is smaller by (world - 1) copies. At one
* rank nothing changes but the layout's bookkeeping. The ranks' copies differ on purpose, so the
* bytes a follower gets back are provably the leader's. */
TEST(a_replicated_group_is_held_once_a_slot_and_restored_on_every_rank) {
for (int world : { 1, 2, 8 }) {
run_transfer({ world, /*fragment=*/false, false, /*replicated=*/true });
run_transfer({ world, /*fragment=*/true, false, /*replicated=*/true });
}
}
/* A replicated group beside a zero one: the last rank's own window is empty, so it writes nothing
* on a copy out and reads the replicated window alone on a restore. */
TEST(a_replicated_group_beside_a_zero_rank_keeps_the_windows_apart) {
run_transfer({ 3, /*fragment=*/false, /*zero_last=*/true, /*replicated=*/true });
run_transfer({ 3, /*fragment=*/true, /*zero_last=*/true, /*replicated=*/true });
}
/* RADIANCE_DEBUG_TIER_VERIFY: a follower hashes the replicated bytes it would have written and
* the leader compares them with its window. Alike, nothing is reported (seven followers at eight
* ranks); differing, every entry, which the two-rank arm logs twenty times. */
TEST(tier_verify_catches_a_follower_whose_replicated_group_differs) {
run_transfer({ 2, /*fragment=*/false, false, /*replicated=*/true, /*alike=*/true, /*verify=*/true });
run_transfer({ 8, /*fragment=*/true, false, /*replicated=*/true, /*alike=*/true, /*verify=*/true });
run_transfer({ 2, /*fragment=*/true, false, /*replicated=*/true, /*alike=*/false, /*verify=*/true });
}
/* The flag must agree across the ranks: one rank's replicated group that another rank declared
* zero or did not declare replicated has no one window every rank can read, so bringup stops. */
TEST(a_group_replicated_on_one_rank_only_is_refused_by_the_slot_layout) {
if (!device_is_host()) {
std::printf(" SKIP the tier layout fixture needs the host backend\n");
return;
}
for (int bad : { 0, 1 }) {
std::vector<std::unique_ptr<Fixture>> fx;
for (int r = 0; r < 2; ++r) {
fx.push_back(std::make_unique<Fixture>());
fx.back()->cfg.checkpoint_slots = 0;
std::vector<KVGroupInfo> groups = two_paged_groups();
groups[1].replicated = r == 0 || bad == 1;
if (bad == 1 && r == 1) groups[1].zero = true;
REQUIRE_EQ(fx.back()->build_groups(groups), RAD_OK);
}
std::vector<const KVManager*> kvs;
for (auto& fp : fx) kvs.push_back(&fp->kv);
TierExec::SlotLayout L;
CHECK_EQ(TierExec::slot_layout(kvs, fx[0]->pc.cached_groups(), &L), RAD_E_STATE);
}
}
/* With speculation each sequence has two physical state slots while the free list counts logical
@@ -6268,7 +6367,7 @@ TEST(a_tier_transfer_gives_back_every_event_and_device_byte) {
const int64_t events0 = rad_dev_live_events();
int64_t vram0 = 0, vram1 = 0;
CHECK_OK(dev_vram_used(0, &vram0));
run_transfer(2, /*fragment=*/true);
run_transfer({ 2, /*fragment=*/true });
CHECK_EQ(rad_dev_live_events(), events0);
CHECK_OK(dev_vram_used(0, &vram1));
CHECK_EQ(vram1, vram0);