forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparseCsrTensorMath.h
84 lines (74 loc) · 2.32 KB
/
SparseCsrTensorMath.h
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
#pragma once
#include <ATen/Tensor.h>
#include <ATen/core/Scalar.h>
#include <ATen/TensorUtils.h>
#include <ATen/native/ReductionType.h>
#include <ATen/native/cpu/SpmmReduceKernel.h>
namespace at::native::sparse::impl {
// Returns true if all entries of self are zero
// TODO: This has potential to be a generic helper
inline bool _is_sparse_and_zero(const Tensor& self) {
if (self.layout() == kSparse || self.layout() == kSparseCsr ||
self.layout() == kSparseCsc || self.layout() == kSparseBsr ||
self.layout() == kSparseBsc) {
if (self._nnz() == 0) {
return true;
}
}
return false;
}
inline void _check_is_cpu(const Tensor& self, std::string_view name) {
TORCH_CHECK(
self.is_cpu(),
"Expected all tensors to be on the same device. addmm expected '",
name,
"' to be CPU tensor, but got ",
self.device(),
" tensor");
}
inline void _check_is_cuda(const Tensor& self, std::string_view name) {
TORCH_CHECK(
self.is_cuda(),
"Expected all tensors to be on the same device. addmm expected '",
name,
"' to be CUDA tensor, but got ",
self.device(),
" tensor");
}
inline void _check_dim(const Tensor& self, int64_t target_dim, std::string_view name) {
if (target_dim == 2) {
TORCH_CHECK(
self.dim() == target_dim,
name, " must be a matrix, ",
"got ", self.dim(), "-D tensor");
}
TORCH_CHECK(
self.dim() == target_dim,
"Expected ",
name,
" to be of dimension ",
target_dim,
" but got ",
self.dim(),
" instead.");
}
template <bool train>
inline void check_sparse_mm_reduce_impl_inputs(
const Tensor& self,
const Tensor& grad_out,
const Tensor& other) {
TORCH_INTERNAL_ASSERT(self.is_sparse_csr());
const auto input_scalar_type = self.values().scalar_type();
CheckedFrom c = train ? "sparse_mm_reduce_backward" : "sparse_mm_reduce";
if (train) {
checkLayout(c, grad_out, kStrided);
checkScalarType(c, {grad_out, "grad_out", 1}, input_scalar_type);
check_dim_size(grad_out, 2, 0, self.size(0));
check_dim_size(grad_out, 2, 1, other.size(1));
}
int pos = train ? 2 : 1;
checkLayout(c, other, kStrided);
checkScalarType(c, {other, "other", pos}, input_scalar_type);
check_dim_size(other, 2, 0, self.size(1));
}
} // at::native::sparse::impl