refactor(math): generalize tensor operations

This commit is contained in:
2026-11-01 05:59:58 +01:00
parent b482010c5d
commit 3fca91e1ea
4 changed files with 29 additions and 165 deletions
+14 -28
View File
@@ -4,6 +4,7 @@
#include "ops.hpp" #include "ops.hpp"
#include "tensor_traits.hpp" #include "tensor_traits.hpp"
#include <stdexcept>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
@@ -71,45 +72,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) };
} }
+10 -99
View File
@@ -2,42 +2,26 @@
#define CENET_MATH_OPS_HPP #define CENET_MATH_OPS_HPP
#include "tensor_shape.hpp" #include "tensor_shape.hpp"
#include "tensor_traits.hpp"
#include <cstddef> #include <cstddef>
#include <functional>
#include <stdexcept> #include <stdexcept>
#include <type_traits>
namespace cenet { namespace cenet {
namespace math { namespace math {
namespace ops { namespace ops {
class plus { template <typename Op>
private: struct generic_binary_op {
template <typename L, typename R> 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) { static bool compatible(const L& lhs, const R& rhs) {
return lhs.shape() == rhs.shape(); 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 (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())>) { if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
return lhs.shape(); return lhs.shape();
} else { } else {
@@ -47,87 +31,14 @@ public:
template <typename LT, typename R> template <typename LT, typename R>
constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const { 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 { struct plus : generic_binary_op<std::plus<>> {};
private: struct minus : generic_binary_op<std::minus<>> {};
template <typename L, typename R> struct multiply : generic_binary_op<std::multiplies<>> {};
struct result_type_impl {}; struct divide : generic_binary_op<std::divides<>> {};
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);
}
};
} // namespace ops } // namespace ops
} // namespace math } // namespace math
+2 -9
View File
@@ -86,11 +86,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 +104,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) {
+3 -29
View File
@@ -36,16 +36,6 @@ 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;
@@ -89,18 +79,6 @@ 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;
@@ -115,13 +93,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>