Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c96d31a378
|
||
|
|
795915d100
|
||
|
|
377cd5941d
|
+44
-14
@@ -12,6 +12,7 @@ namespace cenet {
|
||||
namespace math {
|
||||
|
||||
struct expression_tag {};
|
||||
struct expression_node_tag : expression_tag {};
|
||||
|
||||
template <typename E, typename Op>
|
||||
struct unary_expression;
|
||||
@@ -23,43 +24,72 @@ template <typename CRTP>
|
||||
struct ops_provider {
|
||||
template <typename R>
|
||||
binary_expression<CRTP, R, ops::matmul> matmul(R&& rhs) {
|
||||
if (!ops::matmul::compatible(*crtp_this(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { *crtp_this(), std::forward<R>(rhs) };
|
||||
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(*crtp_this())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { *crtp_this(), std::forward<Func>(func) };
|
||||
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(*crtp_this(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { *crtp_this(), std::forward<R>(rhs), std::forward<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) };
|
||||
}
|
||||
|
||||
unary_expression<CRTP, ops::transpose> transpose() {
|
||||
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
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>
|
||||
struct unary_expression : expression_tag, ops_provider<unary_expression<E, Op>> {
|
||||
E expr;
|
||||
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<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
|
||||
: 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)); }
|
||||
};
|
||||
|
||||
template <typename L, typename R, typename Op>
|
||||
struct binary_expression : expression_tag, ops_provider<binary_expression<L, R, Op>> {
|
||||
L lhs;
|
||||
R rhs;
|
||||
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>
|
||||
|
||||
@@ -94,7 +94,7 @@ struct matmul {
|
||||
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_rows, rhs_cols>{};
|
||||
return dynamic_shape<lhs_rows, rhs_cols>{ lhs_shape::template get<0>(), rhs_shape::template get<1>() };
|
||||
} else {
|
||||
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 math
|
||||
} // namespace cenet
|
||||
|
||||
@@ -87,7 +87,8 @@ public:
|
||||
return *this;
|
||||
}
|
||||
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
|
||||
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||
|
||||
|
||||
@@ -151,6 +151,13 @@ struct is_static_shape : std::false_type {};
|
||||
template <std::size_t... Dims>
|
||||
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>
|
||||
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...>>>;
|
||||
};
|
||||
|
||||
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
|
||||
|
||||
template <std::size_t... Dims>
|
||||
using shape_of = typename detail::shape_of_impl<Dims...>::type;
|
||||
|
||||
template <template <std::size_t...> class L,
|
||||
std::size_t... LDims,
|
||||
template <std::size_t...> class R,
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
@@ -99,6 +100,16 @@ public:
|
||||
}
|
||||
|
||||
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:
|
||||
value_type& coeff(std::size_t index) { return m_view[index]; }
|
||||
const value_type& coeff(std::size_t index) const { return m_view[index]; }
|
||||
|
||||
Reference in New Issue
Block a user