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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/TemporalMaxPooling.c"
#else
static inline void THNN_(TemporalMaxPooling_shapeCheck)(
THNNState *state,
THTensor *input,
THTensor *gradOutput,
THIndexTensor *indices,
int kW,
int dW) {
long niframe;
long framesize;
long noframe;
int dimS = 0; // sequence dimension
int dimF = 1; // feature dimension
int ndims = input->nDimension;
if (input->nDimension == 3)
{
dimS = 1;
dimF = 2;
}
niframe = input->size[dimS];
framesize = input->size[dimF];
noframe = (niframe - kW) / dW + 1;
THArgCheck(kW > 0, 5,
"kernel size should be greater than zero, but got kW: %d", kW);
THArgCheck(dW > 0, 6,
"stride should be greater than zero, but got dW: %d", dW);
THNN_ARGCHECK(input->nDimension == 2 || input->nDimension == 3, 2, input,
"2D or 3D (batch mode) tensor expected for input, but got: %s");
THArgCheck(input->size[dimS] >= kW, 2,
"input sequence smaller than kernel size. Got: %d, Expected: %d",
input->size[dimS], kW);
if (gradOutput != NULL) {
THNN_CHECK_DIM_SIZE(gradOutput, ndims, dimS, noframe);
THNN_CHECK_DIM_SIZE(gradOutput, ndims, dimF, framesize)
}
if (indices != NULL) {
THNN_CHECK_DIM_SIZE_INDICES(indices, ndims, dimS, noframe);
THNN_CHECK_DIM_SIZE_INDICES(indices, ndims, dimF, framesize);
}
}
void THNN_(TemporalMaxPooling_updateOutput)(
THNNState *state,
THTensor *input,
THTensor *output,
THIndexTensor *indices,
int kW,
int dW)
{
long niframe;
long framesize;
long noframe;
real *input_data;
real *output_data;
THIndex_t *indices_data;
long t, y;
int dimS = 0; // sequence dimension
int dimF = 1; // feature dimension
THNN_(TemporalMaxPooling_shapeCheck)(state, input, NULL, NULL, kW, dW);
if (input->nDimension == 3)
{
dimS = 1;
dimF = 2;
}
/* sizes */
niframe = input->size[dimS];
framesize = input->size[dimF];
noframe = (niframe - kW) / dW + 1;
/* get contiguous input */
input = THTensor_(newContiguous)(input);
if (input->nDimension == 2)
{
/* resize output */
THTensor_(resize2d)(output, noframe, framesize);
/* indices will contain index locations for each output point */
THIndexTensor_(resize2d)(indices, noframe, framesize);
/* get raw pointers */
input_data = THTensor_(data)(input);
output_data = THTensor_(data)(output);
indices_data = THIndexTensor_(data)(indices);
for(t = 0; t < noframe; t++)
{
real *ip = input_data + t*framesize*dW;
real *op = output_data + t*framesize;
THIndex_t *xp = indices_data + t*framesize;
#pragma omp parallel for private(y)
for(y = 0; y < framesize; y++)
{
/* compute local max: */
long maxindex = -1;
real maxval = -THInf;
long x;
for(x = 0; x < kW; x++)
{
real val = ip[x*framesize+y];
if (val > maxval)
{
maxval = val;
maxindex = x;
}
}
/* set output to local max */
op[y] = maxval;
xp[y] = (real)maxindex;
}
}
}
else
{
/* number of batch frames */
long nbframe = input->size[0];
long i;
/* resize output */
THTensor_(resize3d)(output, nbframe, noframe, framesize);
/* indices will contain index locations for each output point */
THIndexTensor_(resize3d)(indices, nbframe, noframe, framesize);
/* get raw pointers */
input_data = THTensor_(data)(input);
output_data = THTensor_(data)(output);
indices_data = THIndexTensor_(data)(indices);
for(i = 0; i < nbframe; i++)
{
real *inputSample_data = input_data + i*niframe*framesize;
real *outputSample_data = output_data + i*noframe*framesize;
THIndex_t *indicesSample_data = indices_data + i*noframe*framesize;
for(t = 0; t < noframe; t++)
{
real *ip = inputSample_data + t*framesize*dW;
real *op = outputSample_data + t*framesize;
THIndex_t *xp = indicesSample_data + t*framesize;
#pragma omp parallel for private(y)
for(y = 0; y < framesize; y++)
{
/* compute local max: */
long maxindex = -1;
real maxval = -THInf;
long x;
for(x = 0; x < kW; x++)
{
real val = ip[x*framesize+y];
if (val > maxval)
{
maxval = val;
maxindex = x;
}
}
/* set output to local max */
op[y] = maxval;
xp[y] = (real)maxindex;
}
}
}
}
/* cleanup */
THTensor_(free)(input);
}
void THNN_(TemporalMaxPooling_updateGradInput)(
THNNState *state,
THTensor *input,
THTensor *gradOutput,
THTensor *gradInput,
THIndexTensor *indices,
int kW,
int dW)
{
long niframe;
int noframe;
long framesize;
real *gradInput_data;
real *gradOutput_data;
THIndex_t *indices_data;
long t, y;
THNN_(TemporalMaxPooling_shapeCheck)(state, input, gradOutput, indices, kW, dW);
/* get contiguous gradOutput */
gradOutput = THTensor_(newContiguous)(gradOutput);
/* resize and zero */
THTensor_(resizeAs)(gradInput, input);
THTensor_(zero)(gradInput);
int dimS = 0; // sequence dimension
int dimF = 1; // feature dimension
if (input->nDimension == 3)
{
dimS = 1;
dimF = 2;
}
/* sizes */
niframe = input->size[dimS];
noframe = gradOutput->size[dimS];
framesize = gradOutput->size[dimF];
/* get raw pointers */
gradInput_data = THTensor_(data)(gradInput);
gradOutput_data = THTensor_(data)(gradOutput);
indices_data = THIndexTensor_(data)(indices);
if (input->nDimension == 2)
{
for(t = 0; t < noframe; t++)
{
real *gip = gradInput_data + t*framesize*dW;
real *gop = gradOutput_data + t*framesize;
THIndex_t *xp = indices_data + t*framesize;
#pragma omp parallel for private(y)
for(y = 0; y < framesize; y++)
{
/* compute local max: */
long maxindex = (long)xp[y];
if (maxindex != -1)
gip[maxindex*framesize+y] += gop[y];
}
}
}
else
{
/* number of batch frames */
long nbframe = input->size[0];
long i;
for(i = 0; i < nbframe; i++)
{
real *gradInputSample_data = gradInput_data + i*niframe*framesize;
real *gradOutputSample_data = gradOutput_data + i*noframe*framesize;
THIndex_t *indicesSample_data = indices_data + i*noframe*framesize;
for(t = 0; t < noframe; t++)
{
real *gip = gradInputSample_data + t*framesize*dW;
real *gop = gradOutputSample_data + t*framesize;
THIndex_t *xp = indicesSample_data + t*framesize;
#pragma omp parallel for private(y)
for(y = 0; y < framesize; y++)
{
/* compute local max: */
long maxindex = (long)xp[y];
if (maxindex != -1)
gip[maxindex*framesize+y] += gop[y];
}
}
}
}
/* cleanup */
THTensor_(free)(gradOutput);
}
#endif
|