Skip to content

Commit 96f42d8

Browse files
authored
Merge pull request #1455 from wangyang59/cudnnDeconv
Cudnn deconv
2 parents 4951c46 + fc7f72c commit 96f42d8

22 files changed

+1484
-574
lines changed

paddle/gserver/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ filter_test(GSERVER_HEADER)
2525
filter_test(GSERVER_SOURCES)
2626
if(NOT WITH_GPU)
2727
list(REMOVE_ITEM GSERVER_HEADER
28+
layers/CudnnConvBaseLayer.h
2829
layers/CudnnConvLayer.h
30+
layers/CudnnConvTransLayer.h
2931
layers/CudnnPoolLayer.h
3032
layers/CudnnBatchNormLayer.h)
3133

3234
list(REMOVE_ITEM GSERVER_SOURCES
35+
layers/CudnnConvBaseLayer.cpp
3336
layers/CudnnConvLayer.cpp
37+
layers/CudnnConvTransLayer.cpp
3438
layers/CudnnPoolLayer.cpp
3539
layers/CudnnBatchNormLayer.cpp)
3640
compile_cu_as_cpp(layers/LstmCompute.cu)
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "ConvBaseOperator.h"
16+
#include "paddle/math/MathUtils.h"
17+
#include "paddle/math/Matrix.h"
18+
19+
namespace paddle {
20+
21+
/**
22+
* @brief ConvBaseOperator takes two inputs to perform the convolution.
23+
* The first input is the image, and the second input is the convolution kernel.
24+
* The height of data for two inputs are the same. Each data of the first input
25+
* is convolved with each data of the second input indepedently.
26+
*
27+
* The config file api is conv_operator.
28+
*/
29+
30+
ConvBaseOperator::ConvBaseOperator(const OperatorConfig &config, bool useGpu)
31+
: Operator(config, useGpu) {
32+
CHECK(useGpu);
33+
CHECK_EQ(config_.input_indices_size(), 2L);
34+
35+
caffeMode_ = true;
36+
getConvParams();
37+
computeConvSizes();
38+
39+
// initialize all to default algorithms
40+
fwdAlgo_ = 0;
41+
bwdFilterAlgo_ = 0;
42+
bwdDataAlgo_ = 0;
43+
fwdLimitBytes_ = 0;
44+
bwdDataLimitBytes_ = 0;
45+
bwdFilterLimitBytes_ = 0;
46+
workSpaceInBytes_ = 0;
47+
workSpace_ = nullptr;
48+
49+
isSelectAlgo_ = false;
50+
}
51+
52+
void ConvBaseOperator::allocConvWorkSpace() {
53+
hl_conv_workspace(imageDesc_,
54+
outputDesc_,
55+
filterDesc_,
56+
convDesc_,
57+
&fwdAlgo_,
58+
&fwdLimitBytes_,
59+
&bwdDataAlgo_,
60+
&bwdDataLimitBytes_,
61+
&bwdFilterAlgo_,
62+
&bwdFilterLimitBytes_);
63+
64+
size_t maxWorkSpace = 0;
65+
maxWorkSpace = std::max(fwdLimitBytes_, bwdDataLimitBytes_);
66+
maxWorkSpace = std::max(maxWorkSpace, bwdFilterLimitBytes_);
67+
68+
if (maxWorkSpace > workSpaceInBytes_) {
69+
if (workSpaceInBytes_ != 0) {
70+
hl_free_mem_device(workSpace_);
71+
}
72+
// total amount of storage needed
73+
workSpace_ = hl_malloc_device(maxWorkSpace);
74+
workSpaceInBytes_ = maxWorkSpace;
75+
}
76+
}
77+
78+
void ConvBaseOperator::computeConvSizes() {
79+
hl_create_filter_descriptor(
80+
&filterDesc_, channels_, numFilters_, filterSizeY_, filterSize_);
81+
hl_create_tensor_descriptor(&imageDesc_);
82+
hl_create_tensor_descriptor(&outputDesc_);
83+
hl_create_convolution_descriptor(&convDesc_,
84+
imageDesc_,
85+
filterDesc_,
86+
paddingY_,
87+
padding_,
88+
strideY_,
89+
stride_);
90+
}
91+
92+
void ConvBaseOperator::reshapeImageDescriptors() {
93+
hl_tensor_reshape(imageDesc_,
94+
1,
95+
channels_,
96+
imageH_,
97+
imageW_,
98+
channels_ * imageH_ * imageW_,
99+
imageH_ * imageW_,
100+
imageW_,
101+
1);
102+
hl_tensor_reshape(outputDesc_,
103+
1,
104+
numFilters_,
105+
outputH_,
106+
outputW_,
107+
numFilters_ * outputH_ * outputW_,
108+
outputH_ * outputW_,
109+
outputW_,
110+
1);
111+
hl_reset_convolution_descriptor(convDesc_,
112+
imageDesc_,
113+
filterDesc_,
114+
paddingY_,
115+
padding_,
116+
strideY_,
117+
stride_);
118+
}
119+
120+
void ConvBaseOperator::getConvParams() {
121+
configNumFilters_ = config_.num_filters();
122+
const ConvConfig &conf = config_.conv_conf();
123+
padding_ = conf.padding();
124+
stride_ = conf.stride();
125+
filterSize_ = conf.filter_size();
126+
paddingY_ = conf.padding_y();
127+
strideY_ = conf.stride_y();
128+
filterSizeY_ = conf.filter_size_y();
129+
filterPixels_ = filterSize_ * filterSizeY_;
130+
configChannels_ = conf.channels();
131+
imgSize_ = conf.img_size();
132+
imgSizeY_ = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size();
133+
imgPixels_ = imgSize_ * imgSizeY_;
134+
CHECK_EQ(conf.groups(), 1U);
135+
filterChannels_ = conf.filter_channels();
136+
outputX_ = conf.output_x();
137+
outputY_ = conf.has_output_y() ? conf.output_y() : conf.output_x();
138+
outputs_ = outputX_ * outputX_;
139+
140+
isDeconv_ = (config_.type() == "conv") ? false : true;
141+
if (isDeconv_) {
142+
channels_ = configNumFilters_;
143+
numFilters_ = configChannels_;
144+
} else {
145+
channels_ = configChannels_;
146+
numFilters_ = configNumFilters_;
147+
}
148+
}
149+
150+
} // namespace paddle
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
#pragma once
15+
16+
#include "Operator.h"
17+
#include "paddle/math/MathUtils.h"
18+
#include "paddle/math/Matrix.h"
19+
20+
namespace paddle {
21+
22+
/**
23+
* @brief ConvOperator takes two inputs to perform the convolution.
24+
* The first input is the image, and the second input is the convolution kernel.
25+
* The height of data for two inputs are the same. Each data of the first input
26+
* is convolved with each data of the second input indepedently.
27+
*
28+
* The config file api is conv_operator.
29+
*/
30+
31+
class ConvBaseOperator : public Operator {
32+
public:
33+
ConvBaseOperator(const OperatorConfig &config, bool useGpu);
34+
/**
35+
* Free workspace in device and destroy cudnn tensor descriptor.
36+
*/
37+
virtual ~ConvBaseOperator() {
38+
if (workSpaceInBytes_ != 0) {
39+
hl_free_mem_device(workSpace_);
40+
workSpaceInBytes_ = 0;
41+
}
42+
43+
hl_destroy_tensor_descriptor(imageDesc_);
44+
hl_destroy_tensor_descriptor(outputDesc_);
45+
hl_destroy_filter_descriptor(filterDesc_);
46+
hl_destroy_convolution_descriptor(convDesc_);
47+
}
48+
49+
protected:
50+
/**
51+
* Get convolution parameters from layer config and
52+
* initialize member variables.
53+
*/
54+
void getConvParams();
55+
56+
/**
57+
* Allocate Gpu Memory for cudnn convolution algorithms.
58+
*/
59+
void allocConvWorkSpace();
60+
61+
/**
62+
* Create cudnn tensor descriptor for convolution operation.
63+
*/
64+
void computeConvSizes();
65+
66+
/**
67+
* Reshape cudnn tensor descriptor.
68+
*/
69+
void reshapeImageDescriptors();
70+
71+
/**
72+
* Reshape cudnn tensor descriptor.
73+
*/
74+
virtual void reshape(int batchSize) = 0;
75+
76+
/**
77+
* Check filter size is equal to the size calculated by parameters from
78+
* layer config.
79+
*/
80+
void checkFilterSize(const MatrixPtr &filter) {
81+
CHECK_EQ(static_cast<int>(filter->getWidth()),
82+
filterSize_ * filterSizeY_ * channels_ * numFilters_);
83+
}
84+
85+
/// Most of member variables are same with CudnnConvLayer.
86+
/// There is no explanation here.
87+
bool isDeconv_;
88+
int imageH_, imageW_, outputH_, outputW_;
89+
hl_tensor_descriptor imageDesc_;
90+
hl_tensor_descriptor outputDesc_;
91+
hl_filter_descriptor filterDesc_;
92+
hl_convolution_descriptor convDesc_;
93+
bool caffeMode_;
94+
int inputOffset_, outputOffset_, weightOffset_;
95+
int numFilters_, channels_;
96+
97+
/// from parsing config
98+
int configNumFilters_, configChannels_;
99+
int padding_, stride_, filterSize_, imgSize_, imgSizeY_;
100+
int paddingY_, strideY_, filterSizeY_;
101+
int imgPixels_, filterPixels_, filterChannels_, outputX_, outputY_, outputs_;
102+
103+
/// Following member variables are same with CudnnConvLayer.
104+
/// There is no explanation here.
105+
int fwdAlgo_, bwdFilterAlgo_, bwdDataAlgo_;
106+
size_t fwdLimitBytes_, bwdDataLimitBytes_, bwdFilterLimitBytes_;
107+
size_t workSpaceInBytes_;
108+
void *workSpace_;
109+
bool isSelectAlgo_;
110+
};
111+
112+
} // namespace paddle

0 commit comments

Comments
 (0)