Archived
radiance 1.0.11
- The n-gram embedding table (Qwen3.8-Flash-Next's per-layer embedding) can be gathered on the card. libr4d gains a row for embed_lookup_q whose output is the host gather's to the bit, and --ngram-placement uses it: ram keeps a pinned copy in RAM that the cards read in place over the link, so a step has no host work in it; vram puts the whole table on every card that holds it; disk reads it from the container on the host, as before. auto takes the first of the three that holds the table and prints why it passed over the others. Without a device kernel for the gather, ram is the locked copy the host gather reads and vram is refused, as in 1.0.10. - The pinned copy is not charged to RLIMIT_MEMLOCK; the host gather's locked copy still is, so --ulimit memlock=-1 matters only on a run without the cards. - The idea and a first kernel for the device gather came from Code000.
This commit is contained in:
1 parent
e466fa07e2
commit
469441d90f
24 files changed
+848
-247
No files matched your search
+7
-5
@@ -62,11 +62,13 @@ static const Flag g_flags[] = {
|
||||
" auto keeps it on the card unless the KV cache could then not\n"
|
||||
" hold one --max-model-len sequence. disk is refused: the lookup\n"
|
||||
" writes the card's activations, which no host read can reach" },
|
||||
{ "--ngram-placement", "WHERE", "auto (default) | ram | disk: where an n-gram embedding table\n"
|
||||
" lives (Qwen3.8-Flash-Next: 47.7 GiB). disk reads its rows from\n"
|
||||
" the container; ram copies it into locked memory at load; auto\n"
|
||||
" takes ram when free memory, the memlock limit and the cgroup\n"
|
||||
" allow it. vram is refused: only a host kernel reads the table" },
|
||||
{ "--ngram-placement", "WHERE", "auto (default) | vram | ram | disk: where an n-gram\n"
|
||||
" embedding table lives (Qwen3.8-Flash-Next: 47.7 GiB).\n"
|
||||
" vram puts all of it on every card; ram copies it into\n"
|
||||
" host memory at load, pinned for the cards to read\n"
|
||||
" (locked, where only a host kernel reads it); disk reads\n"
|
||||
" its rows from the container on the host. auto takes the\n"
|
||||
" first of the three that holds it" },
|
||||
{ "--weights-disk-tier", nullptr, "read weights that fit neither VRAM nor the host pool from the\n"
|
||||
" container on disk (O_DIRECT); a routed expert is read a layer\n"
|
||||
" at a time, so a step is paced by the drive" },
|
||||
|
||||
@@ -82,6 +82,8 @@ Engine::~Engine() {
|
||||
ranks_.clear();
|
||||
for (TableCopy& c : table_copies_) table_copy_free(&c);
|
||||
table_copies_.clear();
|
||||
for (void* p : card_copies_) rad_dev_free(p, RAD_MEM_HOST_MAPPED);
|
||||
card_copies_.clear();
|
||||
rad_tools_registry().close();
|
||||
}
|
||||
|
||||
|
||||
+12
-6
@@ -122,13 +122,19 @@ private:
|
||||
* (engine_bringup.cpp says how auto chooses). */
|
||||
int resolve_table_placement(const std::vector<VramFacts>& facts,
|
||||
const std::vector<int64_t>& emb_bytes, int64_t kv_floor,
|
||||
int64_t ngram_bytes, bool ngram_device);
|
||||
/* --ngram-placement ram: the locked copy of the container's [src, src + n), file offset `foff`,
|
||||
* made once and shared by every rank. */
|
||||
int lock_table_copy(const uint8_t* src, int64_t n, int64_t foff, void** out, std::string* why);
|
||||
/* The copies made, each [base, base + bytes), and the container address each one copies.
|
||||
* Unlocked and unmapped in ~Engine after the ranks that read them are gone. */
|
||||
int64_t ngram_bytes, bool ngram_device, bool ngram_host);
|
||||
/* --ngram-placement ram: the copy of the container's [src, src + n), file offset `foff`, made
|
||||
* once and shared by every rank -- locked for the host gather, or pinned for a device kernel
|
||||
* to read over the link (`card`). */
|
||||
int copy_table(const uint8_t* src, int64_t n, int64_t foff, bool card, void** out,
|
||||
std::string* why);
|
||||
/* An auto-chosen pinned copy that could not be made: every rank's n-gram gather to the host. */
|
||||
bool ngram_gather_to_host();
|
||||
/* The copies made: the locked ones, each [base, base + bytes); the pinned ones, rad_dev_alloc
|
||||
* RAD_MEM_HOST_MAPPED ranges; and the container address each one copies. Freed in ~Engine
|
||||
* after the ranks that read them are gone. */
|
||||
std::vector<TableCopy> table_copies_;
|
||||
std::vector<void*> card_copies_;
|
||||
std::vector<std::pair<const void*, void*>> table_copy_of_;
|
||||
int load_weights();
|
||||
int configure_kv();
|
||||
|
||||
+187
-70
@@ -56,9 +56,10 @@ namespace rad {
|
||||
* every one gets a line of its own carrying the geometry and the constraint that refused it.
|
||||
*
|
||||
* A DEVICE miss whose op DID resolve on the host is not a hole at all. The planner puts that op's
|
||||
* arithmetic on the host and its weights in the container's mapping, which is the whole of how
|
||||
* `embed_lookup_q` serves a table too large to copy into VRAM. Reporting that as fatal produces
|
||||
* an error nobody believes, and an error nobody believes is how a real one goes unread.
|
||||
* arithmetic on the host and its weights in the container's mapping, which is how `embed_lookup_q`
|
||||
* serves a table too large to copy into VRAM when no device kernel for it is loaded. Reporting
|
||||
* that as fatal produces an error nobody believes, and an error nobody believes is how a real one
|
||||
* goes unread.
|
||||
*
|
||||
* A HOST miss removes ONE placement option for ONE op, and for a kernel library written for a GPU
|
||||
* it is the ordinary case rather than news: most ops have no host implementation and never will.
|
||||
@@ -286,13 +287,15 @@ static bool cgroup_room(const std::string& dir, bool v2, int64_t* room) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* WHETHER `need` MORE BYTES CAN BE LOCKED IN HOST MEMORY, after `still` more that this process
|
||||
* will allocate before the copy is made, or why not. Each limit is one a copy would otherwise meet
|
||||
* part way through: RLIMIT_MEMLOCK (unless the process holds CAP_IPC_LOCK), the memory the kernel
|
||||
* reports available, and every memory cgroup from the process's own up to the root it can see,
|
||||
* which is the limit Docker's --memory sets and the kernel enforces by killing the process. A
|
||||
* limit that cannot be read is not one; a limit that can is never guessed at. */
|
||||
static bool lockable_room(int64_t need, int64_t still, std::string* why) {
|
||||
/* WHETHER `need` MORE BYTES CAN BE HELD IN HOST MEMORY, after `still` more that this process will
|
||||
* allocate before the copy is made, or why not. Each limit is one a copy would otherwise meet part
|
||||
* way through: the memory the kernel reports available, every memory cgroup from the process's own
|
||||
* up to the root it can see, which is the limit Docker's --memory sets and the kernel enforces by
|
||||
* killing the process -- and, for a copy mlock()ed for the host gather (`mlocks`), RLIMIT_MEMLOCK
|
||||
* unless the process holds CAP_IPC_LOCK. Pinned memory a card reads is the driver's userptr
|
||||
* mapping, the host pools' kind, and RLIMIT_MEMLOCK does not count it. A limit that cannot be read
|
||||
* is not one; a limit that can is never guessed at. */
|
||||
static bool lockable_room(int64_t need, int64_t still, bool mlocks, std::string* why) {
|
||||
long long locked_kb = 0;
|
||||
unsigned long long caps = 0;
|
||||
{
|
||||
@@ -304,7 +307,7 @@ static bool lockable_room(int64_t need, int64_t still, std::string* why) {
|
||||
}
|
||||
}
|
||||
struct rlimit rl {};
|
||||
if (!((caps >> 14) & 1) && ::getrlimit(RLIMIT_MEMLOCK, &rl) == 0 &&
|
||||
if (mlocks && !((caps >> 14) & 1) && ::getrlimit(RLIMIT_MEMLOCK, &rl) == 0 &&
|
||||
rl.rlim_cur != RLIM_INFINITY && (int64_t)rl.rlim_cur < locked_kb * 1024 + need) {
|
||||
*why = fmt("RLIMIT_MEMLOCK allows %s locked and the copy locks %s (Docker: --ulimit "
|
||||
"memlock=-1; systemd: LimitMEMLOCK=infinity)", humanb((int64_t)rl.rlim_cur).c_str(),
|
||||
@@ -361,25 +364,63 @@ static bool lockable_room(int64_t need, int64_t still, std::string* why) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* --ngram-placement ram: the copy is table_copy_make's (core/format/table_copy.h); this keeps one
|
||||
* a container address, shared by every rank, and frees them after the ranks in ~Engine. */
|
||||
int Engine::lock_table_copy(const uint8_t* src, int64_t n, int64_t foff, void** out,
|
||||
std::string* why) {
|
||||
/* --ngram-placement ram: the copy is table_copy_make's (core/format/table_copy.h), or for a card,
|
||||
* table_copy_fill's into pinned memory every card maps at its host address (rad_dev_alloc
|
||||
* RAD_MEM_HOST_MAPPED, locked in pieces). This keeps one a container address, shared by every rank,
|
||||
* and frees them after the ranks in ~Engine. */
|
||||
int Engine::copy_table(const uint8_t* src, int64_t n, int64_t foff, bool card, void** out,
|
||||
std::string* why) {
|
||||
for (const auto& c : table_copy_of_)
|
||||
if (c.first == src) { *out = c.second; return RAD_OK; }
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
TableCopy tc;
|
||||
const int rc = table_copy_make(*file_, src, n, foff, &tc, why);
|
||||
if (rc != RAD_OK) return rc;
|
||||
RAD_INFO("placement: n-gram table copied into locked memory, %s in %.1f s",
|
||||
humanb(n).c_str(),
|
||||
void* base = nullptr;
|
||||
if (card) {
|
||||
base = rad_dev_alloc(n, RAD_MEM_HOST_MAPPED);
|
||||
if (!base) {
|
||||
*why = fmt("pinning %s: %s", humanb(n).c_str(), rad_dev_last_error());
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
const int rc = table_copy_fill(*file_, src, n, foff, static_cast<uint8_t*>(base), why);
|
||||
if (rc != RAD_OK) {
|
||||
rad_dev_free(base, RAD_MEM_HOST_MAPPED);
|
||||
return rc;
|
||||
}
|
||||
card_copies_.push_back(base);
|
||||
} else {
|
||||
TableCopy tc;
|
||||
const int rc = table_copy_make(*file_, src, n, foff, &tc, why);
|
||||
if (rc != RAD_OK) return rc;
|
||||
table_copies_.push_back(tc);
|
||||
base = tc.base;
|
||||
}
|
||||
RAD_INFO("placement: n-gram table copied into %s, %s in %.1f s",
|
||||
card ? "pinned memory the cards read" : "locked memory", humanb(n).c_str(),
|
||||
std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count());
|
||||
table_copies_.push_back(tc);
|
||||
table_copy_of_.push_back({ src, tc.base });
|
||||
*out = tc.base;
|
||||
table_copy_of_.push_back({ src, base });
|
||||
*out = base;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* THE GATHER MOVES TO THE HOST WITH THE TABLE, when --ngram-placement auto chose a pinned copy for
|
||||
* a card and it could not be made. Every rank's n-gram weights take the host site, on the
|
||||
* WeightInfo Ctx::init reads to choose the op's domain and on the plan beside it, so the gather and
|
||||
* its scale move together; a copy some rank already published is host memory, which the host
|
||||
* gather reads as it reads RAM. False where no host kernel gathers the table. */
|
||||
bool Engine::ngram_gather_to_host() {
|
||||
for (auto& r : ranks_) {
|
||||
WeightFootprint f;
|
||||
weight_footprint(r->program, &f, TablePlacement::Vram, TablePlacement::Disk);
|
||||
if (!f.ngram_host) return false;
|
||||
}
|
||||
for (auto& r : ranks_)
|
||||
for (size_t i = 1; i < r->program.weights.size(); ++i) {
|
||||
if (!is_ngram_table(r->program, (rad_weight)i)) continue;
|
||||
r->program.weights[i].site = Site::Host;
|
||||
if (i < r->plan.weight.size()) r->plan.weight[i].site = Site::Host;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* WHERE THE TWO LOOKUP TABLES GO (--embedding-placement, --ngram-placement), decided once for every
|
||||
* rank before the card is split, and written into Config for the planner and the load phase.
|
||||
*
|
||||
@@ -390,12 +431,15 @@ int Engine::lock_table_copy(const uint8_t* src, int64_t n, int64_t foff, void**
|
||||
* lookup writes the activations a card reads, which a host kernel reading the container cannot
|
||||
* reach -- an architecture declares a buffer's domain, and the token embedding's is the device.
|
||||
*
|
||||
* THE N-GRAM TABLE: only a host kernel reads one today, so Vram is refused and the choice is
|
||||
* between a locked copy in RAM and the container. Auto takes RAM when lockable_room says the
|
||||
* memory, the memlock limit and every cgroup allow it after the pools still to be allocated. */
|
||||
* THE N-GRAM TABLE: Vram where a device kernel gathers it and the card holds it; Ram as a copy,
|
||||
* pinned for that kernel to read over the link or locked for the host gather; Disk in the
|
||||
* container, read by the host gather. Each tier is refused where no kernel reads it there. Auto
|
||||
* takes the first that holds the table, in that order: the card's test is the embedding's, and
|
||||
* RAM's is lockable_room's -- the memory and every cgroup after the pools still to be allocated,
|
||||
* and the memlock limit for a locked copy. */
|
||||
int Engine::resolve_table_placement(const std::vector<VramFacts>& facts,
|
||||
const std::vector<int64_t>& emb_bytes, int64_t kv_floor,
|
||||
int64_t ngram_bytes, bool ngram_device) {
|
||||
int64_t ngram_bytes, bool ngram_device, bool ngram_host) {
|
||||
const int64_t MiB = (int64_t)1 << 20;
|
||||
int64_t emb_max = 0;
|
||||
for (int64_t b : emb_bytes) emb_max = std::max(emb_max, b);
|
||||
@@ -445,44 +489,98 @@ int Engine::resolve_table_placement(const std::vector<VramFacts>& facts,
|
||||
|
||||
TablePlacement ng = cfg_.ngram_placement;
|
||||
std::string ng_why;
|
||||
if (ngram_device) {
|
||||
if (ng == TablePlacement::Ram || ng == TablePlacement::Disk) {
|
||||
RAD_ERR("--ngram-placement %s: a device kernel reads this model's n-gram table, so the "
|
||||
"placement strategy places it with the other weights; only vram or auto apply",
|
||||
table_placement_name(ng));
|
||||
return RAD_E_INVAL;
|
||||
/* THE FASTEST TIER WITH THE ROOM, the card first: the table on the card, every rank holding
|
||||
* all of it (a lookup is not sharded), with the fixed weights and a KV pool for one
|
||||
* --max-model-len sequence beside it, as the embedding's test is; then a copy in RAM -- pinned
|
||||
* for a device kernel to read over the link, or locked for the host gather; then the
|
||||
* container, which only the host gather reads. Each is open only where a kernel reads it. */
|
||||
const int64_t still = (int64_t)cfg_.tp * cfg_.host_pool_mib * MiB +
|
||||
cfg_.prefix_cache_host_mib * MiB;
|
||||
const int64_t need = align_up(ngram_bytes, (int64_t)2 << 20);
|
||||
const char* copy = ngram_device ? "a pinned copy the card reads" : "a locked copy";
|
||||
auto card_holds = [&](std::string* why) {
|
||||
std::vector<VramFacts> with = facts;
|
||||
int64_t fixed = 0;
|
||||
for (VramFacts& f : with) {
|
||||
f.statics += ngram_bytes;
|
||||
if (emb == TablePlacement::Ram) f.statics -= emb_max;
|
||||
fixed = std::max(fixed, f.statics);
|
||||
}
|
||||
ng = TablePlacement::Vram;
|
||||
ng_why = "a device kernel reads it; placed with the other weights";
|
||||
} else if (ngram_bytes == 0) {
|
||||
/* `fixed` and not the trial's own statics: the split clamps those to what the card can give
|
||||
* and serves on, streaming every expert, so the trial alone cannot say they do not fit. */
|
||||
VramBudget tb;
|
||||
const int st = vram_budget_trial(cfg_, with, &tb);
|
||||
const bool over = st == RAD_OK && fixed >= tb.claimable;
|
||||
if (st == RAD_OK && !over && (!tb.derived_kv || tb.kv >= kv_floor)) return true;
|
||||
if (st != RAD_OK)
|
||||
*why = "the card cannot hold the other weights with it";
|
||||
else if (over)
|
||||
*why = fmt("with it the fixed weights want %s of a card that has %s to give",
|
||||
humanb(fixed).c_str(), humanb(tb.claimable).c_str());
|
||||
else
|
||||
*why = fmt("with it on the card the KV pool is %s, short of the %s one "
|
||||
"--max-model-len sequence needs", humanb(tb.kv).c_str(),
|
||||
humanb(kv_floor).c_str());
|
||||
return false;
|
||||
};
|
||||
if (ngram_bytes == 0) {
|
||||
if (ng != TablePlacement::Auto)
|
||||
RAD_WARN("--ngram-placement %s: this model has no n-gram table, so there is nothing to "
|
||||
"place", table_placement_name(ng));
|
||||
ng = TablePlacement::Disk;
|
||||
} else if (ng == TablePlacement::Vram) {
|
||||
RAD_ERR("--ngram-placement vram: no loaded kernel plugin reads an n-gram table on a card "
|
||||
"(embed_lookup_q has host kernels only), so it is read from host memory or the "
|
||||
"container: ram or disk");
|
||||
return RAD_E_INVAL;
|
||||
} else if (ng == TablePlacement::Ram || ng == TablePlacement::Auto) {
|
||||
const int64_t still = (int64_t)cfg_.tp * cfg_.host_pool_mib * MiB +
|
||||
cfg_.prefix_cache_host_mib * MiB;
|
||||
const int64_t need = align_up(ngram_bytes, (int64_t)2 << 20);
|
||||
std::string why;
|
||||
const bool room = lockable_room(need, still, &why);
|
||||
if (ng == TablePlacement::Ram && !room) {
|
||||
RAD_ERR("--ngram-placement ram: the n-gram table (%s) cannot be locked in memory: %s",
|
||||
humanb(ngram_bytes).c_str(), why.c_str());
|
||||
if (!ngram_device) {
|
||||
RAD_ERR("--ngram-placement vram: no loaded kernel plugin reads an n-gram table on a "
|
||||
"card (embed_lookup_q has host kernels only), so it is read from host memory "
|
||||
"or the container: ram or disk");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (!card_holds(&why)) {
|
||||
RAD_ERR("--ngram-placement vram: the n-gram table (%s, all of it on every card) does "
|
||||
"not fit: %s. Use ram, disk or auto.", humanb(ngram_bytes).c_str(),
|
||||
why.c_str());
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
if (ng == TablePlacement::Auto)
|
||||
ng_why = room ? "the memory, the memlock limit and the cgroups allow a locked copy"
|
||||
: "a locked copy does not fit: " + why;
|
||||
else
|
||||
ng_why = "as asked";
|
||||
ng = room ? TablePlacement::Ram : TablePlacement::Disk;
|
||||
} else {
|
||||
ng_why = "as asked";
|
||||
} else if (ng == TablePlacement::Disk) {
|
||||
if (!ngram_host) {
|
||||
RAD_ERR("--ngram-placement disk: no loaded kernel plugin gathers an n-gram table on "
|
||||
"the host, which is what reads it from the container. Use vram, ram or auto.");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
ng_why = "as asked";
|
||||
} else {
|
||||
std::string card_why, ram_why;
|
||||
const bool card = ng == TablePlacement::Auto && ngram_device && card_holds(&card_why);
|
||||
const bool room = !card && lockable_room(need, still, !ngram_device, &ram_why);
|
||||
if (ng == TablePlacement::Ram) {
|
||||
if (!room) {
|
||||
RAD_ERR("--ngram-placement ram: the n-gram table (%s) cannot be held in memory as "
|
||||
"%s: %s", humanb(ngram_bytes).c_str(), copy, ram_why.c_str());
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
ng_why = "as asked";
|
||||
} else if (card) {
|
||||
ng = TablePlacement::Vram;
|
||||
ng_why = "the card holds it beside the other weights and a KV pool for one "
|
||||
"--max-model-len sequence";
|
||||
} else if (room) {
|
||||
ng = TablePlacement::Ram;
|
||||
ng_why = fmt("%sthe memory%s and the cgroups allow %s",
|
||||
ngram_device ? (card_why + "; ").c_str() : "",
|
||||
ngram_device ? "" : ", the memlock limit", copy);
|
||||
} else if (ngram_host) {
|
||||
ng = TablePlacement::Disk;
|
||||
ng_why = fmt("%s%s does not fit: %s", ngram_device ? (card_why + "; ").c_str() : "",
|
||||
copy, ram_why.c_str());
|
||||
} else {
|
||||
RAD_ERR("--ngram-placement auto: the n-gram table (%s) has nowhere to go: %s; %s does "
|
||||
"not fit (%s); and no loaded kernel plugin gathers it on the host, which is "
|
||||
"what reads it from the container", humanb(ngram_bytes).c_str(),
|
||||
card_why.c_str(), copy, ram_why.c_str());
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
}
|
||||
cfg_.ngram_resolved = ng;
|
||||
|
||||
@@ -490,7 +588,7 @@ int Engine::resolve_table_placement(const std::vector<VramFacts>& facts,
|
||||
RAD_INFO("placement: token embedding (%s a rank) %s%s: %s", humanb(emb_max).c_str(),
|
||||
table_placement_name(emb),
|
||||
cfg_.embedding_placement == TablePlacement::Auto ? " (auto)" : "", emb_why.c_str());
|
||||
if (ngram_bytes > 0 || ngram_device)
|
||||
if (ngram_bytes > 0)
|
||||
RAD_INFO("placement: n-gram table (%s) %s%s: %s", humanb(ngram_bytes).c_str(),
|
||||
table_placement_name(ng),
|
||||
cfg_.ngram_placement == TablePlacement::Auto ? " (auto)" : "", ng_why.c_str());
|
||||
@@ -967,7 +1065,7 @@ int Engine::plan() {
|
||||
* where they are not (the n-gram table every rank reads from the one mapping). */
|
||||
std::vector<int64_t> emb_bytes;
|
||||
int64_t ngram_bytes = 0, kv_floor = 0;
|
||||
bool ngram_device = false;
|
||||
bool ngram_device = false, ngram_host = false;
|
||||
const int64_t ctx = cfg_.max_ctx ? cfg_.max_ctx : meta_.n_ctx_train;
|
||||
for (auto& r : ranks_) {
|
||||
RAD_TRY(rad_dev_set(r->device));
|
||||
@@ -1005,12 +1103,13 @@ int Engine::plan() {
|
||||
* already this card's even share of the static weights: declare ran once per rank
|
||||
* with every dimension divided. */
|
||||
WeightFootprint wf;
|
||||
weight_footprint(r->program, &wf, TablePlacement::Vram);
|
||||
weight_footprint(r->program, &wf, TablePlacement::Vram, TablePlacement::Disk);
|
||||
f.statics = wf.statics;
|
||||
f.experts = wf.experts;
|
||||
emb_bytes.push_back(wf.embedding);
|
||||
ngram_bytes = std::max(ngram_bytes, wf.ngram);
|
||||
ngram_device = ngram_device || wf.ngram_device;
|
||||
ngram_host = ngram_host || wf.ngram_host;
|
||||
/* ONE --max-model-len SEQUENCE'S CACHE: the same groups planned for a single
|
||||
* sequence, which is the least a KV pool can hold and still serve every request the
|
||||
* API accepts (configure_kv warns below it). --embedding-placement auto keeps the
|
||||
@@ -1068,7 +1167,11 @@ int Engine::plan() {
|
||||
late_lazy_bytes_ = std::max(late_lazy_bytes_, lazy);
|
||||
facts.push_back(f);
|
||||
}
|
||||
RAD_TRY(resolve_table_placement(facts, emb_bytes, kv_floor, ngram_bytes, ngram_device));
|
||||
RAD_TRY(resolve_table_placement(facts, emb_bytes, kv_floor, ngram_bytes, ngram_device,
|
||||
ngram_host));
|
||||
/* The n-gram table was counted off the card; on it, it is every rank's whole table. */
|
||||
if (cfg_.ngram_resolved == TablePlacement::Vram)
|
||||
for (VramFacts& f : facts) f.statics += ngram_bytes;
|
||||
if (cfg_.embedding_resolved == TablePlacement::Ram)
|
||||
for (size_t i = 0; i < facts.size(); ++i) facts[i].statics -= emb_bytes[i];
|
||||
VramBudget vb;
|
||||
@@ -2090,28 +2193,42 @@ int Engine::load_weights() {
|
||||
}
|
||||
w.ptr = (void*)file_->data(p0);
|
||||
w.host_ptr = w.ptr;
|
||||
/* --ngram-placement ram: the host kernel reads the locked copy instead of the
|
||||
* mapping. A copy auto chose and could not make leaves the table where it was, and
|
||||
* says so; one the operator asked for refuses the start. */
|
||||
/* --ngram-placement ram: the gather reads a copy instead of the mapping -- pinned
|
||||
* memory a device kernel reads in place over the link, where the planner put the
|
||||
* table on the zero-copy site, or locked memory for the host gather. A copy auto
|
||||
* chose and could not make leaves the table in the container and says so, and a
|
||||
* card's gather moves to the host with it; one the operator asked for refuses the
|
||||
* start. */
|
||||
if (cfg_.ngram_resolved == TablePlacement::Ram &&
|
||||
is_ngram_table(r->program, (rad_weight)i)) {
|
||||
const bool card = w.site == Site::DeviceZeroCopy;
|
||||
void* copy = nullptr;
|
||||
std::string why;
|
||||
const int cs = lock_table_copy((const uint8_t*)w.ptr, n, (int64_t)p0.offset,
|
||||
©, &why);
|
||||
const int cs = copy_table((const uint8_t*)w.ptr, n, (int64_t)p0.offset, card,
|
||||
©, &why);
|
||||
if (cs == RAD_OK) {
|
||||
w.ptr = copy;
|
||||
void* on_card = card ? rad_dev_device_ptr(copy) : nullptr;
|
||||
w.ptr = on_card ? on_card : copy;
|
||||
w.host_ptr = copy;
|
||||
r->mover.publish_mapped((rad_weight)i, w.ptr);
|
||||
continue; /* reported once, by lock_table_copy, and not as mapped */
|
||||
continue; /* reported once, by copy_table, and not as mapped */
|
||||
}
|
||||
const char* into = card ? "pinned" : "locked";
|
||||
if (cfg_.ngram_placement == TablePlacement::Ram) {
|
||||
RAD_ERR("--ngram-placement ram: '%s' could not be copied into locked "
|
||||
"memory: %s", w.name.c_str(), why.c_str());
|
||||
RAD_ERR("--ngram-placement ram: '%s' could not be copied into %s memory: "
|
||||
"%s", w.name.c_str(), into, why.c_str());
|
||||
return cs;
|
||||
}
|
||||
RAD_WARN("--ngram-placement auto: '%s' could not be copied into locked memory "
|
||||
"(%s); it is read from the container", w.name.c_str(), why.c_str());
|
||||
if (card && !ngram_gather_to_host()) {
|
||||
RAD_ERR("--ngram-placement auto: '%s' could not be copied into pinned "
|
||||
"memory (%s), and no loaded kernel plugin gathers it on the host, "
|
||||
"which is what reads it from the container", w.name.c_str(),
|
||||
why.c_str());
|
||||
return cs;
|
||||
}
|
||||
RAD_WARN("--ngram-placement auto: '%s' could not be copied into %s memory "
|
||||
"(%s); it is read from the container%s", w.name.c_str(), into,
|
||||
why.c_str(), card ? " by the host gather" : "");
|
||||
cfg_.ngram_resolved = TablePlacement::Disk;
|
||||
}
|
||||
/* NO READAHEAD ON A MAPPED WEIGHT. What reads one is a host kernel gathering rows
|
||||
|
||||
+46
-37
@@ -14,6 +14,49 @@
|
||||
|
||||
namespace rad {
|
||||
|
||||
int table_copy_fill(const RadFile& file, const uint8_t* src, int64_t n, int64_t foff,
|
||||
uint8_t* buf, std::string* why) {
|
||||
if (!why || !src || !buf || n <= 0) return RAD_E_INVAL;
|
||||
const int64_t huge = (int64_t)2 << 20;
|
||||
|
||||
/* A reader per hardware thread up to sixteen, each reading its own contiguous span in pieces of
|
||||
* 64 MiB: a drive serves sixteen queued reads at full rate, and the span keeps each reader's
|
||||
* reads sequential on the file. */
|
||||
const int nt = (int)std::max(1u, std::min(16u, std::thread::hardware_concurrency()));
|
||||
const int64_t piece = (int64_t)64 << 20;
|
||||
const int64_t span = ((n + nt - 1) / nt + huge - 1) & ~(huge - 1);
|
||||
std::atomic<int> err{RAD_OK};
|
||||
std::vector<std::thread> rd;
|
||||
for (int i = 0; i < nt; ++i) {
|
||||
const int64_t lo = (int64_t)i * span, hi = std::min(n, lo + span);
|
||||
if (lo >= hi) break;
|
||||
rd.emplace_back([&, lo, hi] {
|
||||
for (int64_t o = lo; o < hi && err.load() == RAD_OK; o += piece) {
|
||||
const int64_t b = std::min(piece, hi - o);
|
||||
const int rc = file.read_at(buf + o, b, foff + o);
|
||||
if (rc < 0) { err.store(rc); return; }
|
||||
file.drop_range(foff + o, b);
|
||||
}
|
||||
});
|
||||
}
|
||||
for (std::thread& t : rd) t.join();
|
||||
|
||||
if (err.load() != RAD_OK) {
|
||||
*why = fmt("reading the container failed (%s)", rad_strerror(err.load()));
|
||||
return RAD_E_IO;
|
||||
}
|
||||
const int64_t probe = std::min<int64_t>(n, 4096);
|
||||
bool same = std::memcmp(buf, src, (size_t)probe) == 0 &&
|
||||
std::memcmp(buf + n - probe, src + n - probe, (size_t)probe) == 0;
|
||||
for (int64_t o = 0; same && o + 4096 <= n; o += std::max<int64_t>(n / 64, 4096))
|
||||
same = std::memcmp(buf + o, src + o, 4096) == 0;
|
||||
if (!same) {
|
||||
*why = "the copy does not match the container's mapping";
|
||||
return RAD_E_IO;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int table_copy_make(const RadFile& file, const uint8_t* src, int64_t n, int64_t foff,
|
||||
TableCopy* out, std::string* why) {
|
||||
if (!out || !why || !src || n <= 0) return RAD_E_INVAL;
|
||||
@@ -32,45 +75,11 @@ int table_copy_make(const RadFile& file, const uint8_t* src, int64_t n, int64_t
|
||||
::munmap(m, sz);
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
uint8_t* buf = static_cast<uint8_t*>(m);
|
||||
|
||||
/* A reader per hardware thread up to sixteen, each reading its own contiguous span in pieces of
|
||||
* 64 MiB: a drive serves sixteen queued reads at full rate, and the span keeps each reader's
|
||||
* reads sequential on the file. */
|
||||
const int nt = (int)std::max(1u, std::min(16u, std::thread::hardware_concurrency()));
|
||||
const int64_t piece = (int64_t)64 << 20;
|
||||
const int64_t span = ((n + nt - 1) / nt + (int64_t)huge - 1) & ~((int64_t)huge - 1);
|
||||
std::atomic<int> err{RAD_OK};
|
||||
std::vector<std::thread> rd;
|
||||
for (int i = 0; i < nt; ++i) {
|
||||
const int64_t lo = (int64_t)i * span, hi = std::min(n, lo + span);
|
||||
if (lo >= hi) break;
|
||||
rd.emplace_back([&, lo, hi] {
|
||||
for (int64_t o = lo; o < hi && err.load() == RAD_OK; o += piece) {
|
||||
const int64_t b = std::min(piece, hi - o);
|
||||
const int rc = file.read_at(buf + o, b, foff + o);
|
||||
if (rc < 0) { err.store(rc); return; }
|
||||
file.drop_range(foff + o, b);
|
||||
}
|
||||
});
|
||||
}
|
||||
for (std::thread& t : rd) t.join();
|
||||
|
||||
bool same = err.load() == RAD_OK;
|
||||
if (same) {
|
||||
const int64_t probe = std::min<int64_t>(n, 4096);
|
||||
same = std::memcmp(buf, src, (size_t)probe) == 0 &&
|
||||
std::memcmp(buf + n - probe, src + n - probe, (size_t)probe) == 0;
|
||||
for (int64_t o = 0; same && o + 4096 <= n; o += std::max<int64_t>(n / 64, 4096))
|
||||
same = std::memcmp(buf + o, src + o, 4096) == 0;
|
||||
if (!same) *why = "the copy does not match the container's mapping";
|
||||
} else {
|
||||
*why = fmt("reading the container failed (%s)", rad_strerror(err.load()));
|
||||
}
|
||||
if (!same) {
|
||||
const int rc = table_copy_fill(file, src, n, foff, static_cast<uint8_t*>(m), why);
|
||||
if (rc != RAD_OK) {
|
||||
::munlock(m, sz);
|
||||
::munmap(m, sz);
|
||||
return RAD_E_IO;
|
||||
return rc;
|
||||
}
|
||||
out->base = m;
|
||||
out->bytes = sz;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
/* table_copy.h -- a container table copied into locked host memory (--ngram-placement ram).
|
||||
/* table_copy.h -- a container table copied into host memory (--ngram-placement ram): locked memory
|
||||
* for the host gather, made here, or pinned memory a card reads, which the caller allocates and
|
||||
* table_copy_fill fills. What follows is about the locked copy; the read and the check are both's.
|
||||
*
|
||||
* A COPY IN HUGE PAGES, NOT THE MAPPING LOCKED. Faulting a container's mapping in costs a fault every
|
||||
* 4 KiB page and leaves every row behind a 4 KiB translation, so a gather of scattered rows walks
|
||||
@@ -28,6 +30,11 @@ struct TableCopy {
|
||||
int table_copy_make(const RadFile& file, const uint8_t* src, int64_t n, int64_t foff,
|
||||
TableCopy* out, std::string* why);
|
||||
|
||||
/* The read and the check table_copy_make makes, into [dst, dst + n) the caller allocated: pinned
|
||||
* memory a card reads in place, when a device kernel gathers the table (--ngram-placement ram). */
|
||||
int table_copy_fill(const RadFile& file, const uint8_t* src, int64_t n, int64_t foff,
|
||||
uint8_t* dst, std::string* why);
|
||||
|
||||
/* Unlocks and unmaps it; a copy never made is a no-op. */
|
||||
void table_copy_free(TableCopy* c);
|
||||
|
||||
|
||||
+82
-49
@@ -101,25 +101,31 @@ int64_t weight_bytes(const WeightInfo& w) {
|
||||
/* See planner.h. Planner::resolve_sites, Planner::build_units and Planner::pin_mapped, run over
|
||||
* the same program without allocating a Plan -- the budget resolver calls this before a Planner
|
||||
* exists. Kept adjacent to the three so a change to any of them has this in the same diff. */
|
||||
void weight_footprint(const Program& prog, WeightFootprint* out, TablePlacement embedding) {
|
||||
void weight_footprint(const Program& prog, WeightFootprint* out, TablePlacement embedding,
|
||||
TablePlacement ngram) {
|
||||
if (!out) return;
|
||||
*out = WeightFootprint{};
|
||||
|
||||
/* resolve_sites: a weight every one of whose ops lacks a device band cannot be read on the
|
||||
* card, so it is never copied there whatever the budgets say. */
|
||||
std::vector<uint8_t> device_ok(prog.weights.size(), 1u);
|
||||
std::vector<uint8_t> device_ok(prog.weights.size(), 1u), host_ok(prog.weights.size(), 1u);
|
||||
for (const OpInfo& o : prog.ops) {
|
||||
bool dev = !o.bands.empty();
|
||||
for (const Band& b : o.bands) if (!b.dom[RAD_DOMAIN_DEVICE]) dev = false;
|
||||
if (dev) continue;
|
||||
for (rad_weight w : o.weights)
|
||||
if (w != RAD_NULL_HANDLE && w < device_ok.size()) device_ok[w] = 0u;
|
||||
bool dev = !o.bands.empty(), host = !o.bands.empty();
|
||||
for (const Band& b : o.bands) {
|
||||
if (!b.dom[RAD_DOMAIN_DEVICE]) dev = false;
|
||||
if (!b.dom[RAD_DOMAIN_HOST]) host = false;
|
||||
}
|
||||
for (rad_weight w : o.weights) {
|
||||
if (w == RAD_NULL_HANDLE || w >= device_ok.size()) continue;
|
||||
if (!dev) device_ok[w] = 0u;
|
||||
if (!host) host_ok[w] = 0u;
|
||||
}
|
||||
}
|
||||
|
||||
/* build_units: a model-level weight is its own unit, everything else groups by the declared
|
||||
* (layer, expert). Ordered, for the same reason build_units is. */
|
||||
struct U { int32_t layer = -1, expert = -1; int64_t bytes = 0; bool mapped = false;
|
||||
bool emb = false, ngram = false; };
|
||||
bool emb = false, ngram = false, host = true; };
|
||||
std::map<std::pair<int32_t, int32_t>, size_t> by_group;
|
||||
std::vector<U> units;
|
||||
|
||||
@@ -148,6 +154,7 @@ void weight_footprint(const Program& prog, WeightFootprint* out, TablePlacement
|
||||
mu.bytes = align_up(mu.bytes, a) + weight_bytes(wi);
|
||||
/* pin_mapped: THE WHOLE UNIT GOES, not the one weight. */
|
||||
if (w < device_ok.size() && !device_ok[w]) mu.mapped = true;
|
||||
if (w < host_ok.size() && !host_ok[w]) mu.host = false;
|
||||
/* The placement flags' tables: model-level, so the unit is this one weight. */
|
||||
if (layer < 0) {
|
||||
mu.emb = is_embedding_table(prog, w);
|
||||
@@ -159,19 +166,24 @@ void weight_footprint(const Program& prog, WeightFootprint* out, TablePlacement
|
||||
* align_up(static_need, RAD_ALIGN_UNIT)` before every unit it places. A budget that summed raw
|
||||
* bytes would be short by one alignment per unit, which on a 512-expert MoE is real. */
|
||||
std::map<int32_t, int64_t> layer_experts;
|
||||
bool ngram_host = true;
|
||||
for (const U& mu : units) if (mu.ngram && !mu.host) ngram_host = false;
|
||||
for (const U& mu : units) {
|
||||
const int64_t b = align_up(mu.bytes, RAD_ALIGN_UNIT);
|
||||
/* pin_embedding moves a table out of the statics only where nothing has mapped it. */
|
||||
if (mu.emb && !mu.mapped) out->embedding += b;
|
||||
if (mu.ngram && mu.mapped) out->ngram += b;
|
||||
if (mu.ngram) out->ngram += b;
|
||||
if (mu.ngram && !mu.mapped) out->ngram_device = true;
|
||||
if (mu.mapped) out->mapped += b;
|
||||
/* pin_mapped: an n-gram table a device kernel reads stays off the card unless it was
|
||||
* placed in VRAM. */
|
||||
if (mu.mapped || (mu.ngram && ngram != TablePlacement::Vram)) out->mapped += b;
|
||||
else if (mu.emb && embedding == TablePlacement::Ram) out->hosted += b;
|
||||
else if (mu.expert >= 0) { out->experts += b; layer_experts[mu.layer] += b; }
|
||||
else out->statics += b;
|
||||
}
|
||||
for (const auto& le : layer_experts)
|
||||
out->expert_layer_max = std::max(out->expert_layer_max, le.second);
|
||||
out->ngram_host = out->ngram > 0 && ngram_host;
|
||||
out->n_units = (int64_t)units.size();
|
||||
}
|
||||
std::string weight_class_key(const std::string& name) {
|
||||
@@ -206,9 +218,8 @@ std::string weight_class_key(const std::string& name) {
|
||||
*
|
||||
* Tracking host alone would rest on the argument that a missing device kernel is fatal at declare
|
||||
* and never reaches the planner. That is true only of an op with NO kernel in either domain: an op
|
||||
* such as `embed_lookup_q` resolves on the host and nowhere else -- deliberately, because its
|
||||
* table is far too large for the card and must not be copied there -- and declare reports that
|
||||
* device band as unserved without refusing the model. */
|
||||
* such as `embed_lookup_q` resolves on the host and nowhere else when no device kernel for it is
|
||||
* loaded, and declare reports that device band as unserved without refusing the model. */
|
||||
void Planner::resolve_sites(const Program& prog) {
|
||||
host_ok_.assign(prog.weights.size(), 1u);
|
||||
device_ok_.assign(prog.weights.size(), 1u);
|
||||
@@ -237,54 +248,75 @@ void Planner::resolve_sites(const Program& prog) {
|
||||
|
||||
/* ------------------------------------------------------------------ the mapped tier */
|
||||
/* A weight no device kernel can read is pinned to the container's mapping BEFORE any budget is
|
||||
* consulted, because it is not a budget question. The other three off-device tiers are what the
|
||||
* planner falls back to when VRAM runs out; this one is where the weight has to be whatever the
|
||||
* budgets say, and charging a pool for bytes that are never copied would make the operator size a
|
||||
* pool against a number that does not exist.
|
||||
* consulted, because it is not a budget question -- and so is an n-gram table --ngram-placement put
|
||||
* in RAM or on disk, whatever kernels could read it on a card. The other three off-device tiers are
|
||||
* what the planner falls back to when VRAM runs out; this one is where the weight has to be
|
||||
* whatever the budgets say, and charging a pool for bytes that are never copied would make the
|
||||
* operator size a pool against a number that does not exist.
|
||||
*
|
||||
* THE WHOLE UNIT GOES, not the one weight. A unit is the movement granularity and a half-mapped
|
||||
* one has no meaning -- and it costs nothing here, because `device_ok_` is cleared for EVERY
|
||||
* weight operand of a host-only op, so the unit's other members are the same op's scale and
|
||||
* constants. What this does exclude is a plugin that groups a host-only weight with a device one:
|
||||
* that collapses the whole group onto the host, so the report names it. */
|
||||
* THE WHOLE UNIT GOES, not the one weight. A unit is the movement granularity and a half-mapped one
|
||||
* has no meaning -- and it costs nothing here, because `device_ok_` is cleared for EVERY weight
|
||||
* operand of a host-only op, so the unit's other members are the same op's scale and constants.
|
||||
* What this does exclude is a plugin that groups a host-only weight with a device one: that
|
||||
* collapses the whole group onto the host, so the report names it. */
|
||||
void Planner::pin_mapped(const Program& prog, Plan* p) {
|
||||
const TablePlacement ng = in_.config->ngram_resolved;
|
||||
for (MoveUnit& mu : p->units) {
|
||||
bool mapped = false;
|
||||
bool host_only = false;
|
||||
for (rad_weight w : mu.weights)
|
||||
if (w < device_ok_.size() && !device_ok_[w]) mapped = true;
|
||||
if (!mapped) continue;
|
||||
if (w < device_ok_.size() && !device_ok_[w]) host_only = true;
|
||||
/* --ngram-placement keeps an n-gram table OFF THE CARD unless it says vram, whether or not
|
||||
* a device kernel could read it there: ram and disk are where the operator put it, and the
|
||||
* strategies below would otherwise spend the card on 48 GB of rows read sixteen a token. */
|
||||
const bool ngram = mu.layer < 0 && !mu.weights.empty() &&
|
||||
is_ngram_table(prog, mu.weights.front());
|
||||
if (!host_only && !(ngram && ng != TablePlacement::Vram)) continue;
|
||||
|
||||
mu.tier = Tier::Mapped;
|
||||
mu.site = Site::Host;
|
||||
mu.movable = false;
|
||||
mu.reason = "no device kernel for its op; read from the container's mapping";
|
||||
/* --ngram-placement ram: the load phase copies an n-gram table out of the mapping into
|
||||
* locked memory and publishes the copy, so the host kernel reads RAM instead of the file.
|
||||
* Every pool still sees the mapped tier; the report shows what the copy costs. */
|
||||
if (in_.config->ngram_resolved == TablePlacement::Ram && mu.layer < 0 &&
|
||||
!mu.weights.empty() && is_ngram_table(prog, mu.weights.front())) {
|
||||
mu.reason = "--ngram-placement ram: a locked copy in RAM, made at load";
|
||||
if (host_only) {
|
||||
mu.site = Site::Host;
|
||||
mu.reason = "no device kernel for its op; read from the container's mapping";
|
||||
} else if (ng == TablePlacement::Ram) {
|
||||
mu.site = Site::DeviceZeroCopy;
|
||||
mu.reason = "--ngram-placement ram: a pinned copy in RAM the card reads in place, made "
|
||||
"at load";
|
||||
} else {
|
||||
mu.site = Site::Host;
|
||||
mu.reason = "--ngram-placement disk: the host gather reads it from the container";
|
||||
for (rad_weight w : mu.weights)
|
||||
if (w < host_ok_.size() && !host_ok_[w])
|
||||
note_unfit(p, mu, "--ngram-placement disk needs a host kernel for its gather, "
|
||||
"and none was loaded");
|
||||
}
|
||||
/* --ngram-placement ram: the load phase copies an n-gram table out of the mapping and
|
||||
* publishes the copy -- locked memory the host kernel reads, or pinned memory the card
|
||||
* reads where a device kernel gathers. Every pool still sees the mapped tier; the report
|
||||
* shows what the copy costs. */
|
||||
if (ngram && ng == TablePlacement::Ram) {
|
||||
if (host_only) mu.reason = "--ngram-placement ram: a locked copy in RAM, made at load";
|
||||
else p->ngram_copy_card = true;
|
||||
p->ngram_copy_bytes += mu.bytes;
|
||||
}
|
||||
/* AND ONLY WHAT LIES IN THE CONTAINER AS THE KERNEL READS IT can be read from the mapping:
|
||||
* a weight its kernel rearranges at load, or a shard of one, has no place in the file
|
||||
* that already looks the way the kernel reads it (spec §5.1). */
|
||||
/* AND ONLY WHAT LIES IN THE CONTAINER AS THE KERNEL READS IT can be read from the mapping,
|
||||
* or copied out of it: a weight its kernel rearranges at load, or a shard of one, has no
|
||||
* place in the file that already looks the way the kernel reads it (spec §5.1). */
|
||||
if (!in_.container)
|
||||
note_unfit(p, mu, "only a host kernel reads it, so it must be read from a "
|
||||
"container's mapping -- and a checkpoint served directly has "
|
||||
"none; convert it to a container");
|
||||
note_unfit(p, mu, "it is read from a container's mapping or copied out of one -- and "
|
||||
"a checkpoint served directly has none; convert it to a container");
|
||||
else
|
||||
for (rad_weight w : mu.weights)
|
||||
if (w < prog.weights.size() &&
|
||||
!weight_file_direct(prog.weights[w], prog.ctx.world_size))
|
||||
note_unfit(p, mu, "only a host kernel reads it, so it must be read from the "
|
||||
"container's mapping -- and its stored form is not the "
|
||||
"container's (a relayout, a widening or a shard)");
|
||||
for (rad_weight w : mu.weights)
|
||||
if (w < device_ok_.size() && device_ok_[w])
|
||||
RAD_WARN("placement: '%s' is grouped with a weight only a host kernel can read, "
|
||||
"so the whole group stays in the container's mapping",
|
||||
prog.weights[w].name.c_str());
|
||||
note_unfit(p, mu, "it is read from the container's mapping or copied out of "
|
||||
"it -- and its stored form is not the container's (a "
|
||||
"relayout, a widening or a shard)");
|
||||
if (host_only)
|
||||
for (rad_weight w : mu.weights)
|
||||
if (w < device_ok_.size() && device_ok_[w])
|
||||
RAD_WARN("placement: '%s' is grouped with a weight only a host kernel can "
|
||||
"read, so the whole group stays in the container's mapping",
|
||||
prog.weights[w].name.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1270,8 +1302,9 @@ std::string Plan::report(const Program& prog) const {
|
||||
s += fmt(" embedding %12s %12s (--embedding-placement ram, pinned, read in place)\n",
|
||||
"-", humanb(embedding_host_bytes).c_str());
|
||||
if (ngram_copy_bytes)
|
||||
s += fmt(" n-gram copy %12s %12s (--ngram-placement ram, locked RAM, filled at load)\n",
|
||||
"-", humanb(ngram_copy_bytes).c_str());
|
||||
s += fmt(" n-gram copy %12s %12s (--ngram-placement ram, %s, filled at load)\n",
|
||||
"-", humanb(ngram_copy_bytes).c_str(),
|
||||
ngram_copy_card ? "pinned RAM the card reads" : "locked RAM");
|
||||
/* Out of the VRAM weight budget, beside the residents and not among them: the two buffers a
|
||||
* routed layer's file-tier experts are read into, each sized to the layer that puts the most
|
||||
* there. */
|
||||
|
||||
+16
-7
@@ -249,9 +249,12 @@ public:
|
||||
* `embedding_host_used` is the same tables' weight bytes, the unit the tier totals count in. */
|
||||
int64_t embedding_host_bytes = 0;
|
||||
int64_t embedding_host_used = 0;
|
||||
/* --ngram-placement ram: the n-gram tables the load phase copies into locked memory. They stay
|
||||
* on the mapped tier as far as every pool is concerned; this is what the copy costs in RAM. */
|
||||
/* --ngram-placement ram: the n-gram tables the load phase copies into host memory. They stay
|
||||
* on the mapped tier as far as every pool is concerned; this is what the copy costs in RAM.
|
||||
* `ngram_copy_card` says which copy: pinned memory a device kernel reads in place over the
|
||||
* link, or locked memory the host gather reads. */
|
||||
int64_t ngram_copy_bytes = 0;
|
||||
bool ngram_copy_card = false;
|
||||
/* ROUTED EXPERTS ON THE FILE TIER have no address of their own: a routed layer's are read into
|
||||
* one of two VRAM buffers before the layer's first expert op and published there until the next
|
||||
* routed layer starts (place/stager.h, FileStager). `file_layer_max` is the most one layer's
|
||||
@@ -377,17 +380,23 @@ struct WeightFootprint {
|
||||
int64_t mapped = 0; /* no device kernel can read them; they never enter a pool at all */
|
||||
int64_t hosted = 0; /* --embedding-placement ram: in the pinned host pool, off the card */
|
||||
/* The tables the two placement flags move, wherever the buckets above count them: `embedding`
|
||||
* is inside statics or hosted, `ngram` inside mapped. */
|
||||
* is inside statics or hosted, `ngram` inside statics when a device kernel reads it and it is
|
||||
* placed in VRAM, inside mapped otherwise. */
|
||||
int64_t embedding = 0;
|
||||
int64_t ngram = 0;
|
||||
/* An n-gram table some device kernel reads, which the strategies place like any weight. No
|
||||
* plugin has one today; it is what makes --ngram-placement vram mean something. */
|
||||
/* A device kernel reads the n-gram table, so it can be on the card or read over the link from
|
||||
* pinned host memory; without one only the host gather reads it, from RAM or the container. */
|
||||
bool ngram_device = false;
|
||||
/* A host kernel gathers it, so it can stay in the container (--ngram-placement disk). */
|
||||
bool ngram_host = false;
|
||||
int64_t n_units = 0;
|
||||
};
|
||||
/* `embedding` is what the token embedding's placement resolved to; only Ram moves it out of the
|
||||
* statics. Stated by every caller, so the budget and the planner cannot count it differently. */
|
||||
void weight_footprint(const Program& prog, WeightFootprint* out, TablePlacement embedding);
|
||||
* statics. `ngram` is the n-gram table's; only Vram puts it among them, and only where a device
|
||||
* kernel reads it. Stated by every caller, so the budget and the planner cannot count it
|
||||
* differently. */
|
||||
void weight_footprint(const Program& prog, WeightFootprint* out, TablePlacement embedding,
|
||||
TablePlacement ngram);
|
||||
|
||||
/* THE TABLES THE PLACEMENT FLAGS MOVE, by what reads them and not by name: a model-level weight
|
||||
* that at least one op reads and that only `embed_lookup` reads is a token embedding table, and
|
||||
|
||||
+5
-3
@@ -109,9 +109,11 @@ struct Band {
|
||||
* beside the mapping that already holds one. A sparse Zipfian gather of a dozen rows out of
|
||||
* hundreds of millions is exactly what a page cache is good at.
|
||||
*
|
||||
* Only a HOST kernel can read one -- the bytes are pageable and at no device address -- so the
|
||||
* planner assigns this tier where, and only where, no device kernel for the op exists at all
|
||||
* (embed_lookup_q, libavx/avx_ngram.cpp, is the one such op in this tree). */
|
||||
* Only a HOST kernel can read the mapping -- the bytes are pageable and at no device address -- so
|
||||
* the planner assigns this tier where no device kernel for the op exists at all, and where
|
||||
* --ngram-placement keeps an n-gram table off the card (embed_lookup_q, libavx/avx_ngram.cpp).
|
||||
* Under `ram` the load phase publishes a copy in place of the mapping: locked memory for the host
|
||||
* gather, or pinned memory a device kernel reads over the link (the zero-copy site). */
|
||||
enum class Tier { VRAM = 0, HostPinned = 1, HostPageable = 2, SSD = 3, Mapped = 4 };
|
||||
|
||||
/* WHERE A LOOKUP TABLE LIVES: the token embedding (--embedding-placement) and an n-gram embedding
|
||||
|
||||
@@ -890,11 +890,11 @@ int Ctx::issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
|
||||
* is the one place it stops being harmless: it runs NOW, on this thread, and reads and writes
|
||||
* the host arena NOW.
|
||||
*
|
||||
* A PLE gather is exactly that shape. `ngram_ids` writes the hashed row ids into a host-domain
|
||||
* buffer on the device, and `embed_lookup_q` reads them on the host. With no join the host
|
||||
* reads whatever the arena holds from an earlier step and gathers the rows of the wrong
|
||||
* n-gram -- valid ids, valid rows, every kernel agreeing with its oracle, and fluent text that
|
||||
* is a stable wrong answer rather than an intermittent one.
|
||||
* A PLE gather on the host is exactly that shape. `ngram_ids` writes the hashed row ids into a
|
||||
* host-domain buffer on the device, and `embed_lookup_q` reads them on the host. With no join
|
||||
* the host reads whatever the arena holds from an earlier step and gathers the rows of the
|
||||
* wrong n-gram -- valid ids, valid rows, every kernel agreeing with its oracle, and fluent text
|
||||
* that is a stable wrong answer rather than an intermittent one.
|
||||
*
|
||||
* WHAT THE JOIN WAITS FOR is the last device op on each lane that touched the host arena
|
||||
* (host_touch_ev_), not the whole stream. Every hazard a host op has is with such an op -- the
|
||||
|
||||
Reference in new issue
Block a user