Skip to content

Commit

Permalink
update c++ for opencl tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alifahrri committed Nov 4, 2023
1 parent f64d6e2 commit 086ec48
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/opencl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ if (NMTOOLS_OPENCL_TEST_UFUNC)

## indexing ##
kernels/broadcast_to.cpp
kernels/transpose.cpp
kernels/repeat.cpp
)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_custom_target( ${PROJECT_NAME}-build-kernels
Expand Down Expand Up @@ -408,6 +410,16 @@ if (NMTOOLS_OPENCL_TEST_UFUNC)
-o ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_subtract.bc -x cl -c ${CMAKE_CURRENT_SOURCE_DIR}/kernels/subtract.cpp
&& llvm-spirv ${LLVM_SPIRV_OPTIONS} -o ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_subtract.spv ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_subtract.bc
&& cd ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ && xxd -i nm_cl_subtract.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_transpose.bc -x cl -c ${CMAKE_CURRENT_SOURCE_DIR}/kernels/transpose.cpp
&& llvm-spirv ${LLVM_SPIRV_OPTIONS} -o ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_transpose.spv ${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_transpose.bc
&& cd ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ && xxd -i nm_cl_transpose.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_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
BYPRODUCTS
${CMAKE_CURRENT_SOURCE_DIR}/kernels/relu.bc
${CMAKE_CURRENT_SOURCE_DIR}/kernels/relu.spv
Expand Down Expand Up @@ -525,6 +537,10 @@ if (NMTOOLS_OPENCL_TEST_UFUNC)
${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_subtract.spv
${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_divide.bc
${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_divide.spv
${CMAKE_CURRENT_SOURCE_DIR}/kernels/nm_cl_transpose.bc
${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}/../../include/nmtools/array/eval/opencl/kernels_spv.hpp
)
endif () # CMAKE_CXX_COMPILER_ID
Expand Down
42 changes: 42 additions & 0 deletions tests/opencl/kernels/repeat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "nmtools/array/eval/opencl/kernels/repeat.hpp"
#ifndef NMTOOLS_OPENCL_BUILD_KERNELS
#include "nmtools/array/eval/opencl/context.hpp"
#include "nmtools/array/eval/opencl/evaluator.hpp"
#include "nmtools/array/array/arange.hpp"
#include "nmtools/array/array/repeat.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("repeat(case1)" * doctest::test_suite("opencl::repeat"))
{
auto shape = nmtools_array<uint32_t,2>{1,64};
auto input = na::reshape(na::arange(ix::product(shape)),shape);
auto repeats = nmtools_array<uint32_t,2>{1,2};
auto axis = 0;
OPENCL_TEST(repeat,input,repeats,axis);
}

TEST_CASE("repeat(case2)" * doctest::test_suite("opencl::repeat"))
{
auto shape = nmtools_array<uint32_t,2>{8,8};
auto input = na::reshape(na::arange(ix::product(shape)),shape);
auto repeats = nmtools_array<uint32_t,2>{1,2};
auto axis = 0;
OPENCL_TEST(repeat,input,repeats,axis);
}

#endif // NMTOOLS_OPENCL_BUILD_KERNELS
40 changes: 40 additions & 0 deletions tests/opencl/kernels/transpose.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "nmtools/array/eval/opencl/kernels/transpose.hpp"
#ifndef NMTOOLS_OPENCL_BUILD_KERNELS
#include "nmtools/array/eval/opencl/context.hpp"
#include "nmtools/array/eval/opencl/evaluator.hpp"
#include "nmtools/array/array/arange.hpp"
#include "nmtools/array/array/transpose.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("transpose(case1)" * doctest::test_suite("opencl::transpose"))
{
auto shape = nmtools_array<uint32_t,2>{1,64};
auto inp = na::reshape(na::arange(ix::product(shape)),shape);
auto axes = nmtools_array<uint32_t,2>{0,1};
OPENCL_TEST(transpose,inp,axes);
}

TEST_CASE("transpose(case1)" * doctest::test_suite("opencl::transpose"))
{
auto shape = nmtools_array<uint32_t,2>{1,64};
auto inp = na::reshape(na::arange(ix::product(shape)),shape);
auto axes = nmtools_array<uint32_t,2>{1,0};
OPENCL_TEST(transpose,inp,axes);
}

#endif // NMTOOLS_OPENCL_BUILD_KERNEL

0 comments on commit 086ec48

Please sign in to comment.