| 304 | |
| 305 | template <typename T> |
| 306 | void 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 | |
| 358 | template <typename dtype> |
| 359 | void run_exponential(Handle* handle) { |
nothing calls this directly
no test coverage detected