Skip to content

Commit

Permalink
Ufuncs, reduce, outer, accumulate: view attribute initializer, genera…
Browse files Browse the repository at this point in the history
…lized functors, more useful specialized utils functions (#269)

* add missing include for cuda context

* add unary_ufunc, binary_ufunc, broadcast_binary_ufunc view entrypoint

* add unary_ufunc, binary_ufunc, and broadcast_binary_ufunc functors

* add accumulate functor

* add outer functor

* add reduce functor

* simplify various ufunc functors

* add accumulate view attribute initializer

* add outer view attribute initializer

* add reduce view attribute initializer

* add ufunc view attribute initializer

* refactor various ufunc, reduce, outer, accumulate views

* refactor various utils for easier specialization

* add functor isequal specialization

* update tests

* try to fix build

* fix tests

* fix adaptivecpp installs scripts

* fix sycl dockerfile deps installation

* fix simd ufunc specialization

* fix simd ufunc specialization

* fix sycl dockerfile deps installation

* fix build

* fix build

* fix build
  • Loading branch information
alifahrri authored Mar 22, 2024
1 parent 5f96390 commit 08e3733
Show file tree
Hide file tree
Showing 256 changed files with 6,189 additions and 3,335 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ jobs:
./dockcross-${{ matrix.name }} bash -c 'build/${{ matrix.name }}/tests/index/numeric-tests-index-doctest'
dockcross-gcc-functional:
name: ${{ matrix.name }}-index
name: ${{ matrix.name }}-functional
runs-on: ubuntu-20.04
strategy:
matrix:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sycl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
- uses: actions/checkout@v2
- name: build docker
run: |
docker build . --tag nmtools:sycl-clang14-cuda --file docker/sycl-cuda.dockerfile
docker build . --tag nmtools:sycl-clang14-cuda --build-arg BASE=nvidia/cuda:11.8.0-devel-ubuntu22.04 --build-arg cuda_backend=ON --build-arg toolchain=sycl-clang14-cuda --file docker/sycl.dockerfile
21 changes: 17 additions & 4 deletions docker/sycl.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from ubuntu:jammy as dev
# ARG BASE=nvidia/cuda:11.8.0-devel-ubuntu22.04
ARG BASE=ubuntu:jammy
from ${BASE} as dev

ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia
Expand Down Expand Up @@ -46,15 +48,26 @@ COPY scripts/install_doctest.sh scripts/install_doctest.sh
RUN bash scripts/install_doctest.sh

RUN apt install -y libclang-dev clang-tools libomp-dev llvm-dev lld libboost-dev libboost-fiber-dev libboost-context-dev
RUN bash scripts/install_opensycl.sh

ARG opencl_backend=OFF
ENV OPENCL_BACKEND=${opencl_backend}

ARG cuda_backend=OFF
ENV CUDA_BACKEND=${cuda_backend}

ARG level_zero_backend=OFF
ENV LEVEL_ZERO_BACKEND=${level_zero_backend}

ARG toolchain=sycl-clang14-omp
ENV TOOLCHAIN=${toolchain}

RUN bash scripts/install_opensycl.sh

RUN mkdir -p build/${toolchain} && cd build/${toolchain} \
&& cmake -DCMAKE_TOOLCHAIN_FILE=cmake/toolchains/${toolchain}.cmake \
-DNMTOOLS_BUILD_META_TESTS=OFF -DNMTOOLS_BUILD_UTL_TESTS=OFF -DNMTOOLS_TEST_ALL=OFF \
-DNMTOOLS_BUILD_SYCL_TESTS=ON \
../.. \
&& make -j2 VERBOSE=1 numeric-tests-sycl-doctest

ENV toolchain=${toolchain}
CMD ["sh", "-c", "/workspace/nmtools/build/${toolchain}/tests/sycl/numeric-tests-sycl-doctest"]
CMD ["sh", "-c", "/workspace/nmtools/build/${TOOLCHAIN}/tests/sycl/numeric-tests-sycl-doctest"]
1 change: 1 addition & 0 deletions include/nmtools/array/eval/cuda/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "nmtools/meta.hpp"
#include "nmtools/array/eval/kernel_helper.hpp"
#include "nmtools/utility/tuple_cat.hpp"
#include "nmtools/array/functional/functor.hpp"
#include <memory>

template <auto out_static_dim=0, typename function_t
Expand Down
12 changes: 6 additions & 6 deletions include/nmtools/array/eval/simd/simde_avx512/ufunc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

namespace nmtools::array::simd
{
template <typename data_t, typename...ufunc_args_t>
struct ufunc_simd_t<view::softshrink_t<ufunc_args_t...>,simde_avx512_t,data_t> : simd_op_t<simde_avx512_t,data_t>
template <typename data_t, typename lambda_t>
struct ufunc_simd_t<view::softshrink_t<lambda_t>,simde_avx512_t,data_t> : simd_op_t<simde_avx512_t,data_t>
{
using simd_t = simd_op_t<simde_avx512_t,data_t>;
using simd_dtype_t = decltype(simd_t::set1(0));
using ufunc_op_t = view::softshrink_t<ufunc_args_t...>;
using ufunc_op_t = view::softshrink_t<lambda_t>;

const simd_dtype_t zero = simd_t::set1(0);
const simd_dtype_t one = simd_t::set1(1);
Expand Down Expand Up @@ -56,12 +56,12 @@ namespace nmtools::array::simd
}
}; // ufunc_simd_t

template <typename data_t, typename...ufunc_args_t>
struct ufunc_simd_t<view::hardshrink_t<ufunc_args_t...>,simde_avx512_t,data_t> : simd_op_t<simde_avx512_t,data_t>
template <typename data_t, typename lambda_t>
struct ufunc_simd_t<view::hardshrink_t<lambda_t>,simde_avx512_t,data_t> : simd_op_t<simde_avx512_t,data_t>
{
using simd_t = simd_op_t<simde_avx512_t,data_t>;
using simd_dtype_t = decltype(simd_t::set1(0));
using ufunc_op_t = view::hardshrink_t<ufunc_args_t...>;
using ufunc_op_t = view::hardshrink_t<lambda_t>;

const simd_dtype_t zero = simd_t::set1(0);
const simd_dtype_t pos_lambda;
Expand Down
36 changes: 18 additions & 18 deletions include/nmtools/array/eval/simd/ufunc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ namespace nmtools::array::simd
}
}; // ufunc_simd_t

