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: 11 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ endif()

enable_testing()

# engine#1864: the nine `add_test(... COMMAND ${Python3_EXECUTABLE} ...)` rows below this point need the interpreter, and
# `find_package(Python3)` used to run ~700 lines LATER, so those rows expanded to an empty command: ctest then executed the
# `.py` file directly and they worked only because the executable bit was set. A plumbing-built commit that rewrote a mode
# 100755 -> 100644 with no content change turned one into `***Not Run (BAD_COMMAND)`. Required, not optional: a configure
# without an interpreter cannot run these rows, and saying so here is better than discovering it as a ctest error.
find_package(Python3 COMPONENTS Interpreter)
if(NOT Python3_Interpreter_FOUND)
message(FATAL_ERROR "Python3 interpreter not found: the ci/registry/serve-classification ctest rows need it")
endif()

# Components are ported in the order listed in docs/PORTING.md.

# ── The engine binary: `1bit serve`, an OpenAI-compatible API (docs/serve.md)
Expand Down Expand Up @@ -757,8 +767,7 @@ if(ONEBIT_LAYA_ROUTE_MODEL)
endif()

# The registry against the reviewed not-an-alias list (docs/arch-gaps.md) and the committed pins:
# no checkouts or network.
find_package(Python3 COMPONENTS Interpreter)
# no checkouts or network. (Python3 was found near enable_testing(), engine#1864: the rows above need it too.)
if(Python3_Interpreter_FOUND)
add_test(NAME registry_gaps COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/registry_build.py --check-gaps)
# pin the guard's firing behavior: the committed registry always passes --check-gaps, so
Expand Down
19 changes: 17 additions & 2 deletions npu/lane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,23 @@ struct Lane::Impl {
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").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");
// engine#1881 bounded this by the tensor's own span (`bytes`), which is right, but kept a **bf16** expected
// size -- and that refuses a quantised table for being smaller than an f32->bf16 table it never claimed to be
// (bonsai2-27b: 794,624,000 B of I8 chunks against 2,542,796,800 B). The expected size comes from the dtype now,
// and the comparison is made only where a row-major reading is meaningful:
// * a float table (bf16/f16/f32) of V x H must be AT LEAST bytes(V*H*width) -- the case #1881 was written for,
// a config overstating vocab, still refused;
// * an I8 table is either plain row-major (exactly V*H bytes, the gemma4 layout) or a CHUNK GRID, whose shape
// product is already a byte count (155,200 "rows" are 7,760 row tiles x 20 column tiles). Comparing those
// against V*H is the mistake this guard made, so an I8 table keeps the file-end bound (`avail`), which is a
// looser but never-wrong answer.
const Tensor& emb = model.tensor("model.embed_tokens.weight");
const uint64_t emb_need = emb.dtype == "I8" ? 0 : emb.expected_bytes(uint64_t(d.vocab) * uint64_t(d.hidden));
const uint64_t emb_have = emb_need ? emb.bytes : emb.avail;
if (emb_need ? (emb.bytes < emb_need) : (emb_have < size_t(d.vocab) * size_t(d.hidden) * 2))
throw std::runtime_error("model.q4nx: model.embed_tokens.weight holds " + std::to_string(emb_have) +
" bytes, fewer than the " + std::to_string(d.vocab) + " x " + std::to_string(d.hidden) +
" elements config.json declares" + (emb_need ? "" : " (I8 chunked table: file-end bound)"));
lmhead_w = make_bo(dev, lmhead_weight_bytes(model));
pack_lmhead_weights(model, static_cast<uint8_t*>(lmhead_w->map()));

Expand Down
14 changes: 14 additions & 0 deletions npu/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@

namespace onebit::npu {

uint64_t Tensor::expected_bytes(uint64_t elements) const {
// Element widths of the dtypes this reader decodes. Callers pass a row-major element count, so a tiled/chunked entry
// (whose shape product is already a byte count: its last dimension is the chunk's BYTES) must not be sent here --
// npu/lane.cpp checks `shape.size() == 2` for that reason. 0 means "not understood", and the caller then keeps the
// file-end bound rather than refusing on a guess.
uint64_t per = 0;
if (dtype == "BF16" || dtype == "F16") per = 2;
else if (dtype == "F32") per = 4;
else if (dtype == "I32") per = 4;
else if (dtype == "I8" || dtype == "U8") per = 1;
if (per == 0 || elements == 0) return 0;
return per * elements;
}

int Tensor::tiles() const {
// [tile rows, tile cols, chunk bytes] (Q4NX with Q4_K or Q8 chunks, npu/q4nx.h)
if (shape.size() == 3 && shape[0] > 0 && shape[1] > 0) return int(shape[0] * shape[1]);
Expand Down
6 changes: 6 additions & 0 deletions npu/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ struct Tensor {

// Tiles in a projection: shape[0] rows of shape[1] bytes, in 5120-byte tiles.
int tiles() const;

// Bytes this tensor SHOULD occupy for `elements` logical elements, from its own dtype, or 0 for a dtype this reader
// does not decode. engine#1881 bounded the embedding by the tensor's own span (`bytes`) but kept a **bf16** figure as
// the expected size, which refuses a quantised table: bonsai2-27b's I8 tiled table is 794,624,000 B against that
// expression's 2,542,796,800 B. `bytes` is the right field; the expected number has to come from the dtype.
uint64_t expected_bytes(uint64_t elements) const;
};

struct Dims {
Expand Down
Loading