Compare commits
10
Commits
09d23cd568
...
75243e9d08
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75243e9d08
|
||
|
|
3dfcb754f8
|
||
|
|
78864efba9
|
||
|
|
2267c8d7f8
|
||
|
|
673e068344
|
||
|
|
0eeafe31c4
|
||
|
|
8dee1cbe1f
|
||
|
|
de783ad7b7
|
||
|
|
3fca91e1ea
|
||
|
|
b482010c5d
|
+60
-51
@@ -2,8 +2,9 @@
|
|||||||
#define CENET_MATH_EXPR_HPP
|
#define CENET_MATH_EXPR_HPP
|
||||||
|
|
||||||
#include "ops.hpp"
|
#include "ops.hpp"
|
||||||
#include "tensor_traits.hpp"
|
#include "type_traits.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
@@ -13,42 +14,65 @@ namespace math {
|
|||||||
struct expression_tag {};
|
struct expression_tag {};
|
||||||
|
|
||||||
template <typename E, typename Op>
|
template <typename E, typename Op>
|
||||||
struct unary_expression : expression_tag {
|
struct unary_expression;
|
||||||
E expr;
|
|
||||||
|
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(*crtp_this(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||||
|
return { *crtp_this(), 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) };
|
||||||
|
}
|
||||||
|
|
||||||
|
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) };
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
decltype(auto) crtp_this() { return reinterpret_cast<CRTP*>(this); }
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename E, typename Op>
|
||||||
|
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>>>
|
||||||
|
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 expr.shape(); }
|
||||||
|
|
||||||
auto coeff(std::size_t index) const { return Op{}(expr.coeff(index)); }
|
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 {
|
struct binary_expression : expression_tag, ops_provider<binary_expression<L, R, Op>> {
|
||||||
L lhs;
|
L lhs;
|
||||||
R rhs;
|
R rhs;
|
||||||
|
Op op;
|
||||||
|
|
||||||
template <typename LFwd = L, typename RFwd = R>
|
template <typename LFwd = L, typename RFwd = R, typename OpFwd = Op>
|
||||||
binary_expression(LFwd&& lhs, RFwd&& rhs)
|
binary_expression(LFwd&& lhs, RFwd&& rhs, OpFwd&& op = Op{})
|
||||||
: lhs(std::forward<LFwd>(lhs)), rhs(std::forward<RFwd>(rhs)) {}
|
: lhs(std::forward<LFwd>(lhs)), rhs(std::forward<RFwd>(rhs)), op(std::forward<OpFwd>(op)) {}
|
||||||
|
|
||||||
constexpr auto shape() const { return Op::shape(lhs, rhs); }
|
constexpr auto shape() const { return Op::shape(lhs, rhs); }
|
||||||
|
|
||||||
auto coeff(std::size_t index) const { return Op{}(index, lhs.coeff(index), as_operand(rhs)); }
|
auto coeff(std::size_t index) const { return op(index, std::as_const(lhs), std::as_const(rhs)); }
|
||||||
private:
|
|
||||||
template <typename T>
|
|
||||||
static decltype(auto) as_operand(T&& value) {
|
|
||||||
if constexpr (is_tensor_v<T>) {
|
|
||||||
return std::as_const(value).view();
|
|
||||||
} else {
|
|
||||||
return std::forward<T>(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
|
||||||
|
|
||||||
template <typename T, typename = void>
|
template <typename T, typename = void>
|
||||||
struct operand {
|
struct operand {
|
||||||
using type = T;
|
using type = T;
|
||||||
@@ -65,45 +89,30 @@ using operand_t = typename operand<T>::type;
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
static constexpr bool is_expression_v = std::is_base_of_v<expression_tag, remove_cvref_t<T>>;
|
static constexpr bool is_expression_v = std::is_base_of_v<expression_tag, remove_cvref_t<T>>;
|
||||||
|
|
||||||
template <typename E>
|
|
||||||
struct expression_result {
|
|
||||||
using type = remove_cvref_t<E>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename E, typename Op>
|
|
||||||
struct expression_result<unary_expression<E, Op>> {
|
|
||||||
using type = typename Op::template result_type<operand_t<typename expression_result<remove_cvref_t<E>>::type>>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename L, typename R, typename Op>
|
|
||||||
struct expression_result<binary_expression<L, R, Op>> {
|
|
||||||
using type = typename Op::template result_type<operand_t<typename expression_result<remove_cvref_t<L>>::type>,
|
|
||||||
operand_t<typename expression_result<remove_cvref_t<R>>::type>>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template <typename L,
|
template <typename L, typename R, typename Op = ops::plus>
|
||||||
typename R,
|
|
||||||
typename Op = ops::plus,
|
|
||||||
typename = std::void_t<typename detail::expression_result<binary_expression<L, R, Op>>::type>>
|
|
||||||
binary_expression<L, R, Op> operator+(L&& lhs, R&& rhs) {
|
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) };
|
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename L,
|
template <typename L, typename R, typename Op = ops::minus>
|
||||||
typename R,
|
|
||||||
typename Op = ops::minus,
|
|
||||||
typename = std::void_t<typename detail::expression_result<binary_expression<L, R, Op>>::type>>
|
|
||||||
binary_expression<L, R, Op> operator-(L&& lhs, R&& rhs) {
|
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) };
|
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename L,
|
template <typename L, typename R, typename Op = ops::multiply>
|
||||||
typename R,
|
|
||||||
typename Op = ops::multiply,
|
|
||||||
typename = std::void_t<typename detail::expression_result<binary_expression<L, R, Op>>::type>>
|
|
||||||
binary_expression<L, R, Op> operator*(L&& lhs, R&& rhs) {
|
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) };
|
return { std::forward<L>(lhs), std::forward<R>(rhs) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+120
-78
@@ -1,10 +1,12 @@
|
|||||||
#ifndef CENET_MATH_OPS_HPP
|
#ifndef CENET_MATH_OPS_HPP
|
||||||
#define CENET_MATH_OPS_HPP
|
#define CENET_MATH_OPS_HPP
|
||||||
|
|
||||||
|
#include "tensor_fwd.hpp"
|
||||||
#include "tensor_shape.hpp"
|
#include "tensor_shape.hpp"
|
||||||
#include "tensor_traits.hpp"
|
#include "type_traits.hpp"
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <functional>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
@@ -12,120 +14,160 @@ namespace cenet {
|
|||||||
namespace math {
|
namespace math {
|
||||||
namespace ops {
|
namespace ops {
|
||||||
|
|
||||||
class plus {
|
namespace detail {
|
||||||
private:
|
|
||||||
|
template <typename T, typename = void>
|
||||||
|
struct has_shape : std::false_type {};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct has_shape<T, std::void_t<decltype(std::declval<T>().shape())>> : std::true_type {};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
template <typename Op>
|
||||||
|
struct generic_binary_op {
|
||||||
template <typename L, typename R>
|
template <typename L, typename R>
|
||||||
struct result_type_impl {};
|
static constexpr bool compatible(const L& lhs, const R& rhs) {
|
||||||
|
if constexpr (std::is_arithmetic_v<L> || std::is_arithmetic_v<R>) {
|
||||||
template <typename LT, std::size_t... LDims, typename RT, std::size_t... RDims>
|
return true;
|
||||||
struct result_type_impl<tensor_view<LT, LDims...>, tensor_view<RT, RDims...>> {
|
} else {
|
||||||
using lhs_view = tensor_view<LT, LDims...>;
|
return lhs.shape() == rhs.shape();
|
||||||
using rhs_view = tensor_view<RT, RDims...>;
|
}
|
||||||
|
|
||||||
using type = std::enable_if_t<
|
|
||||||
detail::is_shape_equivalent<typename lhs_view::shape_type, typename rhs_view::shape_type>::value,
|
|
||||||
tensor<decltype(std::declval<detach_storage_t<LT>>() + std::declval<detach_storage_t<RT>>()), LDims...>>;
|
|
||||||
};
|
|
||||||
public:
|
|
||||||
template <typename L, typename R>
|
|
||||||
using result_type = typename result_type_impl<L, R>::type;
|
|
||||||
|
|
||||||
template <typename L, typename R, typename = std::enable_if_t<(is_tensor_v<L> && is_tensor_v<R>)>>
|
|
||||||
static bool compatible(const L& lhs, const R& rhs) {
|
|
||||||
return lhs.shape() == rhs.shape();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename L, typename R>
|
template <typename L, typename R>
|
||||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape");
|
if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands");
|
||||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
using best_shape = cenet::math::detail::best_shape<decltype(shape_wrapper(lhs)), decltype(shape_wrapper(rhs))>;
|
||||||
|
static_assert(best_shape::valid, "uncompatible operands");
|
||||||
|
if constexpr (best_shape::left) {
|
||||||
return lhs.shape();
|
return lhs.shape();
|
||||||
} else {
|
} else {
|
||||||
return rhs.shape();
|
return rhs.shape();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename LT, typename R>
|
template <typename L, typename R>
|
||||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const {
|
||||||
return lhs + rhs.coeff(index);
|
if constexpr (std::is_arithmetic_v<L>) {
|
||||||
|
return Op{}(lhs, rhs.coeff(index));
|
||||||
|
} else if constexpr (std::is_arithmetic_v<R>) {
|
||||||
|
return Op{}(lhs.coeff(index), rhs);
|
||||||
|
} else {
|
||||||
|
return Op{}(lhs.coeff(index), rhs.coeff(index));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
template <typename T>
|
||||||
|
static constexpr auto shape_wrapper(T&& fwd) {
|
||||||
|
if constexpr (detail::has_shape<T>::value) {
|
||||||
|
return std::forward<T>(fwd).shape();
|
||||||
|
} else {
|
||||||
|
return std::forward<T>(fwd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class minus {
|
struct plus : generic_binary_op<std::plus<>> {};
|
||||||
private:
|
struct minus : generic_binary_op<std::minus<>> {};
|
||||||
|
struct multiply : generic_binary_op<std::multiplies<>> {};
|
||||||
|
struct divide : generic_binary_op<std::divides<>> {};
|
||||||
|
|
||||||
|
struct matmul {
|
||||||
template <typename L, typename R>
|
template <typename L, typename R>
|
||||||
struct result_type_impl {};
|
static constexpr bool compatible(const L& lhs, const R& rhs) {
|
||||||
|
using lhs_shape = remove_cvref_t<decltype(lhs.shape())>;
|
||||||
|
using rhs_shape = remove_cvref_t<decltype(rhs.shape())>;
|
||||||
|
|
||||||
template <typename LT, std::size_t... LDims, typename RT, std::size_t... RDims>
|
return lhs_shape::rank == 2 && rhs_shape::rank == 2 &&
|
||||||
struct result_type_impl<tensor_view<LT, LDims...>, tensor_view<RT, RDims...>> {
|
lhs.shape().template get<1>() == rhs.shape().template get<0>();
|
||||||
using lhs_view = tensor_view<LT, LDims...>;
|
|
||||||
using rhs_view = tensor_view<RT, RDims...>;
|
|
||||||
|
|
||||||
using type = std::enable_if_t<
|
|
||||||
detail::is_shape_equivalent<typename lhs_view::shape_type, typename rhs_view::shape_type>::value,
|
|
||||||
tensor<decltype(std::declval<detach_storage_t<LT>>() - std::declval<detach_storage_t<RT>>()), LDims...>>;
|
|
||||||
};
|
|
||||||
public:
|
|
||||||
template <typename L, typename R>
|
|
||||||
using result_type = typename result_type_impl<L, R>::type;
|
|
||||||
|
|
||||||
template <typename L, typename R, typename = std::enable_if_t<(is_tensor_v<L> && is_tensor_v<R>)>>
|
|
||||||
static bool compatible(const L& lhs, const R& rhs) {
|
|
||||||
return lhs.shape() == rhs.shape();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename L, typename R>
|
template <typename L, typename R>
|
||||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape");
|
if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands");
|
||||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
|
||||||
return lhs.shape();
|
using lhs_shape = remove_cvref_t<decltype(lhs.shape())>;
|
||||||
|
using rhs_shape = remove_cvref_t<decltype(rhs.shape())>;
|
||||||
|
|
||||||
|
constexpr std::size_t lhs_rows = lhs_shape::template sget<0>();
|
||||||
|
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>{};
|
||||||
} else {
|
} else {
|
||||||
return rhs.shape();
|
return static_shape<lhs_rows, rhs_cols>{};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename LT, typename R>
|
template <typename L, typename R>
|
||||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const {
|
||||||
return lhs - rhs.coeff(index);
|
const std::size_t outCols = shape(lhs, rhs).template get<1>();
|
||||||
|
const std::size_t row = index / outCols;
|
||||||
|
const std::size_t col = index % outCols;
|
||||||
|
|
||||||
|
auto lhsRows = lhs.shape().template get<0>();
|
||||||
|
auto lhsCols = lhs.shape().template get<1>();
|
||||||
|
auto rhsCols = rhs.shape().template get<1>();
|
||||||
|
|
||||||
|
decltype(lhs.coeff(0) * rhs.coeff(0)) acc{};
|
||||||
|
for (std::size_t i = 0; i < lhsCols; ++i) {
|
||||||
|
acc += lhs.coeff((row * lhsCols) + i) * rhs.coeff((i * rhsCols) + col);
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class multiply {
|
template <typename Func>
|
||||||
private:
|
struct apply {
|
||||||
|
Func func;
|
||||||
|
|
||||||
|
template <typename Fwd = Func, typename = std::enable_if_t<std::is_constructible_v<Func, Fwd>>>
|
||||||
|
apply(Fwd&& fwd) // NOLINT
|
||||||
|
: func(std::forward<Fwd>(fwd)) {}
|
||||||
|
|
||||||
|
template <typename E>
|
||||||
|
static constexpr bool compatible(const E& expr) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename L, typename R>
|
template <typename L, typename R>
|
||||||
struct result_type_impl {};
|
static constexpr bool compatible(const L& lhs, const R& rhs) {
|
||||||
|
|
||||||
template <typename LT, std::size_t... LDims, typename RT, std::size_t... RDims>
|
|
||||||
struct result_type_impl<tensor_view<LT, LDims...>, tensor_view<RT, RDims...>> {
|
|
||||||
using lhs_view = tensor_view<LT, LDims...>;
|
|
||||||
using rhs_view = tensor_view<RT, RDims...>;
|
|
||||||
|
|
||||||
using type = std::enable_if_t<
|
|
||||||
detail::is_shape_equivalent<typename lhs_view::shape_type, typename rhs_view::shape_type>::value,
|
|
||||||
tensor<decltype(std::declval<detach_storage_t<LT>>() * std::declval<detach_storage_t<RT>>()), LDims...>>;
|
|
||||||
};
|
|
||||||
public:
|
|
||||||
template <typename L, typename R>
|
|
||||||
using result_type = typename result_type_impl<L, R>::type;
|
|
||||||
|
|
||||||
template <typename L, typename R, typename = std::enable_if_t<(is_tensor_v<L> && is_tensor_v<R>)>>
|
|
||||||
static bool compatible(const L& lhs, const R& rhs) {
|
|
||||||
return lhs.shape() == rhs.shape();
|
return lhs.shape() == rhs.shape();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename E>
|
||||||
|
static constexpr auto shape(const E& expr) {
|
||||||
|
if (!compatible(expr)) throw std::runtime_error("uncompatible operands");
|
||||||
|
return expr.shape();
|
||||||
|
}
|
||||||
|
|
||||||
template <typename L, typename R>
|
template <typename L, typename R>
|
||||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape");
|
if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands");
|
||||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
|
||||||
return lhs.shape();
|
using lhs_shape = remove_cvref_t<decltype(lhs.shape())>;
|
||||||
|
using rhs_shape = remove_cvref_t<decltype(rhs.shape())>;
|
||||||
|
|
||||||
|
constexpr std::size_t lhs_rows = lhs_shape::template sget<0>();
|
||||||
|
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>{};
|
||||||
} else {
|
} else {
|
||||||
return rhs.shape();
|
return static_shape<lhs_rows, rhs_cols>{};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename LT, typename R>
|
template <typename E>
|
||||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
constexpr auto operator()(std::size_t index, const E& expr) const {
|
||||||
return lhs * rhs.coeff(index);
|
return func(expr.coeff(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename L, typename R>
|
||||||
|
constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const {
|
||||||
|
return func(lhs.coeff(index), rhs.coeff(index));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ namespace cenet {
|
|||||||
namespace math {
|
namespace math {
|
||||||
|
|
||||||
template <typename T, std::size_t... Dims>
|
template <typename T, std::size_t... Dims>
|
||||||
class tensor : public expression_tag {
|
class tensor : public expression_tag, public ops_provider<tensor<T, Dims...>> {
|
||||||
public:
|
public:
|
||||||
using traits = tensor_traits<tensor>;
|
using traits = tensor_traits<tensor>;
|
||||||
using value_type = typename traits::value_type;
|
using value_type = typename traits::value_type;
|
||||||
@@ -25,6 +25,9 @@ public:
|
|||||||
using view_type = tensor_view<value_type, Dims...>;
|
using view_type = tensor_view<value_type, Dims...>;
|
||||||
using constant_view_type = tensor_view<constant_value_type, Dims...>;
|
using constant_view_type = tensor_view<constant_value_type, Dims...>;
|
||||||
using shape_type = std::conditional_t<traits::dynamic, dynamic_shape<Dims...>, static_shape<Dims...>>;
|
using shape_type = std::conditional_t<traits::dynamic, dynamic_shape<Dims...>, static_shape<Dims...>>;
|
||||||
|
public:
|
||||||
|
template <typename ST>
|
||||||
|
using with_storage = typename traits::template with_storage_t<ST>;
|
||||||
public:
|
public:
|
||||||
tensor() {
|
tensor() {
|
||||||
if constexpr (traits::dynamic) {
|
if constexpr (traits::dynamic) {
|
||||||
@@ -86,11 +89,7 @@ public:
|
|||||||
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>>>
|
||||||
tensor(E&& exprFwd) { // NOLINT
|
tensor(E&& exprFwd) { // NOLINT
|
||||||
// If static shape check failed, check dynamically (requried for static = dynamic op dynamic).
|
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||||
if constexpr (!shape_type::template compatible<typename detail::expression_result<E>::type>()) {
|
|
||||||
// TODO: introduce better exceptions
|
|
||||||
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
|
||||||
}
|
|
||||||
|
|
||||||
if constexpr (!traits::inline_storage) {
|
if constexpr (!traits::inline_storage) {
|
||||||
if constexpr (traits::dynamic) {
|
if constexpr (traits::dynamic) {
|
||||||
@@ -108,10 +107,7 @@ 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>>>
|
||||||
tensor& operator=(E&& exprFwd) {
|
tensor& operator=(E&& exprFwd) {
|
||||||
// If static shape check failed, check dynamically (requried for static = dynamic op dynamic).
|
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||||
if constexpr (!shape_type::template compatible<typename detail::expression_result<E>::type>()) {
|
|
||||||
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
|
||||||
}
|
|
||||||
|
|
||||||
if constexpr (!traits::inline_storage) {
|
if constexpr (!traits::inline_storage) {
|
||||||
if constexpr (traits::dynamic) {
|
if constexpr (traits::dynamic) {
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ struct inline_storage {
|
|||||||
using value_type = T;
|
using value_type = T;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct heap_storage_tag {};
|
||||||
|
struct inline_storage_tag {};
|
||||||
|
|
||||||
template <typename T, std::size_t... Dims>
|
template <typename T, std::size_t... Dims>
|
||||||
class tensor_view;
|
class tensor_view;
|
||||||
|
|
||||||
|
|||||||
@@ -36,20 +36,18 @@ struct static_shape : shape<Dims...> {
|
|||||||
return compatible_impl<T>::value;
|
return compatible_impl<T>::value;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <std::size_t... RDims>
|
|
||||||
bool operator==(const static_shape<RDims...>& rhs) const {
|
|
||||||
return sizeof...(Dims) == sizeof...(RDims) && ((Dims == RDims) && ...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <std::size_t... RDims>
|
|
||||||
bool operator!=(const static_shape<RDims...>& rhs) const {
|
|
||||||
return sizeof...(Dims) != sizeof...(RDims) || ((Dims != RDims) || ...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <std::size_t I>
|
template <std::size_t I>
|
||||||
constexpr auto get() const {
|
constexpr auto get() const {
|
||||||
return get_impl<I, Dims...>::value;
|
return get_impl<I, Dims...>::value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <std::size_t I>
|
||||||
|
static constexpr auto sget() {
|
||||||
|
return get_impl<I, Dims...>::value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr auto rank = sizeof...(Dims);
|
||||||
|
static constexpr auto score = sizeof...(Dims);
|
||||||
private:
|
private:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct compatible_impl : std::false_type {};
|
struct compatible_impl : std::false_type {};
|
||||||
@@ -71,6 +69,12 @@ template <std::size_t... Dims>
|
|||||||
struct dynamic_shape : shape<Dims...> {
|
struct dynamic_shape : shape<Dims...> {
|
||||||
dynamic_shape() = default;
|
dynamic_shape() = default;
|
||||||
|
|
||||||
|
template <typename... RDims,
|
||||||
|
typename = std::enable_if_t<(sizeof...(RDims) == sizeof...(Dims)) && (std::is_integral_v<RDims> && ...)>>
|
||||||
|
explicit dynamic_shape(RDims... rtDimensions) {
|
||||||
|
store_impl<0>(rtDimensions...);
|
||||||
|
}
|
||||||
|
|
||||||
template <std::size_t ExtRank, typename = std::enable_if_t<(ExtRank >= sizeof...(Dims))>>
|
template <std::size_t ExtRank, typename = std::enable_if_t<(ExtRank >= sizeof...(Dims))>>
|
||||||
explicit dynamic_shape(const dynamic_shape<ExtRank>& oth) {
|
explicit dynamic_shape(const dynamic_shape<ExtRank>& oth) {
|
||||||
constexpr std::size_t ext_offset = ExtRank - sizeof...(Dims);
|
constexpr std::size_t ext_offset = ExtRank - sizeof...(Dims);
|
||||||
@@ -89,23 +93,16 @@ struct dynamic_shape : shape<Dims...> {
|
|||||||
return compatible_impl<T>::value;
|
return compatible_impl<T>::value;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <std::size_t... RDims>
|
|
||||||
bool operator==(const static_shape<RDims...>& rhs) const {
|
|
||||||
return sizeof...(Dims) == sizeof...(RDims) &&
|
|
||||||
compare_impl<decltype(rhs), RDims...>(std::make_index_sequence<sizeof...(Dims)>{}, rhs);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <std::size_t... RDims>
|
|
||||||
bool operator==(const dynamic_shape<RDims...>& rhs) const {
|
|
||||||
return sizeof...(Dims) == sizeof...(RDims) &&
|
|
||||||
compare_impl<decltype(rhs), RDims...>(std::make_index_sequence<sizeof...(Dims)>{}, rhs);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <std::size_t I>
|
template <std::size_t I>
|
||||||
constexpr auto get() const {
|
constexpr auto get() const {
|
||||||
return get_impl<I, Dims...>::value == dynamic_extent ? dimensions[I] : get_impl<I, Dims...>::value;
|
return get_impl<I, Dims...>::value == dynamic_extent ? dimensions[I] : get_impl<I, Dims...>::value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <std::size_t I>
|
||||||
|
static constexpr auto sget() {
|
||||||
|
return get_impl<I, Dims...>::value;
|
||||||
|
}
|
||||||
|
|
||||||
std::size_t dimensions[sizeof...(Dims)]{};
|
std::size_t dimensions[sizeof...(Dims)]{};
|
||||||
private:
|
private:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -115,13 +112,9 @@ private:
|
|||||||
struct compatible_impl<tensor<T, OthDims...>>
|
struct compatible_impl<tensor<T, OthDims...>>
|
||||||
: std::conditional_t<((Dims == dynamic_extent || Dims == OthDims) && ...), std::true_type, std::false_type> {};
|
: std::conditional_t<((Dims == dynamic_extent || Dims == OthDims) && ...), std::true_type, std::false_type> {};
|
||||||
|
|
||||||
template <template <std::size_t...> class L,
|
template <template <std::size_t...> class R, std::size_t... RDims, std::size_t... Is>
|
||||||
std::size_t... LDims,
|
constexpr bool compare_impl(std::index_sequence<Is...>, const R<RDims...>& rhs) const {
|
||||||
template <std::size_t...> class R,
|
return ((get<Is>() == rhs.template get<Is>()) && ...);
|
||||||
std::size_t... RDims,
|
|
||||||
std::size_t... Is>
|
|
||||||
static constexpr bool compare_impl(std::index_sequence<Is...>, const L<LDims...>& lhs, const R<RDims...>& rhs) {
|
|
||||||
return ((lhs.template get<Is>() == rhs.template get<Is>()) && ...);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <std::size_t I, std::size_t First, std::size_t... Rest>
|
template <std::size_t I, std::size_t First, std::size_t... Rest>
|
||||||
@@ -131,6 +124,23 @@ private:
|
|||||||
struct get_impl<0, First, Rest...> {
|
struct get_impl<0, First, Rest...> {
|
||||||
static constexpr auto value = First;
|
static constexpr auto value = First;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <std::size_t Score, std::size_t...>
|
||||||
|
struct score_impl {
|
||||||
|
static constexpr auto value = Score;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <std::size_t Score, std::size_t First, std::size_t... Rest>
|
||||||
|
struct score_impl<Score, First, Rest...> : score_impl<Score + ((First != dynamic_extent) ? 1 : 0), Rest...> {};
|
||||||
|
|
||||||
|
template <std::size_t I, typename RDim, typename... RDims, typename = std::enable_if_t<(I < sizeof...(Dims))>>
|
||||||
|
void store_impl(RDim first, RDims... rest) {
|
||||||
|
dimensions[I] = first;
|
||||||
|
store_impl<I + 1>(rest...);
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
static constexpr auto rank = sizeof...(Dims);
|
||||||
|
static constexpr auto score = score_impl<0, Dims...>::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
@@ -170,6 +180,35 @@ struct is_shape_equivalent<L<LFirst, LRest...>, R<RFirst, RRest...>>
|
|||||||
template <template <std::size_t...> class L, template <std::size_t...> class R>
|
template <template <std::size_t...> class L, template <std::size_t...> class R>
|
||||||
struct is_shape_equivalent<L<>, R<>> : std::true_type {};
|
struct is_shape_equivalent<L<>, R<>> : std::true_type {};
|
||||||
|
|
||||||
|
template <typename L, typename R, typename = void>
|
||||||
|
struct best_shape {};
|
||||||
|
|
||||||
|
template <template <std::size_t...> class T, std::size_t... Dims, typename O>
|
||||||
|
struct best_shape<T<Dims...>, O> {
|
||||||
|
using type = T<Dims...>;
|
||||||
|
|
||||||
|
static constexpr bool valid = true;
|
||||||
|
static constexpr bool left = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <template <std::size_t...> class T, std::size_t... Dims, typename O>
|
||||||
|
struct best_shape<O, T<Dims...>> {
|
||||||
|
using type = T<Dims...>;
|
||||||
|
|
||||||
|
static constexpr bool valid = true;
|
||||||
|
static constexpr bool left = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <template <std::size_t...> class L,
|
||||||
|
std::size_t... LDims,
|
||||||
|
template <std::size_t...> class R,
|
||||||
|
std::size_t... RDims>
|
||||||
|
struct best_shape<L<LDims...>, R<RDims...>> {
|
||||||
|
static constexpr bool valid = sizeof...(LDims) == sizeof...(RDims);
|
||||||
|
static constexpr bool left = L<RDims...>::score >= R<LDims...>::score;
|
||||||
|
using type = std::enable_if_t<valid, std::conditional_t<left, L<LDims...>, R<RDims...>>>;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template <template <std::size_t...> class L,
|
template <template <std::size_t...> class L,
|
||||||
|
|||||||
@@ -73,6 +73,25 @@ struct is_tensor_dynamic<tensor<T, dynamic_extent, Rest...>> : std::true_type {}
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
static constexpr auto is_tensor_dynamic_v = is_tensor_dynamic<T>::value;
|
static constexpr auto is_tensor_dynamic_v = is_tensor_dynamic<T>::value;
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
template <typename>
|
||||||
|
struct tensor_with_storage {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct tensor_with_storage<heap_storage_tag> {
|
||||||
|
template <typename T, std::size_t... Dims>
|
||||||
|
using type = tensor<detach_storage_t<T>, Dims...>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct tensor_with_storage<inline_storage_tag> {
|
||||||
|
template <typename T, std::size_t... Dims>
|
||||||
|
using type = tensor<inline_storage<detach_storage_t<T>>, Dims...>;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct tensor_traits {};
|
struct tensor_traits {};
|
||||||
|
|
||||||
@@ -91,6 +110,9 @@ struct tensor_traits<tensor<T, Dims...>> {
|
|||||||
static constexpr std::enable_if_t<(sizeof...(Dims) > 0), std::size_t[sizeof...(Dims)]> dimensions = { Dims... };
|
static constexpr std::enable_if_t<(sizeof...(Dims) > 0), std::size_t[sizeof...(Dims)]> dimensions = { Dims... };
|
||||||
|
|
||||||
static constexpr std::size_t size = dynamic ? dynamic_extent : (Dims * ... * 1);
|
static constexpr std::size_t size = dynamic ? dynamic_extent : (Dims * ... * 1);
|
||||||
|
|
||||||
|
template <typename ST>
|
||||||
|
using with_storage_t = typename detail::tensor_with_storage<ST>::type;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace math
|
} // namespace math
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace cenet {
|
|||||||
namespace math {
|
namespace math {
|
||||||
|
|
||||||
template <typename T, std::size_t... Dims>
|
template <typename T, std::size_t... Dims>
|
||||||
class tensor_view : public expression_tag {
|
class tensor_view : public expression_tag, public ops_provider<tensor_view<T, Dims...>> {
|
||||||
static_assert(std::is_arithmetic_v<T>);
|
static_assert(std::is_arithmetic_v<T>);
|
||||||
|
|
||||||
template <typename, std::size_t...>
|
template <typename, std::size_t...>
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef CENET_MATH_TYPE_TRAITS_HPP
|
||||||
|
#define CENET_MATH_TYPE_TRAITS_HPP
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace cenet {
|
||||||
|
namespace math {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
||||||
|
|
||||||
|
}
|
||||||
|
} // namespace cenet
|
||||||
|
|
||||||
|
#endif // CENET_MATH_TYPE_TRAITS_HPP
|
||||||
Reference in New Issue
Block a user