template <typename simd_tag_t, typename data_t, typename...ufunc_args_t>
struct ufunc_simd_t<view::hardtanh_t<ufunc_args_t...>,simd_tag_t,data_t> : simd_op_t<simd_tag_t,data_t>
template <typename simd_tag_t, typename data_t, typename min_val_t, typename max_val_t>
struct ufunc_simd_t<view::hardtanh_t<min_val_t,max_val_t>,simd_tag_t,data_t> : simd_op_t<simd_tag_t,data_t>
{
using simd_t = simd_op_t<simd_tag_t,data_t>;
using simd_dtype_t = decltype(simd_t::set1(0));
using ufunc_op_t = view::hardtanh_t<ufunc_args_t...>;
using ufunc_op_t = view::hardtanh_t<min_val_t,max_val_t>;

const simd_dtype_t min_val;
const simd_dtype_t max_val;
Expand All @@ -180,12 +180,12 @@ namespace nmtools::array::simd
}
}; // ufunc_simd_t

template <typename simd_tag_t, typename data_t, typename...ufunc_args_t>
struct ufunc_simd_t<view::leaky_relu_t<ufunc_args_t...>,simd_tag_t,data_t> : simd_op_t<simd_tag_t,data_t>
template <typename simd_tag_t, typename data_t, typename negative_slope_t>
struct ufunc_simd_t<view::leaky_relu_t<negative_slope_t>,simd_tag_t,data_t> : simd_op_t<simd_tag_t,data_t>
{
using simd_t = simd_op_t<simd_tag_t,data_t>;
using simd_dtype_t = decltype(simd_t::set1(0));
using ufunc_op_t = view::leaky_relu_t<ufunc_args_t...>;
using ufunc_op_t = view::leaky_relu_t<negative_slope_t>;

const simd_dtype_t zero = simd_t::set1(0);
const simd_dtype_t negative_slope;
Expand All @@ -203,12 +203,12 @@ namespace nmtools::array::simd
}
}; // ufunc_simd_t

