diff --git a/CMakeLists.txt b/CMakeLists.txt index 07707eb3..2fbb1023 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.21) # VERSION is what /version reports and what a client checks compatibility against. It is NOT the # ABI version -- that lives in abi/rad_abi.h and is read out of the header below, because # a plugin contract and a server release do not move together. -project(radiance VERSION 1.0.1 LANGUAGES C CXX) +project(radiance VERSION 1.0.2 LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/docs/DOCKER.md b/docs/DOCKER.md index efc5817b..5ad7bec3 100644 --- a/docs/DOCKER.md +++ b/docs/DOCKER.md @@ -127,7 +127,11 @@ The container names are the files the README's model table downloads or converts Copy the directory to the serving host and fill in `.env` from `.env.example`. It sets the image, the models directory (mounted read-only at `/models`), a writable state directory (`/data`, where the prefix cache's disk tier lives), the API key, the port, and the user and GPU groups the server -runs as. Then: +runs as. The models directory has to be on a local filesystem that does direct I/O, such as ext4, +XFS or btrfs: Qwen3.8-Flash-Next reads its n-gram table from the container that way. On btrfs with +compression on, the extents it stored compressed are read through the page cache instead, which +works but loses what the direct reads are for; a container copied into a directory set `chattr +C` +is stored uncompressed. Then: ```sh docker compose -f flashnext.yaml up -d # start; restarts with the Docker daemon diff --git a/docs/OPS.md b/docs/OPS.md index 1e0d2423..650f2856 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -866,7 +866,10 @@ the wrong shape, pinning it for zero-copy needs all 51.2 GB pinned in host RAM, `hipHostRegister` on the container's mapping would force all of it resident. The container is mmapped, so the table is at a host address on open. libavx keeps the rows a gather has touched in a row cache of its own and reads a miss straight from the file with O_DIRECT -(`libavx/avx_ngram.cpp`): the page cache would map a 4 KiB page per 160-byte row. +(`libavx/avx_ngram.cpp`): the page cache would map a 4 KiB page per 160-byte row. The reads keep +the alignment the filesystem reports for direct I/O, or its block size where it reports none, as +btrfs does; one read of the table proves it before the first row is served. A filesystem with no +direct I/O at all cannot hold a container with this table. The placement planner runs an op on the host when **every weight operand of it** is `Site::Host`, which is why the table must be a weight operand at all: `gather_rows` takes a plain `IN` operand and diff --git a/libavx/avx_check.cpp b/libavx/avx_check.cpp index 3aef8457..5fedf026 100644 --- a/libavx/avx_check.cpp +++ b/libavx/avx_check.cpp @@ -930,7 +930,8 @@ int main(int argc, char** argv) { if (!hand_cases(ref, avx, verbose, &hand_checked)) rc = 1; } if (only_op.empty() || only_op == "embed_lookup_q") { - /* Beside the binary: the table is read with O_DIRECT, which a tmpfs refuses. */ + /* Beside the binary, on the build's filesystem, which reports its direct-I/O + * alignment wherever the build is on ext4 or XFS. */ const std::string self = argv[0]; const size_t sl = self.rfind('/'); const std::string at = sl == std::string::npos ? "." : self.substr(0, sl); @@ -938,6 +939,11 @@ int main(int argc, char** argv) { * 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; + /* 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 (!declined_ops.empty()) { diff --git a/libavx/avx_check_rows.cpp b/libavx/avx_check_rows.cpp index f4a0f19a..9d62e2bf 100644 --- a/libavx/avx_check_rows.cpp +++ b/libavx/avx_check_rows.cpp @@ -17,8 +17,10 @@ * * Every output row is compared bit for bit with that id's bytes in the file through the call's * scale, and a padding id must come back as a zero row. The row is 40 codes, so the AVX-512 decode - * takes one full 32-code step and a masked tail. The table is a file in `dir` rather than under - * TMPDIR, because the gather reads with O_DIRECT and a tmpfs refuses it. */ + * 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. */ #include "avx_harness.h" #include @@ -80,14 +82,18 @@ bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool if (!wrote) { ::unlink(path.c_str()); std::printf(" %s: short write\n", what); return false; } } - /* A filesystem without direct I/O -- a tmpfs -- is one the gather refuses by design, so the - * case cannot run there; it says so rather than counting a refusal per call. */ + /* A filesystem without direct I/O -- one that refuses the open, as a tmpfs did before Linux + * 6.6, or one that reports the file has none -- is one the gather refuses by design, so the + * case cannot run there; it says so rather than counting a refusal per call. Every other + * filesystem the gather has to read, with or without a reported alignment. */ + bool reported = false; { const int dfd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC | O_DIRECT); struct statx sx {}; const bool dio = dfd >= 0 && ::statx(dfd, "", AT_EMPTY_PATH, STATX_DIOALIGN, &sx) == 0 && - (sx.stx_mask & STATX_DIOALIGN) && sx.stx_dio_offset_align != 0; + !((sx.stx_mask & STATX_DIOALIGN) && sx.stx_dio_offset_align == 0); + reported = (sx.stx_mask & STATX_DIOALIGN) != 0; if (dfd >= 0) ::close(dfd); if (!dio) { ::unlink(path.c_str()); @@ -95,6 +101,8 @@ bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool return true; } } + const char* granule = reported ? "the alignment the filesystem reports" + : "no alignment reported, read by the block size"; const int fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC); void* map = fd < 0 ? MAP_FAILED : mmap(nullptr, (size_t)(V * rb), PROT_READ, MAP_SHARED, fd, 0); if (fd >= 0) ::close(fd); @@ -181,11 +189,12 @@ bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool const bool pass = bad[0] + bad[1] + refused[0] + refused[1] == 0; if (verbose || !pass) - std::printf(" %s: %lld calls x 2 ranks, rows wrong %lld/%lld, " - "calls refused %lld/%lld %s\n", what, (long long)calls, (long long)bad[0], - (long long)bad[1], (long long)refused[0], (long long)refused[1], - pass ? "ok" : "FAIL"); + 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, + (long long)bad[0], (long long)bad[1], (long long)refused[0], + (long long)refused[1], pass ? "ok" : "FAIL"); else - std::printf(" %s: %lld calls x 2 ranks, every row exact\n", what, (long long)calls); + 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; } diff --git a/libavx/avx_ngram.cpp b/libavx/avx_ngram.cpp index 868887d2..cd248c2b 100644 --- a/libavx/avx_ngram.cpp +++ b/libavx/avx_ngram.cpp @@ -56,7 +56,8 @@ * THE FILE COMES FROM THE MAPPING. The table operand is a pointer into the container's mmap and the * 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. */ + * 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. */ #include "avx_rows.h" #include "avx_common.h" @@ -121,14 +122,20 @@ struct Gather { std::vector bdst; uint8_t* bounce = nullptr; size_t bounce_bytes = 0; + size_t bounce_align = 0; enum : uint8_t { PAD = 16, DONE }; - uint8_t* bounce_for(size_t bytes) { - if (bytes > bounce_bytes) { + /* A buffer of at least `bytes` on a page and on `align`, a power of two: a direct read's + * buffer has to keep the file's granule, which a filesystem with large blocks puts above a + * page. */ + uint8_t* bounce_for(size_t bytes, size_t align) { + if (align < 4096) align = 4096; + if (bytes > bounce_bytes || align > bounce_align) { std::free(bounce); - bounce = static_cast(std::aligned_alloc(4096, (bytes + 4095) & ~(size_t)4095)); + bounce = static_cast(std::aligned_alloc(align, (bytes + align - 1) & ~(align - 1))); bounce_bytes = bounce ? bytes : 0; + bounce_align = bounce ? align : 0; } return bounce; } @@ -153,6 +160,80 @@ struct TableFile { long long align = 0; }; +/* THE GRANULE OF A DIRECT READ of `t`'s file, opened for them as `t.fd`: the alignment a read's + * file offset, its length and its buffer's address must all keep. 0, with `*why` saying why, when + * the file cannot be read that way. [p, p + n) is the table, inside the mapping, and `ino` the + * mapped file's inode. + * + * STATX_DIOALIGN is the filesystem's own answer, and not every filesystem gives one: btrfs does + * direct reads and reports nothing, and so does a tmpfs since Linux 6.6. Without the report the + * granule is the file's block size, stx_blksize. No block filesystem's direct reads need more -- + * a filesystem's blocks are never smaller than its device's sectors -- and btrfs needs exactly + * that: its sector size, below which it does not refuse a direct read but serves it through the + * page cache instead. + * + * Reported or not, one read proves the granule before any row is served: a span of the table at + * an offset and into a buffer each aligned to the granule and to no more, which has to return the + * bytes the mapping holds there. That catches a filesystem refusing the granule and a file that is + * not the one mapped. A btrfs serving the read through the page cache returns the right bytes and + * passes it; the block size is what keeps btrfs off that path. */ +long long direct_granule(const TableFile& t, const uint8_t* p, long long n, unsigned long long ino, + const char* path, const char** why) { + struct statx sx; + std::memset(&sx, 0, sizeof sx); + if (::statx(t.fd, "", AT_EMPTY_PATH, STATX_INO | STATX_DIOALIGN, &sx) != 0) { + *why = std::strerror(errno); + return 0; + } + if (!(sx.stx_mask & STATX_INO) || sx.stx_ino != ino) { + *why = "the file opened is not the one mapped"; + return 0; + } + const bool reported = (sx.stx_mask & STATX_DIOALIGN) != 0; + if (reported && sx.stx_dio_offset_align == 0) { + *why = "the filesystem says this file has no direct I/O"; + return 0; + } + const long long a = !reported ? (long long)sx.stx_blksize + : sx.stx_dio_offset_align > sx.stx_dio_mem_align + ? (long long)sx.stx_dio_offset_align : (long long)sx.stx_dio_mem_align; + if (a <= 0 || (a & (a - 1))) { + *why = reported ? "the filesystem's direct-I/O alignment is not a power of two" + : "the filesystem reports no direct-I/O alignment, and its block size is " + "not a power of two"; + return 0; + } + + /* An odd multiple of the granule, so neither the offset nor the buffer keeps more than it. */ + const long long tab_lo = t.off + (long long)(p - t.lo); + long long o = (tab_lo + a - 1) & ~(a - 1); + if ((o / a) % 2 == 0) o += a; + if (o + a > tab_lo + n) { + *why = "the table is smaller than one direct read"; + return 0; + } + uint8_t* buf = static_cast(std::aligned_alloc((size_t)(2 * a), (size_t)(2 * a))); + if (!buf) { + *why = "no memory for a read"; + return 0; + } + const ssize_t got = ::pread(t.fd, buf + a, (size_t)a, (off_t)o); + const int e = errno; + const bool same = got == a && std::memcmp(buf + a, t.lo + (o - t.off), (size_t)a) == 0; + std::free(buf); + if (!same) { + *why = got < 0 ? std::strerror(e) + : got != a ? "a read of one granule came back short" + : "a read returned other bytes than the mapping holds"; + return 0; + } + if (!reported) + std::fprintf(stderr, "libavx: %s: the filesystem reports no direct-I/O alignment, so the " + "n-gram table is read in spans of its block size, %lld bytes; a direct read " + "of one returned the file's bytes\n", path, a); + return a; +} + /* 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 @@ -207,21 +288,16 @@ const TableFile* table_file(const uint8_t* p, long long n, int* err) { if ((uintptr_t)(p + n) > end) { *err = RAD_E_IO; return nullptr; } t->hi = (const uint8_t*)end; t->fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC | O_DIRECT); - struct statx sx; - std::memset(&sx, 0, sizeof sx); - if (t->fd < 0 || ::statx(t->fd, "", AT_EMPTY_PATH, STATX_INO | STATX_DIOALIGN, &sx) != 0 || - sx.stx_ino != ino || !(sx.stx_mask & STATX_DIOALIGN) || sx.stx_dio_offset_align == 0) { + 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); + if (t->align == 0) { std::fprintf(stderr, "libavx: %s cannot be read with direct I/O (%s); the n-gram table " - "is read that way\n", path.c_str(), - t->fd < 0 ? std::strerror(errno) : "the filesystem reports no alignment"); + "is read that way. A local filesystem with direct I/O -- ext4, XFS, " + "btrfs -- can hold the container\n", path.c_str(), why); if (t->fd >= 0) ::close(t->fd); *err = RAD_E_UNSUPPORTED; return nullptr; } - const long long a = sx.stx_dio_offset_align > sx.stx_dio_mem_align - ? sx.stx_dio_offset_align : sx.stx_dio_mem_align; - if (a & (a - 1)) { ::close(t->fd); *err = RAD_E_UNSUPPORTED; return nullptr; } - t->align = a; } known.push_back(std::move(t)); return known.back().get(); @@ -479,7 +555,7 @@ int read_and_publish(const TableFile& tf, RowCache& rc, ReadRing& rr, Gather& g, const size_t total = reads.size(); for (size_t b = 0; b < total;) { const unsigned cnt = (unsigned)(total - b < rr.entries ? total - b : rr.entries); - uint8_t* bounce = g.bounce_for((size_t)cnt * (size_t)stride); + uint8_t* bounce = g.bounce_for((size_t)cnt * (size_t)stride, (size_t)A); const int rs = bounce ? rr.read_spans(tf, &reads[b], cnt, n, stride, bounce) : RAD_E_NOMEM; if (rs != RAD_OK) { for (size_t i = b; i < total; ++i) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2e42403b..4b438547 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -156,8 +156,9 @@ endif() # # rad-avx-check holds libavx to libref, op for op and at every ISA level the machine executes, and # runs embed_lookup_q over a file-backed table with two ranks and the read-ahead racing: the path a -# model's host gather takes and that no geometry reaches. It writes its table beside itself, since -# the gather reads with O_DIRECT and a tmpfs refuses it. +# model's host gather takes and that no geometry reaches. It writes its table beside itself, where +# the filesystem reports its direct-I/O alignment, and again in TMPDIR, where a tmpfs reports none +# and the gather aligns its reads by the block size, as it does on btrfs. if(TARGET libavx AND TARGET rad-avx-check) add_test(NAME avx_check COMMAND rad-avx-check --ref ${RAD_PLUGIN_OUT}/kernels/libref.so