ExpandUtils.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #pragma once
  2. #ifndef AT_PER_OPERATOR_HEADERS
  3. #include <ATen/Functions.h>
  4. #else
  5. #include <ATen/ops/view.h>
  6. #include <ATen/ops/view_copy.h>
  7. #endif
  8. #include <ATen/Tensor.h>
  9. #include <ATen/core/DimVector.h>
  10. #include <c10/util/Exception.h>
  11. #include <c10/util/MaybeOwned.h>
  12. #include <c10/util/irange.h>
  13. #include <functional>
  14. #include <tuple>
  15. #include <utility>
  16. namespace at {
  17. TORCH_API std::vector<int64_t> infer_size(IntArrayRef a, IntArrayRef b);
  18. TORCH_API std::vector<SymInt> infer_size_symint(
  19. SymIntArrayRef a,
  20. SymIntArrayRef b);
  21. TORCH_API DimVector infer_size_dimvector(IntArrayRef a, IntArrayRef b);
  22. TORCH_API SymDimVector
  23. infer_size_symdimvector(SymIntArrayRef a, SymIntArrayRef b);
  24. // Named type instead of a pair/tuple so that we can be sure to
  25. // construct the vectors in place and get NRVO.
  26. template <typename Container>
  27. struct InferExpandGeometryResult {
  28. Container sizes;
  29. Container strides;
  30. explicit InferExpandGeometryResult(size_t ndim)
  31. : sizes(ndim), strides(ndim) {}
  32. explicit InferExpandGeometryResult(IntArrayRef sizes_, size_t ndim)
  33. : sizes(sizes_.begin(), sizes_.end()), strides(ndim) {}
  34. };
  35. TORCH_API std::tuple<std::vector<int64_t>, std::vector<int64_t>>
  36. inferExpandGeometry(
  37. IntArrayRef tensor_sizes,
  38. IntArrayRef tensor_strides,
  39. IntArrayRef sizes);
  40. TORCH_API InferExpandGeometryResult<DimVector> inferExpandGeometry_dimvector(
  41. IntArrayRef tensor_sizes,
  42. IntArrayRef tensor_strides,
  43. IntArrayRef sizes);
  44. TORCH_API std::vector<int64_t> infer_dense_strides(
  45. IntArrayRef tensor_sizes,
  46. IntArrayRef tensor_strides);
  47. // True if input shapes are expandable
  48. // NOTE: infer_size did a similar check, please keep them sync if change is
  49. // needed
  50. inline bool are_expandable(IntArrayRef shape1, IntArrayRef shape2) {
  51. size_t ndim1 = shape1.size();
  52. size_t ndim2 = shape2.size();
  53. size_t ndim = ndim1 < ndim2 ? ndim1 : ndim2;
  54. for (int64_t i = static_cast<int64_t>(ndim) - 1; i >= 0; --i) {
  55. if (shape1[--ndim1] == shape2[--ndim2] || shape1[ndim1] == 1 ||
  56. shape2[ndim2] == 1) {
  57. continue;
  58. }
  59. return false;
  60. }
  61. return true;
  62. }
  63. // avoid copy-construction of Tensor by using a reference_wrapper.
  64. inline void check_defined(
  65. std::initializer_list<std::reference_wrapper<const Tensor>> tensors,
  66. const char* api_name) {
  67. for (auto& t : tensors) {
  68. if (!t.get().defined()) {
  69. TORCH_CHECK(false, api_name, "(...) called with an undefined Tensor");
  70. }
  71. }
  72. }
  73. // NOTE [ ExpandUtils Borrowing ]
  74. //
  75. // Functions in ExpandUtils return `c10::MaybeOwned<Tensor>` because
  76. // expansion may not actually be needed, in which case we can improve
  77. // efficiency by returning
  78. // `c10::MaybeOwned<Tensor>::borrowed(to_expand)`. However, this means
  79. // that you need to be careful: the returned `c10::MaybeOwned<Tensor>`
  80. // must not outlive the original `Tensor` object that `to_expand`
  81. // referred to! The deleted rvalue reference overloads of these
  82. // functions help with this by preventing trivial use of a temporary
  83. // resulting from a function call, but it is still possible to make a
  84. // mistake.
  85. inline c10::MaybeOwned<Tensor> expand_inplace(
  86. const Tensor& tensor,
  87. const Tensor& to_expand) {
  88. if (tensor.sym_sizes().equals(to_expand.sym_sizes())) {
  89. return c10::MaybeOwned<Tensor>::borrowed(to_expand);
  90. }
  91. return c10::MaybeOwned<Tensor>::owned(
  92. to_expand.expand_symint(tensor.sym_sizes()));
  93. }
  94. inline c10::MaybeOwned<Tensor> expand_inplace(
  95. const Tensor& tensor,
  96. Tensor&& to_expand) = delete;
  97. inline c10::MaybeOwned<Tensor> expand_inplace(
  98. const Tensor& tensor,
  99. const Tensor& to_expand,
  100. const char* api_name) {
  101. check_defined({tensor, to_expand}, api_name);
  102. return expand_inplace(tensor, to_expand);
  103. }
  104. inline c10::MaybeOwned<Tensor> expand_inplace(
  105. const Tensor& tensor,
  106. Tensor&& to_expand,
  107. const char* api_name) = delete;
  108. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  109. expand_inplace(
  110. const Tensor& tensor,
  111. const Tensor& to_expand1,
  112. const Tensor& to_expand2) {
  113. if (tensor.sizes().equals(to_expand1.sizes()) &&
  114. tensor.sizes().equals((to_expand2.sizes()))) {
  115. return std::make_tuple(
  116. c10::MaybeOwned<Tensor>::borrowed(to_expand1),
  117. c10::MaybeOwned<Tensor>::borrowed(to_expand2));
  118. }
  119. return std::make_tuple(
  120. c10::MaybeOwned<Tensor>::owned(to_expand1.expand(tensor.sizes())),
  121. c10::MaybeOwned<Tensor>::owned(to_expand2.expand(tensor.sizes())));
  122. }
  123. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  124. expand_inplace(
  125. const Tensor& tensor,
  126. Tensor&& to_expand1,
  127. const Tensor& to_expand2) = delete;
  128. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  129. expand_inplace(
  130. const Tensor& tensor,
  131. const Tensor& to_expand1,
  132. Tensor&& to_expand2) = delete;
  133. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  134. expand_inplace(const Tensor& tensor, Tensor&& to_expand1, Tensor&& to_expand2) =
  135. delete;
  136. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  137. expand_inplace(
  138. const Tensor& tensor,
  139. const Tensor& to_expand1,
  140. const Tensor& to_expand2,
  141. const char* api_name) {
  142. check_defined({tensor, to_expand1, to_expand2}, api_name);
  143. return expand_inplace(tensor, to_expand1, to_expand2);
  144. }
  145. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  146. expand_inplace(
  147. const Tensor& tensor,
  148. Tensor&& to_expand1,
  149. const Tensor& to_expand2,
  150. const char* api_name) = delete;
  151. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  152. expand_inplace(
  153. const Tensor& tensor,
  154. const Tensor& to_expand1,
  155. Tensor&& to_expand2,
  156. const char* api_name) = delete;
  157. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  158. expand_inplace(
  159. const Tensor& tensor,
  160. Tensor&& to_expand1,
  161. Tensor&& to_expand2,
  162. const char* api_name) = delete;
  163. // See NOTE [ ExpandUtils Borrowing ] above for `MaybeOwned` explanation.
  164. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  165. expand_outplace(const Tensor& to_expand1, const Tensor& to_expand2) {
  166. auto s1 = to_expand1.sym_sizes();
  167. auto s2 = to_expand2.sym_sizes();
  168. if (s1.equals(s2)) {
  169. return std::make_tuple(
  170. c10::MaybeOwned<Tensor>::borrowed(to_expand1),
  171. c10::MaybeOwned<Tensor>::borrowed(to_expand2));
  172. }
  173. auto expanded_size = infer_size_symdimvector(s1, s2);
  174. return std::make_tuple(
  175. c10::MaybeOwned<Tensor>::owned(to_expand1.expand_symint(expanded_size)),
  176. c10::MaybeOwned<Tensor>::owned(to_expand2.expand_symint(expanded_size)));
  177. }
  178. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  179. expand_outplace(Tensor&& to_expand1, const Tensor& to_expand2) = delete;
  180. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  181. expand_outplace(const Tensor& to_expand1, Tensor&& to_expand2) = delete;
  182. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  183. expand_outplace(Tensor&& to_expand1, Tensor&& to_expand2) = delete;
  184. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  185. expand_outplace(
  186. const Tensor& to_expand1,
  187. const Tensor& to_expand2,
  188. const char* api_name) {
  189. check_defined({to_expand1, to_expand2}, api_name);
  190. return expand_outplace(to_expand1, to_expand2);
  191. }
  192. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  193. expand_outplace(
  194. Tensor&& to_expand1,
  195. const Tensor& to_expand2,
  196. const char* api_name) = delete;
  197. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  198. expand_outplace(
  199. const Tensor& to_expand1,
  200. Tensor&& to_expand2,
  201. const char* api_name) = delete;
  202. inline std::tuple<c10::MaybeOwned<Tensor>, c10::MaybeOwned<Tensor>>
  203. expand_outplace(
  204. Tensor&& to_expand1,
  205. Tensor&& to_expand2,
  206. const char* api_name) = delete;
  207. inline std::tuple<
  208. c10::MaybeOwned<Tensor>,
  209. c10::MaybeOwned<Tensor>,
  210. c10::MaybeOwned<Tensor>>
  211. expand_outplace(
  212. const Tensor& to_expand1,
  213. const Tensor& to_expand2,
  214. const Tensor& to_expand3) {
  215. if (to_expand1.sizes().equals(to_expand2.sizes()) &&
  216. to_expand1.sizes().equals(to_expand3.sizes())) {
  217. return std::make_tuple(
  218. c10::MaybeOwned<Tensor>::borrowed(to_expand1),
  219. c10::MaybeOwned<Tensor>::borrowed(to_expand2),
  220. c10::MaybeOwned<Tensor>::borrowed(to_expand3));
  221. }
  222. auto expanded_size12 =
  223. infer_size_dimvector(to_expand1.sizes(), to_expand2.sizes());
  224. auto expanded_size =
  225. infer_size_dimvector(expanded_size12, to_expand3.sizes());
  226. return std::make_tuple(
  227. c10::MaybeOwned<Tensor>::owned(to_expand1.expand(expanded_size)),
  228. c10::MaybeOwned<Tensor>::owned(to_expand2.expand(expanded_size)),
  229. c10::MaybeOwned<Tensor>::owned(to_expand3.expand(expanded_size)));
  230. }
  231. inline std::tuple<
  232. c10::MaybeOwned<Tensor>,
  233. c10::MaybeOwned<Tensor>,
  234. c10::MaybeOwned<Tensor>>
  235. expand_outplace(
  236. Tensor&& to_expand1,
  237. const Tensor& to_expand2,
  238. const Tensor& to_expand3) = delete;
  239. inline std::tuple<
  240. c10::MaybeOwned<Tensor>,
  241. c10::MaybeOwned<Tensor>,
  242. c10::MaybeOwned<Tensor>>
  243. expand_outplace(
  244. const Tensor& to_expand1,
  245. Tensor&& to_expand2,
  246. const Tensor& to_expand3) = delete;
  247. inline std::tuple<
  248. c10::MaybeOwned<Tensor>,
  249. c10::MaybeOwned<Tensor>,
  250. c10::MaybeOwned<Tensor>>
  251. expand_outplace(
  252. Tensor&& to_expand1,
  253. Tensor&& to_expand2,
  254. const Tensor& to_expand3) = delete;
  255. inline std::tuple<
  256. c10::MaybeOwned<Tensor>,
  257. c10::MaybeOwned<Tensor>,
  258. c10::MaybeOwned<Tensor>>
  259. expand_outplace(
  260. const Tensor& to_expand1,
  261. const Tensor& to_expand2,
  262. Tensor&& to_expand3) = delete;
  263. inline std::tuple<
  264. c10::MaybeOwned<Tensor>,
  265. c10::MaybeOwned<Tensor>,
  266. c10::MaybeOwned<Tensor>>
  267. expand_outplace(
  268. Tensor&& to_expand1,
  269. const Tensor& to_expand2,
  270. Tensor&& to_expand3) = delete;
  271. inline std::tuple<
  272. c10::MaybeOwned<Tensor>,
  273. c10::MaybeOwned<Tensor>,
  274. c10::MaybeOwned<Tensor>>
  275. expand_outplace(
  276. const Tensor& to_expand1,
  277. Tensor&& to_expand2,
  278. Tensor&& to_expand3) = delete;
  279. inline std::tuple<
  280. c10::MaybeOwned<Tensor>,
  281. c10::MaybeOwned<Tensor>,
  282. c10::MaybeOwned<Tensor>>
  283. expand_outplace(Tensor&& to_expand1, Tensor&& to_expand2, Tensor&& to_expand3) =
  284. delete;
  285. inline std::tuple<
  286. c10::MaybeOwned<Tensor>,
  287. c10::MaybeOwned<Tensor>,
  288. c10::MaybeOwned<Tensor>>
  289. expand_outplace(
  290. const Tensor& to_expand1,
  291. const Tensor& to_expand2,
  292. const Tensor& to_expand3,
  293. const char* api_name) {
  294. check_defined({to_expand1, to_expand2, to_expand3}, api_name);
  295. return expand_outplace(to_expand1, to_expand2, to_expand3);
  296. }
  297. inline std::tuple<
  298. c10::MaybeOwned<Tensor>,
  299. c10::MaybeOwned<Tensor>,
  300. c10::MaybeOwned<Tensor>>
  301. expand_outplace(
  302. Tensor&& to_expand1,
  303. const Tensor& to_expand2,
  304. const Tensor& to_expand3,
  305. const char* api_name) = delete;
  306. inline std::tuple<
  307. c10::MaybeOwned<Tensor>,
  308. c10::MaybeOwned<Tensor>,
  309. c10::MaybeOwned<Tensor>>
  310. expand_outplace(
  311. const Tensor& to_expand1,
  312. Tensor&& to_expand2,
  313. const Tensor& to_expand3,
  314. const char* api_name) = delete;
  315. inline std::tuple<
  316. c10::MaybeOwned<Tensor>,
  317. c10::MaybeOwned<Tensor>,
  318. c10::MaybeOwned<Tensor>>
  319. expand_outplace(
  320. Tensor&& to_expand1,
  321. Tensor&& to_expand2,
  322. const Tensor& to_expand3,
  323. const char* api_name) = delete;
  324. inline std::tuple<
  325. c10::MaybeOwned<Tensor>,
  326. c10::MaybeOwned<Tensor>,
  327. c10::MaybeOwned<Tensor>>
  328. expand_outplace(
  329. const Tensor& to_expand1,
  330. const Tensor& to_expand2,
  331. Tensor&& to_expand3,
  332. const char* api_name) = delete;
  333. inline std::tuple<
  334. c10::MaybeOwned<Tensor>,
  335. c10::MaybeOwned<Tensor>,
  336. c10::MaybeOwned<Tensor>>
  337. expand_outplace(
  338. Tensor&& to_expand1,
  339. const Tensor& to_expand2,
  340. Tensor&& to_expand3,
  341. const char* api_name) = delete;
  342. inline std::tuple<
  343. c10::MaybeOwned<Tensor>,
  344. c10::MaybeOwned<Tensor>,
  345. c10::MaybeOwned<Tensor>>
  346. expand_outplace(
  347. const Tensor& to_expand1,
  348. Tensor&& to_expand2,
  349. Tensor&& to_expand3,
  350. const char* api_name) = delete;
  351. inline std::tuple<
  352. c10::MaybeOwned<Tensor>,
  353. c10::MaybeOwned<Tensor>,
  354. c10::MaybeOwned<Tensor>>
  355. expand_outplace(
  356. Tensor&& to_expand1,
  357. Tensor&& to_expand2,
  358. Tensor&& to_expand3,
  359. const char* api_name) = delete;
  360. inline c10::MaybeOwned<Tensor> expand_size(
  361. const Tensor& to_expand,
  362. IntArrayRef sizes) {
  363. if (to_expand.sizes().equals(sizes)) {
  364. return c10::MaybeOwned<Tensor>::borrowed(to_expand);
  365. }
  366. return c10::MaybeOwned<Tensor>::owned(to_expand.expand(sizes));
  367. }
  368. inline c10::MaybeOwned<Tensor> expand_size(
  369. Tensor&& to_expand,
  370. IntArrayRef sizes) = delete;
  371. inline c10::MaybeOwned<Tensor> expand_size(
  372. const Tensor& to_expand,
  373. IntArrayRef sizes,
  374. const char* api_name) {
  375. check_defined({to_expand}, api_name);
  376. return expand_size(to_expand, sizes);
  377. }
  378. inline c10::MaybeOwned<Tensor> expand_size(
  379. Tensor&& to_expand,
  380. IntArrayRef sizes,
  381. const char* api_name) = delete;
  382. inline std::vector<Tensor> expand_outplace(TensorList to_expand) {
  383. // expands a list of Tensors; ignores undefined (null) tensors
  384. bool first = true;
  385. SymDimVector sizes;
  386. for (const auto i : c10::irange(to_expand.size())) {
  387. if (!to_expand[i].defined()) {
  388. continue;
  389. } else if (first) {
  390. sizes = to_expand[i].sym_sizes();
  391. first = false;
  392. } else {
  393. sizes = infer_size_symdimvector(sizes, to_expand[i].sym_sizes());
  394. }
  395. }
  396. std::vector<Tensor> result(to_expand.size());
  397. for (const auto i : c10::irange(to_expand.size())) {
  398. if (!to_expand[i].defined()) {
  399. continue;
  400. } else if (to_expand[i].sym_sizes().equals(sizes)) {
  401. result[i] = to_expand[i];
  402. } else {
  403. result[i] = to_expand[i].expand_symint(sizes);
  404. }
  405. }
  406. return result;
  407. }
  408. template <typename T>
  409. inline Tensor _sum_to(
  410. Tensor tensor,
  411. const c10::ArrayRef<T> shape,
  412. bool always_return_non_view = false) {
  413. if (shape.size() == 0) {
  414. return tensor.sum();
  415. }
  416. auto sizes = at::symint::sizes<T>(tensor);
  417. c10::SmallVector<int64_t, 8> reduce_dims;
  418. const int64_t leading_dims = sizes.size() - shape.size();
  419. for (const auto i : c10::irange(leading_dims)) {
  420. reduce_dims.push_back(i);
  421. }
  422. for (int64_t i = leading_dims; i < static_cast<int64_t>(sizes.size()); ++i) {
  423. if (TORCH_GUARD_OR_FALSE(sym_eq(shape[i - leading_dims], 1)) &&
  424. TORCH_GUARD_OR_TRUE(sym_ne(sizes[i], 1))) {
  425. reduce_dims.push_back(i);
  426. } else {
  427. // if we assume no reduction due to unbacked we ensure that at runtime.
  428. TORCH_MAYBE_SYM_CHECK(
  429. sym_eq(shape[i - leading_dims], sizes[i]),
  430. "non-reduction path was assumed due to unabcked symbols expected those two sizes to be the same:",
  431. shape[i - leading_dims],
  432. ", ",
  433. sizes[i])
  434. }
  435. }
  436. if (!reduce_dims.empty()) {
  437. tensor = tensor.sum(reduce_dims, /*keepdim=*/true);
  438. }
  439. if (always_return_non_view) {
  440. // This is only actually used by the functionalization pass.
  441. // We want to be able to guarantee that this function doesn't return a view
  442. // of the input.
  443. return leading_dims > 0 ? at::symint::view_copy<T>(tensor, shape)
  444. : tensor.clone();
  445. } else {
  446. return leading_dims > 0 ? at::symint::view<T>(tensor, shape) : tensor;
  447. }
  448. }
  449. inline Tensor sum_to(
  450. Tensor tensor,
  451. const c10::SymIntArrayRef shape,
  452. bool always_return_non_view = false) {
  453. return _sum_to(std::move(tensor), shape, always_return_non_view);
  454. }
  455. // Sums `tensor` repeatedly to produce a tensor of shape `shape`.
  456. // Precondition: is_expandable_to(shape, tensor.sizes()) must be true
  457. inline Tensor sum_to(
  458. Tensor tensor,
  459. const IntArrayRef shape,
  460. bool always_return_non_view = false) {
  461. return _sum_to(std::move(tensor), shape, always_return_non_view);
  462. }
  463. inline bool is_expandable_to(
  464. SymIntArrayRef shape,
  465. c10::SymIntArrayRef desired) {
  466. size_t ndim = shape.size();
  467. size_t target_dim = desired.size();
  468. if (ndim > target_dim) {
  469. return false;
  470. }
  471. for (const auto i : c10::irange(ndim)) {
  472. const auto& size = shape[ndim - i - 1];
  473. const auto& target = desired[target_dim - i - 1];
  474. if (size != target && size != 1) {
  475. return false;
  476. }
  477. }
  478. return true;
  479. }
  480. inline bool is_expandable_to(IntArrayRef shape, IntArrayRef desired) {
  481. auto sym_shape = c10::SymIntArrayRef(
  482. reinterpret_cast<const c10::SymInt*>(shape.data()), shape.size());
  483. auto sym_desired = c10::SymIntArrayRef(
  484. reinterpret_cast<const c10::SymInt*>(desired.data()), desired.size());
  485. return is_expandable_to(sym_shape, sym_desired);
  486. }
  487. } // namespace at