test_rowwise.cc 32.7 KB
Newer Older
1
#include <dgl/array.h>
2
3
#include <gtest/gtest.h>

4
#include <set>
5
6
#include <tuple>

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
#include "./common.h"

using namespace dgl;
using namespace dgl::runtime;
using namespace dgl::aten;

template <typename Idx>
using ETuple = std::tuple<Idx, Idx, Idx>;

template <typename Idx>
std::set<ETuple<Idx>> AllEdgeSet(bool has_data) {
  if (has_data) {
    std::set<ETuple<Idx>> eset;
    eset.insert(ETuple<Idx>{0, 0, 2});
    eset.insert(ETuple<Idx>{0, 1, 3});
    eset.insert(ETuple<Idx>{1, 1, 0});
    eset.insert(ETuple<Idx>{3, 2, 1});
    eset.insert(ETuple<Idx>{3, 3, 4});
    return eset;
  } else {
    std::set<ETuple<Idx>> eset;
    eset.insert(ETuple<Idx>{0, 0, 0});
    eset.insert(ETuple<Idx>{0, 1, 1});
    eset.insert(ETuple<Idx>{1, 1, 2});
    eset.insert(ETuple<Idx>{3, 2, 3});
    eset.insert(ETuple<Idx>{3, 3, 4});
    return eset;
  }
}

37
38
39
40
template <typename Idx>
std::set<ETuple<Idx>> AllEdgePerEtypeSet(bool has_data) {
  if (has_data) {
    std::set<ETuple<Idx>> eset;
41
42
43
    eset.insert(ETuple<Idx>{0, 0, 0});
    eset.insert(ETuple<Idx>{0, 1, 1});
    eset.insert(ETuple<Idx>{0, 2, 4});
44
    eset.insert(ETuple<Idx>{0, 3, 6});
45
46
    eset.insert(ETuple<Idx>{3, 2, 5});
    eset.insert(ETuple<Idx>{3, 3, 3});
47
48
49
50
51
52
53
    return eset;
  } else {
    std::set<ETuple<Idx>> eset;
    eset.insert(ETuple<Idx>{0, 0, 0});
    eset.insert(ETuple<Idx>{0, 1, 1});
    eset.insert(ETuple<Idx>{0, 2, 2});
    eset.insert(ETuple<Idx>{0, 3, 3});
54
55
    eset.insert(ETuple<Idx>{3, 3, 5});
    eset.insert(ETuple<Idx>{3, 2, 6});
56
57
58
59
    return eset;
  }
}

60
61
62
63
64
65
66
template <typename Idx>
std::set<ETuple<Idx>> ToEdgeSet(COOMatrix mat) {
  std::set<ETuple<Idx>> eset;
  Idx* row = static_cast<Idx*>(mat.row->data);
  Idx* col = static_cast<Idx*>(mat.col->data);
  Idx* data = static_cast<Idx*>(mat.data->data);
  for (int64_t i = 0; i < mat.row->shape[0]; ++i) {
67
    // std::cout << row[i] << " " << col[i] <<  " " << data[i] << std::endl;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    eset.emplace(row[i], col[i], data[i]);
  }
  return eset;
}

template <typename Idx>
void CheckSampledResult(COOMatrix mat, IdArray rows, bool has_data) {
  ASSERT_EQ(mat.num_rows, 4);
  ASSERT_EQ(mat.num_cols, 4);
  Idx* row = static_cast<Idx*>(mat.row->data);
  Idx* col = static_cast<Idx*>(mat.col->data);
  Idx* data = static_cast<Idx*>(mat.data->data);
  const auto& gt = AllEdgeSet<Idx>(has_data);
  for (int64_t i = 0; i < mat.row->shape[0]; ++i) {
    ASSERT_TRUE(gt.count(std::make_tuple(row[i], col[i], data[i])));
    ASSERT_TRUE(IsInArray(rows, row[i]));
  }
}

87
88
89
90
91
92
93
94
95
template <typename Idx>
void CheckSampledPerEtypeResult(COOMatrix mat, IdArray rows, bool has_data) {
  ASSERT_EQ(mat.num_rows, 4);
  ASSERT_EQ(mat.num_cols, 4);
  Idx* row = static_cast<Idx*>(mat.row->data);
  Idx* col = static_cast<Idx*>(mat.col->data);
  Idx* data = static_cast<Idx*>(mat.data->data);
  const auto& gt = AllEdgePerEtypeSet<Idx>(has_data);
  for (int64_t i = 0; i < mat.row->shape[0]; ++i) {
96
97
    int64_t count = gt.count(std::make_tuple(row[i], col[i], data[i]));
    ASSERT_TRUE(count);
98
99
100
101
    ASSERT_TRUE(IsInArray(rows, row[i]));
  }
}

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
template <typename Idx>
CSRMatrix CSR(bool has_data) {
  IdArray indptr = NDArray::FromVector(std::vector<Idx>({0, 2, 3, 3, 5}));
  IdArray indices = NDArray::FromVector(std::vector<Idx>({0, 1, 1, 2, 3}));
  IdArray data = NDArray::FromVector(std::vector<Idx>({2, 3, 0, 1, 4}));
  if (has_data)
    return CSRMatrix(4, 4, indptr, indices, data);
  else
    return CSRMatrix(4, 4, indptr, indices);
}

