25template <
class T>
struct is_tuple : std::false_type {};
27template <
class... Ts>
struct is_tuple<
std::tuple<Ts...>> : std::true_type {};
38 : std::common_type_t<is_tuple<Ts>...> {};
56template <
typename... Ts,
typename... Tuples>
58 using type =
decltype(std::tuple_cat(
59 std::declval<std::tuple<Ts...>>(),
63template <
typename T,
typename... Tuples>
struct tuple_cat<T, Tuples...> {
64 using type =
decltype(std::tuple_cat(
65 std::declval<std::tuple<T>>(),
74template <
typename... Tuples>
79template <
typename... Tensors>
82 std::vector<torch::Tensor> vec;
83 vec.reserve(
sizeof...(Tensors));
84 std::apply([&](
const auto &...tensor) { (vec.emplace_back(tensor), ...); },
87 return torch::cat(vec, dim);
93template <
typename... Tensors,
typename Func>
95 Func &&
func, int64_t dim = 0) {
96 std::vector<torch::Tensor> vec;
97 vec.reserve(
sizeof...(Tensors));
99 [&](
const auto &...tensor) {
100 (vec.emplace_back(std::invoke(
func, tensor)), ...);
104 return torch::cat(vec, dim);
108template <std::
size_t N,
typename T>
110 return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
111 return std::tuple{((void)Is, value)...};
112 }(std::make_index_sequence<N>{});
117template <std::size_t I = 0,
typename... Tensors,
typename FuncSize,
120 const torch::Tensor &tensor, FuncSize &&funcSize,
121 FuncAssign &&funcAssign, int64_t &offset,
123 if constexpr (I <
sizeof...(Tensors)) {
124 auto &t = std::get<I>(tuple);
125 auto size = std::forward<FuncSize>(funcSize)(t);
126 std::forward<FuncAssign>(funcAssign)(
127 t, tensor.slice(dim, offset, offset + size));
129 slice_tensor_into_tuple<I + 1>(tuple, tensor, funcSize, funcAssign, offset,
134template <
typename... Tensors,
typename FuncSize,
typename FuncAssign>
136 const torch::Tensor &tensor, FuncSize &&funcSize,
137 FuncAssign &&funcAssign, int64_t dim = 0) {
Definition blocktensor.hpp:24
std::tuple<> type
Definition tuple.hpp:53
decltype(std::tuple_cat(std::declval< std::tuple< Ts... > >(), std::declval< typename tuple_cat< Tuples... >::type >())) type
Definition tuple.hpp:60
constexpr auto is_tuple_of_tuples_v
Alias for is_tuple_of_tuples::value.
Definition tuple.hpp:46
constexpr auto repeat_tuple(const T &value)
Returns a std::tuple object with N replications of the given value.
Definition tuple.hpp:109
void slice_tensor_into_tuple(std::tuple< Tensors... > &tuple, const torch::Tensor &tensor, FuncSize &&funcSize, FuncAssign &&funcAssign, int64_t &offset, int64_t dim=0)
Slices the given tensor into the objects of the std::tuple.
Definition tuple.hpp:119
constexpr auto tuple_cat_v
Alias for tuple_cat::value.
Definition tuple.hpp:75
torch::Tensor cat_tuple_into_tensor(const std::tuple< Tensors... > &tensors, int64_t dim=0)
Concatenates the entries of a std::tuple object into a single Torch tensor along the given dimension.
Definition tuple.hpp:80
decltype(std::tuple_cat(std::declval< std::tuple< T > >(), std::declval< typename tuple_cat< Tuples... >::type >())) type
Definition tuple.hpp:66
tuple_cat< Tuples... >::type tuple_cat_t
Alias for tuple_cat::type.
Definition tuple.hpp:71
is_tuple_of_tuples< T >::type is_tuple_of_tuples_t
Alias for is_tuple_of_tuples::type.
Definition tuple.hpp:42
constexpr bool is_tuple_v
Definition tuple.hpp:30
Type trait for concatenating std::tuples.
Definition tuple.hpp:50
Type trait for std::tuple<std::tuple> type.
Definition tuple.hpp:34
Type trait for std::tuple type.
Definition tuple.hpp:25