Archived
tune: measure collectives as the exchange -- per-rank operands and instances, device.p2p measured before declare, tune requests carrying their instance
This commit is contained in:
1 parent
d0c44cf5a3
commit
e9555d2bc1
7 files changed
+339
-101
No files matched your search
@@ -42,6 +42,11 @@ int rad_dev_count(void);
|
|||||||
int rad_dev_set(int device);
|
int rad_dev_set(int device);
|
||||||
int rad_dev_props(int device, RadDeviceProps* out);
|
int rad_dev_props(int device, RadDeviceProps* out);
|
||||||
int rad_dev_enable_peer(int self, int peer);
|
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_alloc(int64_t bytes, int kind);
|
||||||
void rad_dev_free(void* p, int kind);
|
void rad_dev_free(void* p, int kind);
|
||||||
|
|||||||
@@ -197,6 +197,7 @@ private:
|
|||||||
/* init() for every surviving instance, after the whole graph has resolved, so a failing init
|
/* init() for every surviving instance, after the whole graph has resolved, so a failing init
|
||||||
* cannot abort declare before the miss report is assembled. */
|
* cannot abort declare before the miss report is assembled. */
|
||||||
void finish_instances();
|
void finish_instances();
|
||||||
|
void fill_tune_instances();
|
||||||
int32_t find_kv_consumer(rad_kvgroup g);
|
int32_t find_kv_consumer(rad_kvgroup g);
|
||||||
void finish_layouts();
|
void finish_layouts();
|
||||||
void finish_kv_groups();
|
void finish_kv_groups();
|
||||||
|
|||||||
@@ -487,6 +487,7 @@ void Builder::record_tune_request(const Resolved& r) {
|
|||||||
req.geom = r.geom;
|
req.geom = r.geom;
|
||||||
req.geom_key = r.geom_key;
|
req.geom_key = r.geom_key;
|
||||||
req.info = r.row->info;
|
req.info = r.row->info;
|
||||||
|
req.instance = r.instance;
|
||||||
prog_.tune_requests.push_back(std::move(req));
|
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,
|
rad_op Builder::decl_op(const char* opname, const RadParam* p, int n_p,
|
||||||
const rad_weight* w, int n_w) {
|
const rad_weight* w, int n_w) {
|
||||||
if (finished_) return err(RAD_E_STATE, "decl_op('%s') after declare finished", opname ? opname : "");
|
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();
|
if (instances_) finish_instances();
|
||||||
|
fill_tune_instances();
|
||||||
finish_weight_uses();
|
finish_weight_uses();
|
||||||
finish_layouts();
|
finish_layouts();
|
||||||
finish_kv_groups();
|
finish_kv_groups();
|
||||||
|
|||||||
@@ -535,6 +535,7 @@ int dev_code_runs(size_t from, size_t to, int device, std::string* why) {
|
|||||||
|
|
||||||
extern "C" {
|
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) {
|
int rad_event_create_as(RadEvent* out, unsigned flags) {
|
||||||
const int s = dev()->event_create(out, flags);
|
const int s = dev()->event_create(out, flags);
|
||||||
|
|||||||
@@ -504,6 +504,13 @@ public:
|
|||||||
Geometry geom;
|
Geometry geom;
|
||||||
std::string geom_key;
|
std::string geom_key;
|
||||||
const RadKernelInfo* info = nullptr;
|
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<TuneRequest> tune_requests;
|
std::vector<TuneRequest> tune_requests;
|
||||||
|
|
||||||
|
|||||||
@@ -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:
|
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'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
|
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.
|
kernel's axes for such a comparison; it is a measurement aid, not a deployment setting.
|
||||||
|
|
||||||
|
|||||||
+290
-101
@@ -55,9 +55,16 @@ void usage() {
|
|||||||
" -h, --help\n");
|
" -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 {
|
struct DevBuf {
|
||||||
void* p = nullptr;
|
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); }
|
~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;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Every OUT and INOUT operand's bytes after one launch at `g`, which carries the candidate point as
|
/* One rank's side of one candidate's launch: the instance declare made for it, the operands it
|
||||||
* ordinary parameters. Used to compare a candidate with the defaults. */
|
* owns on its own card, its scratch, and the stream it runs on. A row that is not a collective
|
||||||
int capture_outputs(const KernelRow& row, std::vector<RadTensor>& t,
|
* has exactly one of these, as an engine step's local op does. A collective has one per rank, and
|
||||||
const Geometry& g, const RadOpSchema* schema,
|
* they are fired together: a rank that launches alone waits for a peer flag nobody raises.
|
||||||
const std::vector<OpdShape>& sh, RadStream stream,
|
*
|
||||||
std::vector<uint8_t>* out) {
|
* The instance is the reason this exists. init() is what builds the peer bundle -- the scratch and
|
||||||
RadArgs a{};
|
* the flags and the sequence counters, published to every rank and made peer-accessible -- and
|
||||||
a.t = t.data(); a.n_t = (int)t.size();
|
* RadArgs reaches the exchange through nothing else, so a tool that leaves it null cannot form a
|
||||||
a.p = g.params(); a.n_p = g.n_params();
|
* collective's launch at all. Every `ar_*` row used to end here that way: declared tunable
|
||||||
a.rank = 0; a.world_size = 1;
|
* (nblocks, nthreads, drain, acq), requested by the declared graph, and refused at the entry
|
||||||
DevBuf scratch;
|
* point before a single launch, so the served card ran the axis defaults for every all-reduce. */
|
||||||
if (row.info->scratch) {
|
struct Rank {
|
||||||
const int64_t need = row.info->scratch(&a);
|
int rank = 0, world = 1;
|
||||||
if (need > 0) {
|
void* instance = nullptr;
|
||||||
scratch.p = rad_dev_alloc(need, RAD_MEM_DEVICE);
|
RadStream stream = nullptr; /* owned by the request's StreamSet */
|
||||||
if (!scratch.p) return RAD_E_NOMEM;
|
std::vector<RadTensor> t; /* the operands, as the kernel sees them */
|
||||||
rad_memset_async(scratch.p, 0, need, stream);
|
std::vector<DevBuf> buf; /* ...and the allocations behind them */
|
||||||
a.scratch = scratch.p;
|
std::vector<std::vector<uint8_t>> host; /* the draw, only ever a copy source */
|
||||||
a.scratch_bytes = need;
|
DevBuf scratch;
|
||||||
}
|
int64_t scratch_bytes = 0;
|
||||||
}
|
};
|
||||||
if (row.info->launch(&a, stream) < 0) return RAD_E_SHAPE;
|
|
||||||
if (stream && rad_stream_sync(stream) < 0) return RAD_E_STATE;
|
|
||||||
|
|
||||||
|
/* Streams die with the request, every way out of it, skips included. */
|
||||||
|
struct StreamSet {
|
||||||
|
std::vector<RadStream> 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<Rank>& 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<Rank>& 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<Rank>& rs, const Geometry& g, int iters) {
|
||||||
|
std::vector<int> st(rs.size(), (int)RAD_E_STATE);
|
||||||
|
std::vector<std::thread> 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<Rank>& rs, const Geometry& g,
|
||||||
|
const RadOpSchema* schema, const std::vector<OpdShape>& sh,
|
||||||
|
std::vector<uint8_t>* 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
|
/* Size the destination before copying: growing it between async copies would reallocate it
|
||||||
* under copies still in flight. */
|
* under copies still in flight. */
|
||||||
size_t total = 0;
|
size_t total = 0;
|
||||||
for (size_t k = 0; k < sh.size() && k < (size_t)schema->n_operands; ++k) {
|
for (size_t k = 0; k < sh.size() && k < (size_t)schema->n_operands; ++k) {
|
||||||
const int role = schema->operands[k].role;
|
const int role = schema->operands[k].role;
|
||||||
if (role != RAD_OPD_OUT && role != RAD_OPD_INOUT) continue;
|
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));
|
const int64_t bytes = rad_dtype_bytes(sh[k].dtype, rad_numel(sh[k].shape));
|
||||||
if (bytes > 0) total += (size_t)bytes;
|
if (bytes > 0) total += (size_t)bytes;
|
||||||
}
|
}
|
||||||
@@ -160,13 +250,14 @@ int capture_outputs(const KernelRow& row, std::vector<RadTensor>& t,
|
|||||||
for (size_t k = 0; k < sh.size() && k < (size_t)schema->n_operands; ++k) {
|
for (size_t k = 0; k < sh.size() && k < (size_t)schema->n_operands; ++k) {
|
||||||
const int role = schema->operands[k].role;
|
const int role = schema->operands[k].role;
|
||||||
if (role != RAD_OPD_OUT && role != RAD_OPD_INOUT) continue;
|
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));
|
const int64_t bytes = rad_dtype_bytes(sh[k].dtype, rad_numel(sh[k].shape));
|
||||||
if (bytes <= 0) continue;
|
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;
|
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;
|
return RAD_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,43 +294,30 @@ double max_rel_diff(const std::vector<uint8_t>& a, const std::vector<uint8_t>& b
|
|||||||
return worst;
|
return worst;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Median microseconds per launch at `g` (which carries the candidate point). Negative means the
|
/* Median microseconds per iteration at `g` (which carries the candidate point): the wall time of
|
||||||
* kernel refused the shape at its entry point (spec 2.1), which is allowed: tune_valid prunes only
|
* `iters` launches on every rank, from the first launch to the last stream drained. Negative
|
||||||
* what the geometry alone can rule out. */
|
* means the kernel refused the shape at its entry point (spec 2.1), which is allowed: tune_valid
|
||||||
double time_point(const KernelRow& row, std::vector<RadTensor>& t,
|
* prunes only what the geometry alone can rule out.
|
||||||
const Geometry& g, int reps, int iters,
|
*
|
||||||
RadStream stream, double* spread_pct) {
|
* The clock stops only after the devices finish: launches are asynchronous, so without the sync
|
||||||
RadArgs a{};
|
* this would time host submission. For a collective that is doubly true -- the exchange's cost is
|
||||||
a.t = t.data(); a.n_t = (int)t.size();
|
* the wait for the peer, and the wait happens on the card, so a rank timed alone would report a
|
||||||
a.p = g.params(); a.n_p = g.n_params();
|
* local copy. */
|
||||||
a.rank = 0; a.world_size = 1;
|
double time_point(const KernelRow& row, std::vector<Rank>& rs, const Geometry& g,
|
||||||
/* Scratch must be device memory: e.g. a split-K GEMM writes its partial planes there. */
|
int reps, int iters, double* spread_pct) {
|
||||||
DevBuf scratch;
|
if (const int z = zero_scratch(rs)) return (double)z;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* A warm-up launch; its status decides whether this candidate serves the shape at all. */
|
/* A warm-up launch; its status decides whether this candidate serves the shape at all. */
|
||||||
const int s = row.info->launch(&a, stream);
|
const int w = launch_all(row, rs, g, 1);
|
||||||
if (s < 0) return (double)s;
|
if (w < 0) return (double)w;
|
||||||
if (stream && rad_stream_sync(stream) < 0) return (double)RAD_E_STATE;
|
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<double> us;
|
std::vector<double> us;
|
||||||
us.reserve((size_t)reps);
|
us.reserve((size_t)reps);
|
||||||
for (int r = 0; r < reps; ++r) {
|
for (int r = 0; r < reps; ++r) {
|
||||||
const auto t0 = std::chrono::steady_clock::now();
|
const auto t0 = std::chrono::steady_clock::now();
|
||||||
for (int i = 0; i < iters; ++i)
|
if (const int s = launch_all(row, rs, g, iters)) return (double)s;
|
||||||
if (row.info->launch(&a, stream) < 0) return (double)RAD_E_SHAPE;
|
if (const int s = sync_all(rs)) return (double)s;
|
||||||
if (stream && rad_stream_sync(stream) < 0) return (double)RAD_E_STATE;
|
|
||||||
const auto t1 = std::chrono::steady_clock::now();
|
const auto t1 = std::chrono::steady_clock::now();
|
||||||
us.push_back(std::chrono::duration<double, std::micro>(t1 - t0).count() / iters);
|
us.push_back(std::chrono::duration<double, std::micro>(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_spec = spec >= 0 ? spec : probe.draft_depth;
|
||||||
ctx.max_seqs = 256; ctx.max_ctx = meta.n_ctx_train; ctx.scope = "";
|
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()
|
/* 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
|
* runs from declare, and a collective's init is a rendezvous (libr4d's all-reduce waits for its
|
||||||
* its peers), so declaring rank 0 alone would hang. Only rank 0's program is measured. */
|
* peers), so declaring rank 0 alone would hang.
|
||||||
Program prog;
|
*
|
||||||
|
* 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<Program> progs((size_t)tp);
|
||||||
{
|
{
|
||||||
std::vector<Program> progs((size_t)tp);
|
|
||||||
std::vector<int> st((size_t)tp, 0);
|
std::vector<int> st((size_t)tp, 0);
|
||||||
std::vector<std::thread> th;
|
std::vector<std::thread> th;
|
||||||
for (int ri = 0; ri < tp; ++ri) {
|
for (int ri = 0; ri < tp; ++ri) {
|
||||||
@@ -362,7 +463,16 @@ int main(int argc, char** argv) {
|
|||||||
for (auto& t : th) t.join();
|
for (auto& t : th) t.join();
|
||||||
if (tp > 1) rad_dev_set(0);
|
if (tp > 1) rad_dev_set(0);
|
||||||
for (int ri = 0; ri < tp; ++ri) if (st[(size_t)ri] < 0) return 1;
|
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();
|
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("rad-tune %s\n", model.c_str());
|
||||||
std::printf(" machine %s\n", machine.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(" 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",
|
std::printf(" requests %zu (kernel, shape) pair(s) the declared graph reaches\n\n",
|
||||||
prog.tune_requests.size());
|
prog.tune_requests.size());
|
||||||
@@ -393,14 +504,6 @@ int main(int argc, char** argv) {
|
|||||||
int measured = 0, skipped = 0, single = 0;
|
int measured = 0, skipped = 0, single = 0;
|
||||||
std::map<std::string, int> skip_why;
|
std::map<std::string, int> 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) {
|
for (const Program::TuneRequest& tr : prog.tune_requests) {
|
||||||
const KernelRow* row = row_for(tr.plugin, tr.kernel);
|
const KernelRow* row = row_for(tr.plugin, tr.kernel);
|
||||||
if (!row || !row->info || !row->info->launch) {
|
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);
|
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.
|
/* One set of operands per rank that takes part. A collective's ranks each contribute their
|
||||||
* `store` is the host-side draw, only ever a copy source; RadTensor::data must be the
|
* own bytes on their own card -- peers write into each other's scratch, never into each
|
||||||
* device allocation (a host address would fail silently). */
|
* other's input -- so rank 1's x has to be its own allocation, and each rank needs the
|
||||||
std::vector<std::vector<uint8_t>> store(sh.size());
|
* instance its own declare made. Ranks draw different bytes on purpose: a fold of two
|
||||||
std::vector<DevBuf> dev(sh.size());
|
* identical halves would let a kernel that read its own contribution twice pass.
|
||||||
std::vector<RadTensor> t(sh.size());
|
*
|
||||||
|
* `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<Rank> rs((size_t)world);
|
||||||
|
StreamSet streams;
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
for (size_t k = 0; k < sh.size() && ok; ++k) {
|
for (int ri = 0; ri < world && ok; ++ri) {
|
||||||
const int64_t n = rad_numel(sh[k].shape);
|
Rank& r = rs[(size_t)ri];
|
||||||
const int64_t bytes = rad_dtype_bytes(sh[k].dtype, n);
|
r.rank = ri; r.world = world;
|
||||||
if (bytes <= 0) { ok = false; break; }
|
/* The instance is found by the key the cache uses, not by position: a rank that
|
||||||
kf_draw(kop, k, M, vocab_rows, rad_case_seed(1, op, 0, row->info->domain, (int)k),
|
* resolved a different kernel for this band has no peer to exchange with. A
|
||||||
&store[k]);
|
* collective cannot launch without it; a plain kernel has none, and null is exactly
|
||||||
dev[k].p = rad_dev_alloc(bytes, RAD_MEM_DEVICE);
|
* what its launch expects. */
|
||||||
if (!dev[k].p) { ok = false; break; }
|
const Program::TuneRequest* q = &tr;
|
||||||
if (rad_memcpy_async(dev[k].p, store[k].data(), bytes, stream) < 0) { ok = false; break; }
|
if (ri > 0) {
|
||||||
t[k].data = dev[k].p;
|
q = nullptr;
|
||||||
t[k].dtype = sh[k].dtype;
|
for (const auto& p : progs[(size_t)ri].tune_requests)
|
||||||
t[k].rank = (uint32_t)sh[k].shape.size();
|
if (p.kernel == tr.kernel && p.plugin == tr.plugin &&
|
||||||
for (size_t d = 0; d < sh[k].shape.size() && d < RAD_MAX_RANK; ++d)
|
p.geom_key == tr.geom_key) { q = &p; break; }
|
||||||
t[k].shape[d] = sh[k].shape[d];
|
}
|
||||||
rad_tensor_pack(&t[k]);
|
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<DevBuf>(sh.size());
|
||||||
|
r.t = std::vector<RadTensor>(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 "
|
/* Scratch sized from each rank's own arguments: a split kernel writes its partials to
|
||||||
"size)"]++; ++skipped; continue; }
|
* 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
|
/* 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`. */
|
* 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<double> best_us((size_t)n_var, -1.0), spread((size_t)n_var, 0.0);
|
std::vector<double> best_us((size_t)n_var, -1.0), spread((size_t)n_var, 0.0);
|
||||||
for (int v = 0; v < n_var; ++v) {
|
for (int v = 0; v < n_var; ++v) {
|
||||||
double sp = 0;
|
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;
|
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. */
|
* kVariantTol allows the last-bit changes of a reordered reduction. */
|
||||||
std::vector<uint8_t> ref;
|
std::vector<uint8_t> ref;
|
||||||
std::vector<double> verr((size_t)n_var, 0.0);
|
std::vector<double> 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 "
|
skip_why[tr.plugin + "/" + tr.kernel + " (the defaults refused the shape: no "
|
||||||
"reference to check candidates against)"]++;
|
"reference to check candidates against)"]++;
|
||||||
++skipped;
|
++skipped;
|
||||||
@@ -539,7 +729,7 @@ int main(int argc, char** argv) {
|
|||||||
for (int v = 1; v < n_var; ++v) {
|
for (int v = 1; v < n_var; ++v) {
|
||||||
if (best_us[(size_t)v] <= 0) continue;
|
if (best_us[(size_t)v] <= 0) continue;
|
||||||
std::vector<uint8_t> got;
|
std::vector<uint8_t> 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;
|
best_us[(size_t)v] = (double)RAD_E_STATE;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -589,8 +779,9 @@ int main(int argc, char** argv) {
|
|||||||
cache.put(tr_out);
|
cache.put(tr_out);
|
||||||
++measured;
|
++measured;
|
||||||
|
|
||||||
std::printf(" %-22s %-46s %-28s %9.3f us (%d of %lld)\n", op.c_str(),
|
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);
|
tr.geom.str().c_str(), wname.c_str(), tr_out.us, n_var, (long long)space,
|
||||||
|
scale.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dry) {
|
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"
|
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);
|
"own axis defaults are already what it runs.\n", single);
|
||||||
|
|
||||||
rad_stream_destroy(stream);
|
|
||||||
|
|
||||||
cache.set_machine(machine);
|
cache.set_machine(machine);
|
||||||
if (cache.save(save_home) < 0) return 1;
|
if (cache.save(save_home) < 0) return 1;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user