cudnn_rnn_cache.h 10.5 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/cudnn_helper.h"

namespace paddle {
namespace operators {

struct CudnnRNNCache {
  CudnnRNNCache() {
    x_desc_ = NULL;
    y_desc_ = NULL;
    dx_desc_ = NULL;
    dy_desc_ = NULL;
  }
  ~CudnnRNNCache() { release(); }

  cudnnRNNDescriptor_t rnn_desc_;
  cudnnTensorDescriptor_t *x_desc_;
  cudnnTensorDescriptor_t *y_desc_;
  cudnnTensorDescriptor_t *dx_desc_;
  cudnnTensorDescriptor_t *dy_desc_;

  cudnnTensorDescriptor_t hx_desc_;
  cudnnTensorDescriptor_t cx_desc_;
  cudnnTensorDescriptor_t hy_desc_;
  cudnnTensorDescriptor_t cy_desc_;

  cudnnTensorDescriptor_t dhx_desc_;
  cudnnTensorDescriptor_t dcx_desc_;
  cudnnTensorDescriptor_t dhy_desc_;
  cudnnTensorDescriptor_t dcy_desc_;

  cudnnTensorDescriptor_t output_x_desc_;
  cudnnTensorDescriptor_t output_y_desc_;

  cudnnDropoutDescriptor_t dropout_desc_;

  size_t weights_size_;
  cudnnFilterDescriptor_t w_desc_;
  cudnnFilterDescriptor_t dw_desc_;

  size_t workspace_size_;
  size_t reserve_size_;
  framework::Tensor reserve_data_;
  framework::Tensor workspace_data_;

  framework::Tensor dropout_state_;

  size_t max_length_;

  float dropout_prob_;
  bool is_bidirec_;

  int batch_size_;
  int input_size_;
  int hidden_size_;
  int num_layers_;
  int seed_;

  void init(cudnnHandle_t handle, const platform::Place &place, size_t max_len,
            int batch_size, int input_size, int hidden_size, int num_layers,
            float dropout_prob, bool is_bidirec, int seed, int weight_numel) {
    max_length_ = max_len;
    batch_size_ = batch_size;
    input_size_ = input_size;
    hidden_size_ = hidden_size;
    num_layers_ = num_layers;
    dropout_prob_ = dropout_prob;
    is_bidirec_ = is_bidirec;
    seed_ = seed;

    x_desc_ = new cudnnTensorDescriptor_t[max_length_];
    y_desc_ = new cudnnTensorDescriptor_t[max_length_];
    dx_desc_ = new cudnnTensorDescriptor_t[max_length_];
    dy_desc_ = new cudnnTensorDescriptor_t[max_length_];
    int dim_a[3];
    int stride_a[3];

    for (size_t i = 0; i < max_length_; ++i) {
95
      PADDLE_ENFORCE_CUDA_SUCCESS(
S
sneaxiy 已提交
96
          platform::dynload::cudnnCreateTensorDescriptor(&x_desc_[i]));
97
      PADDLE_ENFORCE_CUDA_SUCCESS(
S
sneaxiy 已提交
98
          platform::dynload::cudnnCreateTensorDescriptor(&y_desc_[i]));
99
      PADDLE_ENFORCE_CUDA_SUCCESS(
S
sneaxiy 已提交
100
          platform::dynload::cudnnCreateTensorDescriptor(&dx_desc_[i]));
101
      PADDLE_ENFORCE_CUDA_SUCCESS(
S
sneaxiy 已提交
102 103 104 105 106 107 108 109
          platform::dynload::cudnnCreateTensorDescriptor(&dy_desc_[i]));
      dim_a[0] = batch_size_;
      dim_a[1] = input_size_;
      dim_a[2] = 1;

      stride_a[0] = dim_a[2] * dim_a[1];
      stride_a[1] = dim_a[2];
      stride_a[2] = 1;
110
      PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetTensorNdDescriptor(
S
sneaxiy 已提交
111
          x_desc_[i], CUDNN_DATA_FLOAT, 3, dim_a, stride_a));
112
      PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetTensorNdDescriptor(
S
sneaxiy 已提交
113 114 115 116 117 118 119 120 121 122
          dx_desc_[i], CUDNN_DATA_FLOAT, 3, dim_a, stride_a));

