diff --git a/CMakeLists.txt b/CMakeLists.txt index d5d372dde..924f6c6f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -617,6 +617,12 @@ add_executable(prof_acc_test tests/prof_acc_test.cpp) target_include_directories(prof_acc_test PRIVATE npu/target/xdna) add_test(NAME prof_acc COMMAND prof_acc_test) add_dependencies(onebit prof_acc_test) +# engine#1783: a launch larger than the context it is routed to is refused, not run into its buffers +# (npu/target/xdna/launch_fit.h). Header-only for the same reason as prof_acc_test. +add_executable(launch_fit_test tests/launch_fit_test.cpp) +target_include_directories(launch_fit_test PRIVATE npu/target/xdna) +add_test(NAME launch_fit COMMAND launch_fit_test) +add_dependencies(onebit launch_fit_test) add_executable(npu_sampling_test tests/npu_sampling_test.cpp) # The shape rule for a 1.8 external-data initializer (npu/pack_initializer_shape.h, engine#1503's graph-initializer # reader). Those packs zero every `dims`, so a byte length has to become a shape -- and a shape inferred wrongly imports diff --git a/npu/target/xdna/launch_fit.h b/npu/target/xdna/launch_fit.h new file mode 100644 index 000000000..3a7a063dd --- /dev/null +++ b/npu/target/xdna/launch_fit.h @@ -0,0 +1,51 @@ +// 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/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). +// +// 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 +// activations into one sized MD x KD; nothing compared the two, so the process died with SIGSEGV after both ELFs had +// initialised and nothing said which launch was wrong. Only the pb16i branch checked M. This is the refusal for all of +// them, with the numbers in the message. +// +// A geometry field of 0 means the context's shape is not known yet, and is not checked: contexts are value-initialised +// and only init_elf() assigns MD (npu_engine_i8ctx_inc.h). +#ifndef ONEBIT_NPU_TARGET_XDNA_LAUNCH_FIT_H +#define ONEBIT_NPU_TARGET_XDNA_LAUNCH_FIT_H + +#include + +// Empty when an M x K by K x N launch fits a context built for MD x KD by KD x ND; otherwise why it does not. +inline std::string xdna_launch_misfit(int M, int K, int N, int MD, int KD, int ND) { + if (M < 1 || K < 1 || N < 1) + return "launch shape M=" + std::to_string(M) + " K=" + std::to_string(K) + " N=" + std::to_string(N) + + " has an empty dimension"; + std::string why; + auto over = [&](const char* name, int got, int cap) { + if (cap > 0 && got > cap) { + if (!why.empty()) why += ", "; + why += std::string(name) + "=" + std::to_string(got) + " > " + std::to_string(cap); + } + }; + over("M", M, MD); + over("K", K, KD); + over("N", N, ND); + if (why.empty()) return {}; + return "launch does not fit the role's context (" + why + "; context is M=" + std::to_string(MD) + + " K=" + std::to_string(KD) + " N=" + std::to_string(ND) + "): wrong role index for this shape?"; +} + +#endif diff --git a/npu/target/xdna/xdna_target.cpp b/npu/target/xdna/xdna_target.cpp index 6d59fc846..236e7c074 100644 --- a/npu/target/xdna/xdna_target.cpp +++ b/npu/target/xdna/xdna_target.cpp @@ -20,6 +20,7 @@ // // docs/npu-p4-target-split.md records what moved in A1 and what A2 (plan-driven setup) still owes. #include "target/xdna/xdna_target.h" +#include "target/xdna/launch_fit.h" // engine#1783: refuse a launch its context cannot hold #include "batch_width.h" #include "file_create.h" // dumps are created 0600, never through a symlink @@ -919,6 +920,16 @@ void XdnaTarget::npu_gemm(const GemvArgs& ar, std::vector& out, std::stri onebit::npu::narrow_takes(1, narrow_rows_)) kctx1 = ctxn_[ar.role].get(); I8Ctx& kctx = *kctx1; + // engine#1783: a launch larger than the context it was routed to overruns its BOs; refuse it with the numbers. + // The gemv family's KD is the padded width and its launches are one row by construction, so it is not checked here. + if (!kctx.gemv) { + const std::string misfit = xdna_launch_misfit(1, K, N, kctx.MD, kctx.KD, kctx.ND); + if (!misfit.empty()) { + err = "role " + std::to_string(ar.role) + ": " + misfit; + out.assign((size_t)N, 0.0f); + return; + } + } const bool prof = prof_; const auto tp = prof ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{}; double fill_ms = 0.0; // engine#1783: weight-cache fill time inside tp->tl, reported apart from `pack` @@ -1208,6 +1219,16 @@ void XdnaTarget::npu_gemm_rows(const GemvArgs& ar, std::vector& out, std: onebit::npu::narrow_takes(M, narrow_rows_)) kctxp = ctxn_[ar.role].get(); // a short chunk (a verify pass of a few rows) fits the narrow kernel I8Ctx& kctx = *kctxp; + // engine#1783: the int8 batched body packs K x N into a KD x ND BO and M x K into an MD x KD one, and nothing + // compared them: a caller that routed GU and D to role 0 got SIGSEGV after both ELFs initialised. Refuse instead. + { + const std::string misfit = xdna_launch_misfit(M, K, N, kctx.MD, kctx.KD, kctx.ND); + if (!misfit.empty()) { + err = "role " + std::to_string(ar.role) + ": " + misfit; + out.assign((size_t)M * (size_t)N, 0.0f); + return; + } + } const bool prof = prof_; const auto tp = prof ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{}; double fill_ms = 0.0; // engine#1783: weight-cache fill time inside tp->tl, reported apart from `pack` diff --git a/tests/launch_fit_test.cpp b/tests/launch_fit_test.cpp new file mode 100644 index 000000000..a6871d217 --- /dev/null +++ b/tests/launch_fit_test.cpp @@ -0,0 +1,48 @@ +// 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. + +// engine#1783: npu/target/xdna/launch_fit.h -- a launch larger than its context is refused, with the numbers. +#include "launch_fit.h" + +#include +#include + +static int failures = 0; +static void check(bool ok, const char* what) { + if (!ok) { std::printf("FAIL %s\n", what); failures++; } +} +static bool has(const std::string& s, const char* sub) { return s.find(sub) != std::string::npos; } + +int main() { + // The shapes from the standalone run: role 0's context is O (M=1536 K=768 N=1024). + check(xdna_launch_misfit(1536, 768, 1024, 1536, 768, 1024).empty(), "the context's own shape fits"); + check(xdna_launch_misfit(1, 768, 1024, 16, 768, 1024).empty(), "one decode row on a 16-row twin fits"); + check(xdna_launch_misfit(100, 700, 1000, 128, 768, 1024).empty(), "a smaller launch fits"); + const std::string gu = xdna_launch_misfit(1536, 768, 6144, 1536, 768, 1024); + check(has(gu, "N=6144 > 1024") && !has(gu, "K=768 >"), "GU on O's context: N named, K not"); + const std::string d = xdna_launch_misfit(1536, 3072, 1024, 1536, 768, 1024); + check(has(d, "K=3072 > 768"), "D on O's context: K named"); + const std::string m = xdna_launch_misfit(1536, 768, 1024, 128, 768, 1024); + check(has(m, "M=1536 > 128"), "more rows than the context holds"); + check(has(xdna_launch_misfit(200, 900, 2000, 128, 768, 1024), "M=200 > 128, K=900 > 768, N=2000 > 1024"), + "every dimension over is named"); + check(xdna_launch_misfit(1536, 3072, 6144, 0, 0, 0).empty(), "an unknown geometry (0) is not checked"); + check(!xdna_launch_misfit(0, 768, 1024, 128, 768, 1024).empty(), "an empty launch is refused"); + check(has(d, "wrong role index"), "the message says what to look at"); + + if (failures) { std::printf("launch_fit: %d failure(s)\n", failures); return 1; } + std::printf("launch_fit: ok\n"); + return 0; +}