Skip to content

Commit 73da972

Browse files
committed
fix AvgPoolBackward and using CaffeMode in test_AvgPoolFwdBwd
1 parent 8067a42 commit 73da972

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

paddle/cuda/src/hl_cuda_cnn.cu

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,13 @@ __global__ void KeAvgPoolBackward(const int nthreads,
298298

299299
for (int ph = phstart; ph < phend; ++ph) {
300300
int hstart = ph * strideH - padH;
301-
int hend = min(hstart + sizeY, height + padH);
301+
int hend = min(hstart + sizeY, height);
302+
hstart = max(hstart, 0);
302303
for (int pw = pwstart; pw < pwend; ++pw) {
303304
// figure out the pooling size
304305
int wstart = pw * strideW - padW;
305-
int wend = min(wstart + sizeX, width + padW);
306+
int wend = min(wstart + sizeX, width);
307+
wstart = max(wstart, 0);
306308
int poolsize = (hend - hstart) * (wend - wstart);
307309
gradient += outGrad[ph * pooledW + pw] / poolsize;
308310
}
@@ -708,14 +710,17 @@ __global__ void KeAvgPool3DBackward(const int nthreads,
708710

709711
for (int pd = pdstart; pd < pdend; ++pd) {
710712
int dstart = pd * strideD - padD;
711-
int dend = min(dstart + sizeZ, depth + padD);
713+
int dend = min(dstart + sizeZ, depth);
714+
dstart = max(dstart, 0);
712715
for (int ph = phstart; ph < phend; ++ph) {
713716
int hstart = ph * strideH - padH;
714-
int hend = min(hstart + sizeY, height + padH);
717+
int hend = min(hstart + sizeY, height);
718+
hstart = max(hstart, 0);
715719
for (int pw = pwstart; pw < pwend; ++pw) {
716720
// figure out the pooling size
717721
int wstart = pw * strideW - padW;
718-
int wend = min(wstart + sizeX, width + padW);
722+
int wend = min(wstart + sizeX, width);
723+
wstart = max(wstart, 0);
719724
int poolsize = (dend - dstart) * (hend - hstart) * (wend - wstart);
720725
gradient += outGrad[(pd * pooledH + ph) * pooledW + pw] / poolsize;
721726
}

paddle/math/tests/test_matrixCompare.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,8 @@ void testMaxPoolFwdBwd(int numSamples,
825825
int strideW,
826826
int padH,
827827
int padW) {
828-
int outH = 0, outW = 0;
829-
outH = (imgSizeH - ksizeH + 2 * padH + strideH - 1) / strideH + 1;
830-
outW = (imgSizeW - ksizeW + 2 * padW + strideW - 1) / strideW + 1;
828+
int outH = outputSize(imgSizeH, ksizeH, padH, strideH, true);
829+
int outW = outputSize(imgSizeW, ksizeW, padW, strideW, true);
831830

832831
int inWidth = imgSizeH * imgSizeW * channels;
833832
MatrixPtr input = CpuMatrix::create(numSamples, inWidth, false, false);
@@ -927,9 +926,8 @@ void testAvgPoolFwdBwd(int numSamples,
927926
int strideW,
928927
int padH,
929928
int padW) {
930-
int outH = 0, outW = 0;
931-
outH = (imgSizeH - ksizeH + 2 * padH + strideH - 1) / strideH + 1;
932-
outW = (imgSizeW - ksizeW + 2 * padW + strideW - 1) / strideW + 1;
929+
int outH = outputSize(imgSizeH, ksizeH, padH, strideH, true);
930+
int outW = outputSize(imgSizeW, ksizeW, padW, strideW, true);
933931

934932
int inWidth = imgSizeH * imgSizeW * channels;
935933
MatrixPtr input = CpuMatrix::create(numSamples, inWidth, false, false);

0 commit comments

Comments
 (0)