diff --git a/abi/rad_abi.h b/abi/rad_abi.h index 254977ab..84dec006 100644 --- a/abi/rad_abi.h +++ b/abi/rad_abi.h @@ -544,7 +544,17 @@ typedef int (*RadKernelConcurrentFn)(int i); * do, adds exactly once. The record is RAD_CHAIN_REC_BYTES at byte RAD_CHAIN_KARG_OFF of the * kernel's argument segment: { magic, target, ctr, check }, with check = magic ^ target ^ ctr ^ * RAD_CHAIN_CHECK. A record whose magic or check does not match means: run without waiting and do - * not add. Without the symbol, or with 0, the engine never asks. Symbol name: `rad_kernel_chain`. */ + * not add. Without the symbol, or with 0, the engine never asks. + * + * 2 instead of 1 adds a second promise: the chained form's own acquire covers everything it reads + * behind the wait, scalar loads included. A compiler's agent-scope fence invalidates the vector + * caches only; a load every lane shares may be a scalar one, served from the scalar data cache, + * which still holds what was there before the launch in front wrote. A form that invalidates that + * cache behind its acquire (`s_dcache_inv`), or reads no operand through it, may say 2. Where + * every chainable row an engine loads says 2, the engine issues a chained launch that has lost + * its barrier bit without the packet processor's acquire as well: on an R9700 that acquire holds + * the processor ~0.9 us a launch, and it comes too early to serve a kernel that then waits for + * the launch in front. With 1 the packets keep it. Symbol name: `rad_kernel_chain`. */ typedef int (*RadKernelChainFn)(int i); #define RAD_CHAIN_KARG_OFF 992 #define RAD_CHAIN_REC_BYTES 32 diff --git a/core/device/aql.cpp b/core/device/aql.cpp index 2a7a67c6..47965ae3 100644 --- a/core/device/aql.cpp +++ b/core/device/aql.cpp @@ -27,6 +27,9 @@ * system-scope acquire, since host writes for the next step follow such a wait; * - copies touching host or peer memory carry the scope themselves, as do the barrier packets * that implement events and synchronisation. + * + * A chained kernel that has lost its barrier bit may go without the acquire (see "the acquire of a + * chained launch"): it reads behind a wait in its own prologue, long after its packet's acquire. */ #include "device.h" @@ -454,8 +457,8 @@ bool init_devices() { if (!g.dev[(size_t)i].hdp_flush) RAD_WARN("device: card %d (%s) gives no host data path flush register, so a kernel " "may start before the arguments written for it through the BAR have " - "reached VRAM; only the packet processor's acquire, which holds a launch " - "back, leaves them the time", i, bus); + "reached VRAM; chained launches keep the packet processor's acquire, " + "which leaves them the time they had", i, bus); } } /* Without the handler a fault falls back to the runtime's own handling, which the @@ -1120,13 +1123,15 @@ inline int reserve(Stream* s, uint64_t* out) { * it obeys (an old target, so no wait) or does not accept (so it counts nothing and the chained * launch behind it waits for good), then, behind its wait, its arguments. * - * On an R9700 "soon enough" is a microsecond or two. What kept launches from it so far is that the - * packet processor's acquire holds a launch about 0.9 us and an ordered launch waits for the - * kernel in front: the margin is timing, not ordering. Without the packet's acquire (a test that - * issues it so) runs of fourteen 256 KiB card-side copies issued one by one: back to back, six runs - * in nine of 300 000 ended with a unit moved by another launch's arguments; with the host quiet - * for 50 us behind each doorbell the queue stalled in its first lap, four runs in four, on either - * card. With the flush below neither happened. + * On an R9700 "soon enough" is a microsecond or two. The packet processor's acquire holds a + * launch 0.9 us and an ordered launch waits for the kernel in front, which is all that kept + * launches from it; a chained launch whose packet goes without the acquire starts that much + * sooner behind its doorbell. Runs of fourteen 256 KiB card-side copies issued one by one that + * way: back to back, six runs in nine of 300 000 ended with a unit moved by another launch's + * arguments; with the host quiet for 50 us behind each doorbell the queue stalled in its first + * lap, four runs in four, on either card. With the flush below neither happened (fourteen runs + * and four), nor did it with the acquire on the packets, with plain launches, or with a 2 us + * pause in front of each doorbell. * * The register is the driver's (HSA_AMD_AGENT_INFO_HDP_FLUSH, the remapped page it gives a * process for this), and the store travels the link behind the slot's bytes and in front of the @@ -1180,6 +1185,9 @@ struct TapeRec { uint8_t chain = 0; /* A collective's launch (a_peer_launch). */ uint8_t peer = 0; + /* A chained kernel whose library acquires for it in the kernel (chain_bare): played without + * its barrier bit, its packet takes no acquire from the packet processor. */ + uint8_t bare = 0; }; struct Tape { @@ -1200,12 +1208,13 @@ inline bool capturing() { return t_tape && t_tape_pause == 0; } /* A dispatch just written at `p` on `s`, with its argument block `ka`. */ void tape_note_dispatch(Stream* s, const hsa_kernel_dispatch_packet_t* p, uint16_t setup, - const unsigned char* ka, uint32_t n, uint8_t fence, bool chain) { + const unsigned char* ka, uint32_t n, uint8_t fence, bool chain, bool bare) { Tape* t = t_tape; TapeRec r; r.kind = kTapeDispatch; r.fence = fence; r.chain = chain ? 1 : 0; + r.bare = chain && bare ? 1 : 0; r.peer = t_peer ? 1 : 0; r.s = s; r.pkt = *p; @@ -1226,11 +1235,14 @@ inline uint16_t header(hsa_packet_type_t type, int acq, int rel, bool barrier = (rel << HSA_PACKET_HEADER_SCRELEASE_FENCE_SCOPE)); } -/* Whether the packet at `idx` must acquire at system scope, and the bookkeeping if it does. */ -inline int acquire_scope(Stream* s, uint64_t idx, bool forced) { +/* Whether the packet at `idx` must acquire at system scope, and the bookkeeping if it does. + * `*asked` says the acquire is one the argument ring's half lap, a host write or the caller asks + * for, at whichever scope it comes back, and not the agent scope every dispatch takes besides. */ +inline int acquire_scope(Stream* s, uint64_t idx, bool forced, bool* asked) { const uint64_t ep = G().host_epoch.load(std::memory_order_acquire); const bool lap = (int64_t)idx - s->last_sys_acq >= (int64_t)(kQueuePackets / 2); - if (forced || lap || s->epoch != ep) { + *asked = forced || lap || s->epoch != ep; + if (*asked) { s->last_sys_acq = (int64_t)idx; s->epoch = ep; /* The prefetch stream reads nothing the host writes; a system acquire there would only @@ -1241,6 +1253,55 @@ inline int acquire_scope(Stream* s, uint64_t idx, bool forced) { return HSA_FENCE_SCOPE_AGENT; } +/* The acquire of a chained launch. + * + * A packet's acquire runs when the packet processor launches the kernel. A chained kernel that has + * lost its barrier bit is launched ahead and reads nothing then: it reads behind the wait in its + * prologue, once the launches in front have written, and acquires there itself. The packet's + * acquire guards nothing it reads, and the packet processor launches nothing else while it runs + * one. Measured on an R9700, a chain of small dependent kernels issued behind a gate: 2.1 us a + * launch with the agent-scope acquire, 1.2 us without (system scope costs what agent scope does; + * the same hand-over inside one persistent kernel, with no launch between, 0.3 to 0.6 us). In a + * pass that is time on the path only where a dependent launch reaches the card after the kernel + * it waits for has ended: ~65 of a Qwen3.8-Flash-Next verify pass's 664 dependent launches, the + * delta net's update behind its two overlapped launches first among them. + * + * The acquire did one thing by the way, for every kernel on the card: it emptied the scalar data + * cache. No fence a compiler puts in a kernel does (an agent-scope acquire is a global_inv: the + * vector caches), and a load whose address every lane shares is often a scalar one: a row's + * scale, a routing count, the activation a head reads. A chained form that reads such a value + * where the launch in front wrote it finds what an earlier launch read from that address, unless + * a later packet's acquire happened to fall in between. With an acquire on every packet it nearly + * always had (one launch in 1800 read stale in a probe of 1024-block readers); without, 1797 in + * 1800 did, and the model's text changed. So the acquire goes only where every chained form the + * engine can launch empties that cache itself behind its wait (RadKernelChainFn saying 2, + * rad_dev_chain_unfenced; `s_dcache_inv` in r4d_chain.h and chain_kernel.h), which also closes + * the window the fenced packets leave. + * + * What stays as it was: the release (dropping it as well measured nothing), a packet that keeps + * its bit, a plain kernel, every barrier packet, and every acquire acquire_scope asks for: the + * argument ring's half lap, the first dispatch after a host wait, a copy over the link. Those + * guard what the kernel reads of its own slot, its arguments and chain record, which the host + * wrote where some launch read a lap ago and which the L2 would still hold; they are kept at + * whichever scope they come. The prefetch stream keeps the acquire on every packet, as before: + * its half-lap acquire is an agent-scope one, and nothing waits for its launches. + * + * The acquire did a second thing by the way: it held the launch until the slot the host had + * just written was in VRAM, which nothing else saw to (host_writes_out). A first cut of this + * change, without the flush there, stopped a server once: a chained card-side copy on a card's + * second queue never finished, and the queue stood on it with 14 packets written behind. So a + * card whose arguments are in VRAM and that gives no flush register keeps the acquire on its + * chained launches. RADIANCE_AQL_CHAIN_FENCE=1 keeps it on every chained launch, to rule this + * out. */ +inline bool chain_bare(const Stream* s) { + static const bool keep = [] { + const char* e = std::getenv("RADIANCE_AQL_CHAIN_FENCE"); + return e && *e == '1'; + }(); + if (keep || s->agent_only || rad_dev_chain_unfenced() == 0) return false; + return s->hdp != nullptr || G().dev[(size_t)s->ord].host_kargs; +} + /* Collectives on a shared card. * * A chained kernel launched behind a running one holds its wave slots while it waits in its @@ -1358,20 +1419,27 @@ int dispatch(Stream* s, const KernelRec* k, dim3 g, dim3 b, void** args, size_t const bool chain = k->chained && rad_dev_chain_enabled() && s->chain_ctr && n <= RAD_CHAIN_KARG_OFF; if (k->chained) chain_record(s, slot, chain, s->chain_issued); - if (capturing()) tape_note_dispatch(s, p, setup, ka, n, fence, chain); + const bool bare = chain && chain_bare(s); + if (capturing()) tape_note_dispatch(s, p, setup, ka, n, fence, chain, bare); /* Attach the ride after the tape copied the packet: a recorded packet carries no signal, and * a play attaches whichever ride is pending then (PlayRun::put). */ if (s->ride.handle) { p->completion_signal = s->ride; s->ride.handle = 0; } - const int acq = acquire_scope(s, idx, (fence & kFenceAcquireSystem) != 0); + bool asked = false; + const int acq = acquire_scope(s, idx, (fence & kFenceAcquireSystem) != 0, &asked); const int rel = (fence & kFenceReleaseSystem) ? HSA_FENCE_SCOPE_SYSTEM : HSA_FENCE_SCOPE_AGENT; /* Behind another chained kernel a chained one waits in its prologue; a completion signal keeps * the bit, since its completion must cover everything before it. */ const bool keep = !(chain && s->prev_chained && !s->plain_inflight && !p->completion_signal.handle && !(s->peer_inflight && card_shared(s))); - publish(s, idx, header(HSA_PACKET_TYPE_KERNEL_DISPATCH, acq, rel, keep), setup); + /* Without the bit, a kernel that acquires for itself takes none from the packet processor, + * unless the ring or the host asked for this one. */ + publish(s, idx, + header(HSA_PACKET_TYPE_KERNEL_DISPATCH, + bare && !keep && !asked ? HSA_FENCE_SCOPE_NONE : acq, rel, keep), + setup); s->chain_frontier = s->chain_issued; s->frontier_open = !keep; if (chain) s->chain_issued += blocks_of(p); @@ -1495,7 +1563,8 @@ struct PlayRun { chain_record(s, slot, true, dependent ? s->chain_issued : s->chain_frontier); } } - const int acq = acquire_scope(s, idx, (r.fence & kFenceAcquireSystem) != 0); + bool asked = false; + const int acq = acquire_scope(s, idx, (r.fence & kFenceAcquireSystem) != 0, &asked); int rel = (r.fence & kFenceReleaseSystem) ? HSA_FENCE_SCOPE_SYSTEM : HSA_FENCE_SCOPE_AGENT; if (rel < rel_min) rel = rel_min; /* A dispatch carrying a record keeps the bit: its completion must cover everything before @@ -1514,7 +1583,12 @@ struct PlayRun { stamp->ordered = ordered; stamp->chained = chain; } - const uint16_t h = header(HSA_PACKET_TYPE_KERNEL_DISPATCH, acq, rel, ordered); + /* Without the bit, a kernel that acquires for itself takes none from the packet + * processor, unless the ring or the host asked for this one (see "the acquire of a + * chained launch"). */ + const uint16_t h = header(HSA_PACKET_TYPE_KERNEL_DISPATCH, + chain && r.bare && !ordered && !asked ? HSA_FENCE_SCOPE_NONE : acq, + rel, ordered); __atomic_store_n(reinterpret_cast(p), (uint32_t)h | ((uint32_t)r.pkt.setup << 16), __ATOMIC_RELEASE); foreign = false; diff --git a/core/device/device.h b/core/device/device.h index 65dac58c..22aac128 100644 --- a/core/device/device.h +++ b/core/device/device.h @@ -43,12 +43,18 @@ extern "C" void rad_dev_host_wrote(void); * lets a chained kernel (named r4dchain*) wait in its prologue instead of behind the barrier bit. * Only on the AQL backend, whose queues keep the completion counter, and only where the model's * plugin measured a gain (RadArchProbe::chain_pays): with its drafter decoding speculatively, or - * with and without one (rad_dev_chain_infer, from the engine before any row resolves). */ + * with and without one (rad_dev_chain_infer, from the engine before any row resolves). + * + * rad_dev_chain_infer(2) says more: every chained form this engine can launch fences itself + * (RadKernelChainFn saying 2, abi/rad_abi.h). rad_dev_chain_unfenced then answers 1, and the + * backend issues a chained launch that has lost its barrier bit without the packet processor's + * acquire as well (aql.cpp, "the acquire of a chained launch"). */ extern "C" int rad_dev_chain_enabled(void); /* VRAM the runtime keeps resident as queue scratch on `props`' card once a kernel with a private * segment has run (0 on the host backend and on unified memory). Charged to the VRAM budget with * the hardware queues. */ extern "C" int64_t rad_dev_resident_scratch_bytes(const RadDeviceProps* props); +extern "C" int rad_dev_chain_unfenced(void); extern "C" void rad_dev_chain_infer(int pays); /* One range of a ranged copy: `bytes` from source offset `src` to destination offset `dst`, all diff --git a/core/device/dispatch.cpp b/core/device/dispatch.cpp index 97ca577f..f9a8ef75 100644 --- a/core/device/dispatch.cpp +++ b/core/device/dispatch.cpp @@ -949,21 +949,24 @@ extern "C" int64_t rad_dev_resident_scratch_bytes(const RadDeviceProps* p) { * serves several models, each with its own plugin's answer; a single answer for the process was * the answer of whichever model started last. Owner 0 is a thread outside every engine (a tool, a * test). Only where the backend chains: the AQL backend writes the record a chained form reads, - * while a HIP launch carries only the kernel's own arguments. */ + * while a HIP launch carries only the kernel's own arguments. The answer is 2 where every chained + * form the engine can launch fences itself (RadKernelChainFn saying 2), which + * rad_dev_chain_unfenced reads. */ static std::mutex g_chain_mu; -static std::map g_chain_pays; /* by owner */ +static std::map g_chain_pays; /* by owner: 0, 1, or 2 (their packets unacquired) */ static std::atomic g_chain_gen{1}; void rad_dev_chain_infer(int pays) { { std::lock_guard lk(g_chain_mu); - g_chain_pays[rad::engine_owner()] = pays ? 1 : 0; + g_chain_pays[rad::engine_owner()] = pays == 2 ? 2 : pays ? 1 : 0; } g_chain_gen.fetch_add(1, std::memory_order_release); } -int rad_dev_chain_enabled(void) { - /* Asked on every launch: a thread keeps its engine's answer until some answer changes. */ +/* The calling thread's engine's answer where the backend chains, else 0: asked on every launch, + * so a thread keeps it until some answer changes. */ +static int chain_mode() { thread_local uint64_t seen = 0; - thread_local int owner = -1, on = 0; + thread_local int owner = -1, mode = 0; const uint64_t gen = g_chain_gen.load(std::memory_order_acquire); const int o = rad::engine_owner(); if (seen != gen || owner != o) { @@ -973,12 +976,14 @@ int rad_dev_chain_enabled(void) { const auto it = g_chain_pays.find(o); if (it != g_chain_pays.end()) pays = it->second; } - on = pays && std::strcmp(rad::device_backend_name(), "aql") == 0 ? 1 : 0; + mode = pays && std::strcmp(rad::device_backend_name(), "aql") == 0 ? pays : 0; seen = gen; owner = o; } - return on; + return mode; } +int rad_dev_chain_enabled(void) { return chain_mode() != 0; } +int rad_dev_chain_unfenced(void) { return chain_mode() == 2; } int rad_event_elapsed_ms(RadEvent a, RadEvent b, float* out) { return dev()->event_elapsed_ms(a, b, out); diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index 7bb4f1b2..6b976494 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -1267,7 +1267,14 @@ int Engine::declare() { (void)rad_tools_probe(plugins_, meta_, &pb, cfg_.image_gen_max_pixels, cfg_.image_gen_max_refs); const bool always = pb.chain_pays == RAD_CHAIN_ALWAYS; const bool pays = always || (cfg_.n_spec > 0 && pb.chain_pays != RAD_CHAIN_NONE); - rad_dev_chain_infer(pays); + /* Their packets go without the packet processor's acquire where every row that can chain + * acquires in the kernel for all it reads (RadKernelChainFn saying 2): one row that does + * not, from any library this engine loaded, keeps the acquire on all of them, since a + * queue's packets are not told apart by library. */ + bool fenced = pays; + for (const KernelRow& row : reg_->rows()) + if (row.chain && !row.chain_fenced) fenced = false; + rad_dev_chain_infer(pays ? (fenced ? 2 : 1) : 0); const bool on = rad_dev_chain_enabled() != 0; const char* why = on ? (always ? "the model's plugin measured a gain with them" : "the model's plugin measured a gain with its " @@ -1275,7 +1282,12 @@ int Engine::declare() { : pays ? "the device backend does not chain" : "no drafter whose plugin measured a gain with them decodes " "speculatively"; - RAD_INFO("chained launches: %s -- %s", on ? "on" : "off", why); + RAD_INFO("chained launches: %s -- %s%s", on ? "on" : "off", why, + !on ? "" + : rad_dev_chain_unfenced() ? "; their packets take no acquire from the packet processor" + : "; their packets keep the packet processor's acquire " + "(a row that chains does not acquire for its scalar " + "loads itself)"); decide("chained launches", on ? "on" : "off", why); } /* Trees: a block drafter's proposals verified as a tree, where the model's plugin measured a diff --git a/core/plugin/loader.cpp b/core/plugin/loader.cpp index 575e89b0..d4e84243 100644 --- a/core/plugin/loader.cpp +++ b/core/plugin/loader.cpp @@ -538,7 +538,9 @@ int Registry::commit_(Staged& s, int order) { row.collective = coll != 0; row.releases_link = coll == 2; row.reference = kern_ref ? kern_ref(sr.second) != 0 : p.reference; - row.chain = chainable && chainable(sr.second) != 0; + const int chains = chainable ? chainable(sr.second) : 0; + row.chain = chains != 0; + row.chain_fenced = chains == 2; set_->rows.push_back(row); } diff --git a/core/rad_core.h b/core/rad_core.h index 7e3fa219..34ead20f 100644 --- a/core/rad_core.h +++ b/core/rad_core.h @@ -64,6 +64,9 @@ struct KernelRow { bool reference = false; /* rad_kernel_chain: its launches can wait in the kernel instead of behind the barrier bit. */ bool chain = false; + /* rad_kernel_chain said 2: its chained forms' acquire covers their scalar loads too, so their + * packets need no acquire from the packet processor. */ + bool chain_fenced = false; }; /* A kernel bound to one geometry, one domain and one variant, with init() already run. */ diff --git a/docs/GUIDE.md b/docs/GUIDE.md index acb6c83e..ac23b784 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -1642,6 +1642,17 @@ other model; the startup report's `chained launches` row says which and why. Whe than one card share their cards, a launch behind a collective keeps its barrier bit (about 1% of a step; MULTIMODEL §9.1). +A chained kernel acquires in its own prologue, behind its wait, so its packet goes out without +the packet processor's acquire where every kernel library the engine loaded says its chained forms +cover all they read there (`rad_kernel_chain` answering 2: libr4d's do, by emptying the scalar +data cache behind the fence). That acquire holds the packet processor about 0.9 us a launch on an +R9700 and comes before the kernel in front has written, so it guards nothing the launch reads. It +did give the arguments the host had just written into VRAM the time to get there; the backend now +sees to that itself and flushes the card's host data path in front of every doorbell, for every +launch (a card that offers no flush register keeps the acquire). The log line `chained launches: +on` says whether the packets take the acquire. `RADIANCE_AQL_CHAIN_FENCE=1` puts it back, to rule +the change out. + **Draft trees.** Where the model's plugin measured a gain (the Gemma 4 31B with DFlash2: +7% single-stream decode and +6-9% aggregate at 1-16 users on 4x R9700, +4% and +6-12% on two, BetterBench with thinking off), a block drafter's proposals are verified as a tree: grown best-first @@ -4535,6 +4546,7 @@ TranslateGemma 18.3 ms a step, against 19.8-20.5. | `RADIANCE_G4_CALIB_LAYERS` | `lo:hi`: the same for `arch/gemma4`'s tap, which takes the Gemma 4 31B's Grams from its bf16 checkpoint (docs/ARCHITECTURES.md, gemma4) | | `RADIANCE_PROFILE_EVERY` | with `profile-ops`, dump the per-op table every N steps (default 32) | | `RADIANCE_AQL_STAMP`, `RADIANCE_AQL_STAMP_OUT` | `N`: time every dispatch of one recorded pass in N on card 0, each with a completion signal of its own, without changing its barriers or chains (`profile-ops` plays no recorded pass). One CSV line a dispatch (pass, record, queue, kernel, ordered, chained, start and end in ns) goes to `RADIANCE_AQL_STAMP_OUT` (default `/tmp/radiance-stamps-.csv`); `scripts/stampgaps.py` summarises it per kind of pass. AQL backend only | +| `RADIANCE_AQL_CHAIN_FENCE` | `1`: a chained launch's packet keeps the packet processor's acquire even where the kernel libraries acquire in the kernel (§ "Chained launches"). For ruling the unfenced packets out of a fault, not for deployment: about 1% of a Qwen3.8-Flash-Next step. AQL backend only | | `RADIANCE_LOG_STEPS` | log one line per step: its phase, sequences, and each one's token count and context (§4.5) | Engine development has its own instruments: `RADIANCE_DEBUG_*` (residual probes, routing and issue diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index a5fb9ed9..50e2653c 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -10956,7 +10956,12 @@ extern "C" int rad_kernel_concurrent(int i) { /* Which rows can chain (RadKernelChainFn, rad_abi.h): every kernel their launch emits has a * chained form (r4d_chain.h) the launch selects from the `chain` parameter. A row joins this list - * only once all of its kernels have one. */ + * only once all of its kernels have one. + * + * They answer 2: every chained form here ends its wait in r4d_chain_acquire, which empties the + * scalar data cache behind the fence, so the engine may issue their packets without the packet + * processor's acquire. A form that waited without it would have to keep this at 1 for the whole + * library: one row's 1 keeps the fences on every chained packet of the engine. */ extern "C" int rad_kernel_chain(int i) { const RadKernelInfo* k = rad_kernel_at(i); if (!k) return 0; @@ -11078,13 +11083,13 @@ extern "C" int rad_kernel_chain(int i) { "kv_store_i8", }; for (const char* p : kChained) - if (std::strcmp(k->name, p) == 0) return 1; + if (std::strcmp(k->name, p) == 0) return 2; /* The host-mailbox twins (host_twins, --p2p off) of the two-rank fused writes: the same * launcher and kernels as the rows they are generated from, so the same chained forms. Only * these two: the other collectives' twins have not been run chained through the mailbox. */ static const char* const kChainedHostTwins[] = { "ar_hc_write_host", "ar_gather_hc_write_host" }; for (const char* p : kChainedHostTwins) - if (std::strcmp(k->name, p) == 0) return 1; + if (std::strcmp(k->name, p) == 0) return 2; return 0; } diff --git a/tests/device_test.cpp b/tests/device_test.cpp index a1f41d30..f1fa771a 100644 --- a/tests/device_test.cpp +++ b/tests/device_test.cpp @@ -133,17 +133,25 @@ TEST(chained_launches_follow_the_plugins_ask_on_the_aql_backend_only) { CHECK_EQ(rad_dev_chain_enabled(), 0); rad_dev_chain_infer(1); CHECK_EQ(rad_dev_chain_enabled(), aql ? 1 : 0); + /* Their packets go unfenced only where the engine says every chained form fences itself (2), + * and only where launches chain at all. */ + CHECK_EQ(rad_dev_chain_unfenced(), 0); + rad_dev_chain_infer(2); + CHECK_EQ(rad_dev_chain_enabled(), aql ? 1 : 0); + CHECK_EQ(rad_dev_chain_unfenced(), aql ? 1 : 0); rad_dev_chain_infer(0); CHECK_EQ(rad_dev_chain_enabled(), 0); + CHECK_EQ(rad_dev_chain_unfenced(), 0); } /* A chained kernel reads behind its wait what the launch in front of it wrote, a value all its * lanes share included. The compiler loads such a value through the scalar data cache, which no - * fence it emits empties: only the packet processor's acquire does, when the kernel is launched, - * before the launch in front has written. The wait must therefore empty that cache itself - * (chain_kernel.h): one reader in 1800 saw the value the reader before it loaded on an R9700 - * before it did, with the packets' acquire on. Behind a gate, so the card has the readers launched - * ahead of the stores they wait for, as a played pass has them. */ + * fence it emits empties: only the packet processor's acquire did, when the kernel was launched, + * before the launch in front had written. A chained launch whose packet goes unfenced + * (rad_dev_chain_infer(2)) must therefore empty that cache behind its wait (chain_kernel.h), or + * every reader here sees the value the reader before it loaded: 1797 launches in 1800 did on an + * R9700 before the wait emptied it. Behind a gate, so the card has the readers launched ahead of + * the stores they wait for, as a played pass has them. */ TEST(a_chained_kernel_sees_a_shared_value_the_launch_in_front_wrote) { if (std::strcmp(rad::device_backend_name(), "aql") != 0) SKIP("launches chain on the AQL backend only"); const unsigned pairs = 400, blocks = 256; @@ -152,8 +160,10 @@ TEST(a_chained_kernel_sees_a_shared_value_the_launch_in_front_wrote) { DevBuf seen((int64_t)pairs * blocks * sizeof(unsigned), RAD_MEM_DEVICE); REQUIRE(x && seen); std::vector got((size_t)pairs * blocks); - { - rad_dev_chain_infer(1); + for (int fenced = 0; fenced < 2; ++fenced) { + /* 2: the packets go unfenced, as an engine whose libraries fence in the kernel issues + * them. 1: they keep the packet processor's fences, and the readers must see the same. */ + rad_dev_chain_infer(fenced ? 1 : 2); CHECK_OK(rad_memset_async(x, 0, 64 * sizeof(unsigned), st.s)); CHECK_OK(rad_memset_async(seen, 0, (int64_t)pairs * blocks * sizeof(unsigned), st.s)); CHECK_OK(rad_stream_sync(st.s)); @@ -176,7 +186,8 @@ TEST(a_chained_kernel_sees_a_shared_value_the_launch_in_front_wrote) { /* A chained launch reads its own arguments and its chain record from a slot of the argument ring * that the host wrote and that another launch read a lap of the ring ago; the L2 still holds what * that launch read. Only a system-scope acquire of the packet processor's makes the card forget - * it: the one the ring asks for every half lap. Three laps of launches that each store where their own arguments say, + * it: the one the ring asks for every half lap, which a chained packet keeps while it goes + * without every other. Three laps of launches that each store where their own arguments say, * with no host wait between them: without that acquire every launch past the first lap stores * where the launch a lap before did (as they do on a prefetch stream, which never takes one and * counts on the pass beside it to have turned the L2 over). */ @@ -187,8 +198,8 @@ TEST(a_chained_launch_reads_its_own_arguments_after_the_ring_has_lapped) { DevBuf seen((int64_t)n * sizeof(unsigned), RAD_MEM_DEVICE); REQUIRE(!!seen); std::vector got(n); - { - rad_dev_chain_infer(1); + for (int fenced = 0; fenced < 2; ++fenced) { + rad_dev_chain_infer(fenced ? 1 : 2); CHECK_OK(rad_memset_async(seen, 0, (int64_t)n * sizeof(unsigned), st.s)); CHECK_OK(rad_stream_sync(st.s)); CHECK_OK(rad::aql::chain_marks(st.s, seen, n)); @@ -209,9 +220,8 @@ TEST(a_chained_launch_reads_its_own_arguments_after_the_ring_has_lapped) { * a lap of card-side copies with chaining off leaves every slot with their arguments and a record * no kernel accepts; then the same copies chained and 50 us apart, the host quiet between them as * the mover is between the copies of a loan recall, each saying whether it found a record. One - * that does not would count nothing and stall the queue; the probe counts for it. The packets - * keep the packet processor's acquire here, whose 0.9 us is all that gave the slot time to arrive - * before the flush in front of the doorbell. */ + * that does not would count nothing and stall the queue; the probe counts for it. With the + * packets unacquired and with the packet processor's acquire on them. */ TEST(a_copy_issued_after_a_pause_finds_the_record_written_for_it) { if (std::strcmp(rad::device_backend_name(), "aql") != 0) SKIP("the AQL backend's argument ring"); const unsigned lap = 4096 + 64, words = 8192; @@ -222,14 +232,16 @@ TEST(a_copy_issued_after_a_pause_finds_the_record_written_for_it) { DevBuf src(bytes, RAD_MEM_DEVICE), dst(bytes, RAD_MEM_DEVICE); REQUIRE(seen && src && dst); std::vector got(words); - { - const unsigned n = 1500; + for (int fenced = 0; fenced < 2; ++fenced) { + /* Without the flush four runs in five of this many found unacquired launches that + * had read the old slot on an R9700, a few or tens of them at a time. */ + const unsigned n = fenced ? 1500 : 6000; Stream st; rad_dev_chain_infer(0); CHECK_OK(rad::aql::chain_asks(st.s, src, dst, (uint64_t)bytes, seen, lap)); CHECK_OK(rad_memset_async(seen, 0, (int64_t)words * sizeof(unsigned), st.s)); CHECK_OK(rad_stream_sync(st.s)); - rad_dev_chain_infer(1); + rad_dev_chain_infer(fenced ? 1 : 2); for (unsigned i = 0; i < n; ++i) { /* In runs of eight with a host wait between them, as a recall ends in one. */ if (i % 8 == 0) CHECK_OK(rad_stream_sync(st.s)); diff --git a/tests/shipped_plugins_test.cpp b/tests/shipped_plugins_test.cpp index 4edbcf5a..8849d289 100644 --- a/tests/shipped_plugins_test.cpp +++ b/tests/shipped_plugins_test.cpp @@ -284,3 +284,26 @@ TEST(fishomni_tags_a_reference_transcript_as_the_prompt_builder_does) { } RAD_TEST_MAIN() + +/* libr4d's rows that chain answer 2 to rad_kernel_chain: every chained form of theirs ends its wait + * in r4d_chain_acquire, which empties the scalar data cache behind the fence, and that is what + * lets an engine issue their packets without the packet processor's acquire + * (core/device/aql.cpp, "the acquire of a chained launch"). One row answering 1 keeps the acquire + * on every chained packet of an engine that loads it, so a row added with a wait of its own shows + * up here rather than as a slower step. */ +TEST(libr4ds_rows_that_chain_acquire_for_themselves) { + Registry r; + const int st = r.load_dir(dir_of("kernels"), { "libr4d", "libref" }); + if (!r.plugin("libr4d")) { + (void)st; + ARTIFACT_MISSING("libr4d under " + dir_of("kernels")); + } + int chained = 0; + for (const KernelRow& k : r.rows()) { + if (!k.chain) continue; + ++chained; + CHECK(k.chain_fenced); + if (!k.chain_fenced) fprintf(stderr, " %s chains and answers 1\n", k.info->name); + } + CHECK(chained > 0); +}