Archived
New models and formats: - libr3d: a kernel library for RDNA3 (gfx11) cards - nomic-embed-text-v2-moe (the nomicbert plugin) - Gemma 4 E2B with its per-layer MLP width, and Google's unmodified E2B/E4B directories in bf16 - DeepSeek-V4.1-Flash converted straight from its release: the Engram tables, the fused imatrix and rad-convert --chat-template for a release that ships no template - GGUF Q8_0, Q4_K and Q6_K tensors import as their planes; rad-convert writes them without a recipe - Qwen3.8-Flash-Next-FP8 converts and serves through qwen4exp_fp8 Speed: - hc_read's decode kernels as one grid (34.7 -> 25.2 us at 4 rows) and its tiled up-mix pipelined - gemm_fp8a8: the narrow form's scales staged through LDS (Flash-Next int8 at 32 rows 83.6 -> 68.3 us), the gated three-tile form at 8 waves (gate_up at 48 rows 272.9 -> 180.6 us) - Qwen3.8-27B: the delta net's out-projection read beside its decode recurrent update (x1 19.40 -> 19.10 ms a step), and the verify's split-KV attention sized by context depth (x3 at 180K 42.4 -> 40.3 ms a step) - Hy-Memory (HYV3) decode: fused QK norm, RoPE and KV store, residual adds folded into the norms, coalesced one-token expert GEMMs - spechub: the coupled verify walks the live candidates only (151 -> 31 us a launch) Speculation: - DFlash2: lossless coupled acceptance at T > 0 (--draft-coupling on) - A block drafter's proposals verified as a tree, on by default for the Gemma 4 31B's DFlash2, with a block-16 tree that runs its short block under load Features: - Decode-hot residency: --draft-expert-placement host, --expert-profile, --expert-pin-top, and --expert-stats FILE for the routed experts each layer selects - --ngram-placement ram=DIR: one n-gram table shared by engines on separate cards - Prefix cache: --prefix-resume exact (default) resumes a hit on the cold run's own chunk grid, so it answers bit-identically; snapshots at a head other prompts share, from 528-token prompts, and for tip-only one drafter window below the tip; hits on models whose KV groups have different block sizes; the snapshot store keeps a floor of 8 ahead of --host-headroom-mib - Typed tool-call argument tags; rad-chatparse --request renders and reads back one request as the server does - MiniMax-H3: keyframes ride a latent upscale - Gemma 3's fused all-reduce junction on the lossy wire Fixes: - A speculative step shorter than its band (an MTP step of 4 rows at gqa 8) no longer stops the engine with a scratch refusal at issue (#293) - --p2p off: prefill back to 1.12.0's rate; 1.13.0's push check read a host-memory slot over the link twice (#323) - A chat template that cannot render a conversation is refused at load, not mid-request - imatrix entries found under llama.cpp's spelling; the W4A8 prefill's tail scale read; the GDN conv kernels on an f32 weight; dense integer rows at the group they compute (a Q8_0 weight at group 32) - --expert-profile refuses a profile recorded on another model - hc_read's one-grid wait traps past its deadline instead of reading a stale value Changes: Plugin ABI 31, unchanged. RADIANCE_DRAFT_TREE, DFLASH2_TREE_NODES, DFLASH2_TREE_TEMP and DFLASH_Q_TEMP are gone (trees run where the probe says they pay; temperatures 1.25 / 0.6).
197 lines
8.3 KiB
C++
197 lines
8.3 KiB
C++
/* mirror.cpp -- see mirror.h. */
|
|
#include "kvstream/mirror.h"
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include "kvstream/plan.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
|
|
namespace rad {
|
|
|
|
namespace {
|
|
int64_t ceil_div(int64_t a, int64_t b) { return b > 0 ? (a + b - 1) / b : 0; }
|
|
|
|
/* Each array starts 8-byte aligned: `ctl`'s counters are read as u64 pairs, and an odd block
|
|
* count or ring depth would misalign the next array. The pool base is RAD_ALIGN_UNIT aligned. */
|
|
int64_t align8(int64_t n) { return (n + 7) & ~(int64_t)7; }
|
|
|
|
/* The maps' one layout, shared by alloc(), init() and kvstream_map_bytes(). */
|
|
struct MapLayout {
|
|
int64_t res_bytes = 0, slot_block_bytes = 0, slot_state_bytes = 0, ctl_bytes = 0;
|
|
int64_t res = 0, slot_block = 0, slot_state = 0, ctl = 0;
|
|
int64_t bytes = 0; /* the whole reservation */
|
|
};
|
|
|
|
MapLayout map_layout(int64_t blocks, int64_t slots, int64_t layers) {
|
|
if (layers < 1) layers = 1;
|
|
MapLayout l;
|
|
l.res_bytes = layers * blocks * (int64_t)sizeof(int32_t);
|
|
l.slot_block_bytes = layers * slots * (int64_t)sizeof(int32_t);
|
|
l.slot_state_bytes = layers * slots * 2 * (int64_t)sizeof(int32_t);
|
|
l.ctl_bytes = 16 * (int64_t)sizeof(int32_t);
|
|
l.res = 0; /* res, block -> slot, one map a layer */
|
|
l.slot_block = l.res + align8(l.res_bytes); /* slot_block, slot -> block */
|
|
l.slot_state = l.slot_block + align8(l.slot_block_bytes);/* slot_state: epoch, heat */
|
|
l.ctl = l.slot_state + align8(l.slot_state_bytes);/* ctl, shared by the layers */
|
|
l.bytes = l.ctl + align8(l.ctl_bytes);
|
|
return l;
|
|
}
|
|
} /* namespace */
|
|
|
|
int64_t kvstream_map_bytes(int64_t blocks, int64_t slots, int64_t layers) {
|
|
return map_layout(blocks, slots, layers).bytes;
|
|
}
|
|
|
|
void KVStreamStore::cap(int32_t group, int64_t tokens) {
|
|
if (group < 0) return;
|
|
if ((size_t)group >= resident_tokens_.size()) resident_tokens_.resize((size_t)group + 1, 0);
|
|
resident_tokens_[(size_t)group] = tokens > 0 ? tokens : 0;
|
|
}
|
|
|
|
bool KVStreamStore::windowed(int32_t group) const {
|
|
return group >= 0 && (size_t)group < resident_tokens_.size() &&
|
|
resident_tokens_[(size_t)group] > 0;
|
|
}
|
|
|
|
int64_t KVStreamStore::resident_blocks(const KVGroupPlan& p) const {
|
|
const size_t g = (size_t)p.index;
|
|
if (g >= resident_tokens_.size() || resident_tokens_[g] <= 0 || p.block_size <= 0) return 0;
|
|
return ceil_div(resident_tokens_[g], p.block_size);
|
|
}
|
|
|
|
int64_t KVStreamStore::context_blocks(const KVGroupPlan& p, int64_t max_ctx,
|
|
int64_t max_seqs) const {
|
|
return kv_context_blocks(p.block_size, max_ctx, max_seqs);
|
|
}
|
|
|
|
void KVStreamStore::set_map(int32_t group, const Map& m) {
|
|
if (group < 0) return;
|
|
if ((size_t)group >= maps_.size()) maps_.resize((size_t)group + 1);
|
|
maps_[(size_t)group] = m;
|
|
}
|
|
|
|
const KVStreamStore::Map* KVStreamStore::map(int32_t group) const {
|
|
if (group < 0 || (size_t)group >= maps_.size()) return nullptr;
|
|
return &maps_[(size_t)group];
|
|
}
|
|
|
|
const RadKVStream* KVStreamStore::view(const KVGroupPlan* p, int32_t group) const {
|
|
/* A ring that covers the context is never capped, so such a group gets null here. */
|
|
if (!p || !p->streamed() || !windowed(group)) return nullptr;
|
|
const Map* m = map(group);
|
|
return m && m->host && m->res ? m : nullptr;
|
|
}
|
|
|
|
KVStreamCost KVStreamStore::cost(const std::vector<KVGroupPlan>& plans, int64_t max_ctx,
|
|
int64_t max_seqs) const {
|
|
KVStreamCost c;
|
|
for (const auto& p : plans) {
|
|
if (!windowed(p.index) || p.zero || p.kind != RAD_KV_FULL) continue;
|
|
const int64_t blocks = context_blocks(p, max_ctx, max_seqs);
|
|
const int64_t slots = std::min(resident_blocks(p), blocks);
|
|
if (blocks <= 0 || slots <= 0) continue;
|
|
c.host_bytes += blocks * p.bytes_per_block;
|
|
c.map_bytes += kvstream_map_bytes(blocks, slots, p.n_layers);
|
|
++c.groups;
|
|
}
|
|
return c;
|
|
}
|
|
|
|
int KVStreamStore::alloc(const std::vector<KVGroupPlan>& plans, int64_t max_ctx, int64_t max_seqs,
|
|
MemAllocator& host_alloc, Pool& kv_pool) {
|
|
for (const auto& p : plans) {
|
|
if (!windowed(p.index) || p.zero || p.kind != RAD_KV_FULL) continue;
|
|
const int64_t blocks = context_blocks(p, max_ctx, max_seqs);
|
|
const int64_t slots = std::min(resident_blocks(p), blocks);
|
|
if (blocks <= 0 || slots <= 0) continue;
|
|
|
|
Map m;
|
|
m.slots = slots;
|
|
m.block_bytes = p.layer_bytes_per_block();
|
|
m.host_layer_stride = blocks * m.block_bytes;
|
|
m.host_bytes = blocks * p.bytes_per_block;
|
|
m.host = static_cast<uint8_t*>(host_alloc.alloc(m.host_bytes, RAD_MEM_HOST_MAPPED));
|
|
if (!m.host) {
|
|
RAD_ERR("KV group '%s': the host mirror of %s (%lld blocks of %s) could not be "
|
|
"allocated", p.name.c_str(), humanb(m.host_bytes).c_str(), (long long)blocks,
|
|
humanb(p.bytes_per_block).c_str());
|
|
release(host_alloc);
|
|
return RAD_E_NOMEM;
|
|
}
|
|
/* A block nothing has written is still copied in on a miss, so its bytes must be defined. */
|
|
std::memset(m.host, 0, (size_t)m.host_bytes);
|
|
|
|
/* One reservation for all of the group's maps, from the KV pool so its report names them. */
|
|
const int64_t layers = p.n_layers > 0 ? p.n_layers : 1;
|
|
const MapLayout lay = map_layout(blocks, slots, layers);
|
|
const int64_t map_bytes = lay.bytes;
|
|
std::string what = "kv_stream_maps:" + p.name;
|
|
char* at = static_cast<char*>(kv_pool.reserve(what, map_bytes, RAD_ALIGN_UNIT, nullptr));
|
|
if (!at) {
|
|
RAD_ERR("KV group '%s': its residency map and ring bookkeeping (%s) did not fit the "
|
|
"KV pool", p.name.c_str(), humanb(map_bytes).c_str());
|
|
host_alloc.free(m.host, RAD_MEM_HOST_MAPPED);
|
|
release(host_alloc);
|
|
return RAD_E_NOMEM;
|
|
}
|
|
/* Residency is per layer, because the resolve copies one layer's block: a group-wide map
|
|
* would call a block resident in a layer whose bytes were never copied. The control
|
|
* block is the group's. */
|
|
m.res = reinterpret_cast<int32_t*>(at + lay.res);
|
|
m.slot_block = reinterpret_cast<int32_t*>(at + lay.slot_block);
|
|
m.slot_state = reinterpret_cast<int32_t*>(at + lay.slot_state);
|
|
m.ctl = reinterpret_cast<int32_t*>(at + lay.ctl);
|
|
set_map(p.index, m);
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
int KVStreamStore::init(const std::vector<KVGroupPlan>& plans, RadStream s) {
|
|
if (!s) {
|
|
RAD_ERR("kv streaming: the maps must be filled on a stream, and none was given");
|
|
return RAD_E_INVAL;
|
|
}
|
|
for (const auto& p : plans) {
|
|
const Map* m = map(p.index);
|
|
if (!m || !m->host || !m->res) continue;
|
|
/* The state a resolve expects (libref/ref_kvstream.cpp): both maps -1, slot state and
|
|
* control block 0. Every block starts absent. */
|
|
const MapLayout lay = map_layout((int64_t)p.n_blocks, m->slots,
|
|
p.n_layers > 0 ? p.n_layers : 1);
|
|
int rc = RAD_OK;
|
|
rc = rad_memset_async(m->res, 0xff, lay.res_bytes, s);
|
|
if (rc == RAD_OK) rc = rad_memset_async(m->slot_block, 0xff, lay.slot_block_bytes, s);
|
|
if (rc == RAD_OK) rc = rad_memset_async(m->slot_state, 0, lay.slot_state_bytes, s);
|
|
if (rc == RAD_OK) rc = rad_memset_async(m->ctl, 0, lay.ctl_bytes, s);
|
|
if (rc != RAD_OK) {
|
|
RAD_ERR("KV group '%s': the residency map could not be initialised: %s",
|
|
p.name.c_str(), rad_dev_last_error());
|
|
return rc;
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
void KVStreamStore::release(MemAllocator& host_alloc) {
|
|
for (auto& m : maps_) {
|
|
if (!m.host) continue;
|
|
host_alloc.free(m.host, RAD_MEM_HOST_MAPPED);
|
|
/* Cleared, so a second call is a no-op. */
|
|
m = Map{};
|
|
}
|
|
}
|
|
|
|
KVStreamStats KVStreamStore::stats(const std::vector<KVGroupPlan>& plans) const {
|
|
KVStreamStats st;
|
|
st.misses = (int64_t)misses_.load(std::memory_order_relaxed);
|
|
st.bytes_in = (int64_t)bytes_in_.load(std::memory_order_relaxed);
|
|
for (const auto& p : plans)
|
|
if (p.streamed()) st.resident_blocks += p.pool_blocks();
|
|
return st;
|
|
}
|
|
|
|
} /* namespace rad */
|