IndexingUtils.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #pragma once
  2. #include <ATen/ExpandUtils.h>
  3. #include <ATen/native/CanUse32BitIndexMath.h>
  4. #include <ATen/native/TensorIterator.h>
  5. #include <ATen/core/IListRef.h>
  6. #include <c10/util/irange.h>
  7. #ifndef AT_PER_OPERATOR_HEADERS
  8. #include <ATen/Functions.h>
  9. #else
  10. #include <ATen/ops/empty.h>
  11. #include <ATen/ops/nonzero.h>
  12. #endif
  13. namespace at::native {
  14. [[noreturn]]
  15. static void invalid_mask(const Tensor & self, int64_t idx, const Tensor & mask, int64_t maskIdx) {
  16. TORCH_CHECK_INDEX(false, "The shape of the mask ", mask.sizes(), " at index ", maskIdx,
  17. " does not match the shape of the indexed tensor ", self.sizes(), " at index ", idx);
  18. }
  19. [[maybe_unused]] static std::vector<Tensor> expandTensors(
  20. const Tensor& self,
  21. IOptTensorListRef indices,
  22. bool ensure_same_device = false) {
  23. // If indices come in as ByteTensor or BoolTensor (masks), expand them into
  24. // the equivalent indexing by LongTensors
  25. std::vector<Tensor> result;
  26. for (const auto& index_opt : indices) {
  27. if (!index_opt.has_value()) {
  28. result.emplace_back();
  29. } else {
  30. const auto& index = *index_opt;
  31. if (index.scalar_type() == kByte || index.scalar_type() == kBool) {
  32. if (index.scalar_type() == kByte) {
  33. TORCH_WARN("indexing with dtype torch.uint8 is now deprecated," \
  34. " please use a dtype torch.bool instead.");
  35. }
  36. // The sizes of the ByteTensor mask or bool tensor must match the sizes of the
  37. // corresponding dimensions in self
  38. for (const auto j : c10::irange(index.dim())) {
  39. int64_t srcIdx = static_cast<int64_t>(result.size() + j);
  40. if (index.size(j) != self.size(srcIdx)) {
  41. invalid_mask(self, srcIdx, index, j);
  42. }
  43. }
  44. // Replace with nonzeros
  45. at::Tensor nonzero;
  46. if (ensure_same_device && index.device() != self.device()) {
  47. bool non_blocking = index.is_cpu() && self.device().is_cuda();
  48. auto out = at::empty({0}, index.options().dtype(kLong).pinned_memory(non_blocking));
  49. nonzero = at::nonzero_out(out, index).to(self.device(), non_blocking);
  50. } else {
  51. nonzero = index.nonzero();
  52. }
  53. for (const auto j : c10::irange(index.dim())) {
  54. result.emplace_back(nonzero.select(1, j));
  55. }
  56. } else if (ensure_same_device && index.device() != self.device()) {
  57. result.emplace_back(index.to(self.device()));
  58. } else {
  59. result.emplace_back(index);
  60. }
  61. }
  62. }
  63. return result;
  64. }
  65. [[maybe_unused]] static void checkIndexTensorTypes(
  66. IOptTensorListRef indices,
  67. bool allow_int = false) {
  68. for (const auto& tensor : indices) {
  69. if (tensor.has_value() && tensor->defined()) {
  70. auto scalarType = tensor->scalar_type();
  71. if (allow_int) {
  72. if (scalarType != kLong && scalarType != kByte && scalarType != kBool && scalarType != kInt) {
  73. TORCH_CHECK_INDEX(false, "tensors used as indices must be long, int, byte or bool tensors");
  74. }
  75. } else {
  76. if (scalarType != kLong && scalarType != kByte && scalarType != kBool) {
  77. TORCH_CHECK_INDEX(false, "tensors used as indices must be long, byte or bool tensors");
  78. }
  79. }
  80. }
  81. }
  82. }
  83. inline torch::List<std::optional<Tensor>> toListOfOptionalTensors(ArrayRef<Tensor> list) {
  84. torch::List<std::optional<Tensor>> result;
  85. result.reserve(list.size());
  86. for (const Tensor& a : list) {
  87. result.push_back(a);
  88. }
  89. return result;
  90. }
  91. inline torch::List<std::optional<Tensor>> toListOfOptionalTensors(ArrayRef<IValue> list) {
  92. torch::List<std::optional<Tensor>> result;
  93. result.reserve(list.size());
  94. for (const IValue& a : list) {
  95. result.push_back(a.isTensor() ? std::optional<Tensor>(a.toTensor()) : std::optional<Tensor>());
  96. }
  97. return result;
  98. }
  99. [[maybe_unused]] static bool hasContiguousSubspace(TensorList tl) {
  100. // true if all the non-null tensors are adjacent
  101. auto isDefined = [](const Tensor & tensor){ return tensor.defined(); };
  102. auto isNull = [](const Tensor & tensor){ return !tensor.defined(); };
  103. auto start = std::find_if(tl.begin(), tl.end(), isDefined);
  104. auto stop = std::find_if(tl.rbegin(), tl.rend(), isDefined);
  105. auto it = std::find_if(start, stop.base(), isNull);
  106. return it == stop.base();
  107. }
  108. // Transposes the tensor and indices together so that all the non-null indices
  109. // index the first k dimensions of the tensor. Returns the transposed tensor
  110. // and the reordered indices. For example:
  111. // transposeToFront(tensor, {nullptr, a, nullptr, b})
  112. // returns
  113. // tensor.permute([1, 3, 0, 2]), {a, b, nullptr, nullptr}
  114. [[maybe_unused]] static std::tuple<Tensor, std::vector<Tensor>> transposeToFront(
  115. const Tensor& self,
  116. TensorList indices) {
  117. std::vector<int64_t> dims;
  118. std::vector<Tensor> transposedIndices;
  119. dims.reserve(self.dim());
  120. for (const auto i : c10::irange(self.dim())) {
  121. if (indices[i].defined()) {
  122. dims.push_back(i);
  123. transposedIndices.emplace_back(indices[i]);
  124. }
  125. }
  126. for (const auto i : c10::irange(self.dim())) {
  127. if (!indices[i].defined()) {
  128. dims.push_back(i);
  129. transposedIndices.emplace_back();
  130. }
  131. }
  132. return std::make_tuple(self.permute(dims), std::move(transposedIndices));
  133. }
  134. inline std::tuple<Tensor, std::vector<Tensor>, std::vector<int64_t>>
  135. transposeToFrontAndInvPerm(const Tensor& self, TensorList indices) {
  136. std::vector<int64_t> dims;
  137. std::vector<int64_t> invPerm;
  138. std::vector<Tensor> transposedIndices;
  139. dims.reserve(self.dim());
  140. invPerm.resize(self.dim());
  141. for (const auto i : c10::irange(self.dim())) {
  142. if (indices[i].defined()) {
  143. dims.push_back(i);
  144. transposedIndices.emplace_back(indices[i]);
  145. }
  146. }
  147. for (const auto i : c10::irange(self.dim())) {
  148. if (!indices[i].defined()) {
  149. dims.push_back(i);
  150. transposedIndices.emplace_back();
  151. }
  152. }
  153. for (const auto i : c10::irange(self.dim())) {
  154. invPerm[dims[i]] = i;
  155. }
  156. return std::make_tuple(self.permute(dims), std::move(transposedIndices), std::move(invPerm));
  157. }
  158. struct AdvancedIndex {
  159. AdvancedIndex(const Tensor& src, TensorList indices);
  160. Tensor src;
  161. std::vector<Tensor> indices;
  162. DimVector indexed_sizes;
  163. DimVector indexed_strides;
  164. int64_t dims_before;
  165. int64_t dims_after;
  166. };
  167. } //namespace at::native