template <typename Idx>
COOMatrix COO(bool has_data) {
  IdArray row = NDArray::FromVector(std::vector<Idx>({0, 0, 1, 3, 3}));
  IdArray col = NDArray::FromVector(std::vector<Idx>({0, 1, 1, 2, 3}));
  IdArray data = NDArray::FromVector(std::vector<Idx>({2, 3, 0, 1, 4}));
  if (has_data)
    return COOMatrix(4, 4, row, col, data);
  else
    return COOMatrix(4, 4, row, col);
}

124
template <typename Idx>
125
std::pair<CSRMatrix, std::vector<int64_t>> CSREtypes(bool has_data) {
126
  IdArray indptr = NDArray::FromVector(std::vector<Idx>({0, 4, 5, 5, 7}));
127
128
  IdArray indices =
      NDArray::FromVector(std::vector<Idx>({0, 1, 2, 3, 1, 3, 2}));
129
130
  IdArray data = NDArray::FromVector(std::vector<Idx>({0, 1, 4, 6, 2, 3, 5}));
  auto eid2etype_offsets = std::vector<int64_t>({0, 4, 5, 6, 7});
131
  if (has_data)
132
    return {CSRMatrix(4, 4, indptr, indices, data), eid2etype_offsets};
133
  else
134
    return {CSRMatrix(4, 4, indptr, indices), eid2etype_offsets};
135
136
137
}

template <typename Idx>
138
std::pair<COOMatrix, std::vector<int64_t>> COOEtypes(bool has_data) {
139
  IdArray row = NDArray::FromVector(std::vector<Idx>({0, 0, 0, 0, 1, 3, 3}));
140
141
142
  IdArray col = NDArray::FromVector(std::vector<Idx>({0, 1, 2, 3, 1, 3, 2}));
  IdArray data = NDArray::FromVector(std::vector<Idx>({0, 1, 4, 6, 2, 3, 5}));
  auto eid2etype_offsets = std::vector<int64_t>({0, 4, 5, 6, 7});
143
  if (has_data)
144
    return {COOMatrix(4, 4, row, col, data), eid2etype_offsets};
145
  else
146
    return {COOMatrix(4, 4, row, col), eid2etype_offsets};
147
148
}

149
150
151
template <typename Idx, typename FloatType>
void _TestCSRSampling(bool has_data) {
  auto mat = CSR<Idx>(has_data);
152
153
  FloatArray prob =
      NDArray::FromVector(std::vector<FloatType>({.5, .5, .5, .5, .5}));
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
    auto rst = CSRRowWiseSampling(mat, rows, 2, prob, true);
    CheckSampledResult<Idx>(rst, rows, has_data);
  }
  for (int k = 0; k < 10; ++k) {
    auto rst = CSRRowWiseSampling(mat, rows, 2, prob, false);
    CheckSampledResult<Idx>(rst, rows, has_data);
    auto eset = ToEdgeSet<Idx>(rst);
    ASSERT_EQ(eset.size(), 4);
    if (has_data) {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 0, 2)));
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 3)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 2, 1)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    } else {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 1)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 2, 3)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    }
  }
176
  prob = NDArray::FromVector(std::vector<FloatType>({.0, .5, .5, .0, .5}));
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
  for (int k = 0; k < 100; ++k) {
    auto rst = CSRRowWiseSampling(mat, rows, 2, prob, true);
    CheckSampledResult<Idx>(rst, rows, has_data);
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
      ASSERT_FALSE(eset.count(std::make_tuple(0, 1, 3)));
    } else {
      ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_FALSE(eset.count(std::make_tuple(3, 2, 3)));
    }
  }
}

TEST(RowwiseTest, TestCSRSampling) {
  _TestCSRSampling<int32_t, float>(true);
  _TestCSRSampling<int64_t, float>(true);
  _TestCSRSampling<int32_t, double>(true);
  _TestCSRSampling<int64_t, double>(true);
  _TestCSRSampling<int32_t, float>(false);
  _TestCSRSampling<int64_t, float>(false);
  _TestCSRSampling<int32_t, double>(false);
  _TestCSRSampling<int64_t, double>(false);
}

template <typename Idx, typename FloatType>
void _TestCSRSamplingUniform(bool has_data) {
  auto mat = CSR<Idx>(has_data);
204
  FloatArray prob = aten::NullArray();
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
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
    auto rst = CSRRowWiseSampling(mat, rows, 2, prob, true);
    CheckSampledResult<Idx>(rst, rows, has_data);
  }
  for (int k = 0; k < 10; ++k) {
    auto rst = CSRRowWiseSampling(mat, rows, 2, prob, false);
    CheckSampledResult<Idx>(rst, rows, has_data);
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 0, 2)));
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 3)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 2, 1)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    } else {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 1)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 2, 3)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    }
  }
}

TEST(RowwiseTest, TestCSRSamplingUniform) {
  _TestCSRSamplingUniform<int32_t, float>(true);
  _TestCSRSamplingUniform<int64_t, float>(true);
  _TestCSRSamplingUniform<int32_t, double>(true);
  _TestCSRSamplingUniform<int64_t, double>(true);
  _TestCSRSamplingUniform<int32_t, float>(false);
  _TestCSRSamplingUniform<int64_t, float>(false);
  _TestCSRSamplingUniform<int32_t, double>(false);
  _TestCSRSamplingUniform<int64_t, double>(false);
}

239
240
template <typename Idx, typename FloatType>
void _TestCSRPerEtypeSampling(bool has_data) {
241
242
243
244
  auto pair = CSREtypes<Idx>(has_data);
  auto mat = pair.first;
  auto eid2etype_offset = pair.second;
  std::vector<FloatArray> prob = {
245
246
247
248
      NDArray::FromVector(std::vector<FloatType>({.5, .5, .5, .5})),
      NDArray::FromVector(std::vector<FloatType>({.5})),
      NDArray::FromVector(std::vector<FloatType>({.5})),
      NDArray::FromVector(std::vector<FloatType>({.5}))};
249
250
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
251
252
    auto rst = CSRRowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true);
253
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
254
255
  }
  for (int k = 0; k < 10; ++k) {
256
257
    auto rst = CSRRowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, false);
258
259
260
261
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
      int counts = 0;
262
263
      counts += eset.count(std::make_tuple(0, 0, 0));
      counts += eset.count(std::make_tuple(0, 1, 1));
264
265
      ASSERT_EQ(counts, 2);
      counts = 0;
266
267
268
      counts += eset.count(std::make_tuple(0, 2, 4));
      ASSERT_EQ(counts, 1);
      counts = 0;
269
270
271
      counts += eset.count(std::make_tuple(0, 3, 6));
      ASSERT_EQ(counts, 1);
      counts = 0;
272
      counts += eset.count(std::make_tuple(1, 1, 2));
273
274
      ASSERT_EQ(counts, 0);
      counts = 0;
275
      counts += eset.count(std::make_tuple(3, 2, 5));
276
277
      ASSERT_EQ(counts, 1);
      counts = 0;
278
      counts += eset.count(std::make_tuple(3, 3, 3));
279
280
281
282
283
284
285
      ASSERT_EQ(counts, 1);
    } else {
      int counts = 0;
      counts += eset.count(std::make_tuple(0, 0, 0));
      counts += eset.count(std::make_tuple(0, 1, 1));
      counts += eset.count(std::make_tuple(0, 2, 2));
      counts += eset.count(std::make_tuple(0, 3, 3));
286
      ASSERT_EQ(counts, 2);
287
288
289
290
      counts = 0;
      counts += eset.count(std::make_tuple(1, 1, 4));
      ASSERT_EQ(counts, 0);
      counts = 0;
291
      counts += eset.count(std::make_tuple(3, 3, 5));
292
293
      ASSERT_EQ(counts, 1);
      counts = 0;
294
      counts += eset.count(std::make_tuple(3, 2, 6));
295
296
297
298
      ASSERT_EQ(counts, 1);
    }
  }

299
  prob = {
300
301
302
303
      NDArray::FromVector(std::vector<FloatType>({.0, .5, .0, .0})),
      NDArray::FromVector(std::vector<FloatType>({.5})),
      NDArray::FromVector(std::vector<FloatType>({.5})),
      NDArray::FromVector(std::vector<FloatType>({.5}))};
304
  for (int k = 0; k < 10; ++k) {
305
306
    auto rst = CSRRowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true);
307
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
308
309
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
310
      ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
311
312
313
    } else {
      ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_FALSE(eset.count(std::make_tuple(0, 2, 2)));
314
      ASSERT_FALSE(eset.count(std::make_tuple(0, 3, 3)));
315
316
317
318
    }
  }
}

319
template <typename Idx, typename FloatType>
320
321
322
323
324
void _TestCSRPerEtypeSamplingSorted() {
  auto pair = CSREtypes<Idx>(true);
  auto mat = pair.first;
  auto eid2etype_offset = pair.second;
  std::vector<FloatArray> prob = {
325
326
327
328
      NDArray::FromVector(std::vector<FloatType>({.5, .5, .5, .5})),
      NDArray::FromVector(std::vector<FloatType>({.5})),
      NDArray::FromVector(std::vector<FloatType>({.5})),
      NDArray::FromVector(std::vector<FloatType>({.5}))};
329
330
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
331
332
    auto rst = CSRRowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true, true);
333
    CheckSampledPerEtypeResult<Idx>(rst, rows, true);
334
335
  }
  for (int k = 0; k < 10; ++k) {
336
337
    auto rst = CSRRowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, false, true);
338
    CheckSampledPerEtypeResult<Idx>(rst, rows, true);
339
    auto eset = ToEdgeSet<Idx>(rst);
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
    int counts = 0;
    counts += eset.count(std::make_tuple(0, 0, 0));
    counts += eset.count(std::make_tuple(0, 1, 1));
    ASSERT_EQ(counts, 2);
    counts = 0;
    counts += eset.count(std::make_tuple(0, 2, 4));
    ASSERT_EQ(counts, 1);
    counts = 0;
    counts += eset.count(std::make_tuple(0, 3, 6));
    ASSERT_EQ(counts, 1);
    counts = 0;
    counts += eset.count(std::make_tuple(1, 1, 2));
    ASSERT_EQ(counts, 0);
    counts = 0;
    counts += eset.count(std::make_tuple(3, 2, 5));
    ASSERT_EQ(counts, 1);
    counts = 0;
    counts += eset.count(std::make_tuple(3, 3, 3));
    ASSERT_EQ(counts, 1);
  }

  prob = {
362
363
364
365
      NDArray::FromVector(std::vector<FloatType>({.0, .5, .0, .0})),
      NDArray::FromVector(std::vector<FloatType>({.5})),
      NDArray::FromVector(std::vector<FloatType>({.5})),
      NDArray::FromVector(std::vector<FloatType>({.5}))};
366
  for (int k = 0; k < 10; ++k) {
367
368
    auto rst = CSRRowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true, true);
369
    CheckSampledPerEtypeResult<Idx>(rst, rows, true);
370
    auto eset = ToEdgeSet<Idx>(rst);
371
    ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
372
373
374
  }
}

375
376
377
378
379
380
381
382
383
TEST(RowwiseTest, TestCSRPerEtypeSampling) {
  _TestCSRPerEtypeSampling<int32_t, float>(true);
  _TestCSRPerEtypeSampling<int64_t, float>(true);
  _TestCSRPerEtypeSampling<int32_t, double>(true);
  _TestCSRPerEtypeSampling<int64_t, double>(true);
  _TestCSRPerEtypeSampling<int32_t, float>(false);
  _TestCSRPerEtypeSampling<int64_t, float>(false);
  _TestCSRPerEtypeSampling<int32_t, double>(false);
  _TestCSRPerEtypeSampling<int64_t, double>(false);
384
385
386
387
  _TestCSRPerEtypeSamplingSorted<int32_t, float>();
  _TestCSRPerEtypeSamplingSorted<int64_t, float>();
  _TestCSRPerEtypeSamplingSorted<int32_t, double>();
  _TestCSRPerEtypeSamplingSorted<int64_t, double>();
388
389
390
391
}

template <typename Idx, typename FloatType>
void _TestCSRPerEtypeSamplingUniform(bool has_data) {
392
393
394
395
  auto pair = CSREtypes<Idx>(has_data);
  auto mat = pair.first;
  auto eid2etype_offset = pair.second;
  std::vector<FloatArray> prob = {
396
397
      aten::NullArray(), aten::NullArray(), aten::NullArray(),
      aten::NullArray()};
398
399
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
400
401
    auto rst = CSRRowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true);
402
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
403
404
  }
  for (int k = 0; k < 10; ++k) {
405
406
    auto rst = CSRRowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, false);
407
408
409
410
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
      int counts = 0;
411
412
      counts += eset.count(std::make_tuple(0, 0, 0));
      counts += eset.count(std::make_tuple(0, 1, 1));
413
414
      ASSERT_EQ(counts, 2);
      counts = 0;
415
416
417
      counts += eset.count(std::make_tuple(0, 2, 4));
      ASSERT_EQ(counts, 1);
      counts = 0;
418
419
420
      counts += eset.count(std::make_tuple(0, 3, 6));
      ASSERT_EQ(counts, 1);
      counts = 0;
421
      counts += eset.count(std::make_tuple(1, 1, 2));
422
423
      ASSERT_EQ(counts, 0);
      counts = 0;
424
      counts += eset.count(std::make_tuple(3, 2, 5));
425
426
      ASSERT_EQ(counts, 1);
      counts = 0;
427
      counts += eset.count(std::make_tuple(3, 3, 3));
428
429
430
431
432
433
434
      ASSERT_EQ(counts, 1);
    } else {
      int counts = 0;
      counts += eset.count(std::make_tuple(0, 0, 0));
      counts += eset.count(std::make_tuple(0, 1, 1));
      counts += eset.count(std::make_tuple(0, 2, 2));
      counts += eset.count(std::make_tuple(0, 3, 3));
435
      ASSERT_EQ(counts, 2);
436
437
438
439
      counts = 0;
      counts += eset.count(std::make_tuple(1, 1, 4));
      ASSERT_EQ(counts, 0);
      counts = 0;
440
      counts += eset.count(std::make_tuple(3, 3, 5));
441
442
      ASSERT_EQ(counts, 1);
      counts = 0;
443
      counts += eset.count(std::make_tuple(3, 2, 6));
444
445
446
447
448
      ASSERT_EQ(counts, 1);
    }
  }
}

449
template <typename Idx, typename FloatType>
450
451
452
453
454
void _TestCSRPerEtypeSamplingUniformSorted() {
  auto pair = CSREtypes<Idx>(true);
  auto mat = pair.first;
  auto eid2etype_offset = pair.second;
  std::vector<FloatArray> prob = {
455
456
      aten::NullArray(), aten::NullArray(), aten::NullArray(),
      aten::NullArray()};
457
458
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
459
460
    auto rst = CSRRowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true, true);
461
    CheckSampledPerEtypeResult<Idx>(rst, rows, true);
462
463
  }
  for (int k = 0; k < 10; ++k) {
464
465
    auto rst = CSRRowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, false, true);
466
    CheckSampledPerEtypeResult<Idx>(rst, rows, true);
467
    auto eset = ToEdgeSet<Idx>(rst);
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
    int counts = 0;
    counts += eset.count(std::make_tuple(0, 0, 0));
    counts += eset.count(std::make_tuple(0, 1, 1));
    ASSERT_EQ(counts, 2);
    counts = 0;
    counts += eset.count(std::make_tuple(0, 2, 4));
    ASSERT_EQ(counts, 1);
    counts = 0;
    counts += eset.count(std::make_tuple(0, 3, 6));
    ASSERT_EQ(counts, 1);
    counts = 0;
    counts += eset.count(std::make_tuple(1, 1, 2));
    ASSERT_EQ(counts, 0);
    counts = 0;
    counts += eset.count(std::make_tuple(3, 2, 5));
    ASSERT_EQ(counts, 1);
    counts = 0;
    counts += eset.count(std::make_tuple(3, 3, 3));
    ASSERT_EQ(counts, 1);
487
488
489
  }
}

490
491
492
493
494
495
496
497
498
TEST(RowwiseTest, TestCSRPerEtypeSamplingUniform) {
  _TestCSRPerEtypeSamplingUniform<int32_t, float>(true);
  _TestCSRPerEtypeSamplingUniform<int64_t, float>(true);
  _TestCSRPerEtypeSamplingUniform<int32_t, double>(true);
  _TestCSRPerEtypeSamplingUniform<int64_t, double>(true);
  _TestCSRPerEtypeSamplingUniform<int32_t, float>(false);
  _TestCSRPerEtypeSamplingUniform<int64_t, float>(false);
  _TestCSRPerEtypeSamplingUniform<int32_t, double>(false);
  _TestCSRPerEtypeSamplingUniform<int64_t, double>(false);
499
500
501
502
  _TestCSRPerEtypeSamplingUniformSorted<int32_t, float>();
  _TestCSRPerEtypeSamplingUniformSorted<int64_t, float>();
  _TestCSRPerEtypeSamplingUniformSorted<int32_t, double>();
  _TestCSRPerEtypeSamplingUniformSorted<int64_t, double>();
503
504
}

505
506
507
template <typename Idx, typename FloatType>
void _TestCOOSampling(bool has_data) {
  auto mat = COO<Idx>(has_data);
508
509
  FloatArray prob =
      NDArray::FromVector(std::vector<FloatType>({.5, .5, .5, .5, .5}));
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
    auto rst = COORowWiseSampling(mat, rows, 2, prob, true);
    CheckSampledResult<Idx>(rst, rows, has_data);
  }
  for (int k = 0; k < 10; ++k) {
    auto rst = COORowWiseSampling(mat, rows, 2, prob, false);
    CheckSampledResult<Idx>(rst, rows, has_data);
    auto eset = ToEdgeSet<Idx>(rst);
    ASSERT_EQ(eset.size(), 4);
    if (has_data) {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 0, 2)));
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 3)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 2, 1)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    } else {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 1)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 2, 3)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    }
  }
532
  prob = NDArray::FromVector(std::vector<FloatType>({.0, .5, .5, .0, .5}));
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
  for (int k = 0; k < 100; ++k) {
    auto rst = COORowWiseSampling(mat, rows, 2, prob, true);
    CheckSampledResult<Idx>(rst, rows, has_data);
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
      ASSERT_FALSE(eset.count(std::make_tuple(0, 1, 3)));
    } else {
      ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_FALSE(eset.count(std::make_tuple(3, 2, 3)));
    }
  }
}

TEST(RowwiseTest, TestCOOSampling) {
  _TestCOOSampling<int32_t, float>(true);
  _TestCOOSampling<int64_t, float>(true);
  _TestCOOSampling<int32_t, double>(true);
  _TestCOOSampling<int64_t, double>(true);
  _TestCOOSampling<int32_t, float>(false);
  _TestCOOSampling<int64_t, float>(false);
  _TestCOOSampling<int32_t, double>(false);
  _TestCOOSampling<int64_t, double>(false);
}

template <typename Idx, typename FloatType>
void _TestCOOSamplingUniform(bool has_data) {
  auto mat = COO<Idx>(has_data);
560
  FloatArray prob = aten::NullArray();
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
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
    auto rst = COORowWiseSampling(mat, rows, 2, prob, true);
    CheckSampledResult<Idx>(rst, rows, has_data);
  }
  for (int k = 0; k < 10; ++k) {
    auto rst = COORowWiseSampling(mat, rows, 2, prob, false);
    CheckSampledResult<Idx>(rst, rows, has_data);
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 0, 2)));
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 3)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 2, 1)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    } else {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 1)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 2, 3)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    }
  }
}

TEST(RowwiseTest, TestCOOSamplingUniform) {
  _TestCOOSamplingUniform<int32_t, float>(true);
  _TestCOOSamplingUniform<int64_t, float>(true);
  _TestCOOSamplingUniform<int32_t, double>(true);
  _TestCOOSamplingUniform<int64_t, double>(true);
  _TestCOOSamplingUniform<int32_t, float>(false);
  _TestCOOSamplingUniform<int64_t, float>(false);
  _TestCOOSamplingUniform<int32_t, double>(false);
  _TestCOOSamplingUniform<int64_t, double>(false);
}

595
596
597
// COOPerEtypeSampling with rowwise_etype_sorted == true is not meaningful as
// it's never used in practice.

598
template <typename Idx, typename FloatType>
599
600
601
602
603
void _TestCOOPerEtypeSampling(bool has_data) {
  auto pair = COOEtypes<Idx>(has_data);
  auto mat = pair.first;
  auto eid2etype_offset = pair.second;
  std::vector<FloatArray> prob = {
604
605
606
607
      NDArray::FromVector(std::vector<FloatType>({.5, .5, .5, .5})),
      NDArray::FromVector(std::vector<FloatType>({.5})),
      NDArray::FromVector(std::vector<FloatType>({.5})),
      NDArray::FromVector(std::vector<FloatType>({.5}))};
608
609
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
610
611
    auto rst = COORowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true);
612
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
613
614
  }
  for (int k = 0; k < 10; ++k) {
615
616
    auto rst = COORowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, false);
617
618
619
620
621
622
623
624
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
      int counts = 0;
      counts += eset.count(std::make_tuple(0, 0, 0));
      counts += eset.count(std::make_tuple(0, 1, 1));
      ASSERT_EQ(counts, 2);
      counts = 0;
625
      counts += eset.count(std::make_tuple(0, 2, 4));
626
627
      ASSERT_EQ(counts, 1);
      counts = 0;
628
629
630
      counts += eset.count(std::make_tuple(0, 3, 6));
      ASSERT_EQ(counts, 1);
      counts = 0;
631
      counts += eset.count(std::make_tuple(1, 1, 2));
632
633
      ASSERT_EQ(counts, 0);
      counts = 0;
634
      counts += eset.count(std::make_tuple(3, 2, 5));
635
636
      ASSERT_EQ(counts, 1);
      counts = 0;
637
      counts += eset.count(std::make_tuple(3, 3, 3));
638
639
640
641
642
643
644
      ASSERT_EQ(counts, 1);
    } else {
      int counts = 0;
      counts += eset.count(std::make_tuple(0, 0, 0));
      counts += eset.count(std::make_tuple(0, 1, 1));
      counts += eset.count(std::make_tuple(0, 2, 2));
      counts += eset.count(std::make_tuple(0, 3, 3));
645
      ASSERT_EQ(counts, 2);
646
647
648
649
      counts = 0;
      counts += eset.count(std::make_tuple(1, 1, 4));
      ASSERT_EQ(counts, 0);
      counts = 0;
650
      counts += eset.count(std::make_tuple(3, 3, 5));
651
652
      ASSERT_EQ(counts, 1);
      counts = 0;
653
      counts += eset.count(std::make_tuple(3, 2, 6));
654
655
656
657
      ASSERT_EQ(counts, 1);
    }
  }

658
  prob = {
659
660
661
662
      NDArray::FromVector(std::vector<FloatType>({.0, .5, .0, .0})),
      NDArray::FromVector(std::vector<FloatType>({.5})),
      NDArray::FromVector(std::vector<FloatType>({.5})),
      NDArray::FromVector(std::vector<FloatType>({.5}))};
663
  for (int k = 0; k < 10; ++k) {
664
665
    auto rst = COORowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true);
666
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
667
668
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
669
      ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
670
671
672
    } else {
      ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_FALSE(eset.count(std::make_tuple(0, 2, 2)));
673
      ASSERT_FALSE(eset.count(std::make_tuple(0, 3, 3)));
674
675
676
677
    }
  }
}

678
679
680
681
682
683
684
685
686
TEST(RowwiseTest, TestCOOPerEtypeSampling) {
  _TestCOOPerEtypeSampling<int32_t, float>(true);
  _TestCOOPerEtypeSampling<int64_t, float>(true);
  _TestCOOPerEtypeSampling<int32_t, double>(true);
  _TestCOOPerEtypeSampling<int64_t, double>(true);
  _TestCOOPerEtypeSampling<int32_t, float>(false);
  _TestCOOPerEtypeSampling<int64_t, float>(false);
  _TestCOOPerEtypeSampling<int32_t, double>(false);
  _TestCOOPerEtypeSampling<int64_t, double>(false);
687
688
689
690
}

template <typename Idx, typename FloatType>
void _TestCOOPerEtypeSamplingUniform(bool has_data) {
691
692
693
694
  auto pair = COOEtypes<Idx>(has_data);
  auto mat = pair.first;
  auto eid2etype_offset = pair.second;
  std::vector<FloatArray> prob = {
695
696
      aten::NullArray(), aten::NullArray(), aten::NullArray(),
      aten::NullArray()};
697
698
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
699
700
    auto rst = COORowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true);
701
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
702
703
  }
  for (int k = 0; k < 10; ++k) {
704
705
    auto rst = COORowWisePerEtypeSampling(
        mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, false);
706
707
708
709
710
711
712
713
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
      int counts = 0;
      counts += eset.count(std::make_tuple(0, 0, 0));
      counts += eset.count(std::make_tuple(0, 1, 1));
      ASSERT_EQ(counts, 2);
      counts = 0;
714
      counts += eset.count(std::make_tuple(0, 2, 4));
715
716
      ASSERT_EQ(counts, 1);
      counts = 0;
717
718
719
      counts += eset.count(std::make_tuple(0, 3, 6));
      ASSERT_EQ(counts, 1);
      counts = 0;
720
      counts += eset.count(std::make_tuple(1, 1, 2));
721
722
      ASSERT_EQ(counts, 0);
      counts = 0;
723
      counts += eset.count(std::make_tuple(3, 2, 5));
724
725
      ASSERT_EQ(counts, 1);
      counts = 0;
726
      counts += eset.count(std::make_tuple(3, 3, 3));
727
728
729
730
731
732
733
      ASSERT_EQ(counts, 1);
    } else {
      int counts = 0;
      counts += eset.count(std::make_tuple(0, 0, 0));
      counts += eset.count(std::make_tuple(0, 1, 1));
      counts += eset.count(std::make_tuple(0, 2, 2));
      counts += eset.count(std::make_tuple(0, 3, 3));
734
      ASSERT_EQ(counts, 2);
735
736
737
738
      counts = 0;
      counts += eset.count(std::make_tuple(1, 1, 4));
      ASSERT_EQ(counts, 0);
      counts = 0;
739
      counts += eset.count(std::make_tuple(3, 3, 5));
740
741
      ASSERT_EQ(counts, 1);
      counts = 0;
742
      counts += eset.count(std::make_tuple(3, 2, 6));
743
744
745
746
747
      ASSERT_EQ(counts, 1);
    }
  }
}

748
749
750
751
752
753
754
755
756
757
758
TEST(RowwiseTest, TestCOOPerEtypeSamplingUniform) {
  _TestCOOPerEtypeSamplingUniform<int32_t, float>(true);
  _TestCOOPerEtypeSamplingUniform<int64_t, float>(true);
  _TestCOOPerEtypeSamplingUniform<int32_t, double>(true);
  _TestCOOPerEtypeSamplingUniform<int64_t, double>(true);
  _TestCOOPerEtypeSamplingUniform<int32_t, float>(false);
  _TestCOOPerEtypeSamplingUniform<int64_t, float>(false);
  _TestCOOPerEtypeSamplingUniform<int32_t, double>(false);
  _TestCOOPerEtypeSamplingUniform<int64_t, double>(false);
}

759
760
761
template <typename Idx, typename FloatType>
void _TestCSRTopk(bool has_data) {
  auto mat = CSR<Idx>(has_data);
762
763
  FloatArray weight =
      NDArray::FromVector(std::vector<FloatType>({.1f, .0f, -.1f, .2f, .5f}));
764
765
766
767
  // -.1, .2, .1, .0, .5
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));

  {
768
769
770
771
772
773
774
775
776
777
    auto rst = CSRRowWiseTopk(mat, rows, 1, weight, true);
    auto eset = ToEdgeSet<Idx>(rst);
    ASSERT_EQ(eset.size(), 2);
    if (has_data) {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 0, 2)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 2, 1)));
    } else {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 1)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 2, 3)));
    }
778
779
780
  }

  {
781
782
783
784
785
786
787
788
789
790
    auto rst = CSRRowWiseTopk(mat, rows, 1, weight, false);
    auto eset = ToEdgeSet<Idx>(rst);
    ASSERT_EQ(eset.size(), 2);
    if (has_data) {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 3)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    } else {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    }
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
  }
}

TEST(RowwiseTest, TestCSRTopk) {
  _TestCSRTopk<int32_t, float>(true);
  _TestCSRTopk<int64_t, float>(true);
  _TestCSRTopk<int32_t, double>(true);
  _TestCSRTopk<int64_t, double>(true);
  _TestCSRTopk<int32_t, float>(false);
  _TestCSRTopk<int64_t, float>(false);
  _TestCSRTopk<int32_t, double>(false);
  _TestCSRTopk<int64_t, double>(false);
}

template <typename Idx, typename FloatType>
void _TestCOOTopk(bool has_data) {
  auto mat = COO<Idx>(has_data);
808
809
  FloatArray weight =
      NDArray::FromVector(std::vector<FloatType>({.1f, .0f, -.1f, .2f, .5f}));
810
811
812
813
  // -.1, .2, .1, .0, .5
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));

  {
814
815
816
817
818
819
820
821
822
823
    auto rst = COORowWiseTopk(mat, rows, 1, weight, true);
    auto eset = ToEdgeSet<Idx>(rst);
    ASSERT_EQ(eset.size(), 2);
    if (has_data) {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 0, 2)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 2, 1)));
    } else {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 1)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 2, 3)));
    }
824
825
826
  }

  {
827
828
829
830
831
832
833
834
835
836
    auto rst = COORowWiseTopk(mat, rows, 1, weight, false);
    auto eset = ToEdgeSet<Idx>(rst);
    ASSERT_EQ(eset.size(), 2);
    if (has_data) {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 3)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    } else {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    }
837
838
839
840
841
842
843
844
845
846
847
848
849
  }
}

TEST(RowwiseTest, TestCOOTopk) {
  _TestCOOTopk<int32_t, float>(true);
  _TestCOOTopk<int64_t, float>(true);
  _TestCOOTopk<int32_t, double>(true);
  _TestCOOTopk<int64_t, double>(true);
  _TestCOOTopk<int32_t, float>(false);
  _TestCOOTopk<int64_t, float>(false);
  _TestCOOTopk<int32_t, double>(false);
  _TestCOOTopk<int64_t, double>(false);
}
850
851
852
853
854
855
856
857

template <typename Idx, typename FloatType>
void _TestCSRSamplingBiased(bool has_data) {
  auto mat = CSR<Idx>(has_data);
  // 0 - 0,1
  // 1 - 1
  // 3 - 2,3
  NDArray tag_offset = NDArray::FromVector(
858
      std::vector<Idx>({0, 1, 2, 0, 0, 1, 0, 0, 0, 0, 1, 2}));
859
860
  tag_offset = tag_offset.CreateView({4, 3}, tag_offset->dtype);
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 1, 3}));
861
862
  FloatArray bias = NDArray::FromVector(std::vector<FloatType>({0, 0.5}));
  for (int k = 0; k < 10; ++k) {
863
864
865
866
867
868
869
870
871
872
873
874
875
    auto rst = CSRRowWiseSamplingBiased(mat, rows, 1, tag_offset, bias, false);
    CheckSampledResult<Idx>(rst, rows, has_data);
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 3)));
      ASSERT_TRUE(eset.count(std::make_tuple(1, 1, 0)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    } else {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 1)));
      ASSERT_TRUE(eset.count(std::make_tuple(1, 1, 2)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
    }
  }
876
  for (int k = 0; k < 10; ++k) {
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
    auto rst = CSRRowWiseSamplingBiased(mat, rows, 3, tag_offset, bias, true);
    CheckSampledResult<Idx>(rst, rows, has_data);
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 3)));
      ASSERT_TRUE(eset.count(std::make_tuple(1, 1, 0)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
      ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 2)));
      ASSERT_FALSE(eset.count(std::make_tuple(3, 2, 1)));
    } else {
      ASSERT_TRUE(eset.count(std::make_tuple(0, 1, 1)));
      ASSERT_TRUE(eset.count(std::make_tuple(1, 1, 2)));
      ASSERT_TRUE(eset.count(std::make_tuple(3, 3, 4)));
      ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_FALSE(eset.count(std::make_tuple(3, 2, 3)));
    }
  }
}

TEST(RowwiseTest, TestCSRSamplingBiased) {
  _TestCSRSamplingBiased<int32_t, float>(true);
  _TestCSRSamplingBiased<int32_t, float>(false);
  _TestCSRSamplingBiased<int64_t, float>(true);
  _TestCSRSamplingBiased<int64_t, float>(false);
  _TestCSRSamplingBiased<int32_t, double>(true);
  _TestCSRSamplingBiased<int32_t, double>(false);
  _TestCSRSamplingBiased<int64_t, double>(true);
  _TestCSRSamplingBiased<int64_t, double>(false);
905
}