From 6b5a22a656ba976ea2bcb5152845a7857e870822 Mon Sep 17 00:00:00 2001 From: Vitaliy Urusovskij Date: Tue, 19 Sep 2023 11:27:23 +0400 Subject: [PATCH] Add `bert-base-ner` in MemLeak tests (#19817) * Add `bert-base-ner` in MemLeak tests * Fix segfault caused by `fillTensorRandom()` --- tests/lib/src/common_utils.h | 88 ++++++++++--------- .../nightly_configs/desktop_test_config.xml | 8 ++ .../precommit_configs/desktop_test_config.xml | 8 ++ .../weekly_configs/desktop_test_config.xml | 10 ++- 4 files changed, 73 insertions(+), 41 deletions(-) diff --git a/tests/lib/src/common_utils.h b/tests/lib/src/common_utils.h index ac15570f38c..a3bd72d844f 100644 --- a/tests/lib/src/common_utils.h +++ b/tests/lib/src/common_utils.h @@ -10,6 +10,13 @@ #include +template +using uniformDistribution = typename std::conditional< + std::is_floating_point::value, + std::uniform_real_distribution, + typename std::conditional::value, std::uniform_int_distribution, void>::type>::type; + + /** * @brief Determine if InferenceEngine blob means image or not (OV API 1.0) */ @@ -84,27 +91,20 @@ void fillBlobRandom(InferenceEngine::Blob::Ptr &inputBlob) { /** * @brief Fill InferenceEngine tensor with random values (OV API 2.0) */ -template -ov::Tensor fillTensorRandom(T &input) { - ov::Tensor tensor{input.get_element_type(), input.get_shape()}; - std::vector values(ov::shape_size(input.get_shape())); - - std::random_device rd; - std::mt19937 gen(rd()); - - if (std::is_floating_point::value == true) { - std::uniform_real_distribution<> distrib_f = std::uniform_real_distribution<>(0, std::numeric_limits::max()); - for (size_t i = 0; i < values.size(); ++i) - values[i] = distrib_f(gen); - } else { - std::uniform_int_distribution<> distrib_i = std::uniform_int_distribution<>(0, std::numeric_limits::max()); - for (size_t i = 0; i < values.size(); ++i) - values[i] = distrib_i(gen); +template +void fillTensorRandom(ov::Tensor& tensor, + T rand_min = std::numeric_limits::min(), + T rand_max = std::numeric_limits::max()) { + std::mt19937 gen(0); + size_t tensor_size = tensor.get_size(); + if (0 == tensor_size) { + throw std::runtime_error( + "Models with dynamic shapes aren't supported. Input tensors must have specific shapes before inference"); } - - std::memcpy(tensor.data(), values.data(), sizeof(U) * values.size()); - - return tensor; + T* data = tensor.data(); + uniformDistribution distribution(rand_min, rand_max); + for (size_t i = 0; i < tensor_size; i++) + data[i] = static_cast(distribution(gen)); } @@ -141,26 +141,34 @@ void fillBlobImInfo(InferenceEngine::Blob::Ptr &inputBlob, template void fillTensors(ov::InferRequest &infer_request, std::vector &inputs) { for (size_t i = 0; i < inputs.size(); ++i) { - ov::Tensor input_tensor; - - if (inputs[i].get_element_type() == ov::element::f32) { - input_tensor = fillTensorRandom(inputs[i]); - } else if (inputs[i].get_element_type() == ov::element::f64) { - input_tensor = fillTensorRandom(inputs[i]); - } else if (inputs[i].get_element_type() == ov::element::f16) { - input_tensor = fillTensorRandom(inputs[i]); - } else if (inputs[i].get_element_type() == ov::element::i32) { - input_tensor = fillTensorRandom(inputs[i]); - } else if (inputs[i].get_element_type() == ov::element::i64) { - input_tensor = fillTensorRandom(inputs[i]); - } else if (inputs[i].get_element_type() == ov::element::u8) { - input_tensor = fillTensorRandom(inputs[i]); - } else if (inputs[i].get_element_type() == ov::element::i8) { - input_tensor = fillTensorRandom(inputs[i]); - } else if (inputs[i].get_element_type() == ov::element::u16) { - input_tensor = fillTensorRandom(inputs[i]); - } else if (inputs[i].get_element_type() == ov::element::i16) { - input_tensor = fillTensorRandom(inputs[i]); + auto input_tensor = infer_request.get_tensor(inputs[i]); + auto type = inputs[i].get_element_type(); + if (type == ov::element::f32) { + fillTensorRandom(input_tensor); + } else if (type == ov::element::f64) { + fillTensorRandom(input_tensor); + } else if (type == ov::element::f16) { + fillTensorRandom(input_tensor); + } else if (type == ov::element::i32) { + fillTensorRandom(input_tensor); + } else if (type == ov::element::i64) { + fillTensorRandom(input_tensor); + } else if ((type == ov::element::u8) || (type == ov::element::boolean)) { + // uniform_int_distribution is not allowed in the C++17 + // standard and vs2017/19 + fillTensorRandom(input_tensor); + } else if (type == ov::element::i8) { + // uniform_int_distribution is not allowed in the C++17 standard + // and vs2017/19 + fillTensorRandom(input_tensor, + std::numeric_limits::min(), + std::numeric_limits::max()); + } else if (type == ov::element::u16) { + fillTensorRandom(input_tensor); + } else if (type == ov::element::i16) { + fillTensorRandom(input_tensor); + } else if (type == ov::element::boolean) { + fillTensorRandom(input_tensor, 0, 1); } else { throw std::logic_error( "Input precision is not supported for " + inputs[i].get_element_type().get_type_name()); diff --git a/tests/stress_tests/.automation/memleaks_tests/nightly_configs/desktop_test_config.xml b/tests/stress_tests/.automation/memleaks_tests/nightly_configs/desktop_test_config.xml index 3058ceaa5f5..cca3ec23df1 100644 --- a/tests/stress_tests/.automation/memleaks_tests/nightly_configs/desktop_test_config.xml +++ b/tests/stress_tests/.automation/memleaks_tests/nightly_configs/desktop_test_config.xml @@ -24,6 +24,14 @@ + + + + + + + + diff --git a/tests/stress_tests/.automation/memleaks_tests/precommit_configs/desktop_test_config.xml b/tests/stress_tests/.automation/memleaks_tests/precommit_configs/desktop_test_config.xml index 4909b6411f1..29d54db3f3c 100644 --- a/tests/stress_tests/.automation/memleaks_tests/precommit_configs/desktop_test_config.xml +++ b/tests/stress_tests/.automation/memleaks_tests/precommit_configs/desktop_test_config.xml @@ -8,4 +8,12 @@ + + + + + + + + diff --git a/tests/stress_tests/.automation/memleaks_tests/weekly_configs/desktop_test_config.xml b/tests/stress_tests/.automation/memleaks_tests/weekly_configs/desktop_test_config.xml index d9240cc239a..57fd6c160ca 100644 --- a/tests/stress_tests/.automation/memleaks_tests/weekly_configs/desktop_test_config.xml +++ b/tests/stress_tests/.automation/memleaks_tests/weekly_configs/desktop_test_config.xml @@ -23,7 +23,15 @@ - + + + + + + + + +