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
39 changes: 39 additions & 0 deletions npu/pack_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3435,6 +3435,45 @@ std::string header_witness_refusal(int vote_hdrs, bool declared_qk) {
// classifies as a bias of q_proj rather than as an unknown tensor, and a name that matches nothing lands in `unassigned`
// with its own name rather than being guessed from its shape.
// engine#1788, slice 3c step 1: (role, layer type), the key the writer needs. See the header for the measurement.
PlacementReport container_placement(const std::vector<Q4nxTensor>& tensors,
const std::vector<std::string>& layer_types) {
PlacementReport out;
const ContainerRoles roles = container_roles(tensors);
const RoleSlotReport table = container_role_slots(roles);
for (const auto& t : tensors) {
PlacementRow row;
row.tensor = t.name;
const auto it = roles.role_of.find(t.name);
if (it == roles.role_of.end()) { row.why = "no role recognised"; ++out.no_role; out.rows.push_back(row); continue; }
row.role = it->second;
std::string::size_type pos = t.name.find(".layers.");
std::string::size_type off = 8;
if (pos == std::string::npos) { pos = t.name.find(".layer."); off = 7; }
if (pos != std::string::npos) {
const std::string::size_type start = pos + off;
std::string::size_type end = start;
while (end < t.name.size() && t.name[end] >= '0' && t.name[end] <= '9') ++end;
if (end > start) {
const int64_t layer = std::atoll(t.name.substr(start, end - start).c_str());
row.layer = std::to_string(layer);
if (layer >= 0 && layer < int64_t(layer_types.size())) row.layer_type = layer_types[size_t(layer)];
}
}
row.slot = container_role_slot(row.role, row.layer_type);
if (row.slot.empty()) {
const auto why = table.why.find(row.role);
row.why = why != table.why.end() ? why->second
: "the layer type does not give this role a destination";
++out.unplaced;
} else {
++out.placed;
++out.slot_counts[row.slot];
}
out.rows.push_back(row);
}
return out;
}

RoleLayerReport container_role_layer_types(const std::vector<Q4nxTensor>& tensors,
const std::vector<std::string>& layer_types) {
RoleLayerReport out;
Expand Down
20 changes: 20 additions & 0 deletions npu/pack_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,26 @@ RoleSlotReport container_role_slots(const ContainerRoles& roles);
// not treat that as "unchanged", which is why this returns a string and the report above returns the reasons.
std::string container_role_slot(const std::string& role, const std::string& layer_type);

// engine#1788, slice 3c: the writer's per-TENSOR decision, which is what an import actually iterates. Reads the tensor's
// own layer, takes that layer's declared type, and asks the table above for a destination. A tensor with no role and a
// tensor with a role but no destination are counted apart, because the first is a vocabulary gap and the second is a
// decision that was made on purpose.
struct PlacementRow {
std::string tensor;
std::string layer; // "" when the tensor belongs to no layer (embed, final_norm, lm_head)
std::string layer_type; // "" for the same reason
std::string role;
std::string slot; // "" when there is no destination
std::string why; // the reason, when slot is empty
};
struct PlacementReport {
std::vector<PlacementRow> rows;
std::map<std::string, int64_t> slot_counts;
int64_t placed = 0, unplaced = 0, no_role = 0;
};
PlacementReport container_placement(const std::vector<Q4nxTensor>& tensors,
const std::vector<std::string>& layer_types);

// engine#1788, slice 3c: a container's roles need a SECOND key, and the second key is the layer type.
// Measured: gemma4-e2b's `q_proj` is N=384 on all 28 `sliding_attention` layers and N=768 on all 7 `full_attention` ones;
// bolt-e4b's is (640,5120) on all 35 sliding and (1280,5120) on all 7 full. A perfect partition, structural in origin
Expand Down
32 changes: 32 additions & 0 deletions tests/npu_pack_import_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5177,6 +5177,37 @@ void test_container_role_layer_types() {
"role+layer: a layer index past layer_types is counted as out of range");
}

// engine#1788, 3c: the writer iterates TENSORS, not roles, so the placement has to answer per tensor and use that
// tensor's own layer type. The arm that matters is the pair: the same role lands in two different places on two layers.
void test_container_placement() {
using pi::Q4nxTensor;
const auto t = [](const std::string& n) { Q4nxTensor x; x.name = n; x.dtype = "I8"; return x; };
const std::vector<Q4nxTensor> v = {
t("model.embed_tokens.weight"),
t("model.layers.0.self_attn.qkv_proj.weight"), // layer 0 is linear attention
t("model.layers.1.self_attn.qkv_proj.weight"), // layer 1 is full attention
t("model.layers.1.self_attn.q_proj.weight"),
t("model.layers.0.mlp.gate_up_proj.weight"), // a role this slice still refuses
t("model.layers.1.unknown_thing.weight"), // no role at all
};
const std::vector<std::string> lt = {"linear_attention", "full_attention"};
const pi::PlacementReport r = pi::container_placement(v, lt);

check(r.rows.size() == v.size(), "placement: a row per tensor, so nothing is silently skipped");
check(r.placed + r.unplaced + r.no_role == int64_t(v.size()),
"placement: placed + unplaced + no-role accounts for every tensor");
check(r.rows[0].slot == "model.embed_tokens.weight", "placement: a tensor with no layer still gets a slot");
check(r.rows[0].layer.empty() && r.rows[0].layer_type.empty(), "placement: and reports that it has no layer");
check(r.rows[1].slot == "linear_attn.qkv_proj.weight", "placement: the fused qkv on the LINEAR layer");
check(r.rows[2].slot.empty(), "placement: the same role on the FULL-attention layer has no destination");
check(r.rows[2].why.size() > 10, "placement: and says why");
check(r.rows[3].slot == "self_attn.q_proj.weight", "placement: the query projection is layer-type-independent");
check(r.rows[4].slot.empty() && r.rows[4].role == "gate_up_proj", "placement: gate_up_proj is still refused");
check(r.rows[5].slot.empty() && r.rows[5].role.empty(), "placement: a tensor with no role is counted apart");
check(r.no_role == 1, "placement: no_role counts the vocabulary gap, not the deliberate refusal");
check(r.unplaced == 2, "placement: unplaced counts the two decisions, not the vocabulary gap");
}

void test_container_roles_moe_and_singular_layer() {
const auto t = [](const std::string& n) { pi::Q4nxTensor x; x.name = n; x.dtype = "I8"; return x; };
const auto roles = [&](std::initializer_list<const char*> names) {
Expand Down Expand Up @@ -6359,6 +6390,7 @@ int main(int argc, char** argv) {
test_container_roles();
test_container_roles_gemma_local();
test_container_roles_moe_and_singular_layer();
test_container_placement();
test_container_role_slots();
test_container_role_layer_types();
test_container_plan(golden_dir.empty() ? "." : golden_dir);
Expand Down
Loading