-
Notifications
You must be signed in to change notification settings - Fork 27
/
indrnn_backward_gpu.cu.cc
214 lines (188 loc) · 5.23 KB
/
indrnn_backward_gpu.cu.cc
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2020 LMNT, Inc. 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.
// ==============================================================================
#include <cublas_v2.h>
#include <cuda_runtime_api.h>
#include "blas.h"
#include "haste.h"
#include "inline_ops.h"
namespace {
template<typename T, bool ApplyZoneout>
__global__
void IndrnnBwdOps(
const int steps,
const int batch_size,
const int hidden_size,
const T* u,
const T* h_prev,
const T* h,
const T* dh_new,
T* du_out,
T* db_out,
T* dh_inout,
T* dk_out,
const T* zoneout_mask) {
const int row = blockDim.x * blockIdx.x + threadIdx.x;
const int col = blockDim.y * blockIdx.y + threadIdx.y;
if (row >= hidden_size || col >= batch_size)
return;
const int NH = batch_size * hidden_size;
const int idx = col * hidden_size + row;
const T u_row = u[row];
T dh_inout_idx = dh_inout[idx];
T du_sum = static_cast<T>(0.0);
T db_sum = static_cast<T>(0.0);
for (int i = (steps - 1) * NH; i >= 0; i -= NH) {
T dh_total = dh_new[idx + i] + dh_inout_idx;
T dh = static_cast<T>(0.0);
if (ApplyZoneout) {
const T mask = zoneout_mask[idx + i];
dh = (static_cast<T>(1.0) - mask) * dh_total;
dh_total = mask * dh_total;
}
const T dk = d_tanh(h[idx + i]) * dh_total;
dk_out[idx + i] = dk;
dh_inout_idx = dh + u_row * dk;
du_sum += h_prev[idx + i] * dk;
db_sum += dk;
}
dh_inout[idx] = dh_inout_idx;
atomicAdd(&du_out[row], du_sum);
atomicAdd(&db_out[row], db_sum);
}
} // anonymous namespace
namespace haste {
namespace v0 {
namespace indrnn {
template<typename T>
struct BackwardPass<T>::private_data {
int batch_size;
int input_size;
int hidden_size;
cublasHandle_t blas_handle;
cudaStream_t stream;
cudaStream_t sync_stream;
};
template<typename T>
BackwardPass<T>::BackwardPass(
const int batch_size,
const int input_size,
const int hidden_size,
const cublasHandle_t& blas_handle,
const cudaStream_t& stream) : data_(new private_data) {
data_->batch_size = batch_size;
data_->input_size = input_size;
data_->hidden_size = hidden_size;
data_->blas_handle = blas_handle;
data_->sync_stream = stream;
cudaStreamCreate(&data_->stream);
}
template<typename T>
BackwardPass<T>::~BackwardPass() {
if (data_->sync_stream) {
cudaEvent_t event;
cudaEventCreateWithFlags(&event, cudaEventDisableTiming);
cudaEventRecord(event, data_->stream);
cudaStreamWaitEvent(data_->sync_stream, event, 0);
cudaEventDestroy(event);
} else {
cudaStreamSynchronize(data_->stream);
}
cudaStreamDestroy(data_->stream);
delete data_;
}
template<typename T>
void BackwardPass<T>::Run(
const int steps,
const T* W_t,
const T* u,
const T* b,
const T* x_t,
const T* h,
const T* dh_new,
T* dx,
T* dW,
T* du,
T* db,
T* dh,
T* workspace,
const T* zoneout_mask) {
const T alpha = static_cast<T>(1.0);
const T beta = static_cast<T>(0.0);
const blas<void>::set_pointer_mode scoped1(data_->blas_handle);
const int batch_size = data_->batch_size;
const int input_size = data_->input_size;
const int hidden_size = data_->hidden_size;
const cublasHandle_t blas_handle = data_->blas_handle;
const cudaStream_t stream = data_->stream;
const dim3 blockDim(64, 16);
const dim3 gridDim(
(hidden_size + blockDim.x - 1) / blockDim.x,
(batch_size + blockDim.y - 1) / blockDim.y);
const int NH = batch_size * hidden_size;
if (zoneout_mask) {
IndrnnBwdOps<T, true><<<gridDim, blockDim, 0, stream>>>(
steps,
batch_size,
hidden_size,
u,
h,
h + NH,
dh_new + NH,
du,
db,
dh,
workspace,
zoneout_mask);
} else {
IndrnnBwdOps<T, false><<<gridDim, blockDim, 0, stream>>>(
steps,
batch_size,
hidden_size,
u,
h,
h + NH,
dh_new + NH,
du,
db,
dh,
workspace,
nullptr);
}
cudaStream_t save_stream;
cublasGetStream(blas_handle, &save_stream);
cublasSetStream(blas_handle, stream);
blas<T>::gemm(blas_handle,
CUBLAS_OP_N, CUBLAS_OP_N,
hidden_size, input_size, batch_size * steps,
&alpha,
workspace, hidden_size,
x_t, batch_size * steps,
&beta,
dW, hidden_size);
blas<T>::gemm(blas_handle,
CUBLAS_OP_N, CUBLAS_OP_N,
input_size, steps * batch_size, hidden_size,
&alpha,
W_t, input_size,
workspace, hidden_size,
&beta,
dx, input_size);
cublasSetStream(blas_handle, save_stream);
}
template class BackwardPass<float>;
template class BackwardPass<double>;
} // namespace indrnn
} // namespace v0
} // namespace haste