feat(math): add support for dynamic tensor ops

This commit is contained in:
2026-10-29 22:19:06 +01:00
parent 22bf8db5e8
commit a4640c4835
4 changed files with 289 additions and 88 deletions
+4
View File
@@ -16,6 +16,8 @@ template <typename E, typename Op>
struct unary_expression : expression_tag {
E expr;
constexpr auto shape() const { return expr.shape(); }
auto eval(std::size_t index) { return Op{}(expr.coeff(index)); }
};
@@ -28,6 +30,8 @@ struct binary_expression : expression_tag {
binary_expression(LFwd&& lhs, RFwd&& rhs)
: lhs(std::forward<LFwd>(lhs)), rhs(std::forward<RFwd>(rhs)) {}
constexpr auto shape() const { return Op::shape(lhs, rhs); }
auto eval(std::size_t index) { return Op{}(index, lhs.coeff(index), as_operand(rhs)); }
private:
template <typename T>
+49 -8
View File
@@ -1,9 +1,12 @@
#ifndef CENET_MATH_OPS_HPP
#define CENET_MATH_OPS_HPP
#include "cenet/math/tensor_shape.hpp"
#include "tensor_traits.hpp"
#include <cstddef>
#include <stdexcept>
#include <type_traits>
namespace cenet {
namespace math {
@@ -14,15 +17,34 @@ private:
template <typename L, typename R>
struct result_type_impl {};
template <typename LT, typename RT, std::size_t... Dims>
struct result_type_impl<tensor_view<LT, Dims...>, tensor_view<RT, Dims...>> {
using type =
tensor<decltype(std::declval<detach_storage_t<LT>>() + std::declval<detach_storage_t<RT>>()), Dims...>;
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);
@@ -34,15 +56,34 @@ private:
template <typename L, typename R>
struct result_type_impl {};
template <typename LT, typename RT, std::size_t... Dims>
struct result_type_impl<tensor_view<LT, Dims...>, tensor_view<RT, Dims...>> {
using type =
tensor<decltype(std::declval<detach_storage_t<LT>>() - std::declval<detach_storage_t<RT>>()), Dims...>;
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);
+38 -78
View File
@@ -3,6 +3,7 @@
#include "expr.hpp"
#include "tensor_fwd.hpp"
#include "tensor_shape.hpp"
#include "tensor_traits.hpp"
#include <cstddef>
@@ -14,67 +15,6 @@
namespace cenet {
namespace math {
namespace detail {
template <std::size_t... Dims>
struct static_shape {
static_shape() = default;
template <std::size_t... ExtDims, typename = std::enable_if_t<(sizeof...(ExtDims) >= sizeof...(Dims))>>
explicit static_shape(const static_shape<ExtDims...>& oth) {}
template <std::size_t... OthDims>
static constexpr bool compatible() {
return ((Dims == OthDims) && ...);
}
template <typename T>
static constexpr bool compatible() {
return compatible_impl<T>::value;
}
private:
template <typename T>
struct compatible_impl : std::false_type {};
template <typename T, std::size_t... OthDims>
struct compatible_impl<tensor<T, OthDims...>>
: std::conditional_t<((Dims == OthDims) && ...), std::true_type, std::false_type> {};
};
template <std::size_t... Dims>
struct dynamic_shape {
dynamic_shape() = default;
template <std::size_t ExtRank, typename = std::enable_if_t<(ExtRank >= sizeof...(Dims))>>
explicit dynamic_shape(const dynamic_shape<ExtRank>& oth) {
constexpr std::size_t ext_offset = ExtRank - sizeof...(Dims);
for (std::size_t i = 0; i < sizeof...(Dims); ++i) {
dimensions[i] = oth.dimensions[ext_offset + i];
}
}
template <std::size_t... OthDims>
static constexpr bool compatible() {
return ((Dims == dynamic_extent || Dims == OthDims) && ...);
}
template <typename T>
static constexpr bool compatible() {
return compatible_impl<T>::value;
}
std::size_t dimensions[sizeof...(Dims)]{};
private:
template <typename T>
struct compatible_impl : std::false_type {};
template <typename T, std::size_t... OthDims>
struct compatible_impl<tensor<T, OthDims...>>
: std::conditional_t<((Dims == dynamic_extent || Dims == OthDims) && ...), std::true_type, std::false_type> {};
};
} // namespace detail
template <typename T, std::size_t... Dims>
class tensor_view {
static_assert(std::is_arithmetic_v<T>);
@@ -86,6 +26,7 @@ class tensor_view {
friend class tensor_view;
public:
using traits = tensor_traits<tensor<T, Dims...>>;
using shape_type = std::conditional_t<traits::dynamic, dynamic_shape<Dims...>, static_shape<Dims...>>;
using value_type = T;
public:
template <typename... Offs,
@@ -148,6 +89,8 @@ public:
value_type* data() { return m_view; }
const value_type* data() const { return m_view; }
const shape_type& shape() const { return m_shape; }
private:
template <typename InT, std::size_t Off, std::size_t First, std::size_t... Rest>
auto inner_view(value_type* view, std::index_sequence<First, Rest...>) {
@@ -166,9 +109,6 @@ private:
}
}
private:
using shape_type =
std::conditional_t<traits::dynamic, detail::dynamic_shape<Dims...>, detail::static_shape<Dims...>>;
template <typename Fwd>
tensor_view(value_type* view, Fwd&& shape)
: m_view(view), m_shape(std::forward<Fwd>(shape)) {}
@@ -186,9 +126,7 @@ public:
using constant_value_type = const value_type;
using view_type = tensor_view<value_type, Dims...>;
using constant_view_type = tensor_view<constant_value_type, Dims...>;
private:
using shape_type =
std::conditional_t<traits::dynamic, detail::dynamic_shape<Dims...>, detail::static_shape<Dims...>>;
using shape_type = std::conditional_t<traits::dynamic, dynamic_shape<Dims...>, static_shape<Dims...>>;
public:
tensor() {
if constexpr (traits::dynamic) {
@@ -205,7 +143,7 @@ public:
typename = std::enable_if_t<(
traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
explicit tensor(RtDims... dimensions)
: m_shape(detail::dynamic_shape<Dims...>{}) {
: m_shape(dynamic_shape<Dims...>{}) {
set_dimensions(dimensions...);
}
@@ -213,29 +151,35 @@ public:
tensor(tensor&& other) noexcept
: m_shape(std::move(other.m_shape)) {
if constexpr (!traits::inline_storage) m_items = nullptr;
*this = std::move(other);
if constexpr (!traits::inline_storage) {
m_items = other.m_items;
other.m_items = nullptr;
} else {
std::memcpy(m_items, other.m_items, sizeof(m_items));
}
}
tensor& operator=(tensor&& other) noexcept {
if (this == &other) return *this;
release();
if constexpr (!traits::inline_storage) {
m_items = other.m_items;
if constexpr (!traits::inline_storage) other.m_items = nullptr;
other.m_items = nullptr;
} else {
std::memcpy(m_items, other.m_items, sizeof(m_items));
}
return *this;
}
tensor(const tensor& other)
: m_shape(other.m_shape) {
if constexpr (!traits::inline_storage) m_items = nullptr;
*this = other;
dyn_alloc(std::make_index_sequence<sizeof...(Dims)>{});
dyn_copy(std::make_index_sequence<sizeof...(Dims)>{});
}
tensor& operator=(const tensor& other) {
if (this == &other) return *this;
release();
std::memcpy(m_shape.dimensions, other.m_shape.dimensions, sizeof(m_shape.dimensions));
std::memcpy(m_shape.strides, other.m_shape.strides, sizeof(m_shape.strides));
dyn_alloc(std::make_index_sequence<sizeof...(Dims)>{});
dyn_copy(std::make_index_sequence<sizeof...(Dims)>{});
@@ -247,9 +191,12 @@ public:
shape_type::template compatible<typename detail::expression_result<E>::type>()>>
tensor(E&& expr) { // NOLINT
if constexpr (!traits::inline_storage) {
static_assert(!traits::dynamic, "Dynamic tensors are not supported yet!");
if constexpr (traits::dynamic) {
set_dimensions(expr.shape());
} else {
m_items = new value_type[traits::size];
}
}
for (std::size_t i = 0; i < view().size(); ++i) {
m_items[i] = std::forward<E>(expr).eval(i);
@@ -260,6 +207,7 @@ public:
typename = std::enable_if_t<detail::is_expression_v<E> && !is_tensor_v<E> &&
std::is_same_v<tensor, typename detail::expression_result<E>::type>>>
tensor& operator=(E&& expr) {
if (!expr.valid_for(m_shape)) throw std::runtime_error("unexpected tensor shape");
for (std::size_t i = 0; i < view().size(); ++i) {
m_items[i] = std::forward<E>(expr);
}
@@ -283,6 +231,16 @@ public:
m_items = new value_type[(dimensions * ... * 1)];
std::memset(m_items, 0, sizeof(*m_items) * (dimensions * ... * 1));
}
template <template <std::size_t...> class Shape, std::size_t... ShapeDims>
void set_dimensions(const Shape<ShapeDims...>& shape) {
set_dimensions_impl(shape, std::make_index_sequence<sizeof...(ShapeDims)>{});
}
private:
template <template <std::size_t...> class Shape, std::size_t... ShapeDims, std::size_t... Is>
void set_dimensions_impl(const Shape<ShapeDims...>& shape, std::index_sequence<Is...>) {
set_dimensions(shape.template get<Is>()...);
}
public:
constexpr std::size_t size() const { return size_impl(std::make_index_sequence<sizeof...(Dims)>{}); }
@@ -303,10 +261,12 @@ public:
value_type* data() { return m_items; }
const value_type* data() const { return m_items; }
const shape_type& shape() const { return m_shape; }
private:
template <typename... RtDims, std::size_t... Is>
constexpr bool valid_dims(std::index_sequence<Is...>, RtDims... dimensions) const {
return (valid_dim<Is>(dimensions) && ...);
return (valid_dim<decltype(dimensions), Is>(dimensions) && ...);
}
template <typename RtDim, std::size_t I>
@@ -345,7 +305,7 @@ private:
}
}
private:
typename traits::storage_type m_items;
typename traits::storage_type m_items{};
shape_type m_shape;
};
+196
View File
@@ -0,0 +1,196 @@
#ifndef CENET_MATH_TENSOR_SHAPE_HPP
#define CENET_MATH_TENSOR_SHAPE_HPP
#include "tensor_fwd.hpp"
#include <cstddef>
#include <type_traits>
#include <utility>
namespace cenet {
namespace math {
template <std::size_t... Dims>
struct shape {};
template <std::size_t... Dims>
struct static_shape;
template <std::size_t... Dims>
struct dynamic_shape;
template <std::size_t... Dims>
struct static_shape : shape<Dims...> {
static_shape() = default;
template <std::size_t... ExtDims, typename = std::enable_if_t<(sizeof...(ExtDims) >= sizeof...(Dims))>>
explicit static_shape(const static_shape<ExtDims...>& oth) {}
template <std::size_t... OthDims>
static constexpr bool compatible() {
return ((Dims == OthDims) && ...);
}
template <typename T>
static constexpr bool compatible() {
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;
}
private:
template <typename T>
struct compatible_impl : std::false_type {};
template <typename T, std::size_t... OthDims>
struct compatible_impl<tensor<T, OthDims...>>
: std::conditional_t<((Dims == OthDims) && ...), std::true_type, std::false_type> {};
template <std::size_t I, std::size_t First, std::size_t... Rest>
struct get_impl : get_impl<I - 1, Rest...> {};
template <std::size_t First, std::size_t... Rest>
struct get_impl<0, First, Rest...> {
static constexpr auto value = First;
};
};
template <std::size_t... Dims>
struct dynamic_shape : shape<Dims...> {
dynamic_shape() = default;
template <std::size_t ExtRank, typename = std::enable_if_t<(ExtRank >= sizeof...(Dims))>>
explicit dynamic_shape(const dynamic_shape<ExtRank>& oth) {
constexpr std::size_t ext_offset = ExtRank - sizeof...(Dims);
for (std::size_t i = 0; i < sizeof...(Dims); ++i) {
dimensions[i] = oth.dimensions[ext_offset + i];
}
}
template <std::size_t... OthDims>
static constexpr bool compatible() {
return ((Dims == dynamic_extent || Dims == OthDims) && ...);
}
template <typename T>
static constexpr bool compatible() {
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;
}
std::size_t dimensions[sizeof...(Dims)]{};
private:
template <typename T>
struct compatible_impl : std::false_type {};
template <typename T, std::size_t... OthDims>
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 <std::size_t I, std::size_t First, std::size_t... Rest>
struct get_impl : get_impl<I - 1, Rest...> {};
template <std::size_t First, std::size_t... Rest>
struct get_impl<0, First, Rest...> {
static constexpr auto value = First;
};
};
namespace detail {
template <typename T>
struct is_static_shape : std::false_type {};
template <std::size_t... Dims>
struct is_static_shape<static_shape<Dims...>> : std::true_type {};
template <typename T>
inline constexpr auto is_static_shape_v = is_static_shape<T>::value;
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 shape_compare_impl(std::index_sequence<Is...>, const L<LDims...>& lhs, const R<RDims...>& rhs) {
return ((lhs.template get<Is>() == rhs.template get<Is>()) && ...);
}
template <typename L, typename R>
struct is_shape_equivalent : std::false_type {};
template <template <std::size_t...> class L,
std::size_t LFirst,
std::size_t... LRest,
template <std::size_t...> class R,
std::size_t RFirst,
std::size_t... RRest>
struct is_shape_equivalent<L<LFirst, LRest...>, R<RFirst, RRest...>>
: std::conditional_t<(LFirst == RFirst || LFirst == dynamic_extent || RFirst == dynamic_extent),
is_shape_equivalent<L<LRest...>, R<RRest...>>,
std::false_type> {};
template <template <std::size_t...> class L, template <std::size_t...> class R>
struct is_shape_equivalent<L<>, R<>> : std::true_type {};
} // namespace detail
template <template <std::size_t...> class L,
std::size_t... LDims,
template <std::size_t...> class R,
std::size_t... RDims>
bool operator==(const L<LDims...>& lhs, const R<RDims...>& rhs) {
return sizeof...(LDims) == sizeof...(RDims) &&
detail::shape_compare_impl(std::make_index_sequence<sizeof...(LDims)>{}, lhs, rhs);
}
template <template <std::size_t...> class L,
std::size_t... LDims,
template <std::size_t...> class R,
std::size_t... RDims>
bool operator!=(const L<LDims...>& lhs, const R<RDims...>& rhs) {
return sizeof...(LDims) != sizeof...(RDims) ||
!detail::shape_compare_impl(std::make_index_sequence<sizeof...(LDims)>{}, lhs, rhs);
}
} // namespace math
} // namespace cenet
#endif // CENET_MATH_TENSOR_SHAPE_HPP