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

Function run_shuffle

dnn/test/naive/rng.cpp:306–356  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

304
305template <typename T>
306void run_shuffle(Handle* handle, bool bwd_flag) {
307 using ctype = typename DTypeTrait<T>::ctype;
308 auto run = [&](TensorShape shape) {
309 auto opr = handle->create_operator<ShuffleRNGForward>();
310 TensorLayout srclay{shape, T()};
311 TensorLayout dstlay{shape, T()};
312 TensorLayout indexlay{TensorShape{shape[0]}, dtype::Int32()};
313 Tensor<dt_byte> workspace(
314 handle,
315 {TensorShape{opr->get_workspace_in_bytes(srclay, dstlay, indexlay)},
316 dtype::Byte()});
317 Tensor<ctype> src(handle, srclay);
318 Tensor<ctype> dst(handle, dstlay);
319 Tensor<DTypeTrait<dt_int32>::ctype> index(handle, indexlay);
320 auto sptr = src.ptr();
321 size_t size = src.layout().total_nr_elems();
322 for (size_t j = 0; j < size; ++j) {
323 sptr[j] = j;
324 }
325 opr->exec(
326 src.tensornd(), dst.tensornd(), index.tensornd(),
327 {workspace.ptr(), workspace.layout().total_nr_elems()});
328
329 auto dptr = dst.ptr();
330 auto iptr = index.ptr();
331 size_t len = index.layout().total_nr_elems();
332 size_t step = size / len;
333 for (size_t i = 0; i < len; ++i) {
334 for (size_t j = 0; j < step; ++j) {
335 ASSERT_EQ(dptr[i * step + j], sptr[iptr[i] * step + j]);
336 }
337 }
338 if (bwd_flag) {
339 for (size_t j = 0; j < size; ++j) {
340 sptr[j] = 0;
341 }
342 auto oprbwd = handle->create_operator<ShuffleRNGBackward>();
343 oprbwd->exec(
344 dst.tensornd(), index.tensornd(), src.tensornd(),
345 {workspace.ptr(), workspace.layout().total_nr_elems()});
346 for (size_t i = 0; i < len; ++i) {
347 for (size_t j = 0; j < step; ++j) {
348 ASSERT_EQ(dptr[i * step + j], sptr[iptr[i] * step + j]);
349 }
350 }
351 }
352 };
353
354 run({10});
355 run({6, 3});
356}
357
358template <typename dtype>
359void run_exponential(Handle* handle) {

Callers

nothing calls this directly

Calls 8

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

Tested by

no test coverage detected