Skip to content
Merged
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
28 changes: 25 additions & 3 deletions npu/pack_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3391,11 +3391,33 @@ Plan plan(const std::string& pack_dir) {
p.container_tensors = int64_t(c.tensors.size());
p.container_payload_bytes = c.payload_bytes;
for (const auto& t : c.tensors) p.container_dtypes[t.dtype]++;
// slice 2: the geometry, and the name of the source that answered. config.json is read through the ordinary
// loader so this genre and every other one agree about what config.json means (text_config lift, top-level
// wins) -- the container's own `dims` still takes precedence in container_geometry().
std::string cfg_source;
const json cfg = load_geometry(pack_dir, &cfg_source);
const ContainerGeometry g = container_geometry(c.meta, cfg);
p.dims = g.dims;
p.container_geometry_source = g.source;
p.container_geometry_missing = g.missing;
p.container_geometry_inferred = g.inferred;
std::string geom = g.source.empty()
? "the container declares no `dims` and no config.json answered, so its geometry is not known here"
: "geometry from " + g.source;
if (!g.inferred.empty()) {
geom += " (";
for (size_t i = 0; i < g.inferred.size(); ++i) geom += (i ? ", " : "") + g.inferred[i];
geom += " inferred)";
}
if (!g.missing.empty()) {
geom += "; no source states ";
for (size_t i = 0; i < g.missing.size(); ++i) geom += (i ? ", " : "") + g.missing[i];
}
p.container_note = c.why.empty()
? "the pack is a safetensors container (model.q4nx): " + std::to_string(c.tensors.size()) +
" tensors, " + std::to_string(c.payload_bytes) + " B of payload, no graph. Reading it as a"
" weights source needs the container-genre plan (engine#1786, item F1); until then this pack is"
" recognised and reported, not importable."
" tensors, " + std::to_string(c.payload_bytes) + " B of payload, no graph; " + geom +
". Reading it as a weights source needs the container-genre plan (engine#1786, item F1); until"
" then this pack is recognised and reported, not importable."
: "model.q4nx is present but unreadable: " + c.why;
return p;
}
Expand Down
6 changes: 6 additions & 0 deletions npu/pack_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ struct Plan {
int64_t container_payload_bytes = 0;
std::map<std::string, int64_t> container_dtypes;
std::string container_note; // what the container genre still needs, empty for a non-container pack
// engine#1786 (item F1), slice 2: WHERE the container's geometry came from, and what it could not reach.
// A bare number in `dims` cannot say whether the pack declared it or a file beside it did, and those two are free to
// disagree (13 of the 29 packs measured carry a `dims` block; all 29 carry config.json). Empty for a non-container pack.
std::string container_geometry_source; // "dims", "config.json", or empty when neither answered
std::vector<std::string> container_geometry_missing; // fields no source could fill, by name
std::vector<std::string> container_geometry_inferred; // fields derived from another field, by name
int64_t graph_matmuls = -1; // MatMulNBits nodes in the main graph
int64_t graph_real_weights = 0; // of those, how many point at an initializer with dims (i.e. real weights)
int64_t graph_external = 0; // initializers that declare external data (location + offset)
Expand Down
68 changes: 68 additions & 0 deletions tests/npu_pack_import_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4825,6 +4825,73 @@ void test_container_geometry() {
}
}

