ExpandUtils.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #pragma once
  2. #ifndef AT_PER_OPERATOR_HEADERS
  3. #include <ATen/Functions.h>
  4. #else
  5. #include <ATen/ops/view_copy.h>
  6. #endif
  7. #include <ATen/core/DimVector.h>
  8. #include <ATen/Tensor.h>
  9. #include <c10/util/Exception.h>
  10. #include <c10/util/MaybeOwned.h>
  11. #include <c10/util/irange.h>
  12. #include <functional>
  13. #include <sstream>
  14. #include <tuple>
  15. namespace at {
  16. TORCH_API std::vector<int64_t> infer_size(IntArrayRef a, IntArrayRef b);
  17. TORCH_API DimVector infer_size_dimvector(IntArrayRef a, IntArrayRef b);
  18. // Named type instead of a pair/tuple so that we can be sure to
  19. // construct the vectors in place and get NRVO.
  20. template <typename Container>
  21. struct InferExpandGeometryResult {
  22. Container sizes;
  23. Container strides;
  24. explicit InferExpandGeometryResult(size_t ndim)
  25. : sizes(ndim), strides(ndim) {}
  26. explicit InferExpandGeometryResult(IntArrayRef sizes_, size_t ndim)
  27. : sizes(sizes_.begin(), sizes_.end()), strides(ndim) {}
  28. };
  29. TORCH_API std::tuple<std::vector<int64_t>, std::vector<int64_t>>
  30. inferExpandGeometry(
  31. IntArrayRef tensor_sizes,
  32. IntArrayRef tensor_strides,
  33. IntArrayRef sizes);
  34. TORCH_API InferExpandGeometryResult<DimVector>
  35. inferExpandGeometry_dimvector(
  36. IntArrayRef tensor_sizes,
  37. IntArrayRef tensor_strides,
  38. IntArrayRef sizes);
  39. TORCH_API std::vector<int64_t> infer_dense_strides(
  40. IntArrayRef tensor_sizes,
  41. IntArrayRef tensor_strides);
  42. // True if input shapes are expandable
  43. // NOTE: infer_size did a similar check, please keep them sync if change is needed
  44. inline bool are_expandable(IntArrayRef shape1, IntArrayRef shape2) {
  45. size_t ndim1 = shape1.size();
  46. size_t ndim2 = shape2.size();
  47. size_t ndim = ndim1 < ndim2 ? ndim1 : ndim2;
  48. for (int64_t i = ndim - 1; i >= 0; --i) {
  49. if (shape1[--ndim1] == shape2[--ndim2] || shape1[ndim1] == 1 || shape2[ndim2] == 1) {
  50. continue;
  51. }
  52. return false;
  53. }
  54. return true;
  55. }
  56. // avoid copy-construction of Tensor by using a reference_wrapper.
  57. inline void check_defined(std::initializer_list<std::reference_wrapper<const Tensor>> tensors, const char *api_name) {
  58. for (auto& t : tensors) {
  59. if (!t.get().defined()) {
  60. AT_ERROR(api_name, "(...) called with an undefined Tensor");
  61. }
  62. }
  63. }
  64. // NOTE [ ExpandUtils Borrowing ]
  65. //
  66. // Functions in ExpandUtils return `c10::MaybeOwned<Tensor>` because
  67. // expansion may not actually be needed, in which case we can improve
  68. // efficiency by returning
  69. // `c10::MaybeOwned<Tensor>::borrowed(to_expand)`. However, this means
  70. // that you need to be careful: the returned `c10::MaybeOwned<Tensor>`
  71. // must not outlive the original `Tensor` object that `to_expand`
  72. // referred to! The deleted rvalue reference overloads of these
  73. // functions help with this by preventing trivial use of a temporary
  74. // resulting from a function call, but it is still possible to make a
  75. // mistake.
  76. inline c10::MaybeOwned<Tensor> expand_inplace(const Tensor& tensor, const Tensor& to_expand) {
  77. if (tensor.sizes().equals(to_expand.sizes())) {
  78. return c10::MaybeOwned<Tensor>::borrowed(to_expand);
  79. }
  80. return c10::MaybeOwned<Tensor>::owned(to_expand.expand(tensor.sizes()));
  81. }
  82. inline c10::MaybeOwned<Tensor> expand_inplace(const Tensor& tensor, Tensor&& to_expand) = delete;
  83. inline c10::MaybeOwned<Tensor> expand_inplace(const Tensor &tensor, const Tensor &to_expand, const char *api_name) {
  84. check_defined({tensor, to_expand}, api_name);
  85. return expand_inplace(tensor, to_expand);
  86. }
  87. inline c10::MaybeOwned<Tensor> expand_inplace(const Tensor& tensor, Tensor&& to_expand, const char *api_name) = delete;
  88. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>> expand_inplace(const Tensor &tensor, const Tensor &to_expand1, const Tensor &to_expand2) {
  89. if (tensor.sizes().equals(to_expand1.sizes()) && tensor.sizes().equals((to_expand2.sizes()))) {
  90. return std::make_tuple(
  91. c10::MaybeOwned<Tensor>::borrowed(to_expand1),
  92. c10::MaybeOwned<Tensor>::borrowed(to_expand2));
  93. }
  94. return std::make_tuple(
  95. c10::MaybeOwned<Tensor>::owned(to_expand1.expand(tensor.sizes())),
  96. c10::MaybeOwned<Tensor>::owned(to_expand2.expand(tensor.sizes())));
  97. }
  98. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>> expand_inplace(const Tensor &tensor, Tensor &&to_expand1, const Tensor &to_expand2) = delete;
  99. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>> expand_inplace(const Tensor &tensor, const Tensor &to_expand1, Tensor &&to_expand2) = delete;
  100. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>> expand_inplace(const Tensor &tensor, Tensor &&to_expand1, Tensor &&to_expand2) = delete;
  101. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>> expand_inplace(const Tensor &tensor, const Tensor &to_expand1, const Tensor &to_expand2,
  102. const char *api_name) {
  103. check_defined({tensor, to_expand1, to_expand2}, api_name);
  104. return expand_inplace(tensor, to_expand1, to_expand2);
  105. }
  106. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>> expand_inplace(const Tensor &tensor, Tensor &&to_expand1, const Tensor &to_expand2, const char *api_name) = delete;
  107. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>> expand_inplace(const Tensor &tensor, const Tensor &to_expand1, Tensor &&to_expand2, const char *api_name) = delete;
  108. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>> expand_inplace(const Tensor &tensor, Tensor &&to_expand1, Tensor &&to_expand2, const char *api_name) = delete;
  109. // See NOTE [ ExpandUtils Borrowing ] above for `MaybeOwned` explanation.
  110. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  111. expand_outplace(const Tensor &to_expand1, const Tensor &to_expand2) {
  112. if (to_expand1.sizes().equals(to_expand2.sizes())) {
  113. return std::make_tuple(
  114. c10::MaybeOwned<Tensor>::borrowed(to_expand1),
  115. c10::MaybeOwned<Tensor>::borrowed(to_expand2));
  116. }
  117. auto expanded_size = infer_size_dimvector(to_expand1.sizes(), to_expand2.sizes());
  118. return std::make_tuple(
  119. c10::MaybeOwned<Tensor>::owned(to_expand1.expand(expanded_size)),
  120. c10::MaybeOwned<Tensor>::owned(to_expand2.expand(expanded_size)));
  121. }
  122. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  123. expand_outplace(Tensor &&to_expand1, const Tensor &to_expand2) = delete;
  124. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  125. expand_outplace(const Tensor &to_expand1, Tensor &&to_expand2) = delete;
  126. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  127. expand_outplace(Tensor &&to_expand1, Tensor &&to_expand2) = delete;
  128. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  129. expand_outplace(const Tensor &to_expand1, const Tensor &to_expand2, const char *api_name) {
  130. check_defined({to_expand1, to_expand2}, api_name);
  131. return expand_outplace(to_expand1, to_expand2);
  132. }
  133. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  134. expand_outplace(Tensor &&to_expand1, const Tensor &to_expand2, const char *api_name) = delete;
  135. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  136. expand_outplace(const Tensor &to_expand1, Tensor &&to_expand2, const char *api_name) = delete;
  137. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  138. expand_outplace(Tensor &&to_expand1, Tensor &&to_expand2, const char *api_name) = delete;
  139. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  140. expand_outplace(const Tensor &to_expand1,
  141. const Tensor &to_expand2,
  142. const Tensor &to_expand3) {
  143. if (to_expand1.sizes().equals(to_expand2.sizes()) && to_expand1.sizes().equals(to_expand3.sizes())) {
  144. return std::make_tuple(
  145. c10::MaybeOwned<Tensor>::borrowed(to_expand1),
  146. c10::MaybeOwned<Tensor>::borrowed(to_expand2),
  147. c10::MaybeOwned<Tensor>::borrowed(to_expand3));
  148. }
  149. auto expanded_size12 = infer_size_dimvector(to_expand1.sizes(), to_expand2.sizes());
  150. auto expanded_size = infer_size_dimvector(expanded_size12, to_expand3.sizes());
  151. return std::make_tuple(
  152. c10::MaybeOwned<Tensor>::owned(to_expand1.expand(expanded_size)),
  153. c10::MaybeOwned<Tensor>::owned(to_expand2.expand(expanded_size)),
  154. c10::MaybeOwned<Tensor>::owned(to_expand3.expand(expanded_size)));
  155. }
  156. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  157. expand_outplace(Tensor &&to_expand1,
  158. const Tensor &to_expand2,
  159. const Tensor &to_expand3) = delete;
  160. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  161. expand_outplace(const Tensor &to_expand1,
  162. Tensor &&to_expand2,
  163. const Tensor &to_expand3) = delete;
  164. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  165. expand_outplace(Tensor &&to_expand1,
  166. Tensor &&to_expand2,
  167. const Tensor &to_expand3) = delete;
  168. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  169. expand_outplace(const Tensor &to_expand1,
  170. const Tensor &to_expand2,
  171. Tensor &&to_expand3) = delete;
  172. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  173. expand_outplace(Tensor &&to_expand1,
  174. const Tensor &to_expand2,
  175. Tensor &&to_expand3) = delete;
  176. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  177. expand_outplace(const Tensor &to_expand1,
  178. Tensor &&to_expand2,
  179. Tensor &&to_expand3) = delete;
  180. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  181. expand_outplace(Tensor &&to_expand1,
  182. Tensor &&to_expand2,
  183. Tensor &&to_expand3) = delete;
  184. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  185. expand_outplace(const Tensor &to_expand1,
  186. const Tensor &to_expand2,
  187. const Tensor &to_expand3,
  188. const char *api_name) {
  189. check_defined({to_expand1, to_expand2, to_expand3}, api_name);
  190. return expand_outplace(to_expand1, to_expand2, to_expand3);
  191. }
  192. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  193. expand_outplace(Tensor &&to_expand1,
  194. const Tensor &to_expand2,
  195. const Tensor &to_expand3, const char *api_name) = delete;
  196. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  197. expand_outplace(const Tensor &to_expand1,
  198. Tensor &&to_expand2,
  199. const Tensor &to_expand3, const char *api_name) = delete;
  200. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  201. expand_outplace(Tensor &&to_expand1,
  202. Tensor &&to_expand2,
  203. const Tensor &to_expand3, const char *api_name) = delete;
  204. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  205. expand_outplace(const Tensor &to_expand1,
  206. const Tensor &to_expand2,
  207. Tensor &&to_expand3, const char *api_name) = delete;
  208. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  209. expand_outplace(Tensor &&to_expand1,
  210. const Tensor &to_expand2,
  211. Tensor &&to_expand3, const char *api_name) = delete;
  212. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  213. expand_outplace(const Tensor &to_expand1,
  214. Tensor &&to_expand2,
  215. Tensor &&to_expand3, const char *api_name) = delete;
  216. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  217. expand_outplace(Tensor &&to_expand1,
  218. Tensor &&to_expand2,
  219. Tensor &&to_expand3, const char *api_name) = delete;
  220. inline c10::MaybeOwned<Tensor> expand_size(const Tensor &to_expand, IntArrayRef sizes) {
  221. if (to_expand.sizes().equals(sizes)) {
  222. return c10::MaybeOwned<Tensor>::borrowed(to_expand);
  223. }
  224. return c10::MaybeOwned<Tensor>::owned(to_expand.expand(sizes));
  225. }
  226. inline c10::MaybeOwned<Tensor> expand_size(Tensor &&to_expand, IntArrayRef sizes) = delete;
  227. inline c10::MaybeOwned<Tensor> expand_size(const Tensor &to_expand, IntArrayRef sizes, const char *api_name) {
  228. check_defined({to_expand}, api_name);
  229. return expand_size(to_expand, sizes);
  230. }
  231. inline c10::MaybeOwned<Tensor> expand_size(Tensor &&to_expand, IntArrayRef sizes, const char *api_name) = delete;
  232. inline std::vector<Tensor> expand_outplace(TensorList to_expand) {
  233. // expands a list of Tensors; ignores undefined (null) tensors
  234. bool first = true;
  235. DimVector sizes;
  236. for (const auto i : c10::irange(to_expand.size())) {
  237. if (!to_expand[i].defined()) {
  238. continue;
  239. } else if (first) {
  240. sizes = to_expand[i].sizes();
  241. first = false;
  242. } else {
  243. sizes = infer_size_dimvector(sizes, to_expand[i].sizes());
  244. }
  245. }
  246. std::vector<Tensor> result(to_expand.size());
  247. for (const auto i : c10::irange(to_expand.size())) {
  248. if (!to_expand[i].defined()) {
  249. continue;
  250. } else if (to_expand[i].sizes().equals(sizes)) {
  251. result[i] = to_expand[i];
  252. } else {
  253. result[i] = to_expand[i].expand(sizes);
  254. }
  255. }
  256. return result;
  257. }
  258. // Sums `tensor` repeatedly to produce a tensor of shape `shape`.
  259. // Precondition: is_expandable_to(shape, tensor.sizes()) must be true
  260. static inline Tensor sum_to(Tensor tensor, const IntArrayRef shape, bool always_return_non_view=false) {
  261. if (shape.size() == 0) {
  262. return tensor.sum();
  263. }
  264. c10::SmallVector<int64_t, 8> reduce_dims;
  265. const at::IntArrayRef sizes = tensor.sizes();
  266. const int64_t leading_dims = sizes.size() - shape.size();
  267. for (const auto i : c10::irange(leading_dims)) {
  268. reduce_dims.push_back(i);
  269. }
  270. for (int64_t i = leading_dims; i < static_cast<int64_t>(sizes.size()); ++i) {
  271. if (shape[i - leading_dims] == 1 && sizes[i] != 1) {
  272. reduce_dims.push_back(i);
  273. }
  274. }
  275. if (!reduce_dims.empty()) {
  276. tensor = tensor.sum(reduce_dims, /*keepdim=*/true);
  277. }
  278. if (always_return_non_view) {
  279. // This is only actually used by the functionalization pass.
  280. // We want to be able to guarantee that this function doesn't return a view of the input.
  281. return leading_dims > 0 ? at::view_copy(tensor, shape) : tensor.clone();
  282. } else {
  283. return leading_dims > 0 ? tensor.view(shape) : tensor;
  284. }
  285. }
  286. // True if `shape` can be broadcasted to `desired`
  287. static inline bool is_expandable_to(IntArrayRef shape, IntArrayRef desired) {
  288. size_t ndim = shape.size();
  289. size_t target_dim = desired.size();
  290. if (ndim > target_dim) {
  291. return false;
  292. }
  293. for (const auto i : c10::irange(ndim)) {
  294. int64_t size = shape[ndim - i - 1];
  295. int64_t target = desired[target_dim - i - 1];
  296. if (size != target && size != 1) {
  297. return false;
  298. }
  299. }
  300. return true;
  301. }
  302. }