Skip to content

Commit e802471

Browse files
authored
abstract outputSize function in CNN-related layers (#314)
1 parent f9849ac commit e802471

File tree

11 files changed

+139
-184
lines changed

11 files changed

+139
-184
lines changed

paddle/gserver/layers/ConvBaseLayer.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
1615
#include "paddle/utils/Logging.h"
1716
#include "ConvBaseLayer.h"
1817
namespace paddle {
@@ -78,10 +77,10 @@ size_t ConvBaseLayer::calOutputSize() {
7877
imgSizeH_[i] = config_.inputs(i).conv_conf().img_size();
7978
if (imgSizeW_[i] == 0)
8079
imgSizeW_[i] = config_.inputs(i).conv_conf().img_size();
81-
outputH_.push_back(
82-
outputSize(imgSizeH_[i], filterSizeY_[i], paddingY_[i], strideY_[i]));
83-
outputW_.push_back(
84-
outputSize(imgSizeW_[i], filterSize_[i], padding_[i], stride_[i]));
80+
outputH_.push_back(outputSize(imgSizeH_[i], filterSizeY_[i], paddingY_[i],
81+
strideY_[i], caffeMode_));
82+
outputW_.push_back(outputSize(imgSizeW_[i], filterSize_[i], padding_[i],
83+
stride_[i], caffeMode_));
8584
CHECK_EQ(outputH_[i], outputH_[0]);
8685
CHECK_EQ(outputW_[i], outputW_[0]);
8786
}

paddle/gserver/layers/ConvBaseLayer.h

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License. */
1616
#pragma once
1717

1818
#include "Layer.h"
19+
#include "paddle/math/MathUtils.h"
1920
namespace paddle {
2021

2122
/**
@@ -87,31 +88,6 @@ class ConvBaseLayer : public Layer {
8788
virtual size_t calOutputSize();
8889

8990
Weight& getWeight(int idx) { return *weights_[idx]; }
90-
91-
/**
92-
* Calculate output size based on caffeMode_.
93-
* - input(+padding): 0123456789
94-
* - imageSize(+padding) = 10;
95-
* - filterSize = 3;
96-
* - stride = 2;
97-
* - caffeMode_ is true:
98-
- output: (012), (234), (456), (678)
99-
- outputSize = 4;
100-
* - caffeMode_ is false:
101-
* - output: (012), (234), (456), (678), (9)
102-
* - outputSize = 5;
103-
*/
104-
int outputSize(int imageSize, int filterSize, int padding, int stride) {
105-
int outputSize;
106-
if (!caffeMode_) {
107-
outputSize =
108-
(imageSize - filterSize + 2 * padding + stride - 1) / stride + 1;
109-
} else {
110-
outputSize = (imageSize - filterSize + 2 * padding) / stride + 1;
111-
}
112-
CHECK_GE(outputSize, 1);
113-
return outputSize;
114-
}
11591
};
11692

11793
} // namespace paddle

paddle/gserver/layers/ConvOperator.cpp

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
1615
#include "paddle/math/Matrix.h"
16+
#include "paddle/math/MathUtils.h"
1717
#include "Operator.h"
1818

1919
namespace paddle {
@@ -35,8 +35,8 @@ class ConvOperator : public Operator {
3535
*/
3636
virtual ~ConvOperator() {
3737
if (workSpaceInBytes_ != 0) {
38-
hl_free_mem_device(workSpace_);
39-
workSpaceInBytes_ = 0;
38+
hl_free_mem_device(workSpace_);
39+
workSpaceInBytes_ = 0;
4040
}
4141

4242
hl_destroy_tensor_descriptor(inputDesc_);
@@ -83,33 +83,6 @@ class ConvOperator : public Operator {
8383
filterSize_ * filterSizeY_ * channels_ * numFilters_);
8484
}
8585

86-
/**
87-
* Calculate output size.
88-
*/
89-
int outputSize(int imageSize, int filterSize, int padding, int stride) {
90-
int outputSize;
91-
if (!caffeMode_) {
92-
/* input(+padding): 0123456789
93-
* imageSize(+padding) = 10;
94-
* filterSize = 3;
95-
* stride = 2;
96-
* output: (012), (234), (456), (678), (9)
97-
* outputSize = 5;
98-
*/
99-
outputSize =
100-
(imageSize - filterSize + 2 * padding + stride - 1) / stride + 1;
101-
} else {
102-
/* input(+padding): 0123456789
103-
* imageSize(+padding) = 10;
104-
* filterSize = 3;
105-
* stride = 2;
106-
* output: (012), (234), (456), (678)
107-
* outputSize = 4;
108-
*/
109-
outputSize = (imageSize - filterSize + 2 * padding) / stride + 1;
110-
}
111-
return outputSize;
112-
}
11386
/// Most of member variables are same with CudnnConvLayer.
11487
/// There is no explanation here.
11588
int imageH_, imageW_, outputH_, outputW_;
@@ -129,7 +102,7 @@ class ConvOperator : public Operator {
129102
int fwdAlgo_, bwdFilterAlgo_, bwdDataAlgo_;
130103
size_t fwdLimitBytes_, bwdDataLimitBytes_, bwdFilterLimitBytes_;
131104
size_t workSpaceInBytes_;
132-
void* workSpace_;
105+
void *workSpace_;
133106
bool isSelectAlgo_;
134107
};
135108

@@ -160,33 +133,32 @@ ConvOperator::ConvOperator(const OperatorConfig &config, bool useGpu)
160133
void ConvOperator::allocConvWorkSpace(size_t maxWorkSpace) {
161134
if (maxWorkSpace > workSpaceInBytes_) {
162135
if (workSpaceInBytes_ != 0) {
163-
hl_free_mem_device(workSpace_);
136+
hl_free_mem_device(workSpace_);
164137
}
165138
// total amount of storage needed
166139
workSpace_ = hl_malloc_device(maxWorkSpace);
167140
workSpaceInBytes_ = maxWorkSpace;
168141
}
169142
}
170143

171-
172144
void ConvOperator::reshape(int batchSize) {
173145
imageH_ = ins_[0]->getFrameHeight();
174146
imageW_ = ins_[0]->getFrameWidth();
175147
if (imageH_ == 0) imageH_ = imgSize_;
176148
if (imageW_ == 0) imageW_ = imgSize_;
177-
outputH_ = outputSize(imageH_, filterSizeY_, paddingY_, strideY_);
178-
outputW_ = outputSize(imageW_, filterSize_, padding_, stride_);
149+
outputH_ = outputSize(imageH_, filterSizeY_, paddingY_, strideY_, caffeMode_);
150+
outputW_ = outputSize(imageW_, filterSize_, padding_, stride_, caffeMode_);
179151

180152
out_->setFrameHeight(outputH_);
181153
out_->setFrameWidth(outputW_);
182154

183155
reshapeImageDescriptors();
184156

185157
if (!isSelectAlgo_) {
186-
hl_conv_workspace(inputDesc_, outputDesc_, filterDesc_,
187-
convDesc_, &fwdAlgo_, &fwdLimitBytes_,
188-
&bwdDataAlgo_, &bwdDataLimitBytes_,
189-
&bwdFilterAlgo_, &bwdFilterLimitBytes_);
158+
hl_conv_workspace(inputDesc_, outputDesc_, filterDesc_, convDesc_,
159+
&fwdAlgo_, &fwdLimitBytes_, &bwdDataAlgo_,
160+
&bwdDataLimitBytes_, &bwdFilterAlgo_,
161+
&bwdFilterLimitBytes_);
190162

191163
size_t maxWorkSpace = 0;
192164
maxWorkSpace = std::max(fwdLimitBytes_, bwdDataLimitBytes_);
@@ -202,7 +174,8 @@ void ConvOperator::computeConvSizes() {
202174
hl_create_filter_descriptor(&filterDesc_, channels_, numFilters_,
203175
filterSizeY_, filterSize_);
204176
hl_create_tensor_descriptor(&inputDesc_);
205-
int outputX = outputSize(imgSize_, filterSize_, padding_, stride_);
177+
int outputX =
178+
outputSize(imgSize_, filterSize_, padding_, stride_, caffeMode_);
206179
CHECK_EQ(outputX, outputX_);
207180
hl_create_tensor_descriptor(&outputDesc_);
208181
hl_create_convolution_descriptor(&convDesc_, inputDesc_, filterDesc_,
@@ -211,13 +184,13 @@ void ConvOperator::computeConvSizes() {
211184

212185
void ConvOperator::reshapeImageDescriptors() {
213186
hl_tensor_reshape(inputDesc_, 1, channels_, imageH_, imageW_,
214-
channels_ * imageH_ * imageW_, imageH_ * imageW_,
215-
imageW_, 1);
187+
channels_ * imageH_ * imageW_, imageH_ * imageW_, imageW_,
188+
1);
216189
hl_tensor_reshape(outputDesc_, 1, numFilters_, outputH_, outputW_,
217190
numFilters_ * outputH_ * outputW_, outputH_ * outputW_,
218191
outputW_, 1);
219-
hl_reset_convolution_descriptor(convDesc_, inputDesc_, filterDesc_,
220-
paddingY_, padding_, strideY_, stride_);
192+
hl_reset_convolution_descriptor(convDesc_, inputDesc_, filterDesc_, paddingY_,
193+
padding_, strideY_, stride_);
221194
inputOffset_ = channels_ * imageH_ * imageW_;
222195
outputOffset_ = numFilters_ * outputH_ * outputW_;
223196
weightOffset_ = numFilters_ * channels_ * filterSize_ * filterSize_;
@@ -273,18 +246,17 @@ void ConvOperator::backward() {
273246
real *weightGrad = ins_[1]->grad->getData() + weightOffset_ * batchId;
274247
hl_convolution_backward_filter(inputDesc_, inputData, outputDesc_,
275248
outGrad, filterDesc_, weightGrad,
276-
convDesc_, workSpace_,
277-
workSpaceInBytes_, bwdFilterAlgo_);
249+
convDesc_, workSpace_, workSpaceInBytes_,
250+
bwdFilterAlgo_);
278251
}
279252

280253
MatrixPtr preGrad = ins_[0]->grad;
281254
if (NULL != preGrad) {
282255
real *inputGrad = preGrad->getData() + inputOffset_ * batchId;
283256
real *wgtData = ins_[1]->value->getData() + weightOffset_ * batchId;
284-
hl_convolution_backward_data(inputDesc_, inputGrad, outputDesc_,
285-
outGrad, filterDesc_, wgtData,
286-
convDesc_, workSpace_,
287-
workSpaceInBytes_, bwdDataAlgo_);
257+
hl_convolution_backward_data(
258+
inputDesc_, inputGrad, outputDesc_, outGrad, filterDesc_, wgtData,
259+
convDesc_, workSpace_, workSpaceInBytes_, bwdDataAlgo_);
288260
}
289261
}
290262
}

paddle/gserver/layers/ConvProjection.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
1615
#pragma once
1716

1817
#include "Projection.h"
18+
#include "paddle/math/MathUtils.h"
1919

2020
namespace paddle {
2121

@@ -42,17 +42,15 @@ class ConvProjection : public Projection {
4242
void reshapeTensorDesc(int batchSize);
4343
void reshape(int batchSize);
4444

45-
int outputSize(int imageSize, int filterSize, int padding, int stride) {
46-
return (imageSize - filterSize + 2 * padding) / stride + 1;
47-
}
48-
4945
size_t calOutputSize() {
5046
imageH_ = in_->getFrameHeight();
5147
imageW_ = in_->getFrameWidth();
5248
if (imageH_ == 0) imageH_ = configImgH_;
5349
if (imageW_ == 0) imageW_ = configImgW_;
54-
outputH_ = outputSize(imageH_, filterH_, paddingH_, strideH_);
55-
outputW_ = outputSize(imageW_, filterW_, paddingW_, strideW_);
50+
outputH_ = outputSize(imageH_, filterH_, paddingH_, strideH_,
51+
/* caffeMode */ true);
52+
outputW_ = outputSize(imageW_, filterW_, paddingW_, strideW_,
53+
/* caffeMode */ true);
5654

5755
const_cast<Argument*>(out_)->setFrameHeight(outputH_);
5856
const_cast<Argument*>(out_)->setFrameWidth(outputW_);

paddle/gserver/layers/CudnnPoolLayer.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
1615
#include "paddle/utils/Logging.h"
1716
#include "paddle/utils/Stat.h"
1817
#include "paddle/math/Matrix.h"
@@ -62,9 +61,9 @@ bool CudnnPoolLayer::init(const LayerMap &layerMap,
6261
strideHeight = strideY_;
6362
strideWidth = stride_;
6463

65-
hl_create_pooling_descriptor(&poolingDesc_, mode_, windowHeight,
66-
windowWidth, heightPadding, widthPadding,
67-
strideHeight, strideWidth);
64+
hl_create_pooling_descriptor(&poolingDesc_, mode_, windowHeight, windowWidth,
65+
heightPadding, widthPadding, strideHeight,
66+
strideWidth);
6867

6968
return true;
7069
}
@@ -80,8 +79,10 @@ void CudnnPoolLayer::reshape(int batchSize) {
8079
}
8180
CHECK_EQ(inputLayers_[0]->getOutput().value->getWidth(),
8281
channels_ * imageH_ * imageW_);
83-
outputH_ = outputSize(imageH_, sizeY_, confPaddingY_, strideY_);
84-
outputW_ = outputSize(imageW_, sizeX_, confPadding_, stride_);
82+
outputH_ = outputSize(imageH_, sizeY_, confPaddingY_, strideY_,
83+
/* caffeMode */ false);
84+
outputW_ =
85+
outputSize(imageW_, sizeX_, confPadding_, stride_, /* caffeMode */ false);
8586
getOutput().setFrameHeight(outputH_);
8687
getOutput().setFrameWidth(outputW_);
8788

@@ -99,8 +100,7 @@ void CudnnPoolLayer::forward(PassType passType) {
99100

100101
real *inputData = getInputValue(0)->getData();
101102
real *outData = getOutputValue()->getData();
102-
hl_pooling_forward(inputDesc_, inputData, outputDesc_, outData,
103-
poolingDesc_);
103+
hl_pooling_forward(inputDesc_, inputData, outputDesc_, outData, poolingDesc_);
104104
}
105105

106106
void CudnnPoolLayer::backward(const UpdateCallback &callback) {
@@ -113,8 +113,8 @@ void CudnnPoolLayer::backward(const UpdateCallback &callback) {
113113
real *inputGrad = getInputGrad(0)->getData();
114114
real *outData = getOutputValue()->getData();
115115
real *outGrad = getOutputGrad()->getData();
116-
hl_pooling_backward(inputDesc_, inputData, inputGrad, outputDesc_,
117-
outData, outGrad, poolingDesc_);
116+
hl_pooling_backward(inputDesc_, inputData, inputGrad, outputDesc_, outData,
117+
outGrad, poolingDesc_);
118118
}
119119

120120
CudnnPoolLayer::~CudnnPoolLayer() {

paddle/gserver/layers/PoolLayer.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License. */
1717

1818
#include "Layer.h"
1919
#include "paddle/math/Matrix.h"
20+
#include "paddle/math/MathUtils.h"
2021
#include <vector>
2122

2223
namespace paddle {
@@ -47,16 +48,6 @@ class PoolLayer : public Layer {
4748
static Layer* create(const LayerConfig& config);
4849

4950
virtual bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);
50-
51-
/**
52-
* Calculate output size according window size and padding size.
53-
*/
54-
int outputSize(int imageSize, int windowSize, int padding, int stride) {
55-
int outputSize;
56-
outputSize =
57-
(imageSize - windowSize + 2 * padding + stride - 1) / stride + 1;
58-
return outputSize;
59-
}
6051
};
6152

6253
} // namespace paddle

0 commit comments

Comments
 (0)