SWDEV-546287 - Implement hipLibrary load/unload (#975)

This commit is contained in:
Jatin Chaudhary
2025-09-19 22:23:49 +01:00
committed by GitHub
parent 775ac73d25
commit e79eaaa8a5
26 changed files with 1175 additions and 16 deletions
@@ -65,3 +65,4 @@ endif()
add_subdirectory(synchronization)
add_subdirectory(launchBounds)
add_subdirectory(assertion)
add_subdirectory(library)
@@ -0,0 +1,20 @@
set(TEST_SRC
loadlib_rtc.cc
loadlib_co.cc
library_negative.cc
)
add_custom_target(library_code_load.code
COMMAND ${CMAKE_CXX_COMPILER} --genco ${CMAKE_CURRENT_SOURCE_DIR}/library_code_load.cc
-o ${CMAKE_CURRENT_BINARY_DIR}/../library/library_code_load.code ${OFFLOAD_ARCH_STR}
-I${HIP_PATH}/include/ -I${CMAKE_CURRENT_SOURCE_DIR}/../../include
--rocm-path=${ROCM_PATH})
set_property(GLOBAL APPEND PROPERTY
G_INSTALL_CUSTOM_TARGETS ${CMAKE_CURRENT_BINARY_DIR}/library_code_load.code)
hip_add_exe_to_target(NAME LibraryTests
TEST_SRC ${TEST_SRC}
TEST_TARGET_NAME build_tests
LINKER_LIBS hiprtc)
add_dependencies(LibraryTests library_code_load.code)
@@ -0,0 +1,16 @@
#include <hip/hip_runtime.h>
extern "C" {
__global__ void add_kernel(float* out, float* a, float* b) {
size_t i = threadIdx.x;
out[i] = a[i] + b[i];
}
__global__ void sub_kernel(float* out, float* a, float* b) {
size_t i = threadIdx.x;
out[i] = a[i] - b[i];
}
__global__ void mul_kernel(float* out, float* a, float* b) {
size_t i = threadIdx.x;
out[i] = a[i] * b[i];
}
}
@@ -0,0 +1,47 @@
/*
Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANNTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER INN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR INN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <hip_test_common.hh>
TEST_CASE("Unit_library_negative") {
SECTION("load negative") {
HIP_CHECK_ERROR(hipLibraryLoadData(nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr, 0),
hipErrorInvalidValue);
HIP_CHECK_ERROR(
hipLibraryLoadFromFile(nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr, 0),
hipErrorInvalidValue);
HIP_CHECK_ERROR(hipLibraryUnload(nullptr), hipErrorInvalidValue);
HIP_CHECK_ERROR(hipLibraryGetKernel(nullptr, nullptr, nullptr), hipErrorInvalidValue);
HIP_CHECK_ERROR(hipLibraryGetKernelCount(nullptr, nullptr), hipErrorInvalidValue);
}
SECTION("Load random code") {
const char* code = "call me ishmael"; // definitely not compile-able
hipLibrary_t lib;
hipKernel_t kernel;
// Default behavior is lazy load, so if we pass anything to it, it should pass
HIP_CHECK(hipLibraryLoadData(&lib, code, nullptr, nullptr, 0, nullptr, nullptr, 0));
// But this check will fail
HIP_CHECK_ERROR(hipLibraryGetKernel(&kernel, lib, "moby"), hipErrorInvalidImage);
HIP_CHECK(hipLibraryUnload(lib));
}
}
@@ -0,0 +1,137 @@
/*
Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANNTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER INN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR INN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <hip_test_common.hh>
TEST_CASE("Unit_hip_library_load_co") {
constexpr size_t size = 32;
std::vector<float> input1, input2;
input1.reserve(size);
input2.reserve(size);
for (size_t i = 0; i < size; i++) {
input1[i] = (i + 1) * 2;
input2[i] = i;
}
float *d_in1, *d_in2, *d_out;
HIP_CHECK(hipMalloc(&d_in1, sizeof(float) * size));
HIP_CHECK(hipMalloc(&d_in2, sizeof(float) * size));
HIP_CHECK(hipMalloc(&d_out, sizeof(float) * size));
HIP_CHECK(hipMemset(d_out, 0, sizeof(float) * size));
HIP_CHECK(hipMemcpy(d_in1, input1.data(), sizeof(float) * size, hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(d_in2, input2.data(), sizeof(float) * size, hipMemcpyHostToDevice));
hipStream_t stream;
HIP_CHECK(hipStreamCreate(&stream));
std::string lib_co = "library_code_load.code";
SECTION("One Kernel") {
hipLibrary_t library;
hipKernel_t function;
HIP_CHECK(
hipLibraryLoadFromFile(&library, lib_co.data(), nullptr, nullptr, 0, nullptr, nullptr, 0));
HIP_CHECK(hipLibraryGetKernel(&function, library, "add_kernel"));
unsigned int count = 0;
HIP_CHECK(hipLibraryGetKernelCount(&count, library));
REQUIRE(count == 3);
void* args[] = {&d_out, &d_in1, &d_in2};
HIP_CHECK(hipLaunchKernel(function, 1, size, args, 0, stream));
HIP_CHECK(hipStreamSynchronize(stream));
HIP_CHECK(hipLibraryUnload(library));
std::vector<float> out(size, 0);
HIP_CHECK(hipMemcpy(out.data(), d_out, sizeof(float) * size, hipMemcpyDeviceToHost));
for (size_t i = 0; i < size; i++) {
float tmp = input1[i] + input2[i];
INFO("Index: " << i << " cpu res: " << tmp << " gpu res: " << out[i]);
REQUIRE(out[i] == tmp);
}
}
SECTION("Two Kernel") {
hipLibrary_t library;
hipKernel_t function;
HIP_CHECK(
hipLibraryLoadFromFile(&library, lib_co.data(), nullptr, nullptr, 0, nullptr, nullptr, 0));
HIP_CHECK(hipLibraryGetKernel(&function, library, "sub_kernel"));
unsigned int count = 0;
HIP_CHECK(hipLibraryGetKernelCount(&count, library));
REQUIRE(count == 3);
void* args[] = {&d_out, &d_in1, &d_in2};
HIP_CHECK(hipLaunchKernel(function, 1, size, args, 0, stream));
HIP_CHECK(hipStreamSynchronize(stream));
HIP_CHECK(hipLibraryUnload(library));
std::vector<float> out(size, 0);
HIP_CHECK(hipMemcpy(out.data(), d_out, sizeof(float) * size, hipMemcpyDeviceToHost));
for (size_t i = 0; i < size; i++) {
float tmp = input1[i] - input2[i];
INFO("Index: " << i << " cpu res: " << tmp << " gpu res: " << out[i]);
REQUIRE(out[i] == tmp);
}
}
SECTION("Three Kernel") {
hipLibrary_t library;
hipKernel_t function;
HIP_CHECK(
hipLibraryLoadFromFile(&library, lib_co.data(), nullptr, nullptr, 0, nullptr, nullptr, 0));
HIP_CHECK(hipLibraryGetKernel(&function, library, "mul_kernel"));
unsigned int count = 0;
HIP_CHECK(hipLibraryGetKernelCount(&count, library));
REQUIRE(count == 3);
void* args[] = {&d_out, &d_in1, &d_in2};
HIP_CHECK(hipLaunchKernel(function, 1, size, args, 0, stream));
HIP_CHECK(hipStreamSynchronize(stream));
HIP_CHECK(hipLibraryUnload(library));
std::vector<float> out(size, 0);
HIP_CHECK(hipMemcpy(out.data(), d_out, sizeof(float) * size, hipMemcpyDeviceToHost));
for (size_t i = 0; i < size; i++) {
float tmp = input1[i] * input2[i];
INFO("Index: " << i << " cpu res: " << tmp << " gpu res: " << out[i]);
REQUIRE(out[i] == tmp);
}
}
HIP_CHECK(hipStreamDestroy(stream));
HIP_CHECK(hipFree(d_in1));
HIP_CHECK(hipFree(d_in2));
HIP_CHECK(hipFree(d_out));
}
@@ -0,0 +1,173 @@
/*
Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANNTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER INN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR INN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <hip_test_common.hh>
#include <hip/hiprtc.h>
#include <string>
#include <vector>
static std::vector<char> compile_using_hiprtc(const std::string& code, std::string gpu_arch) {
hiprtcProgram prog;
HIPRTC_CHECK(hiprtcCreateProgram(&prog, code.c_str(), "code.cu", 0, NULL, NULL));
std::string offload_arch = "--offload-arch=" + gpu_arch;
const char* opts[] = {offload_arch.c_str()};
HIPRTC_CHECK(hiprtcCompileProgram(prog, 1, opts));
size_t size;
HIPRTC_CHECK(hiprtcGetCodeSize(prog, &size));
std::vector<char> res(size, 0);
HIPRTC_CHECK(hiprtcGetCode(prog, res.data()));
HIPRTC_CHECK(hiprtcDestroyProgram(&prog));
return res;
}
TEST_CASE("Unit_hip_library_load_rtc") {
constexpr size_t size = 32;
const std::string kernel1 =
"extern \"C\" __global__ void add_kernel(float* out, float*a, float*b) { size_t i = "
"threadIdx.x; out[i] = a[i] + b[i]; }\n";
const std::string kernel2 =
"extern \"C\" __global__ void sub_kernel(float* out, float*a, float*b) { size_t i = "
"threadIdx.x; out[i] = a[i] - b[i]; }\n";
const std::string kernel3 =
"extern \"C\" __global__ void mul_kernel(float* out, float*a, float*b) { size_t i = "
"threadIdx.x; out[i] = a[i] * b[i]; }\n";
hipDeviceProp_t prop;
HIP_CHECK(hipGetDeviceProperties(&prop, 0));
std::string gpu_arch = prop.gcnArchName;
std::vector<float> input1, input2;
input1.reserve(size);
input2.reserve(size);
for (size_t i = 0; i < size; i++) {
input1[i] = (i + 1) * 2;
input2[i] = i;
}
float *d_in1, *d_in2, *d_out;
HIP_CHECK(hipMalloc(&d_in1, sizeof(float) * size));
HIP_CHECK(hipMalloc(&d_in2, sizeof(float) * size));
HIP_CHECK(hipMalloc(&d_out, sizeof(float) * size));
HIP_CHECK(hipMemset(d_out, 0, sizeof(float) * size));
HIP_CHECK(hipMemcpy(d_in1, input1.data(), sizeof(float) * size, hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(d_in2, input2.data(), sizeof(float) * size, hipMemcpyHostToDevice));
hipStream_t stream;
HIP_CHECK(hipStreamCreate(&stream));
SECTION("One Kernel") {
auto kernel = kernel1;
auto code = compile_using_hiprtc(kernel, gpu_arch);
hipLibrary_t library;
hipKernel_t function;
HIP_CHECK(hipLibraryLoadData(&library, code.data(), nullptr, nullptr, 0, nullptr, nullptr, 0));
HIP_CHECK(hipLibraryGetKernel(&function, library, "add_kernel"));
unsigned int count = 0;
HIP_CHECK(hipLibraryGetKernelCount(&count, library));
REQUIRE(count == 1);
void* args[] = {&d_out, &d_in1, &d_in2};
HIP_CHECK(hipLaunchKernel(function, 1, size, args, 0, stream));
HIP_CHECK(hipStreamSynchronize(stream));
HIP_CHECK(hipLibraryUnload(library));
std::vector<float> out(size, 0);
HIP_CHECK(hipMemcpy(out.data(), d_out, sizeof(float) * size, hipMemcpyDeviceToHost));
for (size_t i = 0; i < size; i++) {
float tmp = input1[i] + input2[i];
INFO("Index: " << i << " cpu res: " << tmp << " gpu res: " << out[i]);
REQUIRE(out[i] == tmp);
}
}
SECTION("Two Kernel") {
auto kernel = kernel1 + kernel2;
auto code = compile_using_hiprtc(kernel, gpu_arch);
hipLibrary_t library;
hipKernel_t function;
HIP_CHECK(hipLibraryLoadData(&library, code.data(), nullptr, nullptr, 0, nullptr, nullptr, 0));
HIP_CHECK(hipLibraryGetKernel(&function, library, "sub_kernel"));
unsigned int count = 0;
HIP_CHECK(hipLibraryGetKernelCount(&count, library));
REQUIRE(count == 2);
void* args[] = {&d_out, &d_in1, &d_in2};
HIP_CHECK(hipLaunchKernel(function, 1, size, args, 0, stream));
HIP_CHECK(hipStreamSynchronize(stream));
HIP_CHECK(hipLibraryUnload(library));
std::vector<float> out(size, 0);
HIP_CHECK(hipMemcpy(out.data(), d_out, sizeof(float) * size, hipMemcpyDeviceToHost));
for (size_t i = 0; i < size; i++) {
float tmp = input1[i] - input2[i];
INFO("Index: " << i << " cpu res: " << tmp << " gpu res: " << out[i]);
REQUIRE(out[i] == tmp);
}
}
SECTION("Three Kernel") {
auto kernel = kernel1 + kernel2 + kernel3;
auto code = compile_using_hiprtc(kernel, gpu_arch);
hipLibrary_t library;
hipKernel_t function;
HIP_CHECK(hipLibraryLoadData(&library, code.data(), nullptr, nullptr, 0, nullptr, nullptr, 0));
HIP_CHECK(hipLibraryGetKernel(&function, library, "mul_kernel"));
unsigned int count = 0;
HIP_CHECK(hipLibraryGetKernelCount(&count, library));
REQUIRE(count == 3);
void* args[] = {&d_out, &d_in1, &d_in2};
HIP_CHECK(hipLaunchKernel(function, 1, size, args, 0, stream));
HIP_CHECK(hipStreamSynchronize(stream));
HIP_CHECK(hipLibraryUnload(library));
std::vector<float> out(size, 0);
HIP_CHECK(hipMemcpy(out.data(), d_out, sizeof(float) * size, hipMemcpyDeviceToHost));
for (size_t i = 0; i < size; i++) {
float tmp = input1[i] * input2[i];
INFO("Index: " << i << " cpu res: " << tmp << " gpu res: " << out[i]);
REQUIRE(out[i] == tmp);
}
}
HIP_CHECK(hipStreamDestroy(stream));
HIP_CHECK(hipFree(d_in1));
HIP_CHECK(hipFree(d_in2));
HIP_CHECK(hipFree(d_out));
}