From b0300c96bc7076083353e7b85eb1a16ca55fb598 Mon Sep 17 00:00:00 2001 From: Ilya Sharikov Date: Fri, 23 Jul 2021 10:37:35 +0300 Subject: [PATCH] Common tests library for shared functionality (#6578) --- tests/lib/CMakeLists.txt | 5 + tests/lib/src/CMakeLists.txt | 15 ++ .../ie_utils.cpp => lib/src/common_utils.cpp} | 6 +- .../ie_utils.h => lib/src/common_utils.h} | 4 +- tests/stress_tests/common/CMakeLists.txt | 3 +- .../common/ie_pipelines/pipelines.cpp | 2 +- tests/stress_tests/memcheck_tests/tests.cpp | 2 +- tests/stress_tests/memleaks_tests/tests.cpp | 2 +- .../tests_pipelines_full_pipeline.cpp | 2 +- tests/time_tests/src/timetests/CMakeLists.txt | 4 +- tests/time_tests/src/timetests/common.h | 154 ------------------ .../src/timetests/timetest_infer.cpp | 2 +- .../src/timetests/timetest_infer_cache.cpp | 2 +- 13 files changed, 36 insertions(+), 167 deletions(-) create mode 100644 tests/lib/CMakeLists.txt create mode 100644 tests/lib/src/CMakeLists.txt rename tests/{stress_tests/common/ie_utils.cpp => lib/src/common_utils.cpp} (92%) rename tests/{stress_tests/common/ie_utils.h => lib/src/common_utils.h} (96%) delete mode 100644 tests/time_tests/src/timetests/common.h diff --git a/tests/lib/CMakeLists.txt b/tests/lib/CMakeLists.txt new file mode 100644 index 00000000000000..708f2355b9d903 --- /dev/null +++ b/tests/lib/CMakeLists.txt @@ -0,0 +1,5 @@ +# Copyright (C) 2018-2021 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 +# + +add_subdirectory(src) diff --git a/tests/lib/src/CMakeLists.txt b/tests/lib/src/CMakeLists.txt new file mode 100644 index 00000000000000..8a2c8ec7bb054e --- /dev/null +++ b/tests/lib/src/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright (C) 2018-2021 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 +# + +set (TARGET_NAME "tests_shared_lib") + +file (GLOB SRC *.cpp) +add_library(${TARGET_NAME} STATIC ${SRC}) + +target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +# Search OpenVINO Inference Engine installed +find_package(InferenceEngine REQUIRED) + +target_link_libraries(${TARGET_NAME} PUBLIC ${InferenceEngine_LIBRARIES}) diff --git a/tests/stress_tests/common/ie_utils.cpp b/tests/lib/src/common_utils.cpp similarity index 92% rename from tests/stress_tests/common/ie_utils.cpp rename to tests/lib/src/common_utils.cpp index cc1a1041ac9fcb..dcf8ce0baceb4c 100644 --- a/tests/stress_tests/common/ie_utils.cpp +++ b/tests/lib/src/common_utils.cpp @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 // -#include "ie_utils.h" +#include "common_utils.h" #include @@ -32,7 +32,7 @@ void fillBlobs(InferenceEngine::InferRequest inferRequest, } else if (item.second->getPrecision() == InferenceEngine::Precision::I32) { fillBlobImInfo(inputBlob, batchSize, image_size); } else { - IE_THROW() << "Input precision is not supported for image info!"; + throw std::logic_error("Input precision is not supported for image info!"); } continue; } @@ -52,7 +52,7 @@ void fillBlobs(InferenceEngine::InferRequest inferRequest, } else if (item.second->getPrecision() == InferenceEngine::Precision::I16) { fillBlobRandom(inputBlob); } else { - IE_THROW() << "Input precision is not supported for " << item.first; + throw std::logic_error("Input precision is not supported for " + item.first); } } } \ No newline at end of file diff --git a/tests/stress_tests/common/ie_utils.h b/tests/lib/src/common_utils.h similarity index 96% rename from tests/stress_tests/common/ie_utils.h rename to tests/lib/src/common_utils.h index 7520ed66066459..69f37ce7c094c6 100644 --- a/tests/stress_tests/common/ie_utils.h +++ b/tests/lib/src/common_utils.h @@ -57,7 +57,7 @@ inline std::pair getTensorHeightWidth(const InferenceEngine::Ten // Regardless of layout, dimensions are stored in fixed order return std::make_pair(dims.back(), dims.at(size - 2)); } else { - IE_THROW() << "Tensor does not have height and width dimensions"; + throw std::logic_error("Tensor does not have height and width dimensions"); } } @@ -111,4 +111,4 @@ void fillBlobImInfo(Blob::Ptr& inputBlob, */ void fillBlobs(InferenceEngine::InferRequest inferRequest, const InferenceEngine::ConstInputsDataMap& inputsInfo, - const size_t& batchSize); \ No newline at end of file + const size_t& batchSize); diff --git a/tests/stress_tests/common/CMakeLists.txt b/tests/stress_tests/common/CMakeLists.txt index 008acb5836477f..2ced4a865caa82 100644 --- a/tests/stress_tests/common/CMakeLists.txt +++ b/tests/stress_tests/common/CMakeLists.txt @@ -27,11 +27,12 @@ if(EXISTS "${OpenVINO_SOURCE_DIR}/thirdparty/gflags") add_gflags() endif() +add_subdirectory("${OpenVINO_SOURCE_DIR}/tests/lib" tests_shared_lib) target_link_libraries(${TARGET_NAME} PUBLIC IE::gtest IE::pugixml - ${InferenceEngine_LIBRARIES} gflags + tests_shared_lib PRIVATE IE::gtest_main) diff --git a/tests/stress_tests/common/ie_pipelines/pipelines.cpp b/tests/stress_tests/common/ie_pipelines/pipelines.cpp index 4e6894a7b09591..eccee2bb615be8 100644 --- a/tests/stress_tests/common/ie_pipelines/pipelines.cpp +++ b/tests/stress_tests/common/ie_pipelines/pipelines.cpp @@ -4,7 +4,7 @@ #include "pipelines.h" #include "../utils.h" -#include "../ie_utils.h" +#include "common_utils.h" #include #include diff --git a/tests/stress_tests/memcheck_tests/tests.cpp b/tests/stress_tests/memcheck_tests/tests.cpp index c8cefc9575650a..a432772dc0fc1f 100644 --- a/tests/stress_tests/memcheck_tests/tests.cpp +++ b/tests/stress_tests/memcheck_tests/tests.cpp @@ -4,7 +4,7 @@ #include "tests_utils.h" #include "../common/tests_utils.h" -#include "../common/ie_utils.h" +#include "common_utils.h" #include "../common/managers/thread_manager.h" #include "tests_pipelines/tests_pipelines.h" diff --git a/tests/stress_tests/memleaks_tests/tests.cpp b/tests/stress_tests/memleaks_tests/tests.cpp index 22c1812cb742c4..92bb7982fca8fe 100644 --- a/tests/stress_tests/memleaks_tests/tests.cpp +++ b/tests/stress_tests/memleaks_tests/tests.cpp @@ -3,7 +3,7 @@ // #include "../common/tests_utils.h" -#include "../common/ie_utils.h" +#include "common_utils.h" #include "../common/managers/thread_manager.h" #include "tests_pipelines/tests_pipelines.h" diff --git a/tests/stress_tests/unittests/tests_pipelines/tests_pipelines_full_pipeline.cpp b/tests/stress_tests/unittests/tests_pipelines/tests_pipelines_full_pipeline.cpp index 1c5dae1b860e6b..90ccaab8dd816d 100644 --- a/tests/stress_tests/unittests/tests_pipelines/tests_pipelines_full_pipeline.cpp +++ b/tests/stress_tests/unittests/tests_pipelines/tests_pipelines_full_pipeline.cpp @@ -3,7 +3,7 @@ // #include "tests_pipelines.h" -#include "../common/ie_utils.h" +#include "common_utils.h" #include diff --git a/tests/time_tests/src/timetests/CMakeLists.txt b/tests/time_tests/src/timetests/CMakeLists.txt index 44711e421c277b..c1bb4f64f56a73 100644 --- a/tests/time_tests/src/timetests/CMakeLists.txt +++ b/tests/time_tests/src/timetests/CMakeLists.txt @@ -9,11 +9,13 @@ add_custom_target(time_tests) # Test target name is source file name without extension. FILE(GLOB tests "*.cpp") +add_subdirectory("${OpenVINO_SOURCE_DIR}/tests/lib" tests_shared_lib) + foreach(test_source ${tests}) get_filename_component(test_name ${test_source} NAME_WE) add_executable(${test_name} ${test_source}) - target_link_libraries(${test_name} PRIVATE IE::inference_engine timetests_helper) + target_link_libraries(${test_name} PRIVATE tests_shared_lib timetests_helper) add_dependencies(time_tests ${test_name}) diff --git a/tests/time_tests/src/timetests/common.h b/tests/time_tests/src/timetests/common.h deleted file mode 100644 index 82ef22eeb93bf5..00000000000000 --- a/tests/time_tests/src/timetests/common.h +++ /dev/null @@ -1,154 +0,0 @@ -// Copyright (C) 2018-2021 Intel Corporation -// SPDX-License-Identifier: Apache-2.0 -// - -#pragma once - -using namespace InferenceEngine; - -/** - * @brief Determine if InferenceEngine blob means image or not - */ -template -static bool isImage(const T &blob) { - auto descriptor = blob->getTensorDesc(); - if (descriptor.getLayout() != InferenceEngine::NCHW) { - return false; - } - auto channels = descriptor.getDims()[1]; - return channels == 3; -} - - -/** - * @brief Determine if InferenceEngine blob means image information or not - */ -template -static bool isImageInfo(const T &blob) { - auto descriptor = blob->getTensorDesc(); - if (descriptor.getLayout() != InferenceEngine::NC) { - return false; - } - auto channels = descriptor.getDims()[1]; - return (channels >= 2); -} - - -/** - * @brief Return height and width from provided InferenceEngine tensor description - */ -inline std::pair getTensorHeightWidth(const InferenceEngine::TensorDesc& desc) { - const auto& layout = desc.getLayout(); - const auto& dims = desc.getDims(); - const auto& size = dims.size(); - if ((size >= 2) && - (layout == InferenceEngine::Layout::NCHW || - layout == InferenceEngine::Layout::NHWC || - layout == InferenceEngine::Layout::NCDHW || - layout == InferenceEngine::Layout::NDHWC || - layout == InferenceEngine::Layout::OIHW || - layout == InferenceEngine::Layout::GOIHW || - layout == InferenceEngine::Layout::OIDHW || - layout == InferenceEngine::Layout::GOIDHW || - layout == InferenceEngine::Layout::CHW || - layout == InferenceEngine::Layout::HW)) { - // Regardless of layout, dimensions are stored in fixed order - return std::make_pair(dims.back(), dims.at(size - 2)); - } else { - IE_THROW() << "Tensor does not have height and width dimensions"; - } -} - - -/** - * @brief Fill InferenceEngine blob with random values - */ -template -void fillBlobRandom(Blob::Ptr& inputBlob) { - MemoryBlob::Ptr minput = as(inputBlob); - // locked memory holder should be alive all time while access to its buffer happens - auto minputHolder = minput->wmap(); - - auto inputBlobData = minputHolder.as(); - for (size_t i = 0; i < inputBlob->size(); i++) { - auto rand_max = RAND_MAX; - inputBlobData[i] = (T) rand() / static_cast(rand_max) * 10; - } -} - - -/** - * @brief Fill InferenceEngine blob with image information - */ -template -void fillBlobImInfo(Blob::Ptr& inputBlob, - const size_t& batchSize, - std::pair image_size) { - MemoryBlob::Ptr minput = as(inputBlob); - // locked memory holder should be alive all time while access to its buffer happens - auto minputHolder = minput->wmap(); - - auto inputBlobData = minputHolder.as(); - for (size_t b = 0; b < batchSize; b++) { - size_t iminfoSize = inputBlob->size()/batchSize; - for (size_t i = 0; i < iminfoSize; i++) { - size_t index = b*iminfoSize + i; - if (0 == i) - inputBlobData[index] = static_cast(image_size.first); - else if (1 == i) - inputBlobData[index] = static_cast(image_size.second); - else - inputBlobData[index] = 1; - } - } -} - - -/** - * @brief Fill InferRequest blobs with random values or image information - */ -void fillBlobs(InferenceEngine::InferRequest inferRequest, - const InferenceEngine::ConstInputsDataMap& inputsInfo, - const size_t& batchSize) { - std::vector> input_image_sizes; - for (const ConstInputsDataMap::value_type& item : inputsInfo) { - if (isImage(item.second)) - input_image_sizes.push_back(getTensorHeightWidth(item.second->getTensorDesc())); - } - - for (const ConstInputsDataMap::value_type& item : inputsInfo) { - Blob::Ptr inputBlob = inferRequest.GetBlob(item.first); - if (isImageInfo(inputBlob) && (input_image_sizes.size() == 1)) { - // Fill image information - auto image_size = input_image_sizes.at(0); - if (item.second->getPrecision() == InferenceEngine::Precision::FP32) { - fillBlobImInfo(inputBlob, batchSize, image_size); - } else if (item.second->getPrecision() == InferenceEngine::Precision::FP16) { - fillBlobImInfo(inputBlob, batchSize, image_size); - } else if (item.second->getPrecision() == InferenceEngine::Precision::I32) { - fillBlobImInfo(inputBlob, batchSize, image_size); - } else { - IE_THROW() << "Input precision is not supported for image info!"; - } - continue; - } - // Fill random - if (item.second->getPrecision() == InferenceEngine::Precision::FP32) { - fillBlobRandom(inputBlob); - } else if (item.second->getPrecision() == InferenceEngine::Precision::FP16) { - fillBlobRandom(inputBlob); - } else if (item.second->getPrecision() == InferenceEngine::Precision::I32) { - fillBlobRandom(inputBlob); - } else if (item.second->getPrecision() == InferenceEngine::Precision::U8) { - fillBlobRandom(inputBlob); - } else if (item.second->getPrecision() == InferenceEngine::Precision::I8) { - fillBlobRandom(inputBlob); - } else if (item.second->getPrecision() == InferenceEngine::Precision::U16) { - fillBlobRandom(inputBlob); - } else if (item.second->getPrecision() == InferenceEngine::Precision::I16) { - fillBlobRandom(inputBlob); - } else { - IE_THROW() << "Input precision is not supported for " << item.first; - } - } -} \ No newline at end of file diff --git a/tests/time_tests/src/timetests/timetest_infer.cpp b/tests/time_tests/src/timetests/timetest_infer.cpp index 1292bcffd08794..83f47f837e1690 100644 --- a/tests/time_tests/src/timetests/timetest_infer.cpp +++ b/tests/time_tests/src/timetests/timetest_infer.cpp @@ -5,7 +5,7 @@ #include #include -#include "common.h" +#include "common_utils.h" #include "timetests_helper/timer.h" #include "timetests_helper/utils.h" using namespace InferenceEngine; diff --git a/tests/time_tests/src/timetests/timetest_infer_cache.cpp b/tests/time_tests/src/timetests/timetest_infer_cache.cpp index 95b733758b7e61..f1c657f6aa463f 100644 --- a/tests/time_tests/src/timetests/timetest_infer_cache.cpp +++ b/tests/time_tests/src/timetests/timetest_infer_cache.cpp @@ -5,7 +5,7 @@ #include #include -#include "common.h" +#include "common_utils.h" #include "timetests_helper/timer.h" #include "timetests_helper/utils.h" using namespace InferenceEngine;