Files
cenet/include/tulip/ops.hpp
T

245 lines
7.9 KiB
C++

#ifndef TULIP_OPS_HPP
#define TULIP_OPS_HPP
#include "./simd/traits.hpp"
#include "./tensor_fwd.hpp"
#include "./tensor_shape.hpp"
#include "./type_traits.hpp"
#include <cstddef>
#include <functional>
#include <stdexcept>
#include <type_traits>
namespace tulip {
namespace ops {
namespace detail {
template <typename T, typename = void>
struct has_shape : std::false_type {};
template <typename T>
struct has_shape<T, std::void_t<decltype(std::declval<T>().shape())>> : std::true_type {};
} // namespace detail
template <typename Op>
struct generic_binary_op {
template <typename L, typename R>
static constexpr bool compatible(const L& lhs, const R& rhs) {
if constexpr (std::is_arithmetic_v<L> || std::is_arithmetic_v<R>) {
return true;
} else {
return lhs.shape() == rhs.shape();
}
}
template <typename L, typename R>
static constexpr auto shape(const L& lhs, const R& rhs) {
if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands");
using best_shape = tulip::detail::best_shape<decltype(shape_wrapper(lhs)), decltype(shape_wrapper(rhs))>;
static_assert(best_shape::valid, "uncompatible operands");
if constexpr (best_shape::left) {
return lhs.shape();
} else {
return rhs.shape();
}
}
template <typename L, typename R>
constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const {
if constexpr (std::is_arithmetic_v<L>) {
return Op{}(lhs, rhs.coeff(index));
} else if constexpr (std::is_arithmetic_v<R>) {
return Op{}(lhs.coeff(index), rhs);
} else {
return Op{}(lhs.coeff(index), rhs.coeff(index));
}
}
private:
template <typename T>
static constexpr auto shape_wrapper(T&& fwd) {
if constexpr (detail::has_shape<T>::value) {
return std::forward<T>(fwd).shape();
} else {
return std::forward<T>(fwd);
}
}
};
struct plus : generic_binary_op<std::plus<>> {
template <typename T, typename Isa>
static auto apply_pack(simd::detail::pack_t<T, Isa> lhs, simd::detail::pack_t<T, Isa> rhs) noexcept {
return simd::detail::add<T, Isa>(lhs, rhs);
}
};
struct minus : generic_binary_op<std::minus<>> {
template <typename T, typename Isa>
static auto apply_pack(simd::detail::pack_t<T, Isa> lhs, simd::detail::pack_t<T, Isa> rhs) noexcept {
return simd::detail::sub<T, Isa>(lhs, rhs);
}
};
struct multiply : generic_binary_op<std::multiplies<>> {
template <typename T, typename Isa>
static auto apply_pack(simd::detail::pack_t<T, Isa> lhs, simd::detail::pack_t<T, Isa> rhs) noexcept {
return simd::detail::mul<T, Isa>(lhs, rhs);
}
};
struct divide : generic_binary_op<std::divides<>> {
template <typename T, typename Isa>
static auto apply_pack(simd::detail::pack_t<T, Isa> lhs, simd::detail::pack_t<T, Isa> rhs) noexcept {
return simd::detail::div<T, Isa>(lhs, rhs);
}
};
struct matmul {
template <typename L, typename R>
static constexpr bool compatible(const L& lhs, const R& rhs) {
using lhs_shape = remove_cvref_t<decltype(lhs.shape())>;
using rhs_shape = remove_cvref_t<decltype(rhs.shape())>;
return lhs_shape::rank == 2 && rhs_shape::rank == 2 &&
lhs.shape().template get<1>() == rhs.shape().template get<0>();
}
template <typename L, typename R>
static constexpr auto shape(const L& lhs, const R& rhs) {
if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands");
using lhs_shape = remove_cvref_t<decltype(lhs.shape())>;
using rhs_shape = remove_cvref_t<decltype(rhs.shape())>;
constexpr std::size_t lhs_rows = lhs_shape::template sget<0>();
constexpr std::size_t rhs_cols = rhs_shape::template sget<1>();
if constexpr (lhs_rows == dynamic_extent || rhs_cols == dynamic_extent) {
return dynamic_shape<lhs_rows, rhs_cols>{ lhs_shape::template get<0>(), rhs_shape::template get<1>() };
} else {
return static_shape<lhs_rows, rhs_cols>{};
}
}
template <typename L, typename R>
constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const {
const std::size_t outCols = shape(lhs, rhs).template get<1>();
const std::size_t row = index / outCols;
const std::size_t col = index % outCols;
auto lhsRows = lhs.shape().template get<0>();
auto lhsCols = lhs.shape().template get<1>();
auto rhsCols = rhs.shape().template get<1>();
decltype(lhs.coeff(0) * rhs.coeff(0)) acc{};
for (std::size_t i = 0; i < lhsCols; ++i) {
acc += lhs.coeff((row * lhsCols) + i) * rhs.coeff((i * rhsCols) + col);
}
return acc;
}
};
template <typename Func>
struct apply {
Func func;
template <typename Fwd = Func, typename = std::enable_if_t<std::is_constructible_v<Func, Fwd>>>
apply(Fwd&& fwd) // NOLINT
: func(std::forward<Fwd>(fwd)) {}
template <typename E>
static constexpr bool compatible(const E& expr) {
return true;
}
template <typename L, typename R>
static constexpr bool compatible(const L& lhs, const R& rhs) {
return lhs.shape() == rhs.shape();
}
template <typename E>
static constexpr auto shape(const E& expr) {
if (!compatible(expr)) throw std::runtime_error("uncompatible operands");
return expr.shape();
}
template <typename L, typename R>
static constexpr auto shape(const L& lhs, const R& rhs) {
if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands");
return lhs.shape();
}
template <typename E>
constexpr auto operator()(std::size_t index, const E& expr) const {
return func(expr.coeff(index));
}
template <typename L, typename R>
constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const {
return func(lhs.coeff(index), rhs.coeff(index));
}
};
struct transpose {
template <typename E>
static constexpr bool compatible(const E& expr) {
return true;
}
template <typename E>
static constexpr auto shape(const E& expr) {
if (!compatible(expr)) throw std::runtime_error("uncompatible operands");
using shape_type = remove_cvref_t<decltype(expr.shape())>;
constexpr std::size_t rows = shape_type::template sget<0>();
constexpr std::size_t cols = shape_type::template sget<1>();
if constexpr (rows == dynamic_extent || cols == dynamic_extent) {
return dynamic_shape<cols, rows>{ shape_type::template get<1>(), shape_type::template get<0>() };
} else {
return static_shape<cols, rows>{};
}
}
template <typename E>
constexpr auto operator()(std::size_t index, const E& expr) const {
const std::size_t rows = expr.shape().template get<0>();
const std::size_t cols = expr.shape().template get<1>();
return expr.coeff(((index % rows) * cols) + (index / rows));
}
};
// TODO: introduce dynamic reshape
template <typename S, typename = std::enable_if_t<::tulip::detail::is_static_shape_v<S>>>
struct reshape {
template <typename E>
static constexpr bool compatible(const E& expr) {
using shape_type = remove_cvref_t<decltype(expr.shape())>;
if constexpr (!::tulip::detail::is_static_shape<shape_type>::value) {
return false;
} else {
return shape_type::size == S::size;
}
}
template <typename E>
static constexpr auto shape(const E& expr) {
if (!compatible(expr)) throw std::runtime_error("uncompatible operands");
return S{};
}
template <typename E>
constexpr auto operator()(std::size_t index, const E& expr) const {
return expr.coeff(index);
}
};
} // namespace ops
} // namespace tulip
#endif // TULIP_OPS_HPP