1919namespace onnxruntime {
2020namespace openvino_ep {
2121
22+ class WeightFileManager ;
23+
2224class 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+
121144class SharedContextManager : public WeakSingleton <SharedContextManager> {
122145 public:
123146 std::shared_ptr<SharedContext> GetOrCreateActiveSharedContext (const std::filesystem::path& model_path) {
0 commit comments