-
Notifications
You must be signed in to change notification settings - Fork 29
/
gru.cc
239 lines (205 loc) · 5.94 KB
/
gru.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// 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 <Eigen/Dense>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cuda.h>
#include <cuda_runtime_api.h>
#include <iostream>
#include <string>
#include <unsupported/Eigen/CXX11/Tensor>
#include <vector>
#include "device_ptr.h"
#include "haste.h"
using haste::v0::gru::ForwardPass;
using haste::v0::gru::BackwardPass;
using std::string;
using Tensor1 = Eigen::Tensor<float, 1>;
using Tensor2 = Eigen::Tensor<float, 2>;
using Tensor3 = Eigen::Tensor<float, 3>;
constexpr int BATCH_SIZE = 64;
constexpr int SEQUENCE_LEN = 1000;
constexpr int HIDDEN_DIMS = 512;
constexpr int INPUT_DIMS = 512;
static cublasHandle_t g_blas_handle;
class ScopeTimer {
public:
ScopeTimer(const string& msg) : msg_(msg) {
cudaEventCreate(&start_);
cudaEventCreate(&stop_);
cudaDeviceSynchronize();
cudaEventRecord(start_);
}
~ScopeTimer() {
float elapsed_ms;
cudaEventRecord(stop_);
cudaEventSynchronize(stop_);
cudaEventElapsedTime(&elapsed_ms, start_, stop_);
printf("%s %fms\n", msg_.c_str(), elapsed_ms);
cudaEventDestroy(start_);
cudaEventDestroy(stop_);
}
private:
string msg_;
cudaEvent_t start_, stop_;
};
void GruInference(
const Tensor2& W,
const Tensor2& R,
const Tensor1& bx,
const Tensor1& br,
const Tensor3& x) {
const int time_steps = x.dimension(2);
const int batch_size = x.dimension(1);
const int input_size = x.dimension(0);
const int hidden_size = R.dimension(1);
// Copy weights over to GPU.
device_ptr<Tensor2> W_dev(W);
device_ptr<Tensor2> R_dev(R);
device_ptr<Tensor1> bx_dev(bx);
device_ptr<Tensor1> br_dev(br);
device_ptr<Tensor3> x_dev(x);
device_ptr<Tensor2> h_dev((time_steps + 1) * batch_size * hidden_size);
device_ptr<Tensor3> tmp_Wx_dev(time_steps * batch_size * hidden_size * 3);
device_ptr<Tensor2> tmp_Rh_dev(batch_size * hidden_size * 3);
h_dev.zero();
ScopeTimer t("Inference:");
ForwardPass<float> forward = ForwardPass<float>(
false, // training
batch_size,
input_size,
hidden_size,
g_blas_handle);
forward.Run(
time_steps,
W_dev.data,
R_dev.data,
bx_dev.data,
br_dev.data,
x_dev.data,
h_dev.data,
nullptr,
tmp_Wx_dev.data,
tmp_Rh_dev.data,
0.0f,
nullptr);
}
void GruTrain(
const Tensor2& W,
const Tensor2& R,
const Tensor1& bx,
const Tensor1& br,
const Tensor3& x,
const Tensor3& dh_new) {
const int time_steps = x.dimension(2);
const int batch_size = x.dimension(1);
const int input_size = x.dimension(0);
const int hidden_size = R.dimension(1);
// Copy weights over to GPU.
device_ptr<Tensor2> W_dev(W);
device_ptr<Tensor2> R_dev(R);
device_ptr<Tensor1> bx_dev(bx);
device_ptr<Tensor1> br_dev(br);
device_ptr<Tensor3> x_dev(x);
device_ptr<Tensor3> dh_new_dev(dh_new);
device_ptr<Tensor2> h_dev((time_steps + 1) * batch_size * hidden_size);
device_ptr<Tensor3> tmp_Wx_dev(time_steps * batch_size * hidden_size * 3);
device_ptr<Tensor2> tmp_Rh_dev(batch_size * hidden_size * 3);
device_ptr<Tensor3> v_dev(time_steps * batch_size * hidden_size * 4);
h_dev.zero();
{
ScopeTimer t("Train forward:");
ForwardPass<float> forward = ForwardPass<float>(
true, // training
batch_size,
input_size,
hidden_size,
g_blas_handle);
forward.Run(
time_steps,
W_dev.data,
R_dev.data,
bx_dev.data,
br_dev.data,
x_dev.data,
h_dev.data,
v_dev.data,
tmp_Wx_dev.data,
tmp_Rh_dev.data,
0.0f,
nullptr);
}
device_ptr<Tensor3> dx_dev(time_steps * batch_size * input_size);
device_ptr<Tensor2> dW_dev(input_size * hidden_size * 3);
device_ptr<Tensor2> dR_dev(hidden_size * hidden_size * 3);
device_ptr<Tensor1> dbx_dev(hidden_size * 3);
device_ptr<Tensor1> dbr_dev(hidden_size * 3);
device_ptr<Tensor2> dh_dev(batch_size * hidden_size);
device_ptr<Tensor3> dp_dev(time_steps * batch_size * hidden_size * 3);
device_ptr<Tensor3> dq_dev(time_steps * batch_size * hidden_size * 3);
{
ScopeTimer t("Train backward:");
BackwardPass<float> backward(
batch_size,
input_size,
hidden_size,
g_blas_handle);
backward.Run(
time_steps,
W_dev.data,
R_dev.data,
bx_dev.data,
br_dev.data,
x_dev.data,
h_dev.data,
v_dev.data,
dh_new_dev.data,
dx_dev.data,
dW_dev.data,
dR_dev.data,
dbx_dev.data,
dbr_dev.data,
dh_dev.data,
dp_dev.data,
dq_dev.data,
nullptr);
}
}
int main() {
srand(time(0));
cublasCreate(&g_blas_handle);
// Weights.
Tensor2 W(HIDDEN_DIMS * 3, INPUT_DIMS);
Tensor2 R(HIDDEN_DIMS * 3, HIDDEN_DIMS);
Tensor1 bx(HIDDEN_DIMS * 3);
Tensor1 br(HIDDEN_DIMS * 3);
// Input.
Tensor3 x(INPUT_DIMS, BATCH_SIZE, SEQUENCE_LEN);
// Gradients from upstream layers.
Tensor3 dh(HIDDEN_DIMS, BATCH_SIZE, SEQUENCE_LEN + 1);
W.setRandom();
R.setRandom();
bx.setRandom();
br.setRandom();
x.setRandom();
dh.setRandom();
GruInference(W, R, bx, br, x);
GruTrain(W, R, bx, br, x, dh);
cublasDestroy(g_blas_handle);
return 0;
}