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
19 changes: 7 additions & 12 deletions edge0-infer/src/weights/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ struct Registry {
std::condition_variable changed;
std::map<std::uint64_t, std::weak_ptr<Storage>> storages;
std::map<std::string, std::pair<std::uint64_t, std::size_t>> ids;
std::map<std::pair<std::string,ReadMode>,std::shared_ptr<platform::ReadFile>> files;
// Source file identity is checked once, when the file is opened.
std::map<std::pair<std::string,ReadMode>,std::pair<std::shared_ptr<platform::ReadFile>,platform::FileIdentity>> files;

};
class Usage final : public exec::Use {
Expand Down Expand Up @@ -145,18 +146,19 @@ Result<std::shared_ptr<Write>> Weights::begin_write(const std::shared_ptr<Storag
std::size_t index;
if(found==selected_files.end()) {
std::shared_ptr<platform::ReadFile> file;
platform::FileIdentity identity{};
{
std::lock_guard<std::mutex> 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)
Expand Down Expand Up @@ -335,13 +337,6 @@ Result<std::vector<Binding>> Weights::complete(const std::shared_ptr<Write>& 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<platform::FileIdentity>(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<std::mutex> lock(registry_->mutex);
std::vector<Binding> bindings;
Expand Down
1 change: 0 additions & 1 deletion edge0-infer/src/weights/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class Write {
std::vector<std::shared_ptr<platform::ReadFile>> files_;
std::vector<platform::FileIdentity> identities_;
std::vector<std::size_t> item_files_;
std::vector<std::pair<std::string,ReadMode>> file_keys_;
std::vector<std::future<exec::Status>> futures_;
bool started_ = false, finished_ = false;
void rollback(bool isolate_all = false);
Expand Down
4 changes: 2 additions & 2 deletions edge0-infer/tests/unit/weights/read_batch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ std::pair<std::size_t,std::size_t> group(const std::shared_ptr<exec::Backend>& b
if(!fail) {
std::vector<std::size_t> 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;
Expand Down
11 changes: 0 additions & 11 deletions edge0-infer/tests/unit/weights/storage_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,6 @@ int main() {
resume.set_value(); auto ready = take(weights.complete(write)); CHECK(ready.size() == 3);
weights.before_read = {};
}
{
std::promise<void> 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));
Expand Down
7 changes: 4 additions & 3 deletions edge0-infer/tools/cli/serve_delegate_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading