diff --git a/abi/rad_builder.h b/abi/rad_builder.h index e3a3a477..272987a3 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -451,6 +451,16 @@ int rad_weight_shard_span_parts(RadBuilder* b, rad_weight w, const int64_t* lo, * at issue. Additive export. */ int rad_kv_group_zero(RadBuilder* b, rad_kvgroup g); +/* Every rank holds the same bytes for this paged group, so a tier stores it once. Under tensor + * parallelism a host slot for a cached entry is otherwise every rank's shard end to end; a group + * declared replicated takes one window at the front of the slot, written by rank 0 and read by + * every rank on a restore. The plugin asserts the replication (an MLA latent cache that every + * rank computes whole, an indexer's key cache every rank pools into); the engine moves bytes and + * cannot know. RADIANCE_DEBUG_TIER_VERIFY checks the claim: each follower hashes what it would + * have written and the leader compares. Paged groups only, and never a group this rank declared + * zero. No effect at one rank. Additive export; RAD_ABI_VERSION does not move. */ +int rad_kv_group_replicated(RadBuilder* b, rad_kvgroup g); + #define RAD_BUFS(...) ((const rad_buf[]){ __VA_ARGS__ }), \ (int)(sizeof((const rad_buf[]){ __VA_ARGS__ }) / sizeof(rad_buf)) diff --git a/core/build/rad_build.h b/core/build/rad_build.h index 3b008686..23ea6682 100644 --- a/core/build/rad_build.h +++ b/core/build/rad_build.h @@ -144,6 +144,7 @@ public: int weight_shard_span_parts(rad_weight w, const int64_t* lo, const int64_t* hi, const int64_t* total, int n); int kv_group_zero(rad_kvgroup g); + int kv_group_replicated(rad_kvgroup g); /* Finalise: weight first/last use, KV group geometry, buffer liveness and packing. Returns * RAD_OK, or the first structural error class. Resolution misses are not errors here: an diff --git a/core/build/rad_builder.cpp b/core/build/rad_builder.cpp index 3ff32f5e..5de8deb5 100644 --- a/core/build/rad_builder.cpp +++ b/core/build/rad_builder.cpp @@ -928,10 +928,38 @@ int Builder::kv_group_zero(rad_kvgroup h) { prog_.kv_groups[h].name.c_str()); return RAD_E_INVAL; } + if (prog_.kv_groups[h].replicated) { + err(RAD_E_INVAL, "kv group '%s': kv_group_zero on a group declared replicated; a " + "replicated group is held whole on every rank", prog_.kv_groups[h].name.c_str()); + return RAD_E_INVAL; + } prog_.kv_groups[h].zero = true; return RAD_OK; } +int Builder::kv_group_replicated(rad_kvgroup h) { + if (finished_) { + err(RAD_E_STATE, "kv_group_replicated after declare finished"); + return RAD_E_STATE; + } + if (h == 0 || h >= prog_.kv_groups.size()) return RAD_E_INVAL; + /* Paged groups only: the tiers move blocks. A linear or conv group's state travels in the + * checkpoint store, which has no such window. */ + const int kind = prog_.kv_groups[h].decl.kind; + if (kind != RAD_KV_FULL && kind != RAD_KV_WINDOW) { + err(RAD_E_INVAL, "kv group '%s': kv_group_replicated on a group that is not paged", + prog_.kv_groups[h].name.c_str()); + return RAD_E_INVAL; + } + if (prog_.kv_groups[h].zero) { + err(RAD_E_INVAL, "kv group '%s': kv_group_replicated on a group declared zero; a zero " + "rank holds none of it", prog_.kv_groups[h].name.c_str()); + return RAD_E_INVAL; + } + prog_.kv_groups[h].replicated = true; + return RAD_OK; +} + /* ================================================================== name map */ int Builder::decl_name_map(const RadNameMap* m) { if (finished_) { err(RAD_E_STATE, "decl_name_map after declare finished"); return RAD_E_STATE; } @@ -2319,6 +2347,11 @@ int rad_kv_group_zero(RadBuilder* b, rad_kvgroup g) { return B(b)->kv_group_zero(g); } +int rad_kv_group_replicated(RadBuilder* b, rad_kvgroup g) { + if (!b) return RAD_E_INVAL; + return B(b)->kv_group_replicated(g); +} + int rad_decl_name_map(RadBuilder* b, const RadNameMap* m) { if (!b) return RAD_E_INVAL; return B(b)->decl_name_map(m); diff --git a/core/engine.cpp b/core/engine.cpp index 8569750f..2921c5cf 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -1246,6 +1246,18 @@ void Engine::tier_tick() { } if (ranks_.size() > 1) (void)rad_dev_set(lead.device); if (all) { + /* RADIANCE_DEBUG_TIER_VERIFY: now that every rank's window has landed, each + * follower's hash of the replicated groups it gathered is held against the window + * the leader wrote (rad_kv_group_replicated's claim). The sums are taken either way, + * so a failed batch leaves none behind. */ + if (lead.tierx.verifying_replicated()) { + for (size_t i = 1; i < ranks_.size(); ++i) + for (const TierJob& j : P.copy) { + uint64_t sum = 0; + if (ranks_[i]->tierx.take_replicated_sum(j.key, &sum) && good) + (void)lead.tierx.verify_replicated(j, (int)i, sum); + } + } /* A copy is good only if every rank's shard landed; otherwise the host copy is * partly stale, so the whole batch fails and every slot goes back. */ const std::vector ok(P.copy.size(), good ? 1 : 0); diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index caf81846..9b60aaee 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -3050,21 +3050,17 @@ int Engine::configure_kv() { { const bool tier_asked = cfg_.prefix_cache_mib > 0; const std::vector& cg = ranks_[0]->prefix.cached_groups(); - /* One slot holds every rank's shard of an entry; each rank writes only its window. Shards - * can differ or be empty (uneven heads, rad_kv_group_zero), so a slot is the sum and each - * window starts where the previous ends. TierExec::sizes uses the largest rank's entry, - * since the staging ring is per rank. */ - std::vector rank_entry(ranks_.size(), 0), entry_off(ranks_.size(), 0); - int64_t entry_bytes = 0, slot_bytes = 0; - for (size_t i = 0; i < ranks_.size(); ++i) { - for (int32_t g : cg) { - const KVGroupPlan* p = ranks_[i]->kv.plan(g); - if (p && p->paged()) rank_entry[i] += p->bytes_per_block; - } - entry_off[i] = slot_bytes; - slot_bytes += rank_entry[i]; - entry_bytes = std::max(entry_bytes, rank_entry[i]); - } + /* One slot holds the whole engine's entry: the groups every rank holds alike + * (rad_kv_group_replicated) once, at the front, then every rank's own shard in its own + * window. Shards can differ or be empty (uneven heads, rad_kv_group_zero). The layout is + * TierExec's, so the arithmetic here and the copies agree; TierExec::sizes takes the + * largest rank's entry, since the staging ring is per rank. */ + std::vector rank_kv; + for (auto& r : ranks_) rank_kv.push_back(&r->kv); + TierExec::SlotLayout layout; + RAD_TRY(TierExec::slot_layout(rank_kv, cg, &layout)); + const std::vector& entry_off = layout.own_off; + const int64_t entry_bytes = layout.entry_bytes, slot_bytes = layout.slot_bytes; /* Snapshots live in the checkpoint store (--checkpoint-slots); --prefix-cache-mib is for * blocks only. */ @@ -3092,6 +3088,20 @@ int Engine::configure_kv() { * fit is right after the turn. */ const int64_t held = T.arena().capacity() * tc.block_tokens; const int64_t ctx = cfg_.max_ctx > 0 ? cfg_.max_ctx : meta_.n_ctx_train; + if (layout.rep_bytes > 0 && ranks_.size() > 1) { + /* What the slot would be with every rank's copy of the replicated groups. */ + const int64_t each = slot_bytes + layout.rep_bytes * (int64_t)(ranks_.size() - 1); + std::string names; + for (const std::string& n : layout.replicated) + names += (names.empty() ? "" : ", ") + n; + RAD_INFO("kv tiers: %s (%s of each entry) is the same on every rank and held " + "once a slot: %s a slot rather than %s, so --prefix-cache-mib %lld " + "holds %lld tokens rather than %lld", + names.c_str(), humanb(layout.rep_bytes).c_str(), + humanb(slot_bytes).c_str(), humanb(each).c_str(), + (long long)cfg_.prefix_cache_mib, (long long)held, + (long long)(tc.host_cap_bytes / each * tc.block_tokens)); + } if (held < ctx) RAD_WARN("kv tiers: --prefix-cache-mib %lld holds %lld tokens of context (%s " "per %lld-token block), less than one --max-model-len %lld " diff --git a/core/mem/kv.cpp b/core/mem/kv.cpp index 2d2eb0cb..d6193736 100644 --- a/core/mem/kv.cpp +++ b/core/mem/kv.cpp @@ -228,6 +228,7 @@ int KVManager::plan_groups(const std::vector& groups, const Config& p.bytes_per_block = 0; p.bytes_per_state = 0; } + p.replicated = g.replicated && p.paged() && !g.zero; plans_.push_back(std::move(p)); } bundling_ = compare_bundling(bundles); diff --git a/core/mem/kv.h b/core/mem/kv.h index a29e8c64..046edb8b 100644 --- a/core/mem/kv.h +++ b/core/mem/kv.h @@ -73,6 +73,10 @@ struct KVGroupPlan { * block count (one block table serves every rank); its costs are zero and nothing is carved. * No op on this rank names it (issue refuses a group with no pool bound). */ bool zero = false; + /* rad_kv_group_replicated: every rank holds the same bytes, so a tier slot carries this + * group once, in a window at its front that rank 0 writes and every rank reads + * (TierExec::slot_layout). The pool is carved as for any group. */ + bool replicated = false; int64_t n_states = 0; /* LINEAR/CONV: n_seq_states * state_copies */ /* Physical slots per logical state: 1, or 2 under speculation. A verify runs 1 + n_spec * tokens through the recurrence before acceptance is known; libr4d keeps the committed state diff --git a/core/mem/kvtier.h b/core/mem/kvtier.h index 585eef76..20eccac2 100644 --- a/core/mem/kvtier.h +++ b/core/mem/kvtier.h @@ -96,7 +96,9 @@ class IdleTiers { public: struct Config { int64_t host_cap_bytes = 0; /* 0 is off */ - int64_t entry_bytes = 0; /* one entry: sum over cached groups of bytes_per_block */ + /* One host slot: every cached group's bytes_per_block, under tensor parallelism every + * rank's own shard end to end and a replicated group once (TierExec::SlotLayout). */ + int64_t entry_bytes = 0; /* ---- the linear half ---- * `ckpt_bytes` is one snapshot including every rank's shard, like `entry_bytes`. The * snapshots live in the engine's checkpoint store, `ckpt_slots` deep; reported here, not diff --git a/core/rad_core.h b/core/rad_core.h index 62329504..6951541f 100644 --- a/core/rad_core.h +++ b/core/rad_core.h @@ -287,6 +287,7 @@ struct KVGroupInfo { int64_t bytes_per_state = 0; /* linear/conv: per sequence */ std::vector layers; bool zero = false; /* rad_kv_group_zero: declared for position, nothing carved */ + bool replicated = false; /* rad_kv_group_replicated: the same bytes on every rank */ /* Bound only at meta.n_layers and above: the drafter's own cache, which no trunk layer reads. * Set at declare (Builder::finish_kv_groups). */ bool drafter = false; diff --git a/core/runtime/tierexec.cpp b/core/runtime/tierexec.cpp index b4d4c90a..76f005b3 100644 --- a/core/runtime/tierexec.cpp +++ b/core/runtime/tierexec.cpp @@ -28,7 +28,6 @@ int TierExec::configure(Registry* reg, KVManager* kv, IdleTiers* tiers, shared_ = shared; RAD_TRY(rad_dev_set(device_)); - int64_t off = 0; for (int32_t g : groups) { const KVGroupPlan* p = kv->plan(g); if (!p || !p->paged()) continue; @@ -39,7 +38,7 @@ int TierExec::configure(Registry* reg, KVManager* kv, IdleTiers* tiers, gi.layer_stride = p->layer_stride(); gi.n_blocks = p->n_blocks; gi.base = p->base; - gi.off = off; + gi.replicated = p->replicated; if (p->zero) { gi.zero = true; gi_.push_back(gi); @@ -60,10 +59,18 @@ int TierExec::configure(Registry* reg, KVManager* kv, IdleTiers* tiers, close(); return RAD_E_UNSUPPORTED; } - off += gi.n_layers * gi.frag; gi_.push_back(gi); } if (gi_.empty()) return RAD_E_UNSUPPORTED; + /* The staging entry is the replicated groups first, then this rank's own, each in cached-group + * order, so the two windows of the slot are each one contiguous run of the entry. */ + int64_t off = 0; + for (GroupInfo& gi : gi_) + if (gi.replicated && !gi.zero) { gi.off = off; off += gi.n_layers * gi.frag; } + rep_bytes_ = off; + for (GroupInfo& gi : gi_) + if (!gi.replicated && !gi.zero) { gi.off = off; off += gi.n_layers * gi.frag; } + own_bytes_ = off - rep_bytes_; /* Every cached group is zero on this rank, so its entry would be empty. One rank that cannot * take part turns the tiers off for all. */ if (off == 0) { @@ -75,18 +82,76 @@ int TierExec::configure(Registry* reg, KVManager* kv, IdleTiers* tiers, entry_bytes_ = off; /* Nothing is built on the device here; see device_init(). */ - verify_ = leader && std::getenv("RADIANCE_DEBUG_TIER_VERIFY") != nullptr; + verify_ = std::getenv("RADIANCE_DEBUG_TIER_VERIFY") != nullptr; + verify_rep_ = verify_ && !leader_ && rep_bytes_ > 0; - char buf[320]; + char buf[400]; std::snprintf(buf, sizeof buf, - "kv tier transfer: %zu paged group(s), %s an entry, batches of %d through " + "kv tier transfer: %zu paged group(s), %s an entry%s%s%s, batches of %d through " "gather_rows/scatter_rows at u8", - gi_.size(), humanb(entry_bytes_).c_str(), max_batch_); + gi_.size(), humanb(entry_bytes_).c_str(), + rep_bytes_ > 0 ? " (" : "", + rep_bytes_ > 0 ? humanb(rep_bytes_).c_str() : "", + rep_bytes_ > 0 ? " of it replicated on every rank and held once a slot)" : "", + max_batch_); report_ = buf; enabled_ = true; return RAD_OK; } +int TierExec::slot_layout(const std::vector& ranks, + const std::vector& groups, SlotLayout* out) { + if (!out || ranks.empty()) return RAD_E_INVAL; + for (const KVManager* kv : ranks) if (!kv) return RAD_E_INVAL; + SlotLayout L; + L.own_off.assign(ranks.size(), 0); + L.own_bytes.assign(ranks.size(), 0); + for (int32_t g : groups) { + const KVGroupPlan* p0 = ranks[0]->plan(g); + if (!p0 || !p0->paged()) continue; + bool replicated = false; + for (size_t i = 0; i < ranks.size(); ++i) { + const KVGroupPlan* p = ranks[i]->plan(g); + replicated = replicated || (p && p->replicated); + } + if (!replicated) { + for (size_t i = 0; i < ranks.size(); ++i) { + const KVGroupPlan* p = ranks[i]->plan(g); + if (p && p->paged()) L.own_bytes[i] += p->bytes_per_block; + } + continue; + } + /* Every rank reads the one window back into its own pool, so the group must be the same + * width everywhere, declared replicated everywhere and carved everywhere. */ + for (size_t i = 0; i < ranks.size(); ++i) { + const KVGroupPlan* p = ranks[i]->plan(g); + const char* why = !p || !p->paged() ? "does not hold it as a paged group" + : p->zero ? "declares it zero (rad_kv_group_zero)" + : !p->replicated ? "does not declare it replicated" + : p->bytes_per_block != p0->bytes_per_block + ? "holds it at another width" + : nullptr; + if (!why) continue; + RAD_ERR("kv tiers: KV group '%s' is declared replicated (rad_kv_group_replicated) on " + "one rank, but rank %zu %s. A replicated group is held whole and alike on " + "every rank, since every rank restores it from the one copy a slot holds.", + p0->name.c_str(), i, why); + return RAD_E_STATE; + } + L.rep_bytes += p0->bytes_per_block; + L.replicated.push_back(p0->name); + } + int64_t slot = L.rep_bytes; + for (size_t i = 0; i < ranks.size(); ++i) { + L.own_off[i] = slot; + slot += L.own_bytes[i]; + L.entry_bytes = std::max(L.entry_bytes, L.rep_bytes + L.own_bytes[i]); + } + L.slot_bytes = slot; + *out = std::move(L); + return RAD_OK; +} + TierExec::Sizes TierExec::sizes(int64_t entry_bytes, int64_t max_ctx, int64_t block_size) { Sizes s; if (entry_bytes <= 0) return s; @@ -118,6 +183,10 @@ void TierExec::close() { order_.clear(); verify_pending_.clear(); entry_bytes_ = 0; + rep_bytes_ = own_bytes_ = 0; + verify_ = verify_rep_ = false; + out_sum_.clear(); + rep_sum_.clear(); enabled_ = false; leader_ = false; reg_ = nullptr; kv_ = nullptr; tiers_ = nullptr; @@ -128,6 +197,7 @@ void TierExec::release_device() { if (stage_) { rad_dev_free(stage_, RAD_MEM_DEVICE); stage_ = nullptr; } if (idx_dev_) { rad_dev_free(idx_dev_, RAD_MEM_DEVICE); idx_dev_ = nullptr; } if (idx_host_) { rad_dev_free(idx_host_, RAD_MEM_HOST_PINNED); idx_host_ = nullptr; } + if (vrep_) { rad_dev_free(vrep_, RAD_MEM_HOST_PINNED); vrep_ = nullptr; } for (int i = 0; i < kRing; ++i) { if (ring_ev_[i]) rad_event_destroy(ring_ev_[i]); ring_ev_[i] = nullptr; @@ -156,7 +226,11 @@ int TierExec::device_init() { bool ev = rad_event_create(&ev_compute_) == RAD_OK && rad_event_create(&ev_copy_) == RAD_OK && rad_event_create(&ev_restore_) == RAD_OK; for (int i = 0; i < kRing && ev; ++i) ev = rad_event_create(&ring_ev_[i]) == RAD_OK; - if (!stage_ || !idx_dev_ || !idx_host_ || !ev) { release_device(); return RAD_E_NOMEM; } + if (verify_rep_) vrep_ = (char*)rad_dev_alloc(copy_idx_ * rep_bytes_, RAD_MEM_HOST_PINNED); + if (!stage_ || !idx_dev_ || !idx_host_ || !ev || (verify_rep_ && !vrep_)) { + release_device(); + return RAD_E_NOMEM; + } /* Borrow the mover's maintenance stream if there is one; both move bytes nobody waits for, * and a fourth hardware queue costs about 13 ms per decode step. Never the step's own * stream, or a copy out would become a dependency of the next token. */ @@ -212,29 +286,62 @@ int TierExec::run_rows(const Resolved& r, bool scatter, const GroupInfo& gi, int /* ------------------------------------------------------------------ RADIANCE_DEBUG_TIER_VERIFY */ -static uint64_t tier_fnv(const void* p, int64_t n) { - uint64_t h = 1469598103934665603ull; +/* FNV-1a, continued from `h` so one sum can run over the two windows of a slot. */ +static uint64_t tier_fnv_from(uint64_t h, const void* p, int64_t n) { const unsigned char* b = (const unsigned char*)p; for (int64_t i = 0; i < n; ++i) { h ^= b[i]; h *= 1099511628211ull; } return h; } - -void TierExec::verify_note(const BlockHash& key, const void* p, int64_t n) { - if (!verify_ || !leader_ || !p || n <= 0) return; - out_sum_[key] = tier_fnv(p, n); +static uint64_t tier_fnv(const void* p, int64_t n) { + return tier_fnv_from(1469598103934665603ull, p, n); } -void TierExec::verify_check(const BlockHash& key, const void* p, int64_t n) { - if (!verify_ || !leader_ || !p || n <= 0) return; +/* One sum over the entry as the slot holds it: the replicated window, then this rank's own. */ +uint64_t TierExec::entry_sum(const char* slot) const { + const uint64_t h = tier_fnv(slot, rep_bytes_); + return tier_fnv_from(h, slot + slot_off_, own_bytes_); +} + +void TierExec::verify_note(const BlockHash& key, const char* slot) { + if (!verify_ || !leader_ || !slot) return; + out_sum_[key] = entry_sum(slot); +} + +void TierExec::verify_check(const BlockHash& key, const char* slot) { + if (!verify_ || !leader_ || !slot) return; auto it = out_sum_.find(key); if (it == out_sum_.end()) return; /* never went out through this process */ - const uint64_t back = tier_fnv(p, n); + const uint64_t back = entry_sum(slot); if (back == it->second) return; RAD_ERR("kv tiers: the attention blocks for %s came back different (%016llx out, %016llx in); " "the tier moved the wrong bytes", key.str().c_str(), (unsigned long long)it->second, (unsigned long long)back); } +bool TierExec::take_replicated_sum(const BlockHash& key, uint64_t* sum) { + auto it = rep_sum_.find(key); + if (it == rep_sum_.end()) return false; + if (sum) *sum = it->second; + rep_sum_.erase(it); + return true; +} + +bool TierExec::verify_replicated(const TierJob& j, int rank, uint64_t follower_sum) { + if (!verify_ || !leader_ || rep_bytes_ <= 0 || !tiers_ || j.host_slot < 0) return false; + const char* slot = (const char*)tiers_->arena().at(j.host_slot); + if (!slot) return false; + ++st_.rep_checked; + const uint64_t mine = tier_fnv(slot, rep_bytes_); + if (mine == follower_sum) return true; + ++st_.rep_mismatch; + RAD_ERR("kv tiers: the replicated groups of %s differ between rank 0 (%016llx) and rank %d " + "(%016llx). The plugin declared them replicated (rad_kv_group_replicated) and rank %d " + "holds other bytes; its restore would read rank 0's.", + j.key.str().c_str(), (unsigned long long)mine, rank, (unsigned long long)follower_sum, + rank); + return false; +} + /* ------------------------------------------------------------------ helpers */ /* Like RAD_TRY, but the first failure is logged with the call it came from. A failing tier is @@ -276,11 +383,28 @@ int TierExec::copy_runs(const std::vector& order, size_t first, int64_ while (k + run < n && order[first + (size_t)(k + run)]->host_slot == s0 + run) ++run; char* host = (char*)tiers_->arena().at(s0); if (!host) return RAD_E_STATE; - host += slot_off_; char* dev = (char*)stage_ + k * entry_bytes_; - const int st = out - ? rad_memcpy_2d_async(host, slot_bytes_, dev, entry_bytes_, entry_bytes_, run, stream_) - : rad_memcpy_2d_async(dev, entry_bytes_, host, slot_bytes_, entry_bytes_, run, stream_); + int st = RAD_OK; + if (out) { + /* The replicated window is the leader's to write; a verifying follower keeps its own + * gather beside the copy, indexed by the job's place in the pass. */ + if (rep_bytes_ > 0 && leader_) + st = rad_memcpy_2d_async(host, slot_bytes_, dev, entry_bytes_, rep_bytes_, run, + stream_); + if (st == RAD_OK && verify_rep_ && vrep_) + st = rad_memcpy_2d_async(vrep_ + (int64_t)(first + (size_t)k) * rep_bytes_, + rep_bytes_, dev, entry_bytes_, rep_bytes_, run, stream_); + if (st == RAD_OK && own_bytes_ > 0) + st = rad_memcpy_2d_async(host + slot_off_, slot_bytes_, dev + rep_bytes_, + entry_bytes_, own_bytes_, run, stream_); + } else { + if (rep_bytes_ > 0) + st = rad_memcpy_2d_async(dev, entry_bytes_, host, slot_bytes_, rep_bytes_, run, + stream_); + if (st == RAD_OK && own_bytes_ > 0) + st = rad_memcpy_2d_async(dev + rep_bytes_, entry_bytes_, host + slot_off_, + slot_bytes_, own_bytes_, run, stream_); + } if (st != RAD_OK) return st; k += run; } @@ -326,22 +450,27 @@ int TierExec::enqueue_copy(std::vector& jobs, RadStream compute) { for (int64_t s = 0; s < n; s += max_batch_) { const int64_t c = std::min(max_batch_, n - s); for (size_t gidx = 0; gidx < gi_.size(); ++gidx) - for (int64_t l = 0; l < gi_[gidx].n_layers && !gi_[gidx].zero; ++l) + for (int64_t l = 0; l < gi_[gidx].n_layers && gathers(gi_[gidx]); ++l) TIER_TRY(run_rows(gi_[gidx].gather, /*scatter=*/false, gi_[gidx], l, stage_, idx_dev_ + (int64_t)gidx * n + s, c), "the gather out of the pool"); TIER_TRY(copy_runs(order_, (size_t)s, c, /*out=*/true), "the copy to host memory"); } - st_.bytes_out += n * entry_bytes_; + st_.bytes_out += n * (leader_ ? entry_bytes_ : own_bytes_); } TIER_TRY(rad_event_record(ev_copy_, stream_), "marking the copy"); copy_out_ = true; ++st_.passes; st_.jobs += (int64_t)jobs.size(); - if (verify_) - for (const TierJob& j : jobs) - verify_pending_.push_back({j.key, j.blocks.empty() ? -1 : j.host_slot}); + if (verify_) { + int64_t ord = 0; + for (const TierJob& j : jobs) { + const bool moved = !j.blocks.empty(); + verify_pending_.push_back({j.key, moved ? j.host_slot : -1, moved ? ord : -1}); + if (moved) ++ord; + } + } return RAD_OK; } @@ -353,9 +482,11 @@ int TierExec::copy_state() { copy_out_ = false; if (q > 0 && verify_) { for (const VerifyNote& v : verify_pending_) { - if (v.host_slot >= 0) - verify_note(v.key, (char*)tiers_->arena().at(v.host_slot) + slot_off_, - entry_bytes_); + if (v.host_slot < 0) continue; + if (leader_) + verify_note(v.key, (const char*)tiers_->arena().at(v.host_slot)); + else if (verify_rep_ && vrep_ && v.ord >= 0) + rep_sum_[v.key] = tier_fnv(vrep_ + v.ord * rep_bytes_, rep_bytes_); } } verify_pending_.clear(); @@ -432,8 +563,7 @@ int TierExec::issue_restore(std::vector& jobs, RadStream compute, order_.clear(); for (size_t i = 0; i < m; ++i) order_.push_back(&jobs[i]); for (size_t i = 0; i < m; ++i) - verify_check(jobs[i].key, (char*)tiers_->arena().at(jobs[i].host_slot) + slot_off_, - entry_bytes_); + verify_check(jobs[i].key, (const char*)tiers_->arena().at(jobs[i].host_slot)); /* From here a failure leaves blocks partly overwritten. Mark jobs started first, so report() * drops a failed started job instead of trusting its host copy. The compute stream waits on diff --git a/core/runtime/tierexec.h b/core/runtime/tierexec.h index b070cb80..40c2def2 100644 --- a/core/runtime/tierexec.h +++ b/core/runtime/tierexec.h @@ -20,9 +20,15 @@ * per staging batch. * * Staging layout. Device staging is `max_batch * entry_bytes`; one entry is [group][layer][fragment] - * end to end. For a given (group, layer) the destination rows sit at stride `entry_bytes` from a - * base in the first entry, which is `gather_rows`'s `y` pitch, so one launch writes every entry's - * fragment in place and the host side is one strided copy per run of consecutive arena slots. + * end to end, the replicated groups first. For a given (group, layer) the destination rows sit at + * stride `entry_bytes` from a base in the first entry, which is `gather_rows`'s `y` pitch, so one + * launch writes every entry's fragment in place and the host side is one strided copy per window + * per run of consecutive arena slots. + * + * The host slot. Under tensor parallelism a slot holds the whole engine's entry. A group every + * rank holds whole (rad_kv_group_replicated) is in the slot once, in one window at its front + * that rank 0 writes and every rank reads back; the groups each rank holds a shard of follow, + * one window per rank. slot_layout() is the one place the arithmetic lives. */ #pragma once #include "mem/kv.h" @@ -48,6 +54,23 @@ public: const std::vector& groups, int max_batch, int64_t max_copy, int64_t max_chain, bool leader, int device, int64_t slot_off, RadStream shared); + + /* One shared host slot across the ranks. `[0, rep_bytes)` is the replicated window, the + * replicated cached groups in cached-group order; rank r's own groups follow at + * `own_off[r]`, `own_bytes[r]` wide (uneven heads and rad_kv_group_zero make these differ). + * `entry_bytes` is the largest rank's staging entry, rep_bytes + own_bytes, which sizes() + * takes. Checked here: a group replicated on one rank is replicated, not zero and the same + * width on every rank, since every rank reads the one window back; a violation stops bringup + * with the group named (RAD_E_STATE). */ + struct SlotLayout { + int64_t slot_bytes = 0; + int64_t rep_bytes = 0; + int64_t entry_bytes = 0; + std::vector own_off, own_bytes; + std::vector replicated; /* the replicated groups' names, for the report */ + }; + static int slot_layout(const std::vector& ranks, + const std::vector& groups, SlotLayout* out); void close(); bool enabled() const { return enabled_; } std::string report() const { return report_; } @@ -96,7 +119,25 @@ public: * off-device or already has work outstanding. */ bool plan_promote(const BlockHash& key, TierJob* out); /* leader only */ - struct Stats { int64_t passes = 0, jobs = 0, bytes_out = 0, bytes_in = 0; }; + /* ---------------- RADIANCE_DEBUG_TIER_VERIFY, the replicated window ---------------- + * + * A replicated group's bytes are the plugin's claim. Under the variable a follower gathers the + * replicated groups it would otherwise leave to the leader into a pinned scratch beside the + * copy and hashes them once landed; the engine, once every rank has landed, takes each sum + * and has the leader compare it with the window it wrote. Off, a follower moves nothing for + * the replicated groups. */ + bool verifying_replicated() const { return verify_ && rep_bytes_ > 0; } + /* Follower: the sum of the replicated bytes it gathered for `key` in the last copy out, taken + * once. False when there is none (not verifying, no replicated group, or not in that copy). */ + bool take_replicated_sum(const BlockHash& key, uint64_t* sum); + /* Leader: compares `follower_sum` from rank `rank` with the window the slot holds for job + * `j`; a mismatch is logged and counted (Stats::rep_mismatch). */ + bool verify_replicated(const TierJob& j, int rank, uint64_t follower_sum); + + struct Stats { + int64_t passes = 0, jobs = 0, bytes_out = 0, bytes_in = 0; + int64_t rep_checked = 0, rep_mismatch = 0; /* verify_replicated's tallies */ + }; const Stats& stats() const { return st_; } private: @@ -111,6 +152,9 @@ private: /* rad_kv_group_zero: no shard on this rank. Kept in place because a job's block ids are * one per cached group in the same order on every rank; it moves no bytes. */ bool zero = false; + /* rad_kv_group_replicated: staged at the front of the entry and in the slot's replicated + * window; a follower gathers it only to verify, and scatters it on every restore. */ + bool replicated = false; Resolved gather, scatter; }; @@ -127,9 +171,17 @@ private: void warn_once(const char* what, int st); int run_rows(const Resolved& r, bool scatter, const GroupInfo& gi, int64_t layer, void* stage_base, const int32_t* d_idx, int64_t n_jobs); - /* One strided copy per run of consecutive arena slots, `n` entries starting at job `first` of - * `order`. To host when `out`, from host otherwise. */ + /* One strided copy per window per run of consecutive arena slots, `n` entries starting at job + * `first` of `order`. To host when `out` (the replicated window from the leader only), from + * host otherwise. */ int copy_runs(const std::vector& order, size_t first, int64_t n, bool out); + /* Whether this rank gathers group `gi` for a copy out: its own groups always, a replicated + * one as the leader or to verify. */ + bool gathers(const GroupInfo& gi) const { + return !gi.zero && (!gi.replicated || leader_ || verify_rep_); + } + /* The hash of one entry as the slot holds it: the replicated window, then this rank's own. */ + uint64_t entry_sum(const char* slot) const; int enqueue_copy(std::vector& jobs, RadStream compute); bool enabled_ = false; @@ -141,11 +193,13 @@ private: /* This rank's card. Allocations and launches use the current device, and the engine loop leaves * the last rank's device current, so it is set explicitly. */ int device_ = -1; - /* This rank's offset inside a shared host slot. A slot holds the whole engine's entry, which - * under tensor parallelism is n_ranks distinct KV shards, so the slot is n_ranks entries wide - * and each rank touches only its own window. */ + /* This rank's windows of a shared host slot (SlotLayout): its own groups at `slot_off_`, + * `own_bytes_` wide, and the replicated groups at the slot's front, `rep_bytes_` wide, which + * only the leader writes. The staging entry is [replicated][own], entry_bytes_ in all. */ int64_t slot_off_ = 0; - int64_t slot_bytes_ = 0; /* the whole shared slot: n_ranks * entry_bytes_ */ + int64_t slot_bytes_ = 0; /* the whole shared slot */ + int64_t rep_bytes_ = 0; + int64_t own_bytes_ = 0; Registry* reg_ = nullptr; KVManager* kv_ = nullptr; IdleTiers* tiers_ = nullptr; @@ -184,14 +238,18 @@ private: * * Round-trip check: hashes each entry going out and coming back and names the entry when they * differ, separating "the tier moved wrong bytes" from a downstream fault. A diagnostic, not a - * guard: host hashing costs on every entry. Off unless set; leader-only because the map is keyed - * by an object followers do not own. A copy out is hashed once landed (first seen by copy_state). */ + * guard: host hashing costs on every entry. Off unless set. The round trip is the leader's + * (the map is keyed by an object followers do not own); a follower's part is the replicated + * window, above. A copy out is hashed once landed (first seen by copy_state). */ bool verify_ = false; + bool verify_rep_ = false; /* a follower gathering the replicated groups to verify */ std::unordered_map out_sum_; - struct VerifyNote { BlockHash key; int32_t host_slot = -1; }; + std::unordered_map rep_sum_; /* follower */ + struct VerifyNote { BlockHash key; int32_t host_slot = -1; int64_t ord = -1; }; std::vector verify_pending_; - void verify_note(const BlockHash& key, const void* p, int64_t n); - void verify_check(const BlockHash& key, const void* p, int64_t n); + char* vrep_ = nullptr; /* pinned, max_copy_ * rep_bytes_: a follower's gather */ + void verify_note(const BlockHash& key, const char* slot); + void verify_check(const BlockHash& key, const char* slot); std::string report_; Stats st_; diff --git a/docs/GUIDE.md b/docs/GUIDE.md index fd97537d..99fc0402 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -1213,6 +1213,22 @@ radiance --model m.rad --prefix-cache-mib 8192 --prefix-cache-ttl 3600 - `--prefix-cache-ttl S`: seconds an unused conversation is kept (default 3600); `0` keeps it until the memory is wanted. +**What a slot holds under tensor parallelism.** One host slot holds the whole engine's entry: each +rank's shard of every cached group, end to end, so a slot is as many entries wide as there are +ranks. A group every rank holds whole, which the architecture plugin declares with +`rad_kv_group_replicated`, is in the slot once: rank 0 writes it and every rank reads it back on a +restore. GLM-5.3-Flash declares both of its cached groups so (the MLA latent, which every rank +computes whole, and the DSA index cache, which every rank pools into), so at `--tp 8` a 4-token +block costs 49.59 KiB a slot (`kv_latent` 48.00 KiB over its 12 layers, `kv_dsa_index` 1.59 KiB) +rather than the 396.75 KiB of eight copies: 12.4 KiB a token rather than 99.2 KiB. +`--prefix-cache-mib 32768` then holds 2.7M tokens rather than 338K, and 80 GiB hold 6.8M rather +than 846K. The startup log names what is held once (`kv tiers: kv_latent, kv_dsa_index ... held +once a slot`) and the tokens the tier holds either way. The claim is the plugin's; under +`RADIANCE_DEBUG_TIER_VERIFY` every follower also hashes what it would have written and the leader +compares, logging `kv tiers: the replicated groups of ... differ between rank 0 and rank N` when +a rank holds other bytes. Sharded groups (a GQA model's K/V heads) are unaffected and still take a +window per rank. + Restoring a 200K-token turn costs a copy over PCIe; recomputing it costs minutes. On a hybrid model the recurrent state matters as much as the attention blocks: restoring the blocks diff --git a/spec.md b/spec.md index 9551f665..2eec3bd9 100644 --- a/spec.md +++ b/spec.md @@ -1082,6 +1082,18 @@ moving blocks one at a time would be all dispatch cost. Within a layer blocks ar packing a set is `gather_rows` and restoring is `scatter_rows` (`docs/OPS.md`): one launch per (group, layer) for a whole batch. +**Under tensor parallelism** the policy describes conversations, which are not sharded, and the +transfer is per rank. One host slot holds the whole engine's entry: a group every rank holds whole +(`rad_kv_group_replicated`, an MLA latent cache) once, in one window at the slot's front that rank +0 writes and every rank reads back; then each rank's own shard of the sharded groups, one window +per rank, which can differ in width (uneven heads, `rad_kv_group_zero`). `TierExec::slot_layout` +is the one place the arithmetic lives, and bringup refuses a group replicated on one rank but zero, +unflagged or another width on another. The leader plans and reports; a follower moves only its own +window and, under `RADIANCE_DEBUG_TIER_VERIFY`, also gathers the replicated groups so the leader +can compare its window with every follower's hash once the batch has landed everywhere, which is +how the plugin's claim is checked. On GLM-5.3-Flash at eight ranks, whose two cached groups are +both replicated, a slot is 49.59 KiB rather than 396.75 KiB. + **Recurrent state.** On a hybrid model a session is blocks plus recurrent state. The state lives in a slot of the snapshot store (pinned host memory, `--checkpoint-slots`), copied off the card after the step that produced it and back before the step that resumes from it, so it never holds VRAM. @@ -1143,7 +1155,10 @@ shows every collective where the plugin put it. The residual stream is replicated: every rank holds the whole hidden state between blocks. Weights that act only on the stream, such as hyper-connection mixing (`arch/common/rad_block_hc.h`), are -replicated too, since sharding them would add a collective at each use. +replicated too, since sharding them would add a collective at each use. A KV group every rank +holds whole, such as an MLA latent cache computed from a replicated projection, is declared +`rad_kv_group_replicated`, and the idle session tiers (§7.3) then keep it once a host slot instead +of once a rank; the core cannot tell from the graph, so the plugin says it. `lm_head` is vocabulary-sharded like any column-parallel weight, so the sampler (§13) runs a distributed top-k: each rank reduces its own shard and only the candidate sets are combined. Full diff --git a/tests/arch_fakes.h b/tests/arch_fakes.h index 03902ae4..f8fd6949 100644 --- a/tests/arch_fakes.h +++ b/tests/arch_fakes.h @@ -71,7 +71,7 @@ struct RecMap { std::vector src; int concat_dim = 0; }; -struct RecKV { std::string name; RadKVGroupDecl d{}; bool zero = false; }; +struct RecKV { std::string name; RadKVGroupDecl d{}; bool zero = false; bool replicated = false; }; struct RecIssue { rad_op op = 0; std::vector opd; @@ -325,9 +325,20 @@ extern "C" int rad_weight_shard_span_parts(RadBuilder* b, rad_weight w, const in } extern "C" int rad_kv_group_zero(RadBuilder* b, rad_kvgroup g) { if (!b || g == 0 || g > b->kv_groups.size()) return RAD_E_INVAL; + if (b->kv_groups[g - 1].replicated) return RAD_E_INVAL; b->kv_groups[g - 1].zero = true; return RAD_OK; } +/* Recorded like zero, and like the real builder refused on a zero or unpaged group. Not part of + * graph_hash(): the flag changes what a host tier slot holds, not the graph, so the pinned + * graphs stand. */ +extern "C" int rad_kv_group_replicated(RadBuilder* b, rad_kvgroup g) { + if (!b || g == 0 || g > b->kv_groups.size()) return RAD_E_INVAL; + RecKV& k = b->kv_groups[g - 1]; + if (k.zero || (k.d.kind != RAD_KV_FULL && k.d.kind != RAD_KV_WINDOW)) return RAD_E_INVAL; + k.replicated = true; + return RAD_OK; +} extern "C" int rad_declare_drafter(RadBuilder* b, const RadDrafterDecl* d) { if (!b || !d) return RAD_E_INVAL; b->drafter = *d; diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index 2ad190f4..0a75602e 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -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>& was, const std::vector>& now) { const std::vector& cg = f.pc.cached_groups(); @@ -3963,12 +3968,13 @@ static int64_t same_bytes(Fixture& f, int rank, const std::vectorzero) 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 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()); fx.back()->cfg.checkpoint_slots = 0; std::vector 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& 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 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 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& 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> 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 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(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> fx; + for (int r = 0; r < 2; ++r) { + fx.push_back(std::make_unique()); + fx.back()->cfg.checkpoint_slots = 0; + std::vector 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 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);