TensorAdvancedIndexingUtils.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #pragma once
  2. #include <ATen/core/Tensor.h>
  3. #include <ATen/native/IndexingUtils.h>
  4. #include <ATen/native/TensorIterator.h>
  5. namespace at::native {
  6. namespace {
  7. static std::string shapes_as_str(TensorList tensors) {
  8. std::ostringstream os;
  9. bool first = true;
  10. for (auto& tensor : tensors) {
  11. if (tensor.defined()) {
  12. if (!first) {
  13. os << ", ";
  14. }
  15. os << tensor.sizes();
  16. first = false;
  17. }
  18. }
  19. return os.str();
  20. }
  21. } // anonymous namespace
  22. static std::tuple<bool, Tensor> canDispatchToMaskedFill(const Tensor& self, const torch::List<std::optional<at::Tensor>>& indices,
  23. const Tensor& value){
  24. if (!(value.numel() ==1 && value.device().is_cpu())){
  25. return std::make_tuple(false,Tensor());
  26. }
  27. int64_t num_ind = 0;
  28. Tensor mask;
  29. auto self_device = self.device();
  30. for (const std::optional<Tensor>& i: indices) {
  31. if (!i.has_value() || !(*i).defined()){
  32. num_ind++;
  33. } else {
  34. const Tensor &index = *i;
  35. if ((index.scalar_type() != kByte && index.scalar_type() != kBool) ||
  36. index.device() != self_device || mask.defined()){
  37. return std::make_tuple(false, Tensor());
  38. } else {
  39. mask = index;
  40. for (const auto j : c10::irange(index.dim())) {
  41. int64_t srcIdx = num_ind + j;
  42. TORCH_CHECK_INDEX(index.size(j) == self.size(srcIdx), "The shape of the mask ", index.sizes(), " at index ", j,
  43. " does not match the shape of the indexed tensor ", self.sizes(), " at index ", srcIdx);
  44. }
  45. num_ind += mask.ndimension();
  46. }
  47. }
  48. }
  49. for (C10_UNUSED const auto i : c10::irange(num_ind, self.ndimension())) {
  50. mask = mask.unsqueeze(-1);
  51. }
  52. return std::make_tuple(true, mask);
  53. }
  54. static AdvancedIndex make_info(Tensor self, IOptTensorListRef orig) {
  55. checkIndexTensorTypes(orig, /*allow_int*/ true);
  56. // first expand BoolTensor (masks) or ByteTensor (masks) into 1 or more LongTensors
  57. auto indices = expandTensors(self, orig);
  58. // next broadcast all index tensors together
  59. try {
  60. indices = expand_outplace(indices);
  61. } catch (std::exception& e) {
  62. TORCH_CHECK_INDEX(false, "shape mismatch: indexing tensors could not be broadcast together"
  63. " with shapes ", shapes_as_str(indices));
  64. }
  65. // add missing null Tensors so that it matches self.dim()
  66. while (indices.size() < (size_t)self.dim()) {
  67. indices.emplace_back();
  68. }
  69. // if the non-null indices are not all adjacent, transpose self and indices
  70. // together so that they're adjacent at the front
  71. if (!hasContiguousSubspace(indices)) {
  72. std::tie(self, indices) = transposeToFront(self, indices);
  73. }
  74. // Ensure indices are on the same device as self
  75. for (auto & indice : indices) {
  76. if (indice.defined() && indice.device() != self.device()) {
  77. indice = indice.to(self.device());
  78. }
  79. }
  80. for (auto & indice : indices) {
  81. if (indice.defined() && indice.dtype() == at::kInt) {
  82. indice = indice.to(at::kLong);
  83. }
  84. }
  85. return AdvancedIndex(self, indices);
  86. }
  87. } // namespace at::native