// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "core/common/inlined_containers_fwd.h" #include "core/framework/data_types_internal.h" #include "core/framework/to_tensor_proto_element_type.h" #include "test/util/include/default_providers.h" #include "core/session/lora_adapters.h" #include "lora/adapter_format_version.h" #include "lora/adapter_format_utils.h" #if !defined(ORT_MINIMAL_BUILD) && defined(USE_CUDA) && defined(ORT_UNIT_TEST_HAS_CUDA_PLUGIN_EP) && \ defined(ORT_UNIT_TEST_ENABLE_DYNAMIC_PLUGIN_EP_USAGE) #include "test/unittest_util/test_dynamic_plugin_ep.h" extern std::unique_ptr ort_env; #endif #include "gtest/gtest.h" #include #include #include "test/util/include/asserts.h" namespace onnxruntime { namespace test { namespace { constexpr const int kAdapterVersion = 1; constexpr const int kModelVersion = 1; template struct ReadAndValidateData { void operator()(const Tensor& parameter) const { auto data = parameter.DataAsSpan(); for (size_t i = static_cast(data[0]), size = data.size(); i < size; ++i) { ASSERT_EQ(static_cast(i), data[i]); } } }; template <> struct ReadAndValidateData { void operator()(const Tensor& parameter) const { auto data = parameter.DataAsSpan(); for (size_t i = static_cast(data[0]), size = data.size(); i < size; ++i) { ASSERT_FALSE(std::isnan(data[i])); ASSERT_TRUE(std::isfinite(data[i])); ASSERT_EQ(static_cast(i), data[i]); } } }; template <> struct ReadAndValidateData { void operator()(const Tensor& parameter) const { auto data = parameter.DataAsSpan(); for (size_t i = static_cast(data[0]), size = data.size(); i < size; ++i) { ASSERT_FALSE(std::isnan(data[i])); ASSERT_TRUE(std::isfinite(data[i])); ASSERT_EQ(static_cast(i), data[i]); } } }; template <> struct ReadAndValidateData { void operator()(const Tensor& parameter) const { auto data = parameter.DataAsSpan(); for (size_t i = static_cast(data[0].ToFloat()), size = data.size(); i < size; ++i) { ASSERT_FALSE(data[i].IsNaN()); ASSERT_FALSE(data[i].IsInfinity()); ASSERT_EQ(static_cast(i), data[i].ToFloat()); } } }; template <> struct ReadAndValidateData { void operator()(const Tensor& parameter) const { auto data = parameter.DataAsSpan(); for (size_t i = static_cast(data[0].ToFloat()), size = data.size(); i < size; ++i) { ASSERT_FALSE(data[i].IsNaN()); ASSERT_FALSE(data[i].IsInfinity()); ASSERT_EQ(static_cast(i), data[i].ToFloat()); } } }; auto verify_load = [](const lora::LoraAdapter& adapter) { ASSERT_EQ(kAdapterVersion, adapter.AdapterVersion()); ASSERT_EQ(kModelVersion, adapter.ModelVersion()); const auto param_num = adapter.GetParamNum(); ASSERT_EQ(param_num, 2U); InlinedVector names; InlinedVector ort_values; names.reserve(param_num); ort_values.reserve(param_num); adapter.OutputAdapterParameters(std::back_inserter(names), std::back_inserter(ort_values)); ASSERT_EQ(param_num, names.size()); ASSERT_EQ(param_num, ort_values.size()); for (size_t i = 0; i < param_num; ++i) { const auto& name = names[i]; const auto* ort_value = ort_values[i]; ASSERT_TRUE(name != nullptr); ASSERT_TRUE(ort_value->IsTensor()); const auto& tensor = ort_value->Get(); ASSERT_NE(tensor.GetElementType(), ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED); const auto shape = tensor.Shape().GetDims(); ASSERT_EQ(2U, shape.size()); ASSERT_EQ(8, shape[0]); ASSERT_EQ(4, shape[1]); // Read all the elements to make sure they are accessible // only on CPU const auto& mem_info = tensor.Location(); if (mem_info.device.Type() == OrtDevice::CPU) { utils::MLTypeCallDispatcher disp(tensor.GetElementType()); disp.Invoke(tensor); } } }; constexpr const std::array param_shape = {8, 4}; template struct CreateParam { InlinedVector operator()() const { InlinedVector param(32); std::iota(param.begin(), param.end(), T{0}); return param; } }; template struct GenerateTestParameters { std::vector operator()() const { constexpr const auto data_type = utils::ToTensorProtoElementType(); InlinedVector param_1(32); InlinedVector param_2(32); if constexpr (std::is_same::value || std::is_same::value) { for (float f = 0.f; f < 32; ++f) { param_1[static_cast(f)] = static_cast(f); param_2[static_cast(f)] = static_cast(f + 32); } } else { std::iota(param_1.begin(), param_1.end(), T{0}); std::iota(param_2.begin(), param_2.end(), T{32}); } adapters::utils::AdapterFormatBuilder adapter_builder; adapter_builder.AddParameter("param_1", static_cast(data_type), param_shape, ReinterpretAsSpan(gsl::make_span(param_1))); adapter_builder.AddParameter("param_2", static_cast(data_type), param_shape, ReinterpretAsSpan(gsl::make_span(param_2))); return adapter_builder.Finish(kAdapterVersion, kModelVersion); } }; template struct TestDataType { void operator()() const { const auto test_params = GenerateTestParameters()(); lora::LoraAdapter lora_adapter; lora_adapter.Load(std::move(test_params)); verify_load(lora_adapter); } }; } // namespace TEST(LoraAdapterTest, Load) { // Test different data types const auto data_types = gsl::make_span(adapters::EnumValuesTensorDataType()); for (size_t i = 1, size = data_types.size(); i < size; ++i) { const auto dt = data_types[i]; using namespace adapters; if (dt == TensorDataType::STRING || dt == TensorDataType::BOOL || dt == TensorDataType::COMPLEX64 || dt == TensorDataType::COMPLEX128 || static_cast(dt) >= static_cast(TensorDataType::BFLOAT16)) continue; onnxruntime::utils::MLTypeCallDispatcher disp(static_cast(data_types[i])); disp.Invoke(); } } TEST(LoraAdapterTest, CreateOrtValueOverLoraParameter_ValidParam) { // Build a valid adapter with a single float parameter, then call // CreateOrtValueOverLoraParameter on the deserialized Parameter. constexpr std::array shape = {8, 4}; InlinedVector data(32); std::iota(data.begin(), data.end(), 0.f); adapters::utils::AdapterFormatBuilder adapter_builder; adapter_builder.AddParameter("valid_param", adapters::TensorDataType::FLOAT, shape, ReinterpretAsSpan(gsl::make_span(data))); auto buffer = adapter_builder.Finish(kAdapterVersion, kModelVersion); const auto* adapter = adapters::utils::ValidateAndGetAdapterFromBytes(buffer); ASSERT_NE(adapter, nullptr); ASSERT_NE(adapter->parameters(), nullptr); ASSERT_EQ(adapter->parameters()->size(), 1u); const auto* param = adapter->parameters()->Get(0); auto [name, ort_value] = adapters::utils::CreateOrtValueOverLoraParameter(*param); ASSERT_EQ(name, "valid_param"); ASSERT_TRUE(ort_value.IsTensor()); const auto& tensor = ort_value.Get(); ASSERT_EQ(tensor.GetElementType(), ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT); auto dims = tensor.Shape().GetDims(); ASSERT_EQ(dims.size(), 2u); ASSERT_EQ(dims[0], 8); ASSERT_EQ(dims[1], 4); auto result_span = tensor.DataAsSpan(); ASSERT_EQ(result_span.size(), 32u); for (size_t i = 0; i < result_span.size(); ++i) { ASSERT_EQ(static_cast(i), result_span[i]); } } #ifndef ORT_NO_EXCEPTIONS namespace { // Helper that wraps a single Parameter offset into a finished Adapter flatbuffer // and returns a pointer to the deserialized Parameter. // The FlatBufferBuilder must outlive the returned pointer. const adapters::Parameter* BuildAdapterAndGetParam(flatbuffers::FlatBufferBuilder& fbb, flatbuffers::Offset param_offset) { auto params_offset = fbb.CreateVector(¶m_offset, 1); auto adapter_offset = adapters::CreateAdapter( fbb, adapters::kAdapterFormatVersion, kAdapterVersion, kModelVersion, params_offset); adapters::FinishAdapterBuffer(fbb, adapter_offset); const auto* adapter = adapters::GetAdapter(fbb.GetBufferPointer()); return adapter->parameters()->Get(0); } void ExpectUnsupportedLoraParameterType(const adapters::Parameter& param) { try { adapters::utils::CreateOrtValueOverLoraParameter(param); FAIL() << "Expected unsupported LoRA parameter data_type to be rejected"; } catch (const OnnxRuntimeException& ex) { EXPECT_NE(std::string(ex.what()).find("not a supported LoRA parameter type"), std::string::npos); } } } // namespace TEST(LoraAdapterTest, CreateOrtValueOverLoraParameter_RawDataSizeMismatch) { // Craft a flatbuffer Parameter where raw_data has fewer bytes than // shape (8 x 4) * sizeof(float) = 128 bytes. // We supply only 64 bytes (half the expected amount) so the validation // inside CreateOrtValueOverLoraParameter must throw. flatbuffers::FlatBufferBuilder fbb; auto name_offset = fbb.CreateString("bad_param"); std::vector dims = {8, 4}; auto dims_offset = fbb.CreateVector(dims); // 8 * 4 floats = 32 elements = 128 bytes expected. // Provide only 64 bytes (16 floats worth) to trigger the mismatch. std::vector short_data(64, 0); fbb.ForceVectorAlignment(short_data.size(), sizeof(uint8_t), 8); auto data_offset = fbb.CreateVector(short_data); auto param_offset = adapters::CreateParameter( fbb, name_offset, dims_offset, adapters::TensorDataType::FLOAT, data_offset); // Wrap the single parameter inside an Adapter so the buffer is valid flatbuffers. auto params_offset = fbb.CreateVector(¶m_offset, 1); auto adapter_offset = adapters::CreateAdapter( fbb, adapters::kAdapterFormatVersion, kAdapterVersion, kModelVersion, params_offset); adapters::FinishAdapterBuffer(fbb, adapter_offset); auto* buf = fbb.GetBufferPointer(); // Retrieve the Parameter from the Adapter const auto* adapter = adapters::GetAdapter(buf); ASSERT_NE(adapter, nullptr); ASSERT_NE(adapter->parameters(), nullptr); ASSERT_EQ(adapter->parameters()->size(), 1u); const auto* param = adapter->parameters()->Get(0); ASSERT_NE(param, nullptr); // The raw_data is 64 bytes but shape says 8x4 floats = 128 bytes. // CreateOrtValueOverLoraParameter must throw. ASSERT_THROW(adapters::utils::CreateOrtValueOverLoraParameter(*param), OnnxRuntimeException); } TEST(LoraAdapterTest, CreateOrtValueOverLoraParameter_ExcessRawData) { // Craft a flatbuffer Parameter where raw_data has MORE bytes than expected. // Shape (2, 2) with float => 4 elements => 16 bytes expected, but we supply 32. flatbuffers::FlatBufferBuilder fbb; auto name_offset = fbb.CreateString("excess_param"); std::vector dims = {2, 2}; auto dims_offset = fbb.CreateVector(dims); // 2 * 2 floats = 4 elements = 16 bytes expected. Supply 32. std::vector excess_data(32, 0); fbb.ForceVectorAlignment(excess_data.size(), sizeof(uint8_t), 8); auto data_offset = fbb.CreateVector(excess_data); auto param_offset = adapters::CreateParameter( fbb, name_offset, dims_offset, adapters::TensorDataType::FLOAT, data_offset); auto params_offset = fbb.CreateVector(¶m_offset, 1); auto adapter_offset = adapters::CreateAdapter( fbb, adapters::kAdapterFormatVersion, kAdapterVersion, kModelVersion, params_offset); adapters::FinishAdapterBuffer(fbb, adapter_offset); const auto* adapter = adapters::GetAdapter(fbb.GetBufferPointer()); ASSERT_NE(adapter, nullptr); const auto* param = adapter->parameters()->Get(0); ASSERT_NE(param, nullptr); // Excess data should also trigger the mismatch throw. ASSERT_THROW(adapters::utils::CreateOrtValueOverLoraParameter(*param), OnnxRuntimeException); } TEST(LoraAdapterTest, CreateOrtValueOverLoraParameter_MissingName) { // Parameter with null name should throw gracefully. flatbuffers::FlatBufferBuilder fbb; std::vector dims = {2, 2}; std::vector raw_data(16, 0); // 2*2 floats = 16 bytes // name is nullptr, all other fields are valid auto param_offset = adapters::CreateParameterDirect( fbb, /*name=*/nullptr, &dims, adapters::TensorDataType::FLOAT, &raw_data); const auto* param = BuildAdapterAndGetParam(fbb, param_offset); ASSERT_NE(param, nullptr); ASSERT_EQ(param->name(), nullptr); ASSERT_THROW(adapters::utils::CreateOrtValueOverLoraParameter(*param), OnnxRuntimeException); } TEST(LoraAdapterTest, CreateOrtValueOverLoraParameter_MissingDims) { // Parameter with null dims should throw gracefully. flatbuffers::FlatBufferBuilder fbb; std::vector raw_data(16, 0); // dims is nullptr auto param_offset = adapters::CreateParameterDirect( fbb, "no_dims_param", /*dims=*/nullptr, adapters::TensorDataType::FLOAT, &raw_data); const auto* param = BuildAdapterAndGetParam(fbb, param_offset); ASSERT_NE(param, nullptr); ASSERT_EQ(param->dims(), nullptr); ASSERT_THROW(adapters::utils::CreateOrtValueOverLoraParameter(*param), OnnxRuntimeException); } TEST(LoraAdapterTest, CreateOrtValueOverLoraParameter_EmptyDims) { // Parameter with an empty dims vector should throw gracefully. flatbuffers::FlatBufferBuilder fbb; std::vector empty_dims; std::vector raw_data(16, 0); auto param_offset = adapters::CreateParameterDirect( fbb, "empty_dims_param", &empty_dims, adapters::TensorDataType::FLOAT, &raw_data); const auto* param = BuildAdapterAndGetParam(fbb, param_offset); ASSERT_NE(param, nullptr); ASSERT_EQ(param->dims()->size(), 0u); ASSERT_THROW(adapters::utils::CreateOrtValueOverLoraParameter(*param), OnnxRuntimeException); } TEST(LoraAdapterTest, CreateOrtValueOverLoraParameter_MissingRawData) { // Parameter with null raw_data should throw gracefully. flatbuffers::FlatBufferBuilder fbb; std::vector dims = {2, 2}; // raw_data is nullptr auto param_offset = adapters::CreateParameterDirect( fbb, "no_data_param", &dims, adapters::TensorDataType::FLOAT, /*raw_data=*/nullptr); const auto* param = BuildAdapterAndGetParam(fbb, param_offset); ASSERT_NE(param, nullptr); ASSERT_EQ(param->raw_data(), nullptr); ASSERT_THROW(adapters::utils::CreateOrtValueOverLoraParameter(*param), OnnxRuntimeException); } TEST(LoraAdapterTest, CreateOrtValueOverLoraParameter_UndefinedDataType) { // Parameter with UNDEFINED data_type should throw gracefully. flatbuffers::FlatBufferBuilder fbb; std::vector dims = {2, 2}; std::vector raw_data(16, 0); // data_type defaults to UNDEFINED when not set auto param_offset = adapters::CreateParameterDirect( fbb, "undef_type_param", &dims, adapters::TensorDataType::UNDEFINED, &raw_data); const auto* param = BuildAdapterAndGetParam(fbb, param_offset); ASSERT_NE(param, nullptr); ASSERT_EQ(param->data_type(), adapters::TensorDataType::UNDEFINED); ExpectUnsupportedLoraParameterType(*param); } TEST(LoraAdapterTest, CreateOrtValueOverLoraParameter_StringDataType) { flatbuffers::FlatBufferBuilder fbb; // Provide raw_data sized to pass the old size check: shape [2] * sizeof(std::string). // The guard must reject the type before reaching the size check. std::vector dims = {2}; std::vector raw_data(2 * sizeof(std::string), 0x41); auto param_offset = adapters::CreateParameterDirect( fbb, "string_type_param", &dims, adapters::TensorDataType::STRING, &raw_data); const auto* param = BuildAdapterAndGetParam(fbb, param_offset); ASSERT_NE(param, nullptr); ASSERT_EQ(param->data_type(), adapters::TensorDataType::STRING); ExpectUnsupportedLoraParameterType(*param); } TEST(LoraAdapterTest, CreateOrtValueOverLoraParameter_UndeclaredDataType) { flatbuffers::FlatBufferBuilder fbb; std::vector dims = {2}; std::vector raw_data(2, 0); constexpr int32_t undeclared_data_type = 21; const auto data_type = static_cast(undeclared_data_type); auto param_offset = adapters::CreateParameterDirect( fbb, "undeclared_type_param", &dims, data_type, &raw_data); const auto* param = BuildAdapterAndGetParam(fbb, param_offset); ASSERT_NE(param, nullptr); ASSERT_EQ(static_cast(param->data_type()), undeclared_data_type); ExpectUnsupportedLoraParameterType(*param); } TEST(LoraAdapterTest, CreateOrtValueOverLoraParameter_BoolDataType) { flatbuffers::FlatBufferBuilder fbb; std::vector dims = {1}; std::vector raw_data = {2}; auto param_offset = adapters::CreateParameterDirect( fbb, "bool_type_param", &dims, adapters::TensorDataType::BOOL, &raw_data); const auto* param = BuildAdapterAndGetParam(fbb, param_offset); ASSERT_NE(param, nullptr); ASSERT_EQ(param->data_type(), adapters::TensorDataType::BOOL); ExpectUnsupportedLoraParameterType(*param); } #endif // ORT_NO_EXCEPTIONS namespace { class NoDataTransferAllocator final : public IAllocator { public: NoDataTransferAllocator() : IAllocator(OrtMemoryInfo("NoDataTransfer", OrtDeviceAllocator, OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, OrtDevice::VendorIds::NONE, 0))) {} void* Alloc(size_t) override { ++allocation_count_; return nullptr; } void Free(void*) override {} size_t AllocationCount() const { return allocation_count_; } private: ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(NoDataTransferAllocator); size_t allocation_count_{0}; }; void VerifyMappedParametersAreUsed(const lora::LoraAdapter& adapter) { auto [begin, end] = adapter.GetParamIterators(); for (; begin != end; ++begin) { const auto& param = begin->second; EXPECT_EQ(¶m.GetMapped(), ¶m.GetDeviceOrMapped()); EXPECT_EQ(param.GetDeviceOrMapped().Get().Location().device.Type(), OrtDevice::CPU); } } } // namespace TEST(LoraAdapterTest, UnavailableDataTransferKeepsMappedValues) { auto allocator = std::make_shared(); auto* allocator_ptr = allocator.get(); lora::LoraAdapter adapter(std::move(allocator)); adapter.Load(GenerateTestParameters()()); EXPECT_EQ(allocator_ptr->AllocationCount(), 0U); VerifyMappedParametersAreUsed(adapter); } #ifndef ORT_NO_EXCEPTIONS TEST(LoraAdapterTest, CpuAllocatorIsRejected) { auto cpu_ep = DefaultCpuExecutionProvider(); auto cpu_allocator = cpu_ep->CreatePreferredAllocators()[0]; lora::LoraAdapter adapter(std::move(cpu_allocator)); EXPECT_THROW(adapter.Load(GenerateTestParameters()()), OnnxRuntimeException); } #endif #if !defined(ORT_MINIMAL_BUILD) && defined(USE_CUDA) && defined(ORT_UNIT_TEST_HAS_CUDA_PLUGIN_EP) && \ defined(ORT_UNIT_TEST_ENABLE_DYNAMIC_PLUGIN_EP_USAGE) TEST(LoraAdapterTest, CudaPluginAllocatorKeepsMappedValues) { const auto ep_name = dynamic_plugin_ep_infra::GetEpName(); if (!ep_name.has_value() || *ep_name != dynamic_plugin_ep_infra::kCudaExecutionProviderPluginName) { GTEST_SKIP() << "CUDA plugin EP is not registered"; } auto cuda_ep = DefaultCudaExecutionProvider(); ASSERT_NE(cuda_ep, nullptr); auto cuda_allocators = cuda_ep->CreatePreferredAllocators(); ASSERT_FALSE(cuda_allocators.empty()); ASSERT_EQ(cuda_allocators[0]->Info().device.Type(), OrtDevice::GPU); ASSERT_EQ(cuda_allocators[0]->Info().device.Vendor(), OrtDevice::VendorIds::NVIDIA); lora::LoraAdapter adapter(std::move(cuda_allocators[0])); adapter.Load(GenerateTestParameters()()); VerifyMappedParametersAreUsed(adapter); } TEST(LoraAdapterTest, CudaPluginRunCopiesMappedValues) { const auto ep_name = dynamic_plugin_ep_infra::GetEpName(); if (!ep_name.has_value() || *ep_name != dynamic_plugin_ep_infra::kCudaExecutionProviderPluginName) { GTEST_SKIP() << "CUDA plugin EP is not registered"; } Ort::ConstEpDevice cuda_device; for (const auto& ep_device : ort_env->GetEpDevices()) { if (std::string_view{ep_device.EpName()} == dynamic_plugin_ep_infra::kCudaExecutionProviderPluginName) { cuda_device = ep_device; break; } } ASSERT_NE(cuda_device, nullptr); auto device_memory_info = cuda_device.GetMemoryInfo(OrtDeviceMemoryType_DEFAULT); ASSERT_NE(device_memory_info, nullptr); auto allocator = ort_env->GetSharedAllocator(device_memory_info); ASSERT_NE(allocator, nullptr); constexpr const ORTCHAR_T* adapter_path = ORT_TSTR("testdata/lora/two_params_lora_model.onnx_adapter"); auto adapter = Ort::LoraAdapter::CreateLoraAdapter(adapter_path, allocator); Ort::SessionOptions session_options; session_options.AppendExecutionProvider_V2(*ort_env, {cuda_device}, Ort::KeyValuePairs{}); Ort::Session session(*ort_env, ORT_TSTR("testdata/lora/two_params_lora_model.onnx"), session_options); auto input_devices = session.GetEpDeviceForInputs(); // LoRA parameters are overridable initializers, so only input_x is included in the session input devices. ASSERT_EQ(input_devices.size(), 1U); ASSERT_NE(input_devices[0], nullptr); EXPECT_EQ(std::string_view{input_devices[0].EpName()}, dynamic_plugin_ep_infra::kCudaExecutionProviderPluginName); Ort::RunOptions run_options; run_options.AddActiveLoraAdapter(adapter); constexpr std::array input_shape{4, 4}; std::array input{}; input.fill(1.0f); auto input_value = Ort::Value::CreateTensor( Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU), input.data(), input.size(), input_shape.data(), input_shape.size()); constexpr const char* input_names[]{"input_x"}; constexpr const char* output_names[]{"output"}; auto outputs = session.Run(run_options, input_names, &input_value, 1, output_names, 1); constexpr std::array expected_output{ 154.f, 176.f, 198.f, 220.f, 154.f, 176.f, 198.f, 220.f, 154.f, 176.f, 198.f, 220.f, 154.f, 176.f, 198.f, 220.f}; ASSERT_EQ(outputs.size(), 1U); ASSERT_EQ(outputs[0].GetTensorTypeAndShapeInfo().GetElementCount(), expected_output.size()); const auto* output = outputs[0].GetTensorData(); for (size_t i = 0; i < expected_output.size(); ++i) { EXPECT_NEAR(output[i], expected_output[i], 0.06f); } } #endif #ifdef USE_CUDA TEST(LoraAdapterTest, VerifyDeviceCopy) { auto cpu_ep = DefaultCpuExecutionProvider(); auto cpu_allocator = cpu_ep->CreatePreferredAllocators()[0]; auto cuda_ep = DefaultCudaExecutionProvider(); auto cuda_allocator = cuda_ep->CreatePreferredAllocators()[0]; auto gpu_transfer = cuda_ep->GetDataTransfer(); auto test_params = GenerateTestParameters()(); lora::LoraAdapter adapter(std::move(cuda_allocator)); adapter.Load(std::move(test_params)); auto [begin, end] = adapter.GetParamIterators(); for (; begin != end; ++begin) { const auto& [_, param] = *begin; const auto& tensor_device = param.GetDeviceOrMapped().Get(); const auto& mem_info = tensor_device.Location(); ASSERT_EQ(mem_info.device.Type(), OrtDevice::GPU); ASSERT_EQ(mem_info.device.Vendor(), OrtDevice::VendorIds::NVIDIA); const auto& tensor_cpu = param.GetMapped().Get(); ASSERT_EQ(tensor_cpu.Shape().Size(), tensor_device.Shape().Size()); Tensor copy(tensor_cpu.DataType(), tensor_cpu.Shape(), cpu_allocator); ASSERT_TRUE(gpu_transfer->CanCopy(tensor_device.Location().device, copy.Location().device)); ASSERT_STATUS_OK(gpu_transfer->CopyTensor(tensor_device, copy)); auto expected_span = tensor_cpu.DataAsSpan(); auto copy_span = copy.DataAsSpan(); ASSERT_EQ(expected_span, copy_span); } } #endif } // namespace test } // namespace onnxruntime