forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparseCsrTensorImpl.cpp
99 lines (91 loc) · 2.89 KB
/
SparseCsrTensorImpl.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
#include <ATen/ATen.h>
#include <ATen/InitialTensorOptions.h>
#include <ATen/SparseCsrTensorImpl.h>
#include <ATen/SparseTensorImpl.h>
#include <ATen/SparseTensorUtils.h>
#include <ATen/core/LegacyTypeDispatch.h>
#include <ATen/native/Resize.h>
namespace at {
namespace {
DeviceType SparseCsrTensorSetToDeviceType(DispatchKeySet key_set) {
if (key_set.has(DispatchKey::SparseCsrCPU)) {
return kCPU;
} else if (key_set.has(DispatchKey::SparseCsrCUDA)) {
return kCUDA;
} else {
TORCH_CHECK(false,
"Cannot construct SparseCsrTensor with non-sparse tensor type ID ",
key_set);
}
}
} // namespace
SparseCsrTensorImpl::SparseCsrTensorImpl(
at::DispatchKeySet key_set,
const caffe2::TypeMeta data_type)
: SparseCsrTensorImpl(
key_set,
data_type,
at::empty(
{0},
at::initialTensorOptions()
.device(SparseCsrTensorSetToDeviceType(key_set))
.dtype(ScalarType::Int)) // crow_indices
,
at::empty(
{0},
at::initialTensorOptions()
.device(SparseCsrTensorSetToDeviceType(key_set))
.dtype(ScalarType::Int)) // col_indices
,
at::empty(
{0},
at::initialTensorOptions()
.device(SparseCsrTensorSetToDeviceType(key_set))
.dtype(data_type)) // values
) {}
SparseCsrTensorImpl::SparseCsrTensorImpl(
at::DispatchKeySet key_set,
const caffe2::TypeMeta data_type,
at::Tensor crow_indices,
at::Tensor col_indices,
at::Tensor values)
: TensorImpl(key_set, data_type, values.device()),
crow_indices_(std::move(crow_indices)),
col_indices_(std::move(col_indices)),
values_(std::move(values)) {}
void SparseCsrTensorImpl::resize_as_sparse_csr_tensor_(const Tensor& src) {
crow_indices_ = at::empty_like(
src.crow_indices(),
src.crow_indices().options(),
src.crow_indices().suggest_memory_format());
col_indices_ = at::empty_like(
src.col_indices(),
src.col_indices().options(),
src.col_indices().suggest_memory_format());
values_ = at::empty_like(
src.values(),
src.values().options(),
src.values().suggest_memory_format());
sizes_and_strides_.set_sizes(src.sizes());
refresh_numel();
}
void SparseCsrTensorImpl::set_member_tensors(
const Tensor& crow_indices,
const Tensor& col_indices,
const Tensor& values,
IntArrayRef size) {
// CSR Type Invariants
TORCH_CHECK(
values.scalar_type() == typeMetaToScalarType(dtype()),
"dtype of values (",
values.scalar_type(),
") must match dtype of sparse tensor (",
typeMetaToScalarType(dtype()),
")");
crow_indices_ = crow_indices;
col_indices_ = col_indices;
values_ = values;
sizes_and_strides_.set_sizes(size);
refresh_numel();
}
} // namespace at