| 385 | |
| 386 | template <typename T> |
| 387 | void run_dropout(Handle* handle) { |
| 388 | using ctype = typename DTypeTrait<T>::ctype; |
| 389 | auto run = [&](TensorShape shape, float drop_prob) { |
| 390 | auto fwd = handle->create_operator<DropoutForward>(); |
| 391 | auto bwd = handle->create_operator<DropoutBackward>(); |
| 392 | fwd->param().drop_prob = drop_prob; |
| 393 | bwd->param().drop_prob = drop_prob; |
| 394 | double scale = 1.0 / (1.0 - drop_prob); |
| 395 | |
| 396 | TensorLayout inp_lay{shape, T()}; |
| 397 | TensorLayout oup_lay{shape, T()}; |
| 398 | TensorLayout mask_lay{{fwd->get_mask_size_in_bytes(inp_lay)}, dtype::Byte()}; |
| 399 | TensorLayout doup_lay{shape, T()}; |
| 400 | TensorLayout dinp_lay{shape, T()}; |
| 401 | TensorLayout fwd_ws_lay{ |
| 402 | {fwd->get_workspace_in_bytes(inp_lay, oup_lay, mask_lay)}, |
| 403 | dtype::Byte()}; |
| 404 | TensorLayout bwd_ws_lay{ |
| 405 | {bwd->get_workspace_in_bytes(doup_lay, mask_lay, dinp_lay)}, |
| 406 | dtype::Byte()}; |
| 407 | |
| 408 | Tensor<ctype> inp(handle, inp_lay); |
| 409 | Tensor<ctype> oup(handle, oup_lay); |
| 410 | Tensor<DTypeTrait<dt_byte>::ctype> mask(handle, mask_lay); |
| 411 | Tensor<ctype> doup(handle, doup_lay); |
| 412 | Tensor<ctype> dinp(handle, dinp_lay); |
| 413 | Tensor<DTypeTrait<dt_byte>::ctype> fwd_ws(handle, fwd_ws_lay); |
| 414 | Tensor<DTypeTrait<dt_byte>::ctype> bwd_ws(handle, bwd_ws_lay); |
| 415 | |
| 416 | for (size_t i = 0; i < inp.layout().total_nr_elems(); ++i) { |
| 417 | inp.ptr()[i] = 1; |
| 418 | doup.ptr()[i] = 1; |
| 419 | } |
| 420 | |
| 421 | fwd->exec( |
| 422 | inp.tensornd(), oup.tensornd(), mask.tensornd(), |
| 423 | {fwd_ws.ptr(), fwd_ws.layout().total_nr_elems()}); |
| 424 | size_t droped_cnt = 0; |
| 425 | for (size_t i = 0; i < inp.layout().total_nr_elems(); ++i) { |
| 426 | ASSERT_TRUE(oup.ptr()[i] == 0 || oup.ptr()[i] == static_cast<ctype>(scale)); |
| 427 | if (oup.ptr()[i] == 0) { |
| 428 | droped_cnt++; |
| 429 | } |
| 430 | } |
| 431 | float real_drop = droped_cnt * 1.0 / inp.layout().total_nr_elems(); |
| 432 | ASSERT_LT(abs(drop_prob - real_drop), 1e-2); |
| 433 | |
| 434 | bwd->exec( |
| 435 | doup.tensornd(), mask.tensornd(), dinp.tensornd(), |
| 436 | {bwd_ws.ptr(), bwd_ws.layout().total_nr_elems()}); |
| 437 | for (size_t i = 0; i < inp.layout().total_nr_elems(); ++i) { |
| 438 | ASSERT_TRUE(oup.ptr()[i] == dinp.ptr()[i]); |
| 439 | } |
| 440 | }; |
| 441 | |
| 442 | run({32, 32, 32, 32}, 0.2); |
| 443 | run({100000}, 0.3); |
| 444 | } |
nothing calls this directly
no test coverage detected