From 74a5abbd66103d9b5f10f6e078abe015dd857a83 Mon Sep 17 00:00:00 2001 From: MirceaDan99 Date: Sun, 19 Jan 2025 23:21:53 +0200 Subject: [PATCH] Add unit tests --- .../include/intel_npu/common/igraph.hpp | 1 + .../intel_npu/src/common/src/igraph.cpp | 4 + .../intel_npu/tests/unit/CMakeLists.txt | 2 + .../tests/unit/npu/blob_container.cpp | 193 ++++++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 src/plugins/intel_npu/tests/unit/npu/blob_container.cpp diff --git a/src/plugins/intel_npu/src/common/include/intel_npu/common/igraph.hpp b/src/plugins/intel_npu/src/common/include/intel_npu/common/igraph.hpp index ec4d7091ac6345..4f2583c4f92419 100644 --- a/src/plugins/intel_npu/src/common/include/intel_npu/common/igraph.hpp +++ b/src/plugins/intel_npu/src/common/include/intel_npu/common/igraph.hpp @@ -36,6 +36,7 @@ class IGraph : public std::enable_shared_from_this { virtual ~IGraph() = default; const NetworkMetadata& get_metadata() const; + const BlobContainer& get_blob_container() const; ze_graph_handle_t get_handle() const; void update_network_name(std::string_view name); diff --git a/src/plugins/intel_npu/src/common/src/igraph.cpp b/src/plugins/intel_npu/src/common/src/igraph.cpp index f641813e44c0e7..5552d77d2c0fe5 100644 --- a/src/plugins/intel_npu/src/common/src/igraph.cpp +++ b/src/plugins/intel_npu/src/common/src/igraph.cpp @@ -27,6 +27,10 @@ const NetworkMetadata& IGraph::get_metadata() const { return _metadata; } +const BlobContainer& IGraph::get_blob_container() const { + return *_blobPtr; +} + ze_graph_handle_t IGraph::get_handle() const { return _handle; } diff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txt index 1097e183369fe4..b552979d8b1a51 100644 --- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt +++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt @@ -10,6 +10,7 @@ set(MANDATORY_UNIT_TESTS_LIBS "openvino::gtest" "openvino::gtest_main" "openvino::runtime" + "openvino_runtime_s" "openvino::npu_common" "openvino::npu_al" "openvino::npu_logger_utils" @@ -29,6 +30,7 @@ ov_add_test_target( ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/utils/include ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/include ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/al/include + $ OBJECT_FILES ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/src/metadata.cpp LINK_LIBRARIES diff --git a/src/plugins/intel_npu/tests/unit/npu/blob_container.cpp b/src/plugins/intel_npu/tests/unit/npu/blob_container.cpp new file mode 100644 index 00000000000000..5cf037a8eb84e0 --- /dev/null +++ b/src/plugins/intel_npu/tests/unit/npu/blob_container.cpp @@ -0,0 +1,193 @@ +// Copyright (C) 2018-2025 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include +#include + +#include "common_test_utils/file_utils.hpp" +#include "common_test_utils/subgraph_builders/2_input_subtract.hpp" +#include "common_test_utils/test_assertions.hpp" +#include "common_test_utils/test_constants.hpp" +#include "dev/core_impl.hpp" +#include "intel_npu/common/icompiled_model.hpp" +#include "openvino/runtime/core.hpp" +#include "openvino/runtime/iasync_infer_request.hpp" +#include "openvino/runtime/intel_npu/properties.hpp" +#include "openvino/runtime/properties.hpp" +#include "openvino/util/file_path.hpp" + +using namespace intel_npu; + +using BlobContainerUnitTests = ::testing::Test; + +namespace { +const char* dummyBlobHeader = "blobwillstartafterspace correctblob!"; +const char* testCacheDir = "blob_container_test_cache_dir"; +const char* testFileName = "blob_container_test.blob"; + +} // namespace + +TEST_F(BlobContainerUnitTests, isBlobContainerCorrectlyPickedForCacheEnabled) { + auto core = std::make_shared(); + core->register_compile_time_plugins(); + auto model = ov::test::utils::make_2_input_subtract(); + + { + auto compiledModel = core->compile_model(model, + ov::test::utils::DEVICE_NPU, + {ov::cache_dir(testCacheDir), ov::enable_profiling(true)}); + auto inferRequest = compiledModel->create_infer_request(); + inferRequest->infer(); + OV_ASSERT_NO_THROW(auto profilingInfo = inferRequest->get_profiling_info()); + + auto* compiledModelPtr = dynamic_cast(compiledModel._ptr.get()); + OPENVINO_ASSERT(compiledModelPtr != nullptr); + const auto& blobContainer = compiledModelPtr->get_graph()->get_blob_container(); + auto* blobContainerAlignedBufferPtr = + dynamic_cast(&blobContainer); + OPENVINO_ASSERT(blobContainerAlignedBufferPtr == nullptr, + "Blob after compilation should not be memory mapped!"); + } + + { + auto compiledModel = core->compile_model(model, + ov::test::utils::DEVICE_NPU, + {ov::cache_dir(testCacheDir), ov::enable_profiling(true)}); + auto inferRequest = compiledModel->create_infer_request(); + inferRequest->infer(); + OV_ASSERT_NO_THROW(auto profilingInfo = inferRequest->get_profiling_info()); + auto outputFile = + std::ofstream(std::filesystem::path(testCacheDir) / testFileName, std::ios::out | std::ios::binary); + OV_ASSERT_NO_THROW(compiledModel->export_model(outputFile)); + + auto* compiledModelPtr = dynamic_cast(compiledModel._ptr.get()); + OPENVINO_ASSERT(compiledModelPtr != nullptr); + const auto& blobContainer = compiledModelPtr->get_graph()->get_blob_container(); + auto* blobContainerAlignedBufferPtr = + dynamic_cast(&blobContainer); + OPENVINO_ASSERT(blobContainerAlignedBufferPtr != nullptr, "Cached blob should be memory mapped!"); + } + ov::test::utils::removeDir(testCacheDir); +} + +TEST_F(BlobContainerUnitTests, isBlobContainerCorrectlyPickedForFStream) { + auto core = std::make_shared(); + core->register_compile_time_plugins(); + auto model = ov::test::utils::make_2_input_subtract(); + + { + auto compiledModel = core->compile_model(model, ov::test::utils::DEVICE_NPU, {ov::enable_profiling(true)}); + auto inferRequest = compiledModel->create_infer_request(); + inferRequest->infer(); + OV_ASSERT_NO_THROW(auto profilingInfo = inferRequest->get_profiling_info()); + auto outputFile = std::ofstream(testFileName, std::ios::out | std::ios::binary); + OV_ASSERT_NO_THROW(compiledModel->export_model(outputFile)); + } + + { + auto inputFile = std::ifstream(testFileName, std::ios::in | std::ios::binary); + auto compiledModel = core->import_model(inputFile, ov::test::utils::DEVICE_NPU, {ov::enable_profiling(true)}); + inputFile.close(); + auto inferRequest = compiledModel->create_infer_request(); + inferRequest->infer(); + OV_ASSERT_NO_THROW(auto profilingInfo = inferRequest->get_profiling_info()); + auto outputFile = std::ofstream(testFileName, std::ios::out | std::ios::binary); + OV_ASSERT_NO_THROW(compiledModel->export_model(outputFile)); + + auto* compiledModelPtr = dynamic_cast(compiledModel._ptr.get()); + OPENVINO_ASSERT(compiledModelPtr != nullptr); + const auto& blobContainer = compiledModelPtr->get_graph()->get_blob_container(); + auto* blobContainerAlignedBufferPtr = + dynamic_cast(&blobContainer); + OPENVINO_ASSERT(blobContainerAlignedBufferPtr == nullptr, "Cannot have memory mapped blob for std::fstream!"); + } + ov::test::utils::removeFile(testFileName); +} + +TEST_F(BlobContainerUnitTests, isBlobContainerCorrectlyPickedForSStream) { + auto core = std::make_shared(); + core->register_compile_time_plugins(); + auto model = ov::test::utils::make_2_input_subtract(); + std::stringstream blobStream; + + { + auto compiledModel = core->compile_model(model, ov::test::utils::DEVICE_NPU, {ov::enable_profiling(true)}); + auto inferRequest = compiledModel->create_infer_request(); + inferRequest->infer(); + OV_ASSERT_NO_THROW(auto profilingInfo = inferRequest->get_profiling_info()); + } + + { + auto compiledModel = core->import_model(blobStream, ov::test::utils::DEVICE_NPU, {ov::enable_profiling(true)}); + blobStream = std::stringstream(); + auto inferRequest = compiledModel->create_infer_request(); + inferRequest->infer(); + OV_ASSERT_NO_THROW(auto profilingInfo = inferRequest->get_profiling_info()); + OV_ASSERT_NO_THROW(compiledModel->export_model(blobStream)); + + auto* compiledModelPtr = dynamic_cast(compiledModel._ptr.get()); + OPENVINO_ASSERT(compiledModelPtr != nullptr); + const auto& blobContainer = compiledModelPtr->get_graph()->get_blob_container(); + auto* blobContainerAlignedBufferPtr = + dynamic_cast(&blobContainer); + OPENVINO_ASSERT(blobContainerAlignedBufferPtr == nullptr, + "Cannot have memory mapped blob for std::stringstream!"); + } +} + +TEST_F(BlobContainerUnitTests, isBlobHeaderHandledCorrectly) { + auto core = std::make_shared(); + core->register_compile_time_plugins(); + auto model = ov::test::utils::make_2_input_subtract(); + std::stringstream blobStream(dummyBlobHeader); + + { + auto compiledModel = core->compile_model(model, ov::test::utils::DEVICE_NPU, {ov::enable_profiling(true)}); + auto inferRequest = compiledModel->create_infer_request(); + inferRequest->infer(); + OV_ASSERT_NO_THROW(auto profilingInfo = inferRequest->get_profiling_info()); + auto outputFile = std::ofstream(testFileName, std::ios::out | std::ios::binary); + outputFile << dummyBlobHeader; + OV_ASSERT_NO_THROW(compiledModel->export_model(outputFile)); + OV_ASSERT_NO_THROW(compiledModel->export_model(blobStream)); + } + + { + std::string parseDummyHeader; + std::string blob; + blobStream >> parseDummyHeader; + + EXPECT_THAT(parseDummyHeader, testing::HasSubstr("blobwillstartafterspace")); + auto compiledModel = + core->import_model(blobStream, ov::test::utils::DEVICE_NPU, {ov::intel_npu::defer_weights_load(true)}); + blobStream = {}; + + auto* compiledModelPtr = dynamic_cast(compiledModel._ptr.get()); + OPENVINO_ASSERT(compiledModelPtr != nullptr); + const auto& blobContainer = compiledModelPtr->get_graph()->get_blob_container(); + blob.assign(reinterpret_cast(blobContainer.get_ptr()), blobContainer.size()); + EXPECT_THAT(blob, testing::HasSubstr("correctblob!")); + } + + { + std::string parseDummyHeader; + std::string blob; + auto inputFile = std::ifstream(testFileName, std::ios::in | std::ios::binary); + blobStream >> parseDummyHeader; + + EXPECT_THAT(parseDummyHeader, testing::HasSubstr("blobwillstartafterspace")); + auto compiledModel = + core->import_model(blobStream, ov::test::utils::DEVICE_NPU, {ov::intel_npu::defer_weights_load(true)}); + + auto* compiledModelPtr = dynamic_cast(compiledModel._ptr.get()); + OPENVINO_ASSERT(compiledModelPtr != nullptr); + const auto& blobContainer = compiledModelPtr->get_graph()->get_blob_container(); + blob.assign(reinterpret_cast(blobContainer.get_ptr()), blobContainer.size()); + EXPECT_THAT(blob, testing::HasSubstr("correctblob!")); + } + + ov::test::utils::removeFile(testFileName); +} \ No newline at end of file