From a6d25f213380dd03de1fab904075ba064e872c81 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Fri, 13 Nov 2026 05:36:24 -0400 Subject: [PATCH 1/3] tests: XdnaTarget::gemv() refuses a launch wider than its context, tested through the target (Refs #1783) #2349 added the refusal (launch_fit.h, applied in npu_gemm and npu_gemm_rows) and tested the rule on its own. This drives XdnaTarget::gemv() itself on a bare I8Ctx (geometry only, no buffers, no device), so removing the check from either call site crashes the test the way the #1783 harness crashed. Registered in both configures: the NPU one, and the CI one, which builds xdna_target.cpp into onebit_npu_forward_ci on the runtime stub. launch_fit.h's header comment said no CI job compiles xdna_target.cpp; it now says where it does. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 8 +++ npu/target/xdna/launch_fit.h | 6 +- tests/npu_gemm_shape_guard_test.cpp | 98 +++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 tests/npu_gemm_shape_guard_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 913a95d3b..88a72d0ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2252,6 +2252,10 @@ endif() 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) + # engine#1783: XdnaTarget::gemv() refuses a launch wider than its context, through the target itself (no device). + add_executable(npu_gemm_shape_guard_test tests/npu_gemm_shape_guard_test.cpp) + target_link_libraries(npu_gemm_shape_guard_test PRIVATE onebit_npu_forward) + add_test(NAME npu_gemm_shape_guard COMMAND npu_gemm_shape_guard_test) 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 @@ -2315,6 +2319,10 @@ if(ONEBIT_NPU_HOST AND NOT ONEBIT_NPU AND NOT WIN32 AND Python3_Interpreter_FOUN target_compile_definitions(onebit_npu_rt_ci_stub PUBLIC ONEBIT_NPU_RUNTIME_OWN) target_link_libraries(onebit_npu_rt_ci_stub PUBLIC Threads::Threads) onebit_npu_forward_library(onebit_npu_forward_ci onebit_npu_rt_ci_stub) + # engine#1783: the same gemv()-level shape-guard test as the NPU configure's, on the stub runtime (no device call). + add_executable(npu_gemm_shape_guard_test tests/npu_gemm_shape_guard_test.cpp) + target_link_libraries(npu_gemm_shape_guard_test PRIVATE onebit_npu_forward_ci) + add_test(NAME npu_gemm_shape_guard COMMAND npu_gemm_shape_guard_test) add_executable(batch_check_state_test tests/batch_check_state_test.cpp) target_link_libraries(batch_check_state_test PRIVATE onebit_npu_forward_ci) # direction B (tools/ci/ci_targets_check.py): ci.yml's --target list is a workflow file no lane can patch; the diff --git a/npu/target/xdna/launch_fit.h b/npu/target/xdna/launch_fit.h index 3a7a063dd..9ba282f00 100644 --- a/npu/target/xdna/launch_fit.h +++ b/npu/target/xdna/launch_fit.h @@ -12,8 +12,10 @@ // 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. -// npu/target/xdna/launch_fit.h -- does a launch fit the context it was routed to? Header-only so a test with no device -// arms it (xdna_target.cpp includes the private runtime header, so no CI job compiles it). +// npu/target/xdna/launch_fit.h -- does a launch fit the context it was routed to? Header-only so the rule is tested +// on its own (tests/launch_fit_test.cpp). xdna_target.cpp applies it in both launch paths, and CI compiles that file +// too: a configure without ONEBIT_NPU builds it into onebit_npu_forward_ci against the runtime stub, which is where +// tests/npu_gemm_shape_guard_test.cpp drives XdnaTarget::gemv() with no device. // // engine#1783: a standalone caller passed role 0 for every shape (O K768 N1024, GU K768 N6144, D K3072 N1024), so // GU and D ran on role 0's context. The int8 batched path packs K x N weights into a BO sized KD x ND and reads M x K diff --git a/tests/npu_gemm_shape_guard_test.cpp b/tests/npu_gemm_shape_guard_test.cpp new file mode 100644 index 000000000..ab0a8bf36 --- /dev/null +++ b/tests/npu_gemm_shape_guard_test.cpp @@ -0,0 +1,98 @@ +// 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. + +// npu_gemm_shape_guard_test (engine#1783): a GEMM call wider than the context it would launch on is refused by +// name, before any buffer is touched -- tested through XdnaTarget::gemv() itself, so removing the guard from +// either call site (npu_gemm, npu_gemm_rows) fails here. tests/launch_fit_test.cpp covers the rule +// (launch_fit.h) on its own; this covers that the target applies it. +// +// The defect (#2349 fixed it): XdnaTarget::gemv() never compared the call's M/K/N with the context's MD/KD/ND. A standalone harness +// that passed design index 0 for three designs (O K768 N1024, GU K768 N6144, D K3072 N1024) launched GU on O's +// context: the weight pack wrote 768 x 6144 into a 768 x 1024 weight buffer, and the process died with SIGSEGV and +// nothing on the device side to say why. +// +// This needs no device. The context is a bare I8Ctx with only its geometry set: its buffers were never allocated, +// so any path that reaches a pack, a quantise or a readback dereferences null and crashes the test: the refusal +// has to come first. That a call which fits still gets past it is the device rows' job (a fitting call needs real +// buffers); the PR body carries those rows. + +#include "target/xdna/xdna_target.h" +#include "npu_engine_i8ctx_inc.h" // I8Ctx + +#include +#include +#include +#include + +using npu::target::GemvArgs; +using npu::target::xdna::XdnaTarget; + +static int fails = 0; +static void check(bool ok, const char* what) { + std::printf("%s %s\n", ok ? "ok " : "FAIL", what); + if (!ok) fails++; +} + +// A context with a geometry and no buffers: the guard reads MD/KD/ND only. +static void bare_ctx(XdnaTarget& t, int role, int M, int K, int N) { + auto c = std::make_unique(); + c->MD = M; c->KD = K; c->ND = N; c->NL = 1; + t.ctx_ref(role) = std::move(c); +} + +static bool refused_by_shape(XdnaTarget& t, GemvArgs a, std::vector& out, std::string& err) { + err.clear(); + const bool ok = t.gemv(a, out, err); + return !ok && err.find("launch does not fit the role's context") != std::string::npos; +} + +int main() { + XdnaTarget t(nullptr); + bare_ctx(t, 0, 128, 768, 1024); // design index 0 is O: K768 N1024, 128 rows + + std::vector x((size_t)1536 * 3072, 1.0f), w((size_t)3072 * 6144, 0.01f), out; + std::string err; + GemvArgs a; + a.role = 0; + a.layer = 0; + a.x = x.data(); + a.Bmat = w.data(); + a.wkey = -1; + a.tag = "probe"; + + // The harness's crash, both bodies: GU's N and D's K on O's context. + a.M = 1; a.K = 768; a.N = 6144; a.a_k = 768; a.rows = false; + check(refused_by_shape(t, a, out, err), "one row, N 6144 on an N 1024 context: refused by shape"); + check(out.size() == 6144, "one row: the output is still sized to the call (N floats)"); + a.M = 128; a.rows = true; + check(refused_by_shape(t, a, out, err), "rows, N 6144 on an N 1024 context: refused by shape"); + check(out.size() == (size_t)128 * 6144, "rows: the output is still sized to the call (M x N floats)"); + a.M = 128; a.K = 3072; a.N = 1024; a.a_k = 3072; + check(refused_by_shape(t, a, out, err), "rows, K 3072 on a K 768 context: refused by shape"); + a.M = 1; a.rows = false; + check(refused_by_shape(t, a, out, err), "one row, K 3072 on a K 768 context: refused by shape"); + // More rows than the context holds: the activation and result buffers are MD rows deep. + a.M = 1536; a.K = 768; a.N = 1024; a.a_k = 768; a.rows = true; + check(refused_by_shape(t, a, out, err), "rows, M 1536 on a 128-row context: refused by shape"); + check(err.find("M=1536 > 128") != std::string::npos && err.find("context is M=128 K=768 N=1024") != std::string::npos, + "the message names the dimension that is over and the context's shape"); + // An empty launch (M=0) goes through the same check and is refused as empty before the buffers. + a.M = 0; a.K = 768; a.N = 1024; a.rows = true; + err.clear(); + check(!t.gemv(a, out, err) && err.find("empty dimension") != std::string::npos, "an empty launch: refused as empty"); + + std::printf("npu_gemm_shape_guard: %s\n", fails ? "FAIL" : "ok"); + return fails ? 1 : 0; +} From c0cebf7681950753fbc79c46b308315e8dc5d9c6 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Fri, 13 Nov 2026 05:40:33 -0400 Subject: [PATCH 2/3] tests: npu_gemm_shape_guard's comment points at where the device rows are (#2349's thread) Co-Authored-By: Claude Opus 5.5 --- tests/npu_gemm_shape_guard_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/npu_gemm_shape_guard_test.cpp b/tests/npu_gemm_shape_guard_test.cpp index ab0a8bf36..c901f00fe 100644 --- a/tests/npu_gemm_shape_guard_test.cpp +++ b/tests/npu_gemm_shape_guard_test.cpp @@ -26,7 +26,7 @@ // This needs no device. The context is a bare I8Ctx with only its geometry set: its buffers were never allocated, // so any path that reaches a pack, a quantise or a readback dereferences null and crashes the test: the refusal // has to come first. That a call which fits still gets past it is the device rows' job (a fitting call needs real -// buffers); the PR body carries those rows. +// buffers): engine#2349's thread carries them (Bolt-E4B and the served 27B, byte-equal, no refusals). #include "target/xdna/xdna_target.h" #include "npu_engine_i8ctx_inc.h" // I8Ctx From 66156db75d1d7a3edf4e6faf2ef815d33a276b39 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Fri, 13 Nov 2026 06:06:58 -0400 Subject: [PATCH 3/3] cmake: npu_gemm_shape_guard_test is a dependency of onebit, so CI's build-onebit step builds it (was Not Run) Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 88a72d0ef..14267ecd0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2256,6 +2256,7 @@ endif() add_executable(npu_gemm_shape_guard_test tests/npu_gemm_shape_guard_test.cpp) target_link_libraries(npu_gemm_shape_guard_test PRIVATE onebit_npu_forward) add_test(NAME npu_gemm_shape_guard COMMAND npu_gemm_shape_guard_test) + add_dependencies(onebit npu_gemm_shape_guard_test) # CI builds `onebit` only; without this the test is Not Run 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 @@ -2323,6 +2324,7 @@ if(ONEBIT_NPU_HOST AND NOT ONEBIT_NPU AND NOT WIN32 AND Python3_Interpreter_FOUN add_executable(npu_gemm_shape_guard_test tests/npu_gemm_shape_guard_test.cpp) target_link_libraries(npu_gemm_shape_guard_test PRIVATE onebit_npu_forward_ci) add_test(NAME npu_gemm_shape_guard COMMAND npu_gemm_shape_guard_test) + add_dependencies(onebit npu_gemm_shape_guard_test) # CI builds `onebit` only; without this the test is Not Run add_executable(batch_check_state_test tests/batch_check_state_test.cpp) target_link_libraries(batch_check_state_test PRIVATE onebit_npu_forward_ci) # direction B (tools/ci/ci_targets_check.py): ci.yml's --target list is a workflow file no lane can patch; the