Skip to content

Commit

Permalink
[NFC] Refactor kernel_compiler_spirv tests
Browse files Browse the repository at this point in the history
Updates the test to load the SPIR-V module files using CMake instead of
`std::ifstream`. This change makes the test binary more portable since it now
contains the required SPIR-V modules.

Signed-off-by: Michael Aziz <michael.aziz@intel.com>
  • Loading branch information
0x12CC committed Apr 9, 2024
1 parent a451c36 commit 434aa8f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
19 changes: 13 additions & 6 deletions tests/extension/oneapi_kernel_compiler_spirv/CMakeLists.txt
@@ -1,12 +1,19 @@
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)
add_compile_definitions(KERNELS=${KERNELS})
add_compile_definitions(KERNELS_FP16=${KERNELS_FP16})
add_compile_definitions(KERNELS_FP64=${KERNELS_FP64})

add_cts_test(${test_cases_list})
endif()
Expand Up @@ -22,22 +22,20 @@

namespace kernel_compiler_spirv::tests {

static const std::vector<uint8_t> kernels{KERNELS};
static const std::vector<uint8_t> kernels_fp16{KERNELS_FP16};
static const std::vector<uint8_t> kernels_fp64{KERNELS_FP64};

#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 +174,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 +216,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 +237,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

0 comments on commit 434aa8f

Please sign in to comment.