Skip to content

Commit c7722bc

Browse files
ankitm3kericcraw
authored andcommitted
[OVEP] ORT 1.24 Release Patch (#27238)
### Description Re-use weight files and their underlying memory maps across shared contexts. ### Motivation and Context This reduces resident memory when different ep shared context sets reference the same weight file. Co-authored-by: Eric Crawford <eric.r.crawford@intel.com>
1 parent 5c46e2c commit c7722bc

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

onnxruntime/core/providers/openvino/ov_shared_context.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
namespace onnxruntime {
1111
namespace openvino_ep {
1212

13-
SharedContext::SharedContext(std::filesystem::path bin_path)
14-
: bin_path_(std::move(bin_path)),
15-
bin_manager_(bin_path_) {
13+
SharedContext::SharedContext(const std::filesystem::path& bin_path)
14+
: bin_path_(bin_path),
15+
bin_manager_(bin_path_),
16+
weight_file_manager_(WeightFileManager::Get()) {
1617
}
1718

1819
static bool InRange(size_t offset, size_t size, size_t total_size) {
@@ -74,7 +75,7 @@ void SharedContext::LoadTensorFromFile(
7475
const auto weights_location = model_dir / value.serialized.location;
7576
auto& weights_file = weight_files_[weights_location];
7677
if (!weights_file) {
77-
weights_file = std::make_unique<WeightsFile>(weights_location);
78+
weights_file = weight_file_manager_->GetOrCreateWeightsFile(weights_location);
7879
}
7980

8081
ov::Tensor tensor;

onnxruntime/core/providers/openvino/ov_shared_context.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
namespace onnxruntime {
2020
namespace openvino_ep {
2121

22+
class WeightFileManager;
23+
2224
class SharedContext : public std::enable_shared_from_this<SharedContext> {
2325
public:
24-
explicit SharedContext(std::filesystem::path bin_path);
26+
explicit SharedContext(const std::filesystem::path& bin_path);
2527
SharedContext() : SharedContext("") {}
28+
virtual ~SharedContext() {}
2629

2730
struct Metadata {
2831
struct Value {
@@ -83,7 +86,6 @@ class SharedContext : public std::enable_shared_from_this<SharedContext> {
8386
return BinManager::GetBinPathForModel(model_path);
8487
}
8588

86-
private:
8789
struct WeightsFile {
8890
ORT_DISALLOW_COPY_AND_ASSIGNMENT(WeightsFile);
8991
WeightsFile() = delete;
@@ -104,7 +106,9 @@ class SharedContext : public std::enable_shared_from_this<SharedContext> {
104106
std::map<std::string, MappingContainer> imported_device_tensors_;
105107
};
106108

107-
void LoadTensorFromFile(
109+
private:
110+
void
111+
LoadTensorFromFile(
108112
Metadata::Value& value,
109113
const std::filesystem::path& model_dir,
110114
std::optional<ov::RemoteContext>& remote_context,
@@ -114,10 +118,29 @@ class SharedContext : public std::enable_shared_from_this<SharedContext> {
114118
mutable std::shared_mutex mutex_;
115119
std::filesystem::path bin_path_;
116120
BinManager bin_manager_;
117-
std::unordered_map<std::filesystem::path, std::unique_ptr<WeightsFile>> weight_files_;
121+
std::shared_ptr<WeightFileManager> weight_file_manager_;
122+
std::unordered_map<std::filesystem::path, std::shared_ptr<WeightsFile>> weight_files_;
118123
Metadata::Map metadata_;
119124
};
120125

126+
class WeightFileManager : public WeakSingleton<WeightFileManager> {
127+
public:
128+
using WeightsFile = SharedContext::WeightsFile;
129+
std::shared_ptr<WeightsFile> GetOrCreateWeightsFile(const std::filesystem::path& weights_path) {
130+
auto absolute_path = std::filesystem::absolute(weights_path);
131+
std::lock_guard<std::mutex> lock(mutex_);
132+
auto [it, inserted] = files_.try_emplace(absolute_path, nullptr);
133+
if (inserted) {
134+
it->second = std::make_shared<WeightsFile>(absolute_path);
135+
}
136+
return it->second;
137+
}
138+
139+
private:
140+
mutable std::mutex mutex_;
141+
std::unordered_map<std::filesystem::path, std::shared_ptr<WeightsFile>> files_;
142+
};
143+
121144
class SharedContextManager : public WeakSingleton<SharedContextManager> {
122145
public:
123146
std::shared_ptr<SharedContext> GetOrCreateActiveSharedContext(const std::filesystem::path& model_path) {

0 commit comments

Comments
 (0)