forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparseCsrTensor.cpp
More file actions
472 lines (413 loc) · 19.1 KB
/
SparseCsrTensor.cpp
File metadata and controls
472 lines (413 loc) · 19.1 KB
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
// Basic functions on sparse tensors
#define TORCH_ASSERT_ONLY_METHOD_OPERATORS
#include <ATen/core/Tensor.h>
#include <ATen/Dispatch.h>
#include <ATen/InitialTensorOptions.h>
#include <ATen/Layout.h>
#include <ATen/Parallel.h>
#include <ATen/SparseCsrTensorImpl.h>
#include <ATen/SparseCsrTensorUtils.h>
#include <ATen/SparseTensorImpl.h>
#include <ATen/native/LinearAlgebraUtils.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/Functions.h>
#include <ATen/NativeFunctions.h>
#else
#include <ATen/ops/_nnz_native.h>
#include <ATen/ops/_sparse_csr_tensor_unsafe_native.h>
#include <ATen/ops/_validate_sparse_compressed_tensor_args_native.h>
#include <ATen/ops/_validate_sparse_csr_tensor_args_native.h>
#include <ATen/ops/clone_native.h>
#include <ATen/ops/col_indices_native.h>
#include <ATen/ops/copy_native.h>
#include <ATen/ops/crow_indices_native.h>
#include <ATen/ops/empty.h>
#include <ATen/ops/empty_like_native.h>
#include <ATen/ops/empty_native.h>
#include <ATen/ops/resize_as_sparse_native.h>
#include <ATen/ops/resize_native.h>
#include <ATen/ops/sparse_csr_tensor_native.h>
#include <ATen/ops/values_native.h>
#endif
namespace at {
namespace native {
using namespace at::sparse_csr;
namespace {
} // end anonymous namespace
void _validate_sparse_compressed_tensor_args_worker(const Tensor& compressed_indices, const Tensor& plain_indices, const Tensor& values, const IntArrayRef size, const Layout& layout) {
// Layout must be Sparse Compressed
AT_DISPATCH_ALL_SPARSE_COMPRESSED_LAYOUTS(layout, "validate_sparse_compressed_tensor_args", [&]{});
const std::string layout_name = layoutToString(layout, /*upper=*/ true);
const std::string compressed_indices_name = compressedIndicesName(layout);
const std::string plain_indices_name = plainIndicesName(layout);
// Layout Invariants
TORCH_CHECK(
plain_indices.layout() == kStrided && plain_indices.is_contiguous(),
"expected ", plain_indices_name, " to be a strided and contiguous tensor");
TORCH_CHECK(
compressed_indices.layout() == kStrided && compressed_indices.is_contiguous(),
"expected ", compressed_indices_name ," to be a strided and contiguous tensor");
TORCH_CHECK(
values.layout() == kStrided && values.is_contiguous(),
"expected values to be a strided and contiguous tensor");
// Shape and Strides invariants
TORCH_CHECK(
size.size() >= 2,
"size of a batched ", layout_name, " tensor must have length >= 2, but got: ",
size.size());
TORCH_CHECK(
compressed_indices.dim() >= 1,
compressed_indices_name, " must have dim >= 1 but got ", compressed_indices_name, ".dim() = ",
compressed_indices.dim());
TORCH_CHECK(
plain_indices.dim() >= 1,
plain_indices_name, " must have dim >= 1 but got ", plain_indices_name, ".dim() = ",
plain_indices.dim());
TORCH_CHECK(
values.dim() >= 1,
"values must have dim >= 1 but got values.dim() = ",
values.dim());
TORCH_CHECK(
compressed_indices.dim() == plain_indices.dim(),
"number of dimensions of ", compressed_indices_name, " and ", plain_indices_name, " must be the same.");
AT_DISPATCH_PLAIN_SPARSE_COMPRESSED_LAYOUTS(
layout, "validate_sparse_compressed_tensor_args",
[&] {
TORCH_CHECK(
compressed_indices.dim() == values.dim(),
"number of dimensions of indices and values must be the same.");
},
[&] {
TORCH_CHECK(
compressed_indices.dim() + 2 == values.dim(),
"number of dimensions of indices must be two less than the number of dimensions of the values.");
});
TORCH_CHECK(
static_cast<size_t>(compressed_indices.dim()) == size.size() - 1,
"number of dimensions of indices must be one less than the number of dimensions of the provided size.");
int block_ndim = AT_DISPATCH_PLAIN_SPARSE_COMPRESSED_LAYOUTS(layout, "validate_sparse_compressed_tensor_args", [&]{ return 0; }, [&]{ return 2; });
IntArrayRef block_size = values.sizes().slice(values.dim() - block_ndim, block_ndim);
int64_t numel_per_block = AT_DISPATCH_PLAIN_SPARSE_COMPRESSED_LAYOUTS(layout, "validate_sparse_compressed_tensor_args",
[&]() -> int64_t { return 1; }, [&]() -> int64_t { return block_size[0] * block_size[1]; });
int compressed_dim = compressedDimension(layout, size);
int plain_dim = plainDimension(layout, size);
// All batch sizes must be the same
auto batch_size = size.slice(0, size.size() - 2);
auto compressed_indices_batch_size = compressed_indices.sizes().slice(0, compressed_indices.dim() - 1);
auto plain_indices_batch_size = plain_indices.sizes().slice(0, plain_indices.dim() - 1);
auto values_batch_size = values.sizes().slice(0, values.dim() - 1 - block_ndim);
TORCH_CHECK(
batch_size == compressed_indices_batch_size &&
batch_size == plain_indices_batch_size &&
batch_size == values_batch_size,
"all batch dimensions of the provided size (", batch_size, "), indices (",
compressed_indices_batch_size,", ", plain_indices_batch_size, "), and values (",
values_batch_size,") must be the same.");
// Note, this check also enforces `compressed_indices.size(-1) >= 1`
TORCH_CHECK(
compressed_indices.size(-1) == (size[compressed_dim] + 1),
compressed_indices_name, ".size(-1) must be equal to size[-", (size.size() - compressed_dim), "] + 1 (that is ",
size[compressed_dim] + 1, "), but got: ", compressed_indices.size(-1));
AT_DISPATCH_PLAIN_SPARSE_COMPRESSED_LAYOUTS(layout, "validate_sparse_compressed_tensor_args",
[&] {
TORCH_CHECK(
plain_indices.numel() == values.numel(),
plain_indices_name, " and values must have the same number of elements, but got ", plain_indices_name, ".numel(): ",
plain_indices.numel(), ", values.numel(): ", values.numel());
},
[&] {
TORCH_CHECK(
plain_indices.numel() * numel_per_block == values.numel(),
"number of ", plain_indices_name, " elements must be the same as the number of blocks in values, but got ",
plain_indices_name, ".numel() * numel_per_block: ", plain_indices.numel() * numel_per_block,
", values.numel(): ", values.numel(),", numel_per_block: ", numel_per_block);
});
// Indices invariants
AT_DISPATCH_INDEX_TYPES(compressed_indices.scalar_type(), "validate_sparse_compressed_tensor_args",
[&] {
Tensor compressed_indices_cpu = compressed_indices.to(kCPU);
auto compressed_indices_data_ptr = compressed_indices_cpu.data_ptr<index_t>();
auto batch_stride = compressed_indices_cpu.dim() >= 2 ? compressed_indices_cpu.stride(-2) : 0;
for (const auto batch_id : c10::irange(batchCount(compressed_indices_cpu))) {
TORCH_CHECK(
compressed_indices_data_ptr[batch_id*batch_stride] == 0,
"(Batch element ", batch_id, ") ",
": 0th value of ", compressed_indices_name, " must be 0, but it is ", compressed_indices_data_ptr[batch_id*batch_stride]);
TORCH_CHECK(
compressed_indices_data_ptr[batch_id*batch_stride + compressed_indices.size(-1) - 1] == plain_indices.size(-1),
"(Batch element ", batch_id, ") ",
"last value of ", compressed_indices_name, " should be equal to the length of ", plain_indices_name, ".");
for (int i = 1; i <= size[size.size() - 2]; i++) {
TORCH_CHECK(
compressed_indices_data_ptr[batch_id*batch_stride + i - 1] <= compressed_indices_data_ptr[batch_id*batch_stride + i],
"(Batch element ", batch_id, ") ",
"at position i = ", i, ", the condition ", compressed_indices_name, "[i - 1] <= ", compressed_indices_name, "[i] fails");
}
}
if (plain_indices.numel() > 0) {
TORCH_CHECK(0 <= plain_indices.min().item<index_t>(), plain_indices_name, ".min() should be greater or equal to zero");
TORCH_CHECK(size[plain_dim] > plain_indices.max().item<index_t>(), "size[-", (size.size() - plain_dim),"] should be greater than ", plain_indices_name, ".max()");
}
});
// Type Invariants
auto compressed_indices_type = compressed_indices.scalar_type();
auto plain_indices_type = plain_indices.scalar_type();
TORCH_CHECK(
compressed_indices_type == plain_indices_type,
"both ", compressed_indices_name, " and ", plain_indices_name, " should have the same type.");
TORCH_CHECK(
compressed_indices_type == kInt || compressed_indices_type == kLong,
compressed_indices_name, " and ", plain_indices_name, " must be an int32 or int64 type, but got: ",
compressed_indices_type);
// Device Invariants
TORCH_CHECK(
plain_indices.get_device() == compressed_indices.get_device(),
compressed_indices_name, " and ", plain_indices_name, " devices (",
compressed_indices.get_device(),
", ",
plain_indices.get_device(),
") must match");
TORCH_CHECK(
compressed_indices.get_device() == values.get_device(),
"device of ", compressed_indices_name, " (",
compressed_indices.get_device(),
") must match device of values (",
values.get_device(),
")");
TORCH_CHECK(
values.device().type() == kCPU || values.device().type() == kCUDA,
"device type of values (",
values.device().type(),
") must be CPU or CUDA");
}
void _validate_sparse_compressed_tensor_args(const Tensor& crow_indices, const Tensor& col_indices, const Tensor& values, IntArrayRef size, Layout layout) {
_validate_sparse_compressed_tensor_args_worker(crow_indices, col_indices, values, size, layout);
}
void _validate_sparse_csr_tensor_args(const Tensor& crow_indices, const Tensor& col_indices, const Tensor& values, IntArrayRef size) {
_validate_sparse_compressed_tensor_args_worker(crow_indices, col_indices, values, size, kSparseCsr);
}
// Construction of CSR tensors.
SparseCsrTensor new_csr_tensor(const TensorOptions& options) {
// TODO: remove this comment after enabling autograd support for CSR tensor
// constructor.
// TORCH_INTERNAL_ASSERT(impl::variable_excluded_from_dispatch());
Layout layout = options.layout();
TORCH_INTERNAL_ASSERT(layout == kSparseCsr);
DispatchKey dispatch_key;
TORCH_CHECK_NOT_IMPLEMENTED(
options.device().type() == kCPU || options.device().type() == kCUDA,
"Could not run '", "sparse_csr_tensor", "' from the '", options.device(), "' device.)");
if (options.device().is_cuda()) {
dispatch_key = DispatchKey::SparseCsrCUDA;
} else {
dispatch_key = DispatchKey::SparseCsrCPU;
}
return detail::make_tensor<SparseCsrTensorImpl>(
DispatchKeySet(dispatch_key), layout, options.dtype());
}
Tensor _sparse_csr_tensor_unsafe(const Tensor& crow_indices, const Tensor& col_indices,
const Tensor& values,
IntArrayRef size,
c10::optional<ScalarType> dtype,
c10::optional<Layout> layout,
c10::optional<Device> device,
c10::optional<bool> pin_memory) {
TensorOptions options = TensorOptions().dtype(dtype).layout(layout).device(device).pinned_memory(pin_memory);
SparseCsrTensor self = new_csr_tensor(options);
get_sparse_csr_impl(self)->set_member_tensors(crow_indices, col_indices, values, size);
return self;
}
// TODO: This constructor should probably use an ATen abstract method in order
// to make autograd dispatch available for the CSR constructor. See the relevant
// note in native_functions.yaml.
Tensor sparse_csr_tensor(
const Tensor& crow_indices,
const Tensor& col_indices,
const Tensor& values,
IntArrayRef size,
c10::optional<ScalarType> dtype,
c10::optional<Layout> layout,
c10::optional<Device> device,
c10::optional<bool> pin_memory) {
// See [Note: hacky wrapper removal for TensorOptions]
TensorOptions options = TensorOptions().dtype(dtype).layout(layout).device(device).pinned_memory(pin_memory);
at::native::_validate_sparse_csr_tensor_args(crow_indices, col_indices, values, size);
return at::native::_sparse_csr_tensor_unsafe(
crow_indices,
col_indices,
values,
size,
optTypeMetaToScalarType(options.dtype_opt()),
options.layout_opt(),
options.device_opt(),
options.pinned_memory_opt());
}
Tensor sparse_csr_tensor(
const Tensor& crow_indices,
const Tensor& col_indices,
const Tensor& values,
c10::optional<ScalarType> dtype,
c10::optional<Layout> layout,
c10::optional<Device> device,
c10::optional<bool> pin_memory) {
// See [Note: hacky wrapper removal for TensorOptions]
TensorOptions options = TensorOptions().dtype(dtype).layout(layout).device(device).pinned_memory(pin_memory);
// std::array<int64_t, 2> size = {0, 0};
auto size = DimVector(IntArrayRef(col_indices.sizes().data(), col_indices.dim() - 1));
size.push_back(crow_indices.size(-1) - 1);
size.push_back(col_indices.max().item<int64_t>() + 1);
at::native::_validate_sparse_csr_tensor_args(crow_indices, col_indices, values, size);
return at::native::_sparse_csr_tensor_unsafe(
crow_indices,
col_indices,
values,
size,
optTypeMetaToScalarType(options.dtype_opt()),
options.layout_opt(),
options.device_opt(),
options.pinned_memory_opt());
}
Tensor empty_sparse_csr(
IntArrayRef size,
c10::optional<ScalarType> dtype,
c10::optional<Layout> layout,
c10::optional<Device> device,
c10::optional<bool> pin_memory,
c10::optional<MemoryFormat> optional_memory_format) {
check_size_nonnegative(size);
TORCH_CHECK(size.size() >= 2, "torch.empty: Only batched sparse CSR matrices are supported, but got size ", size);
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(layout == Layout::SparseCsr);
auto rows = size[size.size() - 2];
int64_t nnz = 0;
auto crow_indices_size = DimVector(size.slice(0, size.size() - 2));
crow_indices_size.push_back(rows + 1);
auto col_indices_values_size = DimVector(size.slice(0, size.size() - 2));
col_indices_values_size.push_back(nnz);
TensorOptions options = TensorOptions().dtype(ScalarType::Long).layout(Layout::Strided).device(device).pinned_memory(pin_memory);
auto crow_indices = at::empty(crow_indices_size, options);
auto col_indices = at::empty(col_indices_values_size, options);
auto values = at::empty(col_indices_values_size, options.dtype(dtype));
return at::native::_sparse_csr_tensor_unsafe(
crow_indices,
col_indices,
values,
size,
dtype,
layout,
device,
pin_memory);
}
const Tensor& resize_sparse_csr_(
const Tensor& self,
IntArrayRef size,
c10::optional<MemoryFormat> optional_memory_format) {
check_size_nonnegative(size);
TORCH_CHECK(size.size() >= 2, "torch.resize_: Only batched sparse CSR matrices are supported, but got size ", size);
TORCH_CHECK(
self.size(-1) <= size[size.size() - 1],
"torch.resize_: Resizing columns of sparse CSR tensors to a smaller value is not supported. ",
"The original number of columns is ",
self.size(-1),
" while the requested new number of columns is ", size[size.size() - 1], ".");
get_sparse_csr_impl(self)->resize_(self._nnz(), size);
return self;
}
Tensor& copy_sparse_csr_(Tensor& self, const Tensor& src, bool non_blocking) {
TORCH_CHECK(
self.sizes() == src.sizes(),
"copy_sparse_csr_: only same size tensors are supported.");
TORCH_CHECK(
self.is_sparse_csr() && src.is_sparse_csr(),
"copy_sparse_csr_: copy between different layouts is not supported. Found self type = ",
self.toString(),
" and src type = ",
src.toString());
TORCH_CHECK(
self._nnz() == src._nnz(),
"copy_sparse_csr_: only tensors with the same number of specified elements are supported.");
self.crow_indices().copy_(src.crow_indices(), non_blocking);
self.col_indices().copy_(src.col_indices(), non_blocking);
self.values().copy_(src.values(), non_blocking);
return self;
}
// Access members of CSR tensors.
int64_t _nnz_sparse_csr(const SparseCsrTensor& self) {
return get_sparse_csr_impl(self)->nnz();
}
Tensor values_sparse_csr(const Tensor& self) {
return get_sparse_csr_impl(self)->values().alias();
}
Tensor crow_indices_sparse_csr(const Tensor& self) {
return get_sparse_csr_impl(self)->crow_indices().alias();
}
Tensor col_indices_sparse_csr(const Tensor& self) {
return get_sparse_csr_impl(self)->col_indices().alias();
}
bool _is_same_size_as_sparse_csr(
const SparseCsrTensor& self,
const SparseCsrTensor& src) {
return self.sizes().equals(src.sizes());
}
const SparseCsrTensor& resize_as_sparse_csr_(
const SparseCsrTensor& self,
const SparseCsrTensor& src) {
TORCH_CHECK(
src.is_sparse_csr() && self.is_sparse_csr(),
"resize_as_sparse_csr_: layout for self and src must be sparse_csr but got ",
self.layout(),
" for self, and ",
src.layout(),
" for src");
if (!_is_same_size_as_sparse_csr(self, src)) {
get_sparse_csr_impl(self)->resize_as_sparse_csr_tensor_(src);
}
return self;
}
SparseCsrTensor clone_sparse_csr(
const SparseCsrTensor& self,
c10::optional<c10::MemoryFormat> optional_memory_format) {
TORCH_CHECK(
!optional_memory_format.has_value(),
"unsupported memory format option ",
optional_memory_format.value());
TensorOptions options = self.options();
return at::native::_sparse_csr_tensor_unsafe(
self.crow_indices().clone(),
self.col_indices().clone(),
self.values().clone(),
self.sizes(),
optTypeMetaToScalarType(options.dtype_opt()),
options.layout_opt(),
options.device_opt(),
options.pinned_memory_opt());
}
Tensor empty_like_sparse_csr(
const Tensor& self,
c10::optional<ScalarType> dtype,
c10::optional<Layout> layout,
c10::optional<Device> device,
c10::optional<bool> pin_memory,
c10::optional<c10::MemoryFormat> optional_memory_format) {
TensorOptions options_ = TensorOptions().dtype(dtype).layout(layout).device(device).pinned_memory(pin_memory);
TensorOptions options =
self.options()
.merge_in(options_)
.merge_memory_format(optional_memory_format);
if (options.layout() == kSparseCsr) {
auto result = at::native::_sparse_csr_tensor_unsafe(
self.crow_indices().clone(),
self.col_indices().clone(),
at::empty(self.values().sizes(), options.layout(kStrided)),
self.sizes(),
dtype,
self.layout(),
device);
return result;
} else if (options.layout() == kStrided) {
return at::native::empty_like(self, dtype, layout, device, pin_memory, optional_memory_format);
} else {
TORCH_CHECK(false, "Layout ", options.layout(), " is not supported");
}
}
} // namespace native
} // namespace at