spmm.h 20.4 KB
Newer Older
1
/**
2
 *  Copyright (c) 2020 by Contributors
3
4
 * @file array/cpu/spmm.h
 * @brief SPMM CPU kernel function header.
5
6
7
8
9
10
 */
#ifndef DGL_ARRAY_CPU_SPMM_H_
#define DGL_ARRAY_CPU_SPMM_H_

#include <dgl/array.h>
#include <dgl/bcast.h>
11
#include <dgl/runtime/config.h>
12
#include <dgl/runtime/parallel_for.h>
13
#include <math.h>
14

15
#include <algorithm>
16
17
#include <limits>
#include <memory>
18
#include <vector>
19

20
21
#include "spmm_binary_ops.h"
#if !defined(_WIN32)
22
23
24
#ifdef USE_LIBXSMM
#include "spmm_blocking_libxsmm.h"
#endif  // USE_LIBXSMM
25
#endif  // _WIN32
26
27
28
29
namespace dgl {
namespace aten {
namespace cpu {

30
/**
31
32
33
34
35
36
37
38
 * @brief Naive CPU kernel of SpMM on Csr format.
 * @param cpu_spec JIT'ed kernel
 * @param bcast Broadcast information.
 * @param csr The Csr matrix.
 * @param X The feature on source nodes.
 * @param W The feature on edges.
 * @param O The result feature on destination nodes.
 * @note it uses node parallel strategy, different threads are responsible
39
40
41
 *       for the computation of different nodes.
 */
template <typename IdType, typename DType, typename Op>
42
43
44
void SpMMSumCsrNaive(
    const BcastOff& bcast, const CSRMatrix& csr, const DType* X, const DType* W,
    DType* O) {
45
46
47
48
49
  const bool has_idx = !IsNullArray(csr.data);
  const IdType* indptr = csr.indptr.Ptr<IdType>();
  const IdType* indices = csr.indices.Ptr<IdType>();
  const IdType* edges = csr.data.Ptr<IdType>();
  int64_t dim = bcast.out_len, lhs_dim = bcast.lhs_len, rhs_dim = bcast.rhs_len;
50
51
52
53
54
55
56
57
58
59
60
  runtime::parallel_for(0, csr.num_rows, [&](size_t b, size_t e) {
    for (auto rid = b; rid < e; ++rid) {
      const IdType row_start = indptr[rid], row_end = indptr[rid + 1];
      DType* out_off = O + rid * dim;
      for (IdType j = row_start; j < row_end; ++j) {
        const IdType cid = indices[j];
        const IdType eid = has_idx ? edges[j] : j;
        for (int64_t k = 0; k < dim; ++k) {
          const int64_t lhs_add = bcast.use_bcast ? bcast.lhs_offset[k] : k;
          const int64_t rhs_add = bcast.use_bcast ? bcast.rhs_offset[k] : k;
          const DType* lhs_off =
61
              Op::use_lhs ? X + cid * lhs_dim + lhs_add : nullptr;
62
          const DType* rhs_off =
63
              Op::use_rhs ? W + eid * rhs_dim + rhs_add : nullptr;
64
65
          out_off[k] += Op::Call(lhs_off, rhs_off);
        }
66
67
      }
    }
68
  });
69
70
}

71
/**
72
73
74
75
76
77
78
 * @brief CPU kernel of SpMM on Csr format.
 * @param bcast Broadcast information.
 * @param csr The Csr matrix.
 * @param ufeat The feature on source nodes.
 * @param efeat The feature on edges.
 * @param out The result feature on destination nodes.
 * @note it uses node parallel strategy, different threads are responsible
79
80
81
 *       for the computation of different nodes.
 */
template <typename IdType, typename DType, typename Op>
82
83
84
void SpMMSumCsr(
    const BcastOff& bcast, const CSRMatrix& csr, NDArray ufeat, NDArray efeat,
    NDArray out) {
85
86
87
88
89
90
91
  const bool has_idx = !IsNullArray(csr.data);
  const IdType* indptr = csr.indptr.Ptr<IdType>();
  const IdType* indices = csr.indices.Ptr<IdType>();
  const IdType* edges = csr.data.Ptr<IdType>();
  const DType* X = ufeat.Ptr<DType>();
  const DType* W = efeat.Ptr<DType>();
  DType* O = out.Ptr<DType>();
92
93
94
95
96
97
98
  CHECK_NOTNULL(indptr);
  CHECK_NOTNULL(O);
  if (Op::use_lhs) {
    CHECK_NOTNULL(indices);
    CHECK_NOTNULL(X);
  }
  if (Op::use_rhs) {
99
    if (has_idx) CHECK_NOTNULL(edges);
100
101
    CHECK_NOTNULL(W);
  }
102
#if !defined(_WIN32)
103
#ifdef USE_LIBXSMM
104
105
106
  const bool no_libxsmm = bcast.use_bcast ||
                          std::is_same<DType, double>::value ||
                          !dgl::runtime::Config::Global()->IsLibxsmmAvailable();
107
108
  if (!no_libxsmm) {
    SpMMSumCsrLibxsmm<IdType, DType, Op>(bcast, csr, ufeat, efeat, out);
109
  } else {
110
#endif  // USE_LIBXSMM
111
#endif  // _WIN32
112
    SpMMSumCsrNaive<IdType, DType, Op>(bcast, csr, X, W, O);
113
#if !defined(_WIN32)
114
#ifdef USE_LIBXSMM
115
  }
116
#endif  // USE_LIBXSMM
117
#endif  // _WIN32
118
119
}

120
/**
121
122
123
124
125
126
127
 * @brief CPU kernel of SpMM on Coo format.
 * @param bcast Broadcast information.
 * @param coo The Coo matrix.
 * @param ufeat The feature on source nodes.
 * @param efeat The feature on edges.
 * @param out The result feature on destination nodes.
 * @note it uses node parallel strategy, different threads are responsible
128
129
130
131
 *       for the computation of different nodes. To avoid possible data hazard,
 *       we use atomic operators in the reduction phase.
 */
template <typename IdType, typename DType, typename Op>
132
133
134
void SpMMSumCoo(
    const BcastOff& bcast, const COOMatrix& coo, NDArray ufeat, NDArray efeat,
    NDArray out) {
135
136
137
138
139
140
  const bool has_idx = !IsNullArray(coo.data);
  const IdType* row = coo.row.Ptr<IdType>();
  const IdType* col = coo.col.Ptr<IdType>();
  const IdType* edges = coo.data.Ptr<IdType>();
  const DType* X = ufeat.Ptr<DType>();
  const DType* W = efeat.Ptr<DType>();
141
  int64_t dim = bcast.out_len, lhs_dim = bcast.lhs_len, rhs_dim = bcast.rhs_len;
142
143
144
145
146
147
148
149
150
  DType* O = out.Ptr<DType>();
  const int64_t nnz = coo.row->shape[0];
  // fill zero elements
  memset(O, 0, out.GetSize());
  // spmm
#pragma omp parallel for
  for (IdType i = 0; i < nnz; ++i) {
    const IdType rid = row[i];
    const IdType cid = col[i];
151
    const IdType eid = has_idx ? edges[i] : i;
152
153
154
155
    DType* out_off = O + cid * dim;
    for (int64_t k = 0; k < dim; ++k) {
      const int64_t lhs_add = bcast.use_bcast ? bcast.lhs_offset[k] : k;
      const int64_t rhs_add = bcast.use_bcast ? bcast.rhs_offset[k] : k;
156
      const DType* lhs_off =
157
          Op::use_lhs ? X + rid * lhs_dim + lhs_add : nullptr;
158
      const DType* rhs_off =
159
          Op::use_rhs ? W + eid * rhs_dim + rhs_add : nullptr;
160
      const DType val = Op::Call(lhs_off, rhs_off);
161
      if (val != 0) {
162
#pragma omp atomic
163
164
        out_off[k] += val;
      }
165
166
167
168
    }
  }
}

169
/**
170
171
172
173
174
175
176
 * @brief CPU kernel of SpMM-Min/Max on Csr format.
 * @param bcast Broadcast information.
 * @param csr The Csr matrix.
 * @param ufeat The feature on source nodes.
 * @param efeat The feature on edges.
 * @param out The result feature on destination nodes.
 * @param argu Arg-Min/Max on source nodes, which refers the source node indices
177
 *        correspond to the minimum/maximum values of reduction result on
178
 *        destination nodes. It's useful in computing gradients of Min/Max
179
 *        reducer.
180
 * @param arge Arg-Min/Max on edges. which refers the source node indices
181
          correspond to the minimum/maximum values of reduction result on
182
 *        destination nodes. It's useful in computing gradients of Min/Max
183
 *        reducer.
184
 * @note It uses node parallel strategy, different threads are responsible for
185
 *       the computation of different nodes.
186
 * @note The result will contain infinity for zero-degree nodes.
187
188
 */
template <typename IdType, typename DType, typename Op, typename Cmp>
189
190
191
void SpMMCmpCsr(
    const BcastOff& bcast, const CSRMatrix& csr, NDArray ufeat, NDArray efeat,
    NDArray out, NDArray argu, NDArray arge) {
192
193
194
  const bool has_idx = !IsNullArray(csr.data);
  const IdType* indptr = static_cast<IdType*>(csr.indptr->data);
  const IdType* indices = static_cast<IdType*>(csr.indices->data);
195
  const IdType* edges =
196
      has_idx ? static_cast<IdType*>(csr.data->data) : nullptr;
197
198
199
  const DType* X = Op::use_lhs ? static_cast<DType*>(ufeat->data) : nullptr;
  const DType* W = Op::use_rhs ? static_cast<DType*>(efeat->data) : nullptr;
  const int64_t dim = bcast.out_len, lhs_dim = bcast.lhs_len,
200
201
                rhs_dim = bcast.rhs_len;
  DType* O = static_cast<DType*>(out->data);
202
203
  IdType* argX = Op::use_lhs ? static_cast<IdType*>(argu->data) : nullptr;
  IdType* argW = Op::use_rhs ? static_cast<IdType*>(arge->data) : nullptr;
204
205
206
207
208
209
210
211
  CHECK_NOTNULL(indptr);
  CHECK_NOTNULL(O);
  if (Op::use_lhs) {
    CHECK_NOTNULL(indices);
    CHECK_NOTNULL(X);
    CHECK_NOTNULL(argX);
  }
  if (Op::use_rhs) {
212
    if (has_idx) CHECK_NOTNULL(edges);
213
214
215
216
217
218
    CHECK_NOTNULL(W);
    CHECK_NOTNULL(argW);
  }
#if !defined(_WIN32)
#ifdef USE_LIBXSMM

219
220
221
  const bool no_libxsmm = bcast.use_bcast ||
                          std::is_same<DType, double>::value ||
                          !dgl::runtime::Config::Global()->IsLibxsmmAvailable();
222
  if (!no_libxsmm) {
223
224
    SpMMCmpCsrLibxsmm<IdType, DType, Op, Cmp>(
        bcast, csr, ufeat, efeat, out, argu, arge);
225
226
227
228
  } else {
#endif  // USE_LIBXSMM
#endif  // _WIN32

229
230
231
232
233
234
235
236
237
238
239
240
241
    runtime::parallel_for(0, csr.num_rows, [&](size_t b, size_t e) {
      for (auto rid = b; rid < e; ++rid) {
        const IdType row_start = indptr[rid], row_end = indptr[rid + 1];
        DType* out_off = O + rid * dim;
        IdType* argx_off = argX + rid * dim;
        IdType* argw_off = argW + rid * dim;
        for (IdType j = row_start; j < row_end; ++j) {
          const IdType cid = indices[j];
          const IdType eid = has_idx ? edges[j] : j;
          for (int64_t k = 0; k < dim; ++k) {
            const int64_t lhs_add = bcast.use_bcast ? bcast.lhs_offset[k] : k;
            const int64_t rhs_add = bcast.use_bcast ? bcast.rhs_offset[k] : k;
            const DType* lhs_off =
242
                Op::use_lhs ? X + cid * lhs_dim + lhs_add : nullptr;
243
            const DType* rhs_off =
244
                Op::use_rhs ? W + eid * rhs_dim + rhs_add : nullptr;
245
246
247
248
249
250
251
            const DType val = Op::Call(lhs_off, rhs_off);
            if (Cmp::Call(out_off[k], val)) {
              out_off[k] = val;
              if (Op::use_lhs) argx_off[k] = cid;
              if (Op::use_rhs) argw_off[k] = eid;
            }
          }
252
253
        }
      }
254
    });
255
256
257
258
259
#if !defined(_WIN32)
#ifdef USE_LIBXSMM
  }
#endif  // USE_LIBXSMM
#endif  // _WIN32
260
261
}

262
/**
263
264
265
266
267
268
269
 * @brief CPU kernel of SpMM-Min/Max on Csr format.
 * @param bcast Broadcast information.
 * @param csr The Csr matrix.
 * @param ufeat The feature on source nodes.
 * @param efeat The feature on edges.
 * @param out The result feature on destination nodes.
 * @param argu Arg-Min/Max on source nodes, which refers the source node indices
270
271
 *        correspond to the minimum/maximum values of reduction result on
 *        destination nodes. It's useful in computing gradients of Min/Max
272
 *        reducer.
273
 * @param arge Arg-Min/Max on edges. which refers the source node indices
274
 *        correspond to the minimum/maximum values of reduction result on
275
 *        destination nodes. It's useful in computing gradients of Min/Max
276
 *        reducer.
277
 * @param argu_ntype Node type of the arg-Min/Max on source nodes, which refers
278
279
280
 *        the source node types correspond to the minimum/maximum values of
 *        reduction result on destination nodes. It's useful in computing
 *        gradients of Min/Max reducer.
281
 * @param arge_etype Edge-type of the arg-Min/Max on edges. which refers the
282
283
284
 *        source node indices correspond to the minimum/maximum values of
 *        reduction result on destination nodes. It's useful in computing
 *        gradients of Min/Max reducer.
285
286
 * @param src_type Node type of the source nodes of an etype
 * @param etype Edge type
287
288
 */
template <typename IdType, typename DType, typename Op, typename Cmp>
289
290
291
292
void SpMMCmpCsrHetero(
    const BcastOff& bcast, const CSRMatrix& csr, NDArray ufeat, NDArray efeat,
    NDArray out, NDArray argu, NDArray arge, NDArray argu_ntype,
    NDArray arge_etype, const int ntype, const int etype) {
293
294
295
296
  const bool has_idx = !IsNullArray(csr.data);
  const IdType* indptr = static_cast<IdType*>(csr.indptr->data);
  const IdType* indices = static_cast<IdType*>(csr.indices->data);
  const IdType* edges =
297
      has_idx ? static_cast<IdType*>(csr.data->data) : nullptr;
298
299
300
301
302
303
304
  const DType* X = Op::use_lhs ? static_cast<DType*>(ufeat->data) : nullptr;
  const DType* W = Op::use_rhs ? static_cast<DType*>(efeat->data) : nullptr;
  const int64_t dim = bcast.out_len, lhs_dim = bcast.lhs_len,
                rhs_dim = bcast.rhs_len;
  DType* O = static_cast<DType*>(out->data);
  IdType* argX = Op::use_lhs ? static_cast<IdType*>(argu->data) : nullptr;
  IdType* argW = Op::use_rhs ? static_cast<IdType*>(arge->data) : nullptr;
305
306
307
308
  IdType* argX_ntype =
      Op::use_lhs ? static_cast<IdType*>(argu_ntype->data) : nullptr;
  IdType* argW_etype =
      Op::use_rhs ? static_cast<IdType*>(arge_etype->data) : nullptr;
309
310
311
312
313
314
315
316
  CHECK_NOTNULL(indptr);
  CHECK_NOTNULL(O);
  if (Op::use_lhs) {
    CHECK_NOTNULL(indices);
    CHECK_NOTNULL(X);
    CHECK_NOTNULL(argX);
  }
  if (Op::use_rhs) {
317
    if (has_idx) CHECK_NOTNULL(edges);
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
    CHECK_NOTNULL(W);
    CHECK_NOTNULL(argW);
  }
  // TODO(Israt): Use LIBXSMM. Homogeneous graph uses LIBXMM when enabled.
  runtime::parallel_for(0, csr.num_rows, [&](size_t b, size_t e) {
    for (auto rid = b; rid < e; ++rid) {
      const IdType row_start = indptr[rid], row_end = indptr[rid + 1];
      DType* out_off = O + rid * dim;
      IdType* argx_off = argX + rid * dim;
      IdType* argw_off = argW + rid * dim;
      IdType* argx_ntype = argX_ntype + rid * dim;
      IdType* argw_etype = argW_etype + rid * dim;
      for (IdType j = row_start; j < row_end; ++j) {
        const IdType cid = indices[j];
        const IdType eid = has_idx ? edges[j] : j;
        for (int64_t k = 0; k < dim; ++k) {
          const int64_t lhs_add = bcast.use_bcast ? bcast.lhs_offset[k] : k;
          const int64_t rhs_add = bcast.use_bcast ? bcast.rhs_offset[k] : k;
          const DType* lhs_off =
337
              Op::use_lhs ? X + cid * lhs_dim + lhs_add : nullptr;
338
          const DType* rhs_off =
339
              Op::use_rhs ? W + eid * rhs_dim + rhs_add : nullptr;
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
          const DType val = Op::Call(lhs_off, rhs_off);
          if (Cmp::Call(out_off[k], val)) {
            out_off[k] = val;
            if (Op::use_lhs) {
              argx_off[k] = cid;
              argx_ntype[k] = ntype;
            }
            if (Op::use_rhs) {
              argw_off[k] = eid;
              argw_etype[k] = etype;
            }
          }
        }
      }
    }
  });
}

358
/**
359
360
361
362
363
364
365
 * @brief CPU kernel of SpMM-Min/Max on Coo format.
 * @param bcast Broadcast information.
 * @param coo The Coo matrix.
 * @param ufeat The feature on source nodes.
 * @param efeat The feature on edges.
 * @param out The result feature on destination nodes.
 * @param argu Arg-Min/Max on source nodes, which refers the source node indices
366
 *        correspond to the minimum/maximum values of reduction result on
367
 *        destination nodes. It's useful in computing gradients of Min/Max
368
 *        reducer.
369
 * @param arge Arg-Min/Max on edges. which refers the source node indices
370
 *        correspond to the minimum/maximum values of reduction result on
371
 *        destination nodes. It's useful in computing gradients of Min/Max
372
 *        reducer.
373
 * @note it uses node parallel strategy, different threads are responsible for
374
375
 *       the computation of different nodes. To avoid possible data hazard, we
 *       use atomic operators in the reduction phase.
376
 * @note The result will contain infinity for zero-degree nodes.
377
378
 */
template <typename IdType, typename DType, typename Op, typename Cmp>
379
380
381
void SpMMCmpCoo(
    const BcastOff& bcast, const COOMatrix& coo, NDArray ufeat, NDArray efeat,
    NDArray out, NDArray argu, NDArray arge) {
382
383
384
  const bool has_idx = !IsNullArray(coo.data);
  const IdType* row = static_cast<IdType*>(coo.row->data);
  const IdType* col = static_cast<IdType*>(coo.col->data);
385
  const IdType* edges =
386
      has_idx ? static_cast<IdType*>(coo.data->data) : nullptr;
387
388
389
  const DType* X = Op::use_lhs ? static_cast<DType*>(ufeat->data) : nullptr;
  const DType* W = Op::use_rhs ? static_cast<DType*>(efeat->data) : nullptr;
  const int64_t dim = bcast.out_len, lhs_dim = bcast.lhs_len,
390
391
                rhs_dim = bcast.rhs_len;
  DType* O = static_cast<DType*>(out->data);
392
393
  IdType* argX = Op::use_lhs ? static_cast<IdType*>(argu->data) : nullptr;
  IdType* argW = Op::use_rhs ? static_cast<IdType*>(arge->data) : nullptr;
394
395
396
397
398
399
400
401
  const int64_t nnz = coo.row->shape[0];
  // fill zero elements
  std::fill(O, O + out.NumElements(), Cmp::zero);
  // spmm
#pragma omp parallel for
  for (IdType i = 0; i < nnz; ++i) {
    const IdType rid = row[i];
    const IdType cid = col[i];
402
    const IdType eid = has_idx ? edges[i] : i;
403
    DType* out_off = O + cid * dim;
404
405
    IdType* argx_off = Op::use_lhs ? argX + cid * dim : nullptr;
    IdType* argw_off = Op::use_rhs ? argW + cid * dim : nullptr;
406
407
408
    for (int64_t k = 0; k < dim; ++k) {
      const int64_t lhs_add = bcast.use_bcast ? bcast.lhs_offset[k] : k;
      const int64_t rhs_add = bcast.use_bcast ? bcast.rhs_offset[k] : k;
409
      const DType* lhs_off =
410
          Op::use_lhs ? X + rid * lhs_dim + lhs_add : nullptr;
411
      const DType* rhs_off =
412
          Op::use_rhs ? W + eid * rhs_dim + rhs_add : nullptr;
413
414
415
416
      const DType val = Op::Call(lhs_off, rhs_off);
#pragma omp critical
      if (Cmp::Call(out_off[k], val)) {
        out_off[k] = val;
417
418
        if (Op::use_lhs) argx_off[k] = rid;
        if (Op::use_rhs) argw_off[k] = eid;
419
420
421
422
423
      }
    }
  }
}

424
/**
425
426
427
428
429
430
 * @brief CPU kernel of Edge_softmax_csr_forward on Csr format.
 * @param bcast Broadcast information.
 * @param csr The Csr matrix.
 * @param ufeat The feature on source nodes.
 * @param efeat The feature on edges.
 * @param out The result of edge_softmax_forward.
431
432
 */
template <typename IdType, typename DType, typename Op>
433
434
435
void Edge_softmax_csr_forward(
    const BcastOff& bcast, const CSRMatrix& csr, NDArray ufeat, NDArray efeat,
    NDArray out) {
436
437
438
  const bool has_idx = !IsNullArray(csr.data);
  const IdType* indptr = static_cast<IdType*>(csr.indptr->data);
  const IdType* edges =
439
      has_idx ? static_cast<IdType*>(csr.data->data) : nullptr;
440
441
442
443
444
  const DType* W = Op::use_rhs ? static_cast<DType*>(efeat->data) : nullptr;
  const int64_t dim = bcast.out_len, rhs_dim = bcast.rhs_len;
  runtime::parallel_for(0, csr.num_rows, [&](size_t b, size_t e) {
    for (auto rid = b; rid < e; ++rid) {
      const IdType row_start = indptr[rid], row_end = indptr[rid + 1];
445
446
      std::vector<DType> data_e(row_end - row_start, 0);
      std::vector<IdType> num(row_end - row_start, 0);
447
448
449
450
451
452
      for (int64_t k = 0; k < dim; ++k) {
        DType max_v = -std::numeric_limits<DType>::infinity();
        for (IdType j = row_start; j < row_end; ++j) {
          const IdType eid = has_idx ? edges[j] : j;
          const int64_t rhs_add = bcast.use_bcast ? bcast.rhs_offset[k] : k;
          const DType* rhs_off =
453
454
455
              Op::use_rhs ? W + eid * rhs_dim + rhs_add : nullptr;
          data_e[j - row_start] = *rhs_off;
          num[j - row_start] = eid * rhs_dim + rhs_add;
456
457
458
459
460
461
462
463
          max_v = std::max<DType>(max_v, (*rhs_off));
        }
        DType exp_sum = 0;
        for (auto& element : data_e) {
          element -= max_v;
          element = std::exp(element);
          exp_sum += element;
        }
464
465
        for (int i = 0; i < row_end - row_start; i++) {
          out.Ptr<DType>()[num[i]] = data_e[i] / exp_sum;
466
467
468
469
470
471
        }
      }
    }
  });
}

472
/**
473
474
475
476
477
478
 * @brief CPU kernel of Edge_softmax_csr_backward on Csr format.
 * @param bcast Broadcast information.
 * @param csr The Csr matrix.
 * @param out The result of forward.
 * @param sds The result of gradiet * out.
 * @param back_out The result of edge_softmax_backward.
479
480
 */
template <typename IdType, typename DType, typename Op>
481
482
483
void Edge_softmax_csr_backward(
    const BcastOff& bcast, const CSRMatrix& csr, NDArray out, NDArray sds,
    NDArray back_out) {
484
485
486
  const bool has_idx = !IsNullArray(csr.data);
  const IdType* indptr = static_cast<IdType*>(csr.indptr->data);
  const IdType* edges =
487
      has_idx ? static_cast<IdType*>(csr.data->data) : nullptr;
488
489
490
491
492
493
494
495
496
497
498
499
  const DType* W_out = Op::use_rhs ? static_cast<DType*>(out->data) : nullptr;
  const DType* W_sds = Op::use_rhs ? static_cast<DType*>(sds->data) : nullptr;
  const int64_t dim = bcast.out_len, rhs_dim = bcast.rhs_len;
  runtime::parallel_for(0, csr.num_rows, [&](size_t b, size_t e) {
    for (auto rid = b; rid < e; ++rid) {
      const IdType row_start = indptr[rid], row_end = indptr[rid + 1];
      for (int64_t k = 0; k < dim; ++k) {
        DType sum_sds = 0;
        for (IdType j = row_start; j < row_end; ++j) {
          const IdType eid = has_idx ? edges[j] : j;
          const int64_t rhs_add = bcast.use_bcast ? bcast.rhs_offset[k] : k;
          const DType* rhs_off_sds =
500
              Op::use_rhs ? W_sds + eid * rhs_dim + rhs_add : nullptr;
501
502
          sum_sds += (*rhs_off_sds);
        }
503
        for (IdType j = row_start; j < row_end; ++j) {
504
505
506
          const IdType eid = has_idx ? edges[j] : j;
          const int64_t rhs_add = bcast.use_bcast ? bcast.rhs_offset[k] : k;
          const DType* rhs_off_out =
507
              Op::use_rhs ? W_out + eid * rhs_dim + rhs_add : nullptr;
508
          const DType* rhs_off_sds =
509
510
511
              Op::use_rhs ? W_sds + eid * rhs_dim + rhs_add : nullptr;
          back_out.Ptr<DType>()[eid * rhs_dim + rhs_add] =
              (*rhs_off_sds) - sum_sds * (*rhs_off_out);
512
513
514
515
516
517
        }
      }
    }
  });
}

518
519
520
521
522
}  // namespace cpu
}  // namespace aten
}  // namespace dgl

#endif  // DGL_ARRAY_CPU_SPMM_H_