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