From ae4054a563ea150f070c8f73c0a7f9e65aa5e95c Mon Sep 17 00:00:00 2001 From: Fahri Ali Rahman Date: Sun, 12 Nov 2023 21:18:33 +0700 Subject: [PATCH] add tests --- tests/opencl/CMakeLists.txt | 10 +++++++++- tests/opencl/kernels/pad.cpp | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/opencl/kernels/pad.cpp diff --git a/tests/opencl/CMakeLists.txt b/tests/opencl/CMakeLists.txt index 25fc9d782..f84b2e0ce 100644 --- a/tests/opencl/CMakeLists.txt +++ b/tests/opencl/CMakeLists.txt @@ -49,7 +49,7 @@ set(LLVM_SPIRV_OPTIONS set(CLANG_OPENCL_ARGS -emit-llvm -target spir64-unknown-unknown -Wall -Wextra -Wno-gnu-string-literal-operator-template -Xclang -finclude-default-header - -Xclang -cl-std=CLC++ -O1) + -Xclang -cl-std=CLC++ -O3) set(NMTOOLS_OPENCL_MACROS -D__OPENCL_C_VERSION__=120 -D__OPENCL_VERSION__=120 -DNMTOOLS_OPENCL_BUILD_KERNELS) if (NMTOOLS_OPENCL_TEST_UFUNC) @@ -118,6 +118,7 @@ if (NMTOOLS_OPENCL_TEST_UFUNC) kernels/broadcast_to.cpp kernels/transpose.cpp kernels/repeat.cpp + kernels/pad.cpp ) if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") add_custom_target( ${PROJECT_NAME}-build-kernels @@ -423,6 +424,11 @@ if (NMTOOLS_OPENCL_TEST_UFUNC) -o ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_repeat.bc -x cl -c ${CMAKE_CURRENT_SOURCE_DIR}/kernels/repeat.cpp && llvm-spirv ${LLVM_SPIRV_OPTIONS} -o ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_repeat.spv ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_repeat.bc && cd ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ && xxd -i nm_cl_repeat.spv >> ${CMAKE_CURRENT_SOURCE_DIR}/../../include/nmtools/array/eval/opencl/kernels_spv.hpp + && + ${CMAKE_CXX_COMPILER} ${NMTOOLS_OPENCL_MACROS} ${OPENCL_KHR_EXTENSIONS} -I${NMTOOLS_INCLUDE_DIR} ${CLANG_OPENCL_ARGS} + -o ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_pad.bc -x cl -c ${CMAKE_CURRENT_SOURCE_DIR}/kernels/pad.cpp + && llvm-spirv ${LLVM_SPIRV_OPTIONS} -o ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_pad.spv ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_pad.bc + && cd ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ && xxd -i nm_cl_pad.spv >> ${CMAKE_CURRENT_SOURCE_DIR}/../../include/nmtools/array/eval/opencl/kernels_spv.hpp BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/kernels/relu.bc ${CMAKE_CURRENT_SOURCE_DIR}/kernels/relu.spv @@ -544,6 +550,8 @@ if (NMTOOLS_OPENCL_TEST_UFUNC) ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_transpose.spv ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_repeat.bc ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_repeat.spv + ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_pad.bc + ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_pad.spv ${CMAKE_CURRENT_SOURCE_DIR}/../../include/nmtools/array/eval/opencl/kernels_spv.hpp ) endif () # CMAKE_CXX_COMPILER_ID diff --git a/tests/opencl/kernels/pad.cpp b/tests/opencl/kernels/pad.cpp new file mode 100644 index 000000000..174638128 --- /dev/null +++ b/tests/opencl/kernels/pad.cpp @@ -0,0 +1,36 @@ +#include "nmtools/array/eval/opencl/kernels/pad.hpp" +#ifndef NMTOOLS_OPENCL_BUILD_KERNELS +#include "nmtools/array/eval/opencl/evaluator.hpp" +#include "nmtools/array/array/arange.hpp" +#include "nmtools/array/array/pad.hpp" +#include "nmtools/array/array/reshape.hpp" +#include "nmtools/testing/doctest.hpp" + +namespace nm = nmtools; +namespace ix = nm::index; +namespace na = nm::array; +namespace meta = nm::meta; +namespace opencl = na::opencl; + +#define OPENCL_TEST(fn,...) \ +{ \ + auto expect = na::fn(__VA_ARGS__); \ + auto result = na::fn(__VA_ARGS__,opencl::default_context()); \ + NMTOOLS_ASSERT_EQUAL( nm::shape(result), nm::shape(expect) ); \ + NMTOOLS_ASSERT_CLOSE( result, expect ); \ +} + +TEST_CASE("pad(case1)" * doctest::test_suite("opencl::pad")) +{ + auto shape = nmtools_array{12,8}; + auto numel = ix::product(shape); + auto start = 1; + auto stop = numel+start; + auto input = na::reshape(na::arange(start,stop),shape); + auto pad_width = nmtools_array{0,2,0,0}; + auto pad_value = (float)0; + OPENCL_TEST(pad,input,pad_width,pad_value); +} + + +#endif // NMTOOLS_OPENCL_BUILD_KERNELS \ No newline at end of file