Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[NPU] Update vcl allocator and NetworkDescription for aligned allocation (#33717)

Xin Wang6a59b895bf9f
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/icompiler.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/icompiler.hpp
index ab85f368885..ea6bad78fa7 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/icompiler.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/icompiler.hpp
@@ -27,6 +27,10 @@ struct NetworkDescription final {
NetworkDescription(std::vector<uint8_t>&& compiledNetwork, NetworkMetadata&& metadata)
: compiledNetwork(std::move(compiledNetwork)),
metadata(std::move(metadata)) {}
+ NetworkDescription(ov::Tensor&& compiledNetWorkTensor, NetworkMetadata&& metadata)
+ : compiledNetwork(),
+ metadata(std::move(metadata)),
+ compiledNetworkTensor(std::move(compiledNetWorkTensor)) {}
// Force move semantics to prevent blob copies
NetworkDescription(const NetworkDescription&) = delete;
NetworkDescription(NetworkDescription&&) = default;
@@ -37,6 +41,8 @@ struct NetworkDescription final {
std::vector<uint8_t> compiledNetwork;
NetworkMetadata metadata;
+
+ ov::Tensor compiledNetworkTensor;
};
/**
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/compiler_impl.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/compiler_impl.cpp
index 8b5e40c7a8c..99ef32c5825 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/compiler_impl.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/compiler_impl.cpp
@@ -9,6 +9,7 @@
#include "intel_npu/config/options.hpp"
#include "intel_npu/npu_private_properties.hpp"
#include "intel_npu/profiling.hpp"
+#include "intel_npu/utils/utils.hpp"
#include "openvino/runtime/make_tensor.hpp"
#include "openvino/util/file_util.hpp"
#include "openvino/util/shared_object.hpp"
@@ -57,6 +58,126 @@ bool isUseBaseModelSerializer(UsedVersion useVersion, const intel_npu::FilteredC
return false;
}
+template <typename T>
+class ByteAlignedAllocator {
+private:
+ intel_npu::utils::AlignedAllocator allocator_;
+
+public:
+ using value_type = T;
+ using size_type = std::size_t;
+ using difference_type = std::ptrdiff_t;
+ using pointer = T*;
+ using const_pointer = const T*;
+
+ template <typename U>
+ struct rebind {
+ using other = ByteAlignedAllocator<U>;
+ };
+
+ ByteAlignedAllocator() : allocator_(intel_npu::utils::STANDARD_PAGE_SIZE) {}
+
+ ByteAlignedAllocator(const ByteAlignedAllocator& other) : allocator_(intel_npu::utils::STANDARD_PAGE_SIZE) {}
+
+ template <typename U>
+ ByteAlignedAllocator(const ByteAlignedAllocator<U>& other) : allocator_(intel_npu::utils::STANDARD_PAGE_SIZE) {}
+
+ ByteAlignedAllocator& operator=(const ByteAlignedAllocator& other) {
+ return *this;
+ }
+
+ T* allocate(size_t n) {
+ size_t aligned_size = intel_npu::utils::align_size_to_standard_page_size(n);
+ return static_cast<T*>(allocator_.allocate(aligned_size, 1));
+ }
+
+ void deallocate(T* ptr, size_t n) {
+ size_t aligned_size = intel_npu::utils::align_size_to_standard_page_size(n);
+ allocator_.deallocate(ptr, aligned_size, 1);
+ }
+
+ template <typename U>
+ bool operator==(const ByteAlignedAllocator<U>& other) const {
+ return allocator_.is_equal(other.allocator_);
+ }
+
+ template <typename U>
+ bool operator!=(const ByteAlignedAllocator<U>& other) const {
+ return !(*this == other);
+ }
+
+ size_type max_size() const noexcept {
+ return std::numeric_limits<size_type>::max() / sizeof(T);
+ }
+};
+
+using AlignedVector = std::vector<uint8_t, ByteAlignedAllocator<uint8_t>>;
+struct vcl_allocator_vector : vcl_allocator2_t {
+ vcl_allocator_vector() : vcl_allocator2_t{vector_allocate, vector_deallocate} {}
+
+ static uint8_t* vector_allocate(vcl_allocator2_t* allocator, size_t size) {
+ vcl_allocator_vector* vecAllocator = static_cast<vcl_allocator_vector*>(allocator);
+ size_t aligned_size = intel_npu::utils::align_size_to_standard_page_size(size);
+ auto newVec = std::make_shared<AlignedVector>();
+ vecAllocator->m_vec = newVec;
+ vecAllocator->m_vec->resize(aligned_size);
+ if (intel_npu::utils::memory_and_size_aligned_to_standard_page_size(vecAllocator->m_vec->data(),
+ vecAllocator->m_vec->size()) == false) {
+ OPENVINO_THROW("vcl_allocator_vector: allocated memory is not aligned to standard page size");
+ }
+ return vecAllocator->m_vec->data();
+ }
+
+ static void vector_deallocate(vcl_allocator2_t* allocator, uint8_t* ptr) {
+ vcl_allocator_vector* vecAllocator = static_cast<vcl_allocator_vector*>(allocator);
+ vecAllocator->m_vec->clear();
+ vecAllocator->m_vec->shrink_to_fit();
+ }
+
+ std::shared_ptr<AlignedVector> m_vec;
+};
+
+struct vcl_allocator_vector_2 : vcl_allocator2_t {
+ vcl_allocator_vector_2() : vcl_allocator2_t{vector_allocate, vector_deallocate} {}
+
+ static uint8_t* vector_allocate(vcl_allocator2_t* allocator, size_t size) {
+ vcl_allocator_vector_2* vecAllocator = static_cast<vcl_allocator_vector_2*>(allocator);
+ size_t aligned_size = intel_npu::utils::align_size_to_standard_page_size(size);
+ auto newVec = std::make_shared<AlignedVector>();
+ newVec->resize(aligned_size);
+ uint8_t* ptr = newVec->data();
+ if (intel_npu::utils::memory_and_size_aligned_to_standard_page_size(newVec->data(), newVec->size()) == false) {
+ OPENVINO_THROW("vcl_allocator_vector: allocated memory is not aligned to standard page size");
+ }
+ vecAllocator->m_vector.emplace_back(newVec);
+
+ return ptr;
+ }
+
+ static void vector_deallocate(vcl_allocator2_t* allocator, uint8_t* ptr) {
+ vcl_allocator_vector_2* vecAllocator = static_cast<vcl_allocator_vector_2*>(allocator);
+ auto it = std::find_if(vecAllocator->m_vector.begin(), vecAllocator->m_vector.end(), [ptr](const auto& vec) {
+ return vec->data() == ptr;
+ });
+
+ if (it != vecAllocator->m_vector.end()) {
+ vecAllocator->m_vector.erase(it);
+ vecAllocator->m_vector.shrink_to_fit();
+ } else {
+ OPENVINO_THROW("vcl_allocator_vector_2: pointer to deallocate not found");
+ }
+ }
+
+ std::vector<std::shared_ptr<AlignedVector>> m_vector;
+};
+
+ov::Tensor make_tensor_from_aligned_vector(std::shared_ptr<AlignedVector> vector) {
+ auto tensor = ov::Tensor(ov::element::u8, ov::Shape{vector->size()}, vector->data());
+ auto impl = ov::get_tensor_impl(std::move(tensor));
+ impl._so = vector;
+ return ov::make_tensor(impl);
+}
+
} // namespace
namespace intel_npu {
@@ -298,55 +419,6 @@ std::shared_ptr<void> VCLCompilerImpl::getLinkedLibrary() const {
return VCLApi::getInstance()->getLibrary();
}
-struct vcl_allocator_vector : vcl_allocator2_t {
- vcl_allocator_vector() : vcl_allocator2_t{vector_allocate, vector_deallocate} {}
-
- static uint8_t* vector_allocate(vcl_allocator2_t* allocator, size_t size) {
- vcl_allocator_vector* vecAllocator = static_cast<vcl_allocator_vector*>(allocator);
- vecAllocator->m_vec.resize(size);
- return vecAllocator->m_vec.data();
- }
-
- static void vector_deallocate(vcl_allocator2_t* allocator, uint8_t* ptr) {
- vcl_allocator_vector* vecAllocator = static_cast<vcl_allocator_vector*>(allocator);
- vecAllocator->m_vec.clear();
- vecAllocator->m_vec.shrink_to_fit();
- }
-
- std::vector<uint8_t> m_vec;
-};
-
-struct vcl_allocator_vector_2 : vcl_allocator2_t {
- vcl_allocator_vector_2() : vcl_allocator2_t{vector_allocate, vector_deallocate} {}
-
- static uint8_t* vector_allocate(vcl_allocator2_t* allocator, size_t size) {
- vcl_allocator_vector_2* vecAllocator = static_cast<vcl_allocator_vector_2*>(allocator);
- auto newVec = std::make_shared<std::vector<uint8_t>>();
- newVec->resize(size);
- uint8_t* ptr = newVec->data();
- vecAllocator->m_vector.emplace_back(newVec);
- return ptr;
- }
-
- static void vector_deallocate(vcl_allocator2_t* allocator, uint8_t* ptr) {
- vcl_allocator_vector_2* vecAllocator = static_cast<vcl_allocator_vector_2*>(allocator);
- vecAllocator->m_vector.clear();
- vecAllocator->m_vector.shrink_to_fit();
- }
-
- std::vector<std::shared_ptr<std::vector<uint8_t>>> m_vector;
-};
-
-struct vcl_allocator_malloc {
- static uint8_t* vcl_allocate(uint64_t size) {
- return reinterpret_cast<uint8_t*>(malloc(size));
- }
-
- static void vcl_deallocate(uint8_t* ptr) {
- free(ptr);
- }
-};
-
NetworkDescription VCLCompilerImpl::compile(const std::shared_ptr<const ov::Model>& model, const Config& config) const {
return compile(model, config, false);
}
@@ -410,12 +482,17 @@ NetworkDescription VCLCompilerImpl::compile(const std::shared_ptr<const ov::Mode
if (size == 0 || blob == nullptr) {
OPENVINO_THROW("Failed to create VCL executable, size is zero or blob is null");
}
+ // The allocated size from VCL will be equal or smaller than the allocated size in allocator
+ _logger.debug("Blob size from VCL: %zu ptr %p", size, static_cast<void*>(blob));
+ _logger.debug("Allocated vector size: %zu ptr: %p",
+ allocator.m_vec->size(),
+ static_cast<void*>(allocator.m_vec->data()));
// Use empty metadata as VCL does not support metadata extraction
NetworkMetadata metadata;
- _logger.debug("compile end, blob size:%d", allocator.m_vec.size());
- return NetworkDescription(std::move(allocator.m_vec), std::move(metadata));
+ _logger.debug("compile end, blob size:%d", allocator.m_vec->size());
+ return NetworkDescription(make_tensor_from_aligned_vector(allocator.m_vec), std::move(metadata));
} else {
OPENVINO_THROW("Not supported VCL version: %d.%d, please use VCL 6.1 or later",
_vclVersion.major,
@@ -480,7 +557,8 @@ std::vector<std::shared_ptr<NetworkDescription>> VCLCompilerImpl::compileWsOneSh
for (auto& blob : allocator.m_vector) {
// Use empty metadata as VCL does not support metadata extraction
NetworkMetadata metadata;
- networkDescrs.emplace_back(std::make_shared<NetworkDescription>(std::move(*blob), std::move(metadata)));
+ networkDescrs.emplace_back(
+ std::make_shared<NetworkDescription>(make_tensor_from_aligned_vector(blob), std::move(metadata)));
}
return networkDescrs;
}
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp
index 3b5fc0adf5c..74fa2a641f9 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp
@@ -112,7 +112,12 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compile(const std::shared_ptr<con
auto networkDesc = _compiler->compile(model, config);
_logger.debug("compile end");
- ov::Tensor tensor = make_tensor_from_vector(networkDesc.compiledNetwork);
+ ov::Tensor tensor;
+ if (networkDesc.compiledNetwork.size() > 0) {
+ tensor = make_tensor_from_vector(networkDesc.compiledNetwork);
+ } else {
+ tensor = std::move(networkDesc.compiledNetworkTensor);
+ }
GraphDescriptor graphDesc;
NetworkMetadata networkMeta;
@@ -180,7 +185,12 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compileWS(const std::shared_ptr<o
std::vector<std::shared_ptr<NetworkDescription>> initNetworkDescriptions =
std::move(initMainNetworkDescriptions);
- tensorMain = make_tensor_from_vector(mainNetworkDescription->compiledNetwork);
+ if (mainNetworkDescription->compiledNetwork.size() > 0) {
+ tensorMain = make_tensor_from_vector(mainNetworkDescription->compiledNetwork);
+ } else {
+ tensorMain = std::move(mainNetworkDescription->compiledNetworkTensor);
+ }
+
if (_zeGraphExt) {
// Depending on the config, we may get an error when trying to
// get the graph handle from the compiled network
@@ -201,7 +211,12 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compileWS(const std::shared_ptr<o
tensorsInits.reserve(initNetworkDescriptions.size());
initNetworkMetadata.reserve(initNetworkDescriptions.size());
for (auto& networkDesc : initNetworkDescriptions) {
- ov::Tensor tensor = make_tensor_from_vector(networkDesc->compiledNetwork);
+ ov::Tensor tensor;
+ if (networkDesc->compiledNetwork.size() > 0) {
+ tensor = make_tensor_from_vector(networkDesc->compiledNetwork);
+ } else {
+ tensor = std::move(networkDesc->compiledNetworkTensor);
+ }
GraphDescriptor initGraphDesc;
NetworkMetadata initNetworkMeta;
if (_zeGraphExt) {
@@ -236,7 +251,12 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compileWS(const std::shared_ptr<o
while (auto networkDescription =
std::make_shared<NetworkDescription>(_compiler->compileWsIterative(targetModel, localConfig, i++))) {
- ov::Tensor tensor = make_tensor_from_vector(networkDescription->compiledNetwork);
+ ov::Tensor tensor;
+ if (networkDescription->compiledNetwork.size() > 0) {
+ tensor = make_tensor_from_vector(networkDescription->compiledNetwork);
+ } else {
+ tensor = std::move(networkDescription->compiledNetworkTensor);
+ }
GraphDescriptor graphDesc = _zeGraphExt->getGraphDescriptor(tensor.data(), tensor.get_byte_size());
NetworkMetadata networkMetadata = _zeGraphExt->getNetworkMeta(graphDesc);