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
|
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/SpatialConvolutionMap.c"
#else
static int nn_(SpatialConvolutionMap_updateOutput)(lua_State *L)
{
THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
int kW = luaT_getfieldcheckint(L, 1, "kW");
int kH = luaT_getfieldcheckint(L, 1, "kH");
int dW = luaT_getfieldcheckint(L, 1, "dW");
int dH = luaT_getfieldcheckint(L, 1, "dH");
int nInputPlane = luaT_getfieldcheckint(L, 1, "nInputPlane");
int nOutputPlane = luaT_getfieldcheckint(L, 1, "nOutputPlane");
THTensor *connTable = luaT_getfieldcheckudata(L, 1, "connTable", torch_Tensor);
THTensor *weight = luaT_getfieldcheckudata(L, 1, "weight", torch_Tensor);
THTensor *bias = luaT_getfieldcheckudata(L, 1, "bias", torch_Tensor);
THTensor *output = luaT_getfieldcheckudata(L, 1, "output", torch_Tensor);
luaL_argcheck(L, input->nDimension == 3, 2, "3D tensor expected");
luaL_argcheck(L, input->size[0] == nInputPlane, 2, "invalid number of input planes");
luaL_argcheck(L, input->size[2] >= kW && input->size[1] >= kH, 2, "input image smaller than kernel size");
THTensor_(resize3d)(output, nOutputPlane,
(input->size[1] - kH) / dH + 1,
(input->size[2] - kW) / dW + 1);
// contiguous
input = THTensor_(newContiguous)(input);
output = THTensor_(newContiguous)(output);
// get raw pointers
real *input_data = THTensor_(data)(input);
real *output_data = THTensor_(data)(output);
real *weight_data = THTensor_(data)(weight);
real *bias_data = THTensor_(data)(bias);
real *connTable_data = THTensor_(data)(connTable);
// and dims
long input_h = input->size[1];
long input_w = input->size[2];
long output_h = output->size[1];
long output_w = output->size[2];
long weight_h = weight->size[1];
long weight_w = weight->size[2];
long p;
#pragma omp parallel for private(p)
for (p = 0; p < nOutputPlane; p++) {
// add bias
real *ptr_output = output_data + p*output_w*output_h;
long j;
for(j = 0; j < output_h*output_w; j++)
ptr_output[j] = bias_data[p];
// convolve all maps
int nweight = connTable->size[0];
long k;
for (k = 0; k < nweight; k++) {
// get offsets for input/output
int o = (int)connTable_data[k*2+1]-1;
int i = (int)connTable_data[k*2+0]-1;
if (o == p)
{
THTensor_(validXCorr2Dptr)(output_data + o*output_w*output_h,
1.0,
input_data + i*input_w*input_h, input_h, input_w,
weight_data + k*weight_w*weight_h, weight_h, weight_w,
dH, dW);
}
}
}
// clean up
THTensor_(free)(input);
THTensor_(free)(output);
return 1;
}
static int nn_(SpatialConvolutionMap_updateGradInput)(lua_State *L)
{
THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
THTensor *gradOutput = luaT_checkudata(L, 3, torch_Tensor);
int dW = luaT_getfieldcheckint(L, 1, "dW");
int dH = luaT_getfieldcheckint(L, 1, "dH");
int nInputPlane = luaT_getfieldcheckint(L, 1, "nInputPlane");
THTensor *connTable = luaT_getfieldcheckudata(L, 1, "connTable", torch_Tensor);
THTensor *weight = luaT_getfieldcheckudata(L, 1, "weight", torch_Tensor);
THTensor *gradInput = luaT_getfieldcheckudata(L, 1, "gradInput", torch_Tensor);
// contiguous
gradInput = THTensor_(newContiguous)(gradInput);
gradOutput = THTensor_(newContiguous)(gradOutput);
// Resize/Zero
THTensor_(resizeAs)(gradInput, input);
THTensor_(zero)(gradInput);
// get raw pointers
real *gradInput_data = THTensor_(data)(gradInput);
real *gradOutput_data = THTensor_(data)(gradOutput);
real *weight_data = THTensor_(data)(weight);
real *connTable_data = THTensor_(data)(connTable);
// and dims
long input_h = input->size[1];
long input_w = input->size[2];
long output_h = gradOutput->size[1];
long output_w = gradOutput->size[2];
long weight_h = weight->size[1];
long weight_w = weight->size[2];
long p;
#pragma omp parallel for private(p)
for(p = 0; p < nInputPlane; p++)
{
long k;
// backward all
int nkernel = connTable->size[0];
for(k = 0; k < nkernel; k++)
{
int o = (int)connTable_data[k*2+1]-1;
int i = (int)connTable_data[k*2+0]-1;
if (i == p)
{
// gradient to input
THTensor_(fullConv2Dptr)(gradInput_data + i*input_w*input_h,
1.0,
gradOutput_data + o*output_w*output_h, output_h, output_w,
weight_data + k*weight_w*weight_h, weight_h, weight_w,
dH, dW);
}
}
}
// clean up
THTensor_(free)(gradInput);
THTensor_(free)(gradOutput);
return 1;
}
static int nn_(SpatialConvolutionMap_accGradParameters)(lua_State *L)
{
THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
THTensor *gradOutput = luaT_checkudata(L, 3, torch_Tensor);
int dW = luaT_getfieldcheckint(L, 1, "dW");
int dH = luaT_getfieldcheckint(L, 1, "dH");
int nOutputPlane = luaT_getfieldcheckint(L, 1, "nOutputPlane");
real scale = luaL_optnumber(L, 4, 1);
THTensor *connTable = luaT_getfieldcheckudata(L, 1, "connTable", torch_Tensor);
THTensor *weight = luaT_getfieldcheckudata(L, 1, "weight", torch_Tensor);
THTensor *gradWeight = luaT_getfieldcheckudata(L, 1, "gradWeight", torch_Tensor);
THTensor *gradBias = luaT_getfieldcheckudata(L, 1, "gradBias", torch_Tensor);
// contiguous
input = THTensor_(newContiguous)(input);
gradOutput = THTensor_(newContiguous)(gradOutput);
// get raw pointers
real *input_data = THTensor_(data)(input);
real *gradOutput_data = THTensor_(data)(gradOutput);
real *gradWeight_data = THTensor_(data)(gradWeight);
real *gradBias_data = THTensor_(data)(gradBias);
// and dims
long input_h = input->size[1];
long input_w = input->size[2];
long output_h = gradOutput->size[1];
long output_w = gradOutput->size[2];
long weight_h = weight->size[1];
long weight_w = weight->size[2];
// gradients wrt bias
long k;
#pragma omp parallel for private(k)
for(k = 0; k < nOutputPlane; k++) {
real *ptr_gradOutput = gradOutput_data + k*output_w*output_h;
long l;
for(l = 0; l < output_h*output_w; l++)
gradBias_data[k] += scale*ptr_gradOutput[l];
}
// gradients wrt weight
int nkernel = connTable->size[0];
#pragma omp parallel for private(k)
for(k = 0; k < nkernel; k++)
{
int o = (int)THTensor_(get2d)(connTable,k,1)-1;
int i = (int)THTensor_(get2d)(connTable,k,0)-1;
// gradient to kernel
THTensor_(validXCorr2DRevptr)(gradWeight_data + k*weight_w*weight_h,
scale,
input_data + i*input_w*input_h, input_h, input_w,
gradOutput_data + o*output_w*output_h, output_h, output_w,
dH, dW);
}
// clean up
THTensor_(free)(input);
THTensor_(free)(gradOutput);
return 0;
}
static const struct luaL_Reg nn_(SpatialConvolutionMap__) [] = {
{"SpatialConvolutionMap_updateOutput", nn_(SpatialConvolutionMap_updateOutput)},
{"SpatialConvolutionMap_updateGradInput", nn_(SpatialConvolutionMap_updateGradInput)},
{"SpatialConvolutionMap_accGradParameters", nn_(SpatialConvolutionMap_accGradParameters)},
{NULL, NULL}
};
static void nn_(SpatialConvolutionMap_init)(lua_State *L)
{
luaT_pushmetatable(L, torch_Tensor);
luaT_registeratname(L, nn_(SpatialConvolutionMap__), "nn");
lua_pop(L,1);
}
#endif
|