Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GPU] VariableState::set_state() optimization (#38701)

Paul Youngsoo Ahn995fcabdfde2
diff --git a/src/plugins/intel_gpu/include/intel_gpu/graph/program.hpp b/src/plugins/intel_gpu/include/intel_gpu/graph/program.hpp
index 896427c8e0c..c646362a87d 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/graph/program.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/graph/program.hpp
@@ -17,6 +17,7 @@
#include <string>
#include <vector>
#include <memory>
+#include <mutex>
#include <map>
#include <utility>
#include <set>
@@ -30,7 +31,9 @@ class pass_manager;
class base_pass;
class program_wrapper;
class kernels_cache;
+class tensor_conversion;
class ICompilationContext;
+using tensor_conversion_key = std::pair<data_types, data_types>;
struct program {
@@ -276,6 +279,8 @@ public:
bool is_internal);
static void init_primitives();
kernels_cache& get_kernels_cache() const;
+ std::shared_ptr<tensor_conversion> get_tensor_conversion(bool create_if_not_exist = true) const;
+ void prepare_tensor_conversions(const std::vector<tensor_conversion_key>& keys);
// returns {-1, -1} if it failed to estimate by allocating given batch size
std::pair<int64_t/*const alloc*/, int64_t/*general alloc*/> get_estimated_device_mem_usage();
@@ -315,6 +320,8 @@ private:
stream::ptr _stream;
// TODO: Consider moving it to engine
std::unique_ptr<kernels_cache> _kernels_cache;
+ mutable std::shared_ptr<tensor_conversion> _tensor_conversion;
+ mutable std::mutex _tensor_conversion_mutex;
ExecutionConfig _config;
std::shared_ptr<ov::threading::IStreamsExecutor> _task_executor = nullptr;
std::list<program_node*> inputs;
diff --git a/src/plugins/intel_gpu/include/intel_gpu/graph/tensor_conversion.hpp b/src/plugins/intel_gpu/include/intel_gpu/graph/tensor_conversion.hpp
new file mode 100644
index 00000000000..14313f0fa14
--- /dev/null
+++ b/src/plugins/intel_gpu/include/intel_gpu/graph/tensor_conversion.hpp
@@ -0,0 +1,91 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "intel_gpu/runtime/kernel.hpp"
+#include "intel_gpu/runtime/engine.hpp"
+#include "intel_gpu/runtime/layout.hpp"
+#include "intel_gpu/runtime/stream.hpp"
+#include "openvino/core/strides.hpp"
+
+#include <array>
+#include <cstdint>
+#include <map>
+#include <memory>
+#include <mutex>
+#include <utility>
+#include <vector>
+
+namespace cldnn {
+
+using tensor_conversion_key = std::pair<data_types, data_types>;
+
+struct tensor_conversion_input {
+ size_t count = 1;
+ size_t source_span_bytes = 0;
+ uint64_t source_offset = 0;
+ std::array<uint64_t, 6> dimensions{1, 1, 1, 1, 1, 1};
+ std::array<uint64_t, 6> strides{0, 0, 0, 0, 0, 0};
+ bool padded = false;
+ bool transpose = false;
+};
+
+class kernels_cache;
+
+// Prepares and holds conversion kernels shared by all states of one program.
+// Serializes preparation, lookup, and kernel submission across InferRequests.
+class tensor_conversion {
+public:
+ static bool supports(tensor_conversion_key key) {
+ switch (key.first) {
+ case data_types::bf16:
+ return key.second == data_types::f16;
+ case data_types::f32:
+ return key.second == data_types::f16 || key.second == data_types::f64;
+ case data_types::f64:
+ return key.second == data_types::f32;
+ case data_types::i32:
+ return key.second == data_types::i64 || key.second == data_types::u64 || key.second == data_types::u32;
+ default:
+ return false;
+ }
+ }
+
+ // Compiles supported keys once; later calls must request the same keys.
+ void prepare(const engine& engine, kernels_cache& cache, const std::vector<tensor_conversion_key>& requested_keys);
+
+ void restore(const engine& engine, const std::vector<tensor_conversion_key>& keys,
+ const std::vector<kernel::ptr>& kernels);
+
+ bool compatible_with(const engine& engine) const;
+
+ bool has_kernel(tensor_conversion_key key) const;
+
+ std::vector<tensor_conversion_key> get_keys() const;
+
+ std::vector<kernel::ptr> get_kernels() const;
+
+ // Validate before copying input; execute() reuses the computed span and kernel indices.
+ static bool prepare_input(const layout& source_layout, const ov::Strides& strides, bool transpose,
+ tensor_conversion_input& input);
+
+ event::ptr execute(tensor_conversion_key key, memory::cptr src, memory::cptr dst, stream& stream,
+ const tensor_conversion_input& input, const std::vector<event::ptr>& dependencies = {});
+
+private:
+ void set_kernels(const std::vector<tensor_conversion_key>& keys, const std::vector<kernel::ptr>& kernels,
+ const engine& engine);
+
+ struct kernel_info {
+ kernel::ptr compiled_kernel;
+ size_t max_work_group_size;
+ };
+ std::map<tensor_conversion_key, kernel_info> _kernels;
+ void* _context = nullptr;
+ mutable std::mutex _mutex;
+ bool _prepared = false;
+};
+
+} // namespace cldnn
diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/common_utils.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/common_utils.hpp
index 0cd1e834b24..de16a472b94 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/common_utils.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/common_utils.hpp
@@ -4,18 +4,23 @@
#pragma once
+#include <memory>
#include <ostream>
#include <tuple>
+#include "intel_gpu/runtime/event.hpp"
#include "intel_gpu/runtime/layout.hpp"
#include "intel_gpu/runtime/memory.hpp"
#include "intel_gpu/runtime/shape_predictor.hpp"
#include "intel_gpu/runtime/engine.hpp"
#include "intel_gpu/runtime/device_info.hpp"
-#include "openvino/core/layout.hpp"
#include "openvino/core/node.hpp"
#include "openvino/core/type/element_type.hpp"
+namespace cldnn {
+class tensor_conversion;
+}
+
namespace ov {
namespace intel_gpu {
@@ -138,6 +143,32 @@ inline void ForceExit() {
std::_Exit(-1);
}
+struct PendingConversion {
+ PendingConversion() = default;
+ PendingConversion(const PendingConversion&) = delete;
+ PendingConversion& operator=(const PendingConversion&) = delete;
+
+ void wait();
+ // Call only after GPU completion has been confirmed.
+ void clear_after_completion() noexcept;
+
+ cldnn::memory::ptr staging;
+ cldnn::event::ptr done;
+};
+
+// Returns false before submission for unsupported inputs; true means submitted, not completed.
+// Pass an empty pending object; wait or drain and clear before reuse. No destructor waits automatically.
+// Keep destination, conversion, and engine/context alive until pending completes or the stream is drained.
+// Errors propagate; a failed stream drain retains the pending resources.
+bool convert_and_copy_async(
+ const ov::ITensor* src,
+ const cldnn::memory::ptr& dst,
+ cldnn::stream& stream,
+ const cldnn::layout& src_layout,
+ const std::shared_ptr<cldnn::tensor_conversion>& conversion,
+ PendingConversion& pending,
+ bool transpose = false);
+
void convert_and_copy(
const ov::ITensor* src,
cldnn::memory::ptr dst,
diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/variable_state.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/variable_state.hpp
index 32aa38d0238..14708103e49 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/variable_state.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/variable_state.hpp
@@ -5,6 +5,7 @@
#include "openvino/core/type/element_type.hpp"
#include "openvino/runtime/ivariable_state.hpp"
+#include "intel_gpu/plugin/common_utils.hpp"
#include "intel_gpu/runtime/layout.hpp"
#include "intel_gpu/runtime/shape_predictor.hpp"
#include "intel_gpu/runtime/memory.hpp"
@@ -12,6 +13,10 @@
#include <functional>
#include <unordered_map>
+namespace cldnn {
+class tensor_conversion;
+}
+
namespace ov::intel_gpu {
class RemoteContextImpl;
@@ -51,7 +56,10 @@ protected:
class VariableState : public VariableStateBase {
public:
- VariableState(const VariableStateInfo& info, std::shared_ptr<RemoteContextImpl> context, ShapePredictor::Ptr shape_predictor);
+ VariableState(const VariableStateInfo& info, std::shared_ptr<RemoteContextImpl> context,
+ ShapePredictor::Ptr shape_predictor,
+ std::shared_ptr<cldnn::tensor_conversion> conversion = nullptr);
+ ~VariableState() override;
using Ptr = std::shared_ptr<VariableState>;
void reset() override;
@@ -80,10 +88,16 @@ protected:
std::vector<std::weak_ptr<cldnn::memory_state::releasable_variable>> m_prim_inst;
cldnn::memory::ptr m_memory = nullptr;
bool m_transpose_required = false;
+ std::shared_ptr<cldnn::tensor_conversion> m_conversion;
size_t actual_size = 0;
const cldnn::layout m_initial_layout;
+ // Retain staging memory until GPU conversion completes.
+ // TODO: Review synchronization for concurrent state access across graph switches and external queues.
+ mutable PendingConversion m_pending;
+
+ void wait_for_gpu_conversion() const;
void update_device_buffer();
};
diff --git a/src/plugins/intel_gpu/src/graph/program.cpp b/src/plugins/intel_gpu/src/graph/program.cpp
index bfad75b34f5..5fbb1d02a4c 100644
--- a/src/plugins/intel_gpu/src/graph/program.cpp
+++ b/src/plugins/intel_gpu/src/graph/program.cpp
@@ -22,6 +22,7 @@
#include "intel_gpu/runtime/itt.hpp"
#include "intel_gpu/runtime/compilation_context.hpp"
#include "intel_gpu/graph/program.hpp"
+#include "intel_gpu/graph/tensor_conversion.hpp"
#include "allocation_order.hpp"
@@ -1958,6 +1959,9 @@ void program::save(cldnn::BinaryOutputBuffer& ob) const {
kernels_cache.add_to_cached_kernels(node->get_selected_impl()->get_kernels());
}
}
+ const auto conversion_executor = get_tensor_conversion(false);
+ if (conversion_executor)
+ kernels_cache.add_to_cached_kernels(conversion_executor->get_kernels());
ob << kernels_cache;
ob << impl_ids;
for (auto& impl_id : impl_ids) {
@@ -1968,6 +1972,15 @@ void program::save(cldnn::BinaryOutputBuffer& ob) const {
ob << get_node_ptr(impl_id)->selected_impl;
ob << get_node_ptr(impl_id)->get_selected_impl()->get_cached_kernel_ids(kernels_cache);
}
+
+ auto keys = conversion_executor ? conversion_executor->get_keys() : std::vector<tensor_conversion_key>{};
+ ob << keys.size();
+ for (const auto& key : keys) {
+ ob << static_cast<int32_t>(key.first);
+ ob << static_cast<int32_t>(key.second);
+ }
+ ob << (conversion_executor ? kernels_cache.get_cached_kernel_ids(conversion_executor->get_kernels())
+ : std::vector<std::string>{});
}
ob << optimized_out.size();
@@ -2165,6 +2178,30 @@ void program::load(cldnn::BinaryInputBuffer& ib,
ib >> cached_kernel_ids;
p_node.selected_impl->init_by_cached_kernels(get_kernels_cache(), cached_kernel_ids);
}
+
+ size_t conversion_count;
+ ib >> conversion_count;
+ std::vector<tensor_conversion_key> conversion_keys;
+ conversion_keys.reserve(conversion_count);
+ for (size_t i = 0; i < conversion_count; ++i) {
+ int32_t src_type, dst_type;
+ ib >> src_type;
+ ib >> dst_type;
+ conversion_keys.emplace_back(static_cast<data_types>(src_type), static_cast<data_types>(dst_type));
+ }
+ std::vector<std::string> conversion_ids;
+ ib >> conversion_ids;
+ OPENVINO_ASSERT(conversion_ids.size() == conversion_keys.size(), "[GPU] State conversion cache is incomplete");
+ std::vector<kernel::ptr> conversion_kernels;
+ conversion_kernels.reserve(conversion_ids.size());
+ for (const auto& id : conversion_ids)
+ conversion_kernels.push_back(kernels_cache.get_kernel_from_cached_kernels(id));
+ if (!conversion_keys.empty()) {
+ get_tensor_conversion()->restore(_engine, conversion_keys, conversion_kernels);
+ } else {
+ std::lock_guard<std::mutex> lock(_tensor_conversion_mutex);
+ _tensor_conversion.reset();
+ }
}
size_t optimized_out_size;
diff --git a/src/plugins/intel_gpu/src/graph/sources.cmake b/src/plugins/intel_gpu/src/graph/sources.cmake
index 14163985551..b157ab265df 100644
--- a/src/plugins/intel_gpu/src/graph/sources.cmake
+++ b/src/plugins/intel_gpu/src/graph/sources.cmake
@@ -385,6 +385,7 @@ set(GPU_GRAPH_SOURCES
${CMAKE_CURRENT_LIST_DIR}/stft.cpp
${CMAKE_CURRENT_LIST_DIR}/strided_slice.cpp
${CMAKE_CURRENT_LIST_DIR}/swiglu.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/tensor_conversion.cpp
${CMAKE_CURRENT_LIST_DIR}/tile.cpp
${CMAKE_CURRENT_LIST_DIR}/topology.cpp
${CMAKE_CURRENT_LIST_DIR}/unique.cpp
diff --git a/src/plugins/intel_gpu/src/graph/tensor_conversion.cpp b/src/plugins/intel_gpu/src/graph/tensor_conversion.cpp
new file mode 100644
index 00000000000..720b5223545
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/tensor_conversion.cpp
@@ -0,0 +1,303 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "intel_gpu/graph/program.hpp"
+#include "intel_gpu/graph/tensor_conversion.hpp"
+
+#include "impls/ocl/kernels_cache.hpp"
+#include "intel_gpu/runtime/kernel_args.hpp"
+#include "intel_gpu/runtime/utils.hpp"
+#include "openvino/core/except.hpp"
+
+#ifdef OV_GPU_WITH_OCL_RT
+#include "runtime/ocl/ocl_device.hpp"
+#include "runtime/ocl/ocl_kernel.hpp"
+#endif
+
+#include <algorithm>
+#include <array>
+#include <cstdint>
+#include <set>
+#include <string>
+#include <utility>
+#include <vector>
+
+namespace cldnn {
+namespace {
+
+std::shared_ptr<kernel_string> make_source(tensor_conversion_key key) {
+ auto source = std::make_shared<kernel_string>();
+ source->entry_point = "state_convert_" + std::to_string(static_cast<int>(key.first)) + "_" +
+ std::to_string(static_cast<int>(key.second));
+
+ const char* input_type = nullptr;
+ const char* output_type = nullptr;
+ std::string value;
+ switch (key.first) {
+ case data_types::bf16:
+ input_type = "ushort";
+ output_type = "half";
+ value = "convert_half_rte(as_float(((uint)input[src_index]) << 16))";
+ break;
+ case data_types::f32:
+ input_type = "float";
+ if (key.second == data_types::f16) {
+ output_type = "half";
+ value = "convert_half_rte(input[src_index])";
+ } else {
+ output_type = "double";
+ value = "(double)input[src_index]";
+ }
+ break;
+ case data_types::f64:
+ input_type = "double";
+ output_type = "float";
+ value = "convert_float_rte(input[src_index])";
+ break;
+ case data_types::i32:
+ input_type = "int";
+ output_type = key.second == data_types::i64 ? "long" :
+ key.second == data_types::u64 ? "ulong" : "uint";
+ value = "(" + std::string(output_type) + ")input[src_index]";
+ break;
+ default:
+ OPENVINO_THROW("[GPU] Unsupported state conversion type");
+ }
+
+ if (key.first == data_types::f64 || key.second == data_types::f64)
+ source->str += "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n";
+ if (key.second == data_types::f16)
+ source->str += "#pragma OPENCL EXTENSION cl_khr_fp16 : enable\n";
+ source->str += "__kernel void " + source->entry_point + "(__global const " + input_type + "* input, "
+ "__global " + output_type + "* output, ulong count, ulong src_count, ulong dst_count,\n"
+ " ulong src_offset, ulong padded, ulong transpose,\n"
+ " ulong d0, ulong d1, ulong d2, ulong d3, ulong d4, ulong d5,\n"
+ " ulong s0, ulong s1, ulong s2, ulong s3, ulong s4, ulong s5) {\n"
+ " size_t index = get_global_id(0);\n"
+ " if (index >= count) return;\n"
+ " size_t src_index = src_offset + index;\n"
+ " if (padded) {\n"
+ " const ulong dims[6] = {d0, d1, d2, d3, d4, d5};\n"
+ " const ulong strides[6] = {s0, s1, s2, s3, s4, s5};\n"
+ " size_t remaining = index;\n"
+ " src_index = src_offset;\n"
+ " for (int axis = 5; axis >= 0; --axis) {\n"
+ " src_index += (remaining % dims[axis]) * strides[axis];\n"
+ " remaining /= dims[axis];\n"
+ " }\n"
+ " }\n"
+ " size_t dst_index = index;\n"
+ " if (transpose) {\n"
+ " size_t plane = d4 * d5;\n"
+ " dst_index = (index / plane) * plane + (index % d5) * d4 + (index / d5) % d4;\n"
+ " }\n"
+ " // Skip reads and writes outside the supplied buffers.\n"
+ " if (src_index >= src_count || dst_index >= dst_count || dst_index >= count) return;\n"
+ " output[dst_index] = " + value + ";\n"
+ "}\n";
+ return source;
+}
+
+} // namespace
+
+void tensor_conversion::set_kernels(const std::vector<tensor_conversion_key>& keys,
+ const std::vector<kernel::ptr>& kernels, const engine& engine) {
+ OPENVINO_ASSERT(keys.size() == kernels.size(), "[GPU] State conversion kernel count mismatch");
+ OPENVINO_ASSERT(engine.runtime_type() == runtime_types::ocl, "[GPU] State conversion requires OpenCL");
+#ifdef OV_GPU_WITH_OCL_RT
+ const auto device = engine.get_device();
+ const auto& ocl_device = downcast<ocl::ocl_device>(*device).get_device();
+ const auto device_limit = static_cast<size_t>(engine.get_device_info().max_work_group_size);
+ std::map<tensor_conversion_key, kernel_info> prepared_kernels;
+ for (size_t i = 0; i < keys.size(); ++i) {
+ OPENVINO_ASSERT(supports(keys[i]) && kernels[i], "[GPU] Invalid state conversion kernel");
+ const auto& handle = downcast<ocl::ocl_kernel>(*kernels[i]).get_handle();
+ // Query once for both compiled and imported kernels, never during set_state().
+ const auto kernel_limit = handle.getWorkGroupInfo<CL_KERNEL_WORK_GROUP_SIZE>(ocl_device);
+ const auto limit = std::min(device_limit, kernel_limit);
+ OPENVINO_ASSERT(limit > 0, "[GPU] Invalid state conversion work-group limit");
+ OPENVINO_ASSERT(prepared_kernels.emplace(keys[i], kernel_info{kernels[i], limit}).second,
+ "[GPU] Duplicate state conversion kernel");
+ }
+ _kernels = std::move(prepared_kernels);
+ _context = engine.get_user_context(runtime_types::ocl);
+#else
+ OPENVINO_THROW("[GPU] State conversion requires OpenCL");
+#endif
+}
+
+bool tensor_conversion::compatible_with(const engine& engine) const {
+ std::lock_guard<std::mutex> lock(_mutex);
+ return _context != nullptr && engine.runtime_type() == runtime_types::ocl &&
+ _context == engine.get_user_context(runtime_types::ocl);
+}
+
+bool tensor_conversion::has_kernel(tensor_conversion_key key) const {
+ std::lock_guard<std::mutex> lock(_mutex);
+ return _kernels.find(key) != _kernels.end();
+}
+
+std::vector<tensor_conversion_key> tensor_conversion::get_keys() const {
+ std::lock_guard<std::mutex> lock(_mutex);
+ std::vector<tensor_conversion_key> keys;
+ for (const auto& entry : _kernels)
+ keys.push_back(entry.first);
+ return keys;
+}
+
+std::vector<kernel::ptr> tensor_conversion::get_kernels() const {
+ std::lock_guard<std::mutex> lock(_mutex);
+ std::vector<kernel::ptr> kernels;
+ for (const auto& entry : _kernels)
+ kernels.push_back(entry.second.compiled_kernel);
+ return kernels;
+}
+
+bool tensor_conversion::prepare_input(const layout& source_layout, const ov::Strides& strides,
+ bool transpose, tensor_conversion_input& input) {
+ const auto shape = source_layout.get_shape();
+ if (shape.empty() || shape.size() > 6 || strides.size() != shape.size() ||
+ !format::is_default_format(source_layout.format) || (transpose && shape.size() < 2))
+ return false;
+
+ const auto physical_shape = source_layout.get_padded_dims();
+ const auto element_size = data_type_traits::size_of(source_layout.data_type);
+ tensor_conversion_input prepared;
+ prepared.source_span_bytes = element_size;
+ prepared.padded = static_cast<bool>(source_layout.data_padding);
+ prepared.transpose = transpose;
+ size_t pitch_bytes = element_size;
+ for (size_t i = shape.size(); i-- > 0;) {
+ if (shape[i] == 0 || strides[i] == 0 || (shape[i] > 1 && strides[i] != pitch_bytes) ||
+ source_layout.data_padding._lower_size[i] < 0 || source_layout.data_padding._upper_size[i] < 0 ||
+ physical_shape[i] <= 0)
+ return false;
+ const auto lower = static_cast<size_t>(source_layout.data_padding._lower_size[i]);
+ prepared.source_span_bytes += lower * pitch_bytes;
+ prepared.source_offset += lower * (pitch_bytes / element_size);
+ prepared.source_span_bytes += (shape[i] - 1) * pitch_bytes;
+ // Right-align dimensions so axes 4 and 5 are always the last two tensor axes.
+ const size_t axis = 6 - shape.size() + i;
+ prepared.dimensions[axis] = shape[i];
+ prepared.strides[axis] = pitch_bytes / element_size;
+ prepared.count *= shape[i];
+ pitch_bytes *= static_cast<size_t>(physical_shape[i]);
+ }
+ input = prepared;
+ return true;
+}
+
+event::ptr tensor_conversion::execute(tensor_conversion_key key, memory::cptr src, memory::cptr dst,
+ stream& stream, const tensor_conversion_input& input,
+ const std::vector<event::ptr>& dependencies) {
+ std::lock_guard<std::mutex> lock(_mutex);
+ auto it = _kernels.find(key);
+ OPENVINO_ASSERT(it != _kernels.end(), "[GPU] State conversion kernel was not prepared");
+ const auto count = input.count;
+ if (count == 0)
+ return nullptr;
+ const auto dst_count = dst->size() / data_type_traits::size_of(key.second);
+ OPENVINO_ASSERT(count <= dst_count, "[GPU] State conversion destination memory is too small");
+
+ kernel_arguments_desc desc;
+ const auto local_size = std::min(count, it->second.max_work_group_size);
+ OPENVINO_ASSERT(local_size > 0, "[GPU] Invalid state conversion work-group limit");
+ desc.workGroups.global = {((count - 1) / local_size + 1) * local_size, 1, 1};
+ desc.workGroups.local = {local_size, 1, 1};
+ desc.arguments = {{argument_desc::Types::INPUT, 0},
+ {argument_desc::Types::OUTPUT, 0}};
+ const auto add_scalar = [&](uint64_t value) {
+ desc.arguments.push_back({argument_desc::Types::SCALAR, static_cast<uint32_t>(desc.scalars.size())});
+ scalar_desc scalar{};
+ scalar.t = scalar_desc::Types::UINT64;
+ scalar.v.u64 = value;
+ desc.scalars.push_back(scalar);
+ };
+ add_scalar(count);
+ add_scalar(src->size() / data_type_traits::size_of(key.first));
+ add_scalar(dst_count);
+ add_scalar(input.source_offset);
+ add_scalar(static_cast<uint64_t>(input.padded));
+ add_scalar(static_cast<uint64_t>(input.transpose));
+ for (auto dimension : input.dimensions)
+ add_scalar(dimension);
+ for (auto stride : input.strides)
+ add_scalar(stride);
+ desc.layerID = "state_conversion";
+
+ kernel_arguments_data args;
+ args.inputs.push_back(std::move(src));
+ args.outputs.push_back(std::move(dst));
+ args.scalars = &desc.scalars;
+
+ stream.set_arguments(*it->second.compiled_kernel, desc, args);
+ return stream.enqueue_kernel(*it->second.compiled_kernel, desc, args, dependencies, true);
+}
+
+void tensor_conversion::prepare(const engine& engine, kernels_cache& cache,
+ const std::vector<tensor_conversion_key>& requested_keys) {
+ std::lock_guard<std::mutex> lock(_mutex);
+
+ std::set<tensor_conversion_key> unique_keys;
+ if (engine.runtime_type() == runtime_types::ocl) {
+ const auto& device_info = engine.get_device_info();
+ for (const auto& key : requested_keys) {
+ const bool needs_fp64 = key.first == data_types::f64 || key.second == data_types::f64;
+ const bool needs_fp16 = key.first == data_types::f16 || key.second == data_types::f16;
+ if (key.first != key.second && tensor_conversion::supports(key) &&
+ (!needs_fp64 || device_info.supports_fp64) && (!needs_fp16 || device_info.supports_fp16))
+ unique_keys.insert(key);
+ }
+ }
+ std::vector<tensor_conversion_key> keys(unique_keys.begin(), unique_keys.end());
+
+ if (_prepared) {
+ std::vector<tensor_conversion_key> existing_keys;
+ for (const auto& entry : _kernels)
+ existing_keys.push_back(entry.first);
+ OPENVINO_ASSERT(keys == existing_keys, "[GPU] State conversion kernels do not match the network");
+ return;
+ }
+
+ if (!keys.empty()) {
+ std::vector<std::shared_ptr<kernel_string>> sources;
+ sources.reserve(keys.size());
+ for (const auto& key : keys)
+ sources.push_back(make_source(key));
+
+ kernel_impl_params params;
+ auto compiled = cache.compile(params, sources);
+ OPENVINO_ASSERT(compiled.size() == 1, "[GPU] State conversion compilation failed");
+ std::vector<kernel::ptr> kernels(keys.size());
+ for (const auto& entry : compiled.begin()->second) {
+ OPENVINO_ASSERT(entry.second < kernels.size(), "[GPU] Invalid state conversion kernel index");
+ kernels[entry.second] = entry.first;
+ }
+ set_kernels(keys, kernels, engine);
+ }
+ _prepared = true;
+}
+
+void tensor_conversion::restore(const engine& engine, const std::vector<tensor_conversion_key>& keys,
+ const std::vector<kernel::ptr>& kernels) {
+ std::lock_guard<std::mutex> lock(_mutex);
+ if (!keys.empty())
+ set_kernels(keys, kernels, engine);
+ _prepared = true;
+}
+
+std::shared_ptr<tensor_conversion> program::get_tensor_conversion(bool create_if_not_exist) const {
+ std::lock_guard<std::mutex> lock(_tensor_conversion_mutex);
+ if (!_tensor_conversion && create_if_not_exist)
+ _tensor_conversion = std::make_shared<tensor_conversion>();
+ return _tensor_conversion;
+}
+
+void program::prepare_tensor_conversions(const std::vector<tensor_conversion_key>& requested_keys) {
+ if (requested_keys.empty())
+ return;
+ get_tensor_conversion()->prepare(_engine, *_kernels_cache, requested_keys);
+}
+
+} // namespace cldnn
diff --git a/src/plugins/intel_gpu/src/plugin/common_utils.cpp b/src/plugins/intel_gpu/src/plugin/common_utils.cpp
index eab3babaac2..bcf2a56e0db 100644
--- a/src/plugins/intel_gpu/src/plugin/common_utils.cpp
+++ b/src/plugins/intel_gpu/src/plugin/common_utils.cpp
@@ -4,16 +4,21 @@
#include "intel_gpu/plugin/common_utils.hpp"
#include "intel_gpu/plugin/remote_tensor.hpp"
+#include "intel_gpu/plugin/usm_host_tensor.hpp"
+#include "intel_gpu/graph/tensor_conversion.hpp"
#include "intel_gpu/runtime/layout.hpp"
#include "intel_gpu/runtime/memory.hpp"
#include "intel_gpu/runtime/memory_caps.hpp"
+#include "intel_gpu/runtime/debug_configuration.hpp"
#include "openvino/core/type/element_type.hpp"
+#include "openvino/reference/convert.hpp"
#include "openvino/runtime/tensor.hpp"
#include "openvino/runtime/make_tensor.hpp"
#include "openvino/op/util/op_types.hpp"
#include <algorithm>
+#include <cstring>
#include <memory>
namespace {
@@ -46,12 +51,18 @@ static inline void get_linear_offset_params(layout& layout, tensor& start_pos, t
}
template <typename src_t, typename dst_t>
-static inline void convert_and_copy_padded_source_fast(const src_t* src, dst_t* dst, layout& layout) {
+static inline void convert_and_copy_padded_source_fast(const src_t* src, dst_t* dst, layout& layout, bool transpose) {
tensor axes_start_pos, axes_end_pos;
vector<int64_t> padded_sizes, axes_map;
get_linear_offset_params(layout, axes_start_pos, axes_end_pos, padded_sizes, axes_map);
const size_t map_len = axes_map.size();
+ const auto shape = layout.get_shape();
+ OPENVINO_ASSERT(!transpose || shape.size() >= 2, "[GPU] Transposed state requires at least two axes");
+ const size_t columns = transpose ? shape.back() : 1;
+ const size_t rows = transpose ? shape[shape.size() - 2] : 1;
+ const size_t plane = rows * columns;
+ size_t index = 0;
for (int64_t b = axes_start_pos.batch[0]; b < axes_end_pos.batch[0]; b++) {
for (int64_t f = axes_start_pos.feature[0]; f < axes_end_pos.feature[0]; f++) {
@@ -66,7 +77,10 @@ static inline void convert_and_copy_padded_source_fast(const src_t* src, dst_t*
offset = offset * padded_sizes[i] + element_sizes[axes_map[i]];
}
- *dst++ = static_cast<dst_t>(src[offset]);
+ const size_t dst_index = transpose ? (index / plane) * plane +
+ (index % columns) * rows + (index / columns) % rows : index;
+ dst[dst_index] = static_cast<dst_t>(src[offset]);
+ ++index;
}
}
}
@@ -76,19 +90,28 @@ static inline void convert_and_copy_padded_source_fast(const src_t* src, dst_t*
}
template <typename src_t, typename dst_t>
-void convert_and_copy_padded_source(const src_t* src, dst_t* dst, layout& layout) {
+void convert_and_copy_padded_source(const src_t* src, dst_t* dst, layout& layout, bool transpose) {
if (format::is_default_format(layout.get_format())) {
- return convert_and_copy_padded_source_fast(src, dst, layout);
+ return convert_and_copy_padded_source_fast(src, dst, layout, transpose);
}
cldnn::tensor size = layout.get_tensor();
+ const auto shape = layout.get_shape();
+ OPENVINO_ASSERT(!transpose || shape.size() >= 2, "[GPU] Transposed state requires at least two axes");
+ const size_t columns = transpose ? shape.back() : 1;
+ const size_t rows = transpose ? shape[shape.size() - 2] : 1;
+ const size_t plane = rows * columns;
+ size_t index = 0;
for (int64_t b = 0; b < size.batch[0]; b++) {
for (int64_t f = 0; f < size.feature[0]; f++) {
for (int64_t w = 0; w < size.spatial[3]; w++) {
for (int64_t z = 0; z < size.spatial[2]; z++) {
for (int64_t y = 0; y < size.spatial[1]; y++) {
for (int64_t x = 0; x < size.spatial[0]; x++) {
- *dst++ = static_cast<dst_t>(src[layout.get_linear_offset(cldnn::tensor(b, f, x, y, z, w))]);
+ const size_t dst_index = transpose ? (index / plane) * plane +
+ (index % columns) * rows + (index / columns) % rows : index;
+ dst[dst_index] = static_cast<dst_t>(src[layout.get_linear_offset(cldnn::tensor(b, f, x, y, z, w))]);
+ ++index;
}
}
}
@@ -100,9 +123,7 @@ void convert_and_copy_padded_source(const src_t* src, dst_t* dst, layout& layout
template <typename src_t, typename dst_t>
void convert_and_copy_no_pad(const src_t* src, dst_t* dst, size_t size) {
OPENVINO_ASSERT(src && dst, "[GPU] Src or Dst ptr is null");
- for (size_t i = 0; i < size; i++) {
- dst[i] = static_cast<dst_t>(src[i]);
- }
+ ov::reference::convert(src, dst, size);
}
template <typename src_t, typename dst_t>
@@ -137,7 +158,7 @@ void convert_and_copy(const void* src_ptr, ov::element::Type src_et, void* dst_p
#define CASE(s_et, d_et, s_type, d_type) \
if (src_et == s_et && dst_et == d_et) { \
if (static_cast<bool>(layout.data_padding)) { \
- return convert_and_copy_padded_source(static_cast<const s_type*>(src_ptr), static_cast<d_type*>(dst_ptr), layout); \
+ return convert_and_copy_padded_source(static_cast<const s_type*>(src_ptr), static_cast<d_type*>(dst_ptr), layout, transpose); \
} \
if (transpose) { \
return convert_and_copy_transposed(static_cast<const s_type*>(src_ptr), static_cast<d_type*>(dst_ptr), layout.get_shape()); \
@@ -233,12 +254,117 @@ bool data_types_are_supported(const ov::Node* node) {
return true;
}
+void PendingConversion::wait() {
+ // An absent event does not prove submitted work completed.
+ if (!done)
+ return;
+ done->wait();
+ clear_after_completion();
+}
+
+void PendingConversion::clear_after_completion() noexcept {
+ done.reset();
+ staging.reset();
+}
+
+bool convert_and_copy_async(const ov::ITensor* src, const cldnn::memory::ptr& dst,
+ cldnn::stream& stream, const cldnn::layout& src_layout,
+ const std::shared_ptr<cldnn::tensor_conversion>& conversion,
+ PendingConversion& pending, bool transpose) {
+ if (!conversion ||
+ dst->get_layout().data_padding ||
+ !cldnn::format::is_default_format(src_layout.format) ||
+ src_layout.format != dst->get_layout().format ||
+ src->get_shape() != dst->get_layout().get_shape())
+ return false;
+
+ const cldnn::tensor_conversion_key key{static_cast<ov::element::Type_t>(src->get_element_type()), dst->get_layout().data_type};
+ if (!conversion->has_kernel(key))
+ return false;
+
+ cldnn::tensor_conversion_input input;
+ if (!cldnn::tensor_conversion::prepare_input(src_layout, src->get_strides(), transpose, input))
+ return false;
+ const auto source_span_bytes = input.source_span_bytes;
+
+ auto* dst_engine = dst->get_engine();
+ if (!dst_engine || dst_engine->runtime_type() != cldnn::runtime_types::ocl ||
+ (dst->get_allocation_type() != cldnn::allocation_type::usm_device &&
+ dst->get_allocation_type() != cldnn::allocation_type::cl_mem) ||
+ !conversion->compatible_with(*dst_engine))
+ return false;
+
+ if (!dst_engine->supports_allocation(cldnn::allocation_type::usm_host))
+ return false;
+
+ const void* staging_source = nullptr;
+ cldnn::memory::ptr input_memory;
+ if (const auto* host = dynamic_cast<const USMHostTensor*>(src)) {
+ input_memory = host->get_impl()->get_original_memory();
+ if (!input_memory)
+ return false;
+ } else if (const auto* remote = dynamic_cast<const RemoteTensorImpl*>(src)) {
+ input_memory = remote->get_original_memory();
+ if (!input_memory)
+ return false;
+ } else if (dynamic_cast<const ov::IRemoteTensor*>(src)) {
+ return false;
+ } else {
+ staging_source = src->data();
+ if (staging_source == nullptr)
+ return false;
+ }
+
+ if (input_memory) {
+ auto* src_engine = input_memory->get_engine();
+ if ((input_memory->get_allocation_type() != cldnn::allocation_type::usm_host &&
+ input_memory->get_allocation_type() != cldnn::allocation_type::usm_device &&
+ input_memory->get_allocation_type() != cldnn::allocation_type::cl_mem) ||
+ !src_engine || src_engine->runtime_type() != cldnn::runtime_types::ocl ||
+ src_engine->get_user_context(cldnn::runtime_types::ocl) !=
+ dst_engine->get_user_context(cldnn::runtime_types::ocl))
+ return false;
+ }
+
+ // Preserve row gaps without reading beyond the view's last element.
+ const cldnn::layout span_layout(ov::Shape{source_span_bytes / src->get_element_type().size()},
+ src->get_element_type(), cldnn::format::bfyx);
+ pending.staging = dst_engine->allocate_memory(span_layout, cldnn::allocation_type::usm_host, false);
+ try {
+ if (!input_memory || input_memory->get_allocation_type() == cldnn::allocation_type::usm_host) {
+ if (input_memory) {
+ OPENVINO_ASSERT(source_span_bytes <= input_memory->size(), "[GPU] State source span exceeds input memory");
+ staging_source = input_memory->buffer_ptr();
+ }
+ // Snapshot host input before returning; GPU conversion reads only staging.
+ std::memcpy(pending.staging->buffer_ptr(), staging_source, source_span_bytes);
+ pending.done = conversion->execute(key, pending.staging, dst, stream, input);
+ } else {
+ auto copy_event = pending.staging->copy_from(stream, *input_memory, 0, 0, source_span_bytes, false);
+ OPENVINO_ASSERT(copy_event, "[GPU] State staging copy did not return an event");
+ pending.done = conversion->execute(key, pending.staging, dst, stream, input, {copy_event});
+ copy_event->wait();
+ }
+ OPENVINO_ASSERT(pending.done, "[GPU] State conversion did not return a completion event");
+ } catch (...) {
+ // Drain submitted work before source or staging memory can be released.
+ try {
+ stream.finish();
+ pending.clear_after_completion();
+ } catch (...) {
+ // Keep staging if GPU completion cannot be confirmed.
+ }
+ throw;
+ }
+ return true;
+}
+
void convert_and_copy(const ov::ITensor* src, cldnn::memory::ptr dst, cldnn::stream& stream, const cldnn::layout& src_layout, bool transpose) {
const bool blocking = true;
auto src_et = src->get_element_type();
auto dst_et = dst->get_layout().data_type;
- if (dst_et == src_et && !transpose) {
+ if (dst_et == src_et && !transpose && !src_layout.data_padding) {
if (const auto* remote = dynamic_cast<const ov::intel_gpu::RemoteTensorImpl*>(src)) {
auto mem = remote->get_original_memory();
dst->copy_from(stream, *mem, blocking);
@@ -248,7 +374,12 @@ void convert_and_copy(const ov::ITensor* src, cldnn::memory::ptr dst, cldnn::str
return;
}
+ OPENVINO_ASSERT(dynamic_cast<const ov::intel_gpu::RemoteTensorImpl*>(src) == nullptr,
+ "[GPU] CPU conversion cannot access a remote source tensor");
size_t size = ov::shape_size(src->get_shape());
+ GPU_DEBUG_INFO << "[convert_and_copy] CPU dtype=" << src_et << "->" << ov::element::Type(dst_et)
+ << " count=" << size << " padded=" << static_cast<bool>(src_layout.data_padding)
+ << " transpose=" << transpose << std::endl;
ov::Tensor tmp_tensor(dst_et, src->get_shape());
::convert_and_copy(src->data(), src_et, tmp_tensor.data(), dst_et, size, src_layout, transpose);
dst->copy_from(stream, tmp_tensor.data(), blocking);
diff --git a/src/plugins/intel_gpu/src/plugin/graph.cpp b/src/plugins/intel_gpu/src/plugin/graph.cpp
index 5aefcd206c8..e7e43467cae 100644
--- a/src/plugins/intel_gpu/src/plugin/graph.cpp
+++ b/src/plugins/intel_gpu/src/plugin/graph.cpp
@@ -26,6 +26,7 @@
#include "intel_gpu/primitives/dynamic_quantize.hpp"
#include "intel_gpu/primitives/grouped_matmul.hpp"
#include "intel_gpu/primitives/fully_connected.hpp"
+#include "intel_gpu/primitives/kv_cache.hpp"
#include "dynamic_quantize_inst.h"
#include "grouped_matmul_inst.h"
#include "fully_connected_inst.h"
@@ -227,6 +228,24 @@ void Graph::build(std::shared_ptr<cldnn::program> program) {
m_network = std::make_shared<cldnn::network>(program, m_stream_id);
}
+ std::vector<cldnn::tensor_conversion_key> conversion_keys;
+ for (const auto& variable : m_network->get_variables_info()) {
+ const auto& info = variable.second;
+ const bool special_kv_state = std::any_of(info.m_primitives.begin(), info.m_primitives.end(),
+ [](const cldnn::primitive* primitive) {
+ const auto* kv = dynamic_cast<const cldnn::kv_cache*>(primitive);
+ return kv != nullptr && (kv->compressed || kv->indirect);
+ });
+ if (special_kv_state)
+ continue;
+
+ const auto source_type = info.m_user_specified_type == ov::element::dynamic
+ ? info.m_layout.data_type
+ : static_cast<ov::element::Type_t>(info.m_user_specified_type);
+ conversion_keys.emplace_back(source_type, info.m_layout.data_type);
+ }
+ program->prepare_tensor_conversions(conversion_keys);
+
std::string dry_run_path = GPU_DEBUG_VALUE_OR(m_config.get_dry_run_path(), "");
std::string dump_graphs_path = GPU_DEBUG_VALUE_OR(m_config.get_dump_graphs_path(), "");
GPU_DEBUG_IF(!dry_run_path.empty()) {
diff --git a/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp b/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp
index 82a378460e6..4136a4ab184 100644
--- a/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp
+++ b/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp
@@ -999,7 +999,10 @@ void SyncInferRequest::allocate_states() {
} else if (indirect_kv_cache) {
m_variables.emplace(vi.first, std::make_shared<VariableStateIndirectKVCache>(vi.second, m_context, m_shape_predictor, beam_axis, concat_axis));
} else {
- m_variables.emplace(vi.first, std::make_shared<VariableState>(vi.second, m_context, m_shape_predictor));
+ m_variables.emplace(vi.first, std::make_shared<VariableState>(vi.second,
+ m_context,
+ m_shape_predictor,
+ network->get_program()->get_tensor_conversion()));
}
}
}
diff --git a/src/plugins/intel_gpu/src/plugin/variable_state.cpp b/src/plugins/intel_gpu/src/plugin/variable_state.cpp
index f08cd33f421..ac5892d295c 100644
--- a/src/plugins/intel_gpu/src/plugin/variable_state.cpp
+++ b/src/plugins/intel_gpu/src/plugin/variable_state.cpp
@@ -6,27 +6,49 @@
#include "openvino/runtime/make_tensor.hpp"
#include "intel_gpu/plugin/remote_context.hpp"
#include "intel_gpu/plugin/common_utils.hpp"
-#include "intel_gpu/plugin/remote_tensor.hpp"
#include "intel_gpu/plugin/variable_state.hpp"
#include "intel_gpu/runtime/memory_caps.hpp"
+#include "intel_gpu/runtime/stream.hpp"
#include "intel_gpu/runtime/layout.hpp"
#include "intel_gpu/runtime/debug_configuration.hpp"
#include <memory>
+#include <limits>
+#include <utility>
namespace ov::intel_gpu {
-VariableState::VariableState(const VariableStateInfo& info, RemoteContextImpl::Ptr context, std::shared_ptr<cldnn::ShapePredictor> shape_predictor)
+VariableState::VariableState(const VariableStateInfo& info, RemoteContextImpl::Ptr context,
+ std::shared_ptr<cldnn::ShapePredictor> shape_predictor,
+ std::shared_ptr<cldnn::tensor_conversion> conversion)
: VariableStateBase{info.m_id, context}
, m_layout(info.m_layout)
, m_user_specified_type(info.m_user_specified_type)
, m_shape_predictor(shape_predictor)
, m_prim_inst(info.m_release_variable_inst)
, m_transpose_required(info.transpose_required)
+ , m_conversion(std::move(conversion))
, m_initial_layout(info.m_layout) {
update_device_buffer();
}
+VariableState::~VariableState() {
+ try {
+ wait_for_gpu_conversion();
+ } catch (...) {
+ try {
+ m_context->get_engine().get_service_stream().finish();
+ m_pending.clear_after_completion();
+ } catch (...) {
+ }
+ }
+}
+
+void VariableState::wait_for_gpu_conversion() const {
+ m_pending.wait();
+}
+
void VariableState::reset() {
+ wait_for_gpu_conversion();
m_is_set = false;
set_layout(m_initial_layout);
for (auto& user : m_prim_inst) {
@@ -37,6 +59,7 @@ void VariableState::reset() {
}
cldnn::memory::ptr VariableState::get_memory() const {
+ wait_for_gpu_conversion();
return m_memory;
}
@@ -45,6 +68,7 @@ const cldnn::layout& VariableState::get_layout() const {
}
void VariableState::set_memory(const cldnn::memory::ptr& new_mem, const cldnn::layout& actual_layout) {
+ wait_for_gpu_conversion();
GPU_DEBUG_TRACE_DETAIL << m_name << " : Update memory (Ptr : " << new_mem->buffer_ptr()
<< ", layout : " << actual_layout.to_short_string() << ")" << std::endl;
m_memory = new_mem;
@@ -54,6 +78,7 @@ void VariableState::set_memory(const cldnn::memory::ptr& new_mem, const cldnn::l
}
void VariableState::set_layout(const cldnn::layout& new_layout) {
+ wait_for_gpu_conversion();
if (m_layout == new_layout) {
return;
}
@@ -63,6 +88,7 @@ void VariableState::set_layout(const cldnn::layout& new_layout) {
}
void VariableState::set_state(const ov::SoPtr<ov::ITensor>& state) {
+ wait_for_gpu_conversion();
auto src_shape = state->get_shape();
size_t src_rank = src_shape.size();
cldnn::padding::DynamicDimsMask dynamic_pad_dims;
@@ -73,6 +99,7 @@ void VariableState::set_state(const ov::SoPtr<ov::ITensor>& state) {
std::vector<ov::Dimension::value_type>(src_rank, 0),
dynamic_pad_dims);
auto src_stride = state->get_strides();
+ OPENVINO_ASSERT(src_stride.size() == src_rank, "[GPU] State source stride rank mismatch");
for (size_t i = 0; i < src_rank; ++i) {
src_stride[i] /= state->get_element_type().bitwidth() / 8;
}
@@ -84,27 +111,42 @@ void VariableState::set_state(const ov::SoPtr<ov::ITensor>& state) {
return;
}
- // check whether the src tensor is padded
- std::vector<size_t> src_stride_no_pad(src_rank, 1);
+ // Derive upper padding from adjacent strides to preserve gaps in tensor views.
std::vector<ov::Dimension::value_type> upper_pad(src_rank, 0);
std::vector<ov::Dimension::value_type> lower_pad(src_rank, 0);
- for (int32_t i = static_cast<int32_t>(src_stride.size()) - 1; i >= 0; --i) {
- if (i <= static_cast<int32_t>(src_stride.size()) - 2) {
- src_stride_no_pad[i] = src_stride_no_pad[i + 1] * src_shape[i + 1];
- }
- if (src_stride[i] != src_stride_no_pad[i]) {
- OPENVINO_ASSERT(src_stride[i] > src_stride_no_pad[i]);
- size_t padded_size = src_stride[i] / src_stride[i + 1];
- size_t non_padded_size = src_stride_no_pad[i] / src_stride_no_pad[i + 1];
- ov::Dimension::value_type pad_dim = i + 1;
- upper_pad[pad_dim] = static_cast<ov::Dimension::value_type>(padded_size) - static_cast<ov::Dimension::value_type>(non_padded_size);
- }
+ OPENVINO_ASSERT(src_stride.empty() || src_stride.back() == 1,
+ "[GPU] State source innermost stride must be one element");
+ for (size_t i = src_rank; i-- > 1;) {
+ OPENVINO_ASSERT(src_stride[i] > 0 && src_stride[i - 1] % src_stride[i] == 0,
+ "[GPU] State source strides cannot be represented as padding");
+ const size_t padded_size = src_stride[i - 1] / src_stride[i];
+ OPENVINO_ASSERT(padded_size >= src_shape[i], "[GPU] Overlapping state source strides are unsupported");
+ OPENVINO_ASSERT(padded_size - src_shape[i] <=
+ static_cast<size_t>(std::numeric_limits<ov::Dimension::value_type>::max()),
+ "[GPU] State source padding is too large");
+ upper_pad[i] = static_cast<ov::Dimension::value_type>(padded_size - src_shape[i]);
}
cldnn::padding src_padd = cldnn::padding(lower_pad, upper_pad);
auto src_fmt = cldnn::format::get_default_format(src_rank);
auto src_layout = cldnn::layout(ov::PartialShape(src_shape), state->get_element_type(), src_fmt, src_padd);
- convert_and_copy(state._ptr.get(), m_memory, m_context->get_engine().get_service_stream(), src_layout, m_transpose_required);
+ auto& stream = m_context->get_engine().get_service_stream();
+ bool gpu_conversion_submitted = false;
+ if (state->get_element_type() == get_user_specified_type() &&
+ state->get_element_type() != m_layout.data_type) {
+ gpu_conversion_submitted = convert_and_copy_async(state._ptr.get(), m_memory, stream, src_layout, m_conversion,
+ m_pending, m_transpose_required);
+ }
+ if (gpu_conversion_submitted) {
+ GPU_DEBUG_INFO << "[state_conversion] GPU state=" << m_name
+ << " dtype=" << state->get_element_type() << "->" << ov::element::Type(m_layout.data_type)
+ << " count=" << ov::shape_size(src_shape) << " padded=" << static_cast<bool>(src_padd)
+ << " transpose=" << m_transpose_required << std::endl;
+ set();
+ return;
+ }
+ m_pending.clear_after_completion();
+ convert_and_copy(state._ptr.get(), m_memory, stream, src_layout, m_transpose_required);
set();
}
@@ -136,6 +178,7 @@ ov::element::Type VariableState::get_user_specified_type() const {
}
ov::SoPtr<ov::ITensor> VariableState::get_state() const {
+ wait_for_gpu_conversion();
if (m_memory == nullptr) {
const auto& pshape = m_layout.get_partial_shape();
const auto& shape = get_tensor_shape(pshape);
diff --git a/src/plugins/intel_gpu/tests/unit/module_tests/convert_and_copy_test.cpp b/src/plugins/intel_gpu/tests/unit/module_tests/convert_and_copy_test.cpp
index 2a449779e3e..6062af0aa30 100644
--- a/src/plugins/intel_gpu/tests/unit/module_tests/convert_and_copy_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/module_tests/convert_and_copy_test.cpp
@@ -4,15 +4,259 @@
#include "test_utils.h"
+#include "intel_gpu/graph/network.hpp"
+#include "intel_gpu/graph/program.hpp"
+#include "intel_gpu/graph/serialization/binary_buffer.hpp"
+#include "intel_gpu/graph/tensor_conversion.hpp"
#include "intel_gpu/plugin/common_utils.hpp"
#include "intel_gpu/plugin/remote_context.hpp"
#include "intel_gpu/plugin/remote_tensor.hpp"
#include "intel_gpu/plugin/usm_host_tensor.hpp"
+#include "intel_gpu/plugin/variable_state.hpp"
+#include "intel_gpu/primitives/assign.hpp"
+#include "intel_gpu/primitives/input_layout.hpp"
+#include "intel_gpu/primitives/read_value.hpp"
+#include "openvino/reference/convert.hpp"
+#include "openvino/runtime/make_tensor.hpp"
+#include "openvino/util/memory.hpp"
+
+#include <algorithm>
+#include <cmath>
+#include <cstddef>
+#include <future>
+#include <limits>
+#include <memory>
+#include <sstream>
+#include <string>
+#include <type_traits>
+#include <utility>
+#include <vector>
using namespace cldnn;
using namespace ov::intel_gpu;
using namespace ::tests;
+namespace {
+
+class ControlledConversionEvent : public cldnn::event {
+public:
+ bool fail_wait = false;
+ size_t wait_calls = 0;
+
+protected:
+ void wait_impl() override {
+ ++wait_calls;
+ if (fail_wait)
+ OPENVINO_THROW("Injected conversion wait failure");
+ }
+ void set_impl() override {}
+ bool is_set_impl() override { return _set; }
+};
+
+class GpuOnlyHostTensor : public USMHostTensor {
+public:
+ using USMHostTensor::USMHostTensor;
+
+ const void* data() const override {
+ OPENVINO_THROW("CPU fallback was used for the GPU conversion test");
+ }
+};
+
+class PaddedHostTensor : public USMHostTensor {
+public:
+ // Expose a 2x2 view over a 2x3 allocation to exercise row padding.
+ explicit PaddedHostTensor(const std::shared_ptr<RemoteContextImpl>& context)
+ : USMHostTensor(context, ov::element::f32, {2, 3}) {}
+
+ const ov::Shape& get_shape() const override { return m_shape; }
+ const ov::Strides& get_strides() const override { return m_strides; }
+
+private:
+ ov::Shape m_shape{2, 2};
+ ov::Strides m_strides{3 * sizeof(float), sizeof(float)};
+};
+
+class GpuOnlyHostView : public ov::ITensor {
+public:
+ explicit GpuOnlyHostView(std::shared_ptr<ov::ITensor> tensor) : m_tensor(std::move(tensor)) {}
+
+ void set_shape(ov::Shape shape) override { m_tensor->set_shape(std::move(shape)); }
+ const ov::element::Type& get_element_type() const override { return m_tensor->get_element_type(); }
+ const ov::Shape& get_shape() const override { return m_tensor->get_shape(); }
+ const ov::Strides& get_strides() const override { return m_tensor->get_strides(); }
+ void* data() override { return m_tensor->data(); }
+ void* data_rw() override { return m_tensor->data_rw(); }
+ void* data(const ov::element::Type& type) override { return m_tensor->data(type); }
+ void* data_rw(const ov::element::Type& type) override { return m_tensor->data_rw(type); }
+
+ const void* data() const override {
+ OPENVINO_ASSERT(!m_const_data_accessed, "CPU fallback was used for the host conversion test");
+ m_const_data_accessed = true;
+ return static_cast<const ov::ITensor&>(*m_tensor).data();
+ }
+
+ const void* data(const ov::element::Type& type) const override {
+ return static_cast<const ov::ITensor&>(*m_tensor).data(type);
+ }
+
+private:
+ std::shared_ptr<ov::ITensor> m_tensor;
+ mutable bool m_const_data_accessed = false;
+};
+
+cldnn::network::ptr make_state_network(const ov::Shape& shape,
+ const ov::element::Type& src_type,
+ const ov::element::Type& dst_type) {
+ auto& engine = get_test_engine();
+ const layout state_layout{shape, dst_type, format::bfyx};
+ topology state_topology;
+ state_topology.add(input_layout("input", state_layout));
+ state_topology.add(read_value{"read_value", {input_info("input")}, "state", {state_layout}, src_type});
+ state_topology.add(assign{"assign", {input_info("read_value")}, "state", state_layout});
+ // Prepare conversion kernels before export; the import test performs its own round trip.
+ return get_network(engine, state_topology, get_test_default_config(engine), get_test_stream_ptr(), false);
+}
+
+template <typename Src, typename Dst>
+void check_converted_values(const cldnn::memory::ptr& memory,
+ const std::vector<Src>& values,
+ cldnn::stream& stream) {
+ std::vector<Dst> expected(values.size());
+ ov::reference::convert(values.data(), expected.data(), values.size());
+ cldnn::mem_lock<Dst, mem_lock_type::read> actual(memory, stream);
+ for (size_t i = 0; i < expected.size(); ++i) {
+ if (std::isnan(static_cast<float>(expected[i])))
+ ASSERT_TRUE(std::isnan(static_cast<float>(actual[i]))) << "element " << i;
+ else
+ ASSERT_EQ(actual[i], expected[i]) << "element " << i;
+ }
+}
+
+// Isolate this overload call to avoid an MSVC 19.42 C1001 in the fallback test.
+void convert_host_tensor(const ov::ITensor* source,
+ const cldnn::memory::ptr& destination,
+ cldnn::stream& stream,
+ const cldnn::layout& source_layout) {
+ ov::intel_gpu::convert_and_copy(source, destination, stream, source_layout, false);
+}
+
+template <typename Src, typename Dst>
+void check_async_values(const std::shared_ptr<tensor_conversion>& conversion,
+ const std::shared_ptr<ov::ITensor>& source,
+ const std::vector<Src>& values,
+ const cldnn::memory::ptr& destination,
+ cldnn::stream& stream,
+ const cldnn::memory::ptr& source_memory = nullptr) {
+ if (source_memory)
+ source_memory->copy_from(stream, values.data(), true);
+ else
+ std::copy(values.begin(), values.end(), static_cast<Src*>(source->data()));
+
+ const auto source_layout = make_layout(source->get_element_type(), source->get_shape());
+ PendingConversion pending;
+ ASSERT_TRUE(convert_and_copy_async(source.get(), destination, stream, source_layout, conversion, pending));
+ ASSERT_NO_THROW(pending.wait());
+ check_converted_values<Src, Dst>(destination, values, stream);
+}
+
+template <typename Src, typename Dst>
+void check_state_values(VariableState& variable,
+ const std::shared_ptr<ov::ITensor>& source,
+ const std::vector<Src>& values,
+ cldnn::stream& stream,
+ const cldnn::memory::ptr& source_memory = nullptr) {
+ if (source_memory)
+ source_memory->copy_from(stream, values.data(), true);
+ else
+ std::copy(values.begin(), values.end(), static_cast<Src*>(source->data()));
+
+ ASSERT_NO_THROW(variable.set_state(ov::SoPtr<ov::ITensor>(source)));
+ ASSERT_NE(variable.get_memory(), nullptr);
+ ASSERT_EQ(variable.get_layout().get_shape(), source->get_shape());
+ check_converted_values<Src, Dst>(variable.get_memory(), values, stream);
+}
+
+} // namespace
+
+TEST(convert_and_copy_test, pending_conversion_wait_releases_resources) {
+ static_assert(!std::is_copy_constructible_v<PendingConversion>);
+ static_assert(!std::is_copy_assignable_v<PendingConversion>);
+ static_assert(!std::is_move_constructible_v<PendingConversion>);
+ static_assert(!std::is_move_assignable_v<PendingConversion>);
+
+ PendingConversion pending;
+ pending.staging = get_test_engine().allocate_memory(layout{ov::Shape{1}, data_types::f32, format::bfyx});
+ std::weak_ptr<cldnn::memory> staging = pending.staging;
+ auto done = std::make_shared<ControlledConversionEvent>();
+ pending.done = done;
+
+ ASSERT_NO_THROW(pending.wait());
+ EXPECT_EQ(done->wait_calls, 1);
+ EXPECT_EQ(pending.done, nullptr);
+ EXPECT_EQ(pending.staging, nullptr);
+ EXPECT_TRUE(staging.expired());
+ EXPECT_NO_THROW(pending.wait());
+ EXPECT_EQ(done->wait_calls, 1);
+}
+
+TEST(convert_and_copy_test, pending_conversion_failed_wait_keeps_resources_until_retry) {
+ PendingConversion pending;
+ pending.staging = get_test_engine().allocate_memory(layout{ov::Shape{1}, data_types::f32, format::bfyx});
+ std::weak_ptr<cldnn::memory> staging = pending.staging;
+ auto done = std::make_shared<ControlledConversionEvent>();
+ done->fail_wait = true;
+ pending.done = done;
+
+ EXPECT_THROW(pending.wait(), ov::Exception);
+ EXPECT_EQ(done->wait_calls, 1);
+ EXPECT_EQ(pending.done, done);
+ EXPECT_FALSE(staging.expired());
+
+ done->fail_wait = false;
+ ASSERT_NO_THROW(pending.wait());
+ EXPECT_EQ(done->wait_calls, 2);
+ EXPECT_EQ(pending.done, nullptr);
+ EXPECT_TRUE(staging.expired());
+}
+
+TEST(convert_and_copy_test, pending_conversion_explicit_cleanup_does_not_wait) {
+ PendingConversion pending;
+ pending.staging = get_test_engine().allocate_memory(layout{ov::Shape{1}, data_types::f32, format::bfyx});
+ std::weak_ptr<cldnn::memory> staging = pending.staging;
+ auto done = std::make_shared<ControlledConversionEvent>();
+ done->fail_wait = true;
+ pending.done = done;
+
+ ASSERT_NO_THROW(get_test_stream().finish());
+ pending.clear_after_completion();
+ EXPECT_EQ(done->wait_calls, 0);
+ EXPECT_EQ(pending.done, nullptr);
+ EXPECT_EQ(pending.staging, nullptr);
+ EXPECT_TRUE(staging.expired());
+}
+
+TEST(convert_and_copy_test, pending_conversion_without_event_keeps_staging_until_explicit_cleanup) {
+ PendingConversion pending;
+ pending.staging = get_test_engine().allocate_memory(layout{ov::Shape{1}, data_types::f32, format::bfyx});
+ std::weak_ptr<cldnn::memory> staging = pending.staging;
+
+ ASSERT_NO_THROW(pending.wait());
+ EXPECT_FALSE(staging.expired());
+ ASSERT_NO_THROW(get_test_stream().finish());
+ pending.clear_after_completion();
+ EXPECT_TRUE(staging.expired());
+}
+
+TEST(convert_and_copy_test, pending_conversion_destruction_does_not_wait) {
+ auto done = std::make_shared<ControlledConversionEvent>();
+ done->fail_wait = true;
+ {
+ PendingConversion pending;
+ pending.done = done;
+ }
+ EXPECT_EQ(done->wait_calls, 0);
+}
+
// IRemoteTensor::data() always throws, so if the fast-path `return` is ever dropped again and
// execution falls through to the fallback path, this call throws instead of silently passing.
TEST(convert_and_copy_test, remote_tensor_fast_path_does_not_fall_through) {
@@ -56,3 +300,606 @@ TEST(convert_and_copy_test, string_host_tensor_has_live_elements) {
EXPECT_EQ(tensor->data<std::string>()[1], "short");
tensor = {};
}
+
+TEST(convert_and_copy_gpu_test, async_util_snapshots_host_inputs_without_variable_state_or_program) {
+ auto& engine = get_test_engine();
+ if (engine.runtime_type() != runtime_types::ocl || !engine.get_device_info().supports_fp16 ||
+ !engine.supports_allocation(allocation_type::usm_host) ||
+ !engine.supports_allocation(allocation_type::usm_device))
+ GTEST_SKIP() << "OpenCL FP16 and USM host/device allocations are required";
+
+ const ov::Shape shape{2, 3};
+ const layout src_layout{shape, data_types::f32, format::bfyx};
+ const layout dst_layout{shape, data_types::f16, format::bfyx};
+ auto network = make_state_network(shape, ov::element::f32, ov::element::f16);
+ auto program = network->get_program();
+ program->prepare_tensor_conversions({{data_types::f32, data_types::f16}});
+ auto conversion = program->get_tensor_conversion();
+ std::weak_ptr<cldnn::program> program_weak = program;
+ network.reset();
+ program.reset();
+ ASSERT_TRUE(program_weak.expired());
+
+ auto context = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});
+ auto& stream = context->get_engine().get_service_stream();
+ auto dst = context->get_engine().allocate_memory(dst_layout, allocation_type::usm_device);
+ const std::vector<float> values{1.f, -2.f, 3.f, -4.f, 5.f, -6.f};
+ PendingConversion pending;
+ for (const bool usm_host : {false, true}) {
+ std::shared_ptr<ov::ITensor> source;
+ if (usm_host)
+ source = std::make_shared<GpuOnlyHostTensor>(context, ov::element::f32, shape);
+ else
+ source = ov::make_tensor(ov::element::f32, shape);
+ std::copy(values.begin(), values.end(), static_cast<float*>(source->data()));
+ std::weak_ptr<ov::ITensor> source_weak = source;
+
+ ASSERT_TRUE(convert_and_copy_async(source.get(), dst, stream, src_layout, conversion, pending));
+ std::weak_ptr<cldnn::memory> staging_weak = pending.staging;
+ EXPECT_NE(pending.done, nullptr);
+ EXPECT_FALSE(staging_weak.expired());
+ std::fill_n(static_cast<float*>(source->data()), values.size(), 0.f);
+ source.reset();
+ EXPECT_TRUE(source_weak.expired());
+
+ ASSERT_NO_THROW(pending.wait());
+ EXPECT_TRUE(staging_weak.expired());
+ EXPECT_EQ(pending.done, nullptr);
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> actual(dst, stream);
+ for (size_t i = 0; i < values.size(); ++i)
+ EXPECT_EQ(actual[i], ov::float16(values[i]));
+ }
+}
+
+TEST(convert_and_copy_gpu_test, async_util_converts_remote_sources_without_variable_state) {
+ auto& engine = get_test_engine();
+ if (engine.runtime_type() != runtime_types::ocl || !engine.get_device_info().supports_fp16 ||
+ !engine.supports_allocation(allocation_type::usm_host) ||
+ !engine.supports_allocation(allocation_type::usm_device))
+ GTEST_SKIP() << "OpenCL FP16 and USM host/device allocations are required";
+
+ const ov::Shape shape{2, 3};
+ const layout src_layout{shape, data_types::f32, format::bfyx};
+ const layout dst_layout{shape, data_types::f16, format::bfyx};
+ auto network = make_state_network(shape, ov::element::f32, ov::element::f16);
+ auto program = network->get_program();
+ program->prepare_tensor_conversions({{data_types::f32, data_types::f16}});
+ auto conversion = program->get_tensor_conversion();
+ auto context = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});
+ auto& stream = context->get_engine().get_service_stream();
+ const std::vector<float> values{1.f, -2.f, 3.f, -4.f, 5.f, -6.f};
+ const std::vector<float> zeros(values.size(), 0.f);
+
+ for (const auto tensor_type : {TensorType::BT_USM_DEVICE_INTERNAL, TensorType::BT_BUF_INTERNAL}) {
+ auto source = std::make_shared<RemoteTensorImpl>(context, shape, ov::element::f32, tensor_type);
+ auto source_memory = source->get_original_memory();
+ const auto allocation = tensor_type == TensorType::BT_USM_DEVICE_INTERNAL ? allocation_type::usm_device
+ : allocation_type::cl_mem;
+ ASSERT_EQ(source_memory->get_allocation_type(), allocation);
+ source_memory->copy_from(stream, values.data(), true);
+ auto dst = context->get_engine().allocate_memory(dst_layout, allocation);
+ PendingConversion pending;
+
+ ASSERT_TRUE(convert_and_copy_async(source.get(), dst, stream, src_layout, conversion, pending));
+ EXPECT_NE(pending.done, nullptr);
+ source_memory->copy_from(stream, zeros.data(), true);
+ source_memory.reset();
+ source.reset();
+ ASSERT_NO_THROW(pending.wait());
+ EXPECT_EQ(pending.staging, nullptr);
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> actual(dst, stream);
+ for (size_t i = 0; i < values.size(); ++i)
+ EXPECT_EQ(actual[i], ov::float16(values[i]));
+ }
+}
+
+TEST(convert_and_copy_gpu_test, async_util_unsupported_input_leaves_pending_empty_for_cpu_fallback) {
+ auto& engine = get_test_engine();
+ if (engine.runtime_type() != runtime_types::ocl || !engine.get_device_info().supports_fp16 ||
+ !engine.supports_allocation(allocation_type::usm_host) ||
+ !engine.supports_allocation(allocation_type::usm_device))
+ GTEST_SKIP() << "OpenCL FP16 and USM host/device allocations are required";
+
+ const ov::Shape shape{2, 3};
+ const layout src_layout{shape, data_types::f32, format::bfyx};
+ const layout dst_layout{shape, data_types::f16, format::bfyx};
+ auto source = ov::make_tensor(ov::element::f32, shape);
+ const std::vector<float> values{1.f, -2.f, 3.f, -4.f, 5.f, -6.f};
+ std::copy(values.begin(), values.end(), static_cast<float*>(source->data()));
+ auto dst = engine.allocate_memory(dst_layout);
+ auto& stream = get_test_stream();
+ PendingConversion pending;
+
+ EXPECT_FALSE(convert_and_copy_async(source.get(), dst, stream, src_layout, nullptr, pending));
+ EXPECT_EQ(pending.staging, nullptr);
+ EXPECT_EQ(pending.done, nullptr);
+ auto network = make_state_network(shape, ov::element::f32, ov::element::f16);
+ auto program = network->get_program();
+ EXPECT_EQ(program->get_tensor_conversion(false), nullptr);
+ auto unprepared = std::make_shared<tensor_conversion>();
+ EXPECT_FALSE(convert_and_copy_async(source.get(), dst, stream, src_layout, unprepared, pending));
+ program->prepare_tensor_conversions({});
+ ASSERT_EQ(program->get_tensor_conversion(false), nullptr);
+ program->prepare_tensor_conversions({{data_types::f16, data_types::f16}, {data_types::f16, data_types::f32}});
+ unprepared = program->get_tensor_conversion();
+ ASSERT_NE(unprepared, nullptr);
+ EXPECT_TRUE(unprepared->get_keys().empty());
+ EXPECT_TRUE(unprepared->get_kernels().empty());
+ ASSERT_NO_THROW(program->prepare_tensor_conversions({}));
+ EXPECT_THROW(program->prepare_tensor_conversions({{data_types::f32, data_types::f16}}), ov::Exception);
+ EXPECT_FALSE(convert_and_copy_async(source.get(), dst, stream, src_layout, unprepared, pending));
+ EXPECT_EQ(pending.staging, nullptr);
+ EXPECT_EQ(pending.done, nullptr);
+
+ ASSERT_NO_THROW(convert_host_tensor(source.get(), dst, stream, src_layout));
+ check_converted_values<float, ov::float16>(dst, values, stream);
+
+ auto prepared_network = make_state_network(shape, ov::element::f32, ov::element::f16);
+ auto prepared_program = prepared_network->get_program();
+ prepared_program->prepare_tensor_conversions({{data_types::f32, data_types::f16}});
+ const auto conversion = prepared_program->get_tensor_conversion();
+ const std::vector<tensor_conversion_key> missing_keys{
+ {data_types::bf16, data_types::f16}, {data_types::f16, data_types::f16}, {data_types::f16, data_types::f32}};
+ for (const auto& key : missing_keys) {
+ auto unsupported_source = ov::make_tensor(ov::element::Type(key.first), shape);
+ if (key.first == data_types::bf16)
+ ov::reference::convert(values.data(), static_cast<ov::bfloat16*>(unsupported_source->data()), values.size());
+ else
+ ov::reference::convert(values.data(), static_cast<ov::float16*>(unsupported_source->data()), values.size());
+ auto destination = engine.allocate_memory(layout{shape, key.second, format::bfyx});
+ const auto unsupported_layout = make_layout(unsupported_source->get_element_type(), shape);
+ EXPECT_FALSE(convert_and_copy_async(unsupported_source.get(), destination, stream, unsupported_layout,
+ conversion, pending));
+ EXPECT_EQ(pending.staging, nullptr);
+ EXPECT_EQ(pending.done, nullptr);
+ ASSERT_NO_THROW(convert_host_tensor(unsupported_source.get(), destination, stream, unsupported_layout));
+ if (key.second == data_types::f32)
+ check_converted_values<float, float>(destination, values, stream);
+ else
+ check_converted_values<float, ov::float16>(destination, values, stream);
+ }
+
+ auto context = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});
+ auto remote = std::make_shared<RemoteTensorImpl>(context, shape, ov::element::f32);
+ EXPECT_FALSE(convert_and_copy_async(remote.get(), dst, stream, src_layout, unprepared, pending));
+ EXPECT_EQ(pending.staging, nullptr);
+ EXPECT_EQ(pending.done, nullptr);
+ try {
+ convert_host_tensor(remote.get(), dst, stream, src_layout);
+ FAIL() << "Expected an inaccessible remote source error";
+ } catch (const ov::Exception& e) {
+ EXPECT_NE(std::string(e.what()).find("CPU conversion cannot access a remote source tensor"), std::string::npos);
+ }
+}
+
+TEST(convert_and_copy_gpu_test, async_util_reuses_kernels_for_sources_shapes_and_types) {
+ auto& engine = get_test_engine();
+ if (engine.runtime_type() != runtime_types::ocl || !engine.get_device_info().supports_fp16 ||
+ !engine.supports_allocation(allocation_type::usm_host) ||
+ !engine.supports_allocation(allocation_type::usm_device))
+ GTEST_SKIP() << "OpenCL FP16 and USM host/device allocations are required";
+
+ auto network = make_state_network({2, 6}, ov::element::f32, ov::element::f16);
+ auto program = network->get_program();
+ const tensor_conversion_key key{data_types::f32, data_types::f16};
+ std::vector<tensor_conversion_key> keys{key, key, {data_types::bf16, data_types::f16}};
+ program->prepare_tensor_conversions(keys);
+ auto executor = program->get_tensor_conversion();
+ ASSERT_NE(executor, nullptr);
+ ASSERT_EQ(executor->get_keys().size(), keys.size() - 1);
+ for (const auto& required_key : keys)
+ ASSERT_TRUE(executor->has_kernel(required_key));
+ program->prepare_tensor_conversions(keys);
+ EXPECT_EQ(program->get_tensor_conversion(), executor);
+ EXPECT_THROW(program->prepare_tensor_conversions({{data_types::bf16, data_types::f16}}), ov::Exception);
+
+ auto context = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});
+ auto& stream = context->get_engine().get_service_stream();
+ auto destination = context->get_engine().allocate_memory(
+ layout{ov::Shape{2, 6}, data_types::f16, format::bfyx}, allocation_type::usm_device);
+
+ auto host = std::make_shared<GpuOnlyHostTensor>(context, ov::element::f32, ov::Shape{2, 6});
+ std::vector<float> first_values{0.f, -0.f, 1.f, -1.f, 0.1f, -0.1f,
+ 65504.f, 1e-8f, 3.5f, -3.5f,
+ std::numeric_limits<float>::infinity(), std::numeric_limits<float>::quiet_NaN()};
+ check_async_values<float, ov::float16>(executor, host, first_values, destination, stream,
+ host->get_impl()->get_original_memory());
+
+ std::weak_ptr<cldnn::program> program_weak = program;
+ network.reset();
+ program.reset();
+ ASSERT_TRUE(program_weak.expired());
+
+ auto larger_host = std::make_shared<GpuOnlyHostTensor>(context, ov::element::f32, ov::Shape{3, 7});
+ destination = context->get_engine().allocate_memory(
+ layout{ov::Shape{3, 7}, data_types::f16, format::bfyx}, allocation_type::usm_device);
+ std::vector<float> larger_values(21);
+ for (size_t i = 0; i < larger_values.size(); ++i)
+ larger_values[i] = (static_cast<int>(i) - 10) / 4.f;
+ check_async_values<float, ov::float16>(executor, larger_host, larger_values, destination, stream,
+ larger_host->get_impl()->get_original_memory());
+
+ auto device = std::make_shared<RemoteTensorImpl>(context, ov::Shape{3, 7}, ov::element::f32,
+ TensorType::BT_USM_DEVICE_INTERNAL);
+ auto device_memory = device->get_original_memory();
+ ASSERT_EQ(device_memory->get_allocation_type(), allocation_type::usm_device);
+ check_async_values<float, ov::float16>(executor, device, larger_values, destination, stream, device_memory);
+ EXPECT_TRUE(executor->has_kernel(key));
+
+ destination = context->get_engine().allocate_memory(
+ layout{ov::Shape{2, 6}, data_types::f16, format::bfyx}, allocation_type::usm_device);
+ auto bf16_host = std::make_shared<GpuOnlyHostTensor>(context, ov::element::bf16, ov::Shape{2, 6});
+ const std::vector<ov::bfloat16> bf16_values{0.f, -0.f, 1.f, -1.f, 0.1f, -0.1f,
+ 65504.f, 1e-8f, 3.5f, -3.5f,
+ std::numeric_limits<float>::infinity(),
+ std::numeric_limits<float>::quiet_NaN()};
+ check_async_values<ov::bfloat16, ov::float16>(executor, bf16_host, bf16_values, destination, stream,
+ bf16_host->get_impl()->get_original_memory());
+}
+
+TEST(convert_and_copy_gpu_test, concurrent_preparation_reuses_tensor_conversion_kernels) {
+ auto& engine = get_test_engine();
+ if (engine.runtime_type() != runtime_types::ocl || !engine.get_device_info().supports_fp16)
+ GTEST_SKIP() << "OpenCL FP16 is required";
+
+ auto network = make_state_network({2, 3}, ov::element::f32, ov::element::f16);
+ auto program = network->get_program();
+ EXPECT_EQ(program->get_tensor_conversion(false), nullptr);
+ const std::vector<tensor_conversion_key> keys{{data_types::f32, data_types::f16}};
+ std::vector<std::future<std::shared_ptr<tensor_conversion>>> preparations;
+ for (size_t i = 0; i < 4; ++i) {
+ preparations.push_back(std::async(std::launch::async, [program, keys] {
+ auto conversion = program->get_tensor_conversion();
+ program->prepare_tensor_conversions(keys);
+ return conversion;
+ }));
+ }
+ std::shared_ptr<tensor_conversion> conversion;
+ for (auto& preparation : preparations) {
+ std::shared_ptr<tensor_conversion> prepared;
+ ASSERT_NO_THROW(prepared = preparation.get());
+ ASSERT_NE(prepared, nullptr);
+ if (!conversion)
+ conversion = prepared;
+ EXPECT_EQ(prepared, conversion);
+ }
+
+ EXPECT_EQ(program->get_tensor_conversion(false), conversion);
+ EXPECT_EQ(conversion->get_keys(), keys);
+ const auto kernels = conversion->get_kernels();
+ ASSERT_EQ(kernels.size(), 1);
+ ASSERT_NO_THROW(program->prepare_tensor_conversions(keys));
+ EXPECT_EQ(conversion->get_kernels(), kernels);
+}
+
+TEST(convert_and_copy_gpu_test, variable_state_keeps_tensor_conversion_after_program_destruction) {
+ auto& engine = get_test_engine();
+ if (engine.runtime_type() != runtime_types::ocl || !engine.get_device_info().supports_fp16 ||
+ !engine.supports_allocation(allocation_type::usm_host) ||
+ !engine.supports_allocation(allocation_type::usm_device))
+ GTEST_SKIP() << "OpenCL FP16 and USM host/device allocations are required";
+
+ const ov::Shape shape{2, 3};
+ auto network = make_state_network(shape, ov::element::f32, ov::element::f16);
+ auto program = network->get_program();
+ program->prepare_tensor_conversions({{data_types::f32, data_types::f16}});
+ auto context = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});
+ auto& stream = context->get_engine().get_service_stream();
+ std::weak_ptr<cldnn::network> network_weak = network;
+ std::weak_ptr<cldnn::program> program_weak = program;
+ std::weak_ptr<tensor_conversion> conversion_weak = program->get_tensor_conversion();
+ std::weak_ptr<RemoteContextImpl> context_weak = context;
+ auto variable = std::make_unique<VariableState>(network->get_variable_info("state"), context,
+ network->get_shape_predictor(),
+ program->get_tensor_conversion());
+ ASSERT_TRUE(program->get_tensor_conversion()->compatible_with(context->get_engine()));
+
+ const std::vector<float> values{1.f, -2.f, 3.f, -4.f, 5.f, -6.f};
+ {
+ VariableState without_conversion(network->get_variable_info("state"), context, network->get_shape_predictor());
+ auto host = ov::make_tensor(ov::element::f32, shape);
+ check_state_values<float, ov::float16>(without_conversion, host, values, stream);
+
+ auto wrong_type = ov::make_tensor(ov::element::bf16, shape);
+ const std::vector<ov::bfloat16> bf16_values{1.f, -2.f, 3.f, -4.f, 5.f, -6.f};
+ check_state_values<ov::bfloat16, ov::float16>(*variable, wrong_type, bf16_values, stream);
+
+ auto same_type_info = network->get_variable_info("state");
+ same_type_info.m_user_specified_type = ov::element::f16;
+ VariableState same_type(same_type_info, context, network->get_shape_predictor(),
+ program->get_tensor_conversion());
+ auto f16_host = ov::make_tensor(ov::element::f16, shape);
+ const std::vector<ov::float16> f16_values{1.f, -2.f, 3.f, -4.f, 5.f, -6.f};
+ check_state_values<ov::float16, ov::float16>(same_type, f16_host, f16_values, stream);
+ }
+ auto source = std::make_shared<GpuOnlyHostTensor>(context, ov::element::f32, shape);
+ source->get_impl()->get_original_memory()->copy_from(stream, values.data(), true);
+ ASSERT_NO_THROW(variable->set_state(ov::SoPtr<ov::ITensor>(source)));
+ source.reset();
+ network.reset();
+ program.reset();
+ context.reset();
+ ASSERT_TRUE(network_weak.expired());
+ ASSERT_TRUE(program_weak.expired());
+ ASSERT_FALSE(conversion_weak.expired());
+ ASSERT_FALSE(context_weak.expired());
+
+ {
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> result(variable->get_memory(), stream);
+ for (size_t i = 0; i < values.size(); ++i)
+ EXPECT_EQ(result[i], ov::float16(values[i]));
+ }
+
+ source = std::make_shared<GpuOnlyHostTensor>(context_weak.lock(), ov::element::f32, shape);
+ const std::vector<float> next_values{0.5f, 1.5f, -2.5f, 3.5f, -4.5f, 5.5f};
+ check_state_values<float, ov::float16>(*variable, source, next_values, stream,
+ source->get_impl()->get_original_memory());
+ ASSERT_NO_THROW(variable->set_state(ov::SoPtr<ov::ITensor>(source)));
+ ASSERT_NO_THROW(variable->reset());
+ EXPECT_FALSE(variable->is_set());
+ check_state_values<float, ov::float16>(*variable, source, values, stream,
+ source->get_impl()->get_original_memory());
+
+ auto larger_source = std::make_shared<GpuOnlyHostTensor>(context_weak.lock(), ov::element::f32, ov::Shape{3, 7});
+ const std::vector<float> larger_values(21, 0.25f);
+ check_state_values<float, ov::float16>(*variable, larger_source, larger_values, stream,
+ larger_source->get_impl()->get_original_memory());
+ larger_source.reset();
+ auto empty = ov::make_tensor(ov::element::f32, ov::Shape{0, 3});
+ ASSERT_NO_THROW(variable->set_state(ov::SoPtr<ov::ITensor>(empty)));
+ EXPECT_TRUE(variable->is_set());
+ EXPECT_EQ(variable->get_memory(), nullptr);
+ ASSERT_NO_THROW(variable->reset());
+
+ // Destroy the state with a submitted conversion and no external conversion/context owners.
+ ASSERT_NO_THROW(variable->set_state(ov::SoPtr<ov::ITensor>(source)));
+ source.reset();
+ variable.reset();
+ EXPECT_TRUE(conversion_weak.expired());
+ EXPECT_TRUE(context_weak.expired());
+}
+
+TEST(convert_and_copy_gpu_test, async_util_stages_aligned_and_unaligned_host_inputs) {
+ auto& engine = get_test_engine();
+ const auto alignment = static_cast<size_t>(engine.get_device_info().cacheline_size.value_or(0));
+ if (engine.runtime_type() != runtime_types::ocl || !engine.get_device_info().supports_fp16 ||
+ !engine.supports_allocation(allocation_type::usm_device) ||
+ !engine.supports_allocation(allocation_type::usm_host) || alignment <= sizeof(float))
+ GTEST_SKIP() << "OpenCL FP16, USM host/device allocations, and cache-line alignment are required";
+
+ ASSERT_EQ(alignment % sizeof(float), 0);
+ const ov::Shape aligned_shape{alignment / sizeof(float)};
+ auto network = make_state_network(aligned_shape, ov::element::f32, ov::element::f16);
+ auto program = network->get_program();
+ program->prepare_tensor_conversions({{data_types::f32, data_types::f16}});
+ auto context = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});
+ auto& stream = context->get_engine().get_service_stream();
+ auto conversion = program->get_tensor_conversion();
+ auto destination = context->get_engine().allocate_memory(
+ layout{aligned_shape, data_types::f16, format::bfyx}, allocation_type::usm_device);
+
+ std::unique_ptr<void, decltype(&ov::util::aligned_free)> storage(
+ ov::util::aligned_alloc(alignment, 2 * alignment), ov::util::aligned_free);
+ ASSERT_NE(storage, nullptr);
+ auto aligned_view = ov::make_tensor(ov::element::f32, aligned_shape, storage.get());
+ auto aligned_source = std::make_shared<GpuOnlyHostView>(aligned_view);
+ std::vector<float> aligned_values(ov::shape_size(aligned_shape));
+ for (size_t i = 0; i < aligned_values.size(); ++i)
+ aligned_values[i] = static_cast<float>(i) - 4.f;
+ check_async_values<float, ov::float16>(conversion, aligned_source, aligned_values, destination, stream);
+
+ auto* unaligned_ptr = static_cast<float*>(storage.get()) + 1;
+ auto unaligned_view = ov::make_tensor(ov::element::f32, aligned_shape, unaligned_ptr);
+ auto unaligned_source = std::make_shared<GpuOnlyHostView>(unaligned_view);
+ std::vector<float> staged_values(ov::shape_size(aligned_shape), -0.5f);
+ check_async_values<float, ov::float16>(conversion, unaligned_source, staged_values, destination, stream);
+
+ const ov::Shape unaligned_size_shape{alignment / sizeof(float) + 1};
+ auto unaligned_size_view = ov::make_tensor(ov::element::f32, unaligned_size_shape, storage.get());
+ auto unaligned_size_source = std::make_shared<GpuOnlyHostView>(unaligned_size_view);
+ std::vector<float> unaligned_size_values(ov::shape_size(unaligned_size_shape), 0.25f);
+ destination = context->get_engine().allocate_memory(
+ layout{unaligned_size_shape, data_types::f16, format::bfyx}, allocation_type::usm_device);
+ check_async_values<float, ov::float16>(conversion, unaligned_size_source, unaligned_size_values, destination, stream);
+}
+
+TEST(convert_and_copy_gpu_test, async_util_preserves_padding_and_transpose_without_packing) {
+ auto& engine = get_test_engine();
+ if (engine.runtime_type() != runtime_types::ocl || !engine.get_device_info().supports_fp16 ||
+ !engine.supports_allocation(allocation_type::usm_host) ||
+ !engine.supports_allocation(allocation_type::usm_device))
+ GTEST_SKIP() << "OpenCL FP16 and USM allocations are required";
+
+ auto network = make_state_network({2, 2}, ov::element::f32, ov::element::f16);
+ auto program = network->get_program();
+ program->prepare_tensor_conversions({{data_types::f32, data_types::f16}});
+ auto context = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});
+ auto& stream = context->get_engine().get_service_stream();
+ auto conversion = program->get_tensor_conversion();
+ auto destination = context->get_engine().allocate_memory(
+ layout{ov::Shape{2, 2}, data_types::f16, format::bfyx}, allocation_type::usm_device);
+ for (bool transpose : {false, true}) {
+ ov::Tensor parent(ov::element::f32, {2, 4});
+ const std::vector<float> physical_values{1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f};
+ std::copy(physical_values.begin(), physical_values.end(), parent.data<float>());
+ ov::Tensor view(parent, {0, 2}, {2, 4});
+ ASSERT_FALSE(view.is_continuous());
+ auto source = std::make_shared<GpuOnlyHostView>(get_tensor_impl(view)._ptr);
+ const layout source_layout(ov::Shape{2, 2}, data_types::f32, format::bfyx, padding({0, 0}, {0, 2}));
+ PendingConversion pending;
+ ASSERT_TRUE(convert_and_copy_async(source.get(), destination, stream, source_layout, conversion,
+ pending, transpose));
+ source.reset();
+ view = {};
+ parent = {};
+ ASSERT_NO_THROW(pending.wait());
+ const std::vector<float> expected = transpose ? std::vector<float>{3.f, 7.f, 4.f, 8.f}
+ : std::vector<float>{3.f, 4.f, 7.f, 8.f};
+ check_converted_values<float, ov::float16>(destination, expected, stream);
+
+ auto padded_host = std::make_shared<PaddedHostTensor>(context);
+ const std::vector<float> padded_values{1.f, 2.f, 99.f, 3.f, 4.f, 99.f};
+ padded_host->get_impl()->get_original_memory()->copy_from(stream, padded_values.data(), true);
+ const layout padded_layout(ov::Shape{2, 2}, data_types::f32, format::bfyx, padding({0, 0}, {0, 1}));
+ ASSERT_TRUE(convert_and_copy_async(padded_host.get(), destination, stream, padded_layout, conversion,
+ pending, transpose));
+ ASSERT_NO_THROW(pending.wait());
+ const std::vector<float> padded_expected = transpose ? std::vector<float>{1.f, 3.f, 2.f, 4.f}
+ : std::vector<float>{1.f, 2.f, 3.f, 4.f};
+ check_converted_values<float, ov::float16>(destination, padded_expected, stream);
+ }
+
+ auto dense_host = std::make_shared<GpuOnlyHostTensor>(context, ov::element::f32, ov::Shape{2, 3});
+ const std::vector<float> dense_values{1.f, -2.f, 3.f, -4.f, 5.f, -6.f};
+ dense_host->get_impl()->get_original_memory()->copy_from(stream, dense_values.data(), true);
+ const layout dense_layout{ov::Shape{2, 3}, data_types::f32, format::bfyx};
+ destination = context->get_engine().allocate_memory(
+ layout{ov::Shape{2, 3}, data_types::f16, format::bfyx}, allocation_type::usm_device);
+ PendingConversion pending;
+ ASSERT_TRUE(convert_and_copy_async(dense_host.get(), destination, stream, dense_layout, conversion, pending, true));
+ ASSERT_NO_THROW(pending.wait());
+ check_converted_values<float, ov::float16>(destination, {1.f, -4.f, -2.f, 5.f, 3.f, -6.f}, stream);
+}
+
+TEST(convert_and_copy_gpu_test, tensor_conversion_guards_source_and_destination_bounds) {
+ auto& engine = get_test_engine();
+ if (engine.runtime_type() != runtime_types::ocl || !engine.get_device_info().supports_fp16 ||
+ !engine.supports_allocation(allocation_type::usm_host) ||
+ !engine.supports_allocation(allocation_type::usm_device))
+ GTEST_SKIP() << "OpenCL FP16 and USM host/device allocations are required";
+
+ auto network = make_state_network({2, 2}, ov::element::f32, ov::element::f16);
+ auto program = network->get_program();
+ program->prepare_tensor_conversions({{data_types::f32, data_types::f16}});
+ auto& stream = get_test_stream();
+ auto source = engine.allocate_memory(layout{ov::Shape{2, 2}, data_types::f32, format::bfyx},
+ allocation_type::usm_host);
+ auto destination = engine.allocate_memory(layout{ov::Shape{2, 2}, data_types::f16, format::bfyx},
+ allocation_type::usm_device);
+ const std::vector<float> values{1.f, 2.f, 3.f, 4.f};
+ source->copy_from(stream, values.data(), true);
+ tensor_conversion_input input;
+ ASSERT_TRUE(tensor_conversion::prepare_input(source->get_layout(), {8, 4}, false, input));
+ auto executor = program->get_tensor_conversion();
+ const std::vector<std::vector<float>> expected_values{
+ {-7.f, -7.f, -7.f, -7.f}, {1.f, 2.f, -7.f, -7.f}, {3.f, 4.f, -7.f, -7.f},
+ {1.f, 3.f, -7.f, 2.f}};
+ // Out-of-bounds reads and writes must leave the affected output untouched.
+ for (int scenario = 0; scenario < 4; ++scenario) {
+ auto invalid = input;
+ if (scenario == 0) {
+ invalid.source_offset = 4;
+ } else if (scenario == 1) {
+ invalid.padded = true;
+ invalid.strides[4] = 4;
+ } else if (scenario == 2) {
+ invalid.source_offset = 2;
+ } else if (scenario == 3) {
+ invalid.transpose = true;
+ invalid.dimensions[4] = 3;
+ }
+ const std::vector<ov::float16> sentinel(4, ov::float16(-7.f));
+ destination->copy_from(stream, sentinel.data(), true);
+ auto completion = executor->execute({data_types::f32, data_types::f16}, source, destination, stream, invalid);
+ ASSERT_NE(completion, nullptr);
+ completion->wait();
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> result(destination, stream);
+ for (size_t i = 0; i < sentinel.size(); ++i) {
+ EXPECT_EQ(result[i], ov::float16(expected_values[scenario][i]))
+ << "scenario " << scenario << ", element " << i;
+ }
+ }
+ auto small_destination = engine.allocate_memory(layout{ov::Shape{2}, data_types::f16, format::bfyx},
+ allocation_type::usm_device);
+ EXPECT_THROW(executor->execute({data_types::f32, data_types::f16}, source, small_destination, stream, input),
+ ov::Exception);
+}
+
+TEST(convert_and_copy_gpu_test, cpu_padded_roi_preserves_values_and_transpose) {
+ auto& engine = get_test_engine();
+ auto& stream = get_test_stream();
+ ov::Tensor parent(ov::element::f32, {2, 4});
+ const std::vector<float> values{1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f};
+ std::copy(values.begin(), values.end(), parent.data<float>());
+ ov::Tensor view(parent, {0, 2}, {2, 4});
+ const layout source_layout(ov::Shape{2, 2}, data_types::f32, format::bfyx, padding({0, 0}, {0, 2}));
+ for (bool transpose : {false, true}) {
+ auto destination = engine.allocate_memory(layout{ov::Shape{2, 2}, data_types::f32, format::bfyx});
+ ASSERT_NO_THROW(convert_and_copy(get_tensor_impl(view)._ptr.get(), destination, stream, source_layout, transpose));
+ cldnn::mem_lock<float, mem_lock_type::read> result(destination, stream);
+ const std::vector<float> expected = transpose ? std::vector<float>{3.f, 7.f, 4.f, 8.f}
+ : std::vector<float>{3.f, 4.f, 7.f, 8.f};
+ for (size_t i = 0; i < expected.size(); ++i)
+ ASSERT_EQ(result[i], expected[i]);
+ }
+}
+
+TEST(convert_and_copy_gpu_test, imported_tensor_conversion_kernel_runs_without_recompilation) {
+ auto& engine = get_test_engine();
+ if (engine.runtime_type() != runtime_types::ocl || !engine.get_device_info().supports_fp16 ||
+ !engine.supports_allocation(allocation_type::usm_host) ||
+ !engine.supports_allocation(allocation_type::usm_device))
+ GTEST_SKIP() << "OpenCL FP16 and USM host/device allocations are required";
+
+ auto stream_ptr = get_test_stream_ptr();
+ for (const bool unsupported_request : {false, true}) {
+ auto empty_network = make_state_network({2, 3}, ov::element::f32, ov::element::f16);
+ auto empty_program = empty_network->get_program();
+ ASSERT_EQ(empty_program->get_tensor_conversion(false), nullptr);
+ if (unsupported_request) {
+ empty_program->prepare_tensor_conversions({{data_types::f16, data_types::f32}});
+ ASSERT_NE(empty_program->get_tensor_conversion(), nullptr);
+ EXPECT_TRUE(empty_program->get_tensor_conversion()->get_keys().empty());
+ }
+ const auto conversion_before_save = empty_program->get_tensor_conversion(false);
+ std::stringstream empty_blob;
+ {
+ BinaryOutputBuffer output(empty_blob);
+ output.set_stream(stream_ptr.get());
+ empty_program->save(output);
+ }
+ EXPECT_EQ(empty_program->get_tensor_conversion(false), conversion_before_save);
+ empty_blob.seekg(0);
+ BinaryInputBuffer input(empty_blob, engine);
+ auto restored = std::make_shared<cldnn::program>(engine, get_test_default_config(engine));
+ restored->load(input);
+ EXPECT_EQ(restored->get_tensor_conversion(false), nullptr);
+ ASSERT_NO_THROW(restored->prepare_tensor_conversions({}));
+ ASSERT_EQ(restored->get_tensor_conversion(false), nullptr);
+ }
+
+ const tensor_conversion_key key{data_types::f32, data_types::f16};
+ auto network = make_state_network({2, 3}, ov::element::f32, ov::element::f16);
+ auto program = network->get_program();
+ program->prepare_tensor_conversions({key});
+ std::stringstream blob;
+ {
+ BinaryOutputBuffer output(blob);
+ output.set_stream(stream_ptr.get());
+ program->save(output);
+ }
+ network.reset();
+ program.reset();
+
+ blob.seekg(0);
+ BinaryInputBuffer input(blob, engine);
+ auto restored = std::make_shared<cldnn::program>(engine, get_test_default_config(engine));
+ EXPECT_EQ(restored->get_tensor_conversion(false), nullptr);
+ restored->load(input);
+ ASSERT_NE(restored->get_tensor_conversion(false), nullptr);
+ ASSERT_TRUE(restored->get_tensor_conversion()->has_kernel(key));
+ auto imported_executor = restored->get_tensor_conversion();
+ ASSERT_NO_THROW(restored->prepare_tensor_conversions({}));
+ ASSERT_NO_THROW(restored->prepare_tensor_conversions({key}));
+ EXPECT_EQ(restored->get_tensor_conversion(), imported_executor);
+ EXPECT_THROW(restored->prepare_tensor_conversions({{data_types::bf16, data_types::f16}}), ov::Exception);
+ auto context = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});
+ auto& stream = context->get_engine().get_service_stream();
+ restored.reset();
+ auto source = std::make_shared<GpuOnlyHostTensor>(context, ov::element::f32, ov::Shape{2, 3});
+ const std::vector<float> values{1.f, -2.f, 3.f, -4.f, 5.f, -6.f};
+ auto destination = context->get_engine().allocate_memory(
+ layout{ov::Shape{2, 3}, data_types::f16, format::bfyx}, allocation_type::usm_device);
+ check_async_values<float, ov::float16>(imported_executor, source, values, destination, stream,
+ source->get_impl()->get_original_memory());
+}