      dim_a[0] = batch_size_;
      dim_a[1] = is_bidirec_ ? hidden_size_ * 2 : hidden_size_;
      dim_a[2] = 1;

      stride_a[0] = dim_a[2] * dim_a[1];
      stride_a[1] = dim_a[2];
      stride_a[2] = 1;

123
      PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetTensorNdDescriptor(
S
sneaxiy 已提交
124
          y_desc_[i], CUDNN_DATA_FLOAT, 3, dim_a, stride_a));
125
      PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetTensorNdDescriptor(
S
sneaxiy 已提交
126 127 128 129 130 131 132 133 134 135 136
          dy_desc_[i], CUDNN_DATA_FLOAT, 3, dim_a, stride_a));
    }

    dim_a[0] = num_layers_ * (is_bidirec_ ? 2 : 1);
    dim_a[1] = batch_size_;
    dim_a[2] = hidden_size_;

    stride_a[0] = dim_a[2] * dim_a[1];
    stride_a[1] = dim_a[2];
    stride_a[2] = 1;

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnCreateTensorDescriptor(&hx_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnCreateTensorDescriptor(&cx_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnCreateTensorDescriptor(&hy_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnCreateTensorDescriptor(&cy_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnCreateTensorDescriptor(&dhx_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnCreateTensorDescriptor(&dcx_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnCreateTensorDescriptor(&dhy_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnCreateTensorDescriptor(&dcy_desc_));

    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetTensorNdDescriptor(
S
sneaxiy 已提交
155
        hx_desc_, CUDNN_DATA_FLOAT, 3, dim_a, stride_a));
156
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetTensorNdDescriptor(
S
sneaxiy 已提交
157
        cx_desc_, CUDNN_DATA_FLOAT, 3, dim_a, stride_a));
158
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetTensorNdDescriptor(
S
sneaxiy 已提交
159
        hy_desc_, CUDNN_DATA_FLOAT, 3, dim_a, stride_a));
160
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetTensorNdDescriptor(
S
sneaxiy 已提交
161
        cy_desc_, CUDNN_DATA_FLOAT, 3, dim_a, stride_a));
162
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetTensorNdDescriptor(
S
sneaxiy 已提交
163
        dhx_desc_, CUDNN_DATA_FLOAT, 3, dim_a, stride_a));
164
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetTensorNdDescriptor(
S
sneaxiy 已提交
165
        dcx_desc_, CUDNN_DATA_FLOAT, 3, dim_a, stride_a));
166
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetTensorNdDescriptor(
S
sneaxiy 已提交
167
        dhy_desc_, CUDNN_DATA_FLOAT, 3, dim_a, stride_a));
168
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetTensorNdDescriptor(
S
sneaxiy 已提交
169 170
        dcy_desc_, CUDNN_DATA_FLOAT, 3, dim_a, stride_a));

171
    PADDLE_ENFORCE_CUDA_SUCCESS(
S
sneaxiy 已提交
172 173 174
        platform::dynload::cudnnCreateDropoutDescriptor(&dropout_desc_));

    size_t state_size;
175 176 177
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnDropoutGetStatesSize(handle, &state_size));
    dropout_state_.Resize({static_cast<int64_t>(state_size)});
S
sneaxiy 已提交
178
    auto *dropout_state_data = dropout_state_.mutable_data<uint8_t>(place);
179
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetDropoutDescriptor(
S
sneaxiy 已提交
180 181 182
        dropout_desc_, handle, dropout_prob_, dropout_state_data, state_size,
        seed_));

183 184
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnCreateRNNDescriptor(&rnn_desc_));
S
sneaxiy 已提交
185 186

#if CUDNN_VERSION >= 6000
187
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetRNNDescriptor_v6(
S
sneaxiy 已提交
188 189 190 191 192
        handle, rnn_desc_, hidden_size_, num_layers_, dropout_desc_,
        CUDNN_LINEAR_INPUT,
        is_bidirec_ ? CUDNN_BIDIRECTIONAL : CUDNN_UNIDIRECTIONAL, CUDNN_LSTM,
        CUDNN_RNN_ALGO_STANDARD, CUDNN_DATA_FLOAT));
