-
Notifications
You must be signed in to change notification settings - Fork 57
CVS-174585: Memory map shared weights when possible #829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
256b69d
3420d5a
5b69c25
f6f1dbc
8385a3b
64654ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,11 +20,11 @@ using Exception = ov::Exception; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace onnxruntime { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace openvino_ep { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SharedContext::SharedWeights::WeightsFile::WeightsFile(std::filesystem::path filename) : file_(filename, std::ios::in | std::ios::binary) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SharedContext::SharedWeights::WeightsFile::WeightsFile(std::filesystem::path filename) : file_(filename, std::ios::in | std::ios::binary), file_path_(filename) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| file_.exceptions(std::ifstream::failbit | std::ifstream::badbit); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| weights_size_ = file_.seekg(0, std::ios::end).tellg(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (std::ifstream::failure& e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| weights_size_ = std::filesystem::file_size(filename); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (std::exception& e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ORT_THROW("Error: Failed to open weight file at ", filename.string(), " ", e.what()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -35,6 +35,32 @@ void SharedContext::SharedWeights::WeightsFile::load_weights(size_t file_offset, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| file_.read(reinterpret_cast<char*>(data), size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void* SharedContext::SharedWeights::WeightsFile::TryGetOrCreateDeviceMapping(std::optional<ov::RemoteContext>& remote_context) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::string dev_name{}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (remote_context) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dev_name = remote_context->get_device_name(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auto [it, inserted] = imported_device_tensors_.emplace(dev_name, MappingContainer{}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (inserted) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (dev_name == "NPU") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if OPENVINO_VERSION_AT_LEAST(2025, 3) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // try to import the memory mapped file to remote tensor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ORT_ENFORCE(remote_context, "Error: Remote context is required for NPU device."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auto npu_context = remote_context->as<ov::intel_npu::level_zero::ZeroContext>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auto&& l0_tensor = npu_context.create_tensor(ov::element::Type_t::u8, {weights_size_}, ov::intel_npu::FileDescriptor(file_path_)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it->second = MappingContainer{.ptr_ = l0_tensor.get(), .tensor_ = l0_tensor}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (dev_name.empty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // CPU/virtual device case, create a CPU tensor memory mapped from file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auto&& mmaped_tensor = ov::read_tensor_data(file_path_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it->second = MappingContainer{.ptr_ = mmaped_tensor.data(), .tensor_ = mmaped_tensor}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+55
to
+57
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // CPU/virtual device case, create a CPU tensor memory mapped from file | |
| auto&& mmaped_tensor = ov::read_tensor_data(file_path_); | |
| it->second = MappingContainer{.ptr_ = mmaped_tensor.data(), .tensor_ = mmaped_tensor}; | |
| // CPU/virtual device case, create a CPU tensor memory mmapped from file | |
| auto&& mmapped_tensor = ov::read_tensor_data(file_path_); | |
| it->second = MappingContainer{.ptr_ = mmapped_tensor.data(), .tensor_ = mmapped_tensor}; |
Copilot
AI
Oct 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'mmaped' to 'mmapped' in variable name.
| uint8_t* mmaped_weights = static_cast<uint8_t*>(weights.TryGetOrCreateDeviceMapping(opt_remote_ctx)); | |
| if (mmaped_weights) { | |
| // We have memory mapped weights. Create a Tensor view into it for this value. | |
| ORT_ENFORCE(value.data_offset < weights.Size() && | |
| value.size <= weights.Size() && | |
| (value.data_offset <= weights.Size() - value.size), | |
| "File offset + size outside of external initializer file"); | |
| void* mmapped_offset = static_cast<void*>(mmaped_weights + value.data_offset); | |
| uint8_t* mmapped_weights = static_cast<uint8_t*>(weights.TryGetOrCreateDeviceMapping(opt_remote_ctx)); | |
| if (mmapped_weights) { | |
| // We have memory mapped weights. Create a Tensor view into it for this value. | |
| ORT_ENFORCE(value.data_offset < weights.Size() && | |
| value.size <= weights.Size() && | |
| (value.data_offset <= weights.Size() - value.size), | |
| "File offset + size outside of external initializer file"); | |
| void* mmapped_offset = static_cast<void*>(mmapped_weights + value.data_offset); |
Copilot
AI
Oct 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'mmaped' to 'mmapped' in variable name.
| uint8_t* mmaped_weights = static_cast<uint8_t*>(weights.TryGetOrCreateDeviceMapping(opt_remote_ctx)); | |
| if (mmaped_weights) { | |
| // We have memory mapped weights. Create a Tensor view into it for this value. | |
| ORT_ENFORCE(value.data_offset < weights.Size() && | |
| value.size <= weights.Size() && | |
| (value.data_offset <= weights.Size() - value.size), | |
| "File offset + size outside of external initializer file"); | |
| void* mmapped_offset = static_cast<void*>(mmaped_weights + value.data_offset); | |
| uint8_t* mmapped_weights = static_cast<uint8_t*>(weights.TryGetOrCreateDeviceMapping(opt_remote_ctx)); | |
| if (mmapped_weights) { | |
| // We have memory mapped weights. Create a Tensor view into it for this value. | |
| ORT_ENFORCE(value.data_offset < weights.Size() && | |
| value.size <= weights.Size() && | |
| (value.data_offset <= weights.Size() - value.size), | |
| "File offset + size outside of external initializer file"); | |
| void* mmapped_offset = static_cast<void*>(mmapped_weights + value.data_offset); |
Copilot
AI
Oct 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'mmaped' to 'mmapped' in variable name (note: mmapped_offset is already correct).
| uint8_t* mmaped_weights = static_cast<uint8_t*>(weights.TryGetOrCreateDeviceMapping(opt_remote_ctx)); | |
| if (mmaped_weights) { | |
| // We have memory mapped weights. Create a Tensor view into it for this value. | |
| ORT_ENFORCE(value.data_offset < weights.Size() && | |
| value.size <= weights.Size() && | |
| (value.data_offset <= weights.Size() - value.size), | |
| "File offset + size outside of external initializer file"); | |
| void* mmapped_offset = static_cast<void*>(mmaped_weights + value.data_offset); | |
| uint8_t* mmapped_weights = static_cast<uint8_t*>(weights.TryGetOrCreateDeviceMapping(opt_remote_ctx)); | |
| if (mmapped_weights) { | |
| // We have memory mapped weights. Create a Tensor view into it for this value. | |
| ORT_ENFORCE(value.data_offset < weights.Size() && | |
| value.size <= weights.Size() && | |
| (value.data_offset <= weights.Size() - value.size), | |
| "File offset + size outside of external initializer file"); | |
| void* mmapped_offset = static_cast<void*>(mmapped_weights + value.data_offset); |
Uh oh!
There was an error while loading. Please reload this page.