refactor(math): generalize tensor operations
This commit is contained in:
+14
-28
@@ -4,6 +4,7 @@
|
||||
#include "ops.hpp"
|
||||
#include "tensor_traits.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
@@ -71,45 +72,30 @@ using operand_t = typename operand<T>::type;
|
||||
template <typename 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
|
||||
|
||||
template <typename L,
|
||||
typename R,
|
||||
typename Op = ops::plus,
|
||||
typename = std::void_t<typename detail::expression_result<binary_expression<L, R, Op>>::type>>
|
||||
template <typename L, typename R, typename Op = ops::plus>
|
||||
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) };
|
||||
}
|
||||
|
||||
template <typename L,
|
||||
typename R,
|
||||
typename Op = ops::minus,
|
||||
typename = std::void_t<typename detail::expression_result<binary_expression<L, R, Op>>::type>>
|
||||
template <typename L, typename R, typename Op = ops::minus>
|
||||
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::multiply,
|
||||
typename = std::void_t<typename detail::expression_result<binary_expression<L, R, Op>>::type>>
|
||||
template <typename L, typename R, typename Op = ops::multiply>
|
||||
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) };
|
||||
}
|
||||
|
||||
|
||||
+10
-99
@@ -2,42 +2,26 @@
|
||||
#define CENET_MATH_OPS_HPP
|
||||
|
||||
#include "tensor_shape.hpp"
|
||||
#include "tensor_traits.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
|
||||
namespace cenet {
|
||||
namespace math {
|
||||
namespace ops {
|
||||
|
||||
class plus {
|
||||
private:
|
||||
template <typename Op>
|
||||
struct generic_binary_op {
|
||||
template <typename L, typename R>
|
||||
struct result_type_impl {};
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape");
|
||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||
// TODO: introduce "best shape" function/trait
|
||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
||||
return lhs.shape();
|
||||
} else {
|
||||
@@ -47,87 +31,14 @@ public:
|
||||
|
||||
template <typename LT, typename R>
|
||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
||||
return lhs + rhs.coeff(index);
|
||||
return Op{}(lhs, rhs.coeff(index));
|
||||
}
|
||||
};
|
||||
|
||||
class minus {
|
||||
private:
|
||||
template <typename L, typename R>
|
||||
struct result_type_impl {};
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape");
|
||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
||||
return lhs.shape();
|
||||
} else {
|
||||
return rhs.shape();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename LT, typename R>
|
||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
||||
return lhs - rhs.coeff(index);
|
||||
}
|
||||
};
|
||||
|
||||
class multiply {
|
||||
private:
|
||||
template <typename L, typename R>
|
||||
struct result_type_impl {};
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||
if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape");
|
||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
||||
return lhs.shape();
|
||||
} else {
|
||||
return rhs.shape();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename LT, typename R>
|
||||
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const {
|
||||
return lhs * rhs.coeff(index);
|
||||
}
|
||||
};
|
||||
struct plus : generic_binary_op<std::plus<>> {};
|
||||
struct minus : generic_binary_op<std::minus<>> {};
|
||||
struct multiply : generic_binary_op<std::multiplies<>> {};
|
||||
struct divide : generic_binary_op<std::divides<>> {};
|
||||
|
||||
} // namespace ops
|
||||
} // namespace math
|
||||
|
||||
@@ -86,11 +86,7 @@ public:
|
||||
public:
|
||||
template <typename E, typename = std::enable_if_t<detail::is_expression_v<E>>>
|
||||
tensor(E&& exprFwd) { // NOLINT
|
||||
// If static shape check failed, check dynamically (requried for static = dynamic op dynamic).
|
||||
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 (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||
|
||||
if constexpr (!traits::inline_storage) {
|
||||
if constexpr (traits::dynamic) {
|
||||
@@ -108,10 +104,7 @@ public:
|
||||
|
||||
template <typename E, typename = std::enable_if_t<detail::is_expression_v<E>>>
|
||||
tensor& operator=(E&& exprFwd) {
|
||||
// If static shape check failed, check dynamically (requried for static = dynamic op dynamic).
|
||||
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 (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||
|
||||
if constexpr (!traits::inline_storage) {
|
||||
if constexpr (traits::dynamic) {
|
||||
|
||||
@@ -36,16 +36,6 @@ struct static_shape : shape<Dims...> {
|
||||
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>
|
||||
constexpr auto get() const {
|
||||
return get_impl<I, Dims...>::value;
|
||||
@@ -89,18 +79,6 @@ struct dynamic_shape : shape<Dims...> {
|
||||
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>
|
||||
constexpr auto get() const {
|
||||
return get_impl<I, Dims...>::value == dynamic_extent ? dimensions[I] : get_impl<I, Dims...>::value;
|
||||
@@ -115,13 +93,9 @@ private:
|
||||
struct compatible_impl<tensor<T, OthDims...>>
|
||||
: std::conditional_t<((Dims == dynamic_extent || Dims == OthDims) && ...), std::true_type, std::false_type> {};
|
||||
|
||||
template <template <std::size_t...> class L,
|
||||
std::size_t... LDims,
|
||||
template <std::size_t...> class R,
|
||||
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 <template <std::size_t...> class R, std::size_t... RDims, std::size_t... Is>
|
||||
constexpr bool compare_impl(std::index_sequence<Is...>, const R<RDims...>& rhs) const {
|
||||
return ((get<Is>() == rhs.template get<Is>()) && ...);
|
||||
}
|
||||
|
||||
template <std::size_t I, std::size_t First, std::size_t... Rest>
|
||||
|
||||
Reference in New Issue
Block a user