-
Notifications
You must be signed in to change notification settings - Fork 3
/
cuda_op_kernel.cu
194 lines (175 loc) · 5.88 KB
/
cuda_op_kernel.cu
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
#define EIGEN_USE_GPU
#include <cuda.h>
#include <stdio.h>
#define THREADS_PER_BLOCK 512
__global__ void DenseKernel(
const double* inputs,
const double* weights,
const double* biases,
const int batch_samples,
const int units,
const int input_feature_width,
double* output)
{
//for (int ix_sample = 0; ix_sample < batch_samples; ix_sample++)
//{
// for (int ix_unit = 0; ix_unit < units; ix_unit++)
// {
// output_tensor(ix_sample, ix_unit) = 0;
// for (int ix_input = 0; ix_input < input_feature_width; ix_input++)
// {
// output_tensor(ix_sample, ix_unit) += input_tensor(ix_sample, ix_input) * weights_tensor(ix_input, ix_unit );
// }
// output_tensor(ix_sample, ix_unit) += biases_tensor(0, ix_unit);
// }
//}
unsigned int ix = blockIdx.x * blockDim.x + threadIdx.x;
if(ix < units*batch_samples)
{
int ix_unit = ix % units ;
int ix_sample = ix / units;
output[ix] = 0.0;
for (int ix_input = 0; ix_input < input_feature_width; ix_input++)
{
output[ix] += inputs[ix_sample*input_feature_width+ix_input] * weights[ix_input*units+ix_unit];
}
output[ix] += biases[ix_unit];
}
}
void DenseKernelLauncher(
const double* inputs,
const double* weights,
const double* biases,
const int batch_samples,
const int units,
const int input_feature_width,
double* output)
{
DenseKernel<<<batch_samples,units>>>(inputs, weights, biases, batch_samples, units, input_feature_width, output);
cudaError_t cudaerr = cudaDeviceSynchronize();
if (cudaerr != cudaSuccess)
{
printf("kernel launch failed with error \"%s\".\n", cudaGetErrorString(cudaerr));
}
}
/*
for (int ix_sample = 0; ix_sample < batch_samples; ix_sample++) {
for (int ix_unit = 0; ix_unit < units; ix_unit++) {
//output_tensor(ix_sample, ix_unit) = 0;
for (int ix_input = 0; ix_input < input_feature_width; ix_input++) {
//!!!output_tensor(ix_sample, ix_unit) += input_tensor(ix_sample, ix_input) * weights_tensor(ix_input, ix_unit );
grad_input_tensor(ix_sample, ix_input) += weights_tensor(ix_input, ix_unit )*grad_tensor(ix_sample, ix_unit);
grad_weights_tensor(ix_input, ix_unit ) += input_tensor(ix_sample, ix_input)*grad_tensor(ix_sample, ix_unit);
}
//!!!output_tensor(ix_sample, ix_unit) += biases_tensor(0, ix_unit);
grad_biases_tensor(0, ix_unit) += grad_tensor(ix_sample, ix_unit);
}
}
*/
// Input gradient
__global__ void InputKernel(
const double* grads,
const double* weights,
const int input_feature_width,
const int batch_samples,
const int units,
double* grad_inputs)
{
unsigned int ix = blockIdx.x * blockDim.x + threadIdx.x;
if(ix < batch_samples*input_feature_width)
{
int ix_input = ix % input_feature_width;
int ix_sample = ix / input_feature_width ;
grad_inputs[ix] = 0.0;
//sample //unit //input
for (int ix_unit = 0; ix_unit < units; ix_unit++)
{
grad_inputs[ix_sample*input_feature_width+ix_input] += weights[ix_input*units+ ix_unit]*grads[ix_sample*units+ix_unit];
}
}
}
void InputGradKernelLauncher(
const double* grads,
const double* weights,
const int input_feature_width,
const int batch_samples,
const int units,
double* grad_inputs)
{
InputKernel<<<batch_samples,input_feature_width>>>(grads, weights, input_feature_width, batch_samples, units, grad_inputs);
cudaError_t cudaerr = cudaDeviceSynchronize();
if (cudaerr != cudaSuccess)
{
printf("kernel launch failed with error \"%s\".\n", cudaGetErrorString(cudaerr));
}
}
// Weights gradient
__global__ void WeightsKernel(
const double* grads,
const double* inputs,
const int input_feature_width,
const int batch_samples,
const int units,
double* grad_weights)
{
unsigned int ix = blockIdx.x * blockDim.x + threadIdx.x;
if(ix < units*input_feature_width)
{
int ix_unit = ix % units ;
int ix_input = ix / units;
grad_weights[ix] = 0.0;
//sample //unit //input
for (int ix_sample = 0; ix_sample < batch_samples; ix_sample++)
{
grad_weights[ix] += inputs[input_feature_width*ix_sample+ix_input]*grads[ix_sample*units+ix_unit];
}
}
}
void WeightsGradKernelLauncher(
const double* grads,
const double* inputs,
const int input_feature_width,
const int batch_samples,
const int units,
double* grad_weights)
{
WeightsKernel<<<units,input_feature_width>>>(grads, inputs, input_feature_width, batch_samples, units, grad_weights);
cudaError_t cudaerr = cudaDeviceSynchronize();
if (cudaerr != cudaSuccess)
{
printf("kernel launch failed with error \"%s\".\n", cudaGetErrorString(cudaerr));
}
}
// Bias gradient
__global__ void BiasesKernel(
const double* grads,
const int input_feature_width,
const int batch_samples,
const int units,
double* grad_biases)
{
unsigned int ix = blockIdx.x * blockDim.x + threadIdx.x;
if(ix < units)
{
int ix_unit = ix;
grad_biases[ix_unit] = 0.0;
for (int ix_sample = 0; ix_sample < batch_samples; ix_sample++)
{
grad_biases[ix] += grads[ix_sample*units+ix_unit];
}
}
}
void BiasesGradKernelLauncher(
const double* grads,
const int input_feature_width,
const int batch_samples,
const int units,
double* grad_biases)
{
BiasesKernel<<<1,units>>>(grads, input_feature_width, batch_samples, units, grad_biases);
cudaError_t cudaerr = cudaDeviceSynchronize();
if (cudaerr != cudaSuccess)
{
printf("kernel launch failed with error \"%s\".\n", cudaGetErrorString(cudaerr));
}
}