refactor(math): improve tensor shapes

This commit is contained in:
2026-10-29 10:55:29 +01:00
parent 814050baca
commit 22bf8db5e8
+53 -12
View File
@@ -16,21 +16,61 @@ namespace math {
namespace detail { namespace detail {
struct empty_shape {}; template <std::size_t... Dims>
struct static_shape {
static_shape() = default;
template <std::size_t Rank> 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 { struct dynamic_shape {
dynamic_shape() = default; dynamic_shape() = default;
template <std::size_t ExtRank, typename = std::enable_if_t<(ExtRank >= Rank)>> 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 - Rank; constexpr std::size_t ext_offset = ExtRank - sizeof...(Dims);
for (std::size_t i = 0; i < Rank; ++i) { for (std::size_t i = 0; i < sizeof...(Dims); ++i) {
dimensions[i] = oth.dimensions[ext_offset + i]; dimensions[i] = oth.dimensions[ext_offset + i];
} }
} }
std::size_t dimensions[Rank]{}; 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 } // namespace detail
@@ -126,7 +166,8 @@ private:
} }
} }
private: private:
using shape_type = std::conditional_t<traits::dynamic, detail::dynamic_shape<traits::rank>, detail::empty_shape>; using shape_type =
std::conditional_t<traits::dynamic, detail::dynamic_shape<Dims...>, detail::static_shape<Dims...>>;
template <typename Fwd> template <typename Fwd>
tensor_view(value_type* view, Fwd&& shape) tensor_view(value_type* view, Fwd&& shape)
@@ -145,6 +186,9 @@ public:
using constant_value_type = const value_type; using constant_value_type = const value_type;
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...>;
private:
using shape_type =
std::conditional_t<traits::dynamic, detail::dynamic_shape<Dims...>, detail::static_shape<Dims...>>;
public: public:
tensor() { tensor() {
if constexpr (traits::dynamic) { if constexpr (traits::dynamic) {
@@ -161,7 +205,7 @@ public:
typename = std::enable_if_t<( typename = std::enable_if_t<(
traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>> traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
explicit tensor(RtDims... dimensions) explicit tensor(RtDims... dimensions)
: m_shape(detail::dynamic_shape<sizeof...(Dims)>{}) { : m_shape(detail::dynamic_shape<Dims...>{}) {
set_dimensions(dimensions...); set_dimensions(dimensions...);
} }
@@ -198,10 +242,9 @@ public:
return *this; return *this;
} }
public: public:
// TODO: Introduce another type trait for tensor compability
template <typename E, template <typename E,
typename = std::enable_if_t<detail::is_expression_v<E> && !is_tensor_v<E> && 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>>> shape_type::template compatible<typename detail::expression_result<E>::type>()>>
tensor(E&& expr) { // NOLINT tensor(E&& expr) { // NOLINT
if constexpr (!traits::inline_storage) { if constexpr (!traits::inline_storage) {
static_assert(!traits::dynamic, "Dynamic tensors are not supported yet!"); static_assert(!traits::dynamic, "Dynamic tensors are not supported yet!");
@@ -301,8 +344,6 @@ private:
m_items = nullptr; m_items = nullptr;
} }
} }
private:
using shape_type = std::conditional_t<traits::dynamic, detail::dynamic_shape<traits::rank>, detail::empty_shape>;
private: private:
typename traits::storage_type m_items; typename traits::storage_type m_items;
shape_type m_shape; shape_type m_shape;