Skip to content

Laya router: the step-4 scorer in C++, routing each request to a device - #90

Merged
bong-water-water-bong merged 5 commits into
mainfrom
laya/scorer
Sep 25, 2026
Merged

bong-water-water-bong merged 5 commits into
mainfrom
laya/scorer

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

agent-b72a1b's Laya scorer work, brought onto current main.

What it adds

  • laya/scorer.{h,cpp} and laya/safetensors.{h,cpp}: the non-autoregressive step-4 scorer (ModernBERT-large encoder + RLCD head) in C++. Weights load from model.safetensors, tokens come from npu::Tokenizer, and nothing runs in Python.
  • npu::Tokenizer: gains token_id() and the ByteLevel regex, so ModernBERT's tokenizer.json encodes digit runs correctly.
  • laya/route.{h,cpp}: route_device(scorer, state, devices), which answers the fixed routing question among the candidate devices.
  • 1bit route --laya-model DIR --state TEXT [--devices ...]: prints the scorer's decision.
  • 1bit serve --laya-model DIR (with --device auto):
    • each chat or completion request goes to the device the scorer picks, among the ones this build can run a .gguf on (vulkan, hrx, zinc)
    • a device's backend starts the first time it is picked, so one model is not loaded on every device at once
    • a Q4NX model directory still asks the router, with npu as its only candidate.

Merge notes: the work was based on a pre-#69 main. Its serve.cpp rewrite (Backend/backend_for) is re-applied on today's Launch/launch_for structure, and launch_for is unchanged from main. Laya mode:

  • prepares a Launch per candidate device, and starts it lazily under a lock
  • uses the adaptive router's in-flight accounting and each backend's own drop_model/set_model
  • refuses to combine with --adaptive, --embedding/--reranking, --prefill-device and --lean.

Tested on Strix Halo: ctest 8/8:

  • laya_gate_root and laya_gate_typed_decisions: raw logits ≤ 1e-3, act logits ≤ 1e-3 relative, argmax identical, probabilities ≤ 1e-4 against the Python reference, on the pinned checkpoints.
  • laya_route_e2e: a request reaches the Laya-chosen backend, across vulkan and zinc, plus the npu-only decision.
  • smoke_serve, think_split, npu_*.

🤖 Generated with Claude Code

bong-water-water-bong and others added 3 commits September 25, 2026 15:13
…nd route requests per device

The non-autoregressive System 1 scorer (1bit-MONSTER src/laya_scorer.cpp on
backup/laya-and-results-2026-09-22) lands as laya/scorer.{h,cpp} +
laya/safetensors.{h,cpp} (Apache-2.0, zero Python at runtime): ModernBERT-large
encoder + RLCD head, weights via model.safetensors, tokens via npu::Tokenizer
(tokenizer.json). npu::Tokenizer gains token_id() and the Rust-tokenizers
ByteLevel regex so ModernBERT's tokenizer.json encodes digit runs correctly.

Gated against the Python reference on the pinned root and typed-decisions
checkpoints: tests/laya_gate.cpp compares raw logits (<= 1e-3), act logits
(<= 1e-3 rel), argmax (identical) and probabilities (<= 1e-4). Both pass.

Wired into the router: 1bit serve --laya-model routes each request by the
scorer (replacing the auto->vulkan hardcode) and lazily starts the chosen
device's backend; 1bit route prints the decision. tests/laya_route_e2e.sh
proves a request reaches the Laya-chosen backend.
… dirs, GPU backends for .gguf)

route_device now takes a candidate device list. A .gguf routes among vulkan,
hrx and zinc (NPU is never offered for a .gguf, so a request can no longer
route to a device that cannot run it), and an NPU model directory routes to
npu through the same path. 1bit route gains --devices. The e2e test now
proves routing across more than one device (vulkan and zinc) plus the
npu-only decision.
@context7

context7 Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Docs7 for 1bit-monster/engine

Result Status Action
Deployment ➖ Not used —
Content review ➖ Did not run. This site has no agent runs available this month. Wait for the monthly reset or check your Docs7 plan. —

Commit ad47f86

@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit ad47f86)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

69 - Partially compliant

Compliant requirements:

  • Fix the gap where Lemonade's llamacpp backend forwards certain routes to the backend, but 1bit serve answered 404
  • On vulkan, hrx and rocm, 1bit serve now forwards /v1/responses, /tokenize, /detokenize, /apply-template, GET /slots, POST /slots/?action=…, GET /props, GET /metrics to the llama-server behind the model
  • On npu, zinc and mlx these routes answer 501 with a clear message
  • docs/serve.md lists the routes
  • Measured on Strix Halo, Lemonade's test/server_llm.py --wrapped-server onebit shows improvement from 19/31 to 23/31 passing tests for Vulkan and HRX

Non-compliant requirements:

  • None

Requires further human verification:

  • None
⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incorrect device routing for NPU model directories

When using --laya-model with a Q4NX model directory, the code attempts to route the request to the NPU device, but the logic in run_serve function incorrectly assumes that the device is always one of the GGUF devices (vulkan, hrx, zinc). This can lead to incorrect routing decisions when the model directory is intended for NPU only.

        // A Q4NX model directory runs on the NPU only; with --laya-model the router is still asked
        // (with npu its only candidate), so every device the engine serves goes through Laya.
        if (!o.laya_model.empty()) {
            onebit::laya::Scorer scorer;
            if (!scorer.load(o.laya_model)) throw std::runtime_error("laya: " + scorer.error());
            const std::string dev = onebit::laya::route_device(scorer, o.model, {"npu"});
            std::fprintf(stderr, "1bit serve: laya routes %s to %s (Q4NX model directory)\n", model_id(o).c_str(), dev.c_str());
        }
