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

Function run_shuffle

dnn/test/cuda/rng.cpp:271–322  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

269
270template <typename T>
271void run_shuffle(Handle* handle, bool bwd_flag) {
272 using ctype = typename DTypeTrait<T>::ctype;
273 auto run = [&](TensorShape shape) {
274 auto opr = handle->create_operator<ShuffleRNGForward>();
275 TensorLayout srclay{shape, T()};
276 TensorLayout dstlay{shape, T()};
277 TensorLayout indexlay{TensorShape{shape[0]}, dtype::Int32()};
278 Tensor<dt_byte> workspace(
279 handle,
280 {TensorShape{opr->get_workspace_in_bytes(srclay, dstlay, indexlay)},
281 dtype::Byte()});
282 SyncedTensor<ctype> src(handle, srclay);
283 SyncedTensor<ctype> dst(handle, dstlay);
284 SyncedTensor<DTypeTrait<dt_int32>::ctype> index(handle, indexlay);
285 auto sptr = src.ptr_mutable_host();
286 size_t size = src.layout().total_nr_elems();
287 for (size_t j = 0; j < size; ++j) {
288 sptr[j] = j;
289 }
290 opr->exec(
291 src.tensornd_dev(), dst.tensornd_dev(), index.tensornd_dev(),
292 {workspace.ptr(), workspace.layout().total_nr_elems()});
293
294 auto dptr = dst.ptr_mutable_host();
295 auto iptr = index.ptr_mutable_host();
296 size_t len = index.layout().total_nr_elems();
297 size_t step = size / len;
298 for (size_t i = 0; i < len; ++i) {
299 for (size_t j = 0; j < step; ++j) {
300 ASSERT_EQ(dptr[i * step + j], sptr[iptr[i] * step + j]);
301 }
302 }
303 if (bwd_flag) {
304 for (size_t j = 0; j < size; ++j) {
305 sptr[j] = 0;
306 }
307 auto oprbwd = handle->create_operator<ShuffleRNGBackward>();
308 oprbwd->exec(
309 dst.tensornd_dev(), index.tensornd_dev(), src.tensornd_dev(),
310 {workspace.ptr(), workspace.layout().total_nr_elems()});
311 auto sptr_bwd = src.ptr_mutable_host();
312 for (size_t i = 0; i < len; ++i) {
313 for (size_t j = 0; j < step; ++j) {
314 ASSERT_EQ(dptr[i * step + j], sptr_bwd[iptr[i] * step + j]);
315 }
316 }
317 }
318 };
319
320 run({10});
321 run({6, 3});
322}
323
324template <typename T>
325void run_exponential(Handle* handle) {

Callers

nothing calls this directly

Calls 7

TClass · 0.85
runFunction · 0.50
total_nr_elemsMethod · 0.45
layoutMethod · 0.45
execMethod · 0.45
ptrMethod · 0.45

Tested by

no test coverage detected