From 9f770dbaba257ed0c087349fd9bc8233146bba92 Mon Sep 17 00:00:00 2001 From: shuyuzhang Date: Mon, 23 Nov 2026 18:11:35 +0800 Subject: [PATCH 1/2] perf(weights): check source file identity once, when the file is opened After every expert write, Weights::complete reopened each source file by path and compared both the open handle's and the reopened file's identity with the one recorded at begin_write, which itself called fstat on every write. On the 35B slot path this runs on every synchronous expert load, about 39 times per decode token. Identity is now taken once when a source file is opened and cached with its handle; writes reuse it. A source file modified while the engine is running is no longer detected, so the storage test case for that is removed, and the read-batch test now checks that a repeated write opens and stats nothing. --- edge0-infer/src/weights/runtime.cpp | 19 +++++++------------ edge0-infer/src/weights/storage.h | 1 - .../tests/unit/weights/read_batch_test.cpp | 4 ++-- .../tests/unit/weights/storage_test.cpp | 11 ----------- 4 files changed, 9 insertions(+), 26 deletions(-) diff --git a/edge0-infer/src/weights/runtime.cpp b/edge0-infer/src/weights/runtime.cpp index 0d799da3..c2927ed5 100644 --- a/edge0-infer/src/weights/runtime.cpp +++ b/edge0-infer/src/weights/runtime.cpp @@ -16,7 +16,8 @@ struct Registry { std::condition_variable changed; std::map> storages; std::map> ids; - std::map,std::shared_ptr> files; + // Source file identity is checked once, when the file is opened. + std::map,std::pair,platform::FileIdentity>> files; }; class Usage final : public exec::Use { @@ -145,18 +146,19 @@ Result> Weights::begin_write(const std::shared_ptr file; + platform::FileIdentity identity{}; { std::lock_guard lock(registry_->mutex); auto cached=registry_->files.find(key); if(cached==registry_->files.end()) { auto opened=platform::ReadFile::open(key.first,key.second);if(!opened) return opened.error(); - cached=registry_->files.emplace(key,opened.value()).first; + auto opened_identity=opened.value()->identity();if(!opened_identity) return opened_identity.error(); + cached=registry_->files.emplace(key,std::make_pair(opened.value(),opened_identity.value())).first; } - file=cached->second; + file=cached->second.first;identity=cached->second.second; } - auto identity=file->identity();if(!identity) return identity.error(); index=write->files_.size();selected_files.emplace(key,index); - write->files_.push_back(file);write->identities_.push_back(identity.value());write->file_keys_.push_back(key); + write->files_.push_back(file);write->identities_.push_back(identity); } else index=found->second; const auto& identity=write->identities_[index]; if(item.source.offset>identity.size || item.source.bytes>identity.size-item.source.offset) @@ -335,13 +337,6 @@ Result> Weights::complete(const std::shared_ptr& wri for (auto range : ranges) { if (range.first != end) error = invalid("Incomplete writer ranges"); end = range.second; } if (end != write->items_[i].source.bytes) error = invalid("Incomplete writer coverage"); } - for (std::size_t i = 0; i < write->files_.size(); ++i) { - auto current=write->files_[i]->identity(); - auto path=platform::ReadFile::open(write->file_keys_[i].first,write->file_keys_[i].second); - auto path_identity=path?path.value()->identity():Result(path.error()); - if(!current || !(current.value()==write->identities_[i]) || !path_identity || !(path_identity.value()==write->identities_[i])) - if(!error) error=Error{ErrorCategory::backend_error,"Source file identity changed"}; - } if (error) { write->error_ = error; write->rollback(true); return *error; } std::lock_guard lock(registry_->mutex); std::vector bindings; diff --git a/edge0-infer/src/weights/storage.h b/edge0-infer/src/weights/storage.h index 8501720a..43076626 100644 --- a/edge0-infer/src/weights/storage.h +++ b/edge0-infer/src/weights/storage.h @@ -72,7 +72,6 @@ class Write { std::vector> files_; std::vector identities_; std::vector item_files_; - std::vector> file_keys_; std::vector> futures_; bool started_ = false, finished_ = false; void rollback(bool isolate_all = false); diff --git a/edge0-infer/tests/unit/weights/read_batch_test.cpp b/edge0-infer/tests/unit/weights/read_batch_test.cpp index 935408d0..3a127f20 100644 --- a/edge0-infer/tests/unit/weights/read_batch_test.cpp +++ b/edge0-infer/tests/unit/weights/read_batch_test.cpp @@ -51,9 +51,9 @@ std::pair group(const std::shared_ptr& b if(!fail) { std::vector regions(count);std::iota(regions.begin(),regions.end(),0); CHECK(!weights.evict(storage,regions)); - const auto before=scope.hooks.opens.load(); + const auto before=scope.hooks.opens.load(),identities=scope.hooks.identities.load(); auto again=take(weights.begin_write(storage,items));CHECK(!weights.fill_regions(again));take(weights.complete(again)); - CHECK(scope.hooks.opens-before==((different_file||different_mode)?2:1)); + CHECK(scope.hooks.opens==before && scope.hooks.identities==identities); } CHECK(!weights.close(storage)); return calls; diff --git a/edge0-infer/tests/unit/weights/storage_test.cpp b/edge0-infer/tests/unit/weights/storage_test.cpp index 541c409e..922376d7 100644 --- a/edge0-infer/tests/unit/weights/storage_test.cpp +++ b/edge0-infer/tests/unit/weights/storage_test.cpp @@ -104,17 +104,6 @@ int main() { resume.set_value(); auto ready = take(weights.complete(write)); CHECK(ready.size() == 3); weights.before_read = {}; } - { - std::promise entered, resume; auto wait = resume.get_future().share(); - weights.before_read = [&](std::size_t) { entered.set_value(); wait.wait(); }; - auto write = take(weights.begin_write(other, {item(0, "changed")})); - CHECK(!weights.fill_regions(write)); entered.get_future().wait(); - { std::ofstream out(path, std::ios::app); out << 'x'; } - resume.set_value(); CHECK(!weights.complete(write)); weights.before_read = {}; - CHECK(other->state(0) == RegionState::isolated); - CHECK(!weights.begin_write(other, {item(0, "again")})); - CHECK(!weights.acquire_views(other, {other->identity(), 0, "changed", 2})); - } { auto a = take(weights.create_storage(backend, {{{GGML_TYPE_F32, {4}}, 1}}, edge0::exec::Purpose::resident_weights)); auto b = take(weights.create_storage(backend, {{{GGML_TYPE_F32, {4}}, 1}}, edge0::exec::Purpose::resident_weights)); From d7e94af35fb4f1cc6810745f0eb93315bd2cfcf1 Mon Sep 17 00:00:00 2001 From: shuyuzhang Date: Mon, 23 Nov 2026 18:11:35 +0800 Subject: [PATCH 2/2] fix(cli): link the Android CLI at API 28 serve_delegate_process.cpp calls posix_spawn_file_actions_addchdir_np, which Android provides only from API 34. The android-cpu preset targets API 28 and has failed to link since 5c7f5c2. Android, like iOS, does not run the external engine's preparation scripts, so it now takes the same ENOTSUP path. --- edge0-infer/tools/cli/serve_delegate_process.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/edge0-infer/tools/cli/serve_delegate_process.cpp b/edge0-infer/tools/cli/serve_delegate_process.cpp index e62b9e9d..feb41802 100644 --- a/edge0-infer/tools/cli/serve_delegate_process.cpp +++ b/edge0-infer/tools/cli/serve_delegate_process.cpp @@ -544,9 +544,10 @@ int run_prepare_script(const fs::path& python, const fs::path& cwd, const fs::pa #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" #endif -#if defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) - // iOS cannot run the external engine's preparation scripts. Keep the native - // CLI buildable without referencing this SDK-unavailable process API. +#if defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) || defined(__ANDROID__) + // iOS and Android do not run the external engine's preparation scripts. Keep + // the native CLI buildable without referencing this process API, which the + // iOS SDK lacks and Android provides only from API 34. const int chdir_error = ENOTSUP; #else const int chdir_error = posix_spawn_file_actions_addchdir_np(&actions, directory.c_str());