27template <
class T>
struct is_tuple : std::false_type {};
29template <
class... Ts>
struct is_tuple<
std::tuple<Ts...>> : std::true_type {};
40 : std::conjunction<is_tuple<Ts>...> {};
58template <
typename... Ts,
typename... Tuples>
60 using type =
decltype(std::tuple_cat(
61 std::declval<std::tuple<Ts...>>(),
65template <
typename T,
typename... Tuples>
struct tuple_cat<T, Tuples...> {
66 using type =
decltype(std::tuple_cat(
67 std::declval<std::tuple<T>>(),
76template <
typename... Tuples>
86template <
typename... Tensors>
89 std::vector<torch::Tensor> vec;
90 vec.reserve(
sizeof...(Tensors));
91 std::apply([&](
const auto &...tensor) { (vec.emplace_back(tensor), ...); },
94 return torch::cat(vec, dim);
103template <
typename... Tensors,
typename Func>
104 requires(std::invocable<Func, const Tensors &> && ...)
106 Func &&
func, int64_t dim = 0) {
107 std::vector<torch::Tensor> vec;
108 vec.reserve(
sizeof...(Tensors));
110 [&](
const auto &...tensor) {
111 (vec.emplace_back(std::invoke(
func, tensor)), ...);
115 return torch::cat(vec, dim);
123template <std::
size_t N,
typename T>
125 return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
126 return std::tuple{((void)Is, value)...};
127 }(std::make_index_sequence<N>{});
138template <std::size_t I = 0,
typename... Tensors,
typename FuncSize,
141 const torch::Tensor &tensor, FuncSize &&funcSize,
142 FuncAssign &&funcAssign, int64_t &offset,
144 if constexpr (I <
sizeof...(Tensors)) {
145 auto &t = std::get<I>(tuple);
146 auto size = std::forward<FuncSize>(funcSize)(t);
147 std::forward<FuncAssign>(funcAssign)(
148 t, tensor.slice(dim, offset, offset + size));
150 slice_tensor_into_tuple<I + 1>(tuple, tensor, funcSize, funcAssign, offset,
164template <
typename... Tensors,
typename FuncSize,
typename FuncAssign>
166 const torch::Tensor &tensor, FuncSize &&funcSize,
167 FuncAssign &&funcAssign, int64_t dim = 0) {
Definition blocktensor.hpp:24
std::tuple<> type
Definition tuple.hpp:55
decltype(std::tuple_cat(std::declval< std::tuple< Ts... > >(), std::declval< typename tuple_cat< Tuples... >::type >())) type
Definition tuple.hpp:62
constexpr auto is_tuple_of_tuples_v
Alias for is_tuple_of_tuples::value.
Definition tuple.hpp:48
constexpr auto repeat_tuple(const T &value)
Returns a std::tuple object with N replications of the given value.
Definition tuple.hpp:124
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:140
constexpr auto tuple_cat_v
Alias for tuple_cat::value.
Definition tuple.hpp:77
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:87
decltype(std::tuple_cat(std::declval< std::tuple< T > >(), std::declval< typename tuple_cat< Tuples... >::type >())) type
Definition tuple.hpp:68
tuple_cat< Tuples... >::type tuple_cat_t
Alias for tuple_cat::type.
Definition tuple.hpp:73
is_tuple_of_tuples< T >::type is_tuple_of_tuples_t
Alias for is_tuple_of_tuples::type.
Definition tuple.hpp:44
constexpr bool is_tuple_v
Definition tuple.hpp:32
Type trait for concatenating std::tuples.
Definition tuple.hpp:52
Definition optimizer.hpp:61
Type trait for std::tuple<std::tuple> type.
Definition tuple.hpp:36
Type trait for std::tuple type.
Definition tuple.hpp:27