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
+40 -80
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();
m_items = other.m_items;
if constexpr (!traits::inline_storage) other.m_items = nullptr;
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));
}
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,8 +191,11 @@ 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!");
m_items = new value_type[traits::size];
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) {
@@ -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;
};