/
compressed.py
310 lines (257 loc) · 9.94 KB
/
compressed.py
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
import numpy
try:
import scipy.sparse
scipy_available = True
except ImportError:
scipy_available = False
import cupy
from cupy import core
from cupy.creation import basic
from cupy import cusparse
from cupy.sparse import base
from cupy.sparse import data as sparse_data
from cupy.sparse import util
class _compressed_sparse_matrix(sparse_data._data_matrix):
_compress_getitem_kern = core.ElementwiseKernel(
'T d, S ind, int32 minor', 'raw T answer',
'if (ind == minor) atomicAdd(&answer[0], d);',
'compress_getitem', preamble=util._preamble_atomic_add)
def __init__(self, arg1, shape=None, dtype=None, copy=False):
if shape is not None and len(shape) != 2:
raise ValueError(
'Only two-dimensional sparse arrays are supported.')
if base.issparse(arg1):
x = arg1.asformat(self.format)
data = x.data
indices = x.indices
indptr = x.indptr
if arg1.format != self.format:
# When formats are differnent, all arrays are already copied
copy = False
if shape is None:
shape = arg1.shape
has_canonical_format = x.has_canonical_format
elif util.isshape(arg1):
m, n = arg1
m, n = int(m), int(n)
data = basic.zeros(0, dtype if dtype else 'd')
indices = basic.zeros(0, 'i')
indptr = basic.zeros(self._swap(m, n)[0] + 1, dtype='i')
# shape and copy argument is ignored
shape = (m, n)
copy = False
has_canonical_format = True
elif scipy_available and scipy.sparse.issparse(arg1):
# Convert scipy.sparse to cupy.sparse
x = arg1.asformat(self.format)
data = cupy.array(x.data)
indices = cupy.array(x.indices, dtype='i')
indptr = cupy.array(x.indptr, dtype='i')
copy = False
if shape is None:
shape = arg1.shape
has_canonical_format = x.has_canonical_format
elif isinstance(arg1, tuple) and len(arg1) == 3:
data, indices, indptr = arg1
if not (base.isdense(data) and data.ndim == 1 and
base.isdense(indices) and indices.ndim == 1 and
base.isdense(indptr) and indptr.ndim == 1):
raise ValueError(
'data, indices, and indptr should be 1-D')
if len(data) != len(indices):
raise ValueError('indices and data should have the same size')
has_canonical_format = False
elif base.isdense(arg1):
if arg1.ndim > 2:
raise TypeError('expected dimension <= 2 array or matrix')
elif arg1.ndim == 1:
arg1 = arg1[None]
elif arg1.ndim == 0:
arg1 = arg1[None, None]
data, indices, indptr = self._convert_dense(arg1)
copy = False
if shape is None:
shape = arg1.shape
has_canonical_format = True
else:
raise ValueError(
'Unsupported initializer format')
if dtype is None:
dtype = data.dtype
else:
dtype = numpy.dtype(dtype)
if dtype != 'f' and dtype != 'd':
raise ValueError('Only float32 and float64 are supported')
data = data.astype(dtype, copy=copy)
sparse_data._data_matrix.__init__(self, data)
self.indices = indices.astype('i', copy=copy)
self.indptr = indptr.astype('i', copy=copy)
if shape is None:
shape = self._swap(len(indptr) - 1, int(indices.max()) + 1)
major, minor = self._swap(*shape)
if len(indptr) != major + 1:
raise ValueError('index pointer size (%d) should be (%d)'
% (len(indptr), major + 1))
self._descr = cusparse.MatDescriptor.create()
self._shape = shape
self._has_canonical_format = has_canonical_format
def _with_data(self, data):
return self.__class__(
(data, self.indices.copy(), self.indptr.copy()), shape=self.shape)
def _convert_dense(self, x):
raise NotImplementedError
def _swap(self, x, y):
raise NotImplementedError
def _add_sparse(self, other, alpha, beta):
raise NotImplementedError
def _add(self, other, lhs_negative, rhs_negative):
if cupy.isscalar(other):
if other == 0:
if lhs_negative:
return -self
else:
return self.copy()
else:
raise NotImplementedError(
'adding a nonzero scalar to a sparse matrix is not '
'supported')
elif base.isspmatrix(other):
alpha = -1 if lhs_negative else 1
beta = -1 if rhs_negative else 1
return self._add_sparse(other, alpha, beta)
elif base.isdense(other):
if lhs_negative:
if rhs_negative:
return -self.todense() - other
else:
return other - self.todense()
else:
if rhs_negative:
return self.todense() - other
else:
return self.todense() + other
else:
return NotImplemented
def __add__(self, other):
return self._add(other, False, False)
def __radd__(self, other):
return self._add(other, False, False)
def __sub__(self, other):
return self._add(other, False, True)
def __rsub__(self, other):
return self._add(other, True, False)
def __getitem__(self, slices):
if isinstance(slices, tuple):
slices = list(slices)
elif isinstance(slices, list):
slices = list(slices)
if all([isinstance(s, int) for s in slices]):
slices = [slices]
else:
slices = [slices]
ellipsis = -1
n_ellipsis = 0
for i, s in enumerate(slices):
if s is None:
raise IndexError('newaxis is not supported')
elif s is Ellipsis:
ellipsis = i
n_ellipsis += 1
if n_ellipsis > 0:
ellipsis_size = self.ndim - (len(slices) - 1)
slices[ellipsis:ellipsis + 1] = [slice(None)] * ellipsis_size
if len(slices) == 2:
row, col = slices
elif len(slices) == 1:
row, col = slices[0], slice(None)
else:
raise IndexError('invalid number of indices')
major, minor = self._swap(row, col)
major_size, minor_size = self._swap(*self._shape)
if numpy.isscalar(major):
i = int(major)
if i < 0:
i += major_size
if not (0 <= i < major_size):
raise IndexError('index out of bounds')
if numpy.isscalar(minor):
j = int(minor)
if j < 0:
j += minor_size
if not (0 <= j < minor_size):
raise IndexError('index out of bounds')
return self._get_single(i, j)
elif minor == slice(None):
return self._get_major_slice(slice(i, i + 1))
elif isinstance(major, slice):
if minor == slice(None):
return self._get_major_slice(major)
raise ValueError('unsupported indexing')
def _get_single(self, major, minor):
start = self.indptr[major]
end = self.indptr[major + 1]
answer = cupy.zeros((), self.dtype)
self._compress_getitem_kern(
self.data[start:end], self.indices[start:end], minor, answer)
return answer[()]
def _get_major_slice(self, major):
major_size, minor_size = self._swap(*self._shape)
# major.indices cannot be used because scipy.sparse behaves differently
major_start = major.start
major_stop = major.stop
major_step = major.step
if major_start is None:
major_start = 0
if major_stop is None:
major_stop = major_size
if major_step is None:
major_step = 1
if major_start < 0:
major_start += major_size
if major_stop < 0:
major_stop += major_size
if major_step != 1:
raise ValueError('slicing with step != 1 not supported')
if not (0 <= major_start <= major_size and
0 <= major_stop <= major_size and
major_start <= major_stop):
raise IndexError('index out of bounds')
start = self.indptr[major_start]
stop = self.indptr[major_stop]
data = self.data[start:stop]
indptr = self.indptr[major_start:major_stop + 1] - start
indices = self.indices[start:stop]
shape = self._swap(len(indptr) - 1, minor_size)
return self.__class__(
(data, indices, indptr), shape=shape, dtype=self.dtype, copy=False)
@property
def has_canonical_format(self):
return self._has_canonical_format
def get_shape(self):
"""Returns the shape of the matrix.
Returns:
tuple: Shape of the matrix.
"""
return self._shape
def getnnz(self, axis=None):
"""Returns the number of stored values, including explicit zeros.
Args:
axis: Not supported yet.
Returns:
int: The number of stored values.
"""
if axis is None:
return self.data.size
else:
raise ValueError
# TODO(unno): Implement sorted_indices
def sum_duplicates(self):
if self._has_canonical_format:
return
if self.data.size == 0:
self._has_canonical_format = True
return
coo = self.tocoo()
coo.sum_duplicates()
self.__init__(coo.asformat(self.format))
self._has_canonical_format = True