Files
cenet/include/tulip/expr.hpp
T

301 lines
11 KiB
C++

#ifndef TULIP_EXPR_HPP
#define TULIP_EXPR_HPP
#include "./ops.hpp"
#include "./tensor_traits.hpp"
#include "./type_traits.hpp"
#include <stdexcept>
#include <type_traits>
#include <utility>
namespace tulip {
struct expression_tag {};
struct expression_node_tag : expression_tag {};
template <typename E, typename Op>
struct unary_expression;
template <typename L, typename R, typename Op>
struct binary_expression;
template <typename CRTP>
struct ops_provider {
template <typename R>
binary_expression<CRTP&, R, ops::matmul> matmul(R&& rhs) & {
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<R>(rhs) };
}
template <typename R>
binary_expression<const CRTP&, R, ops::matmul> matmul(R&& rhs) const& {
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<R>(rhs) };
}
template <typename R>
binary_expression<CRTP&&, R, ops::matmul> matmul(R&& rhs) && {
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<R>(rhs) };
}
template <typename R>
binary_expression<const CRTP&&, R, ops::matmul> matmul(R&& rhs) const&& {
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<R>(rhs) };
}
template <typename Func>
unary_expression<CRTP&, ops::apply<Func>> apply(Func&& func) & {
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<Func>(func) };
}
template <typename Func>
unary_expression<const CRTP&, ops::apply<Func>> apply(Func&& func) const& {
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<Func>(func) };
}
template <typename Func>
unary_expression<CRTP&&, ops::apply<Func>> apply(Func&& func) && {
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<Func>(func) };
}
template <typename Func>
unary_expression<const CRTP&&, ops::apply<Func>> apply(Func&& func) const&& {
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<Func>(func) };
}
template <typename R, typename Func>
binary_expression<CRTP&, R, ops::apply<Func>> apply(R&& rhs, Func&& func) & {
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
}
template <typename R, typename Func>
binary_expression<const CRTP&, R, ops::apply<Func>> apply(R&& rhs, Func&& func) const& {
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
}
template <typename R, typename Func>
binary_expression<CRTP&&, R, ops::apply<Func>> apply(R&& rhs, Func&& func) && {
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
}
template <typename R, typename Func>
binary_expression<const CRTP&&, R, ops::apply<Func>> apply(R&& rhs, Func&& func) const&& {
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
}
unary_expression<CRTP&, ops::transpose> transpose() & {
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self() };
}
unary_expression<const CRTP&, ops::transpose> transpose() const& {
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self() };
}
unary_expression<CRTP&&, ops::transpose> transpose() && {
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self() };
}
unary_expression<const CRTP&&, ops::transpose> transpose() const&& {
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self() };
}
template <typename S>
unary_expression<CRTP&, ops::reshape<S>> reshape() & {
if (!ops::reshape<S>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self() };
}
template <typename S>
unary_expression<const CRTP&, ops::reshape<S>> reshape() const& {
if (!ops::reshape<S>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self() };
}
template <typename S>
unary_expression<CRTP&&, ops::reshape<S>> reshape() && {
if (!ops::reshape<S>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self() };
}
template <typename S>
unary_expression<const CRTP&&, ops::reshape<S>> reshape() const&& {
if (!ops::reshape<S>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self() };
}
private:
decltype(auto) self() & { return static_cast<CRTP&>(*this); }
decltype(auto) self() && { return static_cast<CRTP&&>(*this); }
decltype(auto) self() const& { return static_cast<const CRTP&>(*this); }
decltype(auto) self() const&& { return static_cast<const CRTP&&>(*this); }
};
namespace detail {
template <typename T>
struct operand_storage {
using raw_type = remove_cvref_t<T>;
using type = std::conditional_t<std::is_base_of_v<expression_node_tag, raw_type>,
raw_type,
std::conditional_t<std::is_lvalue_reference_v<T>, T, raw_type>>;
};
template <typename T>
using operand_storage_t = typename operand_storage<T>::type;
template <typename T, typename Isa, typename Operand, typename = void>
struct is_pack_operand : std::bool_constant<std::is_arithmetic_v<remove_cvref_t<Operand>>> {};
template <typename T, typename Isa, typename Operand>
struct is_pack_operand<T,
Isa,
Operand,
std::void_t<decltype(std::declval<const remove_cvref_t<Operand>&>().template eval_pack<Isa>(std::size_t{}))>>
: std::is_same<decltype(std::declval<const remove_cvref_t<Operand>&>().template eval_pack<Isa>(std::size_t{})),
simd::detail::pack_t<T, Isa>> {};
template <typename Op, typename T, typename Isa, typename = void>
struct has_pack_apply : std::false_type {};
template <typename Op, typename T, typename Isa>
struct has_pack_apply<Op,
T,
Isa,
std::void_t<decltype(std::declval<const Op&>().template apply_pack<T, Isa>(
std::declval<simd::detail::pack_t<T, Isa>>(),
std::declval<simd::detail::pack_t<T, Isa>>()))>> : std::true_type {};
template <typename T, typename Isa, typename Operand>
auto eval_pack_operand(const Operand& operand, std::size_t index) {
if constexpr (std::is_arithmetic_v<remove_cvref_t<Operand>>) {
return simd::detail::broadcast<T, Isa>(static_cast<T>(operand));
} else {
return operand.template eval_pack<Isa>(index);
}
}
} // namespace detail
template <typename E, typename Op>
struct unary_expression : expression_node_tag, ops_provider<unary_expression<E, Op>> {
using expr_type = detail::operand_storage_t<E>;
expr_type expr;
Op op;
template <typename EFwd = E,
typename OpFwd = Op,
typename = std::enable_if_t<std::is_constructible_v<expr_type, EFwd&&> &&
!std::is_same_v<std::decay_t<EFwd>, unary_expression>>>
unary_expression(EFwd&& expr, OpFwd&& op = Op{}) // NOLINT
: expr(std::forward<EFwd>(expr)), op(std::forward<OpFwd>(op)) {}
constexpr auto shape() const { return Op::shape(expr); }
auto coeff(std::size_t index) const { return op(index, std::as_const(expr)); }
};
template <typename L, typename R, typename Op>
struct binary_expression : expression_node_tag, ops_provider<binary_expression<L, R, Op>> {
using lhs_type = detail::operand_storage_t<L>;
using rhs_type = detail::operand_storage_t<R>;
lhs_type lhs;
rhs_type rhs;
Op op;
template <typename LFwd = L, typename RFwd = R, typename OpFwd = Op>
binary_expression(LFwd&& lhs, RFwd&& rhs, OpFwd&& op = Op{})
: lhs(std::forward<LFwd>(lhs)), rhs(std::forward<RFwd>(rhs)), op(std::forward<OpFwd>(op)) {}
constexpr auto shape() const { return Op::shape(lhs, rhs); }
auto coeff(std::size_t index) const { return op(index, std::as_const(lhs), std::as_const(rhs)); }
using value_type = remove_cvref_t<decltype(std::declval<const binary_expression&>().coeff(std::size_t{}))>;
template <typename Isa,
typename = std::enable_if_t<detail::is_pack_operand<value_type, Isa, lhs_type>::value &&
detail::is_pack_operand<value_type, Isa, rhs_type>::value &&
detail::has_pack_apply<Op, value_type, Isa>::value>>
auto eval_pack(std::size_t index) const {
return op.template apply_pack<value_type, Isa>(detail::eval_pack_operand<value_type, Isa>(lhs, index),
detail::eval_pack_operand<value_type, Isa>(rhs, index));
}
};
namespace detail {
template <typename T, typename = void>
struct operand {
using type = T;
};
template <typename T>
struct operand<T, std::void_t<typename remove_cvref_t<T>::view_type>> {
using type = typename remove_cvref_t<T>::view_type;
};
template <typename T>
using operand_t = typename operand<T>::type;
template <typename T>
static constexpr bool is_expression_v = std::is_base_of_v<expression_tag, remove_cvref_t<T>>;
} // namespace detail
template <typename L, typename R, typename Op = ops::plus>
binary_expression<L, R, Op> operator+(L&& lhs, R&& rhs) {
// TODO: introduce a better exception class
if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch");
return { std::forward<L>(lhs), std::forward<R>(rhs) };
}
template <typename L, typename R, typename Op = ops::minus>
binary_expression<L, R, Op> operator-(L&& lhs, R&& rhs) {
if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch");
return { std::forward<L>(lhs), std::forward<R>(rhs) };
}
template <typename L, typename R, typename Op = ops::multiply>
binary_expression<L, R, Op> operator*(L&& lhs, R&& rhs) {
if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch");
return { std::forward<L>(lhs), std::forward<R>(rhs) };
}
template <typename L, typename R, typename Op = ops::divide>
binary_expression<L, R, Op> operator/(L&& lhs, R&& rhs) {
if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch");
return { std::forward<L>(lhs), std::forward<R>(rhs) };
}
template <typename T>
constexpr decltype(auto) eval(T&& fwd) {
if constexpr (detail::is_expression_v<T>) {
auto tensor = tensor_from_shape<decltype(fwd.coeff(std::declval<std::size_t>()))>(fwd.shape());
tensor = std::forward<T>(fwd);
return tensor;
} else {
return std::forward<T>(fwd);
}
}
} // namespace tulip
#endif // TULIP_EXPR_HPP