sparse_matrix.cc 6.49 KB
Newer Older
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1
/**
2
3
 *  Copyright (c) 2022 by Contributors
 * @file sparse_matrix.cc
czkkkkkk's avatar
czkkkkkk committed
4
 * @brief DGL C++ sparse matrix implementations.
5
 */
czkkkkkk's avatar
czkkkkkk committed
6
7
8
9
10
// clang-format off
#include <sparse/dgl_headers.h>
// clang-format on

#include <c10/util/Logging.h>
11
12
#include <sparse/elementwise_op.h>
#include <sparse/sparse_matrix.h>
czkkkkkk's avatar
czkkkkkk committed
13
#include <torch/script.h>
14
15
16
17
18
19
20
21
22

namespace dgl {
namespace sparse {

SparseMatrix::SparseMatrix(
    const std::shared_ptr<COO>& coo, const std::shared_ptr<CSR>& csr,
    const std::shared_ptr<CSR>& csc, torch::Tensor value,
    const std::vector<int64_t>& shape)
    : coo_(coo), csr_(csr), csc_(csc), value_(value), shape_(shape) {
23
24
25
26
27
28
  TORCH_CHECK(
      coo != nullptr || csr != nullptr || csc != nullptr, "At least ",
      "one of CSR/COO/CSC is required to construct a SparseMatrix.")
  TORCH_CHECK(
      shape.size() == 2, "The shape of a sparse matrix should be ",
      "2-dimensional.");
29
30
31
32
  // NOTE: Currently all the tensors of a SparseMatrix should on the same
  // device. Do we allow the graph structure and values are on different
  // devices?
  if (coo != nullptr) {
33
34
35
36
    TORCH_CHECK(coo->indices.dim() == 2);
    TORCH_CHECK(coo->indices.size(0) == 2);
    TORCH_CHECK(coo->indices.size(1) == value.size(0));
    TORCH_CHECK(coo->indices.device() == value.device());
37
38
  }
  if (csr != nullptr) {
39
40
41
42
43
44
    TORCH_CHECK(csr->indptr.dim() == 1);
    TORCH_CHECK(csr->indices.dim() == 1);
    TORCH_CHECK(csr->indptr.size(0) == shape[0] + 1);
    TORCH_CHECK(csr->indices.size(0) == value.size(0));
    TORCH_CHECK(csr->indptr.device() == value.device());
    TORCH_CHECK(csr->indices.device() == value.device());
45
46
  }
  if (csc != nullptr) {
47
48
49
50
51
52
    TORCH_CHECK(csc->indptr.dim() == 1);
    TORCH_CHECK(csc->indices.dim() == 1);
    TORCH_CHECK(csc->indptr.size(0) == shape[1] + 1);
    TORCH_CHECK(csc->indices.size(0) == value.size(0));
    TORCH_CHECK(csc->indptr.device() == value.device());
    TORCH_CHECK(csc->indices.device() == value.device());
53
54
55
  }
}

56
c10::intrusive_ptr<SparseMatrix> SparseMatrix::FromCOOPointer(
57
58
59
60
61
    const std::shared_ptr<COO>& coo, torch::Tensor value,
    const std::vector<int64_t>& shape) {
  return c10::make_intrusive<SparseMatrix>(coo, nullptr, nullptr, value, shape);
}

62
c10::intrusive_ptr<SparseMatrix> SparseMatrix::FromCSRPointer(
63
64
65
66
67
    const std::shared_ptr<CSR>& csr, torch::Tensor value,
    const std::vector<int64_t>& shape) {
  return c10::make_intrusive<SparseMatrix>(nullptr, csr, nullptr, value, shape);
}

68
c10::intrusive_ptr<SparseMatrix> SparseMatrix::FromCSCPointer(
69
70
71
72
73
    const std::shared_ptr<CSR>& csc, torch::Tensor value,
    const std::vector<int64_t>& shape) {
  return c10::make_intrusive<SparseMatrix>(nullptr, nullptr, csc, value, shape);
}

74
c10::intrusive_ptr<SparseMatrix> SparseMatrix::FromCOO(
75
    torch::Tensor indices, torch::Tensor value,
76
    const std::vector<int64_t>& shape) {
77
78
  auto coo =
      std::make_shared<COO>(COO{shape[0], shape[1], indices, false, false});
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
  return SparseMatrix::FromCOOPointer(coo, value, shape);
}

c10::intrusive_ptr<SparseMatrix> SparseMatrix::FromCSR(
    torch::Tensor indptr, torch::Tensor indices, torch::Tensor value,
    const std::vector<int64_t>& shape) {
  auto csr = std::make_shared<CSR>(
      CSR{shape[0], shape[1], indptr, indices, torch::optional<torch::Tensor>(),
          false});
  return SparseMatrix::FromCSRPointer(csr, value, shape);
}

c10::intrusive_ptr<SparseMatrix> SparseMatrix::FromCSC(
    torch::Tensor indptr, torch::Tensor indices, torch::Tensor value,
    const std::vector<int64_t>& shape) {
  auto csc = std::make_shared<CSR>(
      CSR{shape[1], shape[0], indptr, indices, torch::optional<torch::Tensor>(),
          false});
  return SparseMatrix::FromCSCPointer(csc, value, shape);
}

c10::intrusive_ptr<SparseMatrix> SparseMatrix::ValLike(
    const c10::intrusive_ptr<SparseMatrix>& mat, torch::Tensor value) {
  TORCH_CHECK(
      mat->value().size(0) == value.size(0), "The first dimension of ",
      "the old values and the new values must be the same.");
  TORCH_CHECK(
      mat->value().device() == value.device(), "The device of the ",
      "old values and the new values must be the same.");
  auto shape = mat->shape();
  if (mat->HasCOO()) {
    return SparseMatrix::FromCOOPointer(mat->COOPtr(), value, shape);
  } else if (mat->HasCSR()) {
    return SparseMatrix::FromCSRPointer(mat->CSRPtr(), value, shape);
  } else {
    return SparseMatrix::FromCSCPointer(mat->CSCPtr(), value, shape);
  }
}

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
std::shared_ptr<COO> SparseMatrix::COOPtr() {
  if (coo_ == nullptr) {
    _CreateCOO();
  }
  return coo_;
}

std::shared_ptr<CSR> SparseMatrix::CSRPtr() {
  if (csr_ == nullptr) {
    _CreateCSR();
  }
  return csr_;
}

std::shared_ptr<CSR> SparseMatrix::CSCPtr() {
  if (csc_ == nullptr) {
    _CreateCSC();
  }
  return csc_;
}

139
std::tuple<torch::Tensor, torch::Tensor> SparseMatrix::COOTensors() {
140
  auto coo = COOPtr();
141
  return std::make_tuple(coo->indices.index({0}), coo->indices.index({1}));
142
143
}

144
145
146
147
148
torch::Tensor SparseMatrix::Indices() {
  auto coo = COOPtr();
  return coo->indices;
}

149
150
std::tuple<torch::Tensor, torch::Tensor, torch::optional<torch::Tensor>>
SparseMatrix::CSRTensors() {
151
152
  auto csr = CSRPtr();
  auto val = value();
153
  return std::make_tuple(csr->indptr, csr->indices, csr->value_indices);
154
155
}

156
157
std::tuple<torch::Tensor, torch::Tensor, torch::optional<torch::Tensor>>
SparseMatrix::CSCTensors() {
158
  auto csc = CSCPtr();
159
  return std::make_tuple(csc->indptr, csc->indices, csc->value_indices);
160
161
}

162
163
164
165
166
167
c10::intrusive_ptr<SparseMatrix> SparseMatrix::Transpose() const {
  auto shape = shape_;
  std::swap(shape[0], shape[1]);
  auto value = value_;
  if (HasCOO()) {
    auto coo = COOTranspose(coo_);
168
    return SparseMatrix::FromCOOPointer(coo, value, shape);
169
  } else if (HasCSR()) {
170
    return SparseMatrix::FromCSCPointer(csr_, value, shape);
171
  } else {
172
    return SparseMatrix::FromCSRPointer(csc_, value, shape);
173
174
175
  }
}

czkkkkkk's avatar
czkkkkkk committed
176
void SparseMatrix::_CreateCOO() {
177
  if (HasCOO()) return;
czkkkkkk's avatar
czkkkkkk committed
178
  if (HasCSR()) {
179
    coo_ = CSRToCOO(csr_);
czkkkkkk's avatar
czkkkkkk committed
180
  } else if (HasCSC()) {
181
    coo_ = CSCToCOO(csc_);
czkkkkkk's avatar
czkkkkkk committed
182
183
184
185
186
  } else {
    LOG(FATAL) << "SparseMatrix does not have any sparse format";
  }
}

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
void SparseMatrix::_CreateCSR() {
  if (HasCSR()) return;
  if (HasCOO()) {
    csr_ = COOToCSR(coo_);
  } else if (HasCSC()) {
    csr_ = CSCToCSR(csc_);
  } else {
    LOG(FATAL) << "SparseMatrix does not have any sparse format";
  }
}

void SparseMatrix::_CreateCSC() {
  if (HasCSC()) return;
  if (HasCOO()) {
    csc_ = COOToCSC(coo_);
  } else if (HasCSR()) {
    csc_ = CSRToCSC(csr_);
  } else {
    LOG(FATAL) << "SparseMatrix does not have any sparse format";
  }
}
208
209
210

}  // namespace sparse
}  // namespace dgl