forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NamedTensor.cpp
417 lines (376 loc) · 15.6 KB
/
NamedTensor.cpp
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
#include <ATen/ATen.h>
#include <ATen/NativeFunctions.h>
#include <ATen/NamedTensorUtils.h>
#include <bitset>
namespace at { namespace native {
Tensor& rename_(Tensor& self, optional<DimnameList> names) {
return at::internal_set_names_inplace(self, names);
}
Tensor rename(const Tensor& self, optional<DimnameList> names) {
auto result = self.alias();
at::internal_set_names_inplace(result, names);
return result;
}
static void report_moving_unnamed_dim_error(
DimnameList names, DimnameList other, bool is_aligning_two_tensors) {
if (is_aligning_two_tensors) {
TORCH_CHECK(false,
"Aligning Tensor", names, " and Tensor", other,
" would change the absolute position from the right of an unnamed dimension. ",
"Please name unnamed dimensions to avoid ambiguity.");
} else {
TORCH_CHECK(false,
"Aligning Tensor", names, " to `names` ", other,
" would change the absolute position from the right of an unnamed dimension. ",
"Please name unnamed dimensions to avoid ambiguity.");
}
}
static void report_not_a_subsequence_error(
DimnameList names, DimnameList other, bool is_aligning_two_tensors) {
if (is_aligning_two_tensors) {
auto shorter = names.size() > other.size() ? other : names;
auto longer = names.size() > other.size() ? names : other;
TORCH_CHECK(false,
"Could not align Tensor", shorter, " and Tensor", longer,
" because ", shorter, " is not a subsequence of ", longer, ". ");
} else {
TORCH_CHECK(false,
"Could not align Tensor", names, " to `names` ", other,
" because ", names, " is not a subsequence of `names`.");
}
}
// Let tensor `t` have size `tensor_sizes` and `tensor_names`.
// This helper function computes the resulting size of `t` after aligning it
// to `aligned_names`. Enforces the alignment rules in Note [Alignment rules].
static std::vector<int64_t> aligned_size(
IntArrayRef tensor_sizes,
DimnameList tensor_names,
DimnameList aligned_names,
bool is_aligning_two_tensors) {
std::vector<int64_t> expanded_sizes(aligned_names.size(), 1);
ptrdiff_t dim = (ptrdiff_t)tensor_sizes.size() - 1;
ptrdiff_t idx = (ptrdiff_t)aligned_names.size() - 1;
for (; idx >= 0 && dim >= 0; --idx) {
if (tensor_names[dim] != aligned_names[idx]) {
continue;
}
// We've found a None name in `shorter` and `longer`. If their absolute positions
// from the right are not equal, then aligning the two names would require
// changing the absolute position from right of one of the None names,
// violating condition 2 of our [Alignment rules].
//
// For example:
// *, c, a, b
// *, a
// [*, a] is a subsequence of [*, c, a, b], but in order to align them,
// we'd have to move the * to create [*, c: 1, a, b: 1]
if (tensor_names[dim].isWildcard() &&
tensor_sizes.size() - dim != aligned_names.size() - idx) {
report_moving_unnamed_dim_error(
tensor_names, aligned_names, /*is_aligning_two_tensors=*/false);
}
expanded_sizes[idx] = tensor_sizes[dim];
--dim;
}
if (dim != -1) {
report_not_a_subsequence_error(
tensor_names, aligned_names, /*is_aligning_two_tensors=*/false);
}
return expanded_sizes;
}
Tensor refine_names(const Tensor& self, DimnameList names) {
const auto self_names = self.names();
TORCH_CHECK(self_names.size() == names.size(),
"refine_names: cannot coerce Tensor", self_names, " to Tensor", names,
" because they have a different number of dims (",
self_names.size(), " and ", names.size(), " respectively).");
check_names_valid_for(self, names);
for (size_t idx = 0; idx < self_names.size(); idx++) {
const auto& self_name = self_names[idx];
const auto& out_name = names[idx];
if (self_name == out_name || self_name.isWildcard()) {
continue;
}
if (out_name.isWildcard()) {
TORCH_CHECK(false,
"refine_names: cannot coerce Tensor", self_names, " to Tensor", names,
" because ", self_name, " is more specific than ", out_name, " at index ",
idx);
}
TORCH_CHECK(false,
"refine_names: cannot coerce Tensor", self_names, " to Tensor", names,
" because ", self_name, " is different from ", out_name, " at index ",
idx);
TORCH_INTERNAL_ASSERT(false); // done handling errors
}
auto result = self.alias();
internal_set_names_inplace(result, names);
return result;
}
// [Alignment rules]
// Aligns `tensor` to names with the following rules:
// 1) Check that tensor.names is a subsequence (not necessarily contiguous) of `names`.
// 2) Aligning tensor.names to names must not change the absolute position from the
// right of any unnamed dimension.
//
// is_aligning_two_tensors tunes the error message to better match the following cases:
// 1) tensor.align_to(names) (is_aligning_two_tensors=false)
// 2) torch.align_tensors([tensor, other]) (is_aligning_two_tensors=true)
static Tensor align(const Tensor& tensor, DimnameList names, bool is_aligning_two_tensors) {
std::vector<int64_t> expanded_sizes = aligned_size(
tensor.sizes(),
tensor.names(),
names,
is_aligning_two_tensors);
auto result = tensor.rename(nullopt).view(expanded_sizes);
at::internal_set_names_inplace(result, names);
return result;
}
static int64_t countUnset(std::bitset<kMaxNamedTensorDim> set, int64_t up_to_idx) {
int64_t result = 0;
for (auto i = 0; i < up_to_idx; ++i) {
if (!set.test(i)) result++;
}
return result;
}
// Handles `tensor.align_to(*order)` in the case where there is an ellipsis.
//
// Let tensor: Tensor[N, C, H, W]. Consider `tensor.align_to('W', ..., 'N')`
// We expand the `...` to "all unmentioned dimensions, in the order which they
// appear in the original tensor."
//
// `order` is passed in **without** the ellipsis name. This is because ellipsis
// is not a valid name in cpp right now. Future work should be done on making
// ellipsis a valid name.
//
// `ellipsis_idx` is where the ellipsis occurs in the Python call.
// In our example, `tensor.align_to('W', ..., 'N')`, order = ['W', 'N'] and
// ellipsis_idx = 1.
Tensor align_to(const Tensor& tensor, DimnameList order, int64_t ellipsis_idx) {
const auto tensor_names = tensor.names();
const auto tensor_sizes = tensor.sizes();
const auto tensor_strides = tensor.strides();
const auto tensor_dim = tensor.sizes().size();
constexpr int64_t not_found = -1;
// General strategy.
//
// Step 1: We compute the following 3 things:
// 1. How many names the ellipsis should expand to
// 2. Which names in `tensor.names` are not mentioned in `order`.
// 3. Where names in `order` occur in tensor, if at all.
//
// Step 2: Compute the new sizes/strides/names.
// First, determine the ndim of the output tensor (this is not obvious)
// by counting the number of names in `tensor` that are not in `order`.
// Next, fill in output sizes/strides/names by using `order` and knowledge
// of which dimensions in `tensor` are unmentioned in `order`.
std::bitset<kMaxNamedTensorDim> order_has_tensor_name;
// tensor_idx_for[i] = j means that the ith name in `order`
// appears in the jth element of tensor.
std::vector<int64_t> tensor_idx_for(order.size(), not_found);
for (auto order_idx = 0U; order_idx < order.size(); ++order_idx) {
const auto name = order[order_idx];
TORCH_CHECK(name.isBasic(),
"align_to: the desired order of dimensions cannot contain a None name, got ",
order);
auto it = std::find(tensor_names.begin(), tensor_names.end(), name);
if (it == tensor_names.end()) {
continue;
}
auto idx_in_tensor = std::distance(tensor_names.begin(), it);
tensor_idx_for[order_idx] = idx_in_tensor;
order_has_tensor_name.set(idx_in_tensor);
}
const auto num_ellipsis_names = countUnset(order_has_tensor_name, tensor_dim);
const auto out_dim = num_ellipsis_names + order.size();
// Step 2: Now that we know the size of the output tensor, we can use the
// metadata obtained from Step 1 to fill in the new sizes/strides/names
std::vector<int64_t> new_sizes(out_dim, 1);
std::vector<int64_t> new_strides(out_dim, 0);
std::vector<Dimname> new_names(out_dim, Dimname::wildcard());
auto setNewSizesStridesNamesFor = [&](int64_t out_dim, int64_t tensor_dim) {
new_sizes[out_dim] = tensor_sizes[tensor_dim];
new_strides[out_dim] = tensor_strides[tensor_dim];
new_names[out_dim] = tensor_names[tensor_dim];
};
// Fill in the non-ellipsis dimensions
for (auto order_idx = 0U; order_idx < order.size(); ++order_idx) {
auto out_idx = order_idx;
if (order_idx >= ellipsis_idx) {
out_idx = order_idx + num_ellipsis_names;
}
const auto tensor_idx = tensor_idx_for[order_idx];
if (tensor_idx == not_found) {
// We are adding a new size-one dimension
new_names[out_idx] = order[order_idx];
continue;
}
setNewSizesStridesNamesFor(out_idx, tensor_idx);
}
// Fill in the ellipsis dimensions
for (auto tensor_idx = 0U; tensor_idx < tensor_dim; ++tensor_idx) {
if (order_has_tensor_name.test(tensor_idx)) {
continue;
}
setNewSizesStridesNamesFor(ellipsis_idx, tensor_idx);
ellipsis_idx++;
}
check_names_valid_for(out_dim, new_names);
Tensor result;
{
NoNamesGuard guard;
result = tensor.as_strided(new_sizes, new_strides);
}
internal_set_names_inplace(result, std::move(new_names), /*validate_names=*/false);
return result;
}
Tensor align_to(const Tensor& tensor, DimnameList names) {
auto tensor_names = tensor.names();
auto tensor_sizes = tensor.sizes();
auto tensor_strides = tensor.strides();
std::vector<int64_t> new_sizes(names.size(), 1);
std::vector<int64_t> new_strides(names.size(), 0);
for (auto idx = 0U; idx < tensor_names.size(); ++idx) {
const auto& dim = tensor_names[idx];
TORCH_CHECK(dim.isBasic(),
"align_to: All input dims must be named. Found unnamed dim at index ",
idx, " of Tensor", tensor_names);
auto it = std::find(names.begin(), names.end(), dim);
TORCH_CHECK(it != names.end(),
"align_to: Cannot find dim ", dim, " from Tensor", names,
" in desired alignment ", names, ".");
int64_t new_idx = std::distance(names.begin(), it);
new_sizes[new_idx] = tensor_sizes[idx];
new_strides[new_idx] = tensor_strides[idx];
}
Tensor result;
{
NoNamesGuard guard;
result = tensor.as_strided(new_sizes, new_strides);
}
internal_set_names_inplace(result, names);
return result;
}
Tensor align_as(const Tensor& tensor, const Tensor& other) {
return native::align_to(tensor, other.names());
}
static std::vector<Tensor> align_tensors_to(TensorList tensors, DimnameList names) {
std::vector<Tensor> result;
result.reserve(tensors.size());
for (const auto& tensor : tensors) {
result.emplace_back(align(tensor, names, /*is_aligning_two_tensors=*/true));
}
return result;
}
std::vector<Tensor> align_tensors(TensorList tensors) {
auto longest_dim = std::max_element(
tensors.begin(), tensors.end(),
[](const Tensor& a, const Tensor& b) {
return a.dim() < b.dim();
});
return align_tensors_to(tensors, longest_dim->names());
}
static int64_t cumprod(IntArrayRef sizes) {
int64_t result = 1;
for (auto size : sizes) {
result *= size;
}
return result;
}
Tensor unflatten(const Tensor& self, int64_t dim, IntArrayRef sizes, DimnameList names) {
// unflatten is implemented only as a python method on tensor right now.
// The following asserts should be checked by the python method.
TORCH_INTERNAL_ASSERT(names.size() == sizes.size());
TORCH_INTERNAL_ASSERT(sizes.size() > 0);
TORCH_CHECK(
cumprod(sizes) == self.size(dim),
"unflatten: Provided names ", names, " and sizes ", sizes, " but sizes don't multiply "
"up to the size of dim ", dim, " (", self.names()[dim], ": ", self.size(dim),
") in Tensor", self.names());
int64_t dim_wrap = maybe_wrap_dim(dim, self.dim());
auto outnames = self.names().vec();
outnames.erase(outnames.begin() + dim_wrap);
outnames.insert(outnames.begin() + dim_wrap, names.begin(), names.end());
auto new_sizes = self.sizes().vec();
new_sizes.erase(new_sizes.begin() + dim_wrap);
new_sizes.insert(new_sizes.begin() + dim_wrap, sizes.begin(), sizes.end());
Tensor result;
{
NoNamesGuard guard;
result = self.view(new_sizes);
}
at::internal_set_names_inplace(result, outnames);
return result;
}
Tensor unflatten(const Tensor& self, Dimname dim, IntArrayRef sizes, DimnameList names) {
return native::unflatten(self, dimname_to_position(self, dim), sizes, names);
}
// Misc. Dimname overloads that don't have homes. Maybe we should move
// all of them here or autogenerate them because they look so similar.
Tensor gather(const Tensor& self, Dimname dim, const Tensor& index, bool sparse_grad) {
reportNYIDimnameOverload("gather");
}
Tensor& gather_out(Tensor& result, const Tensor& self, Dimname dim, const Tensor& index, bool sparse_grad) {
reportNYIDimnameOverload("gather");
}
Tensor index_add(const Tensor& self, Dimname dim, const Tensor& index, const Tensor& source) {
reportNYIDimnameOverload("index_add");
}
Tensor& index_add_(Tensor& self, Dimname dim, const Tensor& index, const Tensor& source) {
reportNYIDimnameOverload("index_add");
}
Tensor index_fill(const Tensor& self, Dimname dim, const Tensor& index, Scalar source) {
return at::index_fill(self, dimname_to_position(self, dim), index, source);
}
Tensor& index_fill_(Tensor& self, Dimname dim, const Tensor& index, Scalar source) {
return self.index_fill_(dimname_to_position(self, dim), index, source);
}
Tensor index_fill(const Tensor& self, Dimname dim, const Tensor& index, const Tensor& source) {
return at::index_fill(self, dimname_to_position(self, dim), index, source);
}
Tensor& index_fill_(Tensor& self, Dimname dim, const Tensor& index, const Tensor& source) {
return self.index_fill_(dimname_to_position(self, dim), index, source);
}
Tensor index_copy(const Tensor& self, Dimname dim, const Tensor& index, const Tensor& source) {
reportNYIDimnameOverload("index_copy");
}
Tensor& index_copy_(Tensor& self, Dimname dim, const Tensor& index, const Tensor& source) {
reportNYIDimnameOverload("index_copy");
}
Tensor& index_select_out(Tensor& out, const Tensor& self, Dimname dim, const Tensor& index) {
reportNYIDimnameOverload("index_select");
}
Tensor index_select(const Tensor& self, Dimname dim, const Tensor& index) {
reportNYIDimnameOverload("index_select");
}
Tensor scatter(const Tensor& self, Dimname dim, const Tensor& index, const Tensor& source) {
reportNYIDimnameOverload("scatter");
}
Tensor& scatter_(Tensor& self, Dimname dim, const Tensor& index, const Tensor& source) {
reportNYIDimnameOverload("scatter");
}
Tensor scatter(const Tensor& self, Dimname dim, const Tensor& index, Scalar source) {
reportNYIDimnameOverload("scatter");
}
Tensor& scatter_(Tensor& self, Dimname dim, const Tensor& index, Scalar source) {
reportNYIDimnameOverload("scatter");
}
Tensor scatter_add(const Tensor& self, Dimname dim, const Tensor& index, const Tensor& source) {
reportNYIDimnameOverload("scatter_add");
}
Tensor& scatter_add_(Tensor& self, Dimname dim, const Tensor& index, const Tensor& source) {
reportNYIDimnameOverload("scatter_add");
}
std::tuple<Tensor&, Tensor&> sort_out(Tensor& values, Tensor& indices, const Tensor& self, Dimname dim, bool keepdim) {
reportNYIDimnameOverload("sort");
}
std::tuple<Tensor, Tensor> sort(const Tensor& self, Dimname dim, bool keepdim) {
reportNYIDimnameOverload("sort");
}
Tensor& squeeze_(Tensor& self, Dimname dim) {
reportNYIDimnameOverload("squeeze");
}
Tensor squeeze(const Tensor& self, Dimname dim) {
return at::squeeze(self, dimname_to_position(self, dim));
}
}} // namespace at::native