Non-finite logits (#879): a guard with self-healing

Rarely a window's logits become NaN and the reply degenerates into token 0
('!') repeated (#606); the poison sits in the conversation's K/V and
recurrent state, so with the prompt cache every later turn stays poisoned
until it is read again from token 0. The q8_1 clamps (2b726ba, f6601bf,
7a0e302) remove the overflows found so far; this guard catches whatever
cause is left, and drops the poisoned state instead of serving it again.

On by default, as a fix: STRATA_NAN_GUARD=0 turns it off. It only reads
flags, so an answer without a NaN is the one the engine gives with the
guard off (compared byte for byte on a GPU, below).

Engine:
- every verify-window graph checks its head logits beside the greedy pick
  (logits_nonfinite_rows: 16 blocks a row, one flag word each, into mapped
  memory); Verifier::note_nonfinite reads them after the sync the window
  already does (run, run_slot_rows, batch_poll, pl_finish). No extra sync.
  With STRATA_NAN_GUARD=0 no kernel is recorded.
- --serve: a flagged window emits none of its tokens, one stderr line names
  the position and row, and the request ends with `DONE ... nonfinite` (a
  batch slot: `BDONE <slot> <n> nonfinite`; an admission: BADM <slot> 0).
  The next request first drops the live session, every checkpoint, every
  parked conversation (ConversationCache::clear) and every idle slot, and
  reads its prompt from token 0; a slot still running then finishes its
  reply and is not kept as a cache after it. A request that ends on it does
  not keep its conversation live (#1620's cancelled-read path is skipped
  for it).
  Covered: the serial decode loop (one GPU or a layer split), the
  --pipeline-windows decode loop of a layer split, the prompt's short tail
  (read_windows, and read_windows_pl with --pipeline-windows), and both
  batch-slot loops (run_slot_rows, and the --batch-groups pipeline's
  batch_poll). Not covered: the one-shot command line (no --serve) and the
  SYCL port in sycl/, which has its own verify.cpp and generate.cpp.
- STRATA_NAN_INJECT=<n>[,<n>...] (debug): the n-th window the guard checks
  reports row 0 as non-finite.

Server: EngineNonFinite on that finish. A request that has sent nothing yet
is sent again once (its usage is the retry's: the failed pass's DONE is
dropped); a reply that had started ends as "length"; a second fire on the
retry ends with an error (503 before an answer, an error event in a
stream). With replicas the router forgets what that replica held.
repeat_stop_tokens stays as the backstop.

Checks: serve tests (python -m unittest discover -s serve -t . -p
"test_*.py": 697 tests, OK, 11 skipped; NonFiniteLogits,
test_nonfinite_in_batch_mode and test_nonfinite_forgets_the_replica are
new). ctest with CUDA_VISIBLE_DEVICES=-1: 24 pass (conversation_cache_test
with clear() among them), 3 skip for want of a GPU, and the 43 GPU parity
tests were not run on a GPU. Built for CUDA 13.0 on Linux (GCC, sm_75 +
sm_86); generate.cpp, verify.cpp and verify_kernels.cu compile with
MSVC 2022 + CUDA 13.0, and conversation_cache_test and conv_cache_test
pass there; built for HIP (ROCm 7.0, gfx1100, compile only) and SYCL
(oneAPI 2026.1.1; it takes only the new declaration in verify_kernels.hpp
and the new inline ConversationCache::clear(), and calls neither).

On a GPU (RTX 3080, RVN IQ3_S, strata.service's one-GPU config):
- Answers, guard on vs STRATA_NAN_GUARD=0, at fixed expert sets
  (--pcie-frac 0 --adapt-every 0 --adapt-swaps 0 --mtp-max-t 1
  --suffix-draft 0, STRATA_IQ_MT_MIN=1 STRATA_PREFILL_CPU_SHARE=0), a chat
  replay of 2 x 4 turns on a 16K-token system prompt: byte-identical in 3
  pairs (one with STRATA_ROUTE_TAIL_SKIP=0, one with STRATA_SH_STREAM=0),
  and two guard-on runs 8 of 8. One more guard-off run differed from every
  other run from its first answer, and a second guard-off run matched the
  guard-on runs: a difference between runs of this config, not the guard.
- Cost, Nsight Systems: logits_nonfinite_kernel 424 launches, 4.77 us on
  average (7.78 us at most), 2.0 ms against 21.6 s of decode: 0.009%.
- STRATA_NAN_INJECT=1,60: the first window of a new chat flagged, the
  server sent the request again from token 0 and the client saw one normal
  answer; the 60th, mid-reply in the next turn, ended that reply as
  "length" after 58 tokens; the turn after it reused 0 cached tokens and
  answered normally. The engine logged exactly the two injected windows.
This commit is contained in:
lask3802 authored and Niko1221 committed 2026-10-11 12:01:22 +02:00
1 parent 248dbee5b8
commit f332a85570
13 files changed
+536 -13

No files matched your search

+61 -1
View File
@@ -513,7 +513,7 @@ Verifier::~Verifier() {
if (e) cudaEventDestroy(e);
if (arena_) cudaFree(arena_);
void* hosts[] = {h_tok_, h_step_, h_pos_, h_commit_, h_ple_, h_out_, h_x_, h_ids_, h_w_, h_seq_, h_flag_, h_ymiss_,
h_flagA_, h_plan_, h_flagB_, h_plan_err_};
h_flagA_, h_plan_, h_flagB_, h_plan_err_, h_nf_};
for (void* h : hosts)
if (h) cudaFreeHost(h);
}
@@ -609,6 +609,8 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
mapped((2 + T) * 4 + 16, (void**) &h_commit_, (void**) &m_commit_) &&
mapped(T * N * 4, (void**) &h_ple_, (void**) &m_ple_) &&
mapped(T * 4 + 16, (void**) &h_out_, (void**) &m_out_) &&
mapped((size_t) strata::kernels::kVerifyMaxT * strata::kernels::kNonfiniteBlocks * 4, (void**) &h_nf_,
(void**) &m_nf_) &&
mapped(T * N * 4, (void**) &h_x_, (void**) &m_x_) &&
mapped(T * K * 4, (void**) &h_ids_, (void**) &m_ids_) &&
mapped(T * K * 4, (void**) &h_w_, (void**) &m_w_) &&
@@ -1789,6 +1791,8 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
sp.temperature = 0.0f;
sample_tokens(head_logits_, T, (int) n_vocab_, nullptr, 0, sp, m_out_, cs);
}
// #879: whether each row's logits are all finite, beside the pick (read after the window's own sync)
if (nan_guard_on()) strata::kernels::logits_nonfinite_rows(head_logits_, T, (int) n_vocab_, m_nf_, cs);
}
stamp(g.n_layers, 1, 0);
return true;
@@ -2336,6 +2340,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
}
}
for (int t = 0; t < T; ++t) out[t] = ((volatile int32_t*) h_out_)[t];
note_nonfinite(T, nullptr);
if (static const bool dbg = std::getenv("STRATA_DBG_NAN") != nullptr; dbg) { // debug: the first non-finite head
static bool reported = false;
if (!reported) {
@@ -2869,6 +2874,7 @@ bool Verifier::run_slot_rows(const int* rows, int S, const int32_t* tokens, cons
if (le_ < g.n_layers) return next_ == nullptr || next_->run_slot_rows(rows, S, tokens, pos, pool, next_user_, out, err);
if (!sample_rows(S, err)) return false;
for (int t = 0; t < S; ++t) out[t] = ((volatile int32_t*) h_out_)[t];
note_nonfinite(S, last_pos_b_);
progress_at("decode");
progress_beat();
return true;
@@ -2932,6 +2938,7 @@ bool Verifier::run_slot_rows(const int* rows, int S, const int32_t* tokens, cons
if (le_ < g.n_layers) return next_ == nullptr || next_->run_slot_rows(rows, S, tokens, pos, pool, next_user_, out, err);
if (!sample_rows(S, err)) return false;
for (int t = 0; t < S; ++t) out[t] = ((volatile int32_t*) h_out_)[t];
note_nonfinite(S, last_pos_b_);
progress_at("decode");
progress_beat();
return true;
@@ -3088,6 +3095,7 @@ int Verifier::batch_poll(PoolMultiFn pool, void* user, std::string& err) {
if (last_stage()) {
if (!sample_rows(S, err)) { b_running_ = false; return -1; }
for (int t = 0; t < S; ++t) b_out_[t] = ((volatile int32_t*) h_out_)[t];
note_nonfinite(S, last_pos_b_);
}
++windows;
b_running_ = false;
@@ -3095,6 +3103,57 @@ int Verifier::batch_poll(PoolMultiFn pool, void* user, std::string& err) {
return 1;
}
// #879: STRATA_NAN_GUARD=0 turns the guard off (no kernel is recorded; the graphs are captured once, so it is read once)
bool Verifier::nan_guard_on() {
static const bool on = [] {
const char* v = std::getenv("STRATA_NAN_GUARD");
return v == nullptr || std::atoi(v) != 0;
}();
return on;
}
namespace {
// STRATA_NAN_INJECT=<n>[,<n>...] (debug): the windows (counted from 1 over the process) the guard reports as non-finite
const std::vector<long long>& nan_inject_list() {
static const std::vector<long long> list = [] {
std::vector<long long> v;
if (const char* e = std::getenv("STRATA_NAN_INJECT"))
for (const char* q = e; *q != 0;) {
char* end = nullptr;
const long long n = std::strtoll(q, &end, 10);
if (end == q) break;
if (n > 0) v.push_back(n);
q = *end == ',' ? end + 1 : end;
}
return v;
}();
return list;
}
std::atomic<long long> g_nf_windows{0};
} // namespace
void Verifier::note_nonfinite(int T, const int64_t* pos_b) {
nf_row_ = -1;
nf_pos_ = -1;
nf_mask_ = 0;
if (!nan_guard_on() || h_nf_ == nullptr || !last_stage()) return; // (a stage without the head checks nothing)
const volatile uint32_t* f = h_nf_;
for (int t = 0; t < T && t < strata::kernels::kVerifyMaxT; ++t)
for (int b = 0; b < strata::kernels::kNonfiniteBlocks; ++b)
if (f[t * strata::kernels::kNonfiniteBlocks + b] != 0) { nf_mask_ |= 1u << t; break; }
const std::vector<long long>& inject = nan_inject_list();
if (!inject.empty()) {
const long long k = g_nf_windows.fetch_add(1) + 1;
if (nf_mask_ == 0 && std::find(inject.begin(), inject.end(), k) != inject.end()) {
nf_mask_ = 1u;
std::fprintf(stderr, "strata verify: STRATA_NAN_INJECT: window %lld reported as non-finite\n", k);
}
}
for (int t = 0; t < T && nf_row_ < 0; ++t)
if ((nf_mask_ >> t) & 1u) nf_row_ = t;
if (nf_row_ >= 0) nf_pos_ = pos_b != nullptr ? pos_b[nf_row_] : last_pos0_ + nf_row_;
}
bool Verifier::copy_logits(int t, float* host) const {
if (next_ != nullptr) return next_->copy_logits(t, host); // a layer split: the head is on the last stage
if (head_logits_ == nullptr || host == nullptr || t < 0 || n_vocab_ <= 0) return false;
@@ -3354,6 +3413,7 @@ bool Verifier::pl_finish(int32_t* out, std::string& err) {
}
if (out != nullptr)
for (int t = 0; t < T; ++t) out[t] = ((volatile int32_t*) h_out_)[t];
note_nonfinite(T, nullptr);
progress_beat();
return true;
}