diff --git a/abi/rad_device.h b/abi/rad_device.h index 0bfbfa59..72a12511 100644 --- a/abi/rad_device.h +++ b/abi/rad_device.h @@ -42,6 +42,11 @@ int rad_dev_count(void); int rad_dev_set(int device); int rad_dev_props(int device, RadDeviceProps* out); int rad_dev_enable_peer(int self, int peer); +/* Whether the driver reports that `self` can reach `peer`'s memory, 1 or 0, asked before anything + * is enabled: this is the fact collectives constrain (rad_types.h's RAD_FACT_P2P), so a tool that + * declares a multi-rank graph must measure it and set the fact as Engine does. Negative on a + * device error. */ +int rad_dev_peer_access(int self, int peer); void* rad_dev_alloc(int64_t bytes, int kind); void rad_dev_free(void* p, int kind); diff --git a/core/build/rad_build.h b/core/build/rad_build.h index e606e7e1..ae7d954c 100644 --- a/core/build/rad_build.h +++ b/core/build/rad_build.h @@ -197,6 +197,7 @@ private: /* init() for every surviving instance, after the whole graph has resolved, so a failing init * cannot abort declare before the miss report is assembled. */ void finish_instances(); + void fill_tune_instances(); int32_t find_kv_consumer(rad_kvgroup g); void finish_layouts(); void finish_kv_groups(); diff --git a/core/build/rad_builder.cpp b/core/build/rad_builder.cpp index 2fa9d950..ad8c7f8e 100644 --- a/core/build/rad_builder.cpp +++ b/core/build/rad_builder.cpp @@ -487,6 +487,7 @@ void Builder::record_tune_request(const Resolved& r) { req.geom = r.geom; req.geom_key = r.geom_key; req.info = r.row->info; + req.instance = r.instance; prog_.tune_requests.push_back(std::move(req)); } @@ -597,6 +598,29 @@ void Builder::finish_instances() { } } } + +/* The instances are made in finish_instances(), after record_tune_request ran, so the requests + * now carry the instance their kernel will launch with. A collective's launch cannot be formed + * without it -- the peer bundle (scratch, flags, sequence counters) is what init() builds and + * RadArgs reaches the exchange through nothing else -- so the tuner forms its launches from this. + * Keyed exactly as record_tune_request keyed. */ +void Builder::fill_tune_instances() { + for (size_t i = 1; i < prog_.ops.size(); ++i) { + const OpInfo& oi = prog_.ops[i]; + for (const Band& b : oi.bands) + for (int d = 0; d < RAD_N_DOMAINS; ++d) { + const Resolved& r = b.dom[d]; + if (!r || !r.row || !r.row->info) continue; + const std::string key = r.row->info->name + std::string("\x1f") + r.row->plugin + + std::string("\x1f") + r.geom_key; + for (auto& t : prog_.tune_requests) + if (t.kernel + std::string("\x1f") + t.plugin + std::string("\x1f") + + t.geom_key == key) + t.instance = r.instance; + } + } +} + rad_op Builder::decl_op(const char* opname, const RadParam* p, int n_p, const rad_weight* w, int n_w) { if (finished_) return err(RAD_E_STATE, "decl_op('%s') after declare finished", opname ? opname : ""); @@ -2380,6 +2404,7 @@ int Builder::finish() { } if (instances_) finish_instances(); + fill_tune_instances(); finish_weight_uses(); finish_layouts(); finish_kv_groups(); diff --git a/core/device/dispatch.cpp b/core/device/dispatch.cpp index 16b20d6f..8855e3f9 100644 --- a/core/device/dispatch.cpp +++ b/core/device/dispatch.cpp @@ -535,6 +535,7 @@ int dev_code_runs(size_t from, size_t to, int device, std::string* why) { extern "C" { +int rad_dev_peer_access(int self, int peer) { return rad::dev_peer_access(self, peer); } int rad_event_create_as(RadEvent* out, unsigned flags) { const int s = dev()->event_create(out, flags); diff --git a/core/rad_core.h b/core/rad_core.h index 8b6021e9..dd5fb92c 100644 --- a/core/rad_core.h +++ b/core/rad_core.h @@ -504,6 +504,13 @@ public: Geometry geom; std::string geom_key; const RadKernelInfo* info = nullptr; + /* The instance init() produced for this instantiation, null for a kernel with no init. + * A collective's launch cannot be formed without it: the peer bundle -- the scratch, the + * flags and the sequence counters, published to every rank and made peer-accessible at + * init -- is what makes the exchange addressable, and RadArgs carries no other route to + * it. The instance belongs to the rank this Program was declared for (Program::ctx), and + * the Registry that made it must outlive the launch (spec §1, §15). */ + void* instance = nullptr; }; std::vector tune_requests; diff --git a/docs/TOOLS.md b/docs/TOOLS.md index 9948ea40..08d11452 100644 --- a/docs/TOOLS.md +++ b/docs/TOOLS.md @@ -655,6 +655,16 @@ Declare at the shapes you serve: At `--tp > 1` every rank declares at once, each on its own thread and device, as the engine does: a collective's `init` is a rendezvous, so declaring rank 0 alone at world size 2 hangs. +A collective is then measured as the exchange, not as one rank's copy of it. Each rank gets its own +operands -- drawn with its own seed, so a fold of two identical halves cannot pass a correctness +check -- its own scratch, its own stream, and the instance its own declare made: `init` is what +publishes the peer bundle (scratch, flags, sequence counters) and a launch with no instance is +refused before it starts. The candidate's launches are fired on every rank at once and the clock +stops when the last stream drains, so the reported time is the round trip through the peer, which +is what the served step pays; timing rank 0 alone would time a local copy. Candidates are still +judged against the defaults on rank 0's output. A row that is not collective runs on rank 0 alone, +as it always did. + Before trusting a cache, check that the tuned variants beat the defaults. `RADIANCE_TUNE` pins a kernel's axes for such a comparison; it is a measurement aid, not a deployment setting. diff --git a/tools/rad_tune.cpp b/tools/rad_tune.cpp index f478b47f..d74bf679 100644 --- a/tools/rad_tune.cpp +++ b/tools/rad_tune.cpp @@ -55,9 +55,16 @@ void usage() { " -h, --help\n"); } -/* A device allocation that frees itself, because the timing path has several ways out. */ +/* A device allocation that frees itself, because the timing path has several ways out. Neither + * copyable nor movable: a rank holds a vector of these, and a second handle to the same + * allocation would free the buffer out from under a launch still reading it. */ struct DevBuf { void* p = nullptr; + DevBuf() = default; + DevBuf(const DevBuf&) = delete; + DevBuf& operator=(const DevBuf&) = delete; + DevBuf(DevBuf&&) = delete; + DevBuf& operator=(DevBuf&&) = delete; ~DevBuf() { if (p) rad_dev_free(p, RAD_MEM_DEVICE); } }; @@ -121,37 +128,120 @@ tune_candidates(const RadKernelInfo* k, const Geometry& g, int64_t* space_size, return out; } -/* Every OUT and INOUT operand's bytes after one launch at `g`, which carries the candidate point as - * ordinary parameters. Used to compare a candidate with the defaults. */ -int capture_outputs(const KernelRow& row, std::vector& t, - const Geometry& g, const RadOpSchema* schema, - const std::vector& sh, RadStream stream, - std::vector* out) { - RadArgs a{}; - a.t = t.data(); a.n_t = (int)t.size(); - a.p = g.params(); a.n_p = g.n_params(); - a.rank = 0; a.world_size = 1; - DevBuf scratch; - if (row.info->scratch) { - const int64_t need = row.info->scratch(&a); - if (need > 0) { - scratch.p = rad_dev_alloc(need, RAD_MEM_DEVICE); - if (!scratch.p) return RAD_E_NOMEM; - rad_memset_async(scratch.p, 0, need, stream); - a.scratch = scratch.p; - a.scratch_bytes = need; - } - } - if (row.info->launch(&a, stream) < 0) return RAD_E_SHAPE; - if (stream && rad_stream_sync(stream) < 0) return RAD_E_STATE; +/* One rank's side of one candidate's launch: the instance declare made for it, the operands it + * owns on its own card, its scratch, and the stream it runs on. A row that is not a collective + * has exactly one of these, as an engine step's local op does. A collective has one per rank, and + * they are fired together: a rank that launches alone waits for a peer flag nobody raises. + * + * The instance is the reason this exists. init() is what builds the peer bundle -- the scratch and + * the flags and the sequence counters, published to every rank and made peer-accessible -- and + * RadArgs reaches the exchange through nothing else, so a tool that leaves it null cannot form a + * collective's launch at all. Every `ar_*` row used to end here that way: declared tunable + * (nblocks, nthreads, drain, acq), requested by the declared graph, and refused at the entry + * point before a single launch, so the served card ran the axis defaults for every all-reduce. */ +struct Rank { + int rank = 0, world = 1; + void* instance = nullptr; + RadStream stream = nullptr; /* owned by the request's StreamSet */ + std::vector t; /* the operands, as the kernel sees them */ + std::vector buf; /* ...and the allocations behind them */ + std::vector> host; /* the draw, only ever a copy source */ + DevBuf scratch; + int64_t scratch_bytes = 0; +}; +/* Streams die with the request, every way out of it, skips included. */ +struct StreamSet { + std::vector s; + ~StreamSet() { for (RadStream x : s) if (x) rad_stream_destroy(x); } +}; + +/* The RadArgs a rank's launch takes. The candidate's axes ride in `g` exactly as at issue, so the + * kernel reads `sk` with rad_args_geti() like `M`. */ +void fill_args(const KernelRow& row, Rank& r, const Geometry& g, RadArgs* a) { + std::memset(a, 0, sizeof(*a)); + a->t = r.t.data(); a->n_t = (int)r.t.size(); + a->p = g.params(); a->n_p = g.n_params(); + a->instance = r.instance; + a->rank = r.rank; + a->world_size = r.world; + if (r.scratch.p) { a->scratch = r.scratch.p; a->scratch_bytes = r.scratch_bytes; } +} + +/* Every rank's stream drained. A stream's device has to be current to sync it. */ +int sync_all(std::vector& rs) { + int st = RAD_OK; + for (Rank& r : rs) { + if (!r.stream) continue; + if (rad_dev_set(r.rank) < 0) return RAD_E_DEVICE; + if (rad_stream_sync(r.stream) < 0) st = RAD_E_STATE; + } + return st; +} + +/* Scratch zeroed before a candidate starts, outside the timed region: a candidate must not be + * timed -- or judged -- on the leftovers of the one before it. */ +int zero_scratch(std::vector& rs) { + for (Rank& r : rs) { + if (!r.scratch.p) continue; + if (rad_dev_set(r.rank) < 0) return RAD_E_DEVICE; + if (rad_memset_async(r.scratch.p, 0, (uint64_t)r.scratch_bytes, r.stream) < 0) + return RAD_E_STATE; + } + return RAD_OK; +} + +/* `iters` launches on every rank, from one thread each: the exchange only pipelines once both + * cards have work queued, so enqueuing rank 1's whole batch after rank 0's would charge every + * iteration for the enqueue of the batch, not just the first. + * + * A refusal comes back before anything is waited on. It is symmetric by construction -- every + * rank resolved the same kernel for the same geometry, and the launch's checks read only the + * geometry and the instance -- so a refusal means no rank queued work and there is nothing to + * drain. Were it ever asymmetric, the peers would be left spinning on a flag; that is why the + * caller treats any refusal as fatal for the candidate instead of retrying it. */ +int launch_all(const KernelRow& row, std::vector& rs, const Geometry& g, int iters) { + std::vector st(rs.size(), (int)RAD_E_STATE); + std::vector th; + th.reserve(rs.size()); + for (size_t i = 0; i < rs.size(); ++i) + th.emplace_back([&, i] { + Rank& r = rs[i]; + if (rad_dev_set(r.rank) < 0) { st[i] = RAD_E_DEVICE; return; } + RadArgs a; + fill_args(row, r, g, &a); + for (int k = 0; k < iters; ++k) { + const int s = row.info->launch(&a, r.stream); + if (s < 0) { st[i] = s; return; } + } + st[i] = RAD_OK; + }); + for (std::thread& t : th) t.join(); + for (int s : st) if (s < 0) return s; + return RAD_OK; +} + +/* Rank 0's OUT and INOUT bytes after one launch at `g`, which carries the candidate point as + * ordinary parameters. Every candidate saw the same bytes on every rank, so a variant that + * reorders a reduction, drops a peer's term, or writes the wrong slice differs from the defaults + * here. */ +int capture_outputs(const KernelRow& row, std::vector& rs, const Geometry& g, + const RadOpSchema* schema, const std::vector& sh, + std::vector* out) { + if (const int z = zero_scratch(rs)) return z; + const int w = launch_all(row, rs, g, 1); + if (w < 0) return w; + if (const int s = sync_all(rs)) return s; + + Rank& r = rs[0]; + if (rad_dev_set(r.rank) < 0) return RAD_E_DEVICE; /* Size the destination before copying: growing it between async copies would reallocate it * under copies still in flight. */ size_t total = 0; for (size_t k = 0; k < sh.size() && k < (size_t)schema->n_operands; ++k) { const int role = schema->operands[k].role; if (role != RAD_OPD_OUT && role != RAD_OPD_INOUT) continue; - if (!t[k].data) continue; + if (!r.t[k].data) continue; const int64_t bytes = rad_dtype_bytes(sh[k].dtype, rad_numel(sh[k].shape)); if (bytes > 0) total += (size_t)bytes; } @@ -160,13 +250,14 @@ int capture_outputs(const KernelRow& row, std::vector& t, for (size_t k = 0; k < sh.size() && k < (size_t)schema->n_operands; ++k) { const int role = schema->operands[k].role; if (role != RAD_OPD_OUT && role != RAD_OPD_INOUT) continue; - if (!t[k].data) continue; + if (!r.t[k].data) continue; const int64_t bytes = rad_dtype_bytes(sh[k].dtype, rad_numel(sh[k].shape)); if (bytes <= 0) continue; - if (rad_memcpy_async(out->data() + at, t[k].data, bytes, stream) < 0) return RAD_E_STATE; + if (rad_memcpy_async(out->data() + at, r.t[k].data, bytes, r.stream) < 0) + return RAD_E_STATE; at += (size_t)bytes; } - if (stream && rad_stream_sync(stream) < 0) return RAD_E_STATE; + if (rad_stream_sync(r.stream) < 0) return RAD_E_STATE; return RAD_OK; } @@ -203,43 +294,30 @@ double max_rel_diff(const std::vector& a, const std::vector& b return worst; } -/* Median microseconds per launch at `g` (which carries the candidate point). Negative means the - * kernel refused the shape at its entry point (spec 2.1), which is allowed: tune_valid prunes only - * what the geometry alone can rule out. */ -double time_point(const KernelRow& row, std::vector& t, - const Geometry& g, int reps, int iters, - RadStream stream, double* spread_pct) { - RadArgs a{}; - a.t = t.data(); a.n_t = (int)t.size(); - a.p = g.params(); a.n_p = g.n_params(); - a.rank = 0; a.world_size = 1; - /* Scratch must be device memory: e.g. a split-K GEMM writes its partial planes there. */ - DevBuf scratch; - if (row.info->scratch) { - const int64_t need = row.info->scratch(&a); - if (need > 0) { - scratch.p = rad_dev_alloc(need, RAD_MEM_DEVICE); - if (!scratch.p) return (double)RAD_E_NOMEM; - rad_memset_async(scratch.p, 0, need, stream); - a.scratch = scratch.p; - a.scratch_bytes = need; - } - } +/* Median microseconds per iteration at `g` (which carries the candidate point): the wall time of + * `iters` launches on every rank, from the first launch to the last stream drained. Negative + * means the kernel refused the shape at its entry point (spec 2.1), which is allowed: tune_valid + * prunes only what the geometry alone can rule out. + * + * The clock stops only after the devices finish: launches are asynchronous, so without the sync + * this would time host submission. For a collective that is doubly true -- the exchange's cost is + * the wait for the peer, and the wait happens on the card, so a rank timed alone would report a + * local copy. */ +double time_point(const KernelRow& row, std::vector& rs, const Geometry& g, + int reps, int iters, double* spread_pct) { + if (const int z = zero_scratch(rs)) return (double)z; /* A warm-up launch; its status decides whether this candidate serves the shape at all. */ - const int s = row.info->launch(&a, stream); - if (s < 0) return (double)s; - if (stream && rad_stream_sync(stream) < 0) return (double)RAD_E_STATE; + const int w = launch_all(row, rs, g, 1); + if (w < 0) return (double)w; + if (const int s = sync_all(rs)) return (double)s; - /* Stop the clock only after the device finishes: launches are asynchronous, so without the - * sync this would time host submission only. */ std::vector us; us.reserve((size_t)reps); for (int r = 0; r < reps; ++r) { const auto t0 = std::chrono::steady_clock::now(); - for (int i = 0; i < iters; ++i) - if (row.info->launch(&a, stream) < 0) return (double)RAD_E_SHAPE; - if (stream && rad_stream_sync(stream) < 0) return (double)RAD_E_STATE; + if (const int s = launch_all(row, rs, g, iters)) return (double)s; + if (const int s = sync_all(rs)) return (double)s; const auto t1 = std::chrono::steady_clock::now(); us.push_back(std::chrono::duration(t1 - t0).count() / iters); } @@ -337,12 +415,35 @@ int main(int argc, char** argv) { ctx.max_spec = spec >= 0 ? spec : probe.draft_depth; ctx.max_seqs = 256; ctx.max_ctx = meta.n_ctx_train; ctx.scope = ""; + /* Peer access, measured before selection, as Engine does: collectives constrain device.p2p + * (abi/rad_types.h), and with the fact unset neither the peer-to-peer row nor its host-memory + * twin can match, so every collective resolves to nothing -- silently, with no tune request + * for it ever reaching the loop below. Every ordered pair is asked, since access is granted + * one way at a time. */ + long long p2p = 1; + for (int i = 0; i < tp; ++i) + for (int j = 0; j < tp; ++j) { + if (i == j) continue; + const int a = rad_dev_peer_access(i, j); + if (a < 0) { + std::fprintf(stderr, "rad-tune: could not ask whether card %d can reach card %d's " + "memory: %s\n", i, j, rad_dev_last_error()); + return 1; + } + if (a == 0) p2p = 0; + } + rad_tools_registry().set_device_fact(RAD_FACT_P2P, p2p); + const char* p2p_note = p2p ? "peer to peer" : "the collectives run through pinned host memory"; + /* Every rank declares concurrently on its own thread and device, as Engine does: kernel init() - * runs from declare, and a collective's init is a rendezvous (libr4d's all-reduce waits for - * its peers), so declaring rank 0 alone would hang. Only rank 0's program is measured. */ - Program prog; + * runs from declare, and a collective's init is a rendezvous (libr4d's all-reduce waits for its + * peers), so declaring rank 0 alone would hang. + * + * All the programs stay alive, not only rank 0's. A collective's launch needs the instance each + * rank's own declare made, and an instance is reachable only through the program that declared + * it, so rank 0's program is the one measured while the others supply the peers. */ + std::vector progs((size_t)tp); { - std::vector progs((size_t)tp); std::vector st((size_t)tp, 0); std::vector th; for (int ri = 0; ri < tp; ++ri) { @@ -362,7 +463,16 @@ int main(int argc, char** argv) { for (auto& t : th) t.join(); if (tp > 1) rad_dev_set(0); for (int ri = 0; ri < tp; ++ri) if (st[(size_t)ri] < 0) return 1; - prog = std::move(progs[0]); + } + Program& prog = progs[0]; + + /* Name what resolved to nothing, so a hole is a printed line instead of a tune request that + * never appears: the engine's declare counts the misses, and this is the count's detail. */ + if (!prog.misses.empty()) { + RadDeviceProps dp{}; + const bool host_backend = rad_dev_props(0, &dp) >= 0 && dp.is_host_backend; + for (const Program::Miss* m : prog.device_holes(host_backend)) + std::printf(" unresolved %s\n", m->line().c_str()); } if (machine.empty()) machine = rad_machine_key(); @@ -378,6 +488,7 @@ int main(int argc, char** argv) { std::printf("rad-tune %s\n", model.c_str()); std::printf(" machine %s\n", machine.c_str()); + if (tp > 1) std::printf(" p2p %s\n", p2p_note); std::printf(" cache %s (%zu existing row(s))\n", src.c_str(), before); std::printf(" requests %zu (kernel, shape) pair(s) the declared graph reaches\n\n", prog.tune_requests.size()); @@ -393,14 +504,6 @@ int main(int argc, char** argv) { int measured = 0, skipped = 0, single = 0; std::map skip_why; - /* One explicit stream for the run; timing on the null stream would measure host submission. - * Without ROCm the device layer is a synchronous host stub, so the same path works. */ - RadStream stream = nullptr; - if (rad_stream_create(&stream, 0) < 0) { - std::fprintf(stderr, "rad-tune: could not create a stream: %s\n", rad_dev_last_error()); - return 1; - } - for (const Program::TuneRequest& tr : prog.tune_requests) { const KernelRow* row = row_for(tr.plugin, tr.kernel); if (!row || !row->info || !row->info->launch) { @@ -480,31 +583,118 @@ int main(int argc, char** argv) { } const int64_t vocab_rows = kf_vocab_rows(kop); - /* One set of device buffers shared by all candidates, so they all see the same bytes. - * `store` is the host-side draw, only ever a copy source; RadTensor::data must be the - * device allocation (a host address would fail silently). */ - std::vector> store(sh.size()); - std::vector dev(sh.size()); - std::vector t(sh.size()); + /* One set of operands per rank that takes part. A collective's ranks each contribute their + * own bytes on their own card -- peers write into each other's scratch, never into each + * other's input -- so rank 1's x has to be its own allocation, and each rank needs the + * instance its own declare made. Ranks draw different bytes on purpose: a fold of two + * identical halves would let a kernel that read its own contribution twice pass. + * + * `rs` is sized up front and never grown: DevBuf frees what it points at, so moving one + * into new storage would leave the old handle to free the buffer as well. */ + const int world = row->collective && tp > 1 ? tp : 1; + std::string scale; + if (world > 1) scale = ", " + std::to_string(world) + " ranks"; + std::vector rs((size_t)world); + StreamSet streams; bool ok = true; - for (size_t k = 0; k < sh.size() && ok; ++k) { - const int64_t n = rad_numel(sh[k].shape); - const int64_t bytes = rad_dtype_bytes(sh[k].dtype, n); - if (bytes <= 0) { ok = false; break; } - kf_draw(kop, k, M, vocab_rows, rad_case_seed(1, op, 0, row->info->domain, (int)k), - &store[k]); - dev[k].p = rad_dev_alloc(bytes, RAD_MEM_DEVICE); - if (!dev[k].p) { ok = false; break; } - if (rad_memcpy_async(dev[k].p, store[k].data(), bytes, stream) < 0) { ok = false; break; } - t[k].data = dev[k].p; - t[k].dtype = sh[k].dtype; - t[k].rank = (uint32_t)sh[k].shape.size(); - for (size_t d = 0; d < sh[k].shape.size() && d < RAD_MAX_RANK; ++d) - t[k].shape[d] = sh[k].shape[d]; - rad_tensor_pack(&t[k]); + for (int ri = 0; ri < world && ok; ++ri) { + Rank& r = rs[(size_t)ri]; + r.rank = ri; r.world = world; + /* The instance is found by the key the cache uses, not by position: a rank that + * resolved a different kernel for this band has no peer to exchange with. A + * collective cannot launch without it; a plain kernel has none, and null is exactly + * what its launch expects. */ + const Program::TuneRequest* q = &tr; + if (ri > 0) { + q = nullptr; + for (const auto& p : progs[(size_t)ri].tune_requests) + if (p.kernel == tr.kernel && p.plugin == tr.plugin && + p.geom_key == tr.geom_key) { q = &p; break; } + } + if (world > 1 && (!q || !q->instance)) { + skip_why[op + " (rank " + std::to_string(ri) + " declared no instance for this " + "instantiation: no exchange to launch)"]++; + ++skipped; ok = false; break; + } + r.instance = q ? q->instance : nullptr; + if (rad_dev_set(ri) < 0) { + skip_why[op + " (no device to put rank " + std::to_string(ri) + " on)"]++; + ++skipped; ok = false; break; + } + /* An explicit stream per rank; timing on the null stream would measure host + * submission. Without ROCm the device layer is a synchronous host stub, and the + * same path still works. */ + RadStream s = nullptr; + if (rad_stream_create(&s, 0) < 0) { + std::fprintf(stderr, "rad-tune: could not create a stream for rank %d: %s\n", ri, + rad_dev_last_error()); + return 1; + } + streams.s.push_back(s); + r.stream = s; + /* Inputs come from kf_draw, the same draw rad-kbench checks with, so each operand + * stays in the domain its recipe states (constant `seqused`, cumulative lengths, + * distinct scatter destinations, in-range block tables). */ + r.host.resize(sh.size()); + r.buf = std::vector(sh.size()); + r.t = std::vector(sh.size()); + for (size_t k = 0; k < sh.size(); ++k) { + /* An absent operand (attn's descales and cu_seqlens) is a zeroed tensor: no + * draw, no allocation, data null -- what kbench passes and the kernel checks. */ + if (sh[k].absent) continue; + const int64_t bytes = rad_dtype_bytes(sh[k].dtype, rad_numel(sh[k].shape)); + if (bytes <= 0) { + skip_why[op + " (an operand has a plugin-private dtype the core cannot " + "size)"]++; + ++skipped; ok = false; break; + } + kf_draw(kop, k, M, vocab_rows, + rad_case_seed(1, op, ri, row->info->domain, (int)k), &r.host[k]); + r.buf[k].p = rad_dev_alloc(bytes, RAD_MEM_DEVICE); + if (!r.buf[k].p) { + skip_why[op + " (no device memory for an operand on rank " + + std::to_string(ri) + ")"]++; + ++skipped; ok = false; break; + } + if (rad_memcpy_async(r.buf[k].p, r.host[k].data(), bytes, r.stream) < 0) { + skip_why[op + " (the operands would not copy onto rank " + + std::to_string(ri) + ")"]++; + ++skipped; ok = false; break; + } + /* RadTensor::data must be the device allocation; a host address fails silently. */ + RadTensor& t = r.t[k]; + t = RadTensor{}; + t.data = r.buf[k].p; + t.dtype = sh[k].dtype; + t.rank = (uint32_t)sh[k].shape.size(); + for (size_t d = 0; d < sh[k].shape.size() && d < RAD_MAX_RANK; ++d) + t.shape[d] = sh[k].shape[d]; + rad_tensor_pack(&t); + } } - if (!ok) { skip_why[op + " (an operand has a plugin-private dtype the core cannot " - "size)"]++; ++skipped; continue; } + /* Scratch sized from each rank's own arguments: a split kernel writes its partials to + * device memory, and every card needs its own. */ + if (ok && row->info->scratch) { + for (Rank& r : rs) { + if (rad_dev_set(r.rank) < 0) { + skip_why[op + " (no device to size scratch on for rank " + + std::to_string(r.rank) + ")"]++; + ++skipped; ok = false; break; + } + RadArgs a; + fill_args(*row, r, tr.geom, &a); + const int64_t need = row->info->scratch(&a); + if (need <= 0) continue; + r.scratch.p = rad_dev_alloc(need, RAD_MEM_DEVICE); + if (!r.scratch.p) { + skip_why[op + " (no device memory for the scratch it asks for)"]++; + ++skipped; ok = false; break; + } + r.scratch_bytes = need; + } + } + if (!ok) continue; /* the reason is already counted */ + if (world > 1) rad_dev_set(0); /* One geometry per candidate, built up front. As at issue, the chosen values are appended * to the resolved geometry, so a launch reads `sk` via rad_args_geti() like `M`. */ @@ -518,7 +708,7 @@ int main(int argc, char** argv) { std::vector best_us((size_t)n_var, -1.0), spread((size_t)n_var, 0.0); for (int v = 0; v < n_var; ++v) { double sp = 0; - best_us[(size_t)v] = time_point(*row, t, gv[(size_t)v], reps, iters, stream, &sp); + best_us[(size_t)v] = time_point(*row, rs, gv[(size_t)v], reps, iters, &sp); spread[(size_t)v] = sp; } @@ -530,7 +720,7 @@ int main(int argc, char** argv) { * kVariantTol allows the last-bit changes of a reordered reduction. */ std::vector ref; std::vector verr((size_t)n_var, 0.0); - if (best_us[0] <= 0 || capture_outputs(*row, t, gv[0], schema, sh, stream, &ref) < 0) { + if (best_us[0] <= 0 || capture_outputs(*row, rs, gv[0], schema, sh, &ref) < 0) { skip_why[tr.plugin + "/" + tr.kernel + " (the defaults refused the shape: no " "reference to check candidates against)"]++; ++skipped; @@ -539,7 +729,7 @@ int main(int argc, char** argv) { for (int v = 1; v < n_var; ++v) { if (best_us[(size_t)v] <= 0) continue; std::vector got; - if (capture_outputs(*row, t, gv[(size_t)v], schema, sh, stream, &got) < 0) { + if (capture_outputs(*row, rs, gv[(size_t)v], schema, sh, &got) < 0) { best_us[(size_t)v] = (double)RAD_E_STATE; continue; } @@ -589,8 +779,9 @@ int main(int argc, char** argv) { cache.put(tr_out); ++measured; - std::printf(" %-22s %-46s %-28s %9.3f us (%d of %lld)\n", op.c_str(), - tr.geom.str().c_str(), wname.c_str(), tr_out.us, n_var, (long long)space); + std::printf(" %-22s %-46s %-28s %9.3f us (%d of %lld%s)\n", op.c_str(), + tr.geom.str().c_str(), wname.c_str(), tr_out.us, n_var, (long long)space, + scale.c_str()); } if (dry) { @@ -607,8 +798,6 @@ int main(int argc, char** argv) { std::printf("\n%d request(s) had one point or none: nothing to choose, and the kernel's\n" "own axis defaults are already what it runs.\n", single); - rad_stream_destroy(stream); - cache.set_machine(machine); if (cache.save(save_home) < 0) return 1;