fix(math): fix critical tensor/expression mistakes
This commit is contained in:
+39
-13
@@ -12,6 +12,7 @@ namespace cenet {
|
|||||||
namespace math {
|
namespace math {
|
||||||
|
|
||||||
struct expression_tag {};
|
struct expression_tag {};
|
||||||
|
struct expression_node_tag : expression_tag {};
|
||||||
|
|
||||||
template <typename E, typename Op>
|
template <typename E, typename Op>
|
||||||
struct unary_expression;
|
struct unary_expression;
|
||||||
@@ -23,31 +24,53 @@ template <typename CRTP>
|
|||||||
struct ops_provider {
|
struct ops_provider {
|
||||||
template <typename R>
|
template <typename R>
|
||||||
binary_expression<CRTP, R, ops::matmul> matmul(R&& rhs) {
|
binary_expression<CRTP, R, ops::matmul> matmul(R&& rhs) {
|
||||||
if (!ops::matmul::compatible(*crtp_this(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||||
return { *crtp_this(), std::forward<R>(rhs) };
|
return { self(), std::forward<R>(rhs) };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Func>
|
template <typename Func>
|
||||||
unary_expression<CRTP, ops::apply<Func>> apply(Func&& func) {
|
unary_expression<CRTP, ops::apply<Func>> apply(Func&& func) {
|
||||||
if (!ops::apply<Func>::compatible(*crtp_this())) throw std::runtime_error("tensor shape mismatch");
|
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||||
return { *crtp_this(), std::forward<Func>(func) };
|
return { self(), std::forward<Func>(func) };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename R, typename Func>
|
template <typename R, typename Func>
|
||||||
binary_expression<CRTP, R, ops::apply<Func>> apply(R&& rhs, Func&& func) {
|
binary_expression<CRTP, R, ops::apply<Func>> apply(R&& rhs, Func&& func) {
|
||||||
if (!ops::apply<Func>::compatible(*crtp_this(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||||
return { *crtp_this(), std::forward<R>(rhs), std::forward<Func>(func) };
|
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
decltype(auto) crtp_this() { return reinterpret_cast<CRTP*>(this); }
|
decltype(auto) self() & { return reinterpret_cast<CRTP&>(*this); }
|
||||||
|
decltype(auto) self() && { return reinterpret_cast<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;
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
template <typename E, typename Op>
|
template <typename E, typename Op>
|
||||||
struct unary_expression : expression_tag, ops_provider<unary_expression<E, Op>> {
|
struct unary_expression : expression_node_tag, ops_provider<unary_expression<E, Op>> {
|
||||||
E expr;
|
using expr_type = detail::operand_storage_t<E>;
|
||||||
|
|
||||||
|
expr_type expr;
|
||||||
Op op;
|
Op op;
|
||||||
|
|
||||||
template <typename EFwd = E, typename OpFwd = Op, typename = std::enable_if_t<std::is_constructible_v<E, EFwd>>>
|
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
|
unary_expression(EFwd&& expr, OpFwd&& op = Op{}) // NOLINT
|
||||||
: expr(std::forward<EFwd>(expr)), op(std::forward<OpFwd>(op)) {}
|
: expr(std::forward<EFwd>(expr)), op(std::forward<OpFwd>(op)) {}
|
||||||
|
|
||||||
@@ -57,9 +80,12 @@ struct unary_expression : expression_tag, ops_provider<unary_expression<E, Op>>
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <typename L, typename R, typename Op>
|
template <typename L, typename R, typename Op>
|
||||||
struct binary_expression : expression_tag, ops_provider<binary_expression<L, R, Op>> {
|
struct binary_expression : expression_node_tag, ops_provider<binary_expression<L, R, Op>> {
|
||||||
L lhs;
|
using lhs_type = detail::operand_storage_t<L>;
|
||||||
R rhs;
|
using rhs_type = detail::operand_storage_t<R>;
|
||||||
|
|
||||||
|
lhs_type lhs;
|
||||||
|
rhs_type rhs;
|
||||||
Op op;
|
Op op;
|
||||||
|
|
||||||
template <typename LFwd = L, typename RFwd = R, typename OpFwd = Op>
|
template <typename LFwd = L, typename RFwd = R, typename OpFwd = Op>
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ struct matmul {
|
|||||||
constexpr std::size_t rhs_cols = rhs_shape::template sget<1>();
|
constexpr std::size_t rhs_cols = rhs_shape::template sget<1>();
|
||||||
|
|
||||||
if constexpr (lhs_rows == dynamic_extent || rhs_cols == dynamic_extent) {
|
if constexpr (lhs_rows == dynamic_extent || rhs_cols == dynamic_extent) {
|
||||||
return dynamic_shape<lhs_rows, rhs_cols>{};
|
return dynamic_shape<lhs_rows, rhs_cols>{ lhs_shape::template get<0>(), rhs_shape::template get<1>() };
|
||||||
} else {
|
} else {
|
||||||
return static_shape<lhs_rows, rhs_cols>{};
|
return static_shape<lhs_rows, rhs_cols>{};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,8 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
template <typename E, typename = std::enable_if_t<detail::is_expression_v<E>>>
|
template <typename E,
|
||||||
|
typename = std::enable_if_t<detail::is_expression_v<E> && !std::is_same_v<std::decay_t<E>, tensor>>>
|
||||||
tensor(E&& exprFwd) { // NOLINT
|
tensor(E&& exprFwd) { // NOLINT
|
||||||
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user