This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/core/device/directio.cpp
T
Deadcode b93b531f63 radiance 1.5.0
- Gemma 4 31B (PRs #74, #75, #81, #86; sfjes, keptn42): sliding and global attention with their
  own kernels, the assistant head as a drafter that reads the trunk's cache, the vision tower, a
  fused w4a8 decode step, bf16 and compressed-tensors int4 checkpoints, and window snapshots so a
  prefix hit on a sliding-window model resumes instead of prefilling again.
  deploy/compose/gemma4-31b-w4a8.yaml serves keptn42's container on two cards
- ParoQuant (PRs #65, #66, #67; malic): Qwen3.8-27B checkpoints with learned Givens rotations,
  as int4, int5 and MXFP4, imported without re-quantising, the prefill's rotations on the matrix
  cores. deploy/compose/qwen3.8-27b-paro.yaml serves them on one card. A ParoQuant container made
  with the PR builds is refused: convert it again with --reuse <old container>
- Gemma 4 26B-A4B with z-lab's DFlash drafter (PR #88; Perire): arch/gemma4 serves Google's bf16
  checkpoint with the routed experts in block fp8 or bf16 (data/recipes/gemma4-26b-a4b-df*.recipe),
  and libr4d gains its windowed paged attention, the fused sandwich-norm joins, expert_scale and
  the sharded draft head's pair_argmax. A prefill chunk runs as two slices with its all-reduces
  beside the compute, bit-exact with the whole step. The 31B's global layers take the windowed
  attention too: an 11.5K-token prefill on two R9700s in 3.1 s instead of 4.9
- a startup that logs nothing for two minutes says where it waits (#89): the system call (a KFD
  request by name), the library that made it, and its stack, which addr2line resolves against the
  binary
- the prefix cache is kept in VRAM and host memory only (#70): the disk tier wrote every block and
  snapshot a prompt produced, and its copies never outlived the process. --prefix-cache-host-mib
  is renamed --prefix-cache-mib; --prefix-cache-dir and --prefix-cache-disk-mib are removed, and
  startup refuses all three with what replaced them. The compose files no longer mount /data, and
  .env no longer needs RADIANCE_STATE
- --prefix-cache-ttl S (default 3600, 0 never): a conversation no request has used for that long
  leaves the cache, whichever tier holds it; radiance:prefix_cache_expired_total counts them. A
  full host tier now drops its least recently used conversations
- --vision-tower-placement vram|ram (#84): ram keeps an encoder tower in pinned host memory and
  gives its VRAM to the KV cache, read over the link once an image
- timings carry ttft_ms and total_ms (#85): the server's own time from the request body's arrival
  to the first and the last token, queueing and tokenisation included
- a KV-cache recall that found a moved expert with nowhere to go stopped halfway and leaked the
  slots it had already freed: after it every recall failed, the cache stayed short until a
  restart, and requests failed with HTTP 500. A recall now waits for the slots it is owed and
  always finishes
- a tool result or tool-call arguments that are not JSON are no longer refused as nesting past
  1024 levels for their unmatched brackets (#90): only text that parses as JSON is held to the
  bound, since nothing else is parsed into a tree
- the host-pool refusal names the limit that applies to pinned memory (RAM less 1/64 and
  1.5 GiB), not ttm.pages_limit
- --embedding-placement auto, removed in 1.3.0, is refused with what replaced it
- for out-of-tree kernel libraries: gemm_nt_q gains two operands (pb1, pb2, the part bounds), and
  the loader requires identical op schemas, so a library built against 1.4.0 must be rebuilt. For
  architecture plugins, rad_op_declared(b, op) asks whether an op is in the loaded vocabulary
  without declaring it
2026-10-13 19:35:16 +02:00

227 lines
8.9 KiB
C++

/* directio.cpp -- see directio.h for why the page cache is not wanted here. */
#include "directio.h"
#include "../rad_internal.h"
#include <algorithm>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
namespace rad {
namespace {
/* The bounce is one megabyte, allocated on first misaligned use and never on the aligned path.
* Big enough that a misaligned read of a whole weight is a handful of syscalls; small enough that
* a DirectFile nobody misuses costs nothing. */
constexpr int64_t kBounceBytes = 1 << 20;
int64_t align_down(int64_t v, int64_t a) { return v / a * a; }
/* Fill `bytes` exactly. A short pread inside the file is a torn read, not a smaller one. */
int pread_exact(int fd, void* dst, int64_t bytes, int64_t off) {
int64_t done = 0;
while (done < bytes) {
ssize_t r = ::pread(fd, (uint8_t*)dst + done, (size_t)(bytes - done), (off_t)(off + done));
if (r < 0) {
if (errno == EINTR) continue;
return RAD_E_IO;
}
if (r == 0) return RAD_E_IO; /* end of file inside a range the caller said was there */
done += r;
}
return RAD_OK;
}
/* As much as the file has, which is what the aligned superset of a misaligned tail asks for.
* Negative is an error; otherwise the byte count. */
int64_t pread_some(int fd, void* dst, int64_t bytes, int64_t off) {
int64_t done = 0;
while (done < bytes) {
ssize_t r = ::pread(fd, (uint8_t*)dst + done, (size_t)(bytes - done), (off_t)(off + done));
if (r < 0) {
if (errno == EINTR) continue;
return RAD_E_IO;
}
if (r == 0) break;
done += r;
}
return done;
}
int pwrite_exact(int fd, const void* src, int64_t bytes, int64_t off) {
int64_t done = 0;
while (done < bytes) {
ssize_t w = ::pwrite(fd, (const uint8_t*)src + done, (size_t)(bytes - done),
(off_t)(off + done));
if (w <= 0) {
if (w < 0 && errno == EINTR) continue;
return RAD_E_IO;
}
done += w;
}
return RAD_OK;
}
} /* namespace */
DirectFile::~DirectFile() { close(); }
int DirectFile::open(const char* path, int flags) {
if (!path || !*path) return RAD_E_INVAL;
close();
const bool want_write = (flags & RAD_DIO_WRITE) != 0;
/* O_RDWR whenever writing, even if the caller only said RAD_DIO_WRITE: a write that does not
* cover a whole block has to read the block first (see write_at), and O_WRONLY would turn that
* into an EBADF at the worst possible moment. */
int oflags = want_write ? O_RDWR : O_RDONLY;
if (flags & RAD_DIO_CREATE) oflags |= O_CREAT;
if (flags & RAD_DIO_TRUNC) oflags |= O_TRUNC;
fd_ = ::open(path, oflags | O_DIRECT, 0600);
direct_ = fd_ >= 0;
if (fd_ < 0 && (errno == EINVAL || errno == ENOTSUP || errno == EOPNOTSUPP)) {
/* The filesystem, not the file: tmpfs and some network mounts refuse O_DIRECT outright. */
if (flags & RAD_DIO_ALLOW_BUFFERED) {
fd_ = ::open(path, oflags, 0600);
direct_ = false;
if (fd_ >= 0)
RAD_WARN("directio: %s does not support O_DIRECT; opened buffered. Reads will go "
"through the page cache, which is a second copy of every byte and evicts "
"the weight pool's own pages.", path);
} else {
RAD_ERR("directio: cannot open %s with O_DIRECT: %s. The tier directory must be on a "
"filesystem that supports it.", path, std::strerror(errno));
return RAD_E_IO;
}
}
if (fd_ < 0) {
RAD_ERR("directio: cannot open %s: %s", path, std::strerror(errno));
return RAD_E_IO;
}
return RAD_OK;
}
void DirectFile::close() {
if (fd_ >= 0) ::close(fd_);
fd_ = -1;
direct_ = false;
std::free(bounce_);
bounce_ = nullptr;
bounce_bytes_ = 0;
}
int64_t DirectFile::size() const {
if (fd_ < 0) return -1;
struct stat st{};
if (::fstat(fd_, &st) != 0) return -1;
return (int64_t)st.st_size;
}
int DirectFile::bounce_ready() {
if (bounce_) return RAD_OK;
void* p = nullptr;
if (posix_memalign(&p, (size_t)alignment(), (size_t)kBounceBytes) != 0 || !p)
return RAD_E_NOMEM;
bounce_ = (uint8_t*)p;
bounce_bytes_ = kBounceBytes;
return RAD_OK;
}
/* ------------------------------------------------------------------ read */
int DirectFile::read_at(void* dst, int64_t off, int64_t bytes) {
if (fd_ < 0) return RAD_E_STATE;
if (!dst || off < 0 || bytes < 0) return RAD_E_INVAL;
if (bytes == 0) return RAD_OK;
const int64_t A = alignment();
const bool aligned = (off % A) == 0 && (bytes % A) == 0 &&
((uintptr_t)dst % (uintptr_t)A) == 0;
/* THE PATH THAT MATTERS. One pread, straight into the caller's pinned buffer, no intermediate
* copy anywhere -- which is what makes the mover able to DMA from it (spec §5.4). Every extent
* the .rad container describes lands here, because the container aligns its movement units to
* RAD_ALIGN_UNIT and rad_dev_alloc aligns its pools to the same. */
if (aligned) return pread_exact(fd_, dst, bytes, off);
/* THE ONLY BOUNCE IN THE ENGINE'S READ PATH, and it is here because O_DIRECT gives no
* alternative: the kernel will EINVAL a transfer whose offset, length or buffer is not
* block-aligned, so an unaligned extent can either be read through an aligned superset and
* copied out, or not read at all. It is acceptable here and nowhere else for one reason --
* this path is not the mover's. It serves the tail of a file whose length is not a block
* multiple, and callers reading a header or a footer rather than a movement unit. Anything on
* the step path that finds itself here has a layout bug, not a performance problem. */
RAD_TRY(bounce_ready());
const int64_t lo = align_down(off, A);
const int64_t hi = align_up(off + bytes, A);
for (int64_t base = lo; base < hi; base += bounce_bytes_) {
const int64_t span = std::min(bounce_bytes_, hi - base);
const int64_t s = std::max(off, base);
const int64_t e = std::min(off + bytes, base + span);
if (e <= s) continue;
const int64_t got = pread_some(fd_, bounce_, span, base);
if (got < 0) return (int)got;
/* A short read is only survivable when the file ends past the slice the caller asked for.
* Otherwise the caller named bytes that are not in the file, which is RAD_E_IO and not a
* partial success (see read_at's contract in the header). */
if (got < e - base) return RAD_E_IO;
std::memcpy((uint8_t*)dst + (s - off), bounce_ + (s - base), (size_t)(e - s));
}
return RAD_OK;
}
/* ------------------------------------------------------------------ write */
/* The same shape as read_at -- one fast path for whole aligned blocks, and a read-modify-write for
* anything that does not cover a whole block. */
int DirectFile::write_at(const void* src, int64_t off, int64_t bytes) {
if (fd_ < 0) return RAD_E_STATE;
if (!src || off < 0 || bytes < 0) return RAD_E_INVAL;
if (bytes == 0) return RAD_OK;
const int64_t A = alignment();
const bool aligned = (off % A) == 0 && (bytes % A) == 0 &&
((uintptr_t)src % (uintptr_t)A) == 0;
if (aligned) return pwrite_exact(fd_, src, bytes, off);
RAD_TRY(bounce_ready());
const int64_t old_size = size();
if (old_size < 0) return RAD_E_IO;
const int64_t lo = align_down(off, A);
const int64_t hi = align_up(off + bytes, A);
for (int64_t base = lo; base < hi; base += bounce_bytes_) {
const int64_t span = std::min(bounce_bytes_, hi - base);
/* Read-modify-write the whole chunk, including blocks the request covers completely. That
* is a wasted read per interior block and it is deliberate: the branchless version is
* short enough to be obviously correct, and this path exists for correctness at a boundary,
* not for throughput. The aligned path above is the one that has to be fast. */
const int64_t got = pread_some(fd_, bounce_, span, base);
if (got < 0) return (int)got;
if (got < span) std::memset(bounce_ + got, 0, (size_t)(span - got));
const int64_t s = std::max(off, base);
const int64_t e = std::min(off + bytes, base + span);
if (e > s)
std::memcpy(bounce_ + (s - base), (const uint8_t*)src + (s - off), (size_t)(e - s));
RAD_TRY(pwrite_exact(fd_, bounce_, span, base));
}
/* The last chunk padded the file out to a block boundary. Cut it back, so size() reports what
* was actually written and a later read_at of the tail sees the real end of file. */
const int64_t want = std::max(old_size, off + bytes);
if (size() > want && ::ftruncate(fd_, (off_t)want) != 0) return RAD_E_IO;
return RAD_OK;
}
} /* namespace rad */