Skip to content

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

[NPU] New vcl_allocator_3 to support multiple allocations and robust memory ownership transfer (#35623)

Shaojie Zhu7acedf1f76f5
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/compiler_impl.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/compiler_impl.hpp
index 9212eeffb13..9f1552ea2b9 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/compiler_impl.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/compiler_impl.hpp
@@ -19,7 +19,7 @@ namespace intel_npu {
class VCLCompilerImpl final : public std::enable_shared_from_this<VCLCompilerImpl> {
public:
- VCLCompilerImpl();
+ VCLCompilerImpl(const std::string& library_dir);
~VCLCompilerImpl();
/**
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 ff781fcf2d7..cca2c372fb8 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
@@ -4,6 +4,7 @@
#include "compiler_impl.hpp"
+#include <algorithm>
#include <limits>
#include <mutex>
@@ -11,6 +12,7 @@
#include "intel_npu/npu_private_properties.hpp"
#include "intel_npu/profiling.hpp"
#include "intel_npu/utils/utils.hpp"
+#include "intel_npu/utils/vcl/vcl_allocator.hpp"
#include "intel_npu/utils/vcl/vcl_api.hpp"
#include "model_serializer.hpp"
#include "openvino/runtime/make_tensor.hpp"
@@ -38,76 +40,6 @@ UsedVersion getUsedVclVersion(uint16_t pluginMajor, uint16_t pluginMinor, const
return {usedMajor, usedMinor};
}
-struct vcl_allocator : vcl_allocator2_t {
- vcl_allocator() : vcl_allocator2_t{allocate, deallocate} {}
-
- static uint8_t* allocate(vcl_allocator2_t* allocator, size_t size) {
- vcl_allocator* vclAllocator = static_cast<vcl_allocator*>(allocator);
- vclAllocator->m_size = intel_npu::utils::align_size_to_standard_page_size(size);
- auto allocatedPtr = reinterpret_cast<uint8_t*>(
- vclAllocator->m_allocator.allocate(vclAllocator->m_size, intel_npu::utils::STANDARD_PAGE_SIZE));
- if (allocatedPtr == nullptr) {
- OPENVINO_THROW("Failed to allocate aligned memory for allocator");
- }
- memset(allocatedPtr + size, 0, vclAllocator->m_size - size);
- vclAllocator->m_allocated = allocatedPtr;
- return allocatedPtr;
- }
-
- static void deallocate(vcl_allocator2_t* allocator, uint8_t* ptr) {
- if (ptr == nullptr) {
- OPENVINO_THROW("Pointer is nullptr in deallocate!");
- }
- vcl_allocator* vclAllocator = static_cast<vcl_allocator*>(allocator);
- vclAllocator->m_allocator.deallocate(ptr, vclAllocator->m_size, intel_npu::utils::STANDARD_PAGE_SIZE);
- }
- ov::Allocator m_allocator;
- uint8_t* m_allocated = nullptr;
- size_t m_size = 0;
-};
-
-struct vcl_allocator_2 : vcl_allocator2_t {
- vcl_allocator_2() : vcl_allocator2_t{allocate, deallocate} {}
-
- static uint8_t* allocate(vcl_allocator2_t* allocator, size_t size) {
- vcl_allocator_2* vclAllocator = static_cast<vcl_allocator_2*>(allocator);
- size_t alignedSize = intel_npu::utils::align_size_to_standard_page_size(size);
- auto allocatedPtr = reinterpret_cast<uint8_t*>(
- vclAllocator->m_allocator.allocate(alignedSize, intel_npu::utils::STANDARD_PAGE_SIZE));
- if (allocatedPtr == nullptr) {
- OPENVINO_THROW("Failed to allocate aligned memory for allocator");
- }
- memset(allocatedPtr + size, 0, alignedSize - size);
- vclAllocator->m_info.emplace_back(std::make_pair(allocatedPtr, alignedSize));
- return allocatedPtr;
- }
-
- static void deallocate(vcl_allocator2_t* allocator, uint8_t* ptr) {
- if (ptr == nullptr) {
- OPENVINO_THROW("Pointer is nullptr in deallocate!");
- }
- vcl_allocator_2* vclAllocator = static_cast<vcl_allocator_2*>(allocator);
- // 1 is the placeholder value, as size is not needed in deallocate
- vclAllocator->m_allocator.deallocate(ptr, 1, intel_npu::utils::STANDARD_PAGE_SIZE);
- }
- ov::Allocator m_allocator;
- std::vector<std::pair<uint8_t*, size_t>> m_info;
-};
-
-ov::Tensor make_tensor_from_aligned_addr(uint8_t* allocated, size_t size) {
- ov::Allocator allocator;
- auto tensor = ov::Tensor(ov::element::u8, ov::Shape{size}, allocated);
- auto impl = ov::get_tensor_impl(std::move(tensor));
- std::shared_ptr<void> ptr(allocated, [allocator = std::move(allocator), size](uint8_t* p) mutable {
- if (p == nullptr) {
- OPENVINO_THROW("Pointer is nullptr in memory deallocation of make_tensor_from_aligned_addr!");
- }
- allocator.deallocate(p, size, intel_npu::utils::STANDARD_PAGE_SIZE);
- });
- impl._so = std::move(ptr);
- return ov::make_tensor(impl);
-}
-
} // namespace
namespace intel_npu {
@@ -162,11 +94,13 @@ static inline std::string getLatestVCLLog(vcl_log_handle_t logHandle) {
} \
}
-VCLCompilerImpl::VCLCompilerImpl() : _logHandle(nullptr), _logger("VCLCompilerImpl", Logger::global().level()) {
+VCLCompilerImpl::VCLCompilerImpl(const std::string& library_dir)
+ : _logHandle(nullptr),
+ _logger("VCLCompilerImpl", Logger::global().level()) {
_logger.debug("VCLCompilerImpl constructor start");
// Load VCL library
- (void)VCLApi::getInstance();
+ (void)VCLApi::getInstance(library_dir);
// Initialize the VCL API
THROW_ON_FAIL_FOR_VCL("vclGetVersion", vclGetVersion(&_vclVersion, &_vclProfilingVersion), nullptr);
@@ -294,7 +228,7 @@ std::pair<ov::Tensor, std::optional<std::string>> VCLCompilerImpl::compile(
if (usedVersion.Major > 7 || (usedVersion.Major == 7 && usedVersion.Minor >= 7)) {
// Support only the lastest VCL api
- vcl_allocator_2 allocator;
+ auto allocator = std::make_shared<vcl_allocator_3>();
uint8_t* blob = nullptr;
size_t blobSize = 0;
uint8_t* compatibilityStringBuffer = nullptr;
@@ -302,15 +236,16 @@ std::pair<ov::Tensor, std::optional<std::string>> VCLCompilerImpl::compile(
auto result = vclAllocatedExecutableCreate3(_compilerHandle,
exeDesc,
- &allocator,
+ allocator.get(),
&blob,
&blobSize,
&compatibilityStringBuffer,
&compatibilityStringSize);
if (result != VCL_RESULT_SUCCESS) {
// Check if allocations were performed before throwing exception
- for (const auto& [buffer, size] : allocator.m_info) {
- allocator.deallocate(&allocator, buffer);
+ auto tracked_allocations = allocator->m_info;
+ for (const auto& [buffer, size] : tracked_allocations) {
+ allocator->deallocate(allocator.get(), buffer);
}
OPENVINO_THROW("Compilation failed. vclAllocatedExecutableCreate3 result: 0x",
std::hex,
@@ -323,19 +258,22 @@ std::pair<ov::Tensor, std::optional<std::string>> VCLCompilerImpl::compile(
"Failed to create VCL executable, the blob size is zero or the blob is null");
// Retrieve the real allocated size for the blob from the allocator
- size_t alignedBlobSize = 0;
- for (auto [buffer, size] : allocator.m_info) {
- if (buffer == blob) {
- alignedBlobSize = size;
- break;
- }
- }
+ auto it = std::find_if(allocator->m_info.begin(),
+ allocator->m_info.end(),
+ [blob](const std::pair<uint8_t*, size_t>& item) {
+ return item.first == blob;
+ });
+
+ OPENVINO_ASSERT(it != allocator->m_info.end(), "Failed to find the allocated blob in the allocator records");
+ size_t alignedBlobSize = it->second;
// 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", blobSize, static_cast<void*>(blob));
_logger.debug("Allocated vector size: %zu ptr: %p", alignedBlobSize, static_cast<void*>(blob));
- ov::Tensor alignedBlob = make_tensor_from_aligned_addr(blob, alignedBlobSize);
+ ov::Tensor alignedBlob = make_tensor_from_aligned_addr(blob, alignedBlobSize, allocator);
+ allocator->m_info.erase(it);
+
std::optional<std::string> compatibilityString;
// Populate compatibility string only when VCL provides a buffer.
@@ -346,7 +284,7 @@ std::pair<ov::Tensor, std::optional<std::string>> VCLCompilerImpl::compile(
std::string(reinterpret_cast<char*>(compatibilityStringBuffer), compatibilityStringSize);
_logger.debug("Compatibility string from VCL: %s", compatibilityString->c_str());
- allocator.deallocate(&allocator, compatibilityStringBuffer);
+ allocator->deallocate(allocator.get(), compatibilityStringBuffer);
}
return std::make_pair<ov::Tensor, std::optional<std::string>>(std::move(alignedBlob),
@@ -411,20 +349,23 @@ std::vector<ov::Tensor> VCLCompilerImpl::compileWsOneShot(const std::shared_ptr<
_logger.debug("compiler vcl version: %d.%d", _vclVersion.major, _vclVersion.minor);
_logger.debug("Using vclAllocatedExecutableCreateWSOneShot");
- vcl_allocator_2 allocator;
+ auto allocator = std::make_shared<vcl_allocator_3>();
THROW_ON_FAIL_FOR_VCL("vclAllocatedExecutableCreateWSOneShot",
- vclAllocatedExecutableCreateWSOneShot(_compilerHandle, exeDesc, &allocator),
+ vclAllocatedExecutableCreateWSOneShot(_compilerHandle, exeDesc, allocator.get()),
_logHandle);
- if (allocator.m_info.size() == 0) {
+ if (allocator->m_info.size() == 0) {
OPENVINO_THROW("Failed to create VCL executable, blobCount is zero");
}
std::vector<ov::Tensor> initMainTensors;
- for (auto& blob : allocator.m_info) {
- initMainTensors.emplace_back(make_tensor_from_aligned_addr(blob.first, blob.second));
+ for (const auto& blob : allocator->m_info) {
+ initMainTensors.emplace_back(make_tensor_from_aligned_addr(blob.first, blob.second, allocator));
}
+ // Clean up m_info, delegating actual physical frees strictly to the Tensor/Deleter from now on.
+ allocator->m_info.clear();
+
return initMainTensors;
}
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
index 590d044fcc6..40800c2af42 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
@@ -13,6 +13,7 @@
#include "intel_npu/utils/zero/zero_cmd_queue_pool.hpp"
#include "intel_npu/utils/zero/zero_utils.hpp"
#include "openvino/runtime/make_tensor.hpp"
+#include "openvino/util/file_util.hpp"
namespace intel_npu {
@@ -142,7 +143,8 @@ std::pair<uint64_t, std::optional<std::vector<uint64_t>>> Graph::export_blob(std
}
std::vector<ov::ProfilingInfo> Graph::process_profiling_output(const std::vector<uint8_t>& profData) const {
- auto compiler = std::make_shared<VCLCompilerImpl>();
+ auto ov_lib_path = ov::util::path_to_string(ov::util::get_ov_lib_path());
+ auto compiler = std::make_shared<VCLCompilerImpl>(ov_lib_path);
OPENVINO_ASSERT(compiler != nullptr, "Profiling post-processing requires the NPU plugin compiler library");
std::vector<uint8_t> blob(_blob->get_byte_size());
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 61322037821..7a25b614f65 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
@@ -35,7 +35,8 @@ PluginCompilerAdapter::PluginCompilerAdapter(const std::shared_ptr<ZeroInitStruc
_logger.info("Loading PLUGIN compiler");
try {
- auto vclCompilerPtr = std::make_shared<VCLCompilerImpl>();
+ auto ov_lib_path = ov::util::path_to_string(ov::util::get_ov_lib_path());
+ auto vclCompilerPtr = std::make_shared<VCLCompilerImpl>(ov_lib_path);
OPENVINO_ASSERT(vclCompilerPtr != nullptr, "VCL compiler is nullptr");
auto vclLib = vclCompilerPtr->getLinkedLibrary();
_logger.info("PLUGIN VCL compiler is loading");
diff --git a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/vcl/vcl_allocator.hpp b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/vcl/vcl_allocator.hpp
new file mode 100644
index 00000000000..4eacb1b74a1
--- /dev/null
+++ b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/vcl/vcl_allocator.hpp
@@ -0,0 +1,99 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <algorithm>
+#include <cstring>
+#include <memory>
+#include <utility>
+#include <vector>
+
+#include "intel_npu/utils/utils.hpp"
+#include "intel_npu/utils/vcl/vcl.h"
+#include "openvino/core/except.hpp"
+#include "openvino/runtime/make_tensor.hpp"
+#include "openvino/runtime/tensor.hpp"
+
+namespace intel_npu {
+
+struct vcl_allocator_3 : vcl_allocator2_t {
+ vcl_allocator_3() : vcl_allocator2_t{allocate, deallocate}, m_allocator(intel_npu::utils::STANDARD_PAGE_SIZE) {}
+
+ ~vcl_allocator_3() {
+ for (auto& item : m_info) {
+ if (item.first) {
+ m_allocator.deallocate(item.first, item.second, intel_npu::utils::STANDARD_PAGE_SIZE);
+ }
+ }
+ m_info.clear();
+ }
+
+ static uint8_t* allocate(vcl_allocator2_t* allocator, size_t size) noexcept {
+ vcl_allocator_3* vclAllocator = static_cast<vcl_allocator_3*>(allocator);
+ size_t alignedSize = intel_npu::utils::align_size_to_standard_page_size(size);
+
+ // Prevent integer wraparound on extremely large allocation sizes
+ if (alignedSize < size) {
+ return nullptr;
+ }
+
+ uint8_t* allocatedPtr = nullptr;
+ try {
+ allocatedPtr = static_cast<uint8_t*>(
+ vclAllocator->m_allocator.allocate(alignedSize, intel_npu::utils::STANDARD_PAGE_SIZE));
+
+ if (allocatedPtr == nullptr) {
+ return nullptr;
+ }
+ std::memset(allocatedPtr + size, 0, alignedSize - size);
+
+ vclAllocator->m_info.emplace_back(std::make_pair(allocatedPtr, alignedSize));
+ return allocatedPtr;
+ } catch (...) {
+ if (allocatedPtr != nullptr) {
+ vclAllocator->m_allocator.deallocate(allocatedPtr, alignedSize, intel_npu::utils::STANDARD_PAGE_SIZE);
+ }
+ return nullptr;
+ }
+ }
+
+ static void deallocate(vcl_allocator2_t* allocator, uint8_t* ptr) noexcept {
+ if (ptr == nullptr) {
+ return;
+ }
+ vcl_allocator_3* vclAllocator = static_cast<vcl_allocator_3*>(allocator);
+
+ auto it = std::find_if(vclAllocator->m_info.begin(),
+ vclAllocator->m_info.end(),
+ [ptr](const std::pair<uint8_t*, size_t>& item) {
+ return item.first == ptr;
+ });
+ if (it != vclAllocator->m_info.end()) {
+ vclAllocator->m_info.erase(it);
+ }
+
+ // 1 is the placeholder value, as size is not needed in deallocate
+ vclAllocator->m_allocator.deallocate(ptr, 1, intel_npu::utils::STANDARD_PAGE_SIZE);
+ }
+ intel_npu::utils::AlignedAllocator m_allocator;
+ std::vector<std::pair<uint8_t*, size_t>> m_info;
+};
+
+inline ov::Tensor make_tensor_from_aligned_addr(uint8_t* allocated,
+ size_t size,
+ std::shared_ptr<vcl_allocator_3> sourceAllocator) {
+ auto tensor = ov::Tensor(ov::element::u8, ov::Shape{size}, allocated);
+ auto impl = ov::get_tensor_impl(std::move(tensor));
+ std::shared_ptr<void> ptr(allocated, [sourceAllocator](uint8_t* p) noexcept {
+ if (p == nullptr) {
+ return;
+ }
+ vcl_allocator_3::deallocate(sourceAllocator.get(), p);
+ });
+ impl._so = std::move(ptr);
+ return ov::make_tensor(impl);
+}
+
+} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/vcl/vcl_api.hpp b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/vcl/vcl_api.hpp
index 20f81cb0fe6..83a480fd64c 100644
--- a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/vcl/vcl_api.hpp
+++ b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/vcl/vcl_api.hpp
@@ -42,13 +42,13 @@ namespace intel_npu {
class VCLApi {
public:
- VCLApi();
+ VCLApi(const std::string& library_dir);
VCLApi(const VCLApi& other) = delete;
VCLApi(VCLApi&& other) = delete;
void operator=(const VCLApi&) = delete;
void operator=(VCLApi&&) = delete;
- static const std::shared_ptr<VCLApi> getInstance();
+ static const std::shared_ptr<VCLApi> getInstance(const std::string& library_dir = std::string());
std::shared_ptr<void> getLibrary() const {
return lib;
}
diff --git a/src/plugins/intel_npu/src/utils/src/vcl/vcl_api.cpp b/src/plugins/intel_npu/src/utils/src/vcl/vcl_api.cpp
index 55d631bc361..b1eedc0e8a3 100644
--- a/src/plugins/intel_npu/src/utils/src/vcl/vcl_api.cpp
+++ b/src/plugins/intel_npu/src/utils/src/vcl/vcl_api.cpp
@@ -10,12 +10,12 @@
#include "openvino/util/shared_object.hpp"
namespace intel_npu {
-VCLApi::VCLApi() : _logger("VCLApi", Logger::global().level()) {
+VCLApi::VCLApi(const std::string& library_dir) : _logger("VCLApi", Logger::global().level()) {
const auto baseName = "openvino_intel_npu_compiler_loader";
try {
- const auto libpath = ov::util::make_plugin_library_name(ov::util::get_ov_lib_path(), baseName);
- _logger.debug("Try to load %s", baseName);
+ const auto libpath = ov::util::make_plugin_library_name(std::filesystem::path(library_dir), baseName);
+ _logger.debug("Try to load: %s", ov::util::path_to_string(libpath).c_str());
this->lib = ov::util::load_shared_object(libpath);
} catch (const std::runtime_error& error) {
_logger.debug("Failed to load %s: %s", baseName, error.what());
@@ -48,8 +48,29 @@ VCLApi::VCLApi() : _logger("VCLApi", Logger::global().level()) {
#undef vcl_symbol_statement
}
-const std::shared_ptr<VCLApi> VCLApi::getInstance() {
- static std::shared_ptr<VCLApi> instance = std::make_shared<VCLApi>();
+const std::shared_ptr<VCLApi> VCLApi::getInstance(const std::string& library_dir) {
+ static std::mutex mtx;
+ std::lock_guard<std::mutex> lock(mtx);
+
+ static std::string initialized_dir;
+ static std::shared_ptr<VCLApi> instance = nullptr;
+
+ if (!instance) {
+ if (library_dir.empty()) {
+ OPENVINO_THROW("VCLApi instance has not been loaded yet, and no valid path was provided to load it.");
+ }
+ initialized_dir = library_dir;
+ instance = std::make_shared<VCLApi>(library_dir);
+ } else {
+ if (!library_dir.empty() && library_dir != initialized_dir) {
+ OPENVINO_THROW("VCLApi has already been initialized with path: '",
+ initialized_dir,
+ "'. Dynamic switching to a new compiler path: '",
+ library_dir,
+ "' in the same process is not supported.");
+ }
+ }
+
return instance;
}
diff --git a/src/plugins/intel_npu/tests/functional/internal/compiler_adapter/vcl_allocator_test.cpp b/src/plugins/intel_npu/tests/functional/internal/compiler_adapter/vcl_allocator_test.cpp
new file mode 100644
index 00000000000..da95d1328a2
--- /dev/null
+++ b/src/plugins/intel_npu/tests/functional/internal/compiler_adapter/vcl_allocator_test.cpp
@@ -0,0 +1,17 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "vcl_allocator_test.hpp"
+
+#include "common/npu_test_env_cfg.hpp"
+
+namespace {
+
+using ov::test::behavior::VclAllocatorFuncTests;
+
+INSTANTIATE_TEST_SUITE_P(smoke_BehaviorTest,
+ VclAllocatorFuncTests,
+ ::testing::Values(ov::test::utils::DEVICE_NPU),
+ VclAllocatorFuncTests::getTestCaseName);
+} // namespace
diff --git a/src/plugins/intel_npu/tests/functional/internal/compiler_adapter/vcl_allocator_test.hpp b/src/plugins/intel_npu/tests/functional/internal/compiler_adapter/vcl_allocator_test.hpp
new file mode 100644
index 00000000000..006cbb22032
--- /dev/null
+++ b/src/plugins/intel_npu/tests/functional/internal/compiler_adapter/vcl_allocator_test.hpp
@@ -0,0 +1,335 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <gtest/gtest.h>
+
+#include <algorithm>
+#include <memory>
+#include <vector>
+
+#include "common/npu_test_env_cfg.hpp"
+#include "common_test_utils/file_utils.hpp"
+#include "common_test_utils/subgraph_builders/conv_pool_relu.hpp"
+#include "intel_npu/utils/utils.hpp"
+#include "intel_npu/utils/vcl/vcl_allocator.hpp"
+#include "intel_npu/utils/vcl/vcl_api.hpp"
+#include "model_serializer.hpp"
+#include "openvino/core/except.hpp"
+#include "openvino/runtime/make_tensor.hpp"
+#include "openvino/runtime/tensor.hpp"
+#include "shared_test_classes/base/ov_behavior_test_utils.hpp"
+
+namespace ov::test::behavior {
+
+class VclAllocatorFuncTests : public ::testing::TestWithParam<std::string> {
+public:
+ static std::string getTestCaseName(testing::TestParamInfo<std::string> obj) {
+ auto targetDevice = obj.param;
+ std::replace(targetDevice.begin(), targetDevice.end(), ':', '_');
+ std::ostringstream result;
+ result << "targetDevice=" << ov::test::utils::getDeviceNameTestCase(targetDevice) << "_";
+ result << "targetPlatform=" + ov::test::utils::getTestsPlatformFromEnvironmentOr(ov::test::utils::DEVICE_NPU);
+ return result.str();
+ }
+
+protected:
+ std::string targetDevice;
+ std::shared_ptr<ov::Model> model;
+ std::shared_ptr<::intel_npu::vcl_allocator_3> allocator;
+
+ void SetUp() override {
+ targetDevice = GetParam();
+ model = ov::test::utils::make_conv_pool_relu();
+ allocator = std::make_shared<::intel_npu::vcl_allocator_3>();
+
+ try {
+ std::string ov_lib_dir = ov::test::utils::getOpenvinoLibDirectory();
+ ::intel_npu::VCLApi::getInstance(ov_lib_dir);
+ } catch (const std::exception&) {
+ GTEST_SKIP() << "Couldn't load compiler library";
+ }
+ }
+
+ // Helper struct and function to reduce code duplication
+ struct CompilerSetupState {
+ std::string buildFlags;
+ ::intel_npu::SerializedIR serializedIR;
+ vcl_compiler_handle_t compiler = nullptr;
+ vcl_log_handle_t logHandle = nullptr;
+
+ CompilerSetupState() = default;
+ CompilerSetupState(const CompilerSetupState&) = delete;
+ CompilerSetupState& operator=(const CompilerSetupState&) = delete;
+ CompilerSetupState(CompilerSetupState&& other) noexcept
+ : buildFlags(std::move(other.buildFlags)),
+ serializedIR(std::move(other.serializedIR)),
+ compiler(other.compiler),
+ logHandle(other.logHandle) {
+ other.compiler = nullptr;
+ other.logHandle = nullptr;
+ }
+ CompilerSetupState& operator=(CompilerSetupState&& other) noexcept {
+ if (this != &other) {
+ release();
+ buildFlags = std::move(other.buildFlags);
+ serializedIR = std::move(other.serializedIR);
+ compiler = other.compiler;
+ logHandle = other.logHandle;
+ other.compiler = nullptr;
+ other.logHandle = nullptr;
+ }
+ return *this;
+ }
+
+ ~CompilerSetupState() {
+ release();
+ }
+
+ private:
+ void release() {
+ if (compiler != nullptr) {
+ ::intel_npu::vclCompilerDestroy(compiler);
+ compiler = nullptr;
+ }
+ }
+ };
+
+ CompilerSetupState createCompilerAndDescriptor() {
+ CompilerSetupState state;
+
+ state.buildFlags = ::intel_npu::compiler_utils::serializeIOInfo(model, true);
+
+ auto platform = ov::test::utils::getTestsPlatformFromEnvironmentOr(ov::test::utils::DEVICE_NPU);
+ if (platform.find("NPU") == 0) {
+ platform = platform.substr(3); // Remove "NPU" prefix if present
+ }
+
+ state.buildFlags +=
+ " --config NPU_PLATFORM=\"" + platform + "\" NPU_COMPILATION_MODE_PARAMS=\"optimization-level=0\"";
+
+ vcl_version_info_t vclVersion = {};
+ vcl_version_info_t vclProfilingVersion = {};
+ ::intel_npu::vclGetVersion(&vclVersion, &vclProfilingVersion);
+
+ vcl_compiler_desc_t compilerDesc = {};
+ compilerDesc.version = vclVersion;
+ compilerDesc.debugLevel =
+ static_cast<__vcl_log_level_t>(static_cast<int>(::intel_npu::Logger::global().level()) + 1);
+
+ uint32_t defaultTileCount = std::numeric_limits<uint32_t>::max();
+ if (vclVersion.major == 7 && vclVersion.minor < 6) {
+ defaultTileCount = std::numeric_limits<uint16_t>::max();
+ }
+
+ vcl_device_desc_t deviceDesc = {sizeof(vcl_device_desc_t),
+ 0x00,
+ std::numeric_limits<uint16_t>::max(),
+ defaultTileCount};
+
+ ::intel_npu::vclCompilerCreate(&compilerDesc, &deviceDesc, &state.compiler, &state.logHandle);
+
+ if (state.compiler == nullptr) {
+ ADD_FAILURE() << "vclCompilerCreate failed";
+ return state;
+ }
+
+ ze_graph_compiler_version_info_t vclVersionInfo = {0, 0};
+ vcl_compiler_properties_t compilerProp = {};
+ ::intel_npu::vclCompilerGetProperties(state.compiler, &compilerProp);
+ vclVersionInfo.major = compilerProp.version.major;
+ vclVersionInfo.minor = compilerProp.version.minor;
+
+ auto isOptionValueSupportedByCompiler = [](const std::string&, const std::optional<std::string>&) {
+ return true;
+ };
+
+ state.serializedIR =
+ ::intel_npu::compiler_utils::serializeIR(model,
+ vclVersionInfo,
+ 10,
+ ::intel_npu::MODEL_SERIALIZER_VERSION::defaultValue(),
+ isOptionValueSupportedByCompiler);
+
+ state.buildFlags += " NPU_MODEL_SERIALIZER_VERSION=\"" +
+ ::intel_npu::MODEL_SERIALIZER_VERSION::toString(state.serializedIR.serializerVersion) +
+ "\"";
+
+ return state;
+ }
+};
+
+TEST_P(VclAllocatorFuncTests, VerifyAllocation) {
+ uint8_t* blobBuffer = nullptr;
+ uint64_t blobSize = 0;
+ uint8_t* compatibilityReqBuffer = nullptr;
+ uint64_t compatibilityReqSize = 0;
+
+ auto setup = createCompilerAndDescriptor();
+ ASSERT_NE(setup.compiler, nullptr);
+
+ vcl_executable_desc_t desc = {setup.serializedIR.buffer.get(),
+ setup.serializedIR.size,
+ setup.buildFlags.c_str(),
+ setup.buildFlags.size()};
+
+ auto result = ::intel_npu::vclAllocatedExecutableCreate3(setup.compiler,
+ desc,
+ allocator.get(),
+ &blobBuffer,
+ &blobSize,
+ &compatibilityReqBuffer,
+ &compatibilityReqSize);
+
+ EXPECT_EQ(result, VCL_RESULT_SUCCESS);
+ EXPECT_NE(blobBuffer, nullptr);
+ EXPECT_NE(compatibilityReqBuffer, nullptr);
+
+ // Verify it tracked exactly these two buffers
+ // 2 allocations: blob & compatibility string
+ EXPECT_EQ(allocator->m_info.size(), 2);
+
+ auto blobIt = std::find_if(allocator->m_info.begin(),
+ allocator->m_info.end(),
+ [blobBuffer](const std::pair<uint8_t*, size_t>& item) {
+ return item.first == blobBuffer;
+ });
+ EXPECT_NE(blobIt, allocator->m_info.end());
+
+ auto compatIt = std::find_if(allocator->m_info.begin(),
+ allocator->m_info.end(),
+ [compatibilityReqBuffer](const std::pair<uint8_t*, size_t>& item) {
+ return item.first == compatibilityReqBuffer;
+ });
+ EXPECT_NE(compatIt, allocator->m_info.end());
+}
+
+TEST_P(VclAllocatorFuncTests, VerifyDeallocation) {
+ uint8_t* blobBuffer = nullptr;
+ uint64_t blobSize = 0;
+ uint8_t* compatibilityReqBuffer = nullptr;
+ uint64_t compatibilityReqSize = 0;
+
+ auto setup = createCompilerAndDescriptor();
+ ASSERT_NE(setup.compiler, nullptr);
+
+ vcl_executable_desc_t desc = {setup.serializedIR.buffer.get(),
+ setup.serializedIR.size,
+ setup.buildFlags.c_str(),
+ setup.buildFlags.size()};
+
+ auto result = ::intel_npu::vclAllocatedExecutableCreate3(setup.compiler,
+ desc,
+ allocator.get(),
+ &blobBuffer,
+ &blobSize,
+ &compatibilityReqBuffer,
+ &compatibilityReqSize);
+
+ EXPECT_EQ(result, VCL_RESULT_SUCCESS);
+
+ // Clean up compatibility string
+ if (compatibilityReqBuffer != nullptr) {
+ allocator->deallocate(allocator.get(), compatibilityReqBuffer);
+ }
+
+ // Verify compatibility buffer was erased from tracking vector
+ auto compatIt = std::find_if(allocator->m_info.begin(),
+ allocator->m_info.end(),
+ [compatibilityReqBuffer](const std::pair<uint8_t*, size_t>& item) {
+ return item.first == compatibilityReqBuffer;
+ });
+ EXPECT_EQ(compatIt, allocator->m_info.end());
+ EXPECT_EQ(allocator->m_info.size(), 1);
+}
+
+TEST_P(VclAllocatorFuncTests, VerifyOwnershipTransferToTensor) {
+ uint8_t* blobBuffer = nullptr;
+ uint64_t blobSize = 0;
+ uint8_t* compatibilityReqBuffer = nullptr;
+ uint64_t compatibilityReqSize = 0;
+
+ auto setup = createCompilerAndDescriptor();
+ ASSERT_NE(setup.compiler, nullptr);
+
+ vcl_executable_desc_t desc = {setup.serializedIR.buffer.get(),
+ setup.serializedIR.size,
+ setup.buildFlags.c_str(),
+ setup.buildFlags.size()};
+
+ auto result = ::intel_npu::vclAllocatedExecutableCreate3(setup.compiler,
+ desc,
+ allocator.get(),
+ &blobBuffer,
+ &blobSize,
+ &compatibilityReqBuffer,
+ &compatibilityReqSize);
+
+ EXPECT_EQ(result, VCL_RESULT_SUCCESS);
+
+ // Retrieve the real allocated size for the blob from the allocator (mirroring compiler_impl.cpp)
+ auto it = std::find_if(allocator->m_info.begin(),
+ allocator->m_info.end(),
+ [blobBuffer](const std::pair<uint8_t*, size_t>& item) {
+ return item.first == blobBuffer;
+ });
+ ASSERT_NE(it, allocator->m_info.end());
+ size_t alignedBlobSize = it->second;
+
+ // Wrap Blob in ov::Tensor to test ownership transfer
+ {
+ ov::Tensor tensor = ::intel_npu::make_tensor_from_aligned_addr(blobBuffer, alignedBlobSize, allocator);
+ EXPECT_EQ(tensor.data(), blobBuffer);
+ EXPECT_EQ(tensor.get_byte_size(), alignedBlobSize);
+
+ // Remove only the transferred blob, leaving any possible temporary allocations
+ // to be safely freed by ~vcl_allocator_3 (mirroring compiler_impl.cpp)
+ allocator->m_info.erase(it);
+ } // tensor goes out of scope here: custom deleter correctly handles the physical memory free
+
+ // Clean up compatibility string manually
+ if (compatibilityReqBuffer != nullptr) {
+ allocator->deallocate(allocator.get(), compatibilityReqBuffer);
+ }
+
+ // After erasing the blob and deallocating the compatibility string,
+ // the tracking info in the allocator should be empty.
+ EXPECT_EQ(allocator->m_info.size(), 0);
+}
+
+TEST_P(VclAllocatorFuncTests, VerifyOrphanMemoryCleanupOnDestruction) {
+ uint8_t* blobBuffer = nullptr;
+ uint64_t blobSize = 0;
+ uint8_t* compatibilityReqBuffer = nullptr;
+ uint64_t compatibilityReqSize = 0;
+
+ auto setup = createCompilerAndDescriptor();
+ ASSERT_NE(setup.compiler, nullptr);
+
+ vcl_executable_desc_t desc = {setup.serializedIR.buffer.get(),
+ setup.serializedIR.size,
+ setup.buildFlags.c_str(),
+ setup.buildFlags.size()};
+
+ auto result = ::intel_npu::vclAllocatedExecutableCreate3(setup.compiler,
+ desc,
+ allocator.get(),
+ &blobBuffer,
+ &blobSize,
+ &compatibilityReqBuffer,
+ &compatibilityReqSize);
+
+ EXPECT_EQ(result, VCL_RESULT_SUCCESS);
+ EXPECT_EQ(allocator->m_info.size(), 2); // 2 allocations: blob & compatibility string
+
+ // Purpose: Simulate VCL crash or early return where the memory is temporarily tracked as an orphan leak
+ // When the allocator goes out of scope and gets destroyed, it should cleanly free all tracked memory
+
+ allocator.reset();
+
+ EXPECT_EQ(allocator, nullptr);
+}
+
+} // namespace ov::test::behavior