#endif
Potential race condition in backend startup

The ensure function in serve_child uses a mutex (start_mu) to protect the started vector, but there's a potential race condition where a backend could be started by one thread while another thread is checking if it's already started. This could lead to multiple processes being started for the same backend.

auto ensure = [&](size_t i) -> bool {
    std::lock_guard<std::mutex> lock(start_mu);
    if (started[i]) return true;
    const Launch& b = backends[i];
    std::fprintf(stderr, "1bit serve: %s on %s (%s), picked by laya\n", id.c_str(), b.device.c_str(), b.argv[0].c_str());
    auto c = std::make_unique<Child>(b.argv, b.env, b.port);
    if (!c->wait_ready(std::chrono::seconds(600), g_stop)) return false;
    started[i] = c.get();
    children.push_back(std::move(c));
    return true;
};
Potential memory allocation in hot path

The score function in Scorer class allocates several vectors (seqs, markers, input_ids, attn, mpos, mmask, h, tmp, qkv, attn_out, q, k, v, scores, hn, qkv, attn_out, tmp, tmp2, ffn, m, mn, m1, logits, act_logits, z, e, gp, rp) on the stack. If the number of questions or the length of the input text is large, this could lead to stack overflow, especially in a high-concurrency environment.

bool Scorer::score(const std::string& state, const std::vector<Question>& questions,
                   std::vector<Answer>& answers, RawOutput* raw) {
    err_.clear();
    std::unique_ptr<onebit::npu::Tokenizer> tok;
    try {
        tok = std::make_unique<onebit::npu::Tokenizer>(tok_path_);
    } catch (const std::exception& e) {
        err_ = std::string("load tokenizer: ") + e.what();
        return false;
    }
    const int N = (int)questions.size();
    if (N == 0) { answers.clear(); return true; }

    // ---- build token sequences + markers (laya/common.py:build_sequence) ----
    std::vector<std::vector<int>> seqs(N), markers(N);
    std::vector<int> qtype(N);
    for (int i = 0; i < N; ++i) {
        const Question& q = questions[i];
        std::vector<std::string> opts;
        int qt;
        if (q.type == "choice") {
            qt = 0;
            for (auto& kv : q.criteria)
                opts.push_back(kv.second.empty() ? kv.first : kv.first + ": " + kv.second);
        } else if (q.type == "score") {
            qt = 1;
            for (size_t j = 0; j < q.criteria.size(); ++j)
                opts.push_back("level " + std::to_string(j) + ": " + q.criteria[j].second);
        } else {
            qt = 2;
            const std::string *fc = nullptr, *tc = nullptr;
            for (auto& kv : q.criteria) { if (kv.first == "false") fc = &kv.second; else if (kv.first == "true") tc = &kv.second; }
            opts.push_back("false: " + (fc && !fc->empty() ? *fc : "no, the statement does not hold"));
            opts.push_back("true: " + (tc && !tc->empty() ? *tc : "yes, the statement holds"));
        }
        qtype[i] = qt;

        std::vector<int> head_ids = encode(*tok, q.type + " question: " + q.instructions);
        std::vector<std::vector<int>> opt_ids(opts.size());
        for (size_t oi = 0; oi < opts.size(); ++oi) {
            std::vector<int> o = encode(*tok, " " + opts[oi]);
            if ((int)o.size() > 48) o.resize(48);
            opt_ids[oi].push_back(mask_id_);
            opt_ids[oi].insert(opt_ids[oi].end(), o.begin(), o.end());
        }
        int opt_budget = head_max_len_;
        for (auto& o : opt_ids) opt_budget -= (int)o.size();
        if (opt_budget < 16) {
            int per = std::max(4, (head_max_len_ - 16) / std::max(1, (int)opt_ids.size()));
            for (auto& o : opt_ids) o.resize(std::min((size_t)per, o.size()));
            opt_budget = head_max_len_;
            for (auto& o : opt_ids) opt_budget -= (int)o.size();
        }
        int hb = std::max(8, opt_budget);
        if ((int)head_ids.size() > hb) head_ids.resize(hb);

        std::vector<int> ids;
        ids.push_back(cls_id_);
        ids.insert(ids.end(), head_ids.begin(), head_ids.end());
        ids.push_back(sep_id_);
        for (auto& o : opt_ids) {
            markers[i].push_back((int)ids.size());
            ids.insert(ids.end(), o.begin(), o.end());
        }
        ids.push_back(sep_id_);
        int room = std::max(0, max_len_ - (int)ids.size() - 1);
        std::vector<int> st = encode(*tok, state);
        if ((int)st.size() > room) st.resize(room);  // truncate_left=false
        ids.insert(ids.end(), st.begin(), st.end());
        ids.push_back(sep_id_);
        if ((int)ids.size() > max_len_) ids.resize(max_len_);
        seqs[i] = std::move(ids);
        std::vector<int> m2;
        for (int mm : markers[i]) if (mm < max_len_) m2.push_back(mm);
        markers[i] = std::move(m2);
    }

    // ---- collate (pad to L, K) ----
    int L = 0, K = 0;
    for (int i = 0; i < N; ++i) { L = std::max(L, (int)seqs[i].size()); K = std::max(K, (int)markers[i].size()); }
    std::vector<int64_t> input_ids((size_t)N * L, pad_id_);
    std::vector<int64_t> attn((size_t)N * L, 0);
    std::vector<int64_t> mpos((size_t)N * K, 0);
    std::vector<int8_t> mmask((size_t)N * K, 0);
    for (int i = 0; i < N; ++i) {
        for (size_t j = 0; j < seqs[i].size(); ++j) { input_ids[i * L + j] = seqs[i][j]; attn[i * L + j] = 1; }
        for (size_t j = 0; j < markers[i].size(); ++j) { mpos[i * K + j] = markers[i][j]; mmask[i * K + j] = 1; }
    }

    // ---- ModernBERT encoder forward ----
    std::vector<float> h((size_t)N * L * D);
    for (int b = 0; b < N; ++b)
        for (int t = 0; t < L; ++t)
            memcpy(h.data() + ((size_t)b * L + t) * D, tok_emb_.data() + (size_t)input_ids[b * L + t] * D, D * 4);
    {
        std::vector<float> tmp((size_t)N * L * D);
        layernorm(h.data(), emb_norm_.data(), nullptr, N * L, D, tmp.data());
        h.swap(tmp);
    }
    // RoPE tables
    auto build_cs = [](float theta, int L) {
        std::vector<float> cos(L * HD), sin(L * HD);
        for (int p = 0; p < L; ++p) for (int i = 0; i < HD / 2; ++i) {
            float inv = 1.0f / powf(theta, (float)(2 * i) / HD);
            float f = (float)p * inv;
            cos[p * HD + i] = cosf(f); cos[p * HD + HD/2 + i] = cosf(f);
            sin[p * HD + i] = sinf(f); sin[p * HD + HD/2 + i] = sinf(f);
        }
        return std::make_pair(cos, sin);
    };
    auto full_cs = build_cs(160000.0f, L);
    auto slid_cs = build_cs(10000.0f, L);
    std::vector<int8_t> key_pad((size_t)N * L);
    for (int b = 0; b < N; ++b) for (int t = 0; t < L; ++t) key_pad[b * L + t] = (attn[b * L + t] == 0);

    std::vector<float> hn((size_t)N * L * D), qkv((size_t)N * L * 3072), attn_out((size_t)N * L * D);
    std::vector<float> q((size_t)N * NHEAD * L * HD), k((size_t)N * NHEAD * L * HD), v((size_t)N * NHEAD * L * HD);
    std::vector<float> scores((size_t)L * L);
    for (int li = 0; li < NLAYER; ++li) {
        LayerW& Lw = layers_[li];
        bool is_full = (li % 3 == 0);
        const auto& cs = is_full ? full_cs : slid_cs;
        if (li > 0) layernorm(h.data(), Lw.attn_norm.data(), nullptr, N * L, D, hn.data());
        else memcpy(hn.data(), h.data(), h.size() * 4);
        for (int b = 0; b < N; ++b) for (int t = 0; t < L; ++t) {
            const float* x = hn.data() + ((size_t)b * L + t) * D;
            float* o = qkv.data() + ((size_t)b * L + t) * 3072;
            for (int j = 0; j < 3072; ++j) {
                const float* wr = Lw.Wqkv.data() + (size_t)j * D;
                float acc = 0; for (int c = 0; c < D; ++c) acc += x[c] * wr[c];
                o[j] = acc;
            }
        }
        for (int b = 0; b < N; ++b) for (int t = 0; t < L; ++t) {
            const float* qkv_t = qkv.data() + ((size_t)b * L + t) * 3072;
            for (int hd = 0; hd < NHEAD; ++hd) {
                float* qo = q.data() + (((size_t)b * NHEAD + hd) * L + t) * HD;
                float* ko = k.data() + (((size_t)b * NHEAD + hd) * L + t) * HD;
                float* vo = v.data() + (((size_t)b * NHEAD + hd) * L + t) * HD;
                for (int d = 0; d < HD; ++d) { qo[d] = qkv_t[hd*HD+d]; ko[d] = qkv_t[1024+hd*HD+d]; vo[d] = qkv_t[2048+hd*HD+d]; }
            }
        }
        // RoPE
        for (int b = 0; b < N; ++b) for (int hd = 0; hd < NHEAD; ++hd) for (int t = 0; t < L; ++t) {
            float* qo = q.data() + (((size_t)b * NHEAD + hd) * L + t) * HD;
            float* ko = k.data() + (((size_t)b * NHEAD + hd) * L + t) * HD;
            const float* c = cs.first.data() + (size_t)t * HD;
            const float* s = cs.second.data() + (size_t)t * HD;
            for (int d = 0; d < HD/2; ++d) {
                float qa = qo[d], qb = qo[d+HD/2];
                qo[d] = qa*c[d] - qb*s[d]; qo[d+HD/2] = qb*c[d+HD/2] + qa*s[d+HD/2];
                float ka = ko[d], kb = ko[d+HD/2];
                ko[d] = ka*c[d] - kb*s[d]; ko[d+HD/2] = kb*c[d+HD/2] + ka*s[d+HD/2];
            }
        }
        // attention
        const float scale = 1.0f / sqrtf((float)HD);
        for (int b = 0; b < N; ++b) for (int hd = 0; hd < NHEAD; ++hd) {
            const float* qb = q.data() + (((size_t)b * NHEAD + hd) * L) * HD;
            const float* kb = k.data() + (((size_t)b * NHEAD + hd) * L) * HD;
            for (int qi = 0; qi < L; ++qi) {
                float* sr = scores.data() + (size_t)qi * L;
                for (int kj = 0; kj < L; ++kj) {
                    if (attn[b * L + kj] == 0) { sr[kj] = -INFINITY; continue; }
                    if (!is_full && abs(qi - kj) > 64) { sr[kj] = -INFINITY; continue; }
                    const float* qr = qb + (size_t)qi * HD;
                    const float* kr = kb + (size_t)kj * HD;
                    float acc = 0; for (int d = 0; d < HD; ++d) acc += qr[d]*kr[d];
                    sr[kj] = acc * scale;
                }
                float mx = -INFINITY; for (int kj = 0; kj < L; ++kj) mx = fmaxf(mx, sr[kj]);
                float sum = 0; for (int kj = 0; kj < L; ++kj) { sr[kj] = expf(sr[kj]-mx); sum += sr[kj]; }
                for (int kj = 0; kj < L; ++kj) sr[kj] /= sum;
                float* orow = attn_out.data() + ((size_t)b * L + qi) * D + (size_t)hd * HD;
                for (int d = 0; d < HD; ++d) orow[d] = 0;
                for (int kj = 0; kj < L; ++kj) {
                    const float* vr = v.data() + (((size_t)b * NHEAD + hd) * L + kj) * HD;
                    float s = sr[kj];
                    for (int d = 0; d < HD; ++d) orow[d] += s * vr[d];
                }
            }
        }
        // Wo projection + residual
        for (int b = 0; b < N; ++b) for (int t = 0; t < L; ++t) {
            const float* x = attn_out.data() + ((size_t)b * L + t) * D;
            float* o = hn.data() + ((size_t)b * L + t) * D;
            for (int j = 0; j < D; ++j) {
                const float* wr = Lw.Wo.data() + (size_t)j * D;
                float acc = 0; for (int c = 0; c < D; ++c) acc += x[c] * wr[c];
                o[j] = acc;
            }
        }
        for (size_t z = 0; z < h.size(); ++z) h[z] += hn[z];
        // mlp_norm + geglu + residual
        layernorm(h.data(), Lw.mlp_norm.data(), nullptr, N * L, D, hn.data());
        geglu(hn.data(), Lw.Wi.data(), Lw.Wo_mlp.data(), N * L, attn_out.data());
        for (size_t z = 0; z < h.size(); ++z) h[z] += attn_out[z];
    }
    {
        std::vector<float> tmp((size_t)N * L * D);
        layernorm(h.data(), final_norm_.data(), nullptr, N * L, D, tmp.data());
        h.swap(tmp);
    }

    // ---- decision head ----
    for (int b = 0; b < N; ++b) {
        const float* te = type_emb_.data() + (size_t)qtype[b] * D;
        for (int t = 0; t < L; ++t) {
            float* o = h.data() + ((size_t)b * L + t) * D;
            for (int d = 0; d < D; ++d) o[d] += te[d];
        }
    }
    std::vector<float> tmp((size_t)N * L * D), tmp2((size_t)N * L * D), ffn((size_t)N * L * 4096);
    for (int layer = 0; layer < (int)head_layers_.size(); ++layer) {
        HeadLayerW& H = head_layers_[layer];
        layernorm(h.data(), H.n1w.data(), H.n1b.data(), N * L, D, tmp.data());
        mha(tmp.data(), H.in_w.data(), H.in_b.data(), H.out_w.data(), H.out_b.data(), N, L, key_pad.data(), tmp2.data());
        for (size_t z = 0; z < h.size(); ++z) h[z] += tmp2[z];
        layernorm(h.data(), H.n2w.data(), H.n2b.data(), N * L, D, tmp.data());
        linear(tmp.data(), H.l1w.data(), H.l1b.data(), N * L, D, 4096, ffn.data());
        for (size_t z = 0; z < (size_t)N * L * 4096; ++z) ffn[z] = fmaxf(0.0f, ffn[z]);  // relu
        linear(ffn.data(), H.l2w.data(), H.l2b.data(), N * L, 4096, D, tmp.data());
        for (size_t z = 0; z < h.size(); ++z) h[z] += tmp[z];
    }
    // gather markers
    std::vector<float> m((size_t)N * K * D);
    for (int b = 0; b < N; ++b) for (int kk = 0; kk < K; ++kk) {
        long long p = mpos[b * K + kk]; if (p < 0) p = 0;
        memcpy(m.data() + ((size_t)b * K + kk) * D, h.data() + ((size_t)b * L + p) * D, D * 4);
    }
    // scorer: LayerNorm -> Linear(D->D) -> GELU -> Linear(D->1)
    std::vector<float> mn((size_t)N * K * D), m1((size_t)N * K * D);
    layernorm(m.data(), sc0w.data(), sc0b.data(), N * K, D, mn.data());
    linear(mn.data(), sc1w.data(), sc1b.data(), N * K, D, D, m1.data());
    for (auto& x : m1) x = gelu(x);
    std::vector<float> logits((size_t)N * K);
    linear(m1.data(), sc3w.data(), sc3b.data(), N * K, D, 1, logits.data());
    for (int b = 0; b < N; ++b) for (int kk = 0; kk < K; ++kk)
        if (!mmask[b * K + kk]) logits[b * K + kk] = -1e4f;
    // act_head
    std::vector<float> act_logits((size_t)N * 2);
    for (int b = 0; b < N; ++b) {
        const float* lrow = logits.data() + (size_t)b * K;
        int kvalid = 0; for (int kk = 0; kk < K; ++kk) if (mmask[b * K + kk]) kvalid++;
        float keff = kvalid < 2 ? 2.0f : (float)kvalid;
        float p[64], mx = -INFINITY, sum = 0;
        for (int kk = 0; kk < K; ++kk) mx = fmaxf(mx, lrow[kk]);
        for (int kk = 0; kk < K; ++kk) { p[kk] = expf(lrow[kk] - mx); sum += p[kk]; }
        for (int kk = 0; kk < K; ++kk) p[kk] /= sum;
        float t1 = -INFINITY, t2 = -INFINITY;
        for (int kk = 0; kk < K; ++kk) { if (p[kk] > t1) { t2 = t1; t1 = p[kk]; } else if (p[kk] > t2) t2 = p[kk]; }
        if (t2 == -INFINITY) t2 = t1;
        float ent = 0; for (int kk = 0; kk < K; ++kk) ent += -p[kk] * logf(fmaxf(p[kk], 1e-9f));
        ent /= logf(keff);
        float feats[4] = { t1, t1 - t2, ent, keff / 255.0f };
        const float* pooled = h.data() + (size_t)b * L * D;
        float cat[1028];
        memcpy(cat, pooled, D * 4); memcpy(cat + D, feats, 4 * 4);
        float h256[256];
        linear(cat, act0w.data(), act0b.data(), 1, 1028, 256, h256);
        for (auto& x : h256) x = gelu(x);
        float a2[2];
        linear(h256, act2w.data(), act2b.data(), 1, 256, 2, a2);
        act_logits[b * 2] = a2[0]; act_logits[b * 2 + 1] = a2[1];
    }

    // raw pre-softmax outputs, for gating
    if (raw) {
        raw->logits.clear();
        raw->act_logits.clear();
        raw->logits.reserve(N);
        raw->act_logits.reserve(N);
        for (int b = 0; b < N; ++b) {
            raw->logits.emplace_back(logits.begin() + (size_t)b * K, logits.begin() + (size_t)(b + 1) * K);
            raw->act_logits.emplace_back(act_logits.begin() + (size_t)b * 2, act_logits.begin() + (size_t)(b + 1) * 2);
        }
    }

    // ---- temperature + softmax + confidence -> answers ----
    answers.clear();
    answers.reserve(N);
    for (int b = 0; b < N; ++b) {
        const Question& q = questions[b];
        int kvalid = 0; for (int kk = 0; kk < K; ++kk) if (mmask[b * K + kk]) kvalid++;
        // temperature bucket
        float tscale = temperature_[qtype[b]];
        std::string bucket;
        int sz = kvalid <= 2 ? 2 : kvalid <= 5 ? 5 : kvalid <= 10 ? 10 : 11;
        bucket = std::string(qtype[b] == 0 ? "choice:" : qtype[b] == 1 ? "score:" : "noul:") +
                 (sz == 2 ? "2" : sz == 5 ? "3-5" : sz == 10 ? "6-10" : "11+");
        for (auto& kv : temperature_by_options_) if (kv.first == bucket) { tscale = kv.second; break; }
        float z[64], mx = -INFINITY, sum = 0;
        for (int kk = 0; kk < kvalid; ++kk) { z[kk] = logits[b * K + kk] / tscale; mx = fmaxf(mx, z[kk]); }
        for (int kk = 0; kk < kvalid; ++kk) { z[kk] = expf(z[kk] - mx); sum += z[kk]; }
        for (int kk = 0; kk < kvalid; ++kk) z[kk] /= sum;
        float ent = 0; for (int kk = 0; kk < kvalid; ++kk) ent += -z[kk] * logf(fmaxf(z[kk], 1e-9f));
        float conf = 1.0f - ent / logf((float)kvalid);
        float actp = 0.0f;
        {
            // stable softmax: the act logits are large (~1e3-1e4), so expf alone overflows.
            float mx = fmaxf(act_logits[b*2], act_logits[b*2+1]);
            float ea0 = expf(act_logits[b*2] - mx), ea1 = expf(act_logits[b*2+1] - mx);
            actp = ea0 / (ea0 + ea1);
        }

        Answer a;
        a.type = q.type;
        a.confidence = std::clamp(conf, 0.0f, 1.0f);
        a.act_probability = actp;
        if (q.type == "choice") {
            int bi = 0; for (int kk = 1; kk < kvalid; ++kk) if (z[kk] > z[bi]) bi = kk;
            a.choice = q.criteria[bi].first;
            for (int kk = 0; kk < kvalid; ++kk) a.probabilities.push_back({q.criteria[kk].first, z[kk]});
        } else if (q.type == "score") {
            float exp_score = 0; for (int kk = 0; kk < kvalid; ++kk) exp_score += (float)kk * z[kk];
            a.score = exp_score;
            for (int kk = 0; kk < kvalid; ++kk) a.probabilities.push_back({std::to_string(kk), z[kk]});
        } else {
            a.noul = z[1];
            a.confidence = fmaxf(z[1], 1.0f - z[1]);
        }
        answers.push_back(std::move(a));
    }
    return true;
}

