Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFC] Refactor kernel_compiler_spirv tests #887

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions tests/extension/oneapi_kernel_compiler_spirv/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
function(load_spirv SPIRV_FILE RESULT)
file(READ ${SPIRV_FILE} bytes HEX)
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " values ${bytes})
string(REGEX REPLACE ", $" "" values ${values})
set(${RESULT} ${values} PARENT_SCOPE)
endfunction()

if(SYCL_CTS_ENABLE_EXT_ONEAPI_KERNEL_COMPILER_SPIRV_TESTS)
file(GLOB test_cases_list *.cpp)

file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/kernels.spv" KERNELS_PATH)
file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/kernels_fp16.spv" KERNELS_FP16_PATH)
file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/kernels_fp64.spv" KERNELS_FP64_PATH)
add_compile_definitions(KERNELS_PATH="${KERNELS_PATH}")
add_compile_definitions(KERNELS_FP16_PATH="${KERNELS_FP16_PATH}")
add_compile_definitions(KERNELS_FP64_PATH="${KERNELS_FP64_PATH}")
load_spirv("${CMAKE_CURRENT_SOURCE_DIR}/kernels.spv" KERNELS)
load_spirv("${CMAKE_CURRENT_SOURCE_DIR}/kernels_fp16.spv" KERNELS_FP16)
load_spirv("${CMAKE_CURRENT_SOURCE_DIR}/kernels_fp64.spv" KERNELS_FP64)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/kernels.inc" ${KERNELS})
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/kernels_fp16.inc" ${KERNELS_FP16})
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/kernels_fp64.inc" ${KERNELS_FP64})
include_directories("${CMAKE_CURRENT_BINARY_DIR}")

add_cts_test(${test_cases_list})
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,26 @@

namespace kernel_compiler_spirv::tests {

static const std::vector<uint8_t> kernels{
0x12CC marked this conversation as resolved.
Show resolved Hide resolved
#include "kernels.inc"
};
static const std::vector<uint8_t> kernels_fp16{
#include "kernels_fp16.inc"
};
static const std::vector<uint8_t> kernels_fp64{
#include "kernels_fp64.inc"
};

#ifdef SYCL_EXT_ONEAPI_AUTO_LOCAL_RANGE

sycl::kernel_bundle<sycl::bundle_state::executable> loadKernelsFromFile(
sycl::queue& q, const std::string& file_name) {
sycl::kernel_bundle<sycl::bundle_state::executable> loadKernelsFromVector(
sycl::queue& q, const std::vector<uint8_t>& kernels) {
namespace syclex = sycl::ext::oneapi::experimental;

// Read the SPIR-V module from disk.
std::ifstream spv_stream(file_name, std::ios::binary);
if (!spv_stream.is_open()) {
throw std::runtime_error("Failed to open '" + file_name + "'");
}
spv_stream.seekg(0, std::ios::end);
size_t sz = spv_stream.tellg();
spv_stream.seekg(0);
std::vector<std::byte> spv(sz);
spv_stream.read(reinterpret_cast<char*>(spv.data()), sz);
// Copy the SPIR-V module to a std::vector<std::byte>.
std::vector<std::byte> spv(kernels.size());
std::transform(kernels.begin(), kernels.end(), spv.begin(),
[](uint8_t x) { return std::byte(x); });

// Create a kernel bundle from the binary SPIR-V.
sycl::kernel_bundle<sycl::bundle_state::ext_oneapi_source> kb_src =
Expand Down Expand Up @@ -176,17 +180,15 @@ void testStruct(sycl::queue& q, const sycl::kernel& kernel) {
sycl::free(out_p1, q);
}

void testKernelsFromSpvFile(std::string kernels_file,
std::string fp16_kernel_file,
std::string fp64_kernel_file) {
void testKernels() {
const auto getKernel =
[](sycl::kernel_bundle<sycl::bundle_state::executable>& bundle,
const std::string& name) {
return bundle.ext_oneapi_get_kernel(name);
};

sycl::queue q;
auto bundle = loadKernelsFromFile(q, kernels_file);
auto bundle = loadKernelsFromVector(q, kernels);

// Test kernel retrieval functions.
{
Expand Down Expand Up @@ -220,13 +222,13 @@ void testKernelsFromSpvFile(std::string kernels_file,

// Test OpTypeFloat16 parameters.
if (q.get_device().has(sycl::aspect::fp16)) {
auto fp16_bundle = loadKernelsFromFile(q, fp16_kernel_file);
auto fp16_bundle = loadKernelsFromVector(q, kernels_fp16);
testParam<sycl::half>(q, getKernel(fp16_bundle, "OpTypeFloat16"));
}

// Test OpTypeFloat64 parameters.
if (q.get_device().has(sycl::aspect::fp64)) {
auto fp64_bundle = loadKernelsFromFile(q, fp64_kernel_file);
auto fp64_bundle = loadKernelsFromVector(q, kernels_fp64);
testParam<double>(q, getKernel(fp64_bundle, "OpTypeFloat64"));
}

Expand All @@ -241,7 +243,7 @@ TEST_CASE("Test case for \"Kernel Compiler SPIR-V\" extension",
#ifndef SYCL_EXT_ONEAPI_KERNEL_COMPILER_SPIRV
SKIP("SYCL_EXT_ONEAPI_KERNEL_COMPILER_SPIRV is not defined");
#else
testKernelsFromSpvFile(KERNELS_PATH, KERNELS_FP16_PATH, KERNELS_FP64_PATH);
testKernels();
#endif
}

Expand Down
Loading