MCPcopy Create free account
hub / github.com/apache/singa / init_param_mapping

Method init_param_mapping

src/model/operation/rnn.cc:437–496  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

435}
436
437void 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(&paramDesc));
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, &param_ptr));
477 } else {
478 CUDNN_CHECK(cudnnGetRNNLinLayerMatrixParams(
479 ctx->cudnn_handle, rnnDesc, pseudoLayer, xDesc[0], wDesc, W_ptr,
480 linLayerID, paramDesc, &param_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 }

Callers

nothing calls this directly

Calls 4

make_tupleFunction · 0.85
SetValueMethod · 0.45
dataMethod · 0.45
blockMethod · 0.45

Tested by

no test coverage detected