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: 5 additions & 4 deletions onnxruntime/core/providers/openvino/ov_shared_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
namespace onnxruntime {
namespace openvino_ep {

SharedContext::SharedContext(std::filesystem::path bin_path)
: bin_path_(std::move(bin_path)),
bin_manager_(bin_path_) {
SharedContext::SharedContext(const std::filesystem::path& bin_path)
: bin_path_(bin_path),
bin_manager_(bin_path_),
weight_file_manager_(WeightFileManager::Get()) {
}

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

ov::Tensor tensor;
Expand Down
31 changes: 27 additions & 4 deletions onnxruntime/core/providers/openvino/ov_shared_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
namespace onnxruntime {
namespace openvino_ep {

class WeightFileManager;

class SharedContext : public std::enable_shared_from_this<SharedContext> {
public:
explicit SharedContext(std::filesystem::path bin_path);
explicit SharedContext(const std::filesystem::path& bin_path);
SharedContext() : SharedContext("") {}
virtual ~SharedContext() {}

struct Metadata {
struct Value {
Expand Down Expand Up @@ -83,7 +86,6 @@ class SharedContext : public std::enable_shared_from_this<SharedContext> {
return BinManager::GetBinPathForModel(model_path);
}

private:
struct WeightsFile {
ORT_DISALLOW_COPY_AND_ASSIGNMENT(WeightsFile);
WeightsFile() = delete;
Expand All @@ -104,7 +106,9 @@ class SharedContext : public std::enable_shared_from_this<SharedContext> {
std::map<std::string, MappingContainer> imported_device_tensors_;
};
Comment on lines 89 to 107

Copilot AI Feb 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WeightsFile struct lacks thread synchronization for its member access. With the introduction of WeightFileManager, WeightsFile instances are now shared across multiple SharedContext objects (line 128-137). This means multiple threads can concurrently call LoadWeights() and TryGetOrCreateDeviceMapping() on the same WeightsFile instance. The LoadWeights() method performs non-thread-safe operations on the file_ member (seekg and read), and TryGetOrCreateDeviceMapping() modifies imported_device_tensors_ without synchronization. Add a mutex member to WeightsFile and protect all member accesses to prevent race conditions.

Copilot uses AI. Check for mistakes.

void LoadTensorFromFile(
private:
void
LoadTensorFromFile(
Comment on lines +110 to +111

Copilot AI Feb 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type and function name are split across two lines, which is inconsistent with the rest of the codebase. All other function declarations in this file (e.g., lines 62, 76-79) keep the return type and function name on the same line. This should be reformatted to match the existing code style.

Suggested change
void
LoadTensorFromFile(
void LoadTensorFromFile(

Copilot uses AI. Check for mistakes.
Metadata::Value& value,
const std::filesystem::path& model_dir,
std::optional<ov::RemoteContext>& remote_context,
Expand All @@ -114,10 +118,29 @@ class SharedContext : public std::enable_shared_from_this<SharedContext> {
mutable std::shared_mutex mutex_;
std::filesystem::path bin_path_;
BinManager bin_manager_;
std::unordered_map<std::filesystem::path, std::unique_ptr<WeightsFile>> weight_files_;
std::shared_ptr<WeightFileManager> weight_file_manager_;
std::unordered_map<std::filesystem::path, std::shared_ptr<WeightsFile>> weight_files_;
Metadata::Map metadata_;
};

class WeightFileManager : public WeakSingleton<WeightFileManager> {
public:
using WeightsFile = SharedContext::WeightsFile;
std::shared_ptr<WeightsFile> GetOrCreateWeightsFile(const std::filesystem::path& weights_path) {
auto absolute_path = std::filesystem::absolute(weights_path);
std::lock_guard<std::mutex> lock(mutex_);
auto [it, inserted] = files_.try_emplace(absolute_path, nullptr);
if (inserted) {
it->second = std::make_shared<WeightsFile>(absolute_path);
}
return it->second;
}

private:
mutable std::mutex mutex_;
std::unordered_map<std::filesystem::path, std::shared_ptr<WeightsFile>> files_;
};

class SharedContextManager : public WeakSingleton<SharedContextManager> {
public:
std::shared_ptr<SharedContext> GetOrCreateActiveSharedContext(const std::filesystem::path& model_path) {
Expand Down
Loading