// engine#1786 (item F1), slice 2: `plan()` reports WHERE a container's geometry came from. The three genres the census
// measured are the three cases: `dims` answers (and beats a disagreeing config.json), config.json answers when there is
// no `dims`, and neither answering is reported as unknown rather than as a zero geometry.
void test_container_plan(const std::string& root) {
namespace fs = std::filesystem;
// Under ctest `root` is the source tree's tests/golden, so these three directories are removed on every way out:
// the arm is new, and leaving fixtures behind is the defect #2024 fixes for its neighbour -- not repeating it here.
struct Cleanup {
fs::path base;
~Cleanup() {
for (const char* n : {"cg-dims", "cg-cfg", "cg-silent"}) {
std::error_code e;
fs::remove_all(base / n, e);
}
}
} cleanup{fs::path(root)};
const auto write_pack = [&](const std::string& name, const std::string& meta_json, const std::string& config) {
const fs::path dir = fs::path(root) / name;
std::error_code ec;
fs::remove_all(dir, ec);
fs::create_directories(dir, ec);
std::string hdr = "{";
if (!meta_json.empty()) hdr += "\"__metadata__\":" + meta_json + ",";
hdr += "\"model.embed_tokens.weight\":{\"dtype\":\"BF16\",\"shape\":[4,2],\"data_offsets\":[0,16]}}";
std::string out(8, '\0');
const int64_t n = int64_t(hdr.size());
for (int i = 0; i < 8; ++i) out[size_t(i)] = char((n >> (8 * i)) & 0xFF);
out += hdr;
out.append(16, '\x22');
std::ofstream(dir / "model.q4nx", std::ios::binary).write(out.data(), std::streamsize(out.size()));
if (!config.empty()) std::ofstream(dir / "config.json") << config;
return dir.string();
};
const std::string dims = "{\"dims\":{\"hidden_size\":576,\"intermediate_size\":1536,\"num_hidden_layers\":30,"
"\"num_attention_heads\":9,\"num_key_value_heads\":3,\"vocab_size\":49152,"
"\"head_dim\":64,\"rope_theta\":10000.0,\"model_type\":\"llama\"}}";

// 1. self-describing, and a config.json that disagrees: the container's own statement wins and is named.
{
const auto pl = pi::plan(write_pack("cg-dims", dims, "{\"hidden_size\": 1024}"));
check(pl.container_geometry_source == "dims", "plan: a self-describing container reports source=dims");
check(pl.dims.hidden == 576, "plan: dims.hidden comes from the container, not the disagreeing config.json");
check(pl.dims.layers == 30 && pl.dims.vocab == 49152, "plan: the container's geometry reaches the plan");
check(pl.container_geometry_missing.empty(), "plan: nothing is reported missing for a complete container");
check(pl.container_note.find("geometry from dims") != std::string::npos,
"plan: the note says which source answered");
}
// 2. gguf-converted: no dims, so config.json answers -- and the source says so.
{
const auto pl = pi::plan(write_pack("cg-cfg", "{\"converter\":\"tools/gguf_to_q4nx.py\"}",
"{\"hidden_size\":1024,\"num_hidden_layers\":28,"
"\"num_attention_heads\":16,\"num_key_value_heads\":8,"
"\"vocab_size\":151936,\"intermediate_size\":3072,"
"\"head_dim\":64,\"rope_theta\":1000000.0}"));
check(pl.container_geometry_source == "config.json", "plan: a converted container reports source=config.json");
check(pl.dims.hidden == 1024 && pl.dims.layers == 28, "plan: config.json supplies the geometry");
}
// 3. silent: neither answers, so the plan says the geometry is unknown instead of reporting zeros as facts.
{
const auto pl = pi::plan(write_pack("cg-silent", "", ""));
check(pl.container_geometry_source.empty(), "plan: the silent genre claims no source");
check(pl.container_geometry_missing.size() == 8, "plan: every required field is reported missing, by name");
check(pl.container_note.find("geometry is not known here") != std::string::npos,
"plan: the note says the geometry is unknown rather than printing a zero geometry");
}
}

// engine#1786 (item F1): an NPU2 model source is a safetensors container, and this is the reader that decides whether
// a pack is that genre. The failure that matters is the silent one -- a wrong header length yields an empty inventory,
// which reads as "a pack with no tensors" rather than "a malformed header" -- so each arm asserts the reason, not just
Expand Down Expand Up @@ -5551,6 +5618,7 @@ int main(int argc, char** argv) {
test_text_config_geometry(golden_dir.empty() ? "." : golden_dir);
test_q4nx_container(golden_dir.empty() ? "." : golden_dir);
test_container_geometry();
test_container_plan(golden_dir.empty() ? "." : golden_dir);
test_hybrid_pack_import(golden_dir.empty() ? "." : golden_dir);
test_rope_parameters_theta(golden_dir.empty() ? "." : golden_dir);
test_three_input_matmulnbits(golden_dir.empty() ? "." : golden_dir);
Expand Down
Loading