#else
193
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetRNNDescriptor(
S
sneaxiy 已提交
194 195 196 197 198
        rnn_desc_, hidden_size_, num_layers_, dropout_desc_, CUDNN_LINEAR_INPUT,
        is_bidirec_ ? CUDNN_BIDIRECTIONAL : CUDNN_UNIDIRECTIONAL, CUDNN_LSTM,
        CUDNN_DATA_FLOAT));
#endif

199 200 201 202
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnCreateFilterDescriptor(&w_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnCreateFilterDescriptor(&dw_desc_));
S
sneaxiy 已提交
203

204
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnGetRNNParamsSize(
S
sneaxiy 已提交
205 206 207 208 209 210 211 212
        handle, rnn_desc_, x_desc_[0], &weights_size_, CUDNN_DATA_FLOAT));

    PADDLE_ENFORCE_EQ(weights_size_, sizeof(float) * weight_numel,
                      "cudnn lstm weight size should be SAME");
    int dim_w[3];
    dim_w[0] = weights_size_ / sizeof(float);
    dim_w[1] = 1;
    dim_w[2] = 1;
213
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetFilterNdDescriptor(
S
sneaxiy 已提交
214
        w_desc_, CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 3, dim_w));
215
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnSetFilterNdDescriptor(
S
sneaxiy 已提交
216 217
        dw_desc_, CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 3, dim_w));

218
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnGetRNNWorkspaceSize(
S
sneaxiy 已提交
219
        handle, rnn_desc_, max_length_, x_desc_, &workspace_size_));
220 221 222
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnGetRNNTrainingReserveSize(
            handle, rnn_desc_, max_length_, x_desc_, &reserve_size_));
S
sneaxiy 已提交
223 224 225 226 227 228 229 230 231 232

    reserve_data_.Resize({static_cast<int64_t>(reserve_size_)});
    reserve_data_.mutable_data<uint8_t>(place);

    workspace_data_.Resize({static_cast<int64_t>(workspace_size_)});
    workspace_data_.mutable_data<uint8_t>(place);
  }

  void release() {
    for (size_t i = 0; i < max_length_; ++i) {
233
      PADDLE_ENFORCE_CUDA_SUCCESS(
S
sneaxiy 已提交
234
          platform::dynload::cudnnDestroyTensorDescriptor(x_desc_[i]));
235
      PADDLE_ENFORCE_CUDA_SUCCESS(
S
sneaxiy 已提交
236
          platform::dynload::cudnnDestroyTensorDescriptor(y_desc_[i]));
237
      PADDLE_ENFORCE_CUDA_SUCCESS(
S
sneaxiy 已提交
238
          platform::dynload::cudnnDestroyTensorDescriptor(dx_desc_[i]));
239
      PADDLE_ENFORCE_CUDA_SUCCESS(
S
sneaxiy 已提交
240 241 242 243 244 245 246 247
          platform::dynload::cudnnDestroyTensorDescriptor(dy_desc_[i]));
    }

    delete[] x_desc_;
    delete[] y_desc_;
    delete[] dx_desc_;
    delete[] dy_desc_;

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnDestroyTensorDescriptor(hx_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnDestroyTensorDescriptor(cx_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnDestroyTensorDescriptor(hy_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnDestroyTensorDescriptor(cy_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnDestroyTensorDescriptor(dhx_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnDestroyTensorDescriptor(dcx_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnDestroyTensorDescriptor(dhy_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnDestroyTensorDescriptor(dcy_desc_));

    PADDLE_ENFORCE_CUDA_SUCCESS(
S
sneaxiy 已提交
266
        platform::dynload::cudnnDestroyDropoutDescriptor(dropout_desc_));
267 268
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnDestroyRNNDescriptor(rnn_desc_));
S
sneaxiy 已提交
269

270 271 272 273
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnDestroyFilterDescriptor(w_desc_));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::cudnnDestroyFilterDescriptor(dw_desc_));
S
sneaxiy 已提交
274 275 276 277 278
  }
};

}  // namespace operators
}  // namespace paddle