diff --git a/core/engine.cpp b/core/engine.cpp index 71b4dea8..76283672 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -534,15 +534,20 @@ int Engine::step_loop() { * recorded the grab and cannot do the write itself (StateClear in core/mem/kv.h); this is * the first moment a stream exists and the last before the ranks are released, and it is * off the hot path -- the list is empty on every step but a sequence's first. Every rank - * clears its own pool: the slot number is the scheduler's, the memory is the rank's. */ + * clears its own pool BY ITS OWN PLAN: the slot number is the scheduler's, the memory and + * its size are the rank's. A state is as large as the heads this rank holds of the group, + * and the ranks' shares need not be equal (rad_arch.h remainder_place): a rank clearing + * rank 0's byte count of a larger state leaves its tail as the last sequence left it, and + * the recurrence reads it from the first token. */ for (const StateClear& c : ranks_[0]->kv.state_clears()) { - const KVGroupPlan* gp = ranks_[0]->kv.plan(c.group); - if (!gp || !gp->stateful()) continue; - const int64_t bytes = gp->layer_bytes_per_state(); - for (auto& rk : ranks_) + for (auto& rk : ranks_) { + const KVGroupPlan* gp = rk->kv.plan(c.group); + if (!gp || !gp->stateful()) continue; + const int64_t bytes = gp->layer_bytes_per_state(); for (int64_t l = 0; l < gp->n_layers; ++l) if (void* p = rk->kv.state_ptr(c.group, (int32_t)l, c.slot)) rad_memset_async(p, 0, bytes, rk->ctx.stream()); + } } ranks_[0]->kv.clear_state_clears(); diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index a2246fcd..424995a2 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -2621,15 +2621,31 @@ int Engine::configure_kv() { { const bool tier_asked = cfg_.prefix_cache_host_mib > 0 || cfg_.prefix_cache_disk_mib > 0; const std::vector& cg = ranks_[0]->prefix.cached_groups(); - int64_t entry_bytes = 0; - for (int32_t g : cg) { - const KVGroupPlan* p = ranks_[0]->kv.plan(g); - if (p && p->paged()) entry_bytes += p->bytes_per_block; - } /* ONE SLOT HOLDS EVERY RANK'S SHARD. An entry under tensor parallelism is n_ranks * pieces of KV that are not interchangeable, so the unit the tiers store and budget - * in is the whole thing; each rank writes only its own window of it. */ - const int64_t slot_bytes = entry_bytes * (int64_t)ranks_.size(); + * in is the whole thing; each rank writes only its own window of it. THE SHARDS NEED + * NOT BE EQUAL -- a rank's block holds its own heads of a group, and a world the heads + * do not divide gives the ranks different shares, or none (rad_kv_group_zero) -- so a + * slot is the SUM of the ranks' entries and a rank's window starts where the ranks + * before it end. Where every rank's share is the same this is n_ranks times one entry + * at offsets of one entry, as it always was. The sizing below (TierExec::sizes) takes + * the largest rank's entry: the staging ring is a rank's own, and the ring that fits + * the largest fits every one. */ + std::vector rank_entry(ranks_.size(), 0), rank_ck(ranks_.size(), 0); + std::vector entry_off(ranks_.size(), 0), ck_off(ranks_.size(), 0); + int64_t entry_bytes = 0, slot_bytes = 0, ck_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; + } + rank_ck[i] = ranks_[i]->kv.checkpoint_bytes(); + entry_off[i] = slot_bytes; + ck_off[i] = ck_slot_bytes; + slot_bytes += rank_entry[i]; + ck_slot_bytes += rank_ck[i]; + entry_bytes = std::max(entry_bytes, rank_entry[i]); + } /* ---- how the tier is split between the two halves of a session ---- * @@ -2657,7 +2673,6 @@ int Engine::configure_kv() { * Two thirds is the ceiling. Not because the arithmetic asks for one, but because the * clamp above is a property of THIS build rather than of the design, and a tier with * almost no room for blocks would be badly sized the day it is lifted. */ - const int64_t ck_slot_bytes = ranks_[0]->kv.checkpoint_bytes() * (int64_t)ranks_.size(); int64_t ck_host_cap = 0, ck_disk_slots = 0; if (ck_slot_bytes > 0) { const int64_t bs = ranks_[0]->prefix.block_size(); @@ -2795,8 +2810,8 @@ int Engine::configure_kv() { max_batch, max_copy, max_chain, /*leader=*/&r == &ranks_[0], r->device, - (int64_t)(&r - &ranks_[0]) * entry_bytes, - (int64_t)(&r - &ranks_[0]) * ranks_[0]->kv.checkpoint_bytes(), + entry_off[(size_t)(&r - &ranks_[0])], + ck_off[(size_t)(&r - &ranks_[0])], /*shared=*/r->mover.transfer_stream()) != RAD_OK) { all = false;