MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / run_multinomial_without_replacement

Function run_multinomial_without_replacement

dnn/test/cuda/rng.cpp:153–205  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

151
152template <typename T>
153void run_multinomial_without_replacement(Handle* handle) {
154 using ctype = typename DTypeTrait<T>::ctype;
155
156 size_t num_groups = 2;
157 size_t num_samples = 1;
158 size_t len_probs = 4;
159 bool replacement = false;
160 size_t total_count = 10000;
161
162 TensorLayout ly_probs{TensorShape{num_groups, len_probs}, T()};
163 SyncedTensor<ctype> probs(handle, ly_probs);
164 auto probs_ptr = probs.ptr_mutable_host();
165 probs_ptr[0] = 1;
166 probs_ptr[1] = 2;
167 probs_ptr[2] = 3;
168 probs_ptr[3] = 4;
169 probs_ptr[4] = 0;
170 probs_ptr[5] = 7;
171 probs_ptr[6] = 2;
172 probs_ptr[7] = 1;
173
174 std::vector<float> norm_probs;
175 for (size_t i = 0; i < 8; ++i) {
176 norm_probs.push_back(probs_ptr[i] / 10);
177 }
178
179 auto opr = handle->create_operator<MultinomialRNG>();
180 opr->param().num_samples = num_samples;
181 opr->param().replacement = replacement;
182 TensorLayout ly_out{TensorShape{num_groups, num_samples}, dtype::Int32()};
183 SyncedTensor<dt_int32> out(handle, ly_out);
184 Tensor<dt_byte> workspace(
185 handle, {TensorShape{opr->get_workspace_in_bytes(ly_probs, ly_out)},
186 dtype::Byte()});
187 std::vector<float> sample_probs(num_groups * len_probs, 0);
188 for (size_t i = 0; i < total_count; ++i) {
189 opr->exec(
190 probs.tensornd_dev(), out.tensornd_dev(),
191 {workspace.ptr(), workspace.layout().total_nr_elems()});
192
193 auto ptr = out.ptr_mutable_host();
194 sample_probs[ptr[0]] += 1;
195 sample_probs[len_probs + ptr[1]] += 1;
196 }
197
198 for (size_t i = 0; i < num_groups * len_probs; ++i) {
199 sample_probs[i] /= total_count * num_samples;
200 }
201
202 for (size_t i = 0; i < num_groups * len_probs; ++i) {
203 ASSERT_LE(std::abs(sample_probs[i] - norm_probs[i]), 1e-2);
204 }
205}
206
207template <typename T>
208void run_beta(Handle* handle) {

Callers

nothing calls this directly

Calls 9

TClass · 0.85
absFunction · 0.50
push_backMethod · 0.45
paramMethod · 0.45
execMethod · 0.45
ptrMethod · 0.45
total_nr_elemsMethod · 0.45
layoutMethod · 0.45

Tested by

no test coverage detected