Archived
radiance 1.0.10
- Four-rank Qwen3.8-Flash-Next (PRs 14 and 16 by keptn42): the all-reduce runs fused with the hyper-connection's write, and with the MoE gather in front of it, on decode and verify steps on the exact wire. The routed experts are split in four classes, so every rank computes the same share; moe_gemm_q_mod4 is the new grouped GEMM. The oracle now checks moe_gemm_q on w4nl and w5nl containers. - --embedding-placement and --ngram-placement, each auto | vram | ram | disk. The token embedding can live in the pinned host pool, read in place over the link (from PR 17 by mechkbfan). An n-gram table can be copied into locked RAM at load instead of read from the container (from PR 15 by keptn42). auto takes the fastest tier with room once every other weight is placed, and startup prints what it chose and why. A tier that cannot be honoured stops the start with the reason; the embedding on disk and the n-gram table in VRAM are refused, since nothing can serve them. - The expert_tiered placement no longer spends the pinned host pool twice when it falls back to layer offload, and a lossy-wire request for a four-rank fused write is a selection miss rather than a startup abort.
This commit is contained in:
1 parent
2f56e3c477
commit
e466fa07e2
56 files changed
+1804
-761
No files matched your search
+1
-8
@@ -30,15 +30,8 @@ codes; below it, one code at a time. The row's layout hook only checks the table
|
||||
E4M3 with one bf16 scale, or the checkpoint's plain bf16 — and asks for no re-layout, because the
|
||||
gather reads the rows from the container file in the layout they are stored in.
|
||||
|
||||
With the op's `resident` parameter (`--ngram-table ram`) the first gather instead starts a copy of
|
||||
the whole table into locked huge pages, in the background, and every gather reads the copy once it
|
||||
is in place: 47.7 GiB of RAM for Flash-Next, checked against RLIMIT_MEMLOCK, MemAvailable and the
|
||||
cgroup's memory.max before a byte is read. A process short of any of them says so and keeps
|
||||
reading the file.
|
||||
|
||||
`rad-avx-check` runs the op over a file-backed table with two ranks and the reader racing, at
|
||||
every level, and compares every row with the file's bytes; and once more with `resident`, before
|
||||
and after the copy is in place.
|
||||
every level, and compares every row with the file's bytes.
|
||||
|
||||
## The build is the dispatch
|
||||
|
||||
|
||||
@@ -237,30 +237,32 @@ std::vector<Case> build_cases() {
|
||||
for (const char* order : { "token", "sorted" }) {
|
||||
add("moe_gemm", { HI("M", 9), HI("N", 64), HI("K", 128), HI("n_expert", 8),
|
||||
HI("top_k", 2), HS("a_order", order), HS("dtype", dt) });
|
||||
/* `fp8a8`, the block-fp8 grid: libref describes a grouped GEMM's weight scale plane
|
||||
* only at a dtype that names its grid. */
|
||||
add("moe_gemm_q", { HI("M", 9), HI("N", 64), HI("K", 128), HI("n_expert", 8),
|
||||
HI("top_k", 2), HI("group", 128), HS("a_order", order),
|
||||
HS("dtype", "w8a8") });
|
||||
HS("dtype", "fp8a8") });
|
||||
}
|
||||
/* The two classes at different widths: a gate/up pair whose odd experts are half as wide
|
||||
* (the output's second part begins past the odd class's zeros), and a down projection
|
||||
* whose odd experts are half as long. */
|
||||
add("moe_gemm_q", { HI("M", 9), HI("N", 256), HI("K", 128), HI("N_odd", 128),
|
||||
HI("parts", 2), HI("n_expert", 8), HI("top_k", 2), HI("group", 128),
|
||||
HS("a_order", "token"), HS("dtype", "w8a8") });
|
||||
HS("a_order", "token"), HS("dtype", "fp8a8") });
|
||||
add("moe_gemm_q", { HI("M", 9), HI("N", 64), HI("K", 256), HI("K_odd", 128),
|
||||
HI("n_expert", 8), HI("top_k", 2), HI("group", 128),
|
||||
HS("a_order", "sorted"), HS("dtype", "w8a8") });
|
||||
HS("a_order", "sorted"), HS("dtype", "fp8a8") });
|
||||
/* Four classes at three widths -- a four-rank slice of a five-block expert is two blocks
|
||||
* of one class and one of each other -- over ten experts, so classes 2 and 3 hold one
|
||||
* fewer than 0 and 1: the gate/up pair and the down projection. */
|
||||
add("moe_gemm_q_mod4", { HI("M", 9), HI("N", 128), HI("K", 128), HI("N_1", 256),
|
||||
HI("N_2", 128), HI("N_3", 128), HI("parts", 2),
|
||||
HI("n_expert", 10), HI("top_k", 2), HI("group", 128),
|
||||
HS("a_order", "token"), HS("dtype", "w8a8") });
|
||||
HS("a_order", "token"), HS("dtype", "fp8a8") });
|
||||
add("moe_gemm_q_mod4", { HI("M", 9), HI("N", 64), HI("K", 128), HI("K_1", 128),
|
||||
HI("K_2", 256), HI("K_3", 128), HI("n_expert", 10),
|
||||
HI("top_k", 2), HI("group", 128), HS("a_order", "sorted"),
|
||||
HS("dtype", "w8a8") });
|
||||
HS("dtype", "fp8a8") });
|
||||
}
|
||||
add("moe_scatter", { HI("M", 9), HI("n_expert", 8), HI("top_k", 2), HI("expert_base", 0) });
|
||||
|
||||
|
||||
@@ -799,10 +799,9 @@ static bool schema_same(const RadOpSchema& a, const RadOpSchema& b, std::string*
|
||||
return true;
|
||||
}
|
||||
|
||||
/* embed_lookup_q over a file-backed table, two ranks and the reader racing, or with the table
|
||||
* copied into RAM (avx_check_rows.cpp). */
|
||||
/* embed_lookup_q over a file-backed table, two ranks and the reader racing (avx_check_rows.cpp). */
|
||||
bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool verbose,
|
||||
bool bf16, bool resident);
|
||||
bool bf16);
|
||||
/* embed_lookup_q's init, where io_uring is refused and here (avx_check_rows.cpp). */
|
||||
bool ngram_init_case(const Lib& avx);
|
||||
|
||||
@@ -946,16 +945,13 @@ int main(int argc, char** argv) {
|
||||
const std::string at = sl == std::string::npos ? "." : self.substr(0, sl);
|
||||
/* The E4M3 table the quantised containers carry, and the plain bf16 one a checkpoint
|
||||
* served as it ships carries: one path, two row sizes. */
|
||||
if (!row_gather_case(avx, at, 1000, verbose, false, false)) rc = 1;
|
||||
if (!row_gather_case(avx, at, 1000, verbose, true, false)) rc = 1;
|
||||
/* The copy in RAM is the process's, made once a table, so at one level: the last. */
|
||||
if (lvl == hi && !row_gather_case(avx, at, 1000, verbose, false, true)) rc = 1;
|
||||
if (!row_gather_case(avx, at, 1000, verbose, false)) rc = 1;
|
||||
if (!row_gather_case(avx, at, 1000, verbose, true)) rc = 1;
|
||||
/* And in TMPDIR, which is a tmpfs on most machines: since Linux 6.6 a tmpfs does
|
||||
* direct reads and reports no alignment for them, as btrfs does, so the gather finds
|
||||
* its granule from the block size there. */
|
||||
const char* tmp = std::getenv("TMPDIR");
|
||||
if (!row_gather_case(avx, tmp && *tmp ? tmp : "/tmp", 1000, verbose, false, false))
|
||||
rc = 1;
|
||||
if (!row_gather_case(avx, tmp && *tmp ? tmp : "/tmp", 1000, verbose, false)) rc = 1;
|
||||
}
|
||||
}
|
||||
if (!declined_ops.empty()) {
|
||||
|
||||
+16
-55
@@ -20,17 +20,10 @@
|
||||
* takes one full 32-code step and a masked tail. The table is a file in `dir`, and the filesystem
|
||||
* there decides how the gather aligns its direct reads: by the alignment the filesystem reports
|
||||
* (ext4, XFS), or by its block size where it reports none (btrfs, and a tmpfs since Linux 6.6). The
|
||||
* case says which it ran.
|
||||
*
|
||||
* With `resident` the op is asked to copy the table into locked RAM (--ngram-table ram), over a
|
||||
* table small enough for a default memlock limit. The calls run while the copy is being made, then
|
||||
* the case waits for the process's locked memory to grow by the table and runs them again, from
|
||||
* the copy. Where the process may not lock that much the gather says so and keeps reading the file,
|
||||
* and the case says which of the two it checked; either way every row must be the file's. */
|
||||
* case says which it ran. */
|
||||
#include "avx_harness.h"
|
||||
|
||||
#include <barrier>
|
||||
#include <chrono>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
@@ -133,23 +126,10 @@ bool ngram_init_case(const Lib& avx) {
|
||||
return ok && right;
|
||||
}
|
||||
|
||||
/* This process's locked memory, in bytes: VmLck. */
|
||||
static long long locked_bytes() {
|
||||
long long kb = 0;
|
||||
if (std::FILE* f = std::fopen("/proc/self/status", "re")) {
|
||||
char line[256];
|
||||
while (std::fgets(line, sizeof line, f)) (void)std::sscanf(line, "VmLck: %lld", &kb);
|
||||
std::fclose(f);
|
||||
}
|
||||
return kb << 10;
|
||||
}
|
||||
|
||||
bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool verbose,
|
||||
bool bf16, bool resident) {
|
||||
bool bf16) {
|
||||
const RadKernelInfo* r = avx.row("embed_lookup_q");
|
||||
const char* what = resident ? "embed_lookup_q from a file, resident"
|
||||
: bf16 ? "embed_lookup_q from a file, bf16 table"
|
||||
: "embed_lookup_q from a file";
|
||||
const char* what = bf16 ? "embed_lookup_q from a file, bf16 table" : "embed_lookup_q from a file";
|
||||
if (!r || !r->launch) {
|
||||
std::printf(" %s: no row\n", what);
|
||||
return false;
|
||||
@@ -170,8 +150,7 @@ bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool
|
||||
}
|
||||
::close((int)ring);
|
||||
}
|
||||
/* 2.5 MiB resident, which a default memlock limit of 8 MiB holds. */
|
||||
const int64_t n = 40, V = int64_t(1) << (resident ? 16 : 21), M = 512, hot = 4096;
|
||||
const int64_t n = 40, V = int64_t(1) << 21, M = 512, hot = 4096;
|
||||
const int64_t rb = bf16 ? 2 * n : n;
|
||||
const float scale = 0.375f;
|
||||
|
||||
@@ -259,14 +238,13 @@ bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool
|
||||
tensor(const_cast<uint8_t*>(tab), bf16 ? RAD_BF16 : RAD_F8E4M3, {V, n}),
|
||||
tensor(bf16 ? nullptr : &sc, RAD_BF16, {1}), tensor(out.data(), RAD_BF16, {M, n}),
|
||||
tensor(nullptr, RAD_I32, {M}) };
|
||||
RadParam ps[4] = {};
|
||||
ps[0].key = "M"; ps[0].kind = RAD_P_INT; ps[0].ival = M;
|
||||
ps[1].key = "n_embd"; ps[1].kind = RAD_P_INT; ps[1].ival = n;
|
||||
ps[2].key = "n_vocab"; ps[2].kind = RAD_P_INT; ps[2].ival = V;
|
||||
ps[3].key = "resident"; ps[3].kind = RAD_P_INT; ps[3].ival = 1;
|
||||
RadParam ps[3] = {};
|
||||
ps[0].key = "M"; ps[0].kind = RAD_P_INT; ps[0].ival = M;
|
||||
ps[1].key = "n_embd"; ps[1].kind = RAD_P_INT; ps[1].ival = n;
|
||||
ps[2].key = "n_vocab"; ps[2].kind = RAD_P_INT; ps[2].ival = V;
|
||||
RadArgs a{};
|
||||
a.t = ts.data(); a.n_t = (int)ts.size();
|
||||
a.p = ps; a.n_p = resident ? 4 : 3;
|
||||
a.p = ps; a.n_p = 3;
|
||||
a.rank = rk; a.world_size = 2;
|
||||
for (int64_t c = 0; c < calls; ++c) {
|
||||
ts[0].data = &ids[(size_t)(c * M)];
|
||||
@@ -286,23 +264,9 @@ bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool
|
||||
}
|
||||
}
|
||||
};
|
||||
const long long locked0 = locked_bytes();
|
||||
auto round = [&] {
|
||||
std::thread t0(rank, 0), t1(rank, 1);
|
||||
t0.join();
|
||||
t1.join();
|
||||
};
|
||||
round();
|
||||
/* The copy is made in the background from the first call. Once the locked memory has grown
|
||||
* by the table it is in place, or about to be, so a short wait more and every call reads it. */
|
||||
bool copied = false;
|
||||
if (resident) {
|
||||
for (int i = 0; i < 300 && !copied; ++i) {
|
||||
copied = locked_bytes() - locked0 >= V * rb;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
if (copied) round();
|
||||
}
|
||||
std::thread t0(rank, 0), t1(rank, 1);
|
||||
t0.join();
|
||||
t1.join();
|
||||
/* THE ADDRESS RANGE STAYS RESERVED. libavx resolves a table to its file once, by the address
|
||||
* range of its mapping, for the life of the process (avx_ngram.cpp, table_file) -- the
|
||||
* engine's container mapping is one. A case that unmapped here could have the next case's file
|
||||
@@ -315,16 +279,13 @@ bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool
|
||||
::unlink(path.c_str());
|
||||
|
||||
const bool pass = bad[0] + bad[1] + refused[0] + refused[1] == 0;
|
||||
const char* from = !resident ? ""
|
||||
: copied ? ", then as many again from the copy in RAM"
|
||||
: ", all from the file: the copy was not made here (above)";
|
||||
if (verbose || !pass)
|
||||
std::printf(" %s in %s (%s): %lld calls x 2 ranks%s, rows wrong %lld/%lld, "
|
||||
std::printf(" %s in %s (%s): %lld calls x 2 ranks, rows wrong %lld/%lld, "
|
||||
"calls refused %lld/%lld %s\n", what, dir.c_str(), granule, (long long)calls,
|
||||
from, (long long)bad[0], (long long)bad[1], (long long)refused[0],
|
||||
(long long)bad[0], (long long)bad[1], (long long)refused[0],
|
||||
(long long)refused[1], pass ? "ok" : "FAIL");
|
||||
else
|
||||
std::printf(" %s in %s (%s): %lld calls x 2 ranks%s, every row exact\n", what,
|
||||
dir.c_str(), granule, (long long)calls, from);
|
||||
std::printf(" %s in %s (%s): %lld calls x 2 ranks, every row exact\n", what, dir.c_str(),
|
||||
granule, (long long)calls);
|
||||
return pass;
|
||||
}
|
||||
@@ -700,7 +700,6 @@ static bool rows_form(const RadArgs* a, RowGather* g) {
|
||||
g->n_ahead = ahead ? numel(ahd) : 0;
|
||||
g->rank = a->rank;
|
||||
g->world_size = a->world_size;
|
||||
g->resident = p_int(a, "resident", 0) != 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+12
-197
@@ -57,18 +57,7 @@
|
||||
* plugin ABI carries no file, so /proc/self/maps says which file and offset back that address, once
|
||||
* a table. A table no file backs -- a test's heap buffer -- is read as the memory it is. A file on a
|
||||
* filesystem that cannot do direct I/O is refused; direct_granule below finds the alignment the
|
||||
* reads keep, including on a filesystem that does not report one.
|
||||
*
|
||||
* ============================== OR THE WHOLE TABLE IN RAM ==============================
|
||||
*
|
||||
* All of the above is the default, and it holds no more of the table than the row cache does.
|
||||
* With `resident` set on the op -- the engine's --ngram-table ram -- the table is copied once into
|
||||
* locked host memory and every gather reads the copy, so a miss is a load instead of a read: on
|
||||
* Flash-Next at four ranks a 2k-token prompt prefills 24% faster and 16 to 32 streams gain 4 to 5%,
|
||||
* for 47.7 GiB of RAM. The first gather that asks starts the copy in the background and, until it
|
||||
* is in place, every gather reads the file exactly as it does without it, so serving never waits
|
||||
* for the copy. A process that cannot hold it -- RLIMIT_MEMLOCK, the memory available, a cgroup's
|
||||
* limit -- says so once and keeps reading the file. make_resident has the detail. */
|
||||
* reads keep, including on a filesystem that does not report one. */
|
||||
#include "avx_rows.h"
|
||||
#include "avx_common.h"
|
||||
|
||||
@@ -79,9 +68,7 @@
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
@@ -95,7 +82,6 @@
|
||||
#include <fcntl.h>
|
||||
#include <linux/io_uring.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
@@ -172,15 +158,6 @@ struct TableFile {
|
||||
int fd = -1;
|
||||
long long off = 0;
|
||||
long long align = 0;
|
||||
std::string path;
|
||||
/* --ngram-table ram: the table's own copy, [copy, copy + copy_n) holding [copy_lo, copy_lo +
|
||||
* copy_n) of the mapping. `copy` is stored last, once the copy is filled, locked and checked;
|
||||
* null, the gather reads the file. One copy a file, of the table the first resident gather
|
||||
* names, which in every container so far is the only table in it. */
|
||||
std::once_flag copy_once;
|
||||
std::atomic<const uint8_t*> copy{nullptr};
|
||||
const uint8_t* copy_lo = nullptr;
|
||||
long long copy_n = 0;
|
||||
};
|
||||
|
||||
/* THE GRANULE OF A DIRECT READ of `t`'s file, opened for them as `t.fd`: the alignment a read's
|
||||
@@ -260,12 +237,10 @@ long long direct_granule(const TableFile& t, const uint8_t* p, long long n, unsi
|
||||
/* The file behind [p, p + n): the mapping holding `p`, extended over the mappings that continue it
|
||||
* in the same file (a madvise over a sub-range splits one mapping into several). Looked up once per
|
||||
* table and kept for the life of the process; every rank thread shares the entry and the fd, and a
|
||||
* read at an explicit offset has no file position to share. The list is never destroyed: a copy
|
||||
* thread (make_resident) may still be filling an entry when the process exits. */
|
||||
TableFile* table_file(const uint8_t* p, long long n, int* err) {
|
||||
static std::mutex& mu = *new std::mutex;
|
||||
static std::vector<std::unique_ptr<TableFile>>& known =
|
||||
*new std::vector<std::unique_ptr<TableFile>>;
|
||||
* read at an explicit offset has no file position to share. */
|
||||
const TableFile* table_file(const uint8_t* p, long long n, int* err) {
|
||||
static std::mutex mu;
|
||||
static std::vector<std::unique_ptr<TableFile>> known;
|
||||
std::lock_guard<std::mutex> lk(mu);
|
||||
for (const auto& t : known)
|
||||
if (p >= t->lo && p + n <= t->hi) return t.get();
|
||||
@@ -312,7 +287,6 @@ TableFile* table_file(const uint8_t* p, long long n, int* err) {
|
||||
if (found) {
|
||||
if ((uintptr_t)(p + n) > end) { *err = RAD_E_IO; return nullptr; }
|
||||
t->hi = (const uint8_t*)end;
|
||||
t->path = path;
|
||||
t->fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC | O_DIRECT);
|
||||
const char* why = t->fd < 0 ? std::strerror(errno) : nullptr;
|
||||
if (t->fd >= 0) t->align = direct_granule(*t, p, n, ino, path.c_str(), &why);
|
||||
@@ -329,155 +303,6 @@ TableFile* table_file(const uint8_t* p, long long n, int* err) {
|
||||
return known.back().get();
|
||||
}
|
||||
|
||||
/* A size for a message: GiB from one up, MiB below. */
|
||||
std::string size_str(unsigned long long b) {
|
||||
char s[32];
|
||||
if (b >= 1ull << 30) std::snprintf(s, sizeof s, "%.1f GiB", (double)b / (1ull << 30));
|
||||
else std::snprintf(s, sizeof s, "%.1f MiB", (double)b / (1ull << 20));
|
||||
return s;
|
||||
}
|
||||
|
||||
/* WHETHER `bytes` MORE CAN BE LOCKED IN RAM, or why not. Each limit is one the copy would
|
||||
* otherwise meet after reading the whole table: RLIMIT_MEMLOCK, unless the process has
|
||||
* CAP_IPC_LOCK; what the kernel says is available, less a margin for everything else; and the
|
||||
* cgroup's memory.max, which Docker's --memory sets and which the kernel enforces by killing the
|
||||
* process. A limit that cannot be read is not one. */
|
||||
bool room_to_lock(unsigned long long bytes, std::string* why) {
|
||||
const unsigned long long margin = 4ull << 30;
|
||||
unsigned long long locked = 0, caps = 0, avail = 0;
|
||||
bool have_avail = false;
|
||||
char line[256];
|
||||
if (std::FILE* f = std::fopen("/proc/self/status", "re")) {
|
||||
while (std::fgets(line, sizeof line, f)) {
|
||||
(void)std::sscanf(line, "VmLck: %llu", &locked);
|
||||
(void)std::sscanf(line, "CapEff: %llx", &caps);
|
||||
}
|
||||
std::fclose(f);
|
||||
}
|
||||
struct rlimit rl {};
|
||||
if (!(caps >> 14 & 1) && ::getrlimit(RLIMIT_MEMLOCK, &rl) == 0 &&
|
||||
rl.rlim_cur != RLIM_INFINITY && rl.rlim_cur < (locked << 10) + bytes) {
|
||||
*why = "the copy locks " + size_str(bytes) + " and RLIMIT_MEMLOCK allows " +
|
||||
size_str(rl.rlim_cur) + " (in Docker, --ulimit memlock=-1 lifts it)";
|
||||
return false;
|
||||
}
|
||||
if (std::FILE* f = std::fopen("/proc/meminfo", "re")) {
|
||||
while (!have_avail && std::fgets(line, sizeof line, f))
|
||||
have_avail = std::sscanf(line, "MemAvailable: %llu", &avail) == 1;
|
||||
std::fclose(f);
|
||||
}
|
||||
if (have_avail && (avail << 10) < bytes + margin) {
|
||||
*why = size_str(avail << 10) + " of memory is available, and the copy wants " +
|
||||
size_str(bytes) + " with " + size_str(margin) + " to spare";
|
||||
return false;
|
||||
}
|
||||
/* cgroup v2: the process's group is the "0::" line. */
|
||||
std::string cg;
|
||||
if (std::FILE* f = std::fopen("/proc/self/cgroup", "re")) {
|
||||
while (std::fgets(line, sizeof line, f))
|
||||
if (std::strncmp(line, "0::", 3) == 0) {
|
||||
cg = line + 3;
|
||||
while (!cg.empty() && cg.back() == '\n') cg.pop_back();
|
||||
}
|
||||
std::fclose(f);
|
||||
}
|
||||
auto read_ull = [](const std::string& file, unsigned long long* v) {
|
||||
std::FILE* f = std::fopen(file.c_str(), "re");
|
||||
if (!f) return false;
|
||||
const bool got = std::fscanf(f, "%llu", v) == 1; /* "max" reads as no limit */
|
||||
std::fclose(f);
|
||||
return got;
|
||||
};
|
||||
unsigned long long max = 0, cur = 0;
|
||||
const std::string dir = "/sys/fs/cgroup" + (cg == "/" ? std::string() : cg);
|
||||
if (!cg.empty() && read_ull(dir + "/memory.max", &max) &&
|
||||
read_ull(dir + "/memory.current", &cur) && (cur > max || max - cur < bytes + margin)) {
|
||||
*why = "the cgroup's memory.max leaves " + size_str(cur > max ? 0 : max - cur) +
|
||||
", and the copy wants " + size_str(bytes) + " with " + size_str(margin) +
|
||||
" to spare";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* --ngram-table ram: KEEP THE WHOLE TABLE IN RAM, [p, p + n) of `t`'s mapping. Called once a table,
|
||||
* by the first gather whose op carries `resident`.
|
||||
*
|
||||
* A COPY IN HUGE PAGES, NOT THE MAPPING LOCKED. Faulting the mapping in costs a fault a 4 KiB page
|
||||
* -- 12.5 million of them, ~140 s inside a VM -- and leaves every row behind a 4 KiB translation,
|
||||
* so a gather of scattered rows walks the page tables on nearly every row. Parallel buffered reads
|
||||
* into an anonymous MADV_HUGEPAGE region fill it in 18.7 s for Flash-Next's 47.7 GiB, a 2 MiB page
|
||||
* covers 13,000 rows, and the pages read are dropped from the page cache behind them, so the copy
|
||||
* does not hold the table twice. A thread fills, locks and checks the copy and only then
|
||||
* stores `copy`; until it does the gather reads the file. Whatever stops it -- a limit checked here
|
||||
* before a byte is read, or a failure on the way -- leaves the table on the file path and says
|
||||
* why, once. */
|
||||
void make_resident(TableFile* t, const uint8_t* p, long long n) {
|
||||
auto refuse = [n](const std::string& why) {
|
||||
std::fprintf(stderr, "libavx: --ngram-table ram: the n-gram table (%s) stays on disk and is "
|
||||
"read from the container: %s\n", size_str((unsigned long long)n).c_str(),
|
||||
why.c_str());
|
||||
};
|
||||
const size_t huge = (size_t)2 << 20;
|
||||
const size_t sz = ((size_t)n + huge - 1) & ~(huge - 1);
|
||||
std::string why;
|
||||
if (!room_to_lock(sz, &why)) { refuse(why); return; }
|
||||
const long long foff = t->off + (long long)(p - t->lo);
|
||||
std::thread([t, p, n, sz, foff, refuse] {
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
auto fail = [&](const char* what, int e) {
|
||||
refuse(std::string(what) + " failed (" + std::strerror(e) + ")");
|
||||
};
|
||||
void* m = ::mmap(nullptr, sz, PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
|
||||
if (m == MAP_FAILED) { fail("mmap", errno); return; }
|
||||
uint8_t* buf = static_cast<uint8_t*>(m);
|
||||
(void)::madvise(buf, sz, MADV_HUGEPAGE);
|
||||
const int fd = ::open(t->path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) { fail("open", errno); ::munmap(m, sz); return; }
|
||||
const int nt = 16;
|
||||
const long long step = (n / nt + (long long)huge - 1) & ~((long long)huge - 1);
|
||||
std::atomic<int> err{0};
|
||||
std::vector<std::thread> fill;
|
||||
for (int i = 0; i < nt; ++i) {
|
||||
const long long lo = (long long)i * step, hi = std::min(n, lo + step);
|
||||
if (lo >= hi) break;
|
||||
fill.emplace_back([&, lo, hi] {
|
||||
for (long long o = lo; o < hi && !err.load();) {
|
||||
const size_t want = (size_t)std::min(hi - o, (long long)(64 << 20));
|
||||
const ssize_t r = ::pread(fd, buf + o, want, (off_t)(foff + o));
|
||||
if (r <= 0) { err.store(r < 0 ? errno : EIO); return; }
|
||||
(void)::posix_fadvise(fd, (off_t)(foff + o), (off_t)r, POSIX_FADV_DONTNEED);
|
||||
o += r;
|
||||
}
|
||||
});
|
||||
}
|
||||
for (auto& th : fill) th.join();
|
||||
::close(fd);
|
||||
if (err.load()) { fail("reading the container", err.load()); ::munmap(m, sz); return; }
|
||||
if (::mlock(buf, sz) != 0) { fail("mlock", errno); ::munmap(m, sz); return; }
|
||||
/* The copy has to be the mapping's bytes: a wrong offset would serve every row from the
|
||||
* wrong place without an error. Both ends and a stride of rows in between. */
|
||||
const long long probe = std::min(n, (long long)4096);
|
||||
bool same = std::memcmp(buf, p, (size_t)probe) == 0 &&
|
||||
std::memcmp(buf + n - probe, p + n - probe, (size_t)probe) == 0;
|
||||
for (long long o = 0; same && o + 4096 <= n; o += n / 64)
|
||||
same = std::memcmp(buf + o, p + o, 4096) == 0;
|
||||
if (!same) {
|
||||
fail("checking the copy against the mapping", EIO);
|
||||
::munlock(buf, sz);
|
||||
::munmap(m, sz);
|
||||
return;
|
||||
}
|
||||
t->copy_lo = p;
|
||||
t->copy_n = n;
|
||||
t->copy.store(buf, std::memory_order_release);
|
||||
std::fprintf(stderr, "libavx: n-gram table resident in RAM: %s copied to locked huge pages "
|
||||
"in %.1f s\n", size_str((unsigned long long)n).c_str(),
|
||||
std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count());
|
||||
}).detach();
|
||||
}
|
||||
|
||||
/* ============================== THE ROW CACHE ==============================
|
||||
*
|
||||
* Set-associative: a row's key -- its byte offset in the file -- hashes to a set of kWays slots, and
|
||||
@@ -1036,7 +861,7 @@ inline std::FILE* trace_file() {
|
||||
|
||||
#else /* not Linux: no file reads; a table is read as memory */
|
||||
struct TableFile { int fd = -1; };
|
||||
inline TableFile* table_file(const uint8_t*, long long, int*) { static TableFile m; return &m; }
|
||||
inline const TableFile* table_file(const uint8_t*, long long, int*) { static TableFile m; return &m; }
|
||||
inline std::FILE* trace_file() { return nullptr; }
|
||||
#endif
|
||||
|
||||
@@ -1063,24 +888,14 @@ int avx::row_gather(const RowGather& q, RowDecode decode) {
|
||||
for (int b = 0; b < 256; ++b)
|
||||
lut[b] = rad_f32_to_bf16(rad_fp8e4m3_to_f32((uint8_t)b) * q.scale);
|
||||
|
||||
/* The rows: through the shared cache from the file behind the table, or from the table's copy
|
||||
* in RAM once there is one, or, for a table no file backs, from the table itself. The caller
|
||||
* has refused an id past the table already; an id turned into a file offset has to be one. */
|
||||
/* The rows: through the shared cache from the file behind the table, or, for a table no file
|
||||
* backs, from the table itself. The caller has refused an id past the table already; an id
|
||||
* turned into a file offset has to be one. */
|
||||
int ferr = RAD_OK;
|
||||
const long long bytes = q.n_vocab * q.row_bytes;
|
||||
TableFile* tfile = table_file(q.tab, bytes, &ferr);
|
||||
const TableFile* tfile = table_file(q.tab, q.n_vocab * q.row_bytes, &ferr);
|
||||
if (!tfile) return ferr;
|
||||
const uint8_t* tab = q.tab;
|
||||
#if defined(__linux__)
|
||||
bool from_copy = false;
|
||||
if (q.resident && tfile->fd >= 0)
|
||||
std::call_once(tfile->copy_once, [&] { make_resident(tfile, q.tab, bytes); });
|
||||
if (const uint8_t* copy = tfile->copy.load(std::memory_order_acquire))
|
||||
if (q.tab >= tfile->copy_lo && q.tab + bytes <= tfile->copy_lo + tfile->copy_n) {
|
||||
tab = copy + (q.tab - tfile->copy_lo);
|
||||
from_copy = true;
|
||||
}
|
||||
if (tfile->fd >= 0 && !from_copy) {
|
||||
if (tfile->fd >= 0) {
|
||||
int why = 0;
|
||||
ReadRing* rr = read_ring(&why);
|
||||
if (!rr) {
|
||||
@@ -1111,7 +926,7 @@ int avx::row_gather(const RowGather& q, RowDecode decode) {
|
||||
std::memset(dst, 0, (size_t)q.n_embd * sizeof(uint16_t));
|
||||
continue;
|
||||
}
|
||||
g.src.push_back(tab + id * q.row_bytes);
|
||||
g.src.push_back(q.tab + id * q.row_bytes);
|
||||
g.dst.push_back(dst);
|
||||
}
|
||||
if (!g.src.empty()) decode(g.src.data(), g.dst.data(), (int64_t)g.src.size(), q.n_embd, lut);
|
||||
|
||||
+5
-10
@@ -373,10 +373,6 @@ static const RadParamSpec pEmbed[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_
|
||||
P_STR("dtype"), P_INTO("vocab_offset"),
|
||||
P_F64O("wscale") };
|
||||
static const RadOperandSpec oEmbed[] = { OPD("tokens"), WGT("wte"), OUT("x") };
|
||||
/* embed_lookup_q's, and `resident`: keep the table's copy in RAM (avx_ngram.cpp). */
|
||||
static const RadParamSpec pEmbedQ[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_vocab"),
|
||||
P_STR("dtype"), P_INTO("vocab_offset"),
|
||||
P_F64O("wscale"), P_INTO("resident") };
|
||||
static const RadOperandSpec oEmbedQ[] = { OPD("tokens"), WGT("wte"), WGT_O("scale"), OUT("x"),
|
||||
OPD_O("ahead") };
|
||||
|
||||
@@ -543,7 +539,7 @@ static const RadOpSchema kSchemas[] = {
|
||||
"sum across ranks, f32, ascending rank order." },
|
||||
{ "all_gather", ARR(pAllGather), ARR(oAllGather), "gather every rank's shard." },
|
||||
{ "embed_lookup", ARR(pEmbed), ARR(oEmbed), "embedding gather, vocab-parallel with an offset." },
|
||||
{ "embed_lookup_q", ARR(pEmbedQ), ARR(oEmbedQ),
|
||||
{ "embed_lookup_q", ARR(pEmbed), ARR(oEmbedQ),
|
||||
"embedding gather, one scale for the table; without a scale the table is read as it is." },
|
||||
{ "logits_gemm", ARR(pLogits), ARR(oLogits), "the lm_head projection." },
|
||||
{ "sample_penalties", ARR(pPenal), ARR(oPenalties),
|
||||
@@ -746,13 +742,12 @@ SIMPLE("avx_all_gather", "all_gather", "ar", "gather every rank's shard; world_s
|
||||
SIMPLE("avx_embed_lookup", "embed_lookup", "vocab", "embedding gather", DT_FLOAT, L(embed_lookup)),
|
||||
/* THE ONE ROW WITH A LAYOUT HOOK, and it only checks: the table is read as it is stored, and the
|
||||
* hook refuses a table encoded any other way (avx_ngram.cpp). An E4M3 table into bf16 goes through
|
||||
* the row cache, or with `resident` from a copy in RAM once there is one; every other dtype pair
|
||||
* takes the general gather. Its init refuses a process that may not make the io_uring ring the
|
||||
* cache's misses are read through, at declare and not at the first request. */
|
||||
* the row cache; every other dtype pair takes the general gather. Its init refuses a process that
|
||||
* may not make the io_uring ring the cache's misses are read through, at declare and not at the
|
||||
* first request. */
|
||||
{ "avx_embed_lookup_q", "embed_lookup_q", "vocab",
|
||||
"embedding gather, one scale for the table: an E4M3 table into bf16 through a shared row cache "
|
||||
"and direct reads of the container, a later call's rows read ahead, or with `resident` from a "
|
||||
"copy of the table in locked RAM",
|
||||
"and direct reads of the container, a later call's rows read ahead",
|
||||
SHAPE_ANY, DT_ANY, RAD_DOMAIN_HOST, 0,
|
||||
nullptr, 0, nullptr, 0, nullptr,
|
||||
/* INIT, fini, LAUNCH, scratch, layout, relayout, opd_shape, unrelayout */
|
||||
|
||||
+3
-6
@@ -26,8 +26,7 @@ typedef void (*RowDecode)(const uint8_t* const* src, uint16_t* const* dst, int64
|
||||
* elements -- E4M3 codes, or bf16 as stored -- `row_bytes` apart, `out` M rows of n_embd bf16
|
||||
* `x_ld` elements apart. The row cache and the reads deal in `row_bytes`; the decode in elements.
|
||||
* `ahead` holds n_ahead ids a later call will gather, or is null. rank and world_size are the
|
||||
* call's. `resident` is the op's parameter of that name: keep a copy of the table in RAM and read
|
||||
* that, once it is there (avx_ngram.cpp). */
|
||||
* call's. */
|
||||
struct RowGather {
|
||||
const int32_t* ids;
|
||||
int64_t M;
|
||||
@@ -40,12 +39,10 @@ struct RowGather {
|
||||
const int32_t* ahead;
|
||||
int64_t n_ahead;
|
||||
int rank, world_size;
|
||||
int resident;
|
||||
};
|
||||
|
||||
/* The gather, through the row cache when a file backs the table, from its copy in RAM once
|
||||
* `resident` has made one, and from the table's memory when no file backs it. Returns a RAD_*
|
||||
* status; on anything but RAD_OK the output is not complete. */
|
||||
/* The gather, through the row cache when a file backs the table and from the table's memory when
|
||||
* none does. Returns a RAD_* status; on anything but RAD_OK the output is not complete. */
|
||||
int row_gather(const RowGather& g, RowDecode decode);
|
||||
|
||||
} /* namespace avx */
|
||||
|
||||
Reference in new issue
Block a user