Compare commits

...
3 Commits
5 changed files with 105 additions and 19 deletions
+47 -17
View File
@@ -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,44 +24,73 @@ 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) };
}
unary_expression<CRTP, ops::transpose> transpose() {
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self() };
} }
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); }
}; };
template <typename E, typename Op> namespace detail {
struct unary_expression : expression_tag, ops_provider<unary_expression<E, Op>> {
E expr;
Op op;
template <typename EFwd = E, typename OpFwd = Op, typename = std::enable_if_t<std::is_constructible_v<E, EFwd>>> 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>
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 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)) {}
constexpr auto shape() const { return expr.shape(); } constexpr auto shape() const { return Op::shape(expr); }
auto coeff(std::size_t index) const { return op(index, std::as_const(expr)); } auto coeff(std::size_t index) const { return op(index, std::as_const(expr)); }
}; };
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>;
Op op;
lhs_type lhs;
rhs_type rhs;
Op op;
template <typename LFwd = L, typename RFwd = R, typename OpFwd = Op> template <typename LFwd = L, typename RFwd = R, typename OpFwd = Op>
binary_expression(LFwd&& lhs, RFwd&& rhs, OpFwd&& op = Op{}) binary_expression(LFwd&& lhs, RFwd&& rhs, OpFwd&& op = Op{})
+30 -1
View File
@@ -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>{};
} }
@@ -171,6 +171,35 @@ struct apply {
} }
}; };
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 cols = expr.shape().template get<1>();
return expr.coeff(((index % cols) * cols) + (index / cols));
}
};
} // namespace ops } // namespace ops
} // namespace math } // namespace math
} // namespace cenet } // namespace cenet
+2 -1
View File
@@ -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");
+15
View File
@@ -151,6 +151,13 @@ struct is_static_shape : std::false_type {};
template <std::size_t... Dims> template <std::size_t... Dims>
struct is_static_shape<static_shape<Dims...>> : std::true_type {}; struct is_static_shape<static_shape<Dims...>> : std::true_type {};
template <std::size_t... Dims>
struct is_static_shape_var : std::true_type {};
template <std::size_t First, std::size_t... Rest>
struct is_static_shape_var<First, Rest...>
: std::conditional_t<(First == dynamic_extent), std::false_type, is_static_shape_var<Rest...>> {};
template <typename T> template <typename T>
inline constexpr auto is_static_shape_v = is_static_shape<T>::value; inline constexpr auto is_static_shape_v = is_static_shape<T>::value;
@@ -209,8 +216,16 @@ struct best_shape<L<LDims...>, R<RDims...>> {
using type = std::enable_if_t<valid, std::conditional_t<left, L<LDims...>, R<RDims...>>>; using type = std::enable_if_t<valid, std::conditional_t<left, L<LDims...>, R<RDims...>>>;
}; };
template <std::size_t... Dims>
struct shape_of_impl {
using type = std::conditional_t<is_static_shape_var<Dims...>::value, static_shape<Dims...>, dynamic_shape<Dims...>>;
};
} // namespace detail } // namespace detail
template <std::size_t... Dims>
using shape_of = typename detail::shape_of_impl<Dims...>::type;
template <template <std::size_t...> class L, template <template <std::size_t...> class L,
std::size_t... LDims, std::size_t... LDims,
template <std::size_t...> class R, template <std::size_t...> class R,
+11
View File
@@ -8,6 +8,7 @@
#include <cstddef> #include <cstddef>
#include <cstring> #include <cstring>
#include <random>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
@@ -99,6 +100,16 @@ public:
} }
void fill(const tensor_view& view) { std::memcpy(m_view, view.m_view, size() * sizeof(*m_view)); } void fill(const tensor_view& view) { std::memcpy(m_view, view.m_view, size() * sizeof(*m_view)); }
template <typename Dist>
void fill_random(Dist dist) {
static std::random_device s_rd;
static std::mt19937 s_mt(s_rd());
for (std::size_t i = 0; i < size(); ++i) {
m_view[i] = dist(s_mt);
}
}
public: public:
value_type& coeff(std::size_t index) { return m_view[index]; } value_type& coeff(std::size_t index) { return m_view[index]; }
const value_type& coeff(std::size_t index) const { return m_view[index]; } const value_type& coeff(std::size_t index) const { return m_view[index]; }