From 86a3db23d90ece6db27b6b2645b6c544463dd795 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Tue, 10 Nov 2026 05:09:38 -0400 Subject: [PATCH] npu: bound tensor reads by the tensor, not by the end of the file Fixes #1881. Tensor::avail is measured from a tensor's start to the end of the FILE, and three bounds used it: pack.cpp's need() (every projection, both layer norms, q/k norm, lm_head) and lane.cpp's final-norm and embedding checks. Each caught a read past the file and none a read past the tensor into the next one -- every tensor but the last has neighbours. They now test Tensor::bytes; avail stays the mapping's outer bound. Two new npu_model_bounds arms build a tensor that is NOT last and over-read it (k_proj claiming four tiles of one; input_layernorm at half hidden x 2): both refused by name; with need() back on avail both pass silently. The lane packer over every container dir on strixhalo: the same 43 pack, and every refusal is unchanged. lane.cpp needs the NPU runtime and is not compiled here. Co-Authored-By: Claude Opus 5.5 --- npu/lane.cpp | 10 +++++++--- npu/pack.cpp | 11 ++++++++--- tests/npu_model_bounds_test.cpp | 27 +++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/npu/lane.cpp b/npu/lane.cpp index 303120367..f8414ed79 100644 --- a/npu/lane.cpp +++ b/npu/lane.cpp @@ -98,10 +98,14 @@ struct Lane::Impl { logits = make_bo(dev, kSmallBo); fnorm = make_bo(dev, kSmallBo); const Tensor& norm = model.tensor("model.norm.weight"); - if (norm.avail < size_t(d.hidden) * 2) throw std::runtime_error("model.q4nx: model.norm.weight runs past the end of the file"); + // engine#1881: bounded by the tensor (`bytes`), not by the end of the file (`avail`), which let a short + // tensor be read into its neighbour. + if (norm.bytes < size_t(d.hidden) * 2) + throw std::runtime_error("model.q4nx: model.norm.weight holds " + std::to_string(norm.bytes) + + " bytes, fewer than hidden x 2"); std::memcpy(fnorm->map(), norm.data, size_t(d.hidden) * 2); - if (model.tensor("model.embed_tokens.weight").avail < size_t(d.vocab) * size_t(d.hidden) * 2) - throw std::runtime_error("model.q4nx: model.embed_tokens.weight runs past the end of the file"); + if (model.tensor("model.embed_tokens.weight").bytes < size_t(d.vocab) * size_t(d.hidden) * 2) + throw std::runtime_error("model.q4nx: model.embed_tokens.weight holds fewer than vocab x hidden bf16 values"); lmhead_w = make_bo(dev, lmhead_weight_bytes(model)); pack_lmhead_weights(model, static_cast(lmhead_w->map())); diff --git a/npu/pack.cpp b/npu/pack.cpp index 8ddb3bab3..eb5ba91d6 100644 --- a/npu/pack.cpp +++ b/npu/pack.cpp @@ -64,10 +64,15 @@ size_t tiles_read(int n_tiles, int G) { return most; } -// A tensor's shape decides how much the packer reads; refuse one whose reads would run past the -// end of model.q4nx. +// A tensor's shape decides how much the packer reads; refuse one whose reads would run past the TENSOR. engine#1881: +// this used to test `t.avail`, which is measured to the end of the FILE, so a read that overran its own tensor into +// the next one passed -- every tensor but the last has neighbours. `t.bytes` is the tensor; `avail` stays the outer +// bound of the mapping (and is >= bytes by construction). void need(const Tensor& t, size_t bytes, const char* what) { - if (bytes > t.avail) throw std::runtime_error(std::string("model.q4nx: ") + what + " runs past the end of the file"); + if (bytes > t.bytes) + throw std::runtime_error(std::string("model.q4nx: ") + what + " needs " + std::to_string(bytes) + + " bytes but the tensor holds " + std::to_string(t.bytes) + + (bytes > t.avail ? " (and runs past the end of the file)" : " (the read would run into the next tensor)")); } const char* const kProj[] = {"self_attn.q_proj.weight", "self_attn.k_proj.weight", "self_attn.v_proj.weight", diff --git a/tests/npu_model_bounds_test.cpp b/tests/npu_model_bounds_test.cpp index d76e7805a..89834e1a9 100644 --- a/tests/npu_model_bounds_test.cpp +++ b/tests/npu_model_bounds_test.cpp @@ -195,6 +195,33 @@ int main() { }); check(why.find("runs past the end") != std::string::npos, "a projection larger than the file is refused"); } + { // engine#1881: k_proj claims four tiles and holds one, with more tensors AFTER it in the file. Bounded by the end + // of the file (`avail`), the packer read three tiles of v/o/down as k_proj's; bounded by the tensor, it refuses. + std::vector e = one_layer(); + for (auto& x : e) + if (x.name == "model.layers.0.self_attn.k_proj.weight") x.shape = {4, 5120}; // bytes stay 5120 + write_model(root / "k-overread", e); + const std::string why = thrown([&] { + Model m((root / "k-overread").string()); + std::vector w(layer_weight_bytes(m)); + pack_layer_weights(m, 0, w.data()); + }); + check(why.find("run into the next tensor") != std::string::npos, + "a projection that over-reads into the NEXT tensor (not past the file) is refused (" + why + ")"); + } + { // engine#1881: input_layernorm holds 128 of the 256 bytes hidden x 2 needs; the next tensor follows it + std::vector e = one_layer(); + for (auto& x : e) + if (x.name == "model.layers.0.input_layernorm.weight") { x.shape = {64}; x.bytes = 128; } + write_model(root / "norm-short", e); + const std::string why = thrown([&] { + Model m((root / "norm-short").string()); + std::vector i5(4 * 128); + fill_i5(m, 0, i5.data()); + }); + check(why.find("input_layernorm") != std::string::npos && why.find("next tensor") != std::string::npos, + "a layer norm shorter than hidden x 2 is refused, not completed from its neighbour (" + why + ")"); + } { // gate/up of one tile each: the interleave would write a whole chunk past the layer's buffer write_model(root / "chunks", one_layer({1, 5120}, 1)); const std::string why = thrown([&] {