test_rowwise.cc 32.6 KB
Newer Older
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
#include <gtest/gtest.h>
#include <dgl/array.h>
#include <tuple>
#include <set>
#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;
  }
}

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

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
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) {
    //std::cout << row[i] << " " << col[i] <<  " " << data[i] << std::endl;
    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]));
  }
}

85
86
87
88
89
90
91
92
93
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) {
94
95
    int64_t count = gt.count(std::make_tuple(row[i], col[i], data[i]));
    ASSERT_TRUE(count);
96
97
98
99
    ASSERT_TRUE(IsInArray(rows, row[i]));
  }
}

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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);
}

122
template <typename Idx>
123
std::pair<CSRMatrix, std::vector<int64_t>> CSREtypes(bool has_data) {
124
  IdArray indptr = NDArray::FromVector(std::vector<Idx>({0, 4, 5, 5, 7}));
125
126
127
  IdArray indices = 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});
128
  if (has_data)
129
    return {CSRMatrix(4, 4, indptr, indices, data), eid2etype_offsets};
130
  else
131
    return {CSRMatrix(4, 4, indptr, indices), eid2etype_offsets};
132
133
134
}

template <typename Idx>
135
std::pair<COOMatrix, std::vector<int64_t>> COOEtypes(bool has_data) {
136
  IdArray row = NDArray::FromVector(std::vector<Idx>({0, 0, 0, 0, 1, 3, 3}));
137
138
139
  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});
140
  if (has_data)
141
    return {COOMatrix(4, 4, row, col, data), eid2etype_offsets};
142
  else
143
    return {COOMatrix(4, 4, row, col), eid2etype_offsets};
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
template <typename Idx, typename FloatType>
void _TestCSRSampling(bool has_data) {
  auto mat = CSR<Idx>(has_data);
  FloatArray prob = NDArray::FromVector(
      std::vector<FloatType>({.5, .5, .5, .5, .5}));
  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)));
    }
  }
  prob = NDArray::FromVector(
      std::vector<FloatType>({.0, .5, .5, .0, .5}));
  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);
202
  FloatArray prob = aten::NullArray();
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
  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);
}

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

296
297
298
299
300
301
  prob = {
    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}))
  };
302
  for (int k = 0; k < 10; ++k) {
303
304
    auto rst = CSRRowWisePerEtypeSampling(mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true);
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
305
306
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
307
      ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
308
309
310
    } else {
      ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_FALSE(eset.count(std::make_tuple(0, 2, 2)));
311
      ASSERT_FALSE(eset.count(std::make_tuple(0, 3, 3)));
312
313
314
315
    }
  }
}

316
template <typename Idx, typename FloatType>
317
318
319
320
321
322
323
324
325
326
void _TestCSRPerEtypeSamplingSorted() {
  auto pair = CSREtypes<Idx>(true);
  auto mat = pair.first;
  auto eid2etype_offset = pair.second;
  std::vector<FloatArray> prob = {
    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}))
  };
327
328
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
329
330
    auto rst = CSRRowWisePerEtypeSampling(mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true, true);
    CheckSampledPerEtypeResult<Idx>(rst, rows, true);
331
332
  }
  for (int k = 0; k < 10; ++k) {
333
334
    auto rst = CSRRowWisePerEtypeSampling(mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, false, true);
    CheckSampledPerEtypeResult<Idx>(rst, rows, true);
335
    auto eset = ToEdgeSet<Idx>(rst);
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
    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 = {
    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}))
  };
363
  for (int k = 0; k < 10; ++k) {
364
365
    auto rst = CSRRowWisePerEtypeSampling(mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true, true);
    CheckSampledPerEtypeResult<Idx>(rst, rows, true);
366
    auto eset = ToEdgeSet<Idx>(rst);
367
    ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
368
369
370
  }
}

371
372
373
374
375
376
377
378
379
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);
380
381
382
383
  _TestCSRPerEtypeSamplingSorted<int32_t, float>();
  _TestCSRPerEtypeSamplingSorted<int64_t, float>();
  _TestCSRPerEtypeSamplingSorted<int32_t, double>();
  _TestCSRPerEtypeSamplingSorted<int64_t, double>();
384
385
386
387
}

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

446
template <typename Idx, typename FloatType>
447
448
449
450
451
452
453
454
455
456
void _TestCSRPerEtypeSamplingUniformSorted() {
  auto pair = CSREtypes<Idx>(true);
  auto mat = pair.first;
  auto eid2etype_offset = pair.second;
  std::vector<FloatArray> prob = {
    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);
    CheckSampledPerEtypeResult<Idx>(rst, rows, true);
461
462
  }
  for (int k = 0; k < 10; ++k) {
463
464
    auto rst = CSRRowWisePerEtypeSampling(mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, false, true);
    CheckSampledPerEtypeResult<Idx>(rst, rows, true);
465
    auto eset = ToEdgeSet<Idx>(rst);
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
    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);
485
486
487
  }
}

488
489
490
491
492
493
494
495
496
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);
497
498
499
500
  _TestCSRPerEtypeSamplingUniformSorted<int32_t, float>();
  _TestCSRPerEtypeSamplingUniformSorted<int64_t, float>();
  _TestCSRPerEtypeSamplingUniformSorted<int32_t, double>();
  _TestCSRPerEtypeSamplingUniformSorted<int64_t, double>();
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
template <typename Idx, typename FloatType>
void _TestCOOSampling(bool has_data) {
  auto mat = COO<Idx>(has_data);
  FloatArray prob = NDArray::FromVector(
      std::vector<FloatType>({.5, .5, .5, .5, .5}));
  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)));
    }
  }
  prob = NDArray::FromVector(
      std::vector<FloatType>({.0, .5, .5, .0, .5}));
  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
604
605
606
607
608
void _TestCOOPerEtypeSampling(bool has_data) {
  auto pair = COOEtypes<Idx>(has_data);
  auto mat = pair.first;
  auto eid2etype_offset = pair.second;
  std::vector<FloatArray> prob = {
    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}))
  };
609
610
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
611
612
    auto rst = COORowWisePerEtypeSampling(mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true);
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
613
614
  }
  for (int k = 0; k < 10; ++k) {
615
    auto rst = COORowWisePerEtypeSampling(mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, false);
616
617
618
619
620
621
622
623
    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;
624
      counts += eset.count(std::make_tuple(0, 2, 4));
625
626
      ASSERT_EQ(counts, 1);
      counts = 0;
627
628
629
      counts += eset.count(std::make_tuple(0, 3, 6));
      ASSERT_EQ(counts, 1);
      counts = 0;
630
      counts += eset.count(std::make_tuple(1, 1, 2));
631
632
      ASSERT_EQ(counts, 0);
      counts = 0;
633
      counts += eset.count(std::make_tuple(3, 2, 5));
634
635
      ASSERT_EQ(counts, 1);
      counts = 0;
636
      counts += eset.count(std::make_tuple(3, 3, 3));
637
638
639
640
641
642
643
      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));
644
      ASSERT_EQ(counts, 2);
645
646
647
648
      counts = 0;
      counts += eset.count(std::make_tuple(1, 1, 4));
      ASSERT_EQ(counts, 0);
      counts = 0;
649
      counts += eset.count(std::make_tuple(3, 3, 5));
650
651
      ASSERT_EQ(counts, 1);
      counts = 0;
652
      counts += eset.count(std::make_tuple(3, 2, 6));
653
654
655
656
      ASSERT_EQ(counts, 1);
    }
  }

657
658
659
660
661
662
  prob = {
    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);
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
666
667
    auto eset = ToEdgeSet<Idx>(rst);
    if (has_data) {
668
      ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
669
670
671
    } else {
      ASSERT_FALSE(eset.count(std::make_tuple(0, 0, 0)));
      ASSERT_FALSE(eset.count(std::make_tuple(0, 2, 2)));
672
      ASSERT_FALSE(eset.count(std::make_tuple(0, 3, 3)));
673
674
675
676
    }
  }
}

677
678
679
680
681
682
683
684
685
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);
686
687
688
689
}

template <typename Idx, typename FloatType>
void _TestCOOPerEtypeSamplingUniform(bool has_data) {
690
691
692
693
694
695
696
697
698
  auto pair = COOEtypes<Idx>(has_data);
  auto mat = pair.first;
  auto eid2etype_offset = pair.second;
  std::vector<FloatArray> prob = {
    aten::NullArray(),
    aten::NullArray(),
    aten::NullArray(),
    aten::NullArray()
  };
699
700
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));
  for (int k = 0; k < 10; ++k) {
701
702
    auto rst = COORowWisePerEtypeSampling(mat, rows, eid2etype_offset, {2, 2, 2, 2}, prob, true);
    CheckSampledPerEtypeResult<Idx>(rst, rows, has_data);
703
704
  }
  for (int k = 0; k < 10; ++k) {
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
762
template <typename Idx, typename FloatType>
void _TestCSRTopk(bool has_data) {
  auto mat = CSR<Idx>(has_data);
  FloatArray weight = NDArray::FromVector(
763
      std::vector<FloatType>({.1f, .0f, -.1f, .2f, .5f}));
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
  // -.1, .2, .1, .0, .5
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));

  {
  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)));
  }
  }

  {
  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)));
  }
  }
}

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);
  FloatArray weight = NDArray::FromVector(
810
      std::vector<FloatType>({.1f, .0f, -.1f, .2f, .5f}));
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
  // -.1, .2, .1, .0, .5
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 3}));

  {
  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)));
  }
  }

  {
  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)));
  }
  }
}

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);
}
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

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(
      std::vector<Idx>({0, 1, 2,
                        0, 0, 1,
                        0, 0, 0,
                        0, 1, 2}));
  tag_offset = tag_offset.CreateView({4, 3}, tag_offset->dtype);
  IdArray rows = NDArray::FromVector(std::vector<Idx>({0, 1, 3}));
  FloatArray bias = NDArray::FromVector(
      std::vector<FloatType>({0, 0.5})
  );
  for (int k = 0 ; k < 10 ; ++k) {
    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)));
    }
  }
  for (int k = 0 ; k < 10 ; ++k) {
    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);
911
}