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
9 changes: 9 additions & 0 deletions npu/private_route.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,18 @@ std::string private_route_missing(const std::string& model_type) {
"-DONEBIT_NPU_PRIVATE=<npu-kernels checkout> (docs/npu.md, \"Private routes\")";
}

bool private_routes_available() { return !routes().empty() || !commands().empty(); }

void parse_private_option(const std::string& kv, PrivateOptions& opts) {
const auto eq = kv.find('=');
if (eq == std::string::npos || eq == 0) throw std::runtime_error("--npu-opt takes KEY=VALUE, not '" + kv + "'");
// A private-route option on a build with no private routes is not a preference, it is a request nothing will read.
// Storing it silently is how a run ended up 10x slower than asked with no log line anywhere (engine#1740, W6): the
// caller asked for one route and got another, and the only evidence was that the numbers were identical.
if (!private_routes_available())
throw std::runtime_error("--npu-opt " + kv +
" was given, but this build links no private NPU route, so the option would be ignored. "
"Build with -DONEBIT_NPU_PRIVATE=<npu-kernels checkout> (docs/npu.md, \"Private routes\")");
opts[kv.substr(0, eq)] = kv.substr(eq + 1);
}

Expand Down
5 changes: 5 additions & 0 deletions npu/private_route.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ std::string private_model_name(const std::string& model_type);
// it or it is not a private route's type; otherwise that the route is not in this build.
std::string private_route_missing(const std::string& model_type);
// "KEY=VALUE" -> opts[KEY] = VALUE; throws without '=' or with an empty key.
// True when this build links a private-route add-on and it registered at least one route or command. `--npu-opt` only
// means anything in that case; without it the option used to be stored and silently ignored, which turns a mistyped or
// add-on-less invocation into a 10x slower route with no signal (engine#1740, W6).
bool private_routes_available();

void parse_private_option(const std::string& kv, PrivateOptions& opts);

// Defined by the add-on; `1bit` calls it once at startup in -DONEBIT_NPU_PRIVATE builds.
Expand Down
17 changes: 17 additions & 0 deletions tests/npu_private_route_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,24 @@ bool throws(const std::string& kv) {

} // namespace

// engine#1740 (W6): with no add-on linked, a private-route option must be REFUSED, not stored. The failure it pins is
// concrete: `--npu-opt dx=<dir>` on such a build was accepted, ignored, and served the per-op route instead -- 10x slower
// with no log line, discovered only because two runs produced identical numbers. The arm is ordered before any route is
// registered below, which is exactly the no-add-on state.
void no_addon_refuses_the_option() {
PrivateOptions o;
bool threw = false;
try {
parse_private_option("dx=/tmp/kernels", o);
} catch (const std::runtime_error& e) {
threw = std::string(e.what()).find("links no private NPU route") != std::string::npos;
}
check("no add-on: --npu-opt is refused by name, not stored (engine#1740)", threw && o.empty());
check("no add-on: private_routes_available() is false", !private_routes_available());
}

int main() {
no_addon_refuses_the_option();
namespace fs = std::filesystem;
const fs::path dir = fs::temp_directory_path() / ("npu_private_route_test." + std::to_string(::getpid()));
fs::create_directories(dir);
Expand Down
Loading