Skip to content

Commit b5a448f

Browse files
authored
Merge pull request #4154 from luotao1/avg_pool
refine avg-pooling, which is exclusive. refine related code.
2 parents d59295f + 780e268 commit b5a448f

File tree

6 files changed

+159
-188
lines changed

6 files changed

+159
-188
lines changed

paddle/cuda/include/hl_cuda_cudnn.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ limitations under the License. */
2222
*/
2323
typedef enum {
2424
HL_POOLING_MAX = 0,
25-
// average includes padded values
26-
HL_POOLING_AVERAGE = 1,
2725
// average does not include padded values
28-
HL_POOLING_AVERAGE_EXCLUDE_PADDING = 2,
26+
HL_POOLING_AVERAGE = 1,
27+
// average includes padded values
28+
HL_POOLING_AVERAGE_INCLUDE_PADDING = 2,
2929
HL_POOLING_END
3030
} hl_pooling_mode_t;
3131

paddle/cuda/src/hl_cuda_cnn.cu

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,11 @@ __global__ void KeAvgPoolForward(const int nthreads,
211211

212212
int hstart = ph * strideH - padH;
213213
int wstart = pw * strideW - padW;
214-
int hend = min(hstart + sizeY, height + padH);
215-
int wend = min(wstart + sizeX, width + padW);
216-
int pool_size = (hend - hstart) * (wend - wstart);
214+
int hend = min(hstart + sizeY, height);
215+
int wend = min(wstart + sizeX, width);
217216
hstart = max(hstart, 0);
218217
wstart = max(wstart, 0);
219-
hend = min(hend, height);
220-
wend = min(wend, width);
218+
int pool_size = (hend - hstart) * (wend - wstart);
221219

222220
real aveval = 0;
223221
inputData += (frameNum * channels + c) * height * width;
@@ -299,12 +297,14 @@ __global__ void KeAvgPoolBackward(const int nthreads,
299297
outGrad += (frameNum * outStride + offsetC * pooledH * pooledW);
300298

301299
for (int ph = phstart; ph < phend; ++ph) {
300+
int hstart = ph * strideH - 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
304-
int hstart = ph * strideH - padH;
305305
int wstart = pw * strideW - padW;
306-
int hend = min(hstart + sizeY, height + padH);
307-
int wend = min(wstart + sizeX, width + padW);
306+
int wend = min(wstart + sizeX, width);
307+
wstart = max(wstart, 0);
308308
int poolsize = (hend - hstart) * (wend - wstart);
309309
gradient += outGrad[ph * pooledW + pw] / poolsize;
310310
}
@@ -600,16 +600,13 @@ __global__ void KeAvgPool3DForward(const int nthreads,
600600
int dstart = pd * strideD - padD;
601601
int hstart = ph * strideH - padH;
602602
int wstart = pw * strideW - padW;
603-
int dend = min(dstart + sizeZ, depth + padD);
604-
int hend = min(hstart + sizeY, height + padH);
605-
int wend = min(wstart + sizeX, width + padW);
606-
int pool_size = (dend - dstart) * (hend - hstart) * (wend - wstart);
603+
int dend = min(dstart + sizeZ, depth);
604+
int hend = min(hstart + sizeY, height);
605+
int wend = min(wstart + sizeX, width);
607606
dstart = max(dstart, 0);
608607
hstart = max(hstart, 0);
609608
wstart = max(wstart, 0);
610-
dend = min(dend, depth);
611-
hend = min(hend, height);
612-
wend = min(wend, width);
609+
int pool_size = (dend - dstart) * (hend - hstart) * (wend - wstart);
613610

614611
real aveval = 0;
615612
inputData += (frameNum * channels + c) * depth * height * width;
@@ -712,15 +709,18 @@ __global__ void KeAvgPool3DBackward(const int nthreads,
712709
outGrad += (frameNum * channels + offsetC) * pooledD * pooledH * pooledW;
713710

714711
for (int pd = pdstart; pd < pdend; ++pd) {
712+
int dstart = pd * strideD - padD;
713+
int dend = min(dstart + sizeZ, depth);
714+
dstart = max(dstart, 0);
715715
for (int ph = phstart; ph < phend; ++ph) {
716+
int hstart = ph * strideH - padH;
717+
int hend = min(hstart + sizeY, height);
718+
hstart = max(hstart, 0);
716719
for (int pw = pwstart; pw < pwend; ++pw) {
717720
// figure out the pooling size
718-
int dstart = pd * strideD - padD;
719-
int hstart = ph * strideH - padH;
720721
int wstart = pw * strideW - padW;
721-
int dend = min(dstart + sizeZ, depth + padD);
722-
int hend = min(hstart + sizeY, height + padH);
723-
int wend = min(wstart + sizeX, width + padW);
722+
int wend = min(wstart + sizeX, width);
723+
wstart = max(wstart, 0);
724724
int poolsize = (dend - dstart) * (hend - hstart) * (wend - wstart);
725725
gradient += outGrad[(pd * pooledH + ph) * pooledW + pw] / poolsize;
726726
}

paddle/cuda/src/hl_cuda_cudnn.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,11 @@ void hl_create_pooling_descriptor(hl_pooling_descriptor* pooling_desc,
432432
cudnn_mode = CUDNN_POOLING_MAX;
433433
break;
434434
case HL_POOLING_AVERAGE:
435-
cudnn_mode = CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING;
436-
break;
437-
case HL_POOLING_AVERAGE_EXCLUDE_PADDING:
438435
cudnn_mode = CUDNN_POOLING_AVERAGE_COUNT_EXCLUDE_PADDING;
439436
break;
437+
case HL_POOLING_AVERAGE_INCLUDE_PADDING:
438+
cudnn_mode = CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING;
439+
break;
440440
default:
441441
LOG(FATAL) << "parameter mode error";
442442
}

paddle/gserver/layers/CudnnPoolLayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ bool CudnnPoolLayer::typeCheck(const std::string &poolType,
2929
if (mode) {
3030
*mode = HL_POOLING_AVERAGE;
3131
}
32-
} else if (poolType == "cudnn-avg-excl-pad-pool") {
32+
} else if (poolType == "cudnn-avg-incl-pad-pool") {
3333
if (mode) {
34-
*mode = HL_POOLING_AVERAGE_EXCLUDE_PADDING;
34+
*mode = HL_POOLING_AVERAGE_INCLUDE_PADDING;
3535
}
3636
} else {
3737
return false;

0 commit comments

Comments
 (0)