|
| 1 | + |
| 2 | +// Copyright (C) 2024 Intel Corporation |
| 3 | +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See LICENSE.TXT |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | + |
| 7 | +#include <uur/fixtures.h> |
| 8 | +#include <uur/raii.h> |
| 9 | + |
| 10 | +struct urMultiDeviceProgramCreateWithBinaryTest |
| 11 | + : uur::urMultiDeviceProgramTest { |
| 12 | + void SetUp() override { |
| 13 | + UUR_RETURN_ON_FATAL_FAILURE(urMultiDeviceProgramTest::SetUp()); |
| 14 | + |
| 15 | + // First obtain binaries for all devices from the compiler SPIRV program. |
| 16 | + devices = uur::DevicesEnvironment::instance->devices; |
| 17 | + if (devices.size() < 2) { |
| 18 | + GTEST_SKIP(); |
| 19 | + } |
| 20 | + ASSERT_SUCCESS(urProgramBuild(context, program, nullptr)); |
| 21 | + size_t binary_sizes_len = 0; |
| 22 | + ASSERT_SUCCESS(urProgramGetInfo(program, UR_PROGRAM_INFO_BINARY_SIZES, |
| 23 | + 0, nullptr, &binary_sizes_len)); |
| 24 | + // We're expecting number of binaries equal to number of devices. |
| 25 | + ASSERT_EQ(binary_sizes_len / sizeof(size_t), devices.size()); |
| 26 | + binary_sizes.resize(devices.size()); |
| 27 | + binaries.resize(devices.size()); |
| 28 | + ASSERT_SUCCESS(urProgramGetInfo(program, UR_PROGRAM_INFO_BINARY_SIZES, |
| 29 | + binary_sizes.size() * sizeof(size_t), |
| 30 | + binary_sizes.data(), nullptr)); |
| 31 | + for (size_t i = 0; i < devices.size(); i++) { |
| 32 | + size_t binary_size = binary_sizes[i]; |
| 33 | + binaries[i].resize(binary_size); |
| 34 | + pointers.push_back(binaries[i].data()); |
| 35 | + } |
| 36 | + ASSERT_SUCCESS(urProgramGetInfo(program, UR_PROGRAM_INFO_BINARIES, |
| 37 | + sizeof(uint8_t *) * pointers.size(), |
| 38 | + pointers.data(), nullptr)); |
| 39 | + |
| 40 | + // Now create a program with multiple device binaries. |
| 41 | + ASSERT_SUCCESS(urProgramCreateWithBinary( |
| 42 | + context, devices.size(), devices.data(), binary_sizes.data(), |
| 43 | + pointers.data(), nullptr, &binary_program)); |
| 44 | + } |
| 45 | + |
| 46 | + void TearDown() override { |
| 47 | + if (binary_program) { |
| 48 | + EXPECT_SUCCESS(urProgramRelease(binary_program)); |
| 49 | + } |
| 50 | + UUR_RETURN_ON_FATAL_FAILURE(urMultiDeviceProgramTest::TearDown()); |
| 51 | + } |
| 52 | + |
| 53 | + std::vector<std::vector<uint8_t>> binaries; |
| 54 | + std::vector<ur_device_handle_t> devices; |
| 55 | + std::vector<const uint8_t *> pointers; |
| 56 | + std::vector<size_t> binary_sizes; |
| 57 | + ur_program_handle_t binary_program = nullptr; |
| 58 | +}; |
| 59 | + |
| 60 | +// Create the kernel using the program created with multiple binaries and run it on all devices. |
| 61 | +TEST_F(urMultiDeviceProgramCreateWithBinaryTest, |
| 62 | + CreateAndRunKernelOnAllDevices) { |
| 63 | + constexpr size_t global_offset = 0; |
| 64 | + constexpr size_t n_dimensions = 1; |
| 65 | + constexpr size_t global_size = 100; |
| 66 | + constexpr size_t local_size = 100; |
| 67 | + |
| 68 | + auto kernelName = |
| 69 | + uur::KernelsEnvironment::instance->GetEntryPointNames("foo")[0]; |
| 70 | + |
| 71 | + for (size_t i = 1; i < devices.size(); i++) { |
| 72 | + uur::raii::Kernel kernel; |
| 73 | + ASSERT_SUCCESS(urProgramBuild(context, binary_program, nullptr)); |
| 74 | + ASSERT_SUCCESS( |
| 75 | + urKernelCreate(binary_program, kernelName.data(), kernel.ptr())); |
| 76 | + |
| 77 | + ASSERT_SUCCESS(urEnqueueKernelLaunch( |
| 78 | + queues[i], kernel.get(), n_dimensions, &global_offset, &local_size, |
| 79 | + &global_size, 0, nullptr, nullptr)); |
| 80 | + |
| 81 | + ASSERT_SUCCESS(urQueueFinish(queues[i])); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F(urMultiDeviceProgramCreateWithBinaryTest, CheckCompileAndLink) { |
| 86 | + // TODO: Current behaviour is that we allow to compile only IL programs for Level Zero and link only programs in Object state. |
| 87 | + // OpenCL allows to compile and link programs created from native binaries, so probably we should align those two. |
| 88 | + ur_platform_backend_t backend; |
| 89 | + ASSERT_SUCCESS(urPlatformGetInfo(platform, UR_PLATFORM_INFO_BACKEND, |
| 90 | + sizeof(backend), &backend, nullptr)); |
| 91 | + if (backend == UR_PLATFORM_BACKEND_LEVEL_ZERO) { |
| 92 | + ASSERT_EQ(urProgramCompile(context, binary_program, nullptr), |
| 93 | + UR_RESULT_ERROR_INVALID_OPERATION); |
| 94 | + uur::raii::Program linked_program; |
| 95 | + ASSERT_EQ(urProgramLink(context, 1, &binary_program, nullptr, |
| 96 | + linked_program.ptr()), |
| 97 | + UR_RESULT_ERROR_INVALID_OPERATION); |
| 98 | + } else if (backend == UR_PLATFORM_BACKEND_OPENCL) { |
| 99 | + ASSERT_SUCCESS(urProgramCompile(context, binary_program, nullptr)); |
| 100 | + uur::raii::Program linked_program; |
| 101 | + ASSERT_SUCCESS(urProgramLink(context, 1, &binary_program, nullptr, |
| 102 | + linked_program.ptr())); |
| 103 | + } else { |
| 104 | + GTEST_SKIP(); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +TEST_F(urMultiDeviceProgramCreateWithBinaryTest, |
| 109 | + InvalidProgramBinaryForOneOfTheDevices) { |
| 110 | + std::vector<const uint8_t *> pointers_with_invalid_binary; |
| 111 | + for (size_t i = 1; i < devices.size(); i++) { |
| 112 | + pointers_with_invalid_binary.push_back(nullptr); |
| 113 | + } |
| 114 | + uur::raii::Program invalid_bin_program; |
| 115 | + ASSERT_EQ(urProgramCreateWithBinary(context, devices.size(), devices.data(), |
| 116 | + binary_sizes.data(), |
| 117 | + pointers_with_invalid_binary.data(), |
| 118 | + nullptr, invalid_bin_program.ptr()), |
| 119 | + UR_RESULT_ERROR_INVALID_VALUE); |
| 120 | +} |
| 121 | + |
| 122 | +// Test the case when program is built multiple times for different devices from context. |
| 123 | +TEST_F(urMultiDeviceProgramCreateWithBinaryTest, MultipleBuildCalls) { |
| 124 | + // Run test only for level zero backend which supports urProgramBuildExp. |
| 125 | + ur_platform_backend_t backend; |
| 126 | + ASSERT_SUCCESS(urPlatformGetInfo(platform, UR_PLATFORM_INFO_BACKEND, |
| 127 | + sizeof(backend), &backend, nullptr)); |
| 128 | + if (backend != UR_PLATFORM_BACKEND_LEVEL_ZERO) { |
| 129 | + GTEST_SKIP(); |
| 130 | + } |
| 131 | + auto first_subset = std::vector<ur_device_handle_t>( |
| 132 | + devices.begin(), devices.begin() + devices.size() / 2); |
| 133 | + auto second_subset = std::vector<ur_device_handle_t>( |
| 134 | + devices.begin() + devices.size() / 2, devices.end()); |
| 135 | + ASSERT_SUCCESS(urProgramBuildExp(binary_program, first_subset.size(), |
| 136 | + first_subset.data(), nullptr)); |
| 137 | + auto kernelName = |
| 138 | + uur::KernelsEnvironment::instance->GetEntryPointNames("foo")[0]; |
| 139 | + uur::raii::Kernel kernel; |
| 140 | + ASSERT_SUCCESS( |
| 141 | + urKernelCreate(binary_program, kernelName.data(), kernel.ptr())); |
| 142 | + ASSERT_SUCCESS(urProgramBuildExp(binary_program, second_subset.size(), |
| 143 | + second_subset.data(), nullptr)); |
| 144 | + ASSERT_SUCCESS( |
| 145 | + urKernelCreate(binary_program, kernelName.data(), kernel.ptr())); |
| 146 | + |
| 147 | + // Building for the same subset of devices should not fail. |
| 148 | + ASSERT_SUCCESS(urProgramBuildExp(binary_program, first_subset.size(), |
| 149 | + first_subset.data(), nullptr)); |
| 150 | +} |
| 151 | + |
| 152 | +// Test the case we get native binaries from program created with multiple binaries which wasn't built (i.e. in Native state). |
| 153 | +TEST_F(urMultiDeviceProgramCreateWithBinaryTest, |
| 154 | + GetBinariesAndSizesFromProgramInNativeState) { |
| 155 | + size_t exp_binary_sizes_len = 0; |
| 156 | + std::vector<size_t> exp_binary_sizes; |
| 157 | + std::vector<std::vector<uint8_t>> exp_binaries; |
| 158 | + std::vector<const uint8_t *> exp_pointer; |
| 159 | + ASSERT_SUCCESS(urProgramGetInfo(binary_program, |
| 160 | + UR_PROGRAM_INFO_BINARY_SIZES, 0, nullptr, |
| 161 | + &exp_binary_sizes_len)); |
| 162 | + auto num = exp_binary_sizes_len / sizeof(size_t); |
| 163 | + exp_binary_sizes.resize(num); |
| 164 | + exp_binaries.resize(num); |
| 165 | + exp_pointer.resize(num); |
| 166 | + ASSERT_SUCCESS(urProgramGetInfo(binary_program, |
| 167 | + UR_PROGRAM_INFO_BINARY_SIZES, |
| 168 | + exp_binary_sizes.size() * sizeof(size_t), |
| 169 | + exp_binary_sizes.data(), nullptr)); |
| 170 | + for (size_t i = 0; i < devices.size(); i++) { |
| 171 | + size_t binary_size = exp_binary_sizes[i]; |
| 172 | + exp_binaries[i].resize(binary_size); |
| 173 | + exp_pointer[i] = exp_binaries[i].data(); |
| 174 | + } |
| 175 | + ASSERT_SUCCESS(urProgramGetInfo(program, UR_PROGRAM_INFO_BINARIES, |
| 176 | + sizeof(uint8_t *) * exp_pointer.size(), |
| 177 | + exp_pointer.data(), nullptr)); |
| 178 | + |
| 179 | + // Verify that we get exactly what was provided at the creation step. |
| 180 | + ASSERT_EQ(exp_binaries, binaries); |
| 181 | + ASSERT_EQ(exp_binary_sizes, binary_sizes); |
| 182 | +} |
| 183 | + |
| 184 | +TEST_F(urMultiDeviceProgramCreateWithBinaryTest, GetIL) { |
| 185 | + size_t il_length = 0; |
| 186 | + ASSERT_SUCCESS(urProgramGetInfo(binary_program, UR_PROGRAM_INFO_IL, 0, |
| 187 | + nullptr, &il_length)); |
| 188 | + ASSERT_EQ(il_length, 0); |
| 189 | + std::vector<uint8_t> il(il_length); |
| 190 | + ASSERT_EQ(urProgramGetInfo(binary_program, UR_PROGRAM_INFO_IL, il.size(), |
| 191 | + il.data(), nullptr), |
| 192 | + UR_RESULT_ERROR_INVALID_NULL_POINTER); |
| 193 | +} |
| 194 | + |
| 195 | +TEST_F(urMultiDeviceProgramCreateWithBinaryTest, CheckProgramGetInfo) { |
| 196 | + std::vector<char> property_value; |
| 197 | + size_t property_size = 0; |
| 198 | + |
| 199 | + // Program is not in exe state, so error is expected. |
| 200 | + for (auto prop : |
| 201 | + {UR_PROGRAM_INFO_NUM_KERNELS, UR_PROGRAM_INFO_KERNEL_NAMES}) { |
| 202 | + auto result = |
| 203 | + urProgramGetInfo(binary_program, prop, 0, nullptr, &property_size); |
| 204 | + // TODO: OpenCL and Level Zero return diffent error code, it needs to be fixed. |
| 205 | + ASSERT_TRUE(result == UR_RESULT_ERROR_INVALID_PROGRAM_EXECUTABLE || |
| 206 | + result == UR_RESULT_ERROR_INVALID_PROGRAM); |
| 207 | + } |
| 208 | + |
| 209 | + // Now build the program and check that we can get the info. |
| 210 | + ASSERT_SUCCESS(urProgramBuild(context, binary_program, nullptr)); |
| 211 | + |
| 212 | + size_t logSize; |
| 213 | + std::string log; |
| 214 | + |
| 215 | + for (auto dev : devices) { |
| 216 | + ASSERT_SUCCESS(urProgramGetBuildInfo( |
| 217 | + program, dev, UR_PROGRAM_BUILD_INFO_LOG, 0, nullptr, &logSize)); |
| 218 | + // The size should always include the null terminator. |
| 219 | + ASSERT_GT(logSize, 0); |
| 220 | + log.resize(logSize); |
| 221 | + ASSERT_SUCCESS(urProgramGetBuildInfo(program, dev, |
| 222 | + UR_PROGRAM_BUILD_INFO_LOG, logSize, |
| 223 | + log.data(), nullptr)); |
| 224 | + ASSERT_EQ(log[logSize - 1], '\0'); |
| 225 | + } |
| 226 | + |
| 227 | + ASSERT_SUCCESS(urProgramGetInfo(binary_program, UR_PROGRAM_INFO_NUM_KERNELS, |
| 228 | + 0, nullptr, &property_size)); |
| 229 | + property_value.resize(property_size); |
| 230 | + ASSERT_SUCCESS(urProgramGetInfo(binary_program, UR_PROGRAM_INFO_NUM_KERNELS, |
| 231 | + property_size, property_value.data(), |
| 232 | + nullptr)); |
| 233 | + |
| 234 | + auto returned_num_of_kernels = |
| 235 | + reinterpret_cast<uint32_t *>(property_value.data()); |
| 236 | + ASSERT_GT(*returned_num_of_kernels, 0U); |
| 237 | + ASSERT_SUCCESS(urProgramGetInfo(binary_program, |
| 238 | + UR_PROGRAM_INFO_KERNEL_NAMES, 0, nullptr, |
| 239 | + &property_size)); |
| 240 | + property_value.resize(property_size); |
| 241 | + ASSERT_SUCCESS(urProgramGetInfo(binary_program, |
| 242 | + UR_PROGRAM_INFO_KERNEL_NAMES, property_size, |
| 243 | + property_value.data(), nullptr)); |
| 244 | + auto returned_kernel_names = |
| 245 | + reinterpret_cast<char *>(property_value.data()); |
| 246 | + ASSERT_STRNE(returned_kernel_names, ""); |
| 247 | +} |
0 commit comments