spmm.hip 7.24 KB
Newer Older
sangwzh's avatar
sangwzh committed
1
// !!! This is a file automatically generated by hipify!!!
2
/**
3
 *  Copyright (c) 2020 by Contributors
4
5
 * @file array/cuda/spmm.cu
 * @brief SPMM C APIs and definitions.
6
7
 */
#include <dgl/array.h>
8

9
#include "../../runtime/cuda/cuda_common.h"
sangwzh's avatar
sangwzh committed
10
11
12
#include "functor.cuh"
#include "ge_spmm.cuh"
#include "spmm.cuh"
13
14
15
16
17
18
19

namespace dgl {

using namespace cuda;

namespace aten {

20
/**
21
22
 * @brief CUDA implementation of g-SpMM on Csr format.
 * @note use cusparse if the reduce operator is `sum` and there is
23
24
 *       no broadcast, use dgl's kernel in other cases.
 */
25
template <int XPU, typename IdType, typename DType>
26
27
28
29
void SpMMCsr(
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const CSRMatrix& csr, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux) {
30
31
32
  bool is_scalar_efeat = efeat.NumElements() == csr.indices->shape[0];
  bool use_efeat = op != "copy_lhs";

33
  if (reduce == "sum") {
34
    bool more_nnz = (csr.indices->shape[0] > csr.num_rows * csr.num_cols);
35
    if (op == "copy_lhs" && cusparse_available<DType, IdType>(more_nnz)) {
36
      // cusparse
37
      int64_t x_length = 1;
38
      for (int i = 1; i < ufeat->ndim; ++i) x_length *= ufeat->shape[i];
39
      CusparseCsrmm2<DType, IdType>(
40
41
42
43
44
          ufeat->ctx, csr, static_cast<DType*>(ufeat->data), nullptr,
          static_cast<DType*>(out->data), x_length);
    } else if (
        op == "mul" && is_scalar_efeat &&
        cusparse_available<DType, IdType>(more_nnz)) {
45
      // cusparse
46
      int64_t x_length = 1;
47
      for (int i = 1; i < ufeat->ndim; ++i) x_length *= ufeat->shape[i];
48
      if (!IsNullArray(csr.data)) {
49
        efeat = IndexSelect(efeat, csr.data);
50
      }
51
      CusparseCsrmm2<DType, IdType>(
52
53
          ufeat->ctx, csr, static_cast<DType*>(ufeat->data),
          static_cast<DType*>(efeat->data), static_cast<DType*>(out->data),
54
          x_length);
55
    } else {  // general kernel
56
57
58
      SWITCH_OP(op, Op, {
        cuda::SpMMCsr<IdType, DType, Op, cuda::reduce::Sum<IdType, DType> >(
            bcast, csr, ufeat, efeat, out, NullArray(), NullArray());
59
60
61
      });
    }
  } else if (reduce == "max") {
62
63
64
    SWITCH_OP(op, Op, {
      cuda::SpMMCsr<IdType, DType, Op, cuda::reduce::Max<IdType, DType> >(
          bcast, csr, ufeat, efeat, out, out_aux[0], out_aux[1]);
65
66
    });
  } else if (reduce == "min") {
67
68
69
    SWITCH_OP(op, Op, {
      cuda::SpMMCsr<IdType, DType, Op, cuda::reduce::Min<IdType, DType> >(
          bcast, csr, ufeat, efeat, out, out_aux[0], out_aux[1]);
70
71
72
73
74
75
    });
  } else {
    LOG(FATAL) << "Not implemented";
  }
}

76
/**
77
 * @brief CUDA implementation of g-SpMM on Coo format.
78
 */
79
template <int XPU, typename IdType, typename DType>
80
81
82
83
void SpMMCoo(
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const COOMatrix& coo, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux) {
84
  if (reduce == "sum") {
85
    SWITCH_OP(op, Op, {
86
      cuda::SpMMCoo<IdType, DType, Op, cuda::reduce::Sum<IdType, DType, true> >(
87
          bcast, coo, ufeat, efeat, out, NullArray(), NullArray());
88
89
    });
  } else if (reduce == "max") {
90
    SWITCH_OP(op, Op, {
91
      cuda::SpMMCoo<IdType, DType, Op, cuda::reduce::Max<IdType, DType, true> >(
92
          bcast, coo, ufeat, efeat, out, out_aux[0], out_aux[1]);
93
    });
94
  } else if (reduce == "min") {
95
    SWITCH_OP(op, Op, {
96
      cuda::SpMMCoo<IdType, DType, Op, cuda::reduce::Min<IdType, DType, true> >(
97
          bcast, coo, ufeat, efeat, out, out_aux[0], out_aux[1]);
98
99
100
101
102
103
    });
  } else {
    LOG(FATAL) << "Not implemented";
  }
}

104
template void SpMMCsr<kDGLCUDA, int32_t, __half>(
105
106
107
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const CSRMatrix& csr, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
108
template void SpMMCsr<kDGLCUDA, int64_t, __half>(
109
110
111
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const CSRMatrix& csr, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
112
#if BF16_ENABLED
sangwzh's avatar
sangwzh committed
113
template void SpMMCsr<kDGLCUDA, int32_t, __hip_bfloat16>(
114
115
116
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const CSRMatrix& csr, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
sangwzh's avatar
sangwzh committed
117
template void SpMMCsr<kDGLCUDA, int64_t, __hip_bfloat16>(
118
119
120
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const CSRMatrix& csr, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
121
122
#endif  // BF16_ENABLED
template void SpMMCsr<kDGLCUDA, int32_t, float>(
123
124
125
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const CSRMatrix& csr, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
126
template void SpMMCsr<kDGLCUDA, int64_t, float>(
127
128
129
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const CSRMatrix& csr, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
130
template void SpMMCsr<kDGLCUDA, int32_t, double>(
131
132
133
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const CSRMatrix& csr, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
134
template void SpMMCsr<kDGLCUDA, int64_t, double>(
135
136
137
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const CSRMatrix& csr, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
138

139
template void SpMMCoo<kDGLCUDA, int32_t, __half>(
140
141
142
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const COOMatrix& coo, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
143
template void SpMMCoo<kDGLCUDA, int64_t, __half>(
144
145
146
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const COOMatrix& coo, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
147
#if BF16_ENABLED
sangwzh's avatar
sangwzh committed
148
template void SpMMCoo<kDGLCUDA, int32_t, __hip_bfloat16>(
149
150
151
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const COOMatrix& coo, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
sangwzh's avatar
sangwzh committed
152
template void SpMMCoo<kDGLCUDA, int64_t, __hip_bfloat16>(
153
154
155
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const COOMatrix& coo, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
156
157
#endif  // BF16_ENABLED
template void SpMMCoo<kDGLCUDA, int32_t, float>(
158
159
160
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const COOMatrix& coo, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
161
template void SpMMCoo<kDGLCUDA, int64_t, float>(
162
163
164
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const COOMatrix& coo, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
165
template void SpMMCoo<kDGLCUDA, int32_t, double>(
166
167
168
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const COOMatrix& coo, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
169
template void SpMMCoo<kDGLCUDA, int64_t, double>(
170
171
172
    const std::string& op, const std::string& reduce, const BcastOff& bcast,
    const COOMatrix& coo, NDArray ufeat, NDArray efeat, NDArray out,
    std::vector<NDArray> out_aux);
173
174
175

}  // namespace aten
}  // namespace dgl