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][Coverity] The narrow conversion from size_t to int64. (#38646)
Nikolay Shchegolev529331f083eb
diff --git a/src/core/dev_api/openvino/core/validation_util.hpp b/src/core/dev_api/openvino/core/validation_util.hppindex 4ca5105a6e1..7d6b7e3cf79 100644--- a/src/core/dev_api/openvino/core/validation_util.hpp+++ b/src/core/dev_api/openvino/core/validation_util.hpp@@ -4,8 +4,12 @@#pragma once+#include <limits>+#include <type_traits>+#include "openvino/core/coordinate_diff.hpp"#include "openvino/core/core_visibility.hpp"+#include "openvino/core/except.hpp"#include "openvino/core/partial_shape.hpp"#include "openvino/core/shape.hpp"#include "openvino/core/strides.hpp"@@ -229,5 +233,34 @@ OPENVINO_API AxisVector try_get_normalized_axis_vector(const Tensor& tensor, con/// \return Normalized AxisSet.OPENVINO_API AxisSet try_get_normalized_axis_set(const Tensor& tensor, const Rank& rank, const Node& node);+/// \brief Narrows an integral value to another (usually smaller and/or differently signed)+/// integral type, asserting that no information is lost in the conversion.+///+/// Use this instead of a bare static_cast whenever a size/count value (or any integral value) needs+/// to be narrowed to another integral type to avoid a silent overflow / underflow.+///+/// \tparam To Target integral type to cast to.+/// \tparam From Source type of `value`.+/// \param value Value to be narrowed to `To`.+/// \return `value` casted to `To`.+template <typename To, typename From>+To narrow_cast(From value) {+ static_assert(std::is_integral_v<To> && std::is_integral_v<From>, "narrow_cast supports only integral types");++ if constexpr (std::is_signed_v<From> && std::is_unsigned_v<To>) {+ OPENVINO_ASSERT(value >= 0, "Value is negative and cannot be narrowed to an unsigned type");+ OPENVINO_ASSERT(static_cast<std::make_unsigned_t<From>>(value) <= std::numeric_limits<To>::max(),+ "Value exceeds the maximum representable value of the target type");+ } else if constexpr (std::is_unsigned_v<From> && std::is_signed_v<To>) {+ OPENVINO_ASSERT(value <= static_cast<From>(std::numeric_limits<To>::max()),+ "Value exceeds the maximum representable value of the target type");+ } else {+ OPENVINO_ASSERT(value >= static_cast<From>(std::numeric_limits<To>::min()) &&+ value <= static_cast<From>(std::numeric_limits<To>::max()),+ "Value is out of the representable range of the target type");+ }+ return static_cast<To>(value);+}+} // namespace util} // namespace ovdiff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cppindex c7d33282d74..e6a686c33c1 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp@@ -18,6 +18,7 @@#include "paged_attention_opt.hpp"#include "sdpa_base.hpp"#include "../utils/kernel_generator.hpp"+#include "openvino/core/validation_util.hpp"// clang-format onnamespace ov::intel_gpu::ocl {namespace {@@ -1480,7 +1481,7 @@ JitConstants SDPAMicroGenerator::get_jit_constants(const kernel_impl_params& parconst ov::Dimension n_keys = micro_get_seq_length(params, 1);const ov::Dimension n_queries = micro_get_seq_length(params, 0);- const ov::Dimension n_values = ov::Dimension(static_cast<ov::Dimension::value_type>(v_head_size));+ const ov::Dimension n_values = ov::Dimension(ov::util::narrow_cast<ov::Dimension::value_type>(v_head_size));bool d_full = (head_size == static_cast<size_t>(d_max));bool v_full = (head_size == static_cast<size_t>(tile_v));@@ -1812,7 +1813,7 @@ void SDPAMicroGenerator::init_microkernels(const kernel_impl_params& params,const ov::Dimension n_keys = micro_get_seq_length(params, 1);const ov::Dimension n_queries = micro_get_seq_length(params, 0);- const ov::Dimension n_values = ov::Dimension(static_cast<ov::Dimension::value_type>(v_head_size));+ const ov::Dimension n_values = ov::Dimension(ov::util::narrow_cast<ov::Dimension::value_type>(v_head_size));const auto head_num = micro_get_num_heads(params, 0);const auto batch = out_ps[0] * static_cast<ov::Dimension>(head_num);