radiance 1.11.0

New models:
- GLM-5.3-Flash reads pictures: its vision tower and processor (--image-tokens sets the budget),
  with decode kernels for a few rows and the DSA indexer split over the ranks
- Sage-A1.5 (fp8 and MXFP4) with its vision tower and a DFlash2 drafter
- Unlimited-OCR (bf16)
- Gemma 4 31B with z-lab's DFlash and RedHatAI's DFlash2 drafters; at four cards its all-reduces
  are fused with the junctions, overlapped with the compute and sent compressed
- Ornith-1.5-35B-A3B w4a8 compose files

Speed:
- Block drafters (DFlash, DFlash2) accept their drafts on the card, and the next step is issued
  ahead
- The four-rank all-reduce is faster for every --tp 4 model
- qwen35_w8a8: the norms and SiLU write the int8 linears' operands directly
- --attn-prefill-fp8 off|qk|pv|both: fp8 QK and PV legs for the h256 prefill attention (about +20%
  prefill on a 221K-token prompt for the 27B MXFP4 at TP2); lossy, off by default

Features:
- Control vectors: --control-vector off|on|disabled (disabled by default), a per-request switch,
  and the published Qwen3.8-Flash-Next refusal-direction vector (its license is in NOTICE)
- Krea 2: the `rebalance` request field, as ComfyUI's ConditioningKrea2Rebalance
- Cards with a small PCI BAR run, with kernel arguments kept in host memory
- Dashboard: live acceptance figures; statsdump rotates its file at a size cap
- KV caches every rank holds whole (GLM-5.3's latent and index caches) take one host-tier slot

Fixes:
- Long conversations taking turns no longer fall back to an early snapshot: a host-tier restore
  makes room as memory pressure does and waits a step for the previous turn's release (two
  conversations of 142K and 147K tokens sent back to back: 31.3 s per turn before, 2.8 s now)
- Kernels before Linux 6.16 (amdgpu below 3.64) no longer hang at startup: virtual memory
  management is turned off there, with a warning
- Gemma 4 dense scales its token embeddings as Hugging Face does (bf16), which changes its output
- The four-rank all-reduce fences prefill-sized messages, for a rare divergence after a fresh start
- GLM-5.3: every refusal names the setting and the reason; its template keeps string content
- Several pictures in one request no longer refuse processors that take one image at a time

Changes: Plugin ABI 26: out-of-tree plugins must be rebuilt. The environment switches the PRs
added are fixed rules or flags now (R4D_ATTN_FP8 is --attn-prefill-fp8).
This commit is contained in:
Deadcode committed 2026-10-23 11:57:52 +02:00
210 files changed
+14363 -2138

No files matched your search

