From 7a3a6d4652c7745a8be3d68b9562558e107f98c9 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Tue, 10 Nov 2026 08:11:56 -0400 Subject: [PATCH 1/2] tests: pin #1909's batch-check fix, which merged with no test (engine#1905) #1909 made the NPU_BATCH_CHECK oracle restore the prefix it replays from instead of reset()ing it. It changed q4nx_forward.cpp only, so nothing fails if the reset comes back. tests/batch_check_state_test.cpp runs the forward in host-GEMM mode (it skips with 77 unless ONEBIT_TEST_Q4NX names a container, like mtp_batch_all and gdn_batch_order). It splits six ids into chunks of 3, 2 and 1, the last being the serve path's split-off final token. After each chunk it checks that the forward holds exactly the positions run so far, then checks the last logits against plain step(). NPU_BATCH_CHECK is a per-process static, so it is set before the first call, and the reference is step(), which the oracle does not touch. On Qwen3-0.6B: main 4/4 pass (rel-L1 0). With q4nx_forward.cpp from before #1909: 3 FAIL. The forward holds only the last chunk, and argmax is 4605 vs 368. docs/npu.md gets the oracle's paragraph; it was documented nowhere. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 9 +++ docs/npu.md | 7 ++ tests/batch_check_state_test.cpp | 123 +++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 tests/batch_check_state_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 050a9c8c1..ef5632fb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1589,6 +1589,14 @@ endif() target_link_libraries(gdn_batch_order_test PRIVATE onebit_npu_forward) add_dependencies(npu_sampling_test gdn_batch_order_test) add_test(NAME gdn_batch_order COMMAND gdn_batch_order_test) + # engine#1905: the NPU_BATCH_CHECK oracle restores the prefix it replays from instead of resetting it, so a chunk + # at start_pos > 0 is compared (and then run) from the state the batched pass continues. Host-GEMM, no device; + # skips (77) unless ONEBIT_TEST_Q4NX names a container. + add_executable(batch_check_state_test tests/batch_check_state_test.cpp) + target_include_directories(batch_check_state_test PRIVATE npu/forward/include) + target_link_libraries(batch_check_state_test PRIVATE onebit_npu_forward) + add_dependencies(npu_sampling_test batch_check_state_test) + add_test(NAME batch_check_state COMMAND batch_check_state_test) # The MTP drafter measured against the real base model (tools/mtp_acceptance.cpp): draft, verify through # step_batch_all(), commit the accepted prefix, repeat. The head runs on the host in the pb16i-exact # arithmetic, the base on the exact route. Needs the device, so it is a tool, not a ctest. @@ -1610,6 +1618,7 @@ endif() endif() set_tests_properties(mtp_batch_all PROPERTIES SKIP_RETURN_CODE 77) set_tests_properties(gdn_batch_order PROPERTIES SKIP_RETURN_CODE 77) + set_tests_properties(batch_check_state PROPERTIES SKIP_RETURN_CODE 77) # The fast-lane end-to-end test (npu_lane_e2e) is retired: its kernels were FastFlowLM-derived # and its reference logits are lost. The NPU check is the private dx add-on's from-source # check, outside this repository (docs/npu.md, "Checking the NPU"). diff --git a/docs/npu.md b/docs/npu.md index 98d287a95..6862549d4 100644 --- a/docs/npu.md +++ b/docs/npu.md @@ -1981,6 +1981,13 @@ the images it writes. `--live --match` scores both **at the loop's own chain positions** (so the two per-depth tables are one measurement sampled two ways), and `--batchcheck` takes the verify batch's M-curve. +`NPU_BATCH_CHECK=1` is the oracle underneath that check. Every `step_batch` first replays its rows with sequential +`step()` calls and prints `[batchcheck] M=.. positions=a..b` with the argmax at the last row and a bitwise row count +(`identical=N differing=0`). Both passes continue the same prefix: the oracle snapshots the first `start_pos` +positions before the replay and restores them afterwards (engine#1905). So a chunk after the first, or the serve +path's split-off last token, is compared from the state the batched pass really continues, and the flag leaves the +run's own output unchanged. `tests/batch_check_state_test.cpp` pins both properties. + `tools/mtp_depth_pool.py` pools those tickets. It refuses a pool it cannot read (a repeated tail, a cell scored twice, a short read, a mixed budget, a mixed window identity) and, since the tau=0.6 window, it also says when a depth is too **thin** to carry the 2 pp bar: one chain moves a depth's rate by `100/n` pp, so under 50 chains a single accepted draft diff --git a/tests/batch_check_state_test.cpp b/tests/batch_check_state_test.cpp new file mode 100644 index 000000000..cb59992dc --- /dev/null +++ b/tests/batch_check_state_test.cpp @@ -0,0 +1,123 @@ +// Copyright 2026 bong-water-water-bong +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// batch_check_state_test.cpp -- the NPU_BATCH_CHECK oracle must leave the forward exactly as it found it (engine#1905). +// +// The oracle replays a batch with sequential step() calls before the batched pass runs, then has to put the state +// back. It used reset(), which is the right state only at start_pos 0: for a later chunk it emptied the prefix, so the +// replay compared "position p from scratch" with "position p continuing the prefix" (a divergence on every chunk but +// the first) and the batched pass itself then ran with no prefix -- the debug flag changed the answer it was checking. +// +// Host-only: the forward runs in host-GEMM mode (no device, no XRT). Needs a Q4NX container, so it is skipped (77) +// unless ONEBIT_TEST_Q4NX points at one: +// +// ONEBIT_TEST_Q4NX=~/models/npu/Qwen3-0.6B ./batch_check_state_test +// +// NPU_BATCH_CHECK is read once per process (a static in step_batch), so it is set before the first call and the +// reference is plain step(), which the oracle does not touch. Checked here, for a prompt split the way the serve path +// splits it (a chunk, then a later chunk, then the one-token tail): +// [1] after every chunk the forward holds exactly the positions run so far -- a reset in the oracle leaves it +// holding only the last chunk's; +// [2] the last chunk's logits are the sequential reference's at that position (argmax exact, tight rel-L1). +#include "q4nx_forward.h" + +#include +#include +#include +#include +#include + +namespace { + +double rel_diff(const std::vector& a, const std::vector& b) { + if (a.size() != b.size() || a.empty()) return 1e9; + double num = 0, den = 0; + for (size_t i = 0; i < a.size(); i++) { + num += std::fabs((double)a[i] - (double)b[i]); + den += std::fabs((double)a[i]); + } + return num / (den + 1e-30); +} + +int argmax(const std::vector& v) { + int best = 0; + for (size_t i = 1; i < v.size(); i++) + if (v[i] > v[best]) best = (int)i; + return best; +} + +int fails = 0; +void check(bool cond, const std::string& what) { + std::printf("%s %s\n", cond ? "PASS" : "FAIL", what.c_str()); + if (!cond) fails++; +} + +// The forward holds exactly `n` positions: a snapshot of n succeeds and one of n + 1 does not. +bool holds_exactly(const Q4nxNpuForward& f, int n) { + Q4nxNpuForward::StateSnapshot s; + return f.snapshot_state(n, s) && !f.snapshot_state(n + 1, s); +} + +} // namespace + +int main() { + const char* dir = std::getenv("ONEBIT_TEST_Q4NX"); + if (!dir || !*dir) { + std::printf("SKIP: set ONEBIT_TEST_Q4NX to a Q4NX model directory to run this\n"); + return 77; + } + setenv("NPU_BATCH_CHECK", "1", 1); // before the first step_batch: the flag is read once per process + const std::string model = std::string(dir) + "/model.q4nx"; + Q4nxNpuForward fwd; + if (!fwd.init_host(model.c_str(), std::string(dir) + "/kernels")) { + std::printf("SKIP: init_host failed for %s (%s)\n", model.c_str(), fwd.last_error()); + return 77; + } + + // Values do not matter; the split does. Three chunks: [0,3), [3,5), [5,6) -- the last is the serve path's + // split-off final token, which is a step_batch at start_pos > 0 on every request. + const std::vector ids = {1, 2, 3, 4, 5, 6}; + const std::vector> chunks = {{0, 3}, {3, 5}, {5, 6}}; + + fwd.reset(); + std::vector ref; + for (size_t m = 0; m < ids.size(); m++) { + if (!fwd.step(ids[m], (int)m, ref)) { + std::printf("FAIL sequential step %zu failed: %s\n", m, fwd.last_error()); + return 1; + } + } + + fwd.reset(); + std::vector logits; + for (const auto& c : chunks) { + const std::vector chunk(ids.begin() + c.first, ids.begin() + c.second); + if (!fwd.step_batch(chunk, c.first, logits)) { + std::printf("FAIL step_batch(start=%d, M=%zu) failed: %s\n", c.first, chunk.size(), fwd.last_error()); + return 1; + } + check(holds_exactly(fwd, c.second), + "after the chunk at start " + std::to_string(c.first) + " the forward holds exactly " + + std::to_string(c.second) + " positions"); + } + const double rel = rel_diff(logits, ref); + char msg[200]; + std::snprintf(msg, sizeof msg, "last chunk's logits vs the sequential reference: rel-L1 %.2e, argmax %d vs %d", rel, + argmax(logits), argmax(ref)); + check(argmax(logits) == argmax(ref) && rel < 5e-3, msg); + + std::printf("%s (%d failed)\n", fails == 0 ? "ALL PASS" : "FAILED", fails); + return fails == 0 ? 0 : 1; +} From 44f63e8c5d56c27ace57a7f63206f9aa10818381 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Tue, 10 Nov 2026 09:21:31 -0400 Subject: [PATCH 2/2] tests: batch_check_state is bit-exact and says where it runs (review on #1919) On host GEMM the batched and sequential passes are the same arithmetic in the same order (rel-L1 measured 0.00e+00), so the logit check is now exact; a 5e-3 window could hide a one-row prefix leak. The CMake row and the test header now say the row exists only under ONEBIT_NPU, which ci.yml does not configure: a dev-box test, not CI coverage. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 3 ++- tests/batch_check_state_test.cpp | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ef5632fb3..c46286aca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1591,7 +1591,8 @@ endif() add_test(NAME gdn_batch_order COMMAND gdn_batch_order_test) # engine#1905: the NPU_BATCH_CHECK oracle restores the prefix it replays from instead of resetting it, so a chunk # at start_pos > 0 is compared (and then run) from the state the batched pass continues. Host-GEMM, no device; - # skips (77) unless ONEBIT_TEST_Q4NX names a container. + # skips (77) unless ONEBIT_TEST_Q4NX names a container. It links onebit_npu_forward, so the row exists only where + # ONEBIT_NPU configures; ci.yml does not, so this is a dev-box test (ONEBIT_NPU=ON plus a container), not CI coverage. add_executable(batch_check_state_test tests/batch_check_state_test.cpp) target_include_directories(batch_check_state_test PRIVATE npu/forward/include) target_link_libraries(batch_check_state_test PRIVATE onebit_npu_forward) diff --git a/tests/batch_check_state_test.cpp b/tests/batch_check_state_test.cpp index cb59992dc..deb1da76c 100644 --- a/tests/batch_check_state_test.cpp +++ b/tests/batch_check_state_test.cpp @@ -30,7 +30,11 @@ // splits it (a chunk, then a later chunk, then the one-token tail): // [1] after every chunk the forward holds exactly the positions run so far -- a reset in the oracle leaves it // holding only the last chunk's; -// [2] the last chunk's logits are the sequential reference's at that position (argmax exact, tight rel-L1). +// [2] the last chunk's logits equal the sequential reference's at that position, bit for bit: in host-GEMM mode +// both sides are the same arithmetic in the same order, so any tolerance would only hide a one-row prefix leak. +// +// Where it runs: the row is registered under if(ONEBIT_NPU) (it links onebit_npu_forward) and ci.yml configures +// without it, so it runs on a dev box with ONEBIT_NPU=ON and a container, not in CI. #include "q4nx_forward.h" #include @@ -116,7 +120,7 @@ int main() { char msg[200]; std::snprintf(msg, sizeof msg, "last chunk's logits vs the sequential reference: rel-L1 %.2e, argmax %d vs %d", rel, argmax(logits), argmax(ref)); - check(argmax(logits) == argmax(ref) && rel < 5e-3, msg); + check(argmax(logits) == argmax(ref) && rel == 0.0, msg); std::printf("%s (%d failed)\n", fails == 0 ? "ALL PASS" : "FAILED", fails); return fails == 0 ? 0 : 1;