| 435 | } |
| 436 | |
| 437 | void CudnnRNNHandle::init_param_mapping(cudnnTensorDescriptor_t *xDesc) { |
| 438 | int linLayerIDRange = 2; |
| 439 | if (mode == 0 || mode == 1) { |
| 440 | // vanilla relu/tanh |
| 441 | linLayerIDRange = 2; |
| 442 | } else if (mode == 2) { |
| 443 | // lstm |
| 444 | linLayerIDRange = 8; |
| 445 | } else if (mode == 3) { |
| 446 | // gru |
| 447 | linLayerIDRange = 6; |
| 448 | } |
| 449 | int pseudoLayerRange = (bidirectional ? 2 : 1) * num_layers; |
| 450 | |
| 451 | // dummy weights for getting the offset |
| 452 | Tensor weights( |
| 453 | Shape{ |
| 454 | weights_size, |
| 455 | }, |
| 456 | dev); |
| 457 | weights.SetValue(0.0f); |
| 458 | const void *W_ptr = weights.block()->data(); |
| 459 | |
| 460 | void *param_ptr = nullptr; |
| 461 | int dims[] = {1, 1, 1}; |
| 462 | cudnnDataType_t data_type; |
| 463 | cudnnTensorFormat_t tensor_format; |
| 464 | int n_dims; |
| 465 | cudnnFilterDescriptor_t paramDesc; |
| 466 | CUDNN_CHECK(cudnnCreateFilterDescriptor(¶mDesc)); |
| 467 | |
| 468 | vector<bool> paramTypes{false, true}; |
| 469 | for (int linLayerID = 0; linLayerID < linLayerIDRange; linLayerID++) { |
| 470 | for (int pseudoLayer = 0; pseudoLayer < pseudoLayerRange; pseudoLayer++) { |
| 471 | for (const bool &is_bias : paramTypes) { |
| 472 | // get param ptr |
| 473 | if (is_bias) { |
| 474 | CUDNN_CHECK(cudnnGetRNNLinLayerBiasParams( |
| 475 | ctx->cudnn_handle, rnnDesc, pseudoLayer, xDesc[0], wDesc, W_ptr, |
| 476 | linLayerID, paramDesc, ¶m_ptr)); |
| 477 | } else { |
| 478 | CUDNN_CHECK(cudnnGetRNNLinLayerMatrixParams( |
| 479 | ctx->cudnn_handle, rnnDesc, pseudoLayer, xDesc[0], wDesc, W_ptr, |
| 480 | linLayerID, paramDesc, ¶m_ptr)); |
| 481 | } |
| 482 | |
| 483 | // get param dims |
| 484 | CUDNN_CHECK(cudnnGetFilterNdDescriptor(paramDesc, 3, &data_type, |
| 485 | &tensor_format, &n_dims, dims)); |
| 486 | |
| 487 | // get diff - offset |
| 488 | size_t offset = (float *)param_ptr - (float *)W_ptr; |
| 489 | |
| 490 | // save in map |
| 491 | weights_mapping[std::make_tuple(linLayerID, pseudoLayer, is_bias)] = |
| 492 | std::make_tuple(offset, dims[0] * dims[1] * dims[2]); |
| 493 | } |
| 494 | } |
nothing calls this directly
no test coverage detected