Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alifahrri committed Nov 12, 2023
1 parent b4342ec commit ae4054a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tests/opencl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions tests/opencl/kernels/pad.cpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ae4054a

Please sign in to comment.