OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Fix floating-point ranges in test random_generator (#38007)
Sungeun Kim0c8b064cbef1
diff --git a/src/plugins/intel_gpu/tests/common/random_generator.hpp b/src/plugins/intel_gpu/tests/common/random_generator.hppindex bfc95ac98a2..4847a64048c 100644--- a/src/plugins/intel_gpu/tests/common/random_generator.hpp+++ b/src/plugins/intel_gpu/tests/common/random_generator.hpp@@ -4,6 +4,7 @@#pragma once+#include <cmath>#include <cstdint>#include <iostream>#include <random>@@ -12,6 +13,8 @@#include <string_view>#include <vector>+#include "openvino/core/except.hpp"+#define GET_SUITE_NAME (std::string(::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name()) + \std::string(::testing::UnitTest::GetInstance()->current_test_info()->name()))@@ -59,9 +62,9 @@ public:}template<typename ReturnType>- ReturnType generate_random_val(int min, int max, int k = 8) {+ ReturnType generate_random_val(double min, double max, int k = 8) {// 1/k is the resolution of the floating point numbers- std::uniform_int_distribution<int> distribution(k * min, k * max);+ auto distribution = make_distribution(min, max, k);ReturnType val = static_cast<ReturnType>(distribution(this->generator));val /= k;@@ -69,9 +72,9 @@ public:}template<typename ReturnType>- std::vector<ReturnType> generate_random_1d(size_t a, int min, int max, int k = 8) {+ std::vector<ReturnType> generate_random_1d(size_t a, double min, double max, int k = 8) {// 1/k is the resolution of the floating point numbers- std::uniform_int_distribution<int> distribution(k * min, k * max);+ auto distribution = make_distribution(min, max, k);std::vector<ReturnType> v(a);for (size_t i = 0; i < a; ++i) {@@ -82,7 +85,7 @@ public:}template<typename ReturnType>- std::vector<std::vector<ReturnType>> generate_random_2d(size_t a, size_t b, int min, int max, int k = 8) {+ std::vector<std::vector<ReturnType>> generate_random_2d(size_t a, size_t b, double min, double max, int k = 8) {std::vector<std::vector<ReturnType>> v(a);for (size_t i = 0; i < a; ++i)v[i] = generate_random_1d<ReturnType>(b, min, max, k);@@ -90,7 +93,7 @@ public:}template<typename ReturnType>- std::vector<std::vector<std::vector<ReturnType>>> generate_random_3d(size_t a, size_t b, size_t c, int min, int max, int k = 8) {+ std::vector<std::vector<std::vector<ReturnType>>> generate_random_3d(size_t a, size_t b, size_t c, double min, double max, int k = 8) {std::vector<std::vector<std::vector<ReturnType>>> v(a);for (size_t i = 0; i < a; ++i)v[i] = generate_random_2d<ReturnType>(b, c, min, max, k);@@ -99,7 +102,8 @@ public:// parameters order is assumed to be bfyx or bfyxtemplate<typename ReturnType>- std::vector<std::vector<std::vector<std::vector<ReturnType>>>> generate_random_4d(size_t a, size_t b, size_t c, size_t d, int min, int max, int k = 8) {+ std::vector<std::vector<std::vector<std::vector<ReturnType>>>> generate_random_4d(size_t a, size_t b, size_t c, size_t d,+ double min, double max, int k = 8) {std::vector<std::vector<std::vector<std::vector<ReturnType>>>> v(a);for (size_t i = 0; i < a; ++i)v[i] = generate_random_3d<ReturnType>(b, c, d, min, max, k);@@ -109,7 +113,7 @@ public:// parameters order is assumed to be sbfyx for filters when split > 1template<typename ReturnType>std::vector<std::vector<std::vector<std::vector<std::vector<ReturnType>>>>> generate_random_5d(size_t a, size_t b, size_t c, size_t d, size_t e,- int min, int max, int k = 8) {+ double min, double max, int k = 8) {std::vector<std::vector<std::vector<std::vector<std::vector<ReturnType>>>>> v(a);for (size_t i = 0; i < a; ++i)v[i] = generate_random_4d<ReturnType>(b, c, d, e, min, max, k);@@ -118,7 +122,7 @@ public:template<typename ReturnType>std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<ReturnType>>>>>> generate_random_6d(size_t a, size_t b, size_t c, size_t d,- size_t e, size_t f, int min, int max, int k = 8) {+ size_t e, size_t f, double min, double max, int k = 8) {std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<ReturnType>>>>>> v(a);for (size_t i = 0; i < a; ++i)v[i] = generate_random_5d<ReturnType>(b, c, d, e, f, min, max, k);@@ -155,6 +159,28 @@ public:}private:+ // Values are drawn on a 1/k grid, so the integer bounds are k*min and k*max.+ // min/max are taken as floating point on purpose: they used to be int, which silently truncated+ // calls such as generate_random_1d<ov::float16>(n, -0.25f, 0.25f) into a constant-zero range.+ static std::uniform_int_distribution<int> make_distribution(double min, double max, int k) {+ OPENVINO_ASSERT(min <= max, "random_generator: min (", min, ") must not exceed max (", max, ")");+ OPENVINO_ASSERT(k > 0, "random_generator: resolution k must be positive, got ", k);++ if (min == max) {+ const auto val = static_cast<int>(std::lround(min * k));+ return std::uniform_int_distribution<int>(val, val);+ }++ // ceil/floor (rather than rounding) keeps every generated value inside [min, max]+ const auto lo = static_cast<int>(std::ceil(min * k));+ const auto hi = static_cast<int>(std::floor(max * k));+ OPENVINO_ASSERT(lo < hi,+ "random_generator: range [", min, ", ", max, "] holds fewer than 2 values at resolution 1/", k,+ ", so the data would be constant. Pass k >= ", static_cast<int>(std::ceil(2.0 / (max - min))));++ return std::uniform_int_distribution<int>(lo, hi);+ }+std::default_random_engine generator{DEFAULT_SEED};};diff --git a/src/plugins/intel_gpu/tests/functional/subgraph_tests/transpose_split_vlsdpa.cpp b/src/plugins/intel_gpu/tests/functional/subgraph_tests/transpose_split_vlsdpa.cppindex b84e6e931d0..63b17fe7b1f 100644--- a/src/plugins/intel_gpu/tests/functional/subgraph_tests/transpose_split_vlsdpa.cpp+++ b/src/plugins/intel_gpu/tests/functional/subgraph_tests/transpose_split_vlsdpa.cpp@@ -449,7 +449,6 @@ protected:private:std::vector<int32_t> m_cu_seqlens;- AttentionType m_attn_type;ov::TensorVector inputs_ref;};diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/bf16_onednn_ops_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/bf16_onednn_ops_gpu_test.cppindex 2a52a6758e1..391e4b220a1 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/bf16_onednn_ops_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/bf16_onednn_ops_gpu_test.cpp@@ -42,19 +42,29 @@ static std::vector<ov::bfloat16> generate_bf16_data(tests::random_generator& rg,return bf16_data;}-// Helper: compare f32 outputs with tolerance+// Helper: compare f32 outputs with tolerance.+// bf16 keeps only 8 mantissa bits, so an output element whose magnitude sits far below the tensor+// peak is dominated by cancellation noise rather than by kernel error. Gate the relative check with+// an absolute floor derived from the peak magnitude; the previous fixed 1e-5 cutoff was calibrated+// for f32 and is ~250x below the bf16 noise floor of these networks.static void compare_outputs(const std::vector<float>& ref, const std::vector<float>& actual, float tolerance) {ASSERT_EQ(ref.size(), actual.size());++ constexpr float bf16_unit_roundoff = 1.0f / 256; // 8 explicit mantissa bits+ float peak = 0.f;+ for (auto v : ref)+ peak = std::max(peak, std::abs(v));+ const float abs_tolerance = std::max(peak * bf16_unit_roundoff, 1e-5f);+for (size_t i = 0; i < ref.size(); ++i) {- if (std::abs(ref[i]) < 1e-5f) {- ASSERT_NEAR(actual[i], ref[i], tolerance)- << "Mismatch at index " << i;- } else {- float rel_err = std::abs(actual[i] - ref[i]) / std::max(std::abs(ref[i]), 1e-5f);- ASSERT_LT(rel_err, tolerance)- << "Relative error at index " << i << ": " << rel_err- << " (ref=" << ref[i] << ", actual=" << actual[i] << ")";- }+ const float abs_err = std::abs(actual[i] - ref[i]);+ if (abs_err <= abs_tolerance)+ continue;+ const float rel_err = abs_err / std::max(std::abs(ref[i]), 1e-5f);+ ASSERT_LT(rel_err, tolerance)+ << "Relative error at index " << i << ": " << rel_err+ << " (ref=" << ref[i] << ", actual=" << actual[i]+ << ", abs_err=" << abs_err << ", abs_tolerance=" << abs_tolerance << ")";}}diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/deconvolution_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/deconvolution_gpu_test.cppindex 7ad8d1a21da..ce758b72897 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/deconvolution_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/deconvolution_gpu_test.cpp@@ -3171,7 +3171,7 @@ TEST(deconvolution_gpu_onednn, input_b_fs_zyx_fsv16_output_bfzyx_stride2_nopad)tests::random_generator rg(GET_SUITE_NAME);auto input_data = rg.generate_random_1d<float>(input->count(), -1.0f, 1.0f);auto weights_data = rg.generate_random_1d<float>(weights->count(), -0.5f, 0.5f);- auto bias_data = rg.generate_random_1d<float>(biases->count(), -0.1f, 0.1f);+ auto bias_data = rg.generate_random_1d<float>(biases->count(), -0.1f, 0.1f, 256);set_values(input, input_data);set_values(weights, weights_data);