Files
cenet/include/cenet/math/tensor_shape.hpp
T

295 lines
9.7 KiB
C++

#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 I>
constexpr auto get() const {
return get_impl<I, Dims...>::value;
}
template <std::size_t I>
static constexpr auto sget() {
return get_impl<I, Dims...>::value;
}
static constexpr auto rank = sizeof...(Dims);
static constexpr auto score = 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 == 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;
};
private:
template <std::size_t S, std::size_t...>
struct size_impl {
static constexpr auto value = S;
};
template <std::size_t S, std::size_t First, std::size_t... Rest>
struct size_impl<S, First, Rest...> : size_impl<First * S, Rest...> {};
public:
static constexpr auto size = size_impl<1, Dims...>::value;
template <std::size_t Start = 0, typename... Offs>
std::size_t calculate_offset(const std::tuple<Offs...>& offs) const {
static_assert(sizeof...(Offs) > 0);
static_assert(sizeof...(Offs) <= sizeof...(Dims));
return calculate_offset_impl<Start, sizeof...(Dims) - sizeof...(Offs)>(offs);
}
private:
template <std::size_t I, std::size_t Start, typename... Offs>
std::size_t calculate_offset_impl(const std::tuple<Offs...>& offs) const {
if constexpr (I == 0) {
return std::get<0>(offs);
} else {
return (calculate_offset_impl<I - 1, Start>(offs) * get<Start + I>()) + std::get<I>(offs);
}
}
};
template <std::size_t... Dims>
struct dynamic_shape : shape<Dims...> {
dynamic_shape() = default;
template <typename... RDims,
typename = std::enable_if_t<(sizeof...(RDims) == sizeof...(Dims)) && (std::is_integral_v<RDims> && ...)>>
explicit dynamic_shape(RDims... rtDimensions) {
store_impl<0>(rtDimensions...);
}
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 I>
constexpr auto get() const {
return get_impl<I, Dims...>::value == dynamic_extent ? dimensions[I] : get_impl<I, Dims...>::value;
}
template <std::size_t I>
static constexpr auto sget() {
return 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 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>
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 Score, std::size_t...>
struct score_impl {
static constexpr auto value = Score;
};
template <std::size_t Score, std::size_t First, std::size_t... Rest>
struct score_impl<Score, First, Rest...> : score_impl<Score + ((First != dynamic_extent) ? 1 : 0), Rest...> {};
template <std::size_t I, typename RDim, typename... RDims, typename = std::enable_if_t<(I < sizeof...(Dims))>>
void store_impl(RDim first, RDims... rest) {
dimensions[I] = first;
store_impl<I + 1>(rest...);
}
public:
static constexpr auto rank = sizeof...(Dims);
static constexpr auto score = score_impl<0, Dims...>::value;
template <std::size_t Start = 0, typename... Offs>
std::size_t calculate_offset(const std::tuple<Offs...>& offs) const {
static_assert(sizeof...(Offs) > 0);
static_assert(sizeof...(Offs) <= sizeof...(Dims));
return calculate_offset_impl<Start, sizeof...(Dims) - sizeof...(Offs)>(offs);
}
private:
template <std::size_t I, std::size_t Start, typename... Offs>
std::size_t calculate_offset_impl(const std::tuple<Offs...>& offs) const {
if constexpr (I == 0) {
return std::get<0>(offs);
} else {
return (calculate_offset_impl<I - 1, Start>(offs) * get<Start + I>()) + std::get<I>(offs);
}
}
};
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 <std::size_t... Dims>
struct is_static_shape_var : std::true_type {};
template <std::size_t First, std::size_t... Rest>
struct is_static_shape_var<First, Rest...>
: std::conditional_t<(First == dynamic_extent), std::false_type, is_static_shape_var<Rest...>> {};
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 {};
template <typename L, typename R, typename = void>
struct best_shape {};
template <template <std::size_t...> class T, std::size_t... Dims, typename O>
struct best_shape<T<Dims...>, O> {
using type = T<Dims...>;
static constexpr bool valid = true;
static constexpr bool left = true;
};
template <template <std::size_t...> class T, std::size_t... Dims, typename O>
struct best_shape<O, T<Dims...>> {
using type = T<Dims...>;
static constexpr bool valid = true;
static constexpr bool left = false;
};
template <template <std::size_t...> class L,
std::size_t... LDims,
template <std::size_t...> class R,
std::size_t... RDims>
struct best_shape<L<LDims...>, R<RDims...>> {
static constexpr bool valid = sizeof...(LDims) == sizeof...(RDims);
static constexpr bool left = L<RDims...>::score >= R<LDims...>::score;
using type = std::enable_if_t<valid, std::conditional_t<left, L<LDims...>, R<RDims...>>>;
};
template <std::size_t... Dims>
struct shape_of_impl {
using type = std::conditional_t<is_static_shape_var<Dims...>::value, static_shape<Dims...>, dynamic_shape<Dims...>>;
};
} // namespace detail
template <std::size_t... Dims>
using shape_of = typename detail::shape_of_impl<Dims...>::type;
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