Skip to content

Commit b7ddd7b

Browse files
committed
Refactor the code for PR
1 parent d304d7e commit b7ddd7b

9 files changed

+50
-217
lines changed

onnxruntime/core/providers/openvino/backend_manager.cc

+30-21
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,15 @@ BackendManager::BackendManager(SessionContext& session_context,
7777
ptr_stream_t model_stream;
7878
std::unique_ptr<onnx::ModelProto> model_proto;
7979
if (subgraph_context_.is_ep_ctx_graph) {
80-
std::cout << " inside is_ep_ctx_graph " << std::endl;
81-
std::string model_name = onnxruntime::openvino_ep::BackendManager::stripAfterFirstDot(session_context_.onnx_model_path_name.filename().string());
80+
std::string filename;
81+
if (!session_context_.so_context_file_path.empty()) {
82+
filename = session_context_.so_context_file_path.filename().string();
83+
} else if (!session_context_.onnx_model_path_name.empty()) {
84+
filename = session_context_.onnx_model_path_name.filename().string();
85+
} else {
86+
ORT_THROW("Either Session_options ep.context_file_path or model path must be specified");
87+
}
88+
std::string model_name = onnxruntime::openvino_ep::BackendManager::stripAfterFirstDot(filename);
8289
auto subgraph_name = model_name + "_" + subgraph_context_.subgraph_name;
8390
model_stream = ep_ctx_handle_.GetModelBlobStream(shared_context_,
8491
session_context_.so_context_file_path,
@@ -103,9 +110,9 @@ BackendManager::BackendManager(SessionContext& session_context,
103110
if (!sw.mapped_weights) {
104111
sw.mapped_weights = std::make_unique<SharedContext::SharedWeights::WeightsFile>(weight_filename);
105112
}
106-
std::cout << " Call createOVTensors in backend_manager.cc" << std::endl;
107113
backend_utils::CreateOVTensors(session_context_.device_type, sw.metadata, *sw.mapped_weights);
108-
std::cout << " create OVTensors successful " << std::endl;
114+
} else {
115+
ORT_THROW(" External weight file is not found ");
109116
}
110117
}
111118

@@ -207,11 +214,16 @@ BackendManager::BackendManager(SessionContext& session_context,
207214
}
208215

209216
std::string BackendManager::stripAfterFirstDot(std::string filename) {
210-
size_t dotPos = filename.find('.'); // Find first dot
211-
if (dotPos == std::string::npos) {
217+
size_t dotPos = filename.find('.'); // Find first dot
218+
size_t ctxPos = filename.find("_ctx"); // Find first dot
219+
if (dotPos == std::string::npos && ctxPos == std::string::npos) {
212220
return filename; // No dot found, return full filename
213221
}
214-
return filename.substr(0, dotPos); // Return everything before first dot
222+
if (dotPos != std::string::npos)
223+
filename = filename.substr(0, dotPos); // strip everything after first dot
224+
if (ctxPos != std::string::npos)
225+
filename = filename.substr(0, ctxPos); // strip everything after _ctx
226+
return filename;
215227
}
216228

217229
// Call EPContext model exporter here if the provider option for exporting
@@ -227,36 +239,33 @@ Status BackendManager::ExportCompiledBlobAsEPCtxNode(const onnxruntime::GraphVie
227239
ORT_THROW(exception_str);
228240
}
229241

230-
std::cout << " inside export compiled model " << std::endl;
231-
232242
// If embed_mode, then pass on the serialized blob
233243
// If not embed_mode, dump the blob here and only pass on the path to the blob
234244
std::string model_blob_str;
235245
auto compiled_model = concrete_backend_->GetOVCompiledModel();
236246
if (session_context_.so_share_ep_contexts) {
237247
std::ostringstream model_blob_stream;
238248
compiled_model.export_model(model_blob_stream);
239-
std::cout << " inside export compiled model - share ep contexts" << std::endl;
240249

241-
// std::ofstream file(metadata_filename, std::ios::app| std::ios::binary);
242-
// std::cout << " write to metadata bin - " << metadata_filename << std::endl;
243250
auto& subgraph_metadata = shared_context_.shared_weights.subgraph_metadata;
244-
std::string model_name = onnxruntime::openvino_ep::BackendManager::stripAfterFirstDot(session_context_.onnx_model_path_name.filename().string());
251+
std::string filename = "";
252+
if (!session_context_.so_context_file_path.empty()) {
253+
filename = session_context_.so_context_file_path.filename().string();
254+
} else if (!session_context_.onnx_model_path_name.empty()) {
255+
filename = session_context_.onnx_model_path_name.filename().string();
256+
} else {
257+
ORT_THROW("Either Session_options ep.context_file_path or model path must be specified");
258+
}
259+
std::string model_name = onnxruntime::openvino_ep::BackendManager::stripAfterFirstDot(filename);
245260
auto subgraph_name = model_name + "_" + subgraph_context_.subgraph_name;
246261
sw::SubgraphMetadata::Map::key_type key{subgraph_name};
247262
sw::SubgraphMetadata::Map::mapped_type value{};
248263

249264
auto& bin_file = shared_context_.shared_weights.shared_bin_file.bin_file_;
250-
std::cout << " subgraph name " << subgraph_name << "key = " << key.name << " For bin write " << std::endl;
251265
if (!subgraph_metadata.contains(key) && bin_file.is_open()) {
252-
// std::cout << "Current offset before "<< subgraph_context_.subgraph_name << " = " << bin_file.tellp() << std::endl;
253-
value.epctx_offset = bin_file.tellp();
254-
std::cout << " bin file location for writing subgraph = " << bin_file.tellp() << std::endl;
266+
value.epctx_offset = static_cast<uint64_t>(bin_file.tellp());
255267
bin_file << model_blob_stream.str();
256-
// compiled_model.export_model(bin_file);
257-
// std::cout << "Current offset after "<< subgraph_context_.subgraph_name << " = " << bin_file.tellp() << std::endl;
258-
value.epctx_length = static_cast<size_t>(static_cast<std::streamoff>(bin_file.tellp()) - value.epctx_offset);
259-
// std::cout << "Key = " << key.name << " Offset = " << value.epctx_offset << " , length = " << value.epctx_length << std::endl;
268+
value.epctx_length = static_cast<size_t>(static_cast<uint64_t>(bin_file.tellp()) - value.epctx_offset);
260269
subgraph_metadata.emplace(key, std::move(value));
261270
}
262271

onnxruntime/core/providers/openvino/backend_utils.cc

+1-11
Original file line numberDiff line numberDiff line change
@@ -303,33 +303,23 @@ void CreateOVTensors(const std::string& device_name,
303303
SharedContext::SharedWeights::Metadata::Map& metadata_map,
304304
SharedContext::SharedWeights::WeightsFile& weights) {
305305
for (auto& [key, value] : metadata_map) {
306-
// std::cout << " Key = " << key.name << std::endl;
307306
if (value.tensor) {
308-
// std::cout << " Value already present for key = " << key.name << std::endl;
309307
continue;
310308
}
311309

312310
// Get element data type
313-
// std::cout << " value element type = " << value.element_type << std::endl;
314311
auto onnx_element_type = (ONNX_NAMESPACE::TensorProto_DataType)value.element_type;
315312

316313
ov::element::Type ov_elementType = GetOpenVINOElementType(onnx_element_type); // Map to OpenVINO data type
317-
// std::cout << "value dimensions = " << std::endl;
318-
// for (auto dim:value.dimensions){
319-
// std::cout << dim << std::endl;
320-
// }
314+
321315
// Create OpenVINO Tensor
322316
if (device_name == "NPU") {
323317
// Use remote tensors
324318
auto npu_context = OVCore::Get()->core.get_default_context("NPU").as<ov::intel_npu::level_zero::ZeroContext>();
325319
auto&& remote_tensor = npu_context.create_l0_host_tensor(ov_elementType, value.dimensions, ov::intel_npu::TensorType::INPUT);
326-
// std::cout << " Remote tensor created " << std::endl;
327320
// Copy data to remote tensor
328-
// std::cout << " value size = " << value.size << std::endl;
329321
weights.load_weights(value.data_offset, remote_tensor.get(), value.size);
330322
value.tensor = std::make_shared<ov::Tensor>(remote_tensor);
331-
// std::cout << " value tensor created " << std::endl;
332-
333323
} else {
334324
// Use vanilla tensors
335325
value.tensor = std::make_shared<ov::Tensor>(ov_elementType, value.dimensions);

onnxruntime/core/providers/openvino/backends/basic_backend.cc

-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ BasicBackend::BasicBackend(std::unique_ptr<ONNX_NAMESPACE::ModelProto>& model_pr
116116
ov_model, hw_target, device_config, subgraph_context_.subgraph_name);
117117
}
118118
#endif
119-
std::cout << " loaded model to the plugin " << std::endl;
120119
LOGS_DEFAULT(INFO) << log_tag << "Loaded model to the plugin";
121120
} catch (const char* msg) {
122121
ORT_THROW(msg);
@@ -128,14 +127,11 @@ BasicBackend::BasicBackend(std::unique_ptr<ONNX_NAMESPACE::ModelProto>& model_pr
128127
if (session_context_.so_share_ep_contexts) {
129128
initializer = [&metadata](OVInferRequestPtr ir_ptr) {
130129
const auto input_count = ir_ptr->GetNumInputs();
131-
std::cout << " ov ir input count = " << input_count << std::endl;
132130
for (auto i = 0u; i < input_count; i++) {
133131
using Key = SharedContext::SharedWeights::Metadata::Key;
134132
const auto tensor_key = Key{ir_ptr->GetInputTensorName(i)};
135133
if (metadata.contains(tensor_key)) {
136134
auto& value = metadata.at(tensor_key);
137-
// ORT_ENFORCE(value.tensor->get_byte_size() == value.size, "Unexpected tensor size mismatch");
138-
std::cout << " value tensor is set with shape = " << value.tensor->get_byte_size() << " input size from metadata = " << value.size << std::endl;
139135
ir_ptr->SetTensor(tensor_key.name, value.tensor);
140136
}
141137
}

onnxruntime/core/providers/openvino/contexts.h

+7-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <filesystem>
1212
#include <memory>
1313
#include "core/common/common.h"
14+
#include "core/providers/shared_library/provider_api.h"
1415
#include "core/providers/openvino/ov_interface.h"
1516

1617
namespace onnxruntime {
@@ -27,12 +28,12 @@ class SharedContext : public WeakSingleton<SharedContext> {
2728
struct SharedWeights {
2829
struct Header {
2930
uint32_t bin_version = 1;
30-
long footer_offset = 0;
31+
uint64_t footer_offset = 0;
3132
} header_;
3233
struct Footer {
33-
long subgraph_offset;
34+
uint64_t subgraph_offset;
3435
size_t subgraph_length;
35-
long metadata_offset;
36+
uint64_t metadata_offset;
3637
size_t metadata_length;
3738
} footer_;
3839

@@ -57,7 +58,7 @@ class SharedContext : public WeakSingleton<SharedContext> {
5758
using Map = std::unordered_map<Key, Value, Hash>;
5859
void writeMetadataToBinaryFile(SharedContext& shared_context, const Metadata::Map& metadata);
5960
void readMetadataFromBinaryFile(SharedContext& shared_context, Metadata::Map& metadata);
60-
};
61+
} metadata_;
6162

6263
struct SubgraphMetadata {
6364
struct Key {
@@ -70,15 +71,15 @@ class SharedContext : public WeakSingleton<SharedContext> {
7071
}
7172
};
7273
struct Value {
73-
long epctx_offset;
74+
uint64_t epctx_offset;
7475
size_t epctx_length;
7576
};
7677
using Map = std::unordered_map<Key, Value, Hash>;
7778
void writeSubgraphDataToBinaryFile(SharedContext& shared_context,
7879
const SubgraphMetadata::Map& subgraph_metadata);
7980
void readSubgraphDataFromBinaryFile(SharedContext& shared_context,
8081
SubgraphMetadata::Map& subgraph_metadata);
81-
};
82+
} subgraph_metadata_;
8283

8384
struct WeightsFile {
8485
ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(WeightsFile);
@@ -138,9 +139,7 @@ class SharedContext : public WeakSingleton<SharedContext> {
138139

139140
fs::path external_weight_filename;
140141
std::unique_ptr<WeightsFile> mapped_weights;
141-
Metadata metadata_;
142142
Metadata::Map metadata;
143-
SubgraphMetadata subgraph_metadata_;
144143
SubgraphMetadata::Map subgraph_metadata;
145144
} shared_weights;
146145
};

onnxruntime/core/providers/openvino/onnx_ctx_model_helper.cc

-8
Original file line numberDiff line numberDiff line change
@@ -124,32 +124,24 @@ std::unique_ptr<std::istream> EPCtxHandler::GetModelBlobStream(SharedContext& sh
124124
}
125125
blob_filepath = blob_filepath.parent_path() / ep_cache_context;
126126
ORT_ENFORCE(std::filesystem::exists(blob_filepath), "Blob file not found: ", blob_filepath.string());
127-
std::cout << " blob_filepath " << blob_filepath.filename().string() << std::endl;
128-
std::cout << " shared bin filename = " << shared_context_.shared_weights.shared_bin_file.shared_bin_filename.filename().string() << std::endl;
129127
if (blob_filepath == shared_context_.shared_weights.shared_bin_file.shared_bin_filename) {
130128
LOGS_DEFAULT(VERBOSE) << "[OpenVINO EP] Read blob from Shared bin file - " << blob_filepath;
131129
auto& sb = shared_context_.shared_weights.shared_bin_file;
132130
// check if size of bin file is greater than the header as it gets written at the begining
133131
ORT_ENFORCE(sb.bin_size_ > 8, " Bin file is empty. Regenerate the epctx model. Bin file path : ", blob_filepath.string());
134132
auto subgraph_metadata = shared_context_.shared_weights.subgraph_metadata;
135133
using Key = SharedContext::SharedWeights::SubgraphMetadata::Key;
136-
std::cout << " subgraph name = " << subgraph_name << std::endl;
137134
const auto subgraph_key = Key{subgraph_name};
138135
auto it = subgraph_metadata.find(subgraph_key);
139136
if (it != subgraph_metadata.end()) {
140137
auto& value = it->second;
141-
std::cout << " value.epctx_offset = " << value.epctx_offset << std::endl;
142-
std::cout << " value.epctx_length = " << value.epctx_length << std::endl;
143-
std::cout << " sb.bin_size_ = " << sb.bin_size_ << std::endl;
144-
145138
if (value.epctx_offset < sb.bin_size_ && value.epctx_length <= sb.bin_size_ &&
146139
(value.epctx_offset <= sb.bin_size_ - value.epctx_length)) {
147140
sb.bin_file_.seekg(value.epctx_offset); // Move to the specified offset
148141
std::string buffer(value.epctx_length, '\0'); // preallocate space
149142
sb.bin_file_.read(&buffer[0], value.epctx_length); // Read the specified length
150143
// Adjust string size in case of a short read
151144
buffer.resize(sb.bin_file_.gcount());
152-
std::cout << " Read epctx into stream " << std::endl;
153145
result.reset((std::istream*)new std::istringstream(buffer));
154146
}
155147
}

onnxruntime/core/providers/openvino/onnx_ctx_model_helper.h

-22
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,5 @@ class EPCtxHandler {
4444
std::unique_ptr<Model> epctx_model_;
4545
const logging::Logger& logger_;
4646
};
47-
48-
// class LimitedFileStreambuf : public std::streambuf {
49-
// private:
50-
// std::fstream& file; // Reference to the existing file stream
51-
// long start, end; // Start and end positions
52-
53-
// protected:
54-
// int_type underflow() override {
55-
// if (file.tellg() >= end || file.eof())
56-
// return traits_type::eof(); // Stop reading if we reach the limit
57-
58-
// return file.get(); // Read next character directly from the file
59-
// }
60-
61-
// public:
62-
// LimitedFileStreambuf(std::fstream& bin_file_, long start, long end)
63-
// : file(bin_file_), start(start), end(end) {
64-
// file.clear(); // Clear error flags in case of previous reads
65-
// file.seekg(start); // Move file pointer to the start position
66-
// }
67-
// };
68-
6947
} // namespace openvino_ep
7048
} // namespace onnxruntime

0 commit comments

Comments
 (0)