#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 #include #include #include namespace tulip { namespace ops { namespace detail { template struct has_shape : std::false_type {}; template struct has_shape().shape())>> : std::true_type {}; } // namespace detail template struct generic_binary_op { template static constexpr bool compatible(const L& lhs, const R& rhs) { if constexpr (std::is_arithmetic_v || std::is_arithmetic_v) { return true; } else { return lhs.shape() == rhs.shape(); } } template 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; static_assert(best_shape::valid, "uncompatible operands"); if constexpr (best_shape::left) { return lhs.shape(); } else { return rhs.shape(); } } template constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const { if constexpr (std::is_arithmetic_v) { return Op{}(lhs, rhs.coeff(index)); } else if constexpr (std::is_arithmetic_v) { return Op{}(lhs.coeff(index), rhs); } else { return Op{}(lhs.coeff(index), rhs.coeff(index)); } } private: template static constexpr auto shape_wrapper(T&& fwd) { if constexpr (detail::has_shape::value) { return std::forward(fwd).shape(); } else { return std::forward(fwd); } } }; struct plus : generic_binary_op> { template static auto apply_pack(simd::detail::pack_t lhs, simd::detail::pack_t rhs) noexcept { return simd::detail::add(lhs, rhs); } }; struct minus : generic_binary_op> { template static auto apply_pack(simd::detail::pack_t lhs, simd::detail::pack_t rhs) noexcept { return simd::detail::sub(lhs, rhs); } }; struct multiply : generic_binary_op> { template static auto apply_pack(simd::detail::pack_t lhs, simd::detail::pack_t rhs) noexcept { return simd::detail::mul(lhs, rhs); } }; struct divide : generic_binary_op> { template static auto apply_pack(simd::detail::pack_t lhs, simd::detail::pack_t rhs) noexcept { return simd::detail::div(lhs, rhs); } }; struct matmul { template static constexpr bool compatible(const L& lhs, const R& rhs) { using lhs_shape = remove_cvref_t; using rhs_shape = remove_cvref_t; return lhs_shape::rank == 2 && rhs_shape::rank == 2 && lhs.shape().template get<1>() == rhs.shape().template get<0>(); } template 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; using rhs_shape = remove_cvref_t; 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_shape::template get<0>(), rhs_shape::template get<1>() }; } else { return static_shape{}; } } template 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 struct apply { Func func; template >> apply(Fwd&& fwd) // NOLINT : func(std::forward(fwd)) {} template static constexpr bool compatible(const E& expr) { return true; } template static constexpr bool compatible(const L& lhs, const R& rhs) { return lhs.shape() == rhs.shape(); } template static constexpr auto shape(const E& expr) { if (!compatible(expr)) throw std::runtime_error("uncompatible operands"); return expr.shape(); } template static constexpr auto shape(const L& lhs, const R& rhs) { if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands"); return lhs.shape(); } template constexpr auto operator()(std::size_t index, const E& expr) const { return func(expr.coeff(index)); } template 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 static constexpr bool compatible(const E& expr) { return true; } template static constexpr auto shape(const E& expr) { if (!compatible(expr)) throw std::runtime_error("uncompatible operands"); using shape_type = remove_cvref_t; 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{ shape_type::template get<1>(), shape_type::template get<0>() }; } else { return static_shape{}; } } template 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 >> struct reshape { template static constexpr bool compatible(const E& expr) { using shape_type = remove_cvref_t; if constexpr (!::tulip::detail::is_static_shape::value) { return false; } else { return shape_type::size == S::size; } } template static constexpr auto shape(const E& expr) { if (!compatible(expr)) throw std::runtime_error("uncompatible operands"); return S{}; } template constexpr auto operator()(std::size_t index, const E& expr) const { return expr.coeff(index); } }; } // namespace ops } // namespace tulip #endif // TULIP_OPS_HPP