Transfer completion: flag B stays behind queued copies, and the adaptive-copy range covers the whole round (P1.1)

Correctness (class E): kept even without a speedup.

Flag B (PR #5096): the graph waits for "flag B >= layer + 1" and flag B is one counter that only rises. A non-empty
plan raises it from a host function on the copy stream, after its uploads. An empty plan (n <= 0) raised it on the
spot, even when the copy stream still held an earlier layer's, or the other token group's, uploads: the counter
jumped past a layer whose bytes had not landed. Verifier::raise_flag_b_ordered() now raises it through the copy
stream whenever copies were queued in this window, and directly only when none were. The same ordering is used at
the four places that publish an empty plan when the pool did not (run, run_slot_rows, batch_serve, service), which
had the same hazard. A failed host-function launch used to be ignored (flag B never rose and the window hung); it
now stops the engine like a failed upload does, after releasing the GPU waits.

Adaptive-copy bookkeeping (PR #4844): a round planned on the pipelined loop whose loop drained before step 3 is
marked resident on the serial rule, which used the lo/hi/bi of an earlier round, so only that range of swaps was
marked and the others were lost (the slot held the new expert, the table still named the old one). The pipelined plan
now names the whole round. The range arithmetic moved into AdaptiveCopyRange (adaptive_range.hpp) so a host test can
walk the rounds.

Tests: tests/core/flag_order_test.cu (delayed host-function copy standing in for a slow DMA, a consumer kernel that
reads staging once flag B allows it, a negative control that shows the old direct raise releases it early, flag only
rises, nothing queued, graph replay twice, every device; passes on both A4000s of z440), tests/program/
adaptive_range_test.cpp (serial batches for 0, 1, many swaps and several caps, the stale-range loss, empty round).

Co-authored-by: AlanJager <8843214+AlanJager@users.noreply.github.com>
Co-authored-by: uvlad23 <19749374+uvlad23@users.noreply.github.com>
This commit is contained in:
committed 2026-11-05 23:39:06 +01:00
1 parent a5b820a415
commit 40849df0a8
8 files changed
+379 -31

No files matched your search

+11 -9
View File
@@ -25,6 +25,7 @@
#include "strata/kernels/gfx_arch.hpp"
#include "strata/core/batch_telemetry.hpp"
#include "strata/core/dma_batch.hpp"
#include "strata/program/adaptive_range.hpp"
#include "strata/core/device.hpp"
#include "strata/core/remote_expert_opt.hpp"
#include "strata/core/expert_cache.hpp"
@@ -13104,7 +13105,7 @@ int main(int argc, char** argv) {
Clock::time_point a_t0;
std::atomic<bool> a_err{false};
double a_pick_ms = 0; // the arena's inline choice, summed (the stats line)
size_t a_lo = 0, a_hi = 0, a_bi = 0; // the batch being copied in (a_copy_in), and where the next one starts
strata::program::AdaptiveCopyRange a_rng; // the batch being copied in (a_copy_in), and where the next one starts
std::vector<AHome> ahomes; // [0] = CUDA0's cache, [k] = layer split stage k's
std::unique_ptr<JobThread> ajob;
// the arena mode (srcp == &arena_src): every expert is in the arena already, so a round is a copy in only - no
@@ -13252,7 +13253,7 @@ int main(int argc, char** argv) {
};
auto a_copy_in = [&]() { // step 2 on the job thread (the serial loop): each source read, then copied in
std::vector<uint8_t> used(ahomes.size(), 0);
for (size_t i = a_lo; i < a_hi; ++i) { // (the batch; all the round's swaps unless the arena caps it)
for (size_t i = a_rng.lo; i < a_rng.hi; ++i) { // (the batch; all the round's swaps unless the arena caps it)
const ASwap& w = aswaps[i];
if (const uint8_t* b = a_from(w)) {
a_in_one(w, b);
@@ -13263,9 +13264,7 @@ int main(int argc, char** argv) {
if (used[hi]) a_mark(ahomes[hi]);
};
auto a_post_batch = [&]() { // step 2: the next batch of copies in goes to the job thread (all of them but for the arena's cap)
a_lo = a_bi;
a_hi = a_batch > 0 ? std::min(aswaps.size(), a_bi + a_batch) : aswaps.size();
a_bi = a_hi;
a_rng.next_batch(aswaps.size(), a_batch);
ajob->post(a_copy_in);
};
auto a_stage_in = [&]() { // step 2's host part on the job thread (the pipelined loop: a_in queues the copies)
@@ -13385,10 +13384,13 @@ int main(int argc, char** argv) {
if (pl) { // the copies in at each card's next gap: after this step, so fenced (see above)
a_res_dirty = true;
for (const ASwap& w : aswaps) a_in_due[(size_t) w.home] = 1;
// the whole round is one batch: when the pipelined loop drains before step 3, the next tick
// takes step 3 on the serial rule and marks [lo, hi), which an earlier round left otherwise
a_rng.whole_round(aswaps.size());
ajob->post(a_stage_in);
} else {
res_upload();
a_lo = a_hi = a_bi = 0;
a_rng.clear();
a_post_batch();
}
astate = AState::SwapIn;
@@ -13399,15 +13401,15 @@ int main(int argc, char** argv) {
if (a_due(a_in_due)) return true; // (pipelined) a card not seen idle since step 2
if (!a_ready(drain || a_wait)) return true;
if (a_err.load()) return false;
for (size_t i = pl ? 0 : a_lo; i < (pl ? aswaps.size() : a_hi); ++i) { // (the batch just landed)
for (size_t i = a_rng.landed_begin(pl); i < a_rng.landed_end(pl, aswaps.size()); ++i) { // (the batch just landed)
const ASwap& w = aswaps[i];
host_res[(size_t) w.layer * g.n_expert + w.in] = w.slot;
srcp->release(w.layer, w.in);
}
if (pl) a_res_dirty = true;
else res_upload();
a_swapped += pl ? (int64_t) aswaps.size() : (int64_t) (a_hi - a_lo);
if (!pl && a_bi < aswaps.size()) { // the arena's cap: the next batch goes now
a_swapped += (int64_t) (a_rng.landed_end(pl, aswaps.size()) - a_rng.landed_begin(pl));
if (!pl && a_rng.more(aswaps.size())) { // the arena's cap: the next batch goes now
for (AHome& h : ahomes) h.used = false;
a_post_batch();
if (!drain) return true;