Skip to content

Commit

Permalink
abstract outputSize function in CNN-related layers (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
luotao1 authored Nov 7, 2016
1 parent f9849ac commit e802471
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 184 deletions.
9 changes: 4 additions & 5 deletions paddle/gserver/layers/ConvBaseLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */


#include "paddle/utils/Logging.h"
#include "ConvBaseLayer.h"
namespace paddle {
Expand Down Expand Up @@ -78,10 +77,10 @@ size_t ConvBaseLayer::calOutputSize() {
imgSizeH_[i] = config_.inputs(i).conv_conf().img_size();
if (imgSizeW_[i] == 0)
imgSizeW_[i] = config_.inputs(i).conv_conf().img_size();
outputH_.push_back(
outputSize(imgSizeH_[i], filterSizeY_[i], paddingY_[i], strideY_[i]));
outputW_.push_back(
outputSize(imgSizeW_[i], filterSize_[i], padding_[i], stride_[i]));
outputH_.push_back(outputSize(imgSizeH_[i], filterSizeY_[i], paddingY_[i],
strideY_[i], caffeMode_));
outputW_.push_back(outputSize(imgSizeW_[i], filterSize_[i], padding_[i],
stride_[i], caffeMode_));
CHECK_EQ(outputH_[i], outputH_[0]);
CHECK_EQ(outputW_[i], outputW_[0]);
}
Expand Down
26 changes: 1 addition & 25 deletions paddle/gserver/layers/ConvBaseLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License. */
#pragma once

#include "Layer.h"
#include "paddle/math/MathUtils.h"
namespace paddle {

/**
Expand Down Expand Up @@ -87,31 +88,6 @@ class ConvBaseLayer : public Layer {
virtual size_t calOutputSize();

Weight& getWeight(int idx) { return *weights_[idx]; }

/**
* Calculate output size based on caffeMode_.
* - input(+padding): 0123456789
* - imageSize(+padding) = 10;
* - filterSize = 3;
* - stride = 2;
* - caffeMode_ is true:
- output: (012), (234), (456), (678)
- outputSize = 4;
* - caffeMode_ is false:
* - output: (012), (234), (456), (678), (9)
* - outputSize = 5;
*/
int outputSize(int imageSize, int filterSize, int padding, int stride) {
int outputSize;
if (!caffeMode_) {
outputSize =
(imageSize - filterSize + 2 * padding + stride - 1) / stride + 1;
} else {
outputSize = (imageSize - filterSize + 2 * padding) / stride + 1;
}
CHECK_GE(outputSize, 1);
return outputSize;
}
};

} // namespace paddle
72 changes: 22 additions & 50 deletions paddle/gserver/layers/ConvOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */


#include "paddle/math/Matrix.h"
#include "paddle/math/MathUtils.h"
#include "Operator.h"

namespace paddle {
Expand All @@ -35,8 +35,8 @@ class ConvOperator : public Operator {
*/
virtual ~ConvOperator() {
if (workSpaceInBytes_ != 0) {
hl_free_mem_device(workSpace_);
workSpaceInBytes_ = 0;
hl_free_mem_device(workSpace_);
workSpaceInBytes_ = 0;
}

hl_destroy_tensor_descriptor(inputDesc_);
Expand Down Expand Up @@ -83,33 +83,6 @@ class ConvOperator : public Operator {
filterSize_ * filterSizeY_ * channels_ * numFilters_);
}

/**
* Calculate output size.
*/
int outputSize(int imageSize, int filterSize, int padding, int stride) {
int outputSize;
if (!caffeMode_) {
/* input(+padding): 0123456789
* imageSize(+padding) = 10;
* filterSize = 3;
* stride = 2;
* output: (012), (234), (456), (678), (9)
* outputSize = 5;
*/
outputSize =
(imageSize - filterSize + 2 * padding + stride - 1) / stride + 1;
} else {
/* input(+padding): 0123456789
* imageSize(+padding) = 10;
* filterSize = 3;
* stride = 2;
* output: (012), (234), (456), (678)
* outputSize = 4;
*/
outputSize = (imageSize - filterSize + 2 * padding) / stride + 1;
}
return outputSize;
}
/// Most of member variables are same with CudnnConvLayer.
/// There is no explanation here.
int imageH_, imageW_, outputH_, outputW_;
Expand All @@ -129,7 +102,7 @@ class ConvOperator : public Operator {
int fwdAlgo_, bwdFilterAlgo_, bwdDataAlgo_;
size_t fwdLimitBytes_, bwdDataLimitBytes_, bwdFilterLimitBytes_;
size_t workSpaceInBytes_;
void* workSpace_;
void *workSpace_;
bool isSelectAlgo_;
};

Expand Down Expand Up @@ -160,33 +133,32 @@ ConvOperator::ConvOperator(const OperatorConfig &config, bool useGpu)
void ConvOperator::allocConvWorkSpace(size_t maxWorkSpace) {
if (maxWorkSpace > workSpaceInBytes_) {
if (workSpaceInBytes_ != 0) {
hl_free_mem_device(workSpace_);
hl_free_mem_device(workSpace_);
}
// total amount of storage needed
workSpace_ = hl_malloc_device(maxWorkSpace);
workSpaceInBytes_ = maxWorkSpace;
}
}


void ConvOperator::reshape(int batchSize) {
imageH_ = ins_[0]->getFrameHeight();
imageW_ = ins_[0]->getFrameWidth();
if (imageH_ == 0) imageH_ = imgSize_;
if (imageW_ == 0) imageW_ = imgSize_;
outputH_ = outputSize(imageH_, filterSizeY_, paddingY_, strideY_);
outputW_ = outputSize(imageW_, filterSize_, padding_, stride_);
outputH_ = outputSize(imageH_, filterSizeY_, paddingY_, strideY_, caffeMode_);
outputW_ = outputSize(imageW_, filterSize_, padding_, stride_, caffeMode_);

out_->setFrameHeight(outputH_);
out_->setFrameWidth(outputW_);

reshapeImageDescriptors();

if (!isSelectAlgo_) {
hl_conv_workspace(inputDesc_, outputDesc_, filterDesc_,
convDesc_, &fwdAlgo_, &fwdLimitBytes_,
&bwdDataAlgo_, &bwdDataLimitBytes_,
&bwdFilterAlgo_, &bwdFilterLimitBytes_);
hl_conv_workspace(inputDesc_, outputDesc_, filterDesc_, convDesc_,
&fwdAlgo_, &fwdLimitBytes_, &bwdDataAlgo_,
&bwdDataLimitBytes_, &bwdFilterAlgo_,
&bwdFilterLimitBytes_);

size_t maxWorkSpace = 0;
maxWorkSpace = std::max(fwdLimitBytes_, bwdDataLimitBytes_);
Expand All @@ -202,7 +174,8 @@ void ConvOperator::computeConvSizes() {
hl_create_filter_descriptor(&filterDesc_, channels_, numFilters_,
filterSizeY_, filterSize_);
hl_create_tensor_descriptor(&inputDesc_);
int outputX = outputSize(imgSize_, filterSize_, padding_, stride_);
int outputX =
outputSize(imgSize_, filterSize_, padding_, stride_, caffeMode_);
CHECK_EQ(outputX, outputX_);
hl_create_tensor_descriptor(&outputDesc_);
hl_create_convolution_descriptor(&convDesc_, inputDesc_, filterDesc_,
Expand All @@ -211,13 +184,13 @@ void ConvOperator::computeConvSizes() {

void ConvOperator::reshapeImageDescriptors() {
hl_tensor_reshape(inputDesc_, 1, channels_, imageH_, imageW_,
channels_ * imageH_ * imageW_, imageH_ * imageW_,
imageW_, 1);
channels_ * imageH_ * imageW_, imageH_ * imageW_, imageW_,
1);
hl_tensor_reshape(outputDesc_, 1, numFilters_, outputH_, outputW_,
numFilters_ * outputH_ * outputW_, outputH_ * outputW_,
outputW_, 1);
hl_reset_convolution_descriptor(convDesc_, inputDesc_, filterDesc_,
paddingY_, padding_, strideY_, stride_);
hl_reset_convolution_descriptor(convDesc_, inputDesc_, filterDesc_, paddingY_,
padding_, strideY_, stride_);
inputOffset_ = channels_ * imageH_ * imageW_;
outputOffset_ = numFilters_ * outputH_ * outputW_;
weightOffset_ = numFilters_ * channels_ * filterSize_ * filterSize_;
Expand Down Expand Up @@ -273,18 +246,17 @@ void ConvOperator::backward() {
real *weightGrad = ins_[1]->grad->getData() + weightOffset_ * batchId;
hl_convolution_backward_filter(inputDesc_, inputData, outputDesc_,
outGrad, filterDesc_, weightGrad,
convDesc_, workSpace_,
workSpaceInBytes_, bwdFilterAlgo_);
convDesc_, workSpace_, workSpaceInBytes_,
bwdFilterAlgo_);
}

MatrixPtr preGrad = ins_[0]->grad;
if (NULL != preGrad) {
real *inputGrad = preGrad->getData() + inputOffset_ * batchId;
real *wgtData = ins_[1]->value->getData() + weightOffset_ * batchId;
hl_convolution_backward_data(inputDesc_, inputGrad, outputDesc_,
outGrad, filterDesc_, wgtData,
convDesc_, workSpace_,
workSpaceInBytes_, bwdDataAlgo_);
hl_convolution_backward_data(
inputDesc_, inputGrad, outputDesc_, outGrad, filterDesc_, wgtData,
convDesc_, workSpace_, workSpaceInBytes_, bwdDataAlgo_);
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions paddle/gserver/layers/ConvProjection.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */


#pragma once

#include "Projection.h"
#include "paddle/math/MathUtils.h"

namespace paddle {

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

int outputSize(int imageSize, int filterSize, int padding, int stride) {
return (imageSize - filterSize + 2 * padding) / stride + 1;
}

size_t calOutputSize() {
imageH_ = in_->getFrameHeight();
imageW_ = in_->getFrameWidth();
if (imageH_ == 0) imageH_ = configImgH_;
if (imageW_ == 0) imageW_ = configImgW_;
outputH_ = outputSize(imageH_, filterH_, paddingH_, strideH_);
outputW_ = outputSize(imageW_, filterW_, paddingW_, strideW_);
outputH_ = outputSize(imageH_, filterH_, paddingH_, strideH_,
/* caffeMode */ true);
outputW_ = outputSize(imageW_, filterW_, paddingW_, strideW_,
/* caffeMode */ true);

const_cast<Argument*>(out_)->setFrameHeight(outputH_);
const_cast<Argument*>(out_)->setFrameWidth(outputW_);
Expand Down
20 changes: 10 additions & 10 deletions paddle/gserver/layers/CudnnPoolLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */


#include "paddle/utils/Logging.h"
#include "paddle/utils/Stat.h"
#include "paddle/math/Matrix.h"
Expand Down Expand Up @@ -62,9 +61,9 @@ bool CudnnPoolLayer::init(const LayerMap &layerMap,
strideHeight = strideY_;
strideWidth = stride_;

hl_create_pooling_descriptor(&poolingDesc_, mode_, windowHeight,
windowWidth, heightPadding, widthPadding,
strideHeight, strideWidth);
hl_create_pooling_descriptor(&poolingDesc_, mode_, windowHeight, windowWidth,
heightPadding, widthPadding, strideHeight,
strideWidth);

return true;
}
Expand All @@ -80,8 +79,10 @@ void CudnnPoolLayer::reshape(int batchSize) {
}
CHECK_EQ(inputLayers_[0]->getOutput().value->getWidth(),
channels_ * imageH_ * imageW_);
outputH_ = outputSize(imageH_, sizeY_, confPaddingY_, strideY_);
outputW_ = outputSize(imageW_, sizeX_, confPadding_, stride_);
outputH_ = outputSize(imageH_, sizeY_, confPaddingY_, strideY_,
/* caffeMode */ false);
outputW_ =
outputSize(imageW_, sizeX_, confPadding_, stride_, /* caffeMode */ false);
getOutput().setFrameHeight(outputH_);
getOutput().setFrameWidth(outputW_);

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

real *inputData = getInputValue(0)->getData();
real *outData = getOutputValue()->getData();
hl_pooling_forward(inputDesc_, inputData, outputDesc_, outData,
poolingDesc_);
hl_pooling_forward(inputDesc_, inputData, outputDesc_, outData, poolingDesc_);
}

void CudnnPoolLayer::backward(const UpdateCallback &callback) {
Expand All @@ -113,8 +113,8 @@ void CudnnPoolLayer::backward(const UpdateCallback &callback) {
real *inputGrad = getInputGrad(0)->getData();
real *outData = getOutputValue()->getData();
real *outGrad = getOutputGrad()->getData();
hl_pooling_backward(inputDesc_, inputData, inputGrad, outputDesc_,
outData, outGrad, poolingDesc_);
hl_pooling_backward(inputDesc_, inputData, inputGrad, outputDesc_, outData,
outGrad, poolingDesc_);
}

CudnnPoolLayer::~CudnnPoolLayer() {
Expand Down
11 changes: 1 addition & 10 deletions paddle/gserver/layers/PoolLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License. */

#include "Layer.h"
#include "paddle/math/Matrix.h"
#include "paddle/math/MathUtils.h"
#include <vector>

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

virtual bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);

/**
* Calculate output size according window size and padding size.
*/
int outputSize(int imageSize, int windowSize, int padding, int stride) {
int outputSize;
outputSize =
(imageSize - windowSize + 2 * padding + stride - 1) / stride + 1;
return outputSize;
}
};

} // namespace paddle
Loading

0 comments on commit e802471

Please sign in to comment.