n-gram table: --ngram-table ram reads it from a locked copy in RAM

embed_lookup_q reads the n-gram table through a row cache and direct reads,
and about half of a step's rows are first-seen, so steps wait on the drive.
--ngram-table ram (default disk, unchanged) copies the table into locked
huge pages in the background, checks the copy against the mapping and then
serves the gather from memory. MemAvailable, RLIMIT_MEMLOCK and the cgroup
limit are checked first; if any is short the table stays on disk. The flag
reaches the plugin as an optional `resident` parameter of embed_lookup_q.
This commit is contained in:
keptn42 authored and StillDeadcode committed 2026-10-05 12:52:50 +02:00
1 parent 4252353bc2
commit e6c8a3d7fa
18 files changed
+363 -56

No files matched your search

+9 -5
View File
@@ -799,9 +799,10 @@ 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 (avx_check_rows.cpp). */
/* 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). */
bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool verbose,
bool bf16);
bool bf16, bool resident);
/* embed_lookup_q's init, where io_uring is refused and here (avx_check_rows.cpp). */
bool ngram_init_case(const Lib& avx);
@@ -945,13 +946,16 @@ 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)) rc = 1;
if (!row_gather_case(avx, at, 1000, verbose, true)) rc = 1;
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;
/* 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)) rc = 1;
if (!row_gather_case(avx, tmp && *tmp ? tmp : "/tmp", 1000, verbose, false, false))
rc = 1;
}
}
if (!declined_ops.empty()) {
+55 -16
View File
@@ -20,10 +20,17 @@
* 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. */
* 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. */
#include "avx_harness.h"
#include <barrier>
#include <chrono>
#include <cerrno>
#include <cstring>
#include <thread>
@@ -126,10 +133,23 @@ 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 bf16, bool resident) {
const RadKernelInfo* r = avx.row("embed_lookup_q");
const char* what = bf16 ? "embed_lookup_q from a file, bf16 table" : "embed_lookup_q from a file";
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";
if (!r || !r->launch) {
std::printf(" %s: no row\n", what);
return false;
@@ -150,7 +170,8 @@ bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool
}
::close((int)ring);
}
const int64_t n = 40, V = int64_t(1) << 21, M = 512, hot = 4096;
/* 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 rb = bf16 ? 2 * n : n;
const float scale = 0.375f;
@@ -238,13 +259,14 @@ 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[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;
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;
RadArgs a{};
a.t = ts.data(); a.n_t = (int)ts.size();
a.p = ps; a.n_p = 3;
a.p = ps; a.n_p = resident ? 4 : 3;
a.rank = rk; a.world_size = 2;
for (int64_t c = 0; c < calls; ++c) {
ts[0].data = &ids[(size_t)(c * M)];
@@ -264,9 +286,23 @@ bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool
}
}
};
std::thread t0(rank, 0), t1(rank, 1);
t0.join();
t1.join();
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();
}
/* 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
@@ -279,13 +315,16 @@ 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, rows wrong %lld/%lld, "
std::printf(" %s in %s (%s): %lld calls x 2 ranks%s, rows wrong %lld/%lld, "
"calls refused %lld/%lld %s\n", what, dir.c_str(), granule, (long long)calls,
(long long)bad[0], (long long)bad[1], (long long)refused[0],
from, (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, every row exact\n", what, dir.c_str(),
granule, (long long)calls);
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);
return pass;
}
+1
View File
@@ -700,6 +700,7 @@ 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;
}
+197 -12
View File
@@ -57,7 +57,18 @@
* 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. */
* 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. */
#include "avx_rows.h"
#include "avx_common.h"
@@ -68,7 +79,9 @@
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <deque>
#include <memory>
@@ -82,6 +95,7 @@
#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>
@@ -158,6 +172,15 @@ 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
@@ -237,10 +260,12 @@ 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. */
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;
* 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>>;
std::lock_guard<std::mutex> lk(mu);
for (const auto& t : known)
if (p >= t->lo && p + n <= t->hi) return t.get();
@@ -287,6 +312,7 @@ const 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);
@@ -303,6 +329,155 @@ const 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
@@ -861,7 +1036,7 @@ inline std::FILE* trace_file() {
#else /* not Linux: no file reads; a table is read as memory */
struct TableFile { int fd = -1; };
inline const TableFile* table_file(const uint8_t*, long long, int*) { static TableFile m; return &m; }
inline TableFile* table_file(const uint8_t*, long long, int*) { static TableFile m; return &m; }
inline std::FILE* trace_file() { return nullptr; }
#endif
@@ -888,14 +1063,24 @@ 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, 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 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. */
int ferr = RAD_OK;
const TableFile* tfile = table_file(q.tab, q.n_vocab * q.row_bytes, &ferr);
const long long bytes = q.n_vocab * q.row_bytes;
TableFile* tfile = table_file(q.tab, bytes, &ferr);
if (!tfile) return ferr;
const uint8_t* tab = q.tab;
#if defined(__linux__)
if (tfile->fd >= 0) {
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) {
int why = 0;
ReadRing* rr = read_ring(&why);
if (!rr) {
@@ -926,7 +1111,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(q.tab + id * q.row_bytes);
g.src.push_back(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);
+10 -5
View File
@@ -373,6 +373,10 @@ 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") };
@@ -539,7 +543,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(pEmbed), ARR(oEmbedQ),
{ "embed_lookup_q", ARR(pEmbedQ), 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),
@@ -742,12 +746,13 @@ 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; 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, 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. */
{ "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",
"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",
SHAPE_ANY, DT_ANY, RAD_DOMAIN_HOST, 0,
nullptr, 0, nullptr, 0, nullptr,
/* INIT, fini, LAUNCH, scratch, layout, relayout, opd_shape, unrelayout */
+6 -3
View File
@@ -26,7 +26,8 @@ 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. */
* 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). */
struct RowGather {
const int32_t* ids;
int64_t M;
@@ -39,10 +40,12 @@ 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 and from the table's memory when
* none does. 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, 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. */
int row_gather(const RowGather& g, RowDecode decode);
} /* namespace avx */