Skip to content

Commit 5f65ee9

Browse files
committed
Add more newContiguous calls and checks
1 parent 1feb120 commit 5f65ee9

14 files changed

+135
-89
lines changed

generic/PReLU.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ void THNN_(PReLU_updateGradInput)(
8282
{
8383
input = THTensor_(newContiguous)(input);
8484
gradOutput = THTensor_(newContiguous)(gradOutput);
85+
weight = THTensor_(newContiguous)(weight);
8586
const real *input_data = THTensor_(data)(input);
8687
const real *gradOutput_data = THTensor_(data)(gradOutput);
8788
const real *weight_data = THTensor_(data)(weight);
@@ -126,6 +127,7 @@ void THNN_(PReLU_updateGradInput)(
126127
}
127128
THTensor_(free)(input);
128129
THTensor_(free)(gradOutput);
130+
THTensor_(free)(weight);
129131
}
130132
}
131133

@@ -143,10 +145,10 @@ void THNN_(PReLU_accGradParameters)(
143145
{
144146
real scale = TH_CONVERT_ACCREAL_TO_REAL(scale_);
145147
THNN_CHECK_NELEMENT(input, gradOutput);
146-
real *gradWeight_data = THTensor_(data)(gradWeight);
147148

148149
if (nOutputPlane == 0)
149150
{
151+
real *gradWeight_data = THTensor_(data)(gradWeight);
150152
real sum = 0;
151153
TH_TENSOR_APPLY2(real, input, real, gradOutput,
152154
if ((*input_data) <= 0)
@@ -156,8 +158,10 @@ void THNN_(PReLU_accGradParameters)(
156158
}
157159
else
158160
{
161+
THArgCheck(THTensor_(isContiguous)(gradWeight), 6, "gradWeight needs to be contiguous");
159162
input = THTensor_(newContiguous)(input);
160163
gradOutput = THTensor_(newContiguous)(gradOutput);
164+
weight = THTensor_(newContiguous)(weight);
161165
long bs = 1, ks = 1;
162166
{
163167
long input_ndim = THTensor_(nDimension)(input);
@@ -196,6 +200,7 @@ void THNN_(PReLU_accGradParameters)(
196200
}
197201
THTensor_(free)(input);
198202
THTensor_(free)(gradOutput);
203+
THTensor_(free)(weight);
199204
}
200205
}
201206

generic/SparseLinear.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ void THNN_(SparseLinear_updateOutput)(
6262
THLongTensor * csr = THLongTensor_newWithSize1d(batchSize+1);
6363
THLongTensor_zero(csr);
6464

65+
weight = THTensor_(newContiguous)(weight);
66+
6567
//#pragma omp parallel for private(i, h, hp0, hp1) schedule(static) if (nnz > 10000)
6668
for (i=0; i<nnz; i++) {
6769
hp0 = (long)(THNN_(get2d)(input, i, 0)) - 1;
@@ -106,6 +108,7 @@ void THNN_(SparseLinear_updateOutput)(
106108
}
107109
THTensor_(free)(output_row);
108110
THLongTensor_free(csr);
111+
THTensor_(free)(weight);
109112
}
110113

111114
void THNN_(SparseLinear_legacyUpdateOutput)(
@@ -123,6 +126,8 @@ void THNN_(SparseLinear_legacyUpdateOutput)(
123126
THArgCheck(THTensor_(isContiguous)(output), 3, "output must be contiguous");
124127
THArgCheck(THNN_(checkSize1D)(bias, outDim), 5, "bias size wrong");
125128

129+
weight = THTensor_(newContiguous)(weight);
130+
126131
long batchSize = THTensor_(size)(input, 0);
127132
long nnz = THTensor_(size)(input, 1);
128133
THTensor_(resize2d)(output, batchSize, outDim);
@@ -157,6 +162,7 @@ void THNN_(SparseLinear_legacyUpdateOutput)(
157162
THTensor_(cadd)(output_row, bias, 1.0, output_row);
158163
}
159164
THTensor_(free)(output_row);
165+
THTensor_(free)(weight);
160166
}
161167

162168
void THNN_(SparseLinear_accGradParameters)(
@@ -189,6 +195,7 @@ void THNN_(SparseLinear_accGradParameters)(
189195

190196
THLongTensor* csc = THLongTensor_newWithSize1d(inDim+1);
191197
THLongTensor_zero(csc);
198+
weight = THTensor_(newContiguous)(weight);
192199

193200
#pragma omp parallel for private(i, h, hp0, hp1) schedule(static) if (nnz > 10000)
194201
for (i = 0; i < nnz; i++) {
@@ -235,6 +242,7 @@ void THNN_(SparseLinear_accGradParameters)(
235242
if (weightDecay != 0) {
236243
THTensor_(cadd)(gradWeight, gradWeight, weightDecay, weight);
237244
}
245+
THTensor_(free)(weight);
238246
}
239247

240248
void THNN_(SparseLinear_legacyAccGradParameters)(

generic/SpatialConvolutionLocal.c

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
#else
44

55
static inline void THNN_(SpatialConvolutionLocal_shapeCheck)(
6-
THTensor *input, THTensor *gradOutput,
7-
THTensor *weight, THTensor *bias,
8-
int kH, int kW, int dH,
9-
int dW, int padH, int padW,
10-
long inputHeight, long inputWidth,
11-
long outputHeight, long outputWidth) {
6+
THTensor *input, THTensor *gradOutput,
7+
THTensor *weight, THTensor *bias,
8+
int kH, int kW, int dH,
9+
int dW, int padH, int padW,
10+
long inputHeight, long inputWidth,
11+
long outputHeight, long outputWidth) {
1212

1313
THArgCheck(kW > 0 && kH > 0, 9,
14-
"kernel size should be greater than zero, but got kH: %d kW: %d", kH, kW);
14+
"kernel size should be greater than zero, but got kH: %d kW: %d", kH, kW);
1515
THArgCheck(dW > 0 && dH > 0, 11,
16-
"stride should be greater than zero, but got dH: %d dW: %d", dH, dW);
16+
"stride should be greater than zero, but got dH: %d dW: %d", dH, dW);
1717

1818
int ndim = input->nDimension;
1919
int dimf = 0;
@@ -27,7 +27,7 @@ static inline void THNN_(SpatialConvolutionLocal_shapeCheck)(
2727
}
2828

2929
THNN_ARGCHECK(ndim == 3 || ndim == 4, 2, input,
30-
"3D or 4D input tensor expected but got: %s");
30+
"3D or 4D input tensor expected but got: %s");
3131

3232
long nInputPlane = weight->size[2] / (kH * kW);
3333
long nOutputPlane = weight->size[1];
@@ -47,21 +47,22 @@ static inline void THNN_(SpatialConvolutionLocal_shapeCheck)(
4747
}
4848
}
4949

50-
static int THNN_(view_weight_local)(THTensor **_weight)
50+
static THTensor* THNN_(view_weight_local)(THTensor *_weight)
5151
{
52-
THTensor *weight = *_weight;
52+
THTensor *weight = THTensor_(newContiguous)(_weight);
5353
THArgCheck(weight->nDimension == 3 || weight->nDimension == 6, 4,
5454
"weight tensor should be 3D or 6D - got %dD", weight->nDimension);
5555
if (weight->nDimension == 6) {
5656
long s1 = weight->size[0] * weight->size[1];
5757
long s2 = weight->size[2];
5858
long s3 = weight->size[3] * weight->size[4] * weight->size[5];
59-
*_weight = THTensor_(newWithStorage3d)(weight->storage,
60-
weight->storageOffset,
61-
s1, -1, s2, -1, s3, -1);
62-
return 1;
59+
THTensor *old_weight = weight;
60+
weight = THTensor_(newWithStorage3d)(weight->storage,
61+
weight->storageOffset,
62+
s1, -1, s2, -1, s3, -1);
63+
THTensor_(free)(old_weight);
6364
}
64-
return 0;
65+
return weight;
6566
}
6667

6768
static void THNN_(SpatialConvolutionLocal_updateOutput_frame)
@@ -76,8 +77,8 @@ static void THNN_(SpatialConvolutionLocal_updateOutput_frame)
7677
THTensor *output3d, *finput3d;
7778

7879
THNN_(unfolded_copy)(finput, input, kW, kH, dW, dH, padW, padH,
79-
nInputPlane, inputWidth, inputHeight,
80-
outputWidth, outputHeight);
80+
nInputPlane, inputWidth, inputHeight,
81+
outputWidth, outputHeight);
8182

8283
THTensor_(copy)(output, bias);
8384

@@ -116,7 +117,7 @@ void THNN_(SpatialConvolutionLocal_updateOutput)(
116117
long inputWidth, long inputHeight,
117118
long outputWidth, long outputHeight)
118119
{
119-
int freeWeight = THNN_(view_weight_local)(&weight);
120+
weight = THNN_(view_weight_local)(weight);
120121

121122
THNN_(SpatialConvolutionLocal_shapeCheck)
122123
(input, NULL, weight, bias, kH, kW, dH, dW, padH, padW,
@@ -154,10 +155,10 @@ void THNN_(SpatialConvolutionLocal_updateOutput)(
154155
THTensor *finput_t = THTensor_(newSelect)(finput, 0, t);
155156

156157
THNN_(SpatialConvolutionLocal_updateOutput_frame)
157-
(input_t, output_t, weight, bias, finput_t,
158-
kW, kH, dW, dH, padW, padH,
159-
nInputPlane, inputWidth, inputHeight,
160-
nOutputPlane, outputWidth, outputHeight);
158+
(input_t, output_t, weight, bias, finput_t,
159+
kW, kH, dW, dH, padW, padH,
160+
nInputPlane, inputWidth, inputHeight,
161+
nOutputPlane, outputWidth, outputHeight);
161162

162163
THTensor_(free)(input_t);
163164
THTensor_(free)(output_t);
@@ -166,8 +167,7 @@ void THNN_(SpatialConvolutionLocal_updateOutput)(
166167
}
167168

168169
THTensor_(free)(input);
169-
if (freeWeight)
170-
THTensor_(free)(weight);
170+
THTensor_(free)(weight);
171171
}
172172

173173

@@ -198,8 +198,8 @@ static void THNN_(SpatialConvolutionLocal_updateGradInput_frame)
198198
THTensor_(zero)(gradInput);
199199

200200
THNN_(unfolded_acc)(fgradInput, gradInput, kW, kH, dW, dH, padW, padH,
201-
nInputPlane, inputWidth, inputHeight,
202-
outputWidth, outputHeight);
201+
nInputPlane, inputWidth, inputHeight,
202+
outputWidth, outputHeight);
203203

204204
}
205205

@@ -217,7 +217,7 @@ void THNN_(SpatialConvolutionLocal_updateGradInput)(
217217
long inputWidth, long inputHeight,
218218
long outputWidth, long outputHeight)
219219
{
220-
int freeWeight = THNN_(view_weight_local)(&weight);
220+
weight = THNN_(view_weight_local)(weight);
221221

222222
THNN_(SpatialConvolutionLocal_shapeCheck)
223223
(input, gradOutput, weight, NULL, kH, kW, dH, dW, padH, padW,
@@ -255,10 +255,10 @@ void THNN_(SpatialConvolutionLocal_updateGradInput)(
255255
THTensor *fgradInput_t = THTensor_(newSelect)(fgradInput, 0, t);
256256

257257
THNN_(SpatialConvolutionLocal_updateGradInput_frame)
258-
(gradInput_t, gradOutput_t, tweight, fgradInput_t,
259-
kW, kH, dW, dH, padW, padH,
260-
nInputPlane, inputWidth, inputHeight,
261-
nOutputPlane, outputWidth, outputHeight);
258+
(gradInput_t, gradOutput_t, tweight, fgradInput_t,
259+
kW, kH, dW, dH, padW, padH,
260+
nInputPlane, inputWidth, inputHeight,
261+
nOutputPlane, outputWidth, outputHeight);
262262

263263
THTensor_(free)(gradInput_t);
264264
THTensor_(free)(gradOutput_t);
@@ -269,9 +269,7 @@ void THNN_(SpatialConvolutionLocal_updateGradInput)(
269269
THTensor_(free)(tweight);
270270
THTensor_(free)(input);
271271
THTensor_(free)(gradOutput);
272-
if (freeWeight)
273-
THTensor_(free)(weight);
274-
272+
THTensor_(free)(weight);
275273
}
276274

277275
static void THNN_(SpatialConvolutionLocal_accGradParameters_frame)
@@ -317,8 +315,10 @@ void THNN_(SpatialConvolutionLocal_accGradParameters)(
317315
long outputWidth, long outputHeight,
318316
accreal scale_)
319317
{
318+
THArgCheck(THTensor_(isContiguous)(gradWeight), 4, "gradWeight needs to be contiguous");
319+
THArgCheck(THTensor_(isContiguous)(gradBias), 5, "gradBias needs to be contiguous");
320320
real scale = TH_CONVERT_ACCREAL_TO_REAL(scale_);
321-
int freeWeight = THNN_(view_weight_local)(&gradWeight);
321+
gradWeight = THNN_(view_weight_local)(gradWeight);
322322

323323
THNN_(SpatialConvolutionLocal_shapeCheck)
324324
(input, gradOutput, gradWeight, gradBias, kH, kW, dH, dW, padH, padW,
@@ -349,10 +349,10 @@ void THNN_(SpatialConvolutionLocal_accGradParameters)(
349349
THTensor *finput_t = THTensor_(newSelect)(finput, 0, t);
350350

351351
THNN_(SpatialConvolutionLocal_accGradParameters_frame)
352-
(gradOutput_t, gradWeight, gradBias, finput_t, scale,
353-
kW, kH, dW, dH, padW, padH,
354-
nInputPlane, inputWidth, inputHeight,
355-
nOutputPlane, outputWidth, outputHeight);
352+
(gradOutput_t, gradWeight, gradBias, finput_t, scale,
353+
kW, kH, dW, dH, padW, padH,
354+
nInputPlane, inputWidth, inputHeight,
355+
nOutputPlane, outputWidth, outputHeight);
356356

357357
THTensor_(free)(gradOutput_t);
358358
THTensor_(free)(finput_t);
@@ -361,10 +361,7 @@ void THNN_(SpatialConvolutionLocal_accGradParameters)(
361361

362362
THTensor_(free)(input);
363363
THTensor_(free)(gradOutput);
364-
365-
if (freeWeight)
366-
THTensor_(free)(gradWeight);
367-
364+
THTensor_(free)(gradWeight);
368365
}
369366

370367
#endif

generic/SpatialConvolutionMM.c

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ static inline void THNN_(SpatialConvolutionMM_shapeCheck)(
5353
}
5454
}
5555

56+
static THTensor* THNN_(view_weight_MM2d)(THTensor *weight) {
57+
weight = THTensor_(newContiguous)(weight);
58+
if (weight->nDimension == 4) {
59+
long s1 = weight->size[0];
60+
long s2 = weight->size[1] * weight->size[2] * weight->size[3];
61+
THTensor *old_weight = weight;
62+
weight = THTensor_(newWithStorage2d)(weight->storage, weight->storageOffset,
63+
s1, -1, s2, -1);
64+
THTensor_(free)(old_weight);
65+
}
66+
return weight;
67+
}
68+
5669
static void THNN_(SpatialConvolutionMM_updateOutput_frame)(
5770
THTensor *input,
5871
THTensor *output,
@@ -111,15 +124,7 @@ void THNN_(SpatialConvolutionMM_updateOutput)(
111124
int padW,
112125
int padH)
113126
{
114-
int freeWeight = 0;
115-
116-
if (weight->nDimension == 4) {
117-
long s1 = weight->size[0];
118-
long s2 = weight->size[1] * weight->size[2] * weight->size[3];
119-
weight = THTensor_(newWithStorage2d)(weight->storage, weight->storageOffset,
120-
s1, -1, s2, -1);
121-
freeWeight = 1;
122-
}
127+
weight = THNN_(view_weight_MM2d)(weight);
123128

124129
THNN_(SpatialConvolutionMM_shapeCheck)
125130
(input, NULL, weight, bias, kH, kW, dH, dW, padH, padW);
@@ -182,8 +187,7 @@ void THNN_(SpatialConvolutionMM_updateOutput)(
182187
}
183188

184189
THTensor_(free)(input);
185-
if (freeWeight)
186-
THTensor_(free)(weight);
190+
THTensor_(free)(weight);
187191
}
188192

189193
static void THNN_(SpatialConvolutionMM_updateGradInput_frame)(
@@ -228,15 +232,7 @@ void THNN_(SpatialConvolutionMM_updateGradInput)(
228232
int padW,
229233
int padH)
230234
{
231-
int freeWeight = 0;
232-
233-
if (weight->nDimension == 4) {
234-
long s1 = weight->size[0];
235-
long s2 = weight->size[1] * weight->size[2] * weight->size[3];
236-
weight = THTensor_(newWithStorage2d)(weight->storage, weight->storageOffset,
237-
s1, -1, s2, -1);
238-
freeWeight = 1;
239-
}
235+
weight = THNN_(view_weight_MM2d)(weight);
240236

241237
THNN_(SpatialConvolutionMM_shapeCheck)
242238
(input, gradOutput, weight, NULL, kH, kW, dH, dW, padH, padW);
@@ -285,8 +281,7 @@ void THNN_(SpatialConvolutionMM_updateGradInput)(
285281
THTensor_(free)(tweight);
286282
THTensor_(free)(input);
287283
THTensor_(free)(gradOutput);
288-
if (freeWeight)
289-
THTensor_(free)(weight);
284+
THTensor_(free)(weight);
290285
}
291286

292287
static void THNN_(SpatialConvolutionMM_accGradParameters_frame)(
@@ -338,17 +333,11 @@ void THNN_(SpatialConvolutionMM_accGradParameters)(
338333
int padH,
339334
accreal scale_)
340335
{
336+
THArgCheck(THTensor_(isContiguous)(gradWeight), 4, "gradWeight needs to be contiguous");
337+
THArgCheck(THTensor_(isContiguous)(gradBias), 5, "gradBias needs to be contiguous");
338+
341339
real scale = TH_CONVERT_ACCREAL_TO_REAL(scale_);
342-
int freeWeight = 0;
343-
344-
if (gradWeight->nDimension == 4) {
345-
long s1 = gradWeight->size[0];
346-
long s2 = gradWeight->size[1] * gradWeight->size[2] * gradWeight->size[3];
347-
gradWeight = THTensor_(newWithStorage2d)(gradWeight->storage,
348-
gradWeight->storageOffset,
349-
s1, -1, s2, -1);
350-
freeWeight = 1;
351-
}
340+
gradWeight = THNN_(view_weight_MM2d)(gradWeight);
352341

353342
THNN_(SpatialConvolutionMM_shapeCheck)
354343
(input, gradOutput, gradWeight, gradBias, kH, kW, dH, dW, padH, padW);
@@ -381,8 +370,7 @@ void THNN_(SpatialConvolutionMM_accGradParameters)(
381370

382371
THTensor_(free)(input);
383372
THTensor_(free)(gradOutput);
384-
if (freeWeight)
385-
THTensor_(free)(gradWeight);
373+
THTensor_(free)(gradWeight);
386374
}
387375

388376
#endif

0 commit comments

Comments
 (0)