⚠️ Review coverage: The following files were not included in this review because of the token budget:

  • CMakeLists.txt
  • laya/route.h

… for bit)

One routing decision took about 15 s on one core of Strix Halo's 32.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

Preparing review...

@bong-water-water-bong
bong-water-water-bong enabled auto-merge (squash) September 25, 2026 18:26
@github-actions

Copy link
Copy Markdown

Persistent review updated to latest commit 6fc774b

@github-actions

Copy link
Copy Markdown

Persistent review updated to latest commit ad47f86

@bong-water-water-bong
bong-water-water-bong merged commit 27fefaf into main Sep 25, 2026
5 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the laya/scorer branch September 25, 2026 18:29
bong-water-water-bong added a commit that referenced this pull request Sep 25, 2026
Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
bong-water-water-bong added a commit that referenced this pull request Oct 5, 2026
…to our fork (Loom NaN backport) (#320)

* Bump HRX: llama.cpp 522dab4789ce (long-prompt fix), hrx-system to our fork 98d05d94a9f9

third_party/llama.cpp e57beb9721af -> 522dab4789ce (fork #89 runtime overhead,
#90: graph inputs never written back to host — every multi-ubatch prompt was
wrong since #82 — and the non-replay upload race).
third_party/hrx-system moves from ROCm/hrx-system 51b1739ae5fd to our fork
1bit-MONSTER/hrx-system 1bit/main 98d05d94a9f9 = the same AMD commit plus the
GFX11 wave64 lane-mask drain backport (#313 NaN) and two libhrx device knobs.
bump-hrx.yml now moves only the llama.cpp pin; hrx-system is rebased by hand.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* registry: regenerated at llama.cpp 522dab4789ce

Co-Authored-By: Claude Fable 5.1 <[email protected]>

---------

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>
bong-water-water-bong pushed a commit that referenced this pull request Oct 14, 2026
… root-caused a NaN in the same family

BUILT 05:52, compile caveat discharged: the patch applied to the real tree and the build exited 0
(graph-executor.cpp.o recompiled, libggml-hrx.so relinked), and the artefact was verified rather than
inferred - bin/libggml-hrx.so.0.18.0 contains BOTH GGML_HRX_PRODUCER_PROBE and GGML_HRX_PRODUCER_DEVICE,
object and library both timestamped 05:52, source backed up at /tmp/graph-executor.cpp.orig (sha256
cba0419f55e748ed...). Run 315dev launched behind the gate-aware wrapper that holds no lock and no GTT.
Still NOT claimed: that it arms anything at run time - the armed/anomaly lines are that evidence and do
not exist yet.

THE SIBLING FIX: a fork pin-bump (fb24c1f, Oct 5) turned out to describe a root-caused NaN fix in
exactly this family - llama.cpp e57beb9721af -> 522dab4789ce with fork #90: 'graph inputs never written
back to host - every multi-ubatch prompt was wrong since #82 - and the non-replay upload race'; and
hrx-system moved to our fork 98d05d94a9f9 carrying 'the GFX11 wave64 lane-mask drain backport (#313
NaN)'. The fork merge reads: '522dab478 Merge pull request #90 from 1bit-MONSTER/1bit/kv-nan-rootcause /
ggml-hrx: graph inputs are never written back to host; non-replay programs wait for their input upload'.

Two consequences: it is independent evidence that the family is real and reachable (a KV-NaN root cause
was found in the host write-back path); and the engine pin's tree ALREADY contains a PendingHostWriteback
path whose final step is memcpy(writeback.host_destination, writeback.mapped_source, size) - a DEVICE
MAPPING copied into a HOST DESTINATION (runtime/command-program-executor.cpp) - i.e. the exact line where
'the device wrote these bytes' and 'the mapping still holds something else' are decided, in the pin this
work runs on.

NOT established, and it decides whether #90 is a fix or already shipped: 522dab4789ce versus the engine
pin e44c9d01a. The local clone is SHALLOW - 522dab4789ce has no parents in it and e44c9d01a is absent -
so merge-base cannot run. If 522dab4789ce is an ancestor of the pin, #90 is already shipped and #315's
remaining faults are a different path; if it is a descendant, #90 is a forward-only fix candidate.
Settling it needs a full fetch or the engine repo's submodule, and it is worth doing before any other
run.
bong-water-water-bong pushed a commit that referenced this pull request Oct 14, 2026
…INT=1 to BOTH conditions, making the run self-falsifying

A design risk found while re-reading the publish path, and it would have made the run uninterpretable.

THE RISK: the chain needs the staging path to be WARM, but the record already measured the deferred
writeback route as COLD - '0 [hrx-wb-fp] records with the flag on over a complete 66/66 arm'. If it is
cold in the disc-rig config too, add_host_writeback is never called, no graph input is ever published by
this path, and A' and B' CANNOT DIFFER - a null that would say nothing about the mechanism while looking
like evidence against it.

THE AMENDMENT: both conditions now also carry GGML_HRX_WRITEBACK_FINGERPRINT=1 (the record's instrument;
inert unless set). Added to BOTH arms so it stays a non-difference, and it converts the risk from an
assumption into a measurement. Diagnostic added to the existing run; primary readout, arm counts and
stopping rule unchanged.

EXTRA READOUT, fixed in advance: [hrx-wb-fp] lines present -> the path WAS exercised and the A'/B'
contrast is meaningful. NO [hrx-wb-fp] line in either condition -> the path is cold in this configuration,
the chain has no first step here, the A'/B' null is STRUCTURALLY EXPECTED AND UNINFORMATIVE, and the
candidate is dead FOR THE DISC RIG - needing a configuration that actually exercises host-staging
downloads (the multi-ubatch/split path #90's message implicates) as a new pre-registration.

That second row is the point: the run is now SELF-FALSIFYING, so a null cannot be mistaken for a
refutation and cannot be mistaken for support either.
bong-water-water-bong pushed a commit that referenced this pull request Oct 14, 2026
…wo faces never co-occur

From the existing archive plus the runtime source, no GPU. This is a reclassification of the record and it is
decision-relevant.

INSTRUMENT INVENTORY (first time enumerated): ids-fp 3,412,028; ids-sync 1,160,376; hrx-copy site= and
hrx-copy-LOGITS 164,552 each; tensor-fp 134,180; hrx-logits-fp 104,720 (HOST-side logits, not previously
accounted for); finite-fp 1,698; buf 351; dl-fp 146; sentinel 90; producer 24.

THE REAL MARKERS, AND THE ONES THAT DO NOT EXIST. Over every arm log: 'HTTP 500' 0 arms, 'Invalid UTF' 0
arms (consistent with 315-invalid-utf8-never-observed.md), HSA_STATUS_ERROR 12 arms (Face A), MEMORY_FAULT
3 arms (Face A subset), 'Content-only output' 77 arms (the operative Face-B marker). The marker line is
'common_chat_peg_parse: unparsed Content-only output: <text>' and reading <text> is what splits it.

FACE B IS TWO POPULATIONS OF ABOUT EQUAL SIZE: GARBAGE-text 39, prose-text 38. Prose is coherent English -
"We'll assume the reader has a basic understanding of", verbatim, in roughly twenty separate arms: a normal
model answer that the PEG parser rejected because the harness wanted structured JSON. That is a
FORMAT-COMPLIANCE FAILURE, not corruption - and it cannot be a memory fault, because the text is coherent.
GARBAGE is genuine corruption: token soup, repetition loops ('gaps gaps gaps gaps'), replacement characters,
and the telling COHERENT-THEN-DISSOLVE form ('We'll assume thet ( size(, ((A(A.,,//值*( (名 (。 de*。*') -
correct for a few tokens, then degrades.

THE TWO FACES ARE DISJOINT AND FACE B IS NEVER NaN: GARBAGE arms device-NaN 0, face-A 0; prose arms
device-NaN 0, face-A 0; device tensor-fp NaN present in 0 of 89; and every hrx-logits-fp record in a Face-B
arm reports nan=0 across all 104,720. Face A arms have EMPTY output - they crash before emitting text (all
six v315t-r* arms, 315probe3-p1c). No arm shows both faces. So 'both faces' are not two symptoms of one
intermittent event; they are two disjoint populations, and Face B is mostly not a hardware fault at all.

TWO RECORD ARTEFACTS RETIRED. (1) oor and dup_pairs in hrx-ids-fp are not corruption signals: the probe
checks each id against ne0 = node->ne[0], and for ffn_moe_argsort that is the top-k count (observed 4 or 64),
not the expert-table size, so VALID expert indices are flagged oor permanently. dup_pairs confirms it -
977,666,493 duplicate pairs in the 23 CLEAN arms versus 10,333,698 in the probed Face-B arms. A metric three
orders of magnitude worse in the clean population carries no information; any reading built on oor/dup_pairs
should be withdrawn, including the 'clean permutation' framing. (2) tensor-fp-on-result_output (8 arms) and
hrx-logits-fp (56 arms) have an EMPTY intersection, so the device-vs-host logits comparison is still not
possible on the record - the same disjoint-instrument defect as before.

WHAT THIS CHANGES: the objective's Face B, 'invalid-UTF-8 output surfacing as HTTP 500 parse failures',
describes a phenomenon with ZERO occurrences on the record. What exists is Content-only output and about half
of it is the model declining to emit JSON. A campaign counting all Content-only outcomes as faults counts
benign format failures as failures and can never reach 'zero faults'; one that excludes them silently narrows
the criterion. This needs /goal-tweak before a >=60-arm campaign can be built on it - the decision is the
user's. WHAT IT DOES NOT CHANGE: Face A is real and well-marked (12 arms HSA_STATUS_ERROR, 3 MEMORY_FAULT),
its arms have empty output, and it remains the thing to explain; the 256-byte measurements sit on hidden-state
tensors and are unaffected.

NEW LEAD for the genuine half: GARBAGE arms show finite logits and coherent-then-dissolve text, the signature
of corruption entering PARTWAY THROUGH GENERATION - a property of the decode-loop state (KV cache / hidden
state carried across steps), not of a single forward pass, and consistent with the record's own PR #90 finding
in the input direction. That is where the next mechanism hunt should look, not at the prefill path where the
256-byte readings were taken.
bong-water-water-bong pushed a commit that referenced this pull request Oct 14, 2026
…ingle token

Reading the archive's SMALLEST NaN clusters - the least-propagated observations, therefore the closest to the
seed. No GPU.

THE MINIMAL OBSERVATIONS, RANKED: nan=4 ne0=1 of=4 (48 records, ffn_moe_weights-46 - a FOUR-element tensor,
ALL NaN); nan=40 ne0=1 of=40 (45, ffn_moe_weights-1); nan=64 ne0=64 of=64 (26, ffn_moe_probs-46 - softmax over
64 experts, ALL NaN); nan=8 ne0=1 of=40 (4); nan=192 ne0=2048 of=2048 (1, node_3305 - the ONLY sub-tensor
region found). Every one of the smallest NaN observations is an MoE routing tensor and every one is FULLY NaN,
down to a 4-element tensor. There is no smaller NaN region anywhere in the archive.

MECHANICALLY: ffn_moe_probs is the router softmax and ffn_moe_weights the top-k weight list; their inputs are
ffn_moe_gate (x @ W_gate) and ffn_moe_argsort. A softmax producing all-NaN over 64 experts means its input was
all -inf (the 0/0 case) or already contained NaN, and ffn_moe_gate-* is observed fully NaN at many layers. So:
hidden state x -> ffn_moe_gate -> softmax -> ffn_moe_probs (ALL NaN) -> top-k/weights -> ffn_moe_weights (ALL
NaN) -> MUL_MAT_ID over expert weights -> ffn_moe_weighted/out/down -> residual -> every downstream tensor. The
fault's most primitive observable manifestation is therefore: for a SINGLE TOKEN the MoE router output is
entirely NaN, which requires the hidden state fed to that router to already carry NaN.

THE ONE SUB-TENSOR REGION: node_3305 nan=192 of 2048 ne0=2048 first=1856, and 1856+192 = 2048 EXACTLY - so the
NaN is the last 192 elements of a single 2048-element row, granules 29-31 of 32, running to the end of the row.
node_3305 = 71x46+39, i.e. layer 46 (the final layer), just past the residual ADD. That is the only place on
the record where a NaN region is smaller than its whole tensor and measurable. Everywhere else the NaN has
already filled the tensor.

NOTHING UPSTREAM IS OBSERVED: the graph's input embeddings are never fingerprinted - across 567 distinct tensor
names the record contains no inp_*, no embedding, no token tensor; the earliest tensor ever observed is l_out-0
(layer 0's output).

THE NUMBERS DO NOT SUPPORT A SINGLE CLEAN UNIT. The archive-wide NaN-count histogram is dominated by
whole-tensor NaN (1048576 11.0%, 610304 11.0%, 1572928 3.5%) - the propagated end state. 2048 is third at
5.1% and is the most common value for ne0 in {768, 5120, 576}. 44% of NaN counts are multiples of 2048 and 56%
are not. The fbar arms' first eight buffers ARE all exactly nan=2048 across widths 64..5120, which is striking,
but IT DOES NOT GENERALISE and is recorded as a property of those four arms, not of the fault. So no single
unit - row, wave, granule, or 8192-byte region - is supported archive-wide, and the earlier claims of that
shape in this directory are superseded.

CORRECTED SHAPE OF THE PROBLEM: (1) the input path is unobserved; (2) the router is the seed's first observable
victim, per token, entirely; (3) per-node localization is structurally unavailable (aliasing), so the record
cannot walk the fault back from the router to its origin; (4) Face A = NaN logits (unambiguous, result_output
fully NaN on a unique buffer), Face B = finite logits, ~half benign prose, disjoint from Face A.

The record already contains a finding pointing at (1): PR #90, 'graph inputs are never written back to host',
shipped as an ancestor of the pin. Combined with an input path that is never instrumented, and a mechanism
chain the objective itself states as 'graph input published to host by deferred non-coherent staging writeback
-> re-uploaded to device -> device computes from stale input', the highest-value next instrument is the input
embedding tensor, which has never been observed. No mechanism is claimed.
bong-water-water-bong pushed a commit that referenced this pull request Oct 14, 2026
…erified, and PRE-REGISTERED

The record can no longer localize anything in the graph: the fingerprint reads tensors AFTER COMPUTE (its own
code comment says so) and reads a BUFFER, so every name bound to a shared allocation reports one allocation's
final content - measured node_2948 === ffn_shexp-41 (identical hash 65f4b97ca74c), l_out-42 === l_out-44, and 47
l_out-N names collapsing to 4 hashes. Two things remain unmeasured and both are now instrumented.

THE INSTRUMENT (docs/315-campaign/tools/patch-input-f32-sync-twin.py), VERIFIED NOT ASSERTED: applies cleanly to
ggml-hrx.cpp with exactly one occurrence per anchor (4 of 4), is idempotent on a second run, and the patched
translation unit COMPILES with the exact build flags extracted from the real build directory
(315fp-build/compile_commands.json, -fsyntax-only, exit 0). It adds, both inert unless their env var is set:

(1) hrx-f32-sync - for every f32 tensor the fingerprint already reports, read the same bytes through
hrx_synchronous_d2h and print nan, first, row, a FULL CONTENT HASH, and nan_agree / hashes_equal against the
plain-memcpy line. The i32 branch has had this twin since the ids probe - and comparing its two reads produced
the record's 'the two read paths agree' negative - but the f32 branch has NEVER had it, and f32 tensors are the
ones that carry the fault. A content hash rather than a count, so a divergence confined to the tensor's interior
is still visible (this was the blind spot in the earlier fp/sync comparison).

(2) head:<N> - select the first N nodes, symmetric with the existing tail:<N>. The graph input
(inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd_inp, ubatch.n_tokens)) is created UNNAMED, so the
name list cannot match it and tail never reaches it - it sits at the head of graph->nodes. Across 567 observed
tensor names the record contains no inp_*, no embedding, no token tensor; the earliest thing ever observed is
l_out-0. The input is the one tensor upstream of everything, the objective's own mechanism chain is 'graph input
published to host by deferred non-coherent staging writeback -> re-uploaded to device -> device computes from
stale input', and the record's own PR #90 was an INPUT-path defect.

PRE-REGISTERED BEFORE ANY ARM RUNS (315-PREREGISTRATION-f32-readpath-and-graph-input.md), with the scored
outcomes fixed and non-prunable: on the f32 twin, hashes_equal=0 on a faulting arm with a 256-byte-aligned
differing region CONFIRMS read-path staleness for f32; hashes_equal=1 with nan>0 REFUTES it and leaves genuine
device state; a divergence on a clean arm is a different finding and does not satisfy the criterion. On the
graph input: input NaN while the first reader is clean means the seed is upstream of the graph; input clean with
l_out-0 NaN means the seed is inside layer 0; both clean yet faulting means the forward path is not where it
lives. An arm that does not fault is INVALID and will be reported as INVALID.

STANDING CORRECTIONS THIS CARRIES: (a) the objective's premise that 'arena knobs and transient reuse are already
falsified on the record' is CONTRADICTED by measurement - reuse is pervasive and is what made the
read-pattern/onset result look meaningful, so any acceptance argument resting on that premise needs revisiting
with the user; (b) Face B's stated definition has ZERO occurrences on the record (invalid UTF-8 0 arms, HTTP 500
0 arms) and needs /goal-tweak before a >=60-arm campaign can be scored against it.

No mechanism is named and no fix is proposed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant