test_sampler.cc 8.28 KB
Newer Older
1
#include <gtest/gtest.h>
2

3
4
#include <algorithm>
#include <iostream>
5
6
#include <vector>

7
#include "../../src/random/cpu/sample_utils.h"
8
#include "./common.h"
9
10

using namespace dgl;
11
12
13
using namespace dgl::aten;

// TODO: adapt this to Random::Choice
14
15

template <typename Idx, typename DType>
16
void _TestWithReplacement(RandomEngine* re) {
17
18
  Idx n_categories = 100;
  Idx n_rolls = 1000000;
19
  std::vector<DType> _prob;
20
21
  DType accum = 0.;
  for (Idx i = 0; i < n_categories; ++i) {
22
23
    _prob.push_back(re->Uniform<DType>());
    accum += _prob.back();
24
  }
25
  for (Idx i = 0; i < n_categories; ++i) _prob[i] /= accum;
26
  FloatArray prob = NDArray::FromVector(_prob);
27

28
29
  auto _check_given_sampler = [n_categories, n_rolls,
                               &_prob](utils::BaseSampler<Idx>* s) {
30
31
    std::vector<Idx> counter(n_categories, 0);
    for (Idx i = 0; i < n_rolls; ++i) {
32
      Idx dice = s->Draw();
33
34
35
      counter[dice]++;
    }
    for (Idx i = 0; i < n_categories; ++i)
36
37
38
39
40
41
42
43
44
45
46
      ASSERT_NEAR(static_cast<DType>(counter[i]) / n_rolls, _prob[i], 1e-2);
  };

  auto _check_random_choice = [n_categories, n_rolls, &_prob, prob]() {
    std::vector<int64_t> counter(n_categories, 0);
    for (Idx i = 0; i < n_rolls; ++i) {
      Idx dice = RandomEngine::ThreadLocal()->Choice<int64_t>(prob);
      counter[dice]++;
    }
    for (Idx i = 0; i < n_categories; ++i)
      ASSERT_NEAR(static_cast<DType>(counter[i]) / n_rolls, _prob[i], 1e-2);
47
48
  };

49
50
51
  utils::AliasSampler<Idx, DType, true> as(re, prob);
  utils::CDFSampler<Idx, DType, true> cs(re, prob);
  utils::TreeSampler<Idx, DType, true> ts(re, prob);
52
53
54
  _check_given_sampler(&as);
  _check_given_sampler(&cs);
  _check_given_sampler(&ts);
55
  _check_random_choice();
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
}

TEST(SampleUtilsTest, TestWithReplacement) {
  RandomEngine* re = RandomEngine::ThreadLocal();
  re->SetSeed(42);
  _TestWithReplacement<int32_t, float>(re);
  re->SetSeed(42);
  _TestWithReplacement<int32_t, double>(re);
  re->SetSeed(42);
  _TestWithReplacement<int64_t, float>(re);
  re->SetSeed(42);
  _TestWithReplacement<int64_t, double>(re);
};

template <typename Idx, typename DType>
71
void _TestWithoutReplacementOrder(RandomEngine* re) {
72
  // TODO(BarclayII): is there a reliable way to do this test?
73
  std::vector<DType> _prob = {1e6f, 1e-6f, 1e-2f, 1e2f};
74
  FloatArray prob = NDArray::FromVector(_prob);
75
76
  std::vector<Idx> ground_truth = {0, 3, 2, 1};

77
  auto _check_given_sampler = [&ground_truth](utils::BaseSampler<Idx>* s) {
78
    for (size_t i = 0; i < ground_truth.size(); ++i) {
79
      Idx dice = s->Draw();
80
81
82
83
      ASSERT_EQ(dice, ground_truth[i]);
    }
  };

84
85
86
  utils::AliasSampler<Idx, DType, false> as(re, prob);
  utils::CDFSampler<Idx, DType, false> cs(re, prob);
  utils::TreeSampler<Idx, DType, false> ts(re, prob);
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  _check_given_sampler(&as);
  _check_given_sampler(&cs);
  _check_given_sampler(&ts);
}

TEST(SampleUtilsTest, TestWithoutReplacementOrder) {
  RandomEngine* re = RandomEngine::ThreadLocal();
  re->SetSeed(42);
  _TestWithoutReplacementOrder<int32_t, float>(re);
  re->SetSeed(42);
  _TestWithoutReplacementOrder<int32_t, double>(re);
  re->SetSeed(42);
  _TestWithoutReplacementOrder<int64_t, float>(re);
  re->SetSeed(42);
  _TestWithoutReplacementOrder<int64_t, double>(re);
};

template <typename Idx, typename DType>
105
void _TestWithoutReplacementUnique(RandomEngine* re) {
106
  Idx N = 1000000;
107
  std::vector<DType> _likelihood;
108
  for (Idx i = 0; i < N; ++i) _likelihood.push_back(re->Uniform<DType>());
109
  FloatArray likelihood = NDArray::FromVector(_likelihood);
110

111
  auto _check_given_sampler = [N](utils::BaseSampler<Idx>* s) {
112
113
    std::vector<int> cnt(N, 0);
    for (Idx i = 0; i < N; ++i) {
114
      Idx dice = s->Draw();
115
116
      cnt[dice]++;
    }
117
    for (Idx i = 0; i < N; ++i) ASSERT_EQ(cnt[i], 1);
118
119
  };

120
121
122
  utils::AliasSampler<Idx, DType, false> as(re, likelihood);
  utils::CDFSampler<Idx, DType, false> cs(re, likelihood);
  utils::TreeSampler<Idx, DType, false> ts(re, likelihood);
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  _check_given_sampler(&as);
  _check_given_sampler(&cs);
  _check_given_sampler(&ts);
}

TEST(SampleUtilsTest, TestWithoutReplacementUnique) {
  RandomEngine* re = RandomEngine::ThreadLocal();
  re->SetSeed(42);
  _TestWithoutReplacementUnique<int32_t, float>(re);
  re->SetSeed(42);
  _TestWithoutReplacementUnique<int32_t, double>(re);
  re->SetSeed(42);
  _TestWithoutReplacementUnique<int64_t, float>(re);
  re->SetSeed(42);
  _TestWithoutReplacementUnique<int64_t, double>(re);
};
139
140
141
142
143
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

template <typename Idx, typename DType>
void _TestChoice(RandomEngine* re) {
  re->SetSeed(42);
  std::vector<DType> prob_vec = {1., 0., 0., 0., 2., 2., 0., 0.};
  FloatArray prob = FloatArray::FromVector(prob_vec);
  {
    for (int k = 0; k < 1000; ++k) {
      Idx x = re->Choice<Idx>(prob);
      ASSERT_TRUE(x == 0 || x == 4 || x == 5);
    }
  }
  // num = 0
  {
    IdArray rst = re->Choice<Idx, DType>(0, prob, true);
    ASSERT_EQ(rst->shape[0], 0);
  }
  // w/ replacement
  {
    IdArray rst = re->Choice<Idx, DType>(1000, prob, true);
    ASSERT_EQ(rst->shape[0], 1000);
    for (int64_t i = 0; i < 1000; ++i) {
      Idx x = static_cast<Idx*>(rst->data)[i];
      ASSERT_TRUE(x == 0 || x == 4 || x == 5);
    }
  }
  // w/o replacement
  {
    IdArray rst = re->Choice<Idx, DType>(3, prob, false);
    ASSERT_EQ(rst->shape[0], 3);
    std::set<Idx> idxset;
    for (int64_t i = 0; i < 3; ++i) {
      Idx x = static_cast<Idx*>(rst->data)[i];
      idxset.insert(x);
    }
    ASSERT_EQ(idxset.size(), 3);
    ASSERT_EQ(idxset.count(0), 1);
    ASSERT_EQ(idxset.count(4), 1);
    ASSERT_EQ(idxset.count(5), 1);
  }
}

TEST(RandomTest, TestChoice) {
  RandomEngine* re = RandomEngine::ThreadLocal();
  _TestChoice<int32_t, float>(re);
  _TestChoice<int64_t, float>(re);
  _TestChoice<int32_t, double>(re);
  _TestChoice<int64_t, double>(re);
}

template <typename Idx>
void _TestUniformChoice(RandomEngine* re) {
  re->SetSeed(42);
  // num == 0
  {
    IdArray rst = re->UniformChoice<Idx>(0, 100, true);
    ASSERT_EQ(rst->shape[0], 0);
  }
  // w/ replacement
  {
    IdArray rst = re->UniformChoice<Idx>(1000, 100, true);
    ASSERT_EQ(rst->shape[0], 1000);
    for (int64_t i = 0; i < 1000; ++i) {
      Idx x = static_cast<Idx*>(rst->data)[i];
      ASSERT_TRUE(x >= 0 && x < 100);
    }
  }
  // w/o replacement
  {
    IdArray rst = re->UniformChoice<Idx>(99, 100, false);
    ASSERT_EQ(rst->shape[0], 99);
    std::set<Idx> idxset;
    for (int64_t i = 0; i < 99; ++i) {
      Idx x = static_cast<Idx*>(rst->data)[i];
      ASSERT_TRUE(x >= 0 && x < 100);
      idxset.insert(x);
    }
    ASSERT_EQ(idxset.size(), 99);
  }
}

TEST(RandomTest, TestUniformChoice) {
  RandomEngine* re = RandomEngine::ThreadLocal();
  _TestUniformChoice<int32_t>(re);
  _TestUniformChoice<int64_t>(re);
  _TestUniformChoice<int32_t>(re);
  _TestUniformChoice<int64_t>(re);
}
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241

template <typename Idx, typename FloatType>
void _TestBiasedChoice(RandomEngine* re) {
  re->SetSeed(42);
  // num == 0
  {
    Idx split[] = {0, 1, 2};
    FloatArray bias = NDArray::FromVector(std::vector<FloatType>({1, 3}));
    IdArray rst = re->BiasedChoice<Idx, FloatType>(0, split, bias, true);
    ASSERT_EQ(rst->shape[0], 0);
  }
  // basic test
  {
    Idx sample_num = 100000;
    Idx population = 1000000;
242
    Idx split[] = {0, population / 2, population};
243
244
    FloatArray bias = NDArray::FromVector(std::vector<FloatType>({1, 3}));

245
246
247
    IdArray rst =
        re->BiasedChoice<Idx, FloatType>(sample_num, split, bias, true);
    auto rst_data = static_cast<Idx*>(rst->data);
248
    Idx larger = 0;
249
250
    for (Idx i = 0; i < sample_num; ++i)
      if (rst_data[i] >= population / 2) larger++;
251
252
253
254
255
256
257
258
259
    ASSERT_LE(fabs((double)larger / sample_num - 0.75), 1e-2);
  }
  // without replacement
  {
    Idx sample_num = 500;
    Idx population = 1000;
    Idx split[] = {0, sample_num, population};
    FloatArray bias = NDArray::FromVector(std::vector<FloatType>({1, 0}));

260
261
262
    IdArray rst =
        re->BiasedChoice<Idx, FloatType>(sample_num, split, bias, false);
    auto rst_data = static_cast<Idx*>(rst->data);
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

    std::set<Idx> idxset;
    for (int64_t i = 0; i < sample_num; ++i) {
      Idx x = rst_data[i];
      ASSERT_LT(x, sample_num);
      idxset.insert(x);
    }
    ASSERT_EQ(idxset.size(), sample_num);
  }
}

TEST(RandomTest, TestBiasedChoice) {
  RandomEngine* re = RandomEngine::ThreadLocal();
  _TestBiasedChoice<int32_t, float>(re);
  _TestBiasedChoice<int64_t, float>(re);
  _TestBiasedChoice<int32_t, double>(re);
  _TestBiasedChoice<int64_t, double>(re);
280
}