From 6ffdfc1f3f28dc7ec0258ed119db7a74a7623165 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Tue, 10 Nov 2026 05:15:26 -0400 Subject: [PATCH] npu: bound a tensor read by the tensor, not by the bytes left in the file (engine#1881) Tensor::avail is size_ - base - off[0]: the bytes from a tensor start to the END OF FILE, commented as "the most a reader may touch". Three bounds used it as if it were the tensor: npu/lane.cpp:101 final norm -> memcpy(hidden*2) after checking avail >= hidden*2 npu/lane.cpp:103 embedding -> checks avail against vocab*hidden*2 npu/pack.cpp:70 every projection pack -> need(t, bytes) Each refuses a read past the FILE and permits a read past the TENSOR, i.e. straight into whatever is laid out after it. Same file, wrong data, no error. Add Tensor::usable() = min(bytes, avail) and gate on it at all three sites. Both halves of the min are load-bearing: avail alone misses the read into the next tensor, bytes alone misses a container that declares more than the file holds. The refusal message names the bound that bit (a truncated container reports the file, an overshooting shape reports the tensor), so the existing "runs past the end of the file" case reads exactly as before. Arms in tests/npu_model_bounds_test.cpp (already includes model.h and is already in ci.yml's --target list, so no new target and no workflow scope): ok a tensor with exactly its own bytes is usable to them ok a longer file does not extend the tensor (avail alone would say 4096, got 64) ok a tensor that claims more than the file holds is bounded by the file (got 4096) The middle arm bites: with usable() mutated to `return avail;` it fails as "a longer file does not extend the tensor (avail alone would say 4096, got 4096)". Whole suite green (all cases ok), pre-existing arms unmodified. --- npu/lane.cpp | 13 ++++++++++--- npu/model.h | 7 ++++++- npu/pack.cpp | 12 ++++++++++-- tests/npu_model_bounds_test.cpp | 14 ++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/npu/lane.cpp b/npu/lane.cpp index 303120367..afab2ac43 100644 --- a/npu/lane.cpp +++ b/npu/lane.cpp @@ -98,10 +98,17 @@ 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: bound the copy by the tensor, not by the bytes left in the file -- `avail` passes a tensor + // whose own bytes are short and lets the copy run on into the next tensor. The message names which bound bit. + if (norm.usable() < size_t(d.hidden) * 2) + throw std::runtime_error(norm.avail < size_t(d.hidden) * 2 ? "model.q4nx: model.norm.weight runs past the end of the file" + : "model.q4nx: model.norm.weight runs past its tensor"); 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"); + const Tensor& emb = model.tensor("model.embed_tokens.weight"); + if (emb.usable() < size_t(d.vocab) * size_t(d.hidden) * 2) + throw std::runtime_error(emb.avail < size_t(d.vocab) * size_t(d.hidden) * 2 + ? "model.q4nx: model.embed_tokens.weight runs past the end of the file" + : "model.q4nx: model.embed_tokens.weight runs past its tensor"); lmhead_w = make_bo(dev, lmhead_weight_bytes(model)); pack_lmhead_weights(model, static_cast(lmhead_w->map())); diff --git a/npu/model.h b/npu/model.h index e6b7c7a74..ad930079c 100644 --- a/npu/model.h +++ b/npu/model.h @@ -38,7 +38,12 @@ struct Tensor { std::vector shape; const uint8_t* data = nullptr; uint64_t bytes = 0; - uint64_t avail = 0; // bytes from data to the end of the file: the most a reader may touch + uint64_t avail = 0; // bytes from data to the end of the file: the outer bound a reader may not cross + // engine#1881: the most a reader may touch. `avail` alone is only the FILE bound, so a bounds check written + // against it passes for a tensor whose own bytes are short and lets the read run on into the next tensor's + // data (same file, wrong tensor). Both bounds matter: a malformed container can also declare `bytes` past + // the end of the file, so the tight bound is the smaller of the two. + uint64_t usable() const { return bytes < avail ? bytes : avail; } // Tiles in a projection: shape[0] rows of shape[1] bytes, in 5120-byte tiles. int tiles() const; diff --git a/npu/pack.cpp b/npu/pack.cpp index 8ddb3bab3..21d117bcb 100644 --- a/npu/pack.cpp +++ b/npu/pack.cpp @@ -65,9 +65,17 @@ size_t tiles_read(int n_tiles, int G) { } // A tensor's shape decides how much the packer reads; refuse one whose reads would run past the -// end of model.q4nx. +// tensor (engine#1881: `avail` alone is the FILE bound, so a shape that overshoots its own tensor +// passed this and read on into the next tensor's data). Which bound bit is part of the message: a +// tensor that declares too few bytes for its own shape is a different defect from a container whose +// declared tensor runs past the end of the file. 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"); + // One gate on the tight bound; the message names the bound that bit. A truncated container (avail < bytes) is the + // more specific diagnosis, so it is checked first; for a healthy container avail >= bytes and a shape that + // overshoots its own tensor is reported as such (engine#1881). + if (bytes > t.usable()) + throw std::runtime_error(std::string("model.q4nx: ") + what + + (bytes > t.avail ? " runs past the end of the file" : " runs past its 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..728095aa2 100644 --- a/tests/npu_model_bounds_test.cpp +++ b/tests/npu_model_bounds_test.cpp @@ -205,6 +205,20 @@ int main() { check(why.find("whole chunks") != std::string::npos, "gate/up that do not fill whole chunks are refused"); } fs::remove_all(root); + // engine#1881: the bound is the tensor, not the bytes left in the file. `avail` counts to the end of the + // file, so a check written against it passes for a tensor whose own bytes are short and lets the read run + // on into the next tensor -- same file, wrong data. The middle arm is the one that bites: it is satisfied + // by `avail` and refused by `usable()`. + { + Tensor t; + t.bytes = 64; + t.avail = 64; + check(t.usable() == 64, "a tensor with exactly its own bytes is usable to them"); + t.avail = 4096; // the file continues past this tensor: avail says yes, the tensor says no + check(t.usable() == 64, "a longer file does not extend the tensor (avail alone would say 4096, got " + std::to_string(t.usable()) + ")"); + t.bytes = 8192; // a malformed container may declare more than the file holds + check(t.usable() == 4096, "a tensor that claims more than the file holds is bounded by the file (got " + std::to_string(t.usable()) + ")"); + } std::printf("%s\n", failures ? "FAILED" : "all cases ok"); return failures ? 1 : 0; }