BatchRulesHelper.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // Copyright (c) Facebook, Inc. and its affiliates.
  2. // All rights reserved.
  3. //
  4. // This source code is licensed under the BSD-style license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. #pragma once
  7. #include <c10/util/TypeList.h>
  8. #include <ATen/ATen.h>
  9. #include <ATen/Operators.h>
  10. #include <ATen/functorch/DynamicLayer.h>
  11. #include <ATen/functorch/TensorWrapper.h>
  12. #include <ATen/functorch/BatchingMetaprogramming.h>
  13. #include <ATen/functorch/LegacyVmapTransforms.h>
  14. #include <ATen/functorch/BatchedFallback.h>
  15. #include <ATen/functorch/PlumbingHelper.h>
  16. #include <ATen/core/dispatch/Dispatcher.h>
  17. #include <ATen/VmapGeneratedPlumbing.h>
  18. #include <utility>
  19. // This file contains helper functions for batching rules.
  20. namespace at { namespace functorch {
  21. TORCH_API Tensor reshape_dim_into(int64_t src, int64_t dst, const Tensor& x);
  22. TORCH_API Tensor reshape_dim_outof(int64_t src, int64_t size1, const Tensor& x);
  23. TORCH_API Tensor reshape_dim_outof_symint(int64_t src, c10::SymInt size1, const Tensor& x);
  24. Tensor moveBatchDimToFront(const Tensor& tensor, optional<int64_t> maybe_batch_dim);
  25. int64_t rankWithoutBatchDim(const Tensor& tensor, optional<int64_t> maybe_batch_dim);
  26. int64_t numelWithoutBatchDim(const Tensor& tensor, optional<int64_t> maybe_batch_dim);
  27. optional<int64_t> valIfNonempty(optional<int64_t> maybe_empty, int64_t new_val);
  28. int64_t getPhysicalDim(const Tensor& tensor, bool has_batch_dim, int64_t logical_dim);
  29. VmapDimVector getPhysicalDims(const Tensor& tensor, bool has_batch_dim, IntArrayRef logical_dims);
  30. void vmapIncompatibleInplaceError(const char* schema_name);
  31. Tensor maybePadToLogicalRank(const Tensor& tensor, optional<int64_t> has_bdim, int64_t logical_rank);
  32. void check_randomness(RandomnessType randomness);
  33. void check_randomness(RandomnessType randomness, bool any_tensor_bdim);
  34. inline Tensor ensure_has_bdim(const Tensor& tensor, bool has_bdim, int64_t batch_size) {
  35. if (has_bdim) {
  36. return tensor;
  37. }
  38. const auto sizes = tensor.sizes();
  39. DimVector expanded_shape;
  40. expanded_shape.reserve(sizes.size());
  41. expanded_shape.emplace_back(batch_size);
  42. expanded_shape.insert(expanded_shape.end(), sizes.begin(), sizes.end());
  43. return tensor.expand(expanded_shape);
  44. }
  45. #define VMAP_SUPPORT(op, batch_rule) \
  46. m.impl(#op, op ## _generated_plumbing<decltype(&batch_rule), &batch_rule>);
  47. #define VMAP_SUPPORT2(op, overload, batch_rule) \
  48. m.impl(#op "." #overload, op ## _ ## overload ## _generated_plumbing<decltype(&batch_rule), &batch_rule>);
  49. #define OP_DECOMPOSE(op) m.impl(#op, static_cast<decltype(&ATEN_FN(op))>(native::op));
  50. #define OP_DECOMPOSE2(op, overload) m.impl(#op"."#overload, static_cast<decltype(&ATEN_FN2(op, overload))>(native::op));
  51. // DO NOT USE ME DIRECTLY! Use BASIC_UNARY_BATCH_RULE to save yourself some pain
  52. template <typename A, A a, typename C>
  53. struct BasicUnaryBatchRuleHelper;
  54. template <typename F, F Func, typename A, typename... T>
  55. struct BasicUnaryBatchRuleHelper<F, Func, c10::guts::typelist::typelist<A, T...>> {
  56. static std::tuple<Tensor,optional<int64_t>> apply(
  57. const Tensor& tensor,
  58. optional<int64_t> batch_dim,
  59. T... extra_args) {
  60. return std::make_tuple(Func(tensor, std::forward<T>(extra_args)...), batch_dim);
  61. }
  62. };
  63. // USAGE: BASIC_UNARY_BATCH_RULE(at::sin)
  64. // INCORRECT USAGE: BASIC_UNARY_BATCH_RULE(&at::sin)
  65. // It is important that this macro is not passed a function pointer!!
  66. #define BASIC_UNARY_BATCH_RULE(fn) SINGLE_ARG(\
  67. BasicUnaryBatchRuleHelper<\
  68. decltype(&fn),\
  69. &fn,\
  70. c10::guts::function_traits<decltype(fn)>::parameter_types>::apply)
  71. #define UNARY_POINTWISE(op) \
  72. VMAP_SUPPORT(op, BASIC_UNARY_BATCH_RULE(ATEN_FN(op)));
  73. template <typename A, A a, typename C>
  74. struct VariadicBdimsBatchRuleHelper;
  75. template <typename F, F Func, typename A, typename... T>
  76. struct VariadicBdimsBatchRuleHelper<F, Func, c10::guts::typelist::typelist<A, T...>> {
  77. static std::tuple<Tensor,optional<int64_t>> apply(
  78. const Tensor& tensor,
  79. optional<int64_t> batch_dim,
  80. T... extra_args) {
  81. auto tensor_ = moveBatchDimToFront(tensor, batch_dim);
  82. return std::make_tuple(Func(tensor_, std::forward<T>(extra_args)...), 0);
  83. }
  84. };
  85. // USAGE: VARIADIC_BDIMS_BATCH_RULE(at::cholesky_inverse)
  86. // INCORRECT USAGE: VARIADIC_BDIMS_BATCH_RULE(&at::cholesky_inverse)
  87. // It is important that this macro is not passed a function pointer!!
  88. #define VARIADIC_BDIMS_BATCH_RULE(fn) SINGLE_ARG(\
  89. VariadicBdimsBatchRuleHelper<\
  90. decltype(&fn),\
  91. &fn,\
  92. c10::guts::function_traits<decltype(fn)>::parameter_types>::apply)
  93. #define VARIADIC_BDIMS(op) \
  94. VMAP_SUPPORT(op, VARIADIC_BDIMS_BATCH_RULE(ATEN_FN(op)));
  95. #define VARIADIC_BDIMS2(op, overload) \
  96. VMAP_SUPPORT2(op, overload, VARIADIC_BDIMS_BATCH_RULE(ATEN_FN2(op, overload)));
  97. template<class F, F Func>
  98. void boxed_tensor_inputs_batch_rule(const c10::OperatorHandle& op, torch::jit::Stack* stack) {
  99. const auto& schema = op.schema();
  100. const auto num_returns = schema.returns().size();
  101. const auto num_arguments = schema.arguments().size();
  102. c10::impl::ExcludeDispatchKeyGuard guard(DispatchKey::FuncTorchBatched);
  103. auto maybe_layer = maybeCurrentDynamicLayer();
  104. vmap_check_escaped(maybe_layer, "boxed_tensor_inputs_batch_rule");
  105. int64_t cur_level = maybe_layer->layerId();
  106. auto orig_arguments = torch::jit::last(*stack, num_arguments);
  107. if (std::none_of(orig_arguments.begin(), orig_arguments.end(), ivalueParticipatesInCurrentLevel)) {
  108. op.callBoxed(stack);
  109. return;
  110. }
  111. auto arguments = torch::jit::pop(*stack, num_arguments);
  112. std::vector<std::pair<Tensor, optional<int64_t>>> tensor_inputs;
  113. std::vector<int64_t> tensor_pos;
  114. for (const auto idx : c10::irange(0, num_arguments)) {
  115. const auto& ivalue = arguments[idx];
  116. if (ivalue.isTensor()) {
  117. Tensor tensor_value;
  118. optional<int64_t> tensor_bdim;
  119. std::tie(tensor_value, tensor_bdim) = unwrapTensorAtLevel(ivalue.toTensor(), cur_level);
  120. tensor_inputs.emplace_back(tensor_value, tensor_bdim);
  121. tensor_pos.push_back(idx);
  122. }
  123. }
  124. Func(tensor_inputs);
  125. size_t tensor_idx = 0;
  126. TORCH_INTERNAL_ASSERT(!tensor_pos.empty());
  127. for (const auto arg_idx : c10::irange(0, num_arguments)) {
  128. if (tensor_idx >= tensor_pos.size() || (int64_t)arg_idx != tensor_pos[tensor_idx]) {
  129. torch::jit::push(stack, arguments[arg_idx]);
  130. } else {
  131. TORCH_INTERNAL_ASSERT(tensor_idx < tensor_inputs.size());
  132. torch::jit::push(stack, tensor_inputs[tensor_idx].first);
  133. tensor_idx++;
  134. }
  135. }
  136. op.callBoxed(stack);
  137. const auto returns = torch::jit::pop(*stack, num_returns);
  138. for (const auto& ret : returns) {
  139. if (ret.isTensor()) {
  140. torch::jit::push(stack, makeBatched(ret.toTensor(), 0, cur_level));
  141. } else {
  142. TORCH_INTERNAL_ASSERT(false, "This boxed batching rule does not currently support ops that return non-tensor values");
  143. }
  144. }
  145. }
  146. inline void handle_pointwise_ops(std::vector<std::pair<Tensor, optional<int64_t>>> &tensor_inputs) {
  147. int64_t out_logical_rank = 0;
  148. for (auto& tensor_input : tensor_inputs) {
  149. int64_t cur_logical_rank = rankWithoutBatchDim(tensor_input.first, tensor_input.second);
  150. out_logical_rank = std::max(out_logical_rank, cur_logical_rank);
  151. }
  152. for (auto& tensor_input: tensor_inputs) {
  153. tensor_input.first = moveBatchDimToFront(tensor_input.first, tensor_input.second);
  154. tensor_input.first = maybePadToLogicalRank(tensor_input.first, tensor_input.second, out_logical_rank);
  155. }
  156. }
  157. #define POINTWISE_BOXED(op) \
  158. m.impl(#op, torch::CppFunction::makeFromBoxedFunction<boxed_tensor_inputs_batch_rule<decltype(&handle_pointwise_ops), &handle_pointwise_ops>>());
  159. #define POINTWISE_BOXED2(op, overload) \
  160. m.impl(#op "." #overload, torch::CppFunction::makeFromBoxedFunction<boxed_tensor_inputs_batch_rule<decltype(&handle_pointwise_ops), &handle_pointwise_ops>>());
  161. inline void handle_variadic_bdims(std::vector<std::pair<Tensor, optional<int64_t>>> &tensor_inputs) {
  162. for (auto & tensor_input : tensor_inputs) {
  163. tensor_input.first = moveBatchDimToFront(tensor_input.first, tensor_input.second);
  164. }
  165. }
  166. #define VARIADIC_BDIMS_BOXED(op) \
  167. m.impl(#op, torch::CppFunction::makeFromBoxedFunction<boxed_tensor_inputs_batch_rule<decltype(&handle_variadic_bdims), &handle_variadic_bdims>>());
  168. using UnpackedBatchedTensor = std::tuple<Tensor,optional<int64_t>>;
  169. inline void find_and_unpack_tensors(
  170. const torch::jit::Stack* stack,
  171. int64_t num_args,
  172. int64_t cur_level,
  173. SmallVector<UnpackedBatchedTensor, 5>* tensors,
  174. SmallVector<int64_t, 5>* tensors_pos,
  175. int64_t* batch_size) {
  176. int64_t computed_batch_size = -1;
  177. int64_t args_begin = stack->size() - num_args;
  178. for (const auto idx : c10::irange(0, num_args)) {
  179. const auto& ivalue = (*stack)[args_begin + idx];
  180. if (!ivalue.isTensor()) {
  181. continue;
  182. }
  183. auto unpacked = unwrapTensorAtLevel(ivalue.toTensor(), cur_level);
  184. const auto& tensor_value = std::get<0>(unpacked);
  185. const auto tensor_bdim = std::get<1>(unpacked);
  186. if (tensor_bdim.has_value()) {
  187. auto candidate_batch_size = tensor_value.size(*tensor_bdim);
  188. if (computed_batch_size == -1) {
  189. computed_batch_size = candidate_batch_size;
  190. }
  191. TORCH_INTERNAL_ASSERT(candidate_batch_size == computed_batch_size);
  192. }
  193. tensors->push_back(std::move(unpacked));
  194. tensors_pos->push_back(idx);
  195. }
  196. TORCH_INTERNAL_ASSERT(computed_batch_size > -1);
  197. *batch_size = computed_batch_size;
  198. }
  199. inline void boxed_existing_bdim_all_batch_rule(
  200. const c10::OperatorHandle& op, torch::jit::Stack* stack) {
  201. const auto& schema = op.schema();
  202. const auto num_returns = schema.returns().size();
  203. const auto num_arguments = schema.arguments().size();
  204. c10::impl::ExcludeDispatchKeyGuard guard(DispatchKey::FuncTorchBatched);
  205. auto maybe_layer = maybeCurrentDynamicLayer();
  206. vmap_check_escaped(maybe_layer, "boxed_existing_bdim_all_batch_rule");
  207. int64_t cur_level = maybe_layer->layerId();
  208. const auto arguments = torch::jit::last(stack, num_arguments);
  209. if (std::none_of(arguments.begin(), arguments.end(), ivalueParticipatesInCurrentLevel)) {
  210. op.callBoxed(stack);
  211. return;
  212. }
  213. int64_t args_begin = stack->size() - num_arguments;
  214. SmallVector<UnpackedBatchedTensor, 5> tensor_inputs;
  215. SmallVector<int64_t, 5> tensor_pos;
  216. int64_t batch_size;
  217. find_and_unpack_tensors(
  218. stack, num_arguments, cur_level,
  219. &tensor_inputs, &tensor_pos, &batch_size);
  220. // for each tensor, ensure it has a bdim and reshape it.
  221. for (const auto tensor_idx : c10::irange(0, tensor_inputs.size())) {
  222. const auto& value = std::get<0>(tensor_inputs[tensor_idx]);
  223. auto bdim = std::get<1>(tensor_inputs[tensor_idx]);
  224. auto value_ = ensure_has_bdim(value, bdim.has_value(), batch_size);
  225. if (!bdim.has_value()) {
  226. bdim = 0;
  227. }
  228. (*stack)[args_begin + tensor_pos[tensor_idx]] = reshape_dim_into(*bdim, 0, value_);
  229. }
  230. op.callBoxed(stack);
  231. for (const auto idx : c10::irange(args_begin, args_begin + num_returns)) {
  232. const auto& ret = (*stack)[idx];
  233. TORCH_INTERNAL_ASSERT(ret.isTensor(),
  234. "This boxed batching rule does not currently support ops that return non-tensor values");
  235. (*stack)[idx] = makeBatched(reshape_dim_outof(0, batch_size, ret.toTensor()), 0, cur_level);
  236. }
  237. }
  238. // Use when all tensors arguments accept one (normal) batch dim.
  239. // This batching rule expands the batch dim on all Tensors, reshapes it into
  240. // dim 0, calls the op, and then reshapes the batch dim out of dim 0.
  241. // This is not the most efficient thing; if there are alternatives, plese try
  242. // to use them. Use this only as a last resort.
  243. #define EXISTING_BDIM_ALL_BOXED(op) \
  244. m.impl(#op, torch::CppFunction::makeFromBoxedFunction<boxed_existing_bdim_all_batch_rule>());
  245. template <int64_t feature_rank, int64_t contig_tensor_index=-1>
  246. inline void boxed_all_tensors_have_optional_bdim(
  247. const c10::OperatorHandle& op, torch::jit::Stack* stack) {
  248. const auto& schema = op.schema();
  249. const auto num_returns = schema.returns().size();
  250. const auto num_arguments = schema.arguments().size();
  251. c10::impl::ExcludeDispatchKeyGuard guard(DispatchKey::FuncTorchBatched);
  252. auto maybe_layer = maybeCurrentDynamicLayer();
  253. vmap_check_escaped(maybe_layer, "boxed_all_tensors_have_optional_bdim");
  254. int64_t cur_level = maybe_layer->layerId();
  255. const auto arguments = torch::jit::last(stack, num_arguments);
  256. if (std::none_of(arguments.begin(), arguments.end(), ivalueParticipatesInCurrentLevel)) {
  257. op.callBoxed(stack);
  258. return;
  259. }
  260. int64_t args_begin = stack->size() - num_arguments;
  261. SmallVector<UnpackedBatchedTensor, 5> tensor_inputs;
  262. SmallVector<int64_t, 5> tensor_pos;
  263. int64_t batch_size;
  264. find_and_unpack_tensors(
  265. stack, num_arguments, cur_level,
  266. &tensor_inputs, &tensor_pos, &batch_size);
  267. optional<bool> is_no_batch_dim_case;
  268. for (const auto tensor_idx : c10::irange(0, tensor_inputs.size())) {
  269. const auto& value = std::get<0>(tensor_inputs[tensor_idx]);
  270. auto bdim = std::get<1>(tensor_inputs[tensor_idx]);
  271. const auto logical_rank = rankWithoutBatchDim(value, bdim);
  272. if (!is_no_batch_dim_case.has_value()) {
  273. is_no_batch_dim_case = (logical_rank == feature_rank);
  274. }
  275. auto value_ = ensure_has_bdim(value, bdim.has_value(), batch_size);
  276. if (!bdim.has_value()) {
  277. bdim = 0;
  278. }
  279. if (*is_no_batch_dim_case) {
  280. TORCH_INTERNAL_ASSERT(logical_rank == feature_rank);
  281. value_ = moveBatchDimToFront(value_, bdim);
  282. if (tensor_idx == contig_tensor_index) {
  283. value_ = value_.contiguous();
  284. }
  285. (*stack)[args_begin + tensor_pos[tensor_idx]] = std::move(value_);
  286. continue;
  287. }
  288. TORCH_INTERNAL_ASSERT(logical_rank == feature_rank + 1);
  289. value_ = reshape_dim_into(*bdim, 0, value_);
  290. if (tensor_idx == contig_tensor_index) {
  291. value_ = value_.contiguous();
  292. }
  293. (*stack)[args_begin + tensor_pos[tensor_idx]] = std::move(value_);
  294. }
  295. op.callBoxed(stack);
  296. for (const auto idx : c10::irange(args_begin, args_begin + num_returns)) {
  297. const auto& ret = (*stack)[idx];
  298. TORCH_INTERNAL_ASSERT(ret.isTensor(),
  299. "This boxed batching rule does not currently support ops that return non-tensor values");
  300. if (*is_no_batch_dim_case) {
  301. (*stack)[idx] = makeBatched(ret.toTensor(), 0, cur_level);
  302. } else {
  303. (*stack)[idx] = makeBatched(reshape_dim_outof(0, batch_size, ret.toTensor()), 0, cur_level);
  304. }
  305. }
  306. }
  307. // Useful for many NN operators.
  308. // The operator must satisfy the following:
  309. // - All arguments must accept an optional batch dim.
  310. // - All arguments must be the same rank
  311. #define ALL_TENSORS_HAVE_OPTIONAL_BDIM_BOXED(feature_rank, op) \
  312. m.impl(#op, torch::CppFunction::makeFromBoxedFunction<boxed_all_tensors_have_optional_bdim<feature_rank>>());
  313. #define ALL_TENSORS_HAVE_OPTIONAL_BDIM_BOXED_CONTIG1(feature_rank, op, contig_tensor_index) \
  314. m.impl(#op, \
  315. torch::CppFunction::makeFromBoxedFunction<\
  316. boxed_all_tensors_have_optional_bdim<\
  317. feature_rank, \
  318. contig_tensor_index>\
  319. >());
  320. template <typename A, A a, typename C>
  321. struct ExistingBdimBatchRuleHelper;
  322. template <typename F, F Func, typename A, typename... T>
  323. struct ExistingBdimBatchRuleHelper<F, Func, c10::guts::typelist::typelist<A, T...>> {
  324. static std::tuple<Tensor,optional<int64_t>> apply(
  325. const Tensor& self,
  326. optional<int64_t> self_bdim,
  327. T... extra_args) {
  328. auto self_ = reshape_dim_into(*self_bdim, 0, self);
  329. auto out = Func(self_, std::forward<T>(extra_args)...);
  330. return std::make_tuple(reshape_dim_outof_symint(0, self.sym_sizes()[*self_bdim], out), 0);
  331. }
  332. };
  333. // USAGE: EXISTING_BDIM_BATCH_RULE(at::cholesky_inverse)
  334. // INCORRECT USAGE: EXISTING_BDIM_BATCH_RULE(&at::cholesky_inverse)
  335. // It is important that this macro is not passed a function pointer!!
  336. #define EXISTING_BDIM_BATCH_RULE(fn) SINGLE_ARG(\
  337. ExistingBdimBatchRuleHelper<\
  338. decltype(&fn),\
  339. &fn,\
  340. c10::guts::function_traits<decltype(fn)>::parameter_types>::apply)
  341. #define EXISTING_BDIM(op) \
  342. VMAP_SUPPORT(op, EXISTING_BDIM_BATCH_RULE(ATEN_FN(op)));
  343. #define EXISTING_BDIM2(op, overload) \
  344. VMAP_SUPPORT2(op, overload, EXISTING_BDIM_BATCH_RULE(ATEN_FN2(op, overload)));
  345. #define INVOKE(object,ptrToMember) ((object).*(ptrToMember))
  346. template <typename F, F Method, typename... ExtraArgs>
  347. Tensor& unary_inplace_batch_rule(Tensor& self, optional<int64_t>, ExtraArgs... extra_args) {
  348. INVOKE(self, Method)(std::forward<ExtraArgs>(extra_args)...);
  349. return self;
  350. }
  351. inline int64_t get_bdim_size4(
  352. const Tensor& a_value, optional<int64_t> a_bdim,
  353. const Tensor& b_value, optional<int64_t> b_bdim,
  354. const Tensor& c_value, optional<int64_t> c_bdim,
  355. const Tensor& d_value, optional<int64_t> d_bdim) {
  356. if (a_bdim)
  357. return a_value.size(*a_bdim);
  358. if (b_bdim)
  359. return b_value.size(*b_bdim);
  360. if (c_bdim)
  361. return c_value.size(*c_bdim);
  362. if (d_bdim)
  363. return d_value.size(*d_bdim);
  364. TORCH_INTERNAL_ASSERT(false);
  365. }
  366. inline int64_t get_bdim_size3(
  367. const Tensor& a_value, optional<int64_t> a_bdim,
  368. const Tensor& b_value, optional<int64_t> b_bdim,
  369. const Tensor& c_value, optional<int64_t> c_bdim) {
  370. if (a_bdim)
  371. return a_value.size(*a_bdim);
  372. if (b_bdim)
  373. return b_value.size(*b_bdim);
  374. if (c_bdim)
  375. return c_value.size(*c_bdim);
  376. TORCH_INTERNAL_ASSERT(false);
  377. }
  378. inline int64_t get_bdim_size2(
  379. const Tensor& a_value, optional<int64_t> a_bdim,
  380. const Tensor& b_value, optional<int64_t> b_bdim) {
  381. if (a_bdim)
  382. return a_value.size(*a_bdim);
  383. if (b_bdim)
  384. return b_value.size(*b_bdim);
  385. TORCH_INTERNAL_ASSERT(false);
  386. }
  387. // [start, start + 1, ..., stop - 1]
  388. inline VmapDimVector range(int64_t start, int64_t stop) {
  389. TORCH_INTERNAL_ASSERT(stop >= start);
  390. VmapDimVector dims;
  391. dims.reserve(stop - start);
  392. for (int64_t i = start; i < stop; i++) {
  393. dims.emplace_back(i);
  394. }
  395. return dims;
  396. }
  397. std::tuple<Tensor, Tensor> _binary_pointwise_helper(
  398. const Tensor& tensor, optional<int64_t> tensor_batch_dim, const Tensor& other, optional<int64_t> other_batch_dim,
  399. bool do_type_promotion=true);
  400. }}