From d1489d432ea7885784feea64101d926156e9a7b5 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Sat, 7 Nov 2026 18:09:02 -0400 Subject: [PATCH] npu plan: two message-quality refusals in the designs section, from the #1330 review Both were non-blocking nits in pi-prs' READY on #1330, and both are about what a reader sees when a plan is wrong rather than about what the engine does: * a wrongly typed field ("M": "128") used to escape as nlohmann's type_error, which names no field and no position -- the one place in this loader that did not say which plan entry was wrong. It is now read through typed()/int_field(), so it refuses with "plan: designs[0].M must be an integer", like every other field here; * an exact duplicate entry used to be refused by the geometry comparison, which printed the same elf on both sides ("elf \"a.elf\", the container derives \"a.elf\""). The loader now catches it first and says which entry it repeats: "designs[2] repeats designs[0] (QKV 128x1024x4096 ...)". Two cases added to tests/npu_plan_test.cpp for exactly those messages. No behaviour change for a valid plan; the parse is otherwise the same. --- npu/plan/plan.h | 47 ++++++++++++++++++++++++++++++++++++----- tests/npu_plan_test.cpp | 16 ++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/npu/plan/plan.h b/npu/plan/plan.h index 80b6cb7d7..aabc75d27 100644 --- a/npu/plan/plan.h +++ b/npu/plan/plan.h @@ -394,19 +394,33 @@ struct Plan { const std::string where = "designs[" + std::to_string(i) + "]"; if (!d.is_object()) throw PlanError("plan: " + path + " " + where + " is not an object"); Design des; - des.role = d.value("role", ""); + // Every field is read through typed()/int_field() so a wrongly typed value ("M": "128") + // is a refusal that names plans[i].M, like every other field in this loader, instead of + // nlohmann's type_error escaping to the caller with no position in it (engine#1330 review). + des.role = typed(d, "role", where, path); if (des.role.empty()) throw PlanError("plan: " + path + " " + where + ".role is empty"); // Named with the index, not just the field: a plan can carry a dozen of these and // "M must be positive" without a position is a hunt. - des.M = d.value("M", 0); + des.M = int_field(d, "M", where, path); if (des.M <= 0) throw PlanError("plan: " + path + " " + where + ".M must be positive"); - des.K = d.value("K", 0); + des.K = int_field(d, "K", where, path); if (des.K <= 0) throw PlanError("plan: " + path + " " + where + ".K must be positive"); - des.N = d.value("N", 0); + des.N = int_field(d, "N", where, path); if (des.N <= 0) throw PlanError("plan: " + path + " " + where + ".N must be positive"); - des.elf = d.value("elf", ""); + des.elf = typed(d, "elf", where, path); if (des.elf.empty()) throw PlanError("plan: " + path + " " + where + ".elf is empty (the artifact the role runs)"); + // An exact duplicate is a plan mistake, and saying which entry it repeats reads better than + // the geometry comparison's "elf a.elf, the container derives a.elf" (engine#1330 review). + for (size_t k = 0; k < p.designs.size(); k++) { + const Design& prev = p.designs[k]; + if (prev.role == des.role && prev.M == des.M && prev.K == des.K && prev.N == des.N && + prev.elf == des.elf) + throw PlanError("plan: " + path + " " + where + " repeats designs[" + + std::to_string(k) + "] (" + des.role + " " + std::to_string(des.M) + + "x" + std::to_string(des.K) + "x" + std::to_string(des.N) + " " + + des.elf + ")"); + } p.designs.push_back(des); } } @@ -414,6 +428,29 @@ struct Plan { } private: + // A typed read that names the field: nlohmann's own type_error says nothing about which entry of + // which plan was wrong, and a plan can carry a dozen designs. + template + static T typed(const nlohmann::json& j, const char* key, const std::string& where, + const std::string& path) { + if (!j.contains(key)) return T{}; + if (!j.at(key).is_string() && !j.at(key).is_number_integer() && !j.at(key).is_number_float() && + !j.at(key).is_boolean()) + throw PlanError("plan: " + path + " " + where + "." + key + " has the wrong type"); + try { + return j.at(key).get(); + } catch (const std::exception&) { + throw PlanError("plan: " + path + " " + where + "." + key + " has the wrong type"); + } + } + static int int_field(const nlohmann::json& j, const char* key, const std::string& where, + const std::string& path) { + if (!j.contains(key)) return 0; + if (!j.at(key).is_number_integer()) + throw PlanError("plan: " + path + " " + where + "." + key + " must be an integer"); + return j.at(key).get(); + } + static const nlohmann::json& obj(const nlohmann::json& j, const char* key, const std::string& path) { if (!j.contains(key) || !j.at(key).is_object()) throw PlanError("plan: " + path + " has no \"" + key + "\" object"); diff --git a/tests/npu_plan_test.cpp b/tests/npu_plan_test.cpp index 6d0e91e83..319b4507d 100644 --- a/tests/npu_plan_test.cpp +++ b/tests/npu_plan_test.cpp @@ -444,6 +444,22 @@ void test_designs() { w), "designs: a duplicated geometry cannot stand in for an omitted one"); } + // A wrong TYPE is a refusal naming the field, not nlohmann's type_error with no position in it. + { + auto bad = with_designs(true); + bad["designs"][0]["M"] = "128"; + const std::string why = refusal(write_plan("dtype", bad)); + check(has(why, "designs[0].M") && has(why, "must be an integer"), + "designs: a wrongly typed M is refused by field: " + why); + } + // ... and an exact duplicate says which entry it repeats, rather than naming the same elf on both sides. + { + auto dup = with_designs(true); + dup["designs"].push_back(dup["designs"][0]); + const std::string why = refusal(write_plan("ddup2", dup)); + check(has(why, "designs[2] repeats designs[0]"), + "designs: an exact duplicate names the entry it repeats: " + why); + } // No section: the probes' behaviour, and every plan written before the field keeps loading. const Plan plain = Plan::load(write_plan("nodesigns", base_plan())); check(plain.designs.empty(), "designs: an absent section parses to empty");