forked from torch/cunn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSECriterion.cu
240 lines (185 loc) · 7.44 KB
/
MSECriterion.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
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
240
#include "utils.h"
#include <thrust/fill.h>
#include <thrust/functional.h>
#include <thrust/device_ptr.h>
#include <thrust/reduce.h>
#include <thrust/inner_product.h>
struct mse_functor
{
mse_functor() {}
__host__ __device__ float operator()(const float& x, const float& y) const
{
float z = x-y;
return z*z;
}
};
static int cunn_MSECriterion_updateOutput(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *target = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
THAssert(THCudaTensor_checkGPU(state, 2, input, target));
int sizeAverage = luaT_getfieldcheckboolean(L, 1, "sizeAverage");
luaL_argcheck(L, THCudaTensor_nElement(state, input) == THCudaTensor_nElement(state, target), 2,
"input and target need to have the same number of elements");
float sum;
long size = THCudaTensor_nElement(state, input);
input = THCudaTensor_newContiguous(state, input);
target = THCudaTensor_newContiguous(state, target);
thrust::device_ptr<float> input_data(THCudaTensor_data(state, input));
thrust::device_ptr<float> target_data(THCudaTensor_data(state, target));
sum = thrust::inner_product(input_data, input_data+size, target_data, (float) 0, thrust::plus<float>(), mse_functor());
if(sizeAverage)
sum /= size;
THCudaTensor_free(state, input);
THCudaTensor_free(state, target);
lua_pushnumber(L, sum);
lua_setfield(L, 1, "output");
lua_pushnumber(L, sum);
return 1;
}
struct mse_updateGradInput_functor
{
const float norm;
mse_updateGradInput_functor(float norm_) : norm(norm_) {}
__host__ __device__ float operator()(const float& x, const float& y) const
{
return norm * (x - y);
}
};
static int cunn_MSECriterion_updateGradInput(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *target = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
int sizeAverage = luaT_getfieldcheckboolean(L, 1, "sizeAverage");
THCudaTensor *gradInput = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "gradInput", "torch.CudaTensor");
luaL_argcheck(L, THCudaTensor_nElement(state, input) == THCudaTensor_nElement(state, target), 2,
"input and target need to have the same number of elements");
THAssert(THCudaTensor_checkGPU(state, 3, input, target, gradInput));
long size = THCudaTensor_nElement(state, input);
float norm = (sizeAverage ? 2./size : 2.);
input = THCudaTensor_newContiguous(state, input);
target = THCudaTensor_newContiguous(state, target);
THCudaTensor_resizeAs(state, gradInput, input);
thrust::device_ptr<float> input_data(THCudaTensor_data(state, input));
thrust::device_ptr<float> target_data(THCudaTensor_data(state, target));
thrust::device_ptr<float> gradInput_data(THCudaTensor_data(state, gradInput));
thrust::transform(input_data, input_data+size, target_data, gradInput_data, mse_updateGradInput_functor(norm));
THCudaTensor_free(state, input);
THCudaTensor_free(state, target);
return 1;
}
#define MSECRITERION_THREADS 128
__global__ void cunn_MSECriterion_updateOutput_kernel(float* output, float *input, float *target, int nframe, int dim, int sizeAverage)
{
__shared__ float buffer[MSECRITERION_THREADS];
int k = blockIdx.x;
float *input_k = input + k*dim;
float *target_k = target + k*dim;
int i_start = threadIdx.x;
int i_end = dim;
int i_step = blockDim.x;
// mse
buffer[threadIdx.x] = 0;
for (int i=i_start; i<i_end; i+=i_step)
{
float z = input_k[i] - target_k[i];
buffer[threadIdx.x] += z*z;
}
__syncthreads();
//reduce
if (threadIdx.x == 0)
{
*output = 0;
for (int i=0; i<blockDim.x; i++)
{
*output += buffer[i];
}
if (sizeAverage)
*output /= dim;
}
}
__global__ void cunn_MSECriterion_updateGradInput_kernel(float *gradInput, float *input, float *target, float norm, int nframe, int dim)
{
int k = blockIdx.x;
float *gradInput_k = gradInput + k*dim;
float *input_k = input + k*dim;
float *target_k = target + k*dim;
int i_start = threadIdx.x;
int i_end = dim;
int i_step = blockDim.x;
// gradInput
for (int i=i_start; i<i_end; i+=i_step)
gradInput_k[i] = norm*(input_k[i] - target_k[i]);
}
static int cunn_MSECriterion_updateOutput2(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *target = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
int sizeAverage = luaT_getfieldcheckboolean(L, 1, "sizeAverage");
long size = THCudaTensor_nElement(state, input);
input = THCudaTensor_newContiguous(state, input);
target = THCudaTensor_newContiguous(state, target);
THCudaStorage *output = THCudaStorage_newWithSize(state, 1);
dim3 blocks(1);
dim3 threads(MSECRITERION_THREADS);
cunn_MSECriterion_updateOutput_kernel<<<blocks,threads,
0, THCState_getCurrentStream(state)>>>(output->data,
THCudaTensor_data(state, input),
THCudaTensor_data(state, target),
1, size,
sizeAverage);
lua_pushnumber(L, THCudaStorage_get(state, output, 0));
cudaError errcode = cudaGetLastError();
if(errcode != cudaSuccess)
THError(cudaGetErrorString(errcode));
THCudaTensor_free(state, input);
THCudaTensor_free(state, target);
THCudaStorage_free(state, output);
lua_pushstring(L, "output");
lua_pushvalue(L, -2);
lua_rawset(L, 1);
return 1;
}
static int cunn_MSECriterion_updateGradInput2(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *target = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
int sizeAverage = luaT_getfieldcheckboolean(L, 1, "sizeAverage");
THCudaTensor *gradInput = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "gradInput", "torch.CudaTensor");
long size = THCudaTensor_nElement(state, input);
float norm = (sizeAverage ? 2./size : 2.);
input = THCudaTensor_newContiguous(state, input);
target = THCudaTensor_newContiguous(state, target);
THCudaTensor_resizeAs(state, gradInput, input);
dim3 blocks(1);
dim3 threads(MSECRITERION_THREADS);
cunn_MSECriterion_updateGradInput_kernel<<<blocks,threads,
0, THCState_getCurrentStream(state)>>>(THCudaTensor_data(state, gradInput),
THCudaTensor_data(state, input),
THCudaTensor_data(state, target),
norm,
1, size);
cudaError errcode = cudaGetLastError();
if(errcode != cudaSuccess)
THError(cudaGetErrorString(errcode));
THCudaTensor_free(state, input);
THCudaTensor_free(state, target);
return 1;
}
static const struct luaL_Reg cunn_MSECriterion__ [] = {
{"MSECriterion_updateOutput", cunn_MSECriterion_updateOutput},
{"MSECriterion_updateGradInput", cunn_MSECriterion_updateGradInput},
{"MSECriterion_updateOutput2", cunn_MSECriterion_updateOutput2},
{"MSECriterion_updateGradInput2", cunn_MSECriterion_updateGradInput2},
{NULL, NULL}
};
void cunn_MSECriterion_init(lua_State *L)
{
luaT_pushmetatable(L, "torch.CudaTensor");
luaT_registeratname(L, cunn_MSECriterion__, "nn");
lua_pop(L,1);
}