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