forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparseTensorMath.cpp
1270 lines (1079 loc) · 46 KB
/
SparseTensorMath.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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ATen/native/sparse/SparseTensorMath.h>
#include <ATen/ATen.h>
#include <ATen/Parallel.h>
#include <ATen/SparseTensorImpl.h>
#include <ATen/ExpandUtils.h>
#include <ATen/NativeFunctions.h>
#include <ATen/InitialTensorOptions.h>
#include <ATen/SparseTensorUtils.h>
#include <ATen/WrapDimUtilsMulti.h>
#include <ATen/native/BinaryOps.h>
#include <TH/THBlasUtils.h>
namespace at { namespace native {
using namespace at::sparse;
// --------------------------------------------------------------------
// Utility functions
// --------------------------------------------------------------------
namespace {
LongTensor _to_csr(const int64_t* indices, int64_t dim, int64_t nnz) {
LongTensor csr = native::zeros({dim + 1}, kLong);
// TODO: eliminate this conditional when zero-size dims supported correctly
if (nnz > 0) {
auto csr_accessor = csr.accessor<int64_t, 1>();
// Convert the sparse matrix to CSR format
at::parallel_for(0, nnz, 10000, [&](int64_t start, int64_t end) {
int64_t h, hp0, hp1;
for (auto i = start; i < end; i++) {
hp0 = indices[i];
hp1 = (i+1 == nnz) ? dim : indices[i+1];
if (hp0 != hp1) for (h = hp0; h < hp1; h++) {
csr_accessor[h+1] = i+1;
}
}
});
}
return csr;
}
}
// --------------------------------------------------------------------
// zero_(SparseTensor)
// --------------------------------------------------------------------
// hummu hummu
SparseTensor& zero_sparse_(SparseTensor& self) {
AT_ASSERT(self.is_sparse());
at::zeros_out(self, get_sparse_impl(self)->sizes());
return self._coalesced_(true);
}
// NB: Don't need zeros, zeros_like, already implemented in TensorFactories
// --------------------------------------------------------------------
// mul(SparseTensor, Scalar)
// --------------------------------------------------------------------
static Tensor wrapped_scalar_tensor(Scalar s) {
auto tensor = scalar_to_tensor(s);
tensor.unsafeGetTensorImpl()->set_wrapped_number(true);
return tensor;
}
SparseTensor& mul_out_sparse_zerodim(SparseTensor& r, const SparseTensor& t, const Tensor& value) {
AT_ASSERT(r.is_sparse());
AT_ASSERT(t.is_sparse());
AT_ASSERT(value.dim() == 0);
if (is_same_tensor(r, t)) {
r._values().mul_(value);
} else {
r.resize_as_(t);
auto indices = r._indices();
indices.resize_as_(t._indices());
indices.copy_(t._indices());
Tensor r_values = r._values(); // Sigh... needed because mul_out takes Tensor&
at::mul_out(r_values, t._values(), value);
get_sparse_impl(r)->set_nnz_and_narrow(t._nnz());
r._coalesced_(t.is_coalesced());
}
return r;
}
SparseTensor& mul_out_sparse_scalar(SparseTensor& r, const SparseTensor& t, Scalar value) {
return mul_out_sparse_zerodim(r, t, wrapped_scalar_tensor(value));
}
// --------------------------------------------------------------------
// log1p(SparseTensor)
// --------------------------------------------------------------------
// TODO: add in-place variant
SparseTensor& log1p_out_sparse(SparseTensor& r, const SparseTensor& t) {
AT_ASSERT(r.is_sparse());
AT_ASSERT(t.is_sparse());
if (is_same_tensor(r, t)) {
// don't have in-place log1p for uncoalesced input because coalesce() is not in-place
TORCH_CHECK(
r.is_coalesced(), "log1p: in-place on uncoalesced tensors is not supported yet!");
}
else {
copy_sparse_to_sparse_(r, t.coalesce());
}
r._values().log1p_();
return r;
}
SparseTensor& log1p_sparse_(SparseTensor& t) {
TORCH_CHECK(t.is_coalesced(), "log1p: in-place on uncoalesced tensors is not supported yet!");
return log1p_out_sparse(t, t);
}
// --------------------------------------------------------------------
// pow(SparseTensor, Scalar)
// --------------------------------------------------------------------
// TODO: add in-place variant
SparseTensor& pow_out_sparse_scalar(SparseTensor& r, const SparseTensor& t_, Scalar value) {
AT_ASSERT(r.is_sparse());
AT_ASSERT(t_.is_sparse());
TORCH_CHECK(value.toDouble() != 0, "pow: cannot raise to zeroth power on sparse tensor; it would make the result tensor dense");
// This coalesce is why we can't easily provide an inplace variant
SparseTensor t = t_.coalesce();
r.resize_as_(t);
auto indices = r._indices();
indices.resize_as_(t._indices());
indices.copy_(t._indices());
Tensor r_values = r._values(); // Sigh... needed because pow_out takes Tensor&
at::pow_out(r_values, t._values(), value);
get_sparse_impl(r)->set_nnz_and_narrow(t._nnz());
return r._coalesced_(t.is_coalesced());
}
SparseTensor pow_sparse_scalar(const SparseTensor& t, Scalar value) {
SparseTensor r = at::empty({0}, t.options());
pow_out_sparse_scalar(r, t, value);
return r;
}
// --------------------------------------------------------------------
// div(SparseTensor, Scalar)
// --------------------------------------------------------------------
SparseTensor& div_out_sparse_zerodim(SparseTensor& r, const SparseTensor& t, const Tensor& value);
Tensor div_sparse(const Tensor& self, const Tensor& value) {
auto commonDtype = at::result_type(self, value);
Tensor result = at::empty({0}, self.options().dtype(commonDtype));
return div_out_sparse_zerodim(result, self, value);
}
Tensor& div_sparse_(Tensor& self, const Tensor& value) {
return div_out_sparse_zerodim(self, self, value);
}
static SparseTensor& coalesce_(SparseTensor& tensor) {
SparseTensor coalesced = tensor.coalesce();
tensor._values().resize_as_(coalesced._values());
tensor._indices().resize_as_(coalesced._indices());
tensor._values().copy_(coalesced._values());
tensor._indices().copy_(coalesced._indices());
tensor._coalesced_(true);
return tensor;
}
SparseTensor& div_out_sparse_zerodim(SparseTensor& r, const SparseTensor& t, const Tensor& value) {
TORCH_CHECK(value.dim() == 0, "sparse division only supports division by a scalar (got shape ",
value.sizes(), " for argument 'other')");
TORCH_CHECK(!value.is_sparse(), "A Sparse Tensor can only be divided by a scalar or zero-dim dense tensor");
AT_ASSERT(r.is_sparse());
AT_ASSERT(t.is_sparse());
if (is_same_tensor(r, t)) {
// Can't divide an uncoalesced integral tensor accurately. e.g. for a sparse int tensor with value 6
// represented as values=[3, 3], integer division by 2 would give values=[1, 1] => 2 instead
// of 6 / 2 => 3
if (!r.is_coalesced() && isIntegralType(r.scalar_type(), /*includeBool=*/true)) {
coalesce_(r);
}
r._values().div_(value);
} else {
Tensor t_tmp = t;
if (!t.is_coalesced() && isIntegralType(r.scalar_type(), /*includeBool=*/true)) {
t_tmp = t.coalesce();
}
r.resize_as_(t_tmp);
auto indices = r._indices();
indices.resize_as_(t_tmp._indices());
indices.copy_(t_tmp._indices());
Tensor r_values = r._values(); // Sigh... needed because div_out takes Tensor&
at::div_out(r_values, t_tmp._values(), value);
get_sparse_impl(r)->set_nnz_and_narrow(t_tmp._nnz());
r._coalesced_(t_tmp.is_coalesced());
}
return r;
}
SparseTensor& div_out_sparse_scalar(SparseTensor& r, const SparseTensor& t, Scalar value) {
return div_out_sparse_zerodim(r, t, wrapped_scalar_tensor(value));
}
// --------------------------------------------------------------------
// norm(SparseTensor, Scalar)
// --------------------------------------------------------------------
// Only supports floating point, FYI
Tensor norm_sparse(const SparseTensor& self, Scalar value) {
AT_ASSERT(self.is_sparse());
return self.coalesce()._values().norm(value);
}
// --------------------------------------------------------------------
// add(SparseTensor, SparseTensor, Scalar) [broadcasts]
// --------------------------------------------------------------------
Tensor add_sparse(const Tensor& self, const Tensor& other, Scalar alpha) {
// TODO: Why?! Can't we just flip the order here...
TORCH_CHECK(!(self.is_sparse() && !other.is_sparse()),
"add(sparse, dense) is not supported. Use add(dense, sparse) instead.");
auto commonDtype = at::result_type(self, other);
alpha_check(commonDtype, alpha);
Tensor result = at::empty({0}, self.options().dtype(commonDtype));
return at::add_out(result, self, other, alpha); // redispatch!
}
Tensor& add_sparse_(Tensor& self, const Tensor& other, Scalar alpha) {
return at::add_out(self, self, other, alpha); // redispatch!
}
// There's actually nothing sparse specific about these implementations
Tensor sub_sparse(const Tensor& self, const Tensor& other, Scalar alpha) {
sub_check(self, other);
return native::add_sparse(self, other, -alpha);
}
Tensor& sub_sparse_(Tensor& self, const Tensor& other, Scalar alpha) {
sub_check(self, other);
return native::add_sparse_(self, other, -alpha);
}
Tensor& sub_out_sparse(Tensor& r, const Tensor& self, const Tensor& other, Scalar alpha) {
sub_check(self, other);
return at::add_out(r, self, other, -alpha); // redispatch!
}
SparseTensor& add_out_sparse_contiguous(SparseTensor& r, const SparseTensor& t, const SparseTensor& src, Scalar value, ScalarType commonDtype) {
// saving those because they can be overwritten when doing in-place operations
int64_t t_nnz = t._nnz(), s_nnz = src._nnz(), max_nnz = t_nnz + s_nnz;
bool coalesced = t.is_coalesced() && src.is_coalesced();
int64_t sparse_dim = src.sparse_dim();
LongTensor r_indices = at::empty({src.sparse_dim(), max_nnz}, t._indices().options());
Tensor t_values = t._values().to(commonDtype);
Tensor s_values = src._values().to(commonDtype);
Tensor r_values = new_values_with_size_of(s_values, max_nnz).zero_();
int64_t blockSize = r_values.stride(0);
int64_t cmp, d;
int64_t r_i = 0, t_i = 0, s_i = 0;
auto t_indices = t._indices();
auto src_indices = src._indices();
// NB: relies on nnz tests above
auto t_indices_accessor = t_indices.accessor<int64_t, 2>();
auto r_indices_accessor = r_indices.accessor<int64_t, 2>();
auto src_indices_accessor = src_indices.accessor<int64_t, 2>();
AT_DISPATCH_ALL_TYPES(
commonDtype, "cadd_sparse", [&] {
scalar_t* t_values_ptr = t_values.data_ptr<scalar_t>();
scalar_t* s_values_ptr = s_values.data_ptr<scalar_t>();
scalar_t* r_values_ptr = r_values.data_ptr<scalar_t>();
scalar_t cast_value = value.to<scalar_t>();
while (t_i < t_nnz || s_i < s_nnz) {
if (t_i >= t_nnz) {
cmp = -1;
} else if (s_i >= s_nnz) {
cmp = 1;
} else {
cmp = 0;
for (d = 0; d < sparse_dim; d++) {
if (t_indices_accessor[d][t_i] < src_indices_accessor[d][s_i]) {
cmp = 1;
break;
}
if (t_indices_accessor[d][t_i] > src_indices_accessor[d][s_i]) {
cmp = -1;
break;
}
}
}
if (cmp >= 0) {
for (d = 0; d < sparse_dim; d++) {
r_indices_accessor[d][r_i] = t_indices_accessor[d][t_i];
}
if (t_values.numel() > 0) { // We add all elements from t_values to r_values only if t_values is not an empty tensor
THBlas_axpy<scalar_t>(blockSize, 1,
t_values_ptr + t_i * blockSize, 1,
r_values_ptr + r_i * blockSize, 1);
}
t_i++;
}
if (cmp <= 0) {
for (d = 0; d < sparse_dim; d++) {
r_indices_accessor[d][r_i] = src_indices_accessor[d][s_i];
}
if (s_values.numel() > 0) { // We add all elements from s_values to r_values only if s_values is not an empty tensor
THBlas_axpy<scalar_t>(blockSize, cast_value,
s_values_ptr + s_i * blockSize, 1,
r_values_ptr + r_i * blockSize, 1);
}
s_i++;
}
r_i++;
}
}
);
if (r.scalar_type() != commonDtype) {
r_values = r_values.to(r.scalar_type());
}
get_sparse_impl(r)->set_indices_and_values_unsafe(r_indices, r_values);
get_sparse_impl(r)->set_nnz_and_narrow(r_i);
// TODO: I think it may be possible to track inside the loop and
// detect when we are uncoalesced (e.g., by observing that an
// index goes backwards) which may be more precise than using the
// coalesced flag here. But this is easy.
return r._coalesced_(coalesced);
}
SparseTensor& add_out_sparse_non_contiguous(SparseTensor& r, const SparseTensor& t, const SparseTensor& src, Scalar value, ScalarType commonDtype) {
Tensor t_values = t._values().to(commonDtype);
Tensor s_values = src._values().to(commonDtype);
// If `t` or `src` contains non-contiguous `values`, `THBlas_axpy` doesn't work
// and we concat the indices and values tensors instead.
AT_DISPATCH_ALL_TYPES(
commonDtype, "add_out_sparse_cpu", [&] {
if (value.to<scalar_t>() != static_cast<scalar_t>(1)) {
s_values = s_values.mul(value);
}
});
LongTensor r_indices = at::cat({t._indices(), src._indices()}, 1);
Tensor r_values = at::cat({t_values, s_values}, 0).to(r.scalar_type());
alias_into_sparse(r, r_indices, r_values);
return r;
}
Tensor& add_out_dense_sparse_cpu(Tensor& r, const Tensor& dense, const SparseTensor& sparse_, Scalar value);
SparseTensor& add_out_sparse_cpu(SparseTensor& r, const SparseTensor& t, const SparseTensor& src, Scalar value) {
if (!t.is_sparse()) {
return add_out_dense_sparse_cpu(r, t, src, value);
}
// TODO: This test seems a bit goofy
TORCH_CHECK(src.is_sparse(), "add(sparse, dense) is not supported. Use add(dense, sparse) instead.");
AT_ASSERT(!t.is_cuda()); // the dispatch argument
TORCH_CHECK(!r.is_cuda(), "add: expected 'out' to be CPU tensor, but got CUDA tensor");
TORCH_CHECK(!src.is_cuda(), "add: expected 'other' to be a CPU tensor, but got a CUDA tensor");
TORCH_CHECK(t.sizes().equals(src.sizes()), "add: expected sizes of 'self' and 'other' to match, but ", t.sizes(), " != ", src.sizes());
auto commonDtype = promoteTypes(t.scalar_type(), src.scalar_type());
TORCH_CHECK(canCast(commonDtype, r.scalar_type()), "Can't convert result type ", commonDtype, " to output ", r.scalar_type(), " in add operation");
if (src._nnz() == 0) {
return copy_sparse_to_sparse_(r, t);
}
if (t._nnz() == 0) {
return mul_out_sparse_scalar(r, src, value);
}
TORCH_CHECK(is_same_density(t, src), "add: expected 'self' and 'other' to have same density, but 'self' has ", t.sparse_dim(), " sparse dimensions while 'other' has ", src.sparse_dim(), " sparse dimensions");
r.resize_as_(src);
if (src._values().is_contiguous() && t._values().is_contiguous()) {
return add_out_sparse_contiguous(r, t, src, value, commonDtype);
} else {
return add_out_sparse_non_contiguous(r, t, src, value, commonDtype);
}
}
// --------------------------------------------------------------------
// add(Tensor, SparseTensor, Scalar)
// formerly known as spcadd
// --------------------------------------------------------------------
template <typename scalar_t>
void add_dense_sparse_worker_cpu(Tensor& r, Scalar value, const SparseTensor& sparse, const Tensor& indices, const Tensor& values) {
auto indices_accessor = indices.accessor<int64_t, 2>();
auto values_accessor = values.accessor<scalar_t, 1>();
scalar_t* r_ptr = r.data_ptr<scalar_t>();
scalar_t cast_value = value.to<scalar_t>();
at::parallel_for(0, sparse._nnz(), 0, [&](int64_t start, int64_t end) {
for (auto k = start; k < end; k++) {
int64_t index = r.storage_offset();
for (int64_t d = 0; d < sparse.sparse_dim(); d++) {
index += r.stride(d) * indices_accessor[d][k];
}
r_ptr[index] += cast_value * values_accessor[k];
}
});
}
Tensor& add_out_dense_sparse_cpu(Tensor& r, const Tensor& dense, const SparseTensor& sparse_, Scalar value) {
AT_ASSERT(!r.is_sparse());
AT_ASSERT(!dense.is_sparse());
AT_ASSERT(sparse_.is_sparse());
AT_ASSERT(!dense.is_cuda()); // dispatch argument
TORCH_CHECK(!r.is_cuda(), "add: expected 'out' to be CPU tensor, but got CUDA tensor");
TORCH_CHECK(!sparse_.is_cuda(), "add: expected 'other' to be a CPU tensor, but got a CUDA tensor");
TORCH_CHECK(dense.sizes().equals(sparse_.sizes()), "add: expected 'self' and 'other' to have same size, but self has size ",
dense.sizes(), " while other has size ", sparse_.sizes(), " (FYI: dense-sparse addition does not currently support broadcasting)");
auto commonDtype = promoteTypes(dense.scalar_type(), sparse_.scalar_type());
TORCH_CHECK(canCast(commonDtype, r.scalar_type()), "Can't convert result type ", commonDtype, " to output ", r.scalar_type(), " in add operation");
r.resize_as_(dense);
SparseTensor sparse = sparse_.coalesce();
LongTensor indices = sparse._indices();
Tensor values = sparse._values();
int64_t nDim = dense.dim();
int64_t nDimI = sparse.sparse_dim();
if (sparse._nnz() == 0) {
if (!is_same_tensor(r, dense)) r.copy_(dense);
return r;
}
Tensor valuesBuffer = values.to(commonDtype);
Tensor resultBuffer = r;
if (r.scalar_type() != commonDtype) {
resultBuffer = dense.to(commonDtype);
} else if (!is_same_tensor(r, dense)) {
resultBuffer.copy_(dense);
}
// accessors rely on nnz test
if (nDim > nDimI) {
auto indices_accessor = indices.accessor<int64_t, 2>();
for (int64_t k = 0; k < sparse._nnz(); k++) {
Tensor dstBuffer = resultBuffer;
for (int64_t d = 0; d < sparse.sparse_dim(); d++) {
dstBuffer = dstBuffer.select(0, indices_accessor[d][k]);
}
Tensor srcBuffer = valuesBuffer.select(0, k);
dstBuffer.add_(srcBuffer, value);
}
} else {
AT_DISPATCH_ALL_TYPES(
commonDtype, "add_dense_sparse", [&] {
add_dense_sparse_worker_cpu<scalar_t>(resultBuffer, value, sparse, indices, valuesBuffer);
});
}
if (r.scalar_type() != commonDtype) {
r.copy_(resultBuffer);
}
return r;
}
// --------------------------------------------------------------------
// mul(SparseTensor, SparseTensor) [broadcasts]
// --------------------------------------------------------------------
Tensor mul_sparse(const Tensor& self, const Tensor& other) {
auto commonDtype = at::result_type(self, other);
Tensor result = at::empty({0}, self.options().dtype(commonDtype));
return at::mul_out(result, self, other); // redispatch!
}
Tensor& mul_sparse_(Tensor& self, const Tensor& other) {
return at::mul_out(self, self, other); // redispatch!
}
SparseTensor& mul_out_sparse_cpu(SparseTensor& r, const Tensor& t_, const Tensor& src_) {
if (src_.dim() == 0) {
return mul_out_sparse_zerodim(r, t_, src_);
} else if (t_.dim() == 0) {
return mul_out_sparse_zerodim(r, src_, t_);
}
TORCH_CHECK(t_.sizes().equals(src_.sizes()), "mul operands have incompatible sizes");
AT_ASSERT(!t_.is_cuda()); // dispatch argument
TORCH_CHECK(!r.is_cuda(), "mul: expected 'out' to be CPU tensor, but got CUDA tensor");
TORCH_CHECK(!src_.is_cuda(), "mul: expected 'other' to be a CPU tensor, but got a CUDA tensor");
TORCH_CHECK(t_.sizes().equals(src_.sizes()), "mul: expected 'self' and 'other' to have same sizes, but ", t_.sizes(), " != ", src_.sizes());
if (src_._nnz() == 0 || t_._nnz() == 0) {
r.resize_as_(src_);
return r.zero_();
}
SparseTensor t = t_.coalesce();
SparseTensor src = src_.coalesce();
// saving those because they can be overwritten when doing in-place operations
int64_t t_nnz = t._nnz(), s_nnz = src._nnz();
int64_t max_nnz = std::min(t_nnz, s_nnz); // multiply by zero is zero, and can be dropped
int64_t sparse_dim = src.sparse_dim();
LongTensor t_indices = t._indices();
LongTensor src_indices = src._indices();
LongTensor r_indices = at::empty({sparse_dim, max_nnz}, t_indices.options());
int64_t match, d;
int64_t r_i = 0, t_i = 0, s_i = 0;
auto commonDtype = promoteTypes(t_.scalar_type(), src_.scalar_type());
TORCH_CHECK(canCast(commonDtype, r.scalar_type()), "Can't convert result type ", commonDtype, " to output ", r.scalar_type(), " in mul operation");
Tensor t_values = t._values().to(commonDtype);
Tensor s_values = src._values().to(commonDtype);
Tensor r_buffer = new_values_with_size_of(t_values, max_nnz).zero_();
// NB: relies on nnz test above
auto t_indices_accessor = t_indices.accessor<int64_t, 2>();
auto r_indices_accessor = r_indices.accessor<int64_t, 2>();
auto src_indices_accessor = src_indices.accessor<int64_t, 2>();
// Check if we can find matching indices, and if so, write an
// entry to the result indices vector. Returns true if matching
// indices were found.
auto index_preamble = [&]() {
match = 1;
for (d = 0; d < sparse_dim; d++) {
if (t_indices_accessor[d][t_i] < src_indices_accessor[d][s_i]) {
t_i++;
match = 0;
break;
}
if (t_indices_accessor[d][t_i] > src_indices_accessor[d][s_i]) {
s_i++;
match = 0;
break;
}
}
if (!match) return false;
for (d = 0; d < sparse_dim; d++) {
r_indices_accessor[d][r_i] = t_indices_accessor[d][t_i];
}
return true;
};
if (t_values.dim() > 1) {
while (t_i < t_nnz && s_i < s_nnz) {
if (!index_preamble()) continue;
r_buffer.select(0, r_i).addcmul_(t_values.select(0, t_i), s_values.select(0, s_i));
r_i++;
t_i++;
s_i++;
}
} else {
AT_DISPATCH_ALL_TYPES(
commonDtype, "mul_out_sparse", [&] {
auto r_accessor = r_buffer.accessor<scalar_t, 1>();
auto t_accessor = t_values.accessor<scalar_t, 1>();
auto s_accessor = s_values.accessor<scalar_t, 1>();
while (t_i < t_nnz && s_i < s_nnz) {
if (!index_preamble()) continue;
r_accessor[r_i] = t_accessor[t_i] * s_accessor[s_i];
r_i++;
t_i++;
s_i++;
}
}
);
}
r.resize_as_(src);
Tensor r_values = r_buffer.to(r.scalar_type());
get_sparse_impl(r)->set_indices_and_values_unsafe(r_indices, r_values);
get_sparse_impl(r)->set_nnz_and_narrow(r_i);
return r._coalesced_(true);
}
// --------------------------------------------------------------------
// addmm(D1, S, D2, beta, alpha) -> D [broadcasts]
//
// D = beta * D1 + alpha * mm(S, D2)
// --------------------------------------------------------------------
template <typename scalar_t>
void s_addmm_out_sparse_dense_worker(int64_t nnz, int64_t dim_i, int64_t dim_j, int64_t dim_k, Tensor& r, Scalar beta, const Tensor& t, Scalar alpha, const Tensor& indices, const Tensor& values, const Tensor& dense) {
int64_t i;
// r_ = alpha * sparse * dense
scalar_t cast_alpha = alpha.to<scalar_t>();
scalar_t cast_beta = beta.to<scalar_t>();
if (cast_beta == 0) {
r.zero_();
} else if (cast_beta == 1) {
if (!is_same_tensor(r, t)) {
r.copy_(t);
}
} else {
at::mul_out(r, t, scalar_to_tensor(beta));
}
auto indices_accessor = indices.accessor<int64_t, 2>();
auto values_accessor = values.accessor<scalar_t, 1>();
scalar_t* dense_ptr = dense.data_ptr<scalar_t>();
scalar_t* r_ptr = r.data_ptr<scalar_t>();
int64_t dense_stride0 = dense.stride(0);
int64_t dense_stride1 = dense.stride(1);
int64_t r_stride0 = r.stride(0);
int64_t r_stride1 = r.stride(1);
for (i = 0; i < nnz; i++) {
scalar_t val = values_accessor[i];
int64_t row = indices_accessor[0][i];
int64_t col = indices_accessor[1][i];
if (col >= 0 && col < dim_j && row >= 0 && row < dim_i) {
THBlas_axpy<scalar_t>(dim_k,
cast_alpha * val,
dense_ptr + col * dense_stride0, dense_stride1,
r_ptr + row * r_stride0, r_stride1);
} else {
if (col < 0 || col >= dim_j) {
AT_ERROR("addmm: index out of column bound: ", col, " not between 1 and ", dim_j);
} else {
AT_ERROR("addmm: index out of row bound: ", row, " not between 1 and ", dim_i);
}
}
}
};
Tensor& s_addmm_out_sparse_dense_cpu(
Tensor& r,
const Tensor& t,
const SparseTensor& sparse_,
const Tensor& dense,
Scalar beta,
Scalar alpha
) {
// TODO: This error message seems awfully opaque
AT_ASSERT(!t.is_cuda());
TORCH_CHECK(!r.is_cuda(), "addmm: expected 'out' to be CPU tensor, but got CUDA tensor");
TORCH_CHECK(!sparse_.is_cuda(), "addmm: expected 'mat1' to be a CPU tensor, but got a CUDA tensor");
TORCH_CHECK(!dense.is_cuda(), "addmm: expected 'mat2' to be a CPU tensor, but got a CUDA tensor");
TORCH_CHECK(sparse_.sparse_dim() == 2, "addmm: matrices expected, got ", sparse_.sparse_dim(), "D tensor");
TORCH_CHECK(sparse_.dense_dim() == 0, "addmm: scalar values expected, got ", sparse_.dense_dim(), "D values");
TORCH_CHECK(dense.dim() == 2, "addmm: matrices expected, got ", dense.dim(), "D tensor");
// ixj * jxk = ixk
int64_t dim_i = sparse_.size(0);
int64_t dim_j = sparse_.size(1);
int64_t dim_k = dense.size(1);
TORCH_CHECK(dense.size(0) == dim_j,
"addmm: Argument #3 (dense): Expected dim 0 size ", dim_j, ", got ", dense.size(0));
TORCH_CHECK(t.size(0) == dim_i,
"addmm: Argument #1 (t): Expected dim 0 size ", dim_i, ", got ", t.size(0));
TORCH_CHECK(t.size(1) == dim_k,
"addmm: Argument #1 (t): Expected dim 1 size ", dim_k, ", got ", t.size(1));
r.resize_({dim_i, dim_k});
int64_t nnz = sparse_._nnz();
if (nnz == 0) {
at::mul_out(r, t, at::scalar_tensor(beta, r.options()));
return r;
}
LongTensor indices = sparse_._indices();
Tensor values = sparse_._values();
AT_DISPATCH_ALL_TYPES(
values.scalar_type(), "addmm_sparse_dense", [&] {
s_addmm_out_sparse_dense_worker<scalar_t>(nnz, dim_i, dim_j, dim_k, r, beta, t, alpha, indices, values, dense);
}
);
return r;
}
Tensor& addmm_out_sparse_dense_cpu(
Tensor& result,
const Tensor& self,
const SparseTensor& mat1,
const Tensor& mat2,
Scalar beta,
Scalar alpha
) {
Tensor b_self;
std::tie(b_self) = expand_size(self, {mat1.size(0), mat2.size(1)}, "addmm_out");
return s_addmm_out_sparse_dense_cpu(result, b_self, mat1, mat2, beta, alpha);
}
Tensor s_addmm_sparse_dense_cpu(
const Tensor& t,
const SparseTensor& sparse,
const Tensor& dense,
Scalar beta,
Scalar alpha
) {
Tensor r = at::empty({0}, t.options());
s_addmm_out_sparse_dense_cpu(r, t, sparse, dense, beta, alpha);
return r;
}
Tensor addmm_sparse_dense_cpu(
const Tensor& self,
const SparseTensor& mat1,
const Tensor& mat2,
Scalar beta,
Scalar alpha
) {
Tensor b_self;
std::tie(b_self) = expand_size(self, {mat1.size(0), mat2.size(1)}, "addmm_out");
return s_addmm_sparse_dense_cpu(b_self, mat1, mat2, beta, alpha);
}
Tensor& s_addmm_sparse_dense_cpu_(
Tensor& t,
const SparseTensor& sparse,
const Tensor& dense,
Scalar beta,
Scalar alpha
) {
return s_addmm_out_sparse_dense_cpu(t, t, sparse, dense, beta, alpha);
}
// NB: Purposely no broadcasting version of addmm inplace
Tensor _sparse_addmm(
const Tensor& t,
const SparseTensor& sparse,
const Tensor& dense,
Scalar beta,
Scalar alpha
) {
// _sparse_addmm forward is functionally equivalent to addmm; it's
// just the backward that is different. This technically does an
// unnecessary redispatch, I was too lazy to make it not do that
return at::addmm(t, sparse, dense, beta, alpha);
}
Tensor _sparse_mm(
const SparseTensor& sparse,
const Tensor& dense
) {
Tensor t = at::zeros({}, dense.options());
return at::_sparse_addmm(t, sparse, dense, 0, 1); // redispatch!
}
// NB: Despite its suggestive name, this actually only exists so that
// we can redispatch to addmm_out; this is NOT an implementation of
// the sparse masking version of mm
SparseTensor& _sparse_mm_out(
SparseTensor& result,
const SparseTensor& sparse,
const Tensor& dense
) {
Tensor t = at::zeros({}, dense.options());
return at::addmm_out(result, t, sparse, dense, 0, 1); // redispatch!
}
// --------------------------------------------------------------------
// hspmm(SparseTensor mat1, Tensor mat2)
// --------------------------------------------------------------------
SparseTensor& hspmm_out_sparse_cpu(SparseTensor& r, const SparseTensor& sparse_, const Tensor& dense) {
// TODO: Make this a real argument
Scalar alpha = 1;
AT_ASSERT(!sparse_.is_cuda()); // dispatch argument
TORCH_CHECK(!r.is_cuda(), "hspmm: expected 'out' to be CPU tensor, but got CUDA tensor");
TORCH_CHECK(!dense.is_cuda(), "hspmm: expected 'other' to be a CPU tensor, but got a CUDA tensor");
TORCH_CHECK(sparse_.sparse_dim() == 2,
"hspmm: Argument #2: matrices expected, got ", sparse_.sparse_dim(), "D tensor");
TORCH_CHECK(sparse_.dense_dim() == 0,
"hspmm: Argument #2: scalar values expected, got ", sparse_.dense_dim(), "D values");
TORCH_CHECK(dense.dim() == 2,
"hspmm: Argument #3: matrices expected, got ", dense.dim(), "D tensor");
int64_t m = sparse_.size(0);
int64_t k = sparse_.size(1);
int64_t n = dense.size(1);
TORCH_CHECK(dense.size(0) == k,
"hspmm: Argument #3: Expected dim 0 size ", k, ", got ", dense.size(0));
get_sparse_impl(r)->raw_resize_(1, 1, {m, n});
SparseTensor sparse = sparse_.coalesce();
int64_t nnz = sparse._nnz();
if (nnz == 0) {
r.zero_();
return r;
}
LongTensor indices = at::empty({1, nnz}, at::initialTensorOptions().dtype(kLong));
// Initialize the sparse matrix that will be used with spaddmm to send rows
// from the dense matrix to rows of the output's value tensor
SparseTensor newSparse = sparse.clone();
LongTensor spIndices = newSparse._indices();
LongTensor valueIndices = spIndices.select(0, 0);
// Compute output indices
auto valueIndices_accessor = valueIndices.accessor<int64_t, 1>();
auto indices_accessor = indices.accessor<int64_t, 2>();
int64_t i = -1, prevIdx = -1;
for (int64_t j = 0; j < nnz; j++) {
int64_t currIdx = valueIndices_accessor[j];
if (currIdx != prevIdx) {
indices_accessor[0][++i] = currIdx;
prevIdx = currIdx;
}
valueIndices_accessor[j] = i;
}
int64_t outNnz = i + 1;
indices.resize_({1, outNnz});
Tensor values = at::empty({outNnz, n}, dense.options());
std::vector<int64_t> new_size = get_sparse_impl(newSparse)->sizes().vec();
new_size[0] = outNnz;
get_sparse_impl(newSparse)->raw_resize_(get_sparse_impl(newSparse)->sparse_dim(), get_sparse_impl(newSparse)->dense_dim(), new_size);
// Compute output values tensor with sparse * dense multiplication
s_addmm_out_sparse_dense_cpu(values, values, newSparse, dense, 0, alpha);
get_sparse_impl(r)->set_indices_and_values_unsafe(indices, values);
return r;
}
SparseTensor hspmm_sparse_cpu(const SparseTensor& sparse, const Tensor& dense) {
SparseTensor r = at::empty({0}, sparse.options());
hspmm_out_sparse_cpu(r, sparse, dense);
return r;
}
// --------------------------------------------------------------------
// sspaddmm(S1, S2, D, beta, alpha) -> S
//
// S = beta * S1 + alpha * mm(S2, D)
// --------------------------------------------------------------------
SparseTensor& _sspaddmm_out_cpu(
SparseTensor& r,
const SparseTensor& t,
const SparseTensor& sparse_,
const Tensor& dense,
Scalar beta,
Scalar alpha
) {
AT_ASSERT(!t.is_cuda()); // dispatch argument
TORCH_CHECK(!r.is_cuda(), "sspaddmm: expected 'out' to be CPU tensor, but got CUDA tensor");
TORCH_CHECK(!sparse_.is_cuda(), "sspaddmm: expected 'mat1' to be a CPU tensor, but got a CUDA tensor");
TORCH_CHECK(!dense.is_cuda(), "sspaddmm: expected 'mat2' to be a CPU tensor, but got a CUDA tensor");
TORCH_CHECK(sparse_.sparse_dim() == 2,
"sspaddmm: Argument #2: matrices expected, got ", sparse_.sparse_dim(), "D tensor");
TORCH_CHECK(sparse_.dense_dim() == 0,
"sspaddmm: Argument #2: scalar values expected, got ", sparse_.dense_dim(), "D values");
TORCH_CHECK(dense.dim() == 2,
"sspaddmm: Argument #2: matrices expected, got ", dense.dim(), "D tensor");
SparseTensor sparse = sparse_.coalesce();
// ixj * jxk = ixk
int64_t dim_i = sparse.size(0);
int64_t dim_j = sparse.size(1);
int64_t dim_k = dense.size(1);
// NB: This has to occur before the checks, because r may alias t.
// See test_saddmm
get_sparse_impl(r)->raw_resize_(2, 0, {dim_i, dim_k});
TORCH_CHECK(dense.size(0) == dim_j,
"sspaddmm: Argument #3: Expected dim 0 size ", dim_j, ", got ", dense.size(0));
TORCH_CHECK(t.size(0) == dim_i,
"sspaddmm: Argument #1: Expected dim 0 size ", dim_i, ", got ", t.size(0));
TORCH_CHECK(t.size(1) == dim_k,
"sspaddmm: Argument #1: Expected dim 1 size ", dim_k, ", got ", t.size(1));
int64_t nnz = sparse._nnz();
LongTensor indices = sparse._indices();
Tensor values = sparse._values();
LongTensor csr = _to_csr(indices.data_ptr<int64_t>(), dim_i, nnz);
int64_t t_nnz = t._nnz();
int64_t r_nnz = nnz * dim_k + t_nnz;
LongTensor newi = at::empty({2, r_nnz}, kLong);
LongTensor newv = native::zeros({r_nnz}, values.options());
if (t_nnz != 0) {
LongTensor narrowi = newi.narrow(1, 0, t_nnz);
Tensor narrowv = newv.narrow(0, 0, t_nnz);
narrowi.copy_(t._indices());
narrowv.copy_(t._values());
newv.mul_(beta);
}
// sparse = sparse * dense
int64_t p = t_nnz;
auto csr_accessor = csr.accessor<int64_t, 1>();
auto indices_accessor = indices.accessor<int64_t, 2>();
auto newi_accessor = newi.accessor<int64_t, 2>();
int64_t dense_stride0 = dense.stride(0);
int64_t dense_stride1 = dense.stride(1);
int64_t newv_stride0 = newv.stride(0);
AT_DISPATCH_ALL_TYPES(
values.scalar_type(), "sspmm", [&] {
auto values_accessor = values.accessor<scalar_t, 1>();
scalar_t* dense_ptr = dense.data_ptr<scalar_t>();
scalar_t* newv_ptr = newv.data_ptr<scalar_t>();
scalar_t cast_alpha = alpha.to<scalar_t>();
for (int64_t h = 0; h < dim_i; h++) {
int64_t i_start = csr_accessor[h];
int64_t i_end = csr_accessor[h+1];
for (int64_t i = i_start; i < i_end; i++) {
scalar_t val = values_accessor[i];
int64_t col = indices_accessor[1][i];
if (col >= 0 && col < dim_j) {
THBlas_axpy<scalar_t>(dim_k,
cast_alpha * val,
dense_ptr + col * dense_stride0, dense_stride1,
newv_ptr + p * newv_stride0, 1);
} else {
AT_ERROR("index out of bound. sspmm: ", col, " not between 1 and ", dim_j);
}
}
// Fill up the indices with the right values
if (i_start != i_end) {
for (int64_t i = 0; i < dim_k; i++) {
newi_accessor[0][p+i] = h;
newi_accessor[1][p+i] = i;
}
p += dim_k;
}
}
}
);
// to avoid a clone
get_sparse_impl(r)->set_indices_and_values_unsafe(newi, newv);
get_sparse_impl(r)->set_nnz_and_narrow(p);
return r;
}
// sparse, sparse, sparse, dense, real, real -> sparse
Tensor& _sspaddmm_out_only_sparse(Tensor& result, const Tensor& self,
const Tensor& mat1, const Tensor& mat2, Scalar beta, Scalar alpha) {
AT_ERROR("tensor.sspaddmm(...) can only be called on sparse tensors");
}
// sparse, dense -> sparse
Tensor smm(const Tensor& self, const Tensor& mat2) {
auto result = at::empty({0}, self.options());
at::sspaddmm_out(result, result, self, mat2, 0.0, 1.0);
return result;
}
// sparse, sparse, dense, real, real -> sparse
Tensor sspaddmm(const Tensor& self, const Tensor& mat1, const Tensor& mat2,
Scalar beta, Scalar alpha) {