+47 -7
View File
@@ -12,6 +12,7 @@
#include <cfloat>
#include <cmath>
#include <numeric>
#include <unordered_set>
namespace rad {
@@ -51,12 +52,6 @@ static uint64_t stream_key(uint32_t stream) {
}
}
/* rad_sample_uniform01's bits before the float conversion: one splitmix64 round over
* ((seed ^ key) * golden ratio + pos), cheap and stateless enough for a kernel. */
uint64_t rng_bits(uint64_t seed, uint64_t pos, uint32_t stream) {
return rad_sample_splitmix64((seed ^ stream_key(stream)) * 0x9E3779B97F4A7C15ull + pos);
}
float rng_uniform(uint64_t seed, uint64_t pos, uint32_t stream) {
/* 24 bits: 32 bits divided by 2^32 can round to 1.0f, which walks off the end of an inverse
* CDF. Calls rad_sample_uniform01 itself so host and device cannot drift apart. */
@@ -526,8 +521,48 @@ int32_t host_argmax(const float* logits, int64_t n_vocab) {
return (int32_t)best;
}
/* ================================================================== the chain */
void host_ngram_ban(CandidateSet& s, const int32_t* history, int64_t n_history, int32_t n,
int32_t window, const uint32_t* whitelist, int64_t wl_words) {
if (n <= 0 || !history || n_history < (int64_t)n) return;
const int64_t len = n_history;
const int64_t win = (window > 0 && window < len) ? window : len;
const int64_t start = len - win, end = len - n + 1;
if (end <= start) return;
const int64_t plen = (int64_t)n - 1, p0 = len - plen;
/* The set of tokens a continuation would complete an n-gram with, as the reference builds it:
* every n-gram in the window whose first n-1 tokens equal the current prefix contributes the
* token after them. */
std::unordered_set<int32_t> banned;
for (int64_t idx = start; idx < end; ++idx) {
bool match = true;
for (int64_t j = 0; j < plen; ++j) {
if (history[idx + j] != history[p0 + j]) { match = false; break; }
}
if (match) banned.insert(history[idx + plen]);
}
/* The reference's `banned.difference_update(self.whitelist)`, before any -inf: a token whose
* whitelist bit is set is never banned. A bit past the plane is not set, so a token outside
* it stays banned, as an id the reference's set does not contain. */
if (whitelist && wl_words > 0) {
for (auto it = banned.begin(); it != banned.end();) {
const int64_t id = *it;
if (id >= 0 && (id >> 5) < wl_words &&
(whitelist[(size_t)(id >> 5)] >> ((uint32_t)id & 31u)) & 1u)
it = banned.erase(it);
else
++it;
}
}
if (banned.empty()) return;
s.sorted = false; /* a -infinity candidate belongs after every finite one */
for (Candidate& c : s.c)
if (banned.count(c.id) != 0) c.logit = -INFINITY;
}
/* ================================================================== the chain */
int32_t host_sample_into(CandidateSet& s, const SamplingParams& sp, const HostRefInput& in,
ChainOrder order) {
/* Penalties first in both orders: they act on raw logits, the cuts on probabilities. */
@@ -547,6 +582,11 @@ int32_t host_sample_into(CandidateSet& s, const SamplingParams& sp, const HostRe
in.dry_breakers ? *in.dry_breakers : kNone);
}
/* The n-gram ban, after the penalties and before the greedy split: the device chain issues it
* there, so a greedy request sees the same masked distribution. Off at ngram_ban_n 0. */
host_ngram_ban(s, in.history, in.n_history, sp.ngram_ban_n, sp.ngram_window,
in.ngram_whitelist, in.ngram_whitelist_words);
/* Greedy skips the chain: penalties -> mask -> argmax. Temperature 0 collapses every other
* logit to -inf, so a mask applied after it could leave nothing. */
if (sp.greedy()) {
+19 -2
View File
@@ -71,7 +71,6 @@ enum : uint32_t {
RAD_RNG_XTC = 1 /* XTC's "does this fire at all" coin */
};
uint64_t rng_bits(uint64_t seed, uint64_t pos, uint32_t stream);
/* Uniform in [0,1) with 24 bits, the most a float resolves; more would let host and device
* disagree in the last place. */
float rng_uniform(uint64_t seed, uint64_t pos, uint32_t stream);
@@ -177,6 +176,19 @@ void host_dry(CandidateSet& s, const int32_t* history, int64_t n_history,
void host_xtc(CandidateSet& s, float probability, float threshold, size_t min_keep,
uint64_t seed, uint64_t pos);
/* The windowed no-repeat-ngram ban (`sample_ngram_ban`, Unlimited-OCR's
* SlidingWindowNoRepeatNgramProcessor): a candidate whose token would complete an `n`-gram the
* last `window` tokens of `history` already contain gets -infinity. `n` <= 0 is off, `window` <= 0
* is the whole history. It runs after the penalties and DRY and before every cut, so the greedy
* path sees it too.
*
* `whitelist` is the reference's whitelist (a SET bit means exempt from the ban, the same plane
* form as host_mask's, word w bit b covering token 32w + b): ids removed from the banned set
* before anything is written, as `banned.difference_update(self.whitelist)` does. Null or zero
* words is off -- the reference's empty whitelist, the default serving form. */
void host_ngram_ban(CandidateSet& s, const int32_t* history, int64_t n_history, int32_t n,
int32_t window, const uint32_t* whitelist = nullptr, int64_t wl_words = 0);
/* The grammar mask. One bit per token, set means admissible; a cleared bit sets the logit to -inf
* instead of removing the candidate, so nothing downstream is renumbered. `n_words` is
* ceil(n_vocab/32). */
@@ -203,7 +215,8 @@ constexpr int64_t kSampleCandidates = 1024;
constexpr int64_t kSampleMergeCandidates = 4096;
enum class ChainOrder {
Radiance = 0, /* penalties, DRY, temperature, mask, top-k, top-p/min-p/typical, XTC, pick */
/* penalties, DRY, n-gram ban, temperature, mask, top-k, top-p/min-p/typical, XTC, pick */
Radiance = 0,
LlamaCpp = 1 /* penalties, DRY, top-k, typical, top-p, min-p, XTC, temperature, pick */
};
@@ -215,6 +228,10 @@ struct HostRefInput {
int64_t total_context = 0; /* DRY's clamp; 0 means use n_history */
const uint32_t* bitmask = nullptr; /* grammar mask, or null */
int64_t n_words = 0;
/* The n-gram ban's whitelist bitmask (a SET bit exempts a token, host_ngram_ban), null =
* no exemptions. */
const uint32_t* ngram_whitelist = nullptr;
int64_t ngram_whitelist_words = 0;
uint64_t pos = 0; /* output position: the RNG's second coordinate */
const DryBreakers* dry_breakers = nullptr; /* null runs DRY with no breakers */
/* The chain's candidate width (SamplerConfig::max_candidates): top-k narrows every row to
+105 -13
View File
@@ -178,6 +178,12 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) {
/* One byte per history token, nonzero where the token is a whole DRY sequence breaker (never
* penalised). Resolved on the host, which has the vocabulary. */
buf_brk_ = decl("sampler.dry_breakers", RAD_U8, rows, cfg_.max_hist, RAD_BUF_PERSIST);
/* The n-gram ban's per-row {n-gram length, window}. Its own tiny plane because the params row
* is the fixed RadSampleParams wire format every sampler op shares. */
buf_ngram_ = decl("sampler.ngram", RAD_I32, rows, 2, RAD_BUF_PERSIST);
/* The ban's whitelist bitmask, this rank's shard, shaped like the mask plane: a SET bit
* exempts that column. The ban op indexes it by the sampled row, not by mask_row. */
buf_ngram_wl_ = decl("sampler.ngram_wl", RAD_U32, rows, mask_words_, RAD_BUF_PERSIST);
buf_mask_ = decl("sampler.mask", RAD_U32, rows, mask_words_, RAD_BUF_PERSIST);
buf_cand_i_ = decl("sampler.cand_idx", RAD_I32, rows, ncand, RAD_BUF_TRANSIENT);
buf_cand_v_ = decl("sampler.cand_val", RAD_F32, rows, ncand, RAD_BUF_TRANSIENT);
@@ -197,7 +203,8 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) {
buf_lp_g_ = decl("sampler.logprobs_gathered", RAD_F32, rows,
lp_width() * cfg_.world_size, RAD_BUF_PERSIST);
if (!buf_params_ || !buf_params_late_ || !buf_hist_ || !buf_brk_ || !buf_mask_ ||
if (!buf_params_ || !buf_params_late_ || !buf_hist_ || !buf_brk_ || !buf_ngram_ ||
!buf_ngram_wl_ || !buf_mask_ ||
!buf_cand_i_ || !buf_cand_v_ || !buf_lp_ ||
(vocab_parallel() && (!buf_pairs_ || !buf_gather_ || !buf_lp_g_))) {
RAD_ERR("sampler: a staging buffer failed to declare");
@@ -239,6 +246,7 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) {
op_penalties_ = op_vocab_off("sample_penalties");
op_dry_ = op_vocab_off("sample_dry");
op_ngram_ban_ = op_vocab_off("sample_ngram_ban");
op_temp_ = op_vocab("sample_temp");
op_mask_ = op_vocab("sample_mask");
op_topk_ = op_vocab_off("sample_topk");
@@ -320,6 +328,8 @@ int Sampler::stage_alloc() {
greedy_ready_ = false;
hist_stage_ = (int32_t*)rad_dev_alloc(rows * cfg_.max_hist * 4, RAD_MEM_HOST_PINNED);
brk_stage_ = (uint8_t*)rad_dev_alloc(rows * cfg_.max_hist, RAD_MEM_HOST_PINNED);
ngram_stage_ = (int32_t*)rad_dev_alloc(rows * 2 * 4, RAD_MEM_HOST_PINNED);
ngram_wl_stage_ = (uint32_t*)rad_dev_alloc(rows * mask_words_ * 4, RAD_MEM_HOST_PINNED);
mask_stage_ = (uint32_t*)rad_dev_alloc(rows * mask_words_ * 4, RAD_MEM_HOST_PINNED);
/* Logprobs readback; only rank 0 reads it. */
@@ -337,10 +347,10 @@ int Sampler::stage_alloc() {
}
if (!params_stage_ || !greedy_stage_ || !late_stage_ || !hist_stage_ || !brk_stage_ ||
!mask_stage_) {
!ngram_stage_ || !ngram_wl_stage_ || !mask_stage_) {
RAD_ERR("sampler: could not pin %s of staging",
humanb(rows * (3 * (int64_t)sizeof(DeviceSampleParams) + cfg_.max_hist * 5 +
mask_words_ * 4)).c_str());
mask_words_ * 8 + 8)).c_str());
stage_free();
return RAD_E_NOMEM;
}
@@ -353,6 +363,8 @@ void Sampler::stage_free() {
if (late_stage_) rad_dev_free(late_stage_, RAD_MEM_HOST_PINNED);
if (hist_stage_) rad_dev_free(hist_stage_, RAD_MEM_HOST_PINNED);
if (brk_stage_) rad_dev_free(brk_stage_, RAD_MEM_HOST_PINNED);
if (ngram_stage_) rad_dev_free(ngram_stage_, RAD_MEM_HOST_PINNED);
if (ngram_wl_stage_) rad_dev_free(ngram_wl_stage_, RAD_MEM_HOST_PINNED);
if (mask_stage_) rad_dev_free(mask_stage_, RAD_MEM_HOST_PINNED);
if (lp_host_) rad_dev_free(lp_host_, RAD_MEM_HOST_PINNED);
if (lp_ci_host_) rad_dev_free(lp_ci_host_, RAD_MEM_HOST_PINNED);
@@ -366,6 +378,8 @@ void Sampler::stage_free() {
cur_stage_ = nullptr;
hist_stage_ = nullptr;
brk_stage_ = nullptr;
ngram_stage_ = nullptr;
ngram_wl_stage_ = nullptr;
mask_stage_ = nullptr;
}
@@ -373,9 +387,9 @@ Sampler::~Sampler() { stage_free(); }
/* ================================================================== admission */
/* Whether the two history stages run, shared by admission, request setup and step staging so
* they agree. As in llama.cpp, a window of 0 turns a stage off and DRY needs a base >= 1. DRY
* also requires a positive multiplier; a negative one would reward repetition. */
/* Whether the history stages run, shared by admission, request setup and step staging so they
* agree. As in llama.cpp, a window of 0 turns a stage off and DRY needs a base >= 1. DRY also
* requires a positive multiplier; a negative one would reward repetition. */
static bool penalties_on(const SamplingParams& sp) {
return sp.penalty_last_n != 0 &&
(sp.rep_penalty != 1.0f || sp.freq_penalty != 0.0f || sp.pres_penalty != 0.0f);
@@ -385,6 +399,14 @@ static bool dry_on(const SamplingParams& sp) {
return sp.dry_multiplier > 0.0f && sp.dry_base >= 1.0f && sp.dry_penalty_last_n != 0;
}
/* The windowed no-repeat-ngram ban: n-gram length 0 is off (the request default). A window <= 0
* is legal and means the whole staged history, the un-windowed processor; it is not a separate
* switch, because the reference's windowed form is what the model ships and a ban with no window
* is still a ban. */
static bool ngram_ban_on(const SamplingParams& sp) {
return sp.ngram_ban_n > 0;
}
int Sampler::check_request(const SamplingParams& sp, std::string* why) const {
auto refuse = [&](const char* what, int st) {
if (why) *why = what;
@@ -418,6 +440,12 @@ int Sampler::check_request(const SamplingParams& sp, std::string* why) const {
/* The DRY kernels read an allowed length below 1 as 2; refused rather than rewritten. */
if (dry_on(sp) && sp.dry_allowed_length < 1)
return refuse("dry_allowed_length must be >= 1", RAD_E_INVAL);
/* The n-gram ban: an n-gram shorter than 2 is a single-token ban, which the reference defines
* (an empty prefix) and this chain implements, so 1 is accepted. A negative n or window has no
* meaning; window 0 is the whole staged history. */
if (sp.ngram_ban_n < 0) return refuse("ngram_ban_n is negative", RAD_E_INVAL);
if (sp.ngram_window < 0)
return refuse("ngram_window is negative (0 searches the whole history)", RAD_E_INVAL);
if (sp.n_probs > RAD_MAX_LOGPROBS) {
if (why) *why = "at most " + std::to_string(RAD_MAX_LOGPROBS) +
" logprobs alternatives may be asked for";
@@ -457,6 +485,7 @@ int Sampler::check_request(const SamplingParams& sp, std::string* why) const {
if (!missing) missing = need(!sp.greedy() && sp.temp_last, op_temp_late_,
"sample_temp (over the candidates, for temperature last)");
if (!missing) missing = need(dry_on(sp), op_dry_, "sample_dry");
if (!missing) missing = need(ngram_ban_on(sp), op_ngram_ban_, "sample_ngram_ban");
if (!missing) missing = need(!sp.grammar.empty() || sp.thinking_budget >= 0, op_mask_,
"sample_mask");
if (!missing) missing = need(penalties_on(sp), op_penalties_, "sample_penalties");
@@ -581,6 +610,8 @@ int Sampler::build_step(const std::vector<Request*>& reqs) {
n_seq_ = 0;
any_brk_ = false;
any_late_ = false;
any_ngram_ = false;
any_ngram_wl_ = false;
cur_stage_ = params_stage_;
lp_rows_.clear();
@@ -635,6 +666,7 @@ int Sampler::build_step(const std::vector<Request*>& reqs) {
const int64_t n_total = n_prompt + n_out;
const bool pen = penalties_on(sp);
const bool dry = dry_on(sp);
const bool ngram = ngram_ban_on(sp);
auto it = seqs_.find(r->id);
SeqState* st = it == seqs_.end() ? nullptr : &it->second;
@@ -695,6 +727,26 @@ int Sampler::build_step(const std::vector<Request*>& reqs) {
p.dry_penalty_last_n = sp.dry_penalty_last_n;
p.mask_row = -1;
/* The n-gram ban's pair, staged for every row so a row that turns it off cannot inherit
* its predecessor's values. */
ngram_stage_[2 * row] = (int32_t)sp.ngram_ban_n;
ngram_stage_[2 * row + 1] = (int32_t)sp.ngram_window;
/* The ban's whitelist, cut to this rank's shard like the grammar mask (shard_mask):
* global ids outside it exempt nothing here. Every row that runs the ban gets its
* words freshly written -- the plane is uploaded whole with the ban, so a row must
* never read another request's stale exemptions. */
if (ngram) {
uint32_t* w = ngram_wl_stage_ + row * mask_words_;
std::memset(w, 0, (size_t)mask_words_ * 4);
for (int32_t id : sp.ngram_whitelist) {
const int64_t at = (int64_t)id - cfg_.vocab_off;
if (at < 0 || at >= cfg_.n_vocab_local) continue;
w[at >> 5] |= 1u << (uint32_t)(at & 31);
any_ngram_wl_ = true;
}
}
/* Greedy over a split vocabulary is a top-1 chain row, not an argmax: sample_argmax
* writes a column of this rank's shard, not a token id. Top-k plus the merge produce
* global ids, and the pick over one candidate is deterministic. A greedy row asking
@@ -723,12 +775,14 @@ int Sampler::build_step(const std::vector<Request*>& reqs) {
any_late_ = true;
}
/* History for the penalties and DRY, staged only for rows that run one of them:
* prompt, output so far and, at draft position k, the k drafts before it (they are
* emitted only if accepted, as one token at a time). Oldest first, truncated to
/* History for the penalties, DRY and the n-gram ban, staged only for rows that run one
* of them: prompt, output so far and, at draft position k, the k drafts before it (they
* are emitted only if accepted, as one token at a time). Oldest first, truncated to
* max_hist; penalty_last_n still decides how much counts. Each row owns its row of the
* plane, so hist_off stays 0. */
if (pen || dry) {
* plane, so hist_off stays 0. The n-gram ban's own window is `ngram_window`, applied to
* this span, so the reference's update order holds at a verify: position k sees the
* drafts before it, as the processor would after emitting them. */
if (pen || dry || ngram) {
const int64_t len = n_total + k;
const int64_t take = std::min<int64_t>(len, cfg_.max_hist);
int32_t* h = hist_stage_ + row * cfg_.max_hist;
@@ -741,6 +795,9 @@ int Sampler::build_step(const std::vector<Request*>& reqs) {
p.hist_len = (int32_t)take;
if (pen) p.flags |= RAD_SP_PENALTY;
if (dry && stage_dry(p, h, take, brk, row)) p.flags |= RAD_SP_DRY;
/* A row whose history is shorter than its n-gram has nothing to ban (the reference
* skips it too), so the op is issued only when some row has a full n-gram. */
if (ngram && take >= sp.ngram_ban_n) any_ngram_ = true;
}
if (gr && !dead && gr->constrains()) {
@@ -881,7 +938,16 @@ int Sampler::build_greedy(int64_t n) {
n_seq_ = n;
any_brk_ = false;
any_late_ = false;
any_ngram_ = false;
any_ngram_wl_ = false;
cur_stage_ = greedy_stage_;
/* A drafter's rows run the argmax only, so the n-gram plane is cleared: `run` reads it from
* the staging, and a stale pair from an earlier request's build must not issue the ban over
* history that was not staged this step. The exemption plane needs no clear: `run` uploads
* it only beside an issued ban, every row that runs the ban has its words rewritten in full
* by the build that staged the pair (build_step), and rows below a zeroed pair are skipped
* by the op before they read a word. */
std::memset(ngram_stage_, 0, (size_t)cap * 2 * sizeof(int32_t));
if (greedy_ready_) return RAD_OK;
for (int64_t i = 0; i < cap; ++i) {
DeviceSampleParams& p = greedy_stage_[i];
@@ -904,7 +970,7 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
const RadStream s = rad_stream(c);
bool any_mask = false, any_greedy = false, any_chain = false;
bool any_penalty = false, any_dry = false, any_xtc = false;
bool any_penalty = false, any_dry = false, any_xtc = false, any_ngram = false;
bool any_topp = false, any_minp = false, any_typ = false, any_temp = false;
for (int64_t i = 0; i < n_seq_; ++i) {
const DeviceSampleParams& p = cur_stage_[i];
@@ -912,6 +978,11 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
any_penalty |= (p.flags & RAD_SP_PENALTY) != 0;
any_dry |= (p.flags & RAD_SP_DRY) != 0;
any_xtc |= (p.flags & RAD_SP_XTC) != 0;
/* The pair plane is read directly, not through cur_stage_: it is per-step staging the
* LAST builder filled (build_step writes a row's pair, build_greedy zeroes every pair),
* so it agrees with cur_stage_ by builder contract. A new builder must keep that
* contract: stage the pair with the params, zero it with the greedy ones. */
any_ngram |= ngram_stage_[2 * i] > 0;
if (p.flags & RAD_SP_GREEDY) any_greedy = true;
else {
any_chain = true;
@@ -941,13 +1012,21 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
if (late)
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_params_late_), late_stage_,
n_seq_ * (int64_t)sizeof(DeviceSampleParams), s));
if (any_penalty || any_dry)
if (any_penalty || any_dry || any_ngram)
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_hist_), hist_stage_,
n_seq_ * cfg_.max_hist * 4, s));
const bool pass_brk = any_dry && any_brk_;
if (pass_brk)
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_brk_), brk_stage_,
n_seq_ * cfg_.max_hist, s));
/* The ban's whitelist rides with the ban, and only a row that runs the ban reads it. */
const bool pass_wl = any_ngram && any_ngram_wl_;
if (any_ngram)
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_ngram_), ngram_stage_,
n_seq_ * 2 * (int64_t)sizeof(int32_t), s));
if (pass_wl)
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_ngram_wl_), ngram_wl_stage_,
n_seq_ * mask_words_ * 4, s));
if (any_mask)
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_mask_), mask_stage_,
n_seq_ * mask_words_ * 4, s));
@@ -956,6 +1035,8 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
const RadOperand o_params = opd_rows(buf_params_, n_seq_);
const RadOperand o_hist = opd_rows(buf_hist_, n_seq_);
const RadOperand o_brk = opd_rows(buf_brk_, n_seq_);
const RadOperand o_ngram = opd_rows(buf_ngram_, n_seq_);
const RadOperand o_ngram_wl = opd_rows(buf_ngram_wl_, n_seq_);
const RadOperand o_mask = opd_rows(buf_mask_, n_seq_);
const RadOperand o_ci = opd_rows(buf_cand_i_, n_seq_);
const RadOperand o_cv = opd_rows(buf_cand_v_, n_seq_);
@@ -983,6 +1064,17 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
if (any_dry)
RAD_TRY(issue(op_dry_, { o_logits, o_params, o_hist, pass_brk ? o_brk : opd_none() }));
/* The windowed no-repeat-ngram ban (Unlimited-OCR's SlidingWindowNoRepeatNgramProcessor),
* after the penalties and before every cut: a banned logit is -infinity for the greedy argmax
* below as well as for the chain, which is the reference processor's effect on the
* distribution. Off (the default) the op is not issued at all, so a request without it
* samples exactly as before. */
/* The whitelist operand is passed only when some staged row carries exemptions; absent
* means nothing is exempt, the reference's empty whitelist and the bit-identical default. */
if (any_ngram)
RAD_TRY(issue(op_ngram_ban_, { o_logits, o_params, o_hist, o_ngram,
pass_wl ? o_ngram_wl : opd_none() }));
/* Greedy rows take the argmax after penalties and the mask. In a mixed batch both the argmax
* and the chain run over the whole batch: the argmax writes every row, then the candidate
* stages skip RAD_SP_GREEDY rows so the pick overwrites only chain rows. One extra logits
+22 -1
View File
@@ -9,6 +9,7 @@
* Chain order (reordering changes which tokens survive):
*
* penalties (repetition / frequency / presence, then DRY)
* the windowed no-repeat-ngram ban
* temperature
* the grammar mask
* top-k
@@ -47,6 +48,9 @@
* sample_penalties M(range) n_vocab vocab_off? logits, params, history -> logits (inout)
* sample_dry M(range) n_vocab vocab_off? logits, params, history, breakers?
* -> logits (inout)
* sample_ngram_ban M(range) n_vocab vocab_off? logits, params, history, ngram,
* whitelist?
* -> logits (inout)
* sample_temp M(range) n_vocab logits, params -> logits (inout)
* sample_mask M(range) n_vocab logits, params, bitmask -> logits (inout)
* sample_topk M(range) n_vocab vocab_off? logits, params -> cand_idx, cand_val,
@@ -64,7 +68,9 @@
* a verify. `params` is [M] DeviceSampleParams rows. `history` is [M, max_hist] int32 global
* token ids, row m addressed by its hist_off/hist_len. `breakers` is [M, max_hist] uint8 beside
* it, nonzero where that token is a whole DRY sequence breaker. `bitmask` is [M, mask_words]
* uint32, addressed by the row's mask_row. `vocab_off` is this rank's first vocabulary row,
* uint32, addressed by the row's mask_row. The ban's `whitelist` is a [M, mask_words] uint32
* bitmask (a SET bit exempts that column from the ban) addressed by the row itself, absent when
* no staged row carries one. `vocab_off` is this rank's first vocabulary row,
* declared only when the vocabulary is split; the three ops that map between token ids and this
* rank's logit columns use it. Every op has a libref implementation, which is both its fallback
* and its oracle (spec §17).
@@ -232,6 +238,9 @@ private:
/* Declared op handles; RAD_NULL_HANDLE means unresolved, which admission refuses by name. */
rad_op op_penalties_ = RAD_NULL_HANDLE;
rad_op op_dry_ = RAD_NULL_HANDLE;
/* The windowed no-repeat-ngram ban (`sample_ngram_ban`), the stage Unlimited-OCR's
* generation_config asks for. It reads the same history plane as the two above. */
rad_op op_ngram_ban_ = RAD_NULL_HANDLE;
rad_op op_temp_ = RAD_NULL_HANDLE;
rad_op op_temp_late_ = RAD_NULL_HANDLE; /* sample_temp over the candidates (temp_last) */
rad_op op_mask_ = RAD_NULL_HANDLE;
@@ -252,6 +261,10 @@ private:
rad_buf buf_params_late_ = RAD_NULL_HANDLE; /* the late temperature's rows */
rad_buf buf_hist_ = RAD_NULL_HANDLE;
rad_buf buf_brk_ = RAD_NULL_HANDLE; /* DRY's single-token breaker marks, beside history */
rad_buf buf_ngram_ = RAD_NULL_HANDLE; /* the n-gram ban's {n, window} pair a row */
/* The ban's whitelist bitmask, [rows, mask_words] like the grammar mask plane: a SET bit
* exempts that column from the ban. Uploaded with the ban, only when a staged row has one. */
rad_buf buf_ngram_wl_ = RAD_NULL_HANDLE;
rad_buf buf_mask_ = RAD_NULL_HANDLE;
rad_buf buf_cand_i_ = RAD_NULL_HANDLE;
rad_buf buf_cand_v_ = RAD_NULL_HANDLE;
@@ -275,6 +288,8 @@ private:
const DeviceSampleParams* cur_stage_ = nullptr; /* what the last build staged */
int32_t* hist_stage_ = nullptr;
uint8_t* brk_stage_ = nullptr;
int32_t* ngram_stage_ = nullptr; /* [rows][2]: n-gram length, window */
uint32_t* ngram_wl_stage_ = nullptr; /* [rows][mask_words] exemption bitmask */
uint32_t* mask_stage_ = nullptr;
int64_t mask_words_ = 0; /* words for this rank's shard; what the mask op indexes */
@@ -282,6 +297,12 @@ private:
/* Whether any DRY row this step has breakers; only then is the breaker plane uploaded and
* passed to sample_dry. */
bool any_brk_ = false;
/* Whether any row this step runs the n-gram ban. Its history is the plane the penalties use,
* so this only decides whether that plane is uploaded and the op issued. */
bool any_ngram_ = false;
/* Whether any banned row this step whitelists a column of this rank's shard; only then is
* the exemption plane uploaded and passed to sample_ngram_ban. */
bool any_ngram_wl_ = false;
/* The whole-vocabulary mask the grammar fills, from which this rank's shard is cut. Used
* only when the head is sharded; see declare(). */