template <typename simd_tag_t, typename data_t,typename...ufunc_args_t>
struct ufunc_simd_t<view::prelu_t<ufunc_args_t...>,simd_tag_t,data_t> : simd_op_t<simd_tag_t,data_t>
template <typename simd_tag_t, typename data_t,typename alpha_t>
struct ufunc_simd_t<view::prelu_t<alpha_t>,simd_tag_t,data_t> : simd_op_t<simd_tag_t,data_t>
{
using simd_t = simd_op_t<simd_tag_t,data_t>;
using simd_dtype_t = decltype(simd_t::set1(0));
using ufunc_op_t = view::prelu_t<ufunc_args_t...>;
using ufunc_op_t = view::prelu_t<alpha_t>;

const simd_dtype_t zero = simd_t::set1(0);
const simd_dtype_t alpha;
Expand All @@ -226,12 +226,12 @@ namespace nmtools::array::simd
}
}; // ufunc_simd_t

template <typename simd_tag_t, typename data_t, typename...ufunc_args_t>
struct ufunc_simd_t<view::softshrink_t<ufunc_args_t...>,simd_tag_t,data_t> : simd_op_t<simd_tag_t,data_t>
template <typename simd_tag_t, typename data_t, typename lambda_t>
struct ufunc_simd_t<view::softshrink_t<lambda_t>,simd_tag_t,data_t> : simd_op_t<simd_tag_t,data_t>
{
using simd_t = simd_op_t<simd_tag_t,data_t>;
using simd_dtype_t = decltype(simd_t::set1(0));
using ufunc_op_t = view::softshrink_t<ufunc_args_t...>;
using ufunc_op_t = view::softshrink_t<lambda_t>;

const simd_dtype_t zero = simd_t::set1(0);
const simd_dtype_t pos_lambda;
Expand Down Expand Up @@ -289,12 +289,12 @@ namespace nmtools::array::simd
}
}; // ufunc_simd_t

template <typename simd_tag_t, typename data_t, typename...ufunc_args_t>
struct ufunc_simd_t<view::hardshrink_t<ufunc_args_t...>,simd_tag_t,data_t> : simd_op_t<simd_tag_t,data_t>
template <typename simd_tag_t, typename data_t, typename lambda_t>
struct ufunc_simd_t<view::hardshrink_t<lambda_t>,simd_tag_t,data_t> : simd_op_t<simd_tag_t,data_t>
{
using simd_t = simd_op_t<simd_tag_t,data_t>;
using simd_dtype_t = decltype(simd_t::set1(0));
using ufunc_op_t = view::hardshrink_t<ufunc_args_t...>;
using ufunc_op_t = view::hardshrink_t<lambda_t>;

const simd_dtype_t zero = simd_t::set1(0);
const simd_dtype_t pos_lambda;
Expand Down Expand Up @@ -340,12 +340,12 @@ namespace nmtools::array::simd
}
};

template <typename simd_tag_t, typename data_t, typename...op_args_t>
struct ufunc_simd_t<view::add_t<op_args_t...>,simd_tag_t,data_t> : simd_op_t<simd_tag_t,data_t>
template <typename simd_tag_t, typename data_t, typename lhs_t, typename rhs_t, typename res_t>
struct ufunc_simd_t<view::add_t<lhs_t,rhs_t,res_t>,simd_tag_t,data_t> : simd_op_t<simd_tag_t,data_t>
{
using simd_t = simd_op_t<simd_tag_t,data_t>;
using simd_dtype_t = decltype(simd_t::set1(data_t{0}));
using ufunc_op_t = view::add_t<op_args_t...>;
using ufunc_op_t = view::add_t<lhs_t,rhs_t,res_t>;

ufunc_simd_t([[maybe_unused]] ufunc_op_t op) {}

Expand Down
31 changes: 6 additions & 25 deletions include/nmtools/array/functional/activations/celu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,20 @@

#include "nmtools/array/functional/functor.hpp"
#include "nmtools/array/view/activations/celu.hpp"
#include "nmtools/array/functional/ufunc/ufunc.hpp"

namespace nmtools::functional
{
namespace fun
{
struct celu_t
{
template <typename...args_t>
constexpr auto operator()(const args_t&...args) const
{
return view::celu(args...);
}
};
using celu = fun::unary_ufunc<view::celu_t<>>;
}
constexpr inline auto celu = functor_t(unary_fmap_t<fun::celu_t>{});

template <typename alpha_t, typename ...arrays_t>
struct get_function_t<
view::decorator_t<
view::ufunc_t, view::celu_t<alpha_t>, arrays_t...
>
> {
using view_type = view::decorator_t<
view::ufunc_t, view::celu_t<alpha_t>, arrays_t...
>;
// we want celu[alpha] to works, so do not use fun::unary_ufunc<view::celu_t<>>;
constexpr inline auto celu = functor_t(unary_fmap_t<fun::celu>{});

view_type view;

constexpr auto operator()() const noexcept
{
return celu[view.op.alpha];
}
};
// using lambda breaks boost type index: Type name demangling failed
// constexpr inline auto celu = functor_t(unary_fmap_t<decltype(celu_fun)>{celu_fun});
} // namespace nmtools::functional


Expand Down
30 changes: 3 additions & 27 deletions include/nmtools/array/functional/activations/elu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,16 @@

#include "nmtools/array/functional/functor.hpp"
#include "nmtools/array/view/activations/elu.hpp"
#include "nmtools/array/functional/ufunc/ufunc.hpp"

namespace nmtools::functional
{
namespace fun
{
struct elu_t
{
template <typename ...args_t>
constexpr auto operator()(const args_t&...args) const
{
return view::elu(args...);
}
};
using elu = fun::unary_ufunc<view::elu_t<>>;
}

constexpr inline auto elu = functor_t(unary_fmap_t<fun::elu_t>{});

template <typename alpha_t, typename...arrays_t>
struct get_function_t<
view::decorator_t<
view::ufunc_t, view::elu_t<alpha_t>, arrays_t...
>
> {
using view_type = view::decorator_t<
view::ufunc_t, view::elu_t<alpha_t>, arrays_t...
>;

view_type view;

constexpr auto operator()() const noexcept
{
return elu[view.op.alpha];
}
};
constexpr inline auto elu = functor_t(unary_fmap_t<fun::elu>{});
} // namespace nmtools::functional


Expand Down
30 changes: 3 additions & 27 deletions include/nmtools/array/functional/activations/hardshrink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,16 @@

#include "nmtools/array/functional/functor.hpp"
#include "nmtools/array/view/activations/hardshrink.hpp"
#include "nmtools/array/functional/ufunc/ufunc.hpp"

namespace nmtools::functional
{
namespace fun
{
struct hardshrink_t
{
template <typename...args_t>
constexpr auto operator()(const args_t&...args) const
{
return view::hardshrink(args...);
}
};
using hardshrink = fun::unary_ufunc<view::hardshrink_t<>>;
}

constexpr inline auto hardshrink = functor_t(unary_fmap_t<fun::hardshrink_t>{});

template <typename lambda_t, typename...arrays_t>
struct get_function_t<
view::decorator_t<
view::ufunc_t, view::hardshrink_t<lambda_t>, arrays_t...
>
> {
using view_type = view::decorator_t<
view::ufunc_t, view::hardshrink_t<lambda_t>, arrays_t...
>;

view_type view;

constexpr auto operator()() const noexcept
{
return hardshrink[view.op.lambda];
}
};
constexpr inline auto hardshrink = functor_t(unary_fmap_t<fun::hardshrink>{});
} // namespace nmtools::functional


Expand Down
30 changes: 3 additions & 27 deletions include/nmtools/array/functional/activations/hardswish.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,16 @@

#include "nmtools/array/functional/functor.hpp"
#include "nmtools/array/view/activations/hardswish.hpp"
#include "nmtools/array/functional/ufunc/ufunc.hpp"

namespace nmtools::functional
{
namespace fun
{
struct hardswish_t
{
template <typename...args_t>
constexpr auto operator()(const args_t&...args) const
{
return view::hardswish(args...);
}
};
using hardswish = fun::unary_ufunc<view::hardswish_t>;
}

constexpr inline auto hardswish = functor_t(unary_fmap_t<fun::hardswish_t>{});

template <typename...arrays_t>
struct get_function_t<
view::decorator_t<
view::ufunc_t, view::hardswish_t, arrays_t...
>
> {
using view_type = view::decorator_t<
view::ufunc_t, view::hardswish_t, arrays_t...
>;

view_type view;

constexpr auto operator()() const noexcept
{
return hardswish;
}
};
constexpr inline auto hardswish = functor_t(unary_fmap_t<fun::hardswish>{});
} // namespace nmtools::functional


Expand Down
Loading

0 comments on commit 08e3733

Please sign in to comment.