Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions npu/lane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t*>(lmhead_w->map()));

Expand Down
7 changes: 6 additions & 1 deletion npu/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ struct Tensor {
std::vector<int64_t> 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;
Expand Down
12 changes: 10 additions & 2 deletions npu/pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions tests/npu_model_bounds_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading