-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add convolution Function #2282
Merged
Merged
Add convolution Function #2282
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3b65bc7
Add a naive convolution implement
hedaoyuan b6de52c
Bug fix
hedaoyuan 1846d9e
Add a convolution Function based on gemm.
hedaoyuan 1879332
Modify FunctionCompare to Compare2Function to support comparison of t…
hedaoyuan 455888c
Add ConvOpTest for NaiveConv and GemmConv
hedaoyuan 048b14a
Change stride to strides, and change padding to paddings.
hedaoyuan 3ce974b
Add group argument in ConvFunctionBase
hedaoyuan 3c0aa0c
Add GPU GemmConvFunction implementation
hedaoyuan c70d3e1
Some bug fix
hedaoyuan 3408b4b
Bug fix
hedaoyuan afbe556
Modify the arguments description of ConvFunctionBase. And add the def…
hedaoyuan 6a93f0f
Add the calculation implementation of GemmConvGradFilterFunction
hedaoyuan 9032619
Bug fix & add test of GemmConvGradFilter.
hedaoyuan d99faf3
Add the calculation implementation of GemmConvGradInputFunction.
hedaoyuan 9885c57
format
hedaoyuan 7aac38c
Refactoring the code implementation of exconv adn exconvt layer with …
hedaoyuan 784e218
Fix the error of group convolution.
hedaoyuan 95a7bc0
follow comments
hedaoyuan e039410
Remove the code of ExpandConvTransLayer.
hedaoyuan 1e0cc74
Merge branch 'develop' of https://github.com/baidu/Paddle into convol…
hedaoyuan 01d52eb
Fix RowConvOpTest use CpuGpuFuncCompare.
hedaoyuan 2608c48
Add test cases where the height and width (input, filter) are not equal.
hedaoyuan c6e010d
Follow comments.
hedaoyuan 1ed31b4
Bug fix.
hedaoyuan 9c47c42
Change the groups in the comment to 1049089.
hedaoyuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
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 "Function.h" | ||
|
||
namespace paddle { | ||
|
||
/* | ||
* \brief Based on the ConvFunctionBase class, the forward calculation, | ||
* backward input calculation and backward filter calculation | ||
* of convolution operations can be implemented. | ||
* | ||
* Arguments of forward and backward calculation: | ||
* 1. Forward calculation of convolution. | ||
* inputs = {INPUT, FILTER}, outputs = {OUTPUT} | ||
* The first and second input arguments are input image and filter data. | ||
* The output argument is output image. | ||
* | ||
* 2. Backward input calculation of convolution. | ||
* inputs = {OUTPUT_GRAD, FILTER}, outputs = {INPUT_GRAD} | ||
* The first and second input arguments are output grad image | ||
* and filter data. | ||
* The output argument is input grad image. | ||
* | ||
* 3. Backward filter calculation of convolution. | ||
* inputs = {OUTPUT_GRAD, INPUT}, outputs = {FILTER_GRAD} | ||
* The first and second input arguments are output grad image | ||
* and input image. | ||
* The output argument is filter grad. | ||
* | ||
* Arguments format of input, filter and output: | ||
* 1. Input image, output image, input image gradient, output image gradient | ||
* are all NCHW format. Where N is batch size, C is the number of channels, | ||
* H and W is the height and width of image or image gradient. | ||
* | ||
* 2. The format of the filter data is MCHW, where M is the number of output | ||
* image channels, C is the number of input image channels, | ||
* H and W is height and width of filter. | ||
* | ||
* If `groups` is greater than 1, the filter's data format should be GMCHW, | ||
* where G is the `groups`, and G * M is the number of output image | ||
* channels, G * C is the number of input image channels, | ||
* H and W is height and width of filter. | ||
*/ | ||
class ConvFunctionBase : public FunctionBase { | ||
public: | ||
void init(const FuncConfig& config) override { | ||
// function arguments | ||
strides_ = config.get<std::vector<size_t>>("strides"); | ||
paddings_ = config.get<std::vector<size_t>>("paddings"); | ||
groups_ = config.get<size_t>("groups"); | ||
|
||
// number of inputs and outputs | ||
numInputs_ = 2; | ||
numOutputs_ = 1; | ||
} | ||
|
||
virtual void calc(const BufferArgs& inputs, const BufferArgs& outputs) {} | ||
|
||
// input can be INPUT and INPUT_GRAD | ||
// filter can be FILTER and FILTER_GRAD | ||
// output can be OUTPUT and OUTPUT_GRAD | ||
void check(const TensorShape& input, | ||
const TensorShape& filter, | ||
const TensorShape& output) { | ||
// inputs and outputs arguments should be 4-dimensional. | ||
CHECK_EQ(input.ndims(), (size_t)4); | ||
CHECK_EQ(output.ndims(), (size_t)4); | ||
// The batchSize of the input needs to be equal to | ||
// the batchSize of the output. | ||
CHECK_EQ(input[0], output[0]); | ||
|
||
if (filter.ndims() == (size_t)4) { | ||
// If the filter's dimension is 4, groups convolution is not supported. | ||
CHECK_EQ(groups_, (size_t)1); | ||
// The input and output channel dimensions are the second and first | ||
// dimensions of the filter shape. | ||
CHECK_EQ(input[1], filter[1]); | ||
CHECK_EQ(output[1], filter[0]); | ||
} else { | ||
// filter argument should be 5-dimensional. | ||
CHECK_EQ(filter.ndims(), (size_t)5); | ||
// The first dimension of the filter is the size of the group | ||
CHECK_EQ(filter[0], groups_); | ||
// The input and output channel dimensions are the third and second | ||
// dimensions of the filter shape. | ||
CHECK_EQ(input[1], filter[2] * groups_); | ||
CHECK_EQ(output[1], filter[1] * groups_); | ||
} | ||
} | ||
|
||
protected: | ||
size_t getFilterHeight(const TensorShape& filter) const { | ||
return filter[filter.ndims() - 2]; | ||
} | ||
|
||
size_t getFilterWidth(const TensorShape& filter) const { | ||
return filter[filter.ndims() - 1]; | ||
} | ||
|
||
std::vector<size_t> strides_; | ||
std::vector<size_t> paddings_; | ||
|
||
/// Group size, refer to grouped convolution in | ||
/// Alex Krizhevsky's paper: when group=2, the first half of the | ||
/// filters are only connected to the first half of the input channels, | ||
/// and the second half only connected to the second half. | ||
size_t groups_; | ||
|
||
inline int strideH() const { return strides_[0]; } | ||
|
||
inline int strideW() const { return strides_[1]; } | ||
|
||
inline int paddingH() const { return paddings_[0]; } | ||
|
||
inline int paddingW() const { return paddings_[1]; } | ||
|
||
// A temporary memory in convolution calculation. | ||
MemoryHandlePtr memory_; | ||
|
||
template <DeviceType Device> | ||
void resizeBuffer(size_t newSize) { | ||
if (!memory_ || newSize * sizeof(real) > memory_->getAllocSize()) { | ||
if (Device == DEVICE_TYPE_CPU) { | ||
memory_ = std::make_shared<CpuMemoryHandle>(newSize * sizeof(real)); | ||
} else { | ||
memory_ = std::make_shared<GpuMemoryHandle>(newSize * sizeof(real)); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
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 <gtest/gtest.h> | ||
#include <memory> | ||
#include "Function.h" | ||
#include "FunctionTest.h" | ||
|
||
namespace paddle { | ||
|
||
enum TestType { | ||
kForwardTest = 0, | ||
kBackwardInputTest = 1, | ||
kBackwardFilterTest = 2, | ||
}; | ||
|
||
template <DeviceType DType1, DeviceType DType2> | ||
class ConvolutionTest { | ||
public: | ||
ConvolutionTest(const std::string& conv1, | ||
const std::string& conv2, | ||
TestType type, | ||
std::string algo = "auto") { | ||
for (size_t batchSize : {1, 32}) { | ||
for (size_t inputSize : {7, 14, 54}) { | ||
for (size_t filterSize : {1, 3, 5}) { | ||
for (size_t inputChannels : {3, 64}) { | ||
for (size_t outputChannels : {3, 64, 128}) { | ||
if (inputChannels < outputChannels) break; | ||
for (size_t stride : {1, 2}) { | ||
for (size_t padding : {0, 1}) { | ||
if (padding >= filterSize) break; | ||
size_t outputSize = | ||
(inputSize - filterSize + 2 * padding + stride) / stride; | ||
VLOG(3) << " batchSize=" << batchSize | ||
<< " inputChannels=" << inputChannels | ||
<< " inputHeight=" << inputSize | ||
<< " inputWidth=" << inputSize | ||
<< " outputChannels=" << outputChannels | ||
<< " filterHeight=" << filterSize | ||
<< " filterWidth=" << filterSize | ||
<< " outputHeight=" << outputSize | ||
<< " outputWidth=" << outputSize | ||
<< " stride=" << stride << " padding=" << padding; | ||
|
||
std::vector<size_t> paddings = {padding, padding}; | ||
std::vector<size_t> strides = {stride, stride}; | ||
Compare2Function<DType1, DType2> test( | ||
conv1, | ||
conv2, | ||
FuncConfig() | ||
.set("paddings", paddings) | ||
.set("strides", strides) | ||
.set("groups", (size_t)1) | ||
.set("algo", algo)); | ||
|
||
TensorShape input{ | ||
batchSize, inputChannels, inputSize, inputSize}; | ||
TensorShape filter{ | ||
outputChannels, inputChannels, filterSize, filterSize}; | ||
TensorShape output{ | ||
batchSize, outputChannels, outputSize, outputSize}; | ||
|
||
if (type == kForwardTest) { | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, input)); | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, filter)); | ||
test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, output)); | ||
test.run(); | ||
} else if (type == kBackwardInputTest) { | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, output)); | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, filter)); | ||
test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, input), ADD_TO); | ||
test.run(); | ||
} else if (type == kBackwardFilterTest) { | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, output)); | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, input)); | ||
test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, filter)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里不是ADD_TO? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BackwardFilter是支持ASSIGN_TO模式的。Test可以默认都用ASSIGN_TO测试,对于不支持ASSIGN_TO的Function,需要显示配置ADD_TO。 |
||
test.run(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
// Mainly used to test cases where the height and width (input, filter) | ||
// are not equal. | ||
template <DeviceType DType1, DeviceType DType2> | ||
class ConvolutionTest2 { | ||
public: | ||
ConvolutionTest2(const std::string& conv1, | ||
const std::string& conv2, | ||
TestType type, | ||
std::string algo = "auto") { | ||
for (size_t batchSize : {16}) { | ||
for (size_t inputHeight : {7, 31}) { | ||
for (size_t inputWidth : {10, 54}) { | ||
for (size_t filterHeight : {1, 5}) { | ||
for (size_t filterWidth : {3, 7}) { | ||
for (size_t inputChannels : {7}) { | ||
for (size_t outputChannels : {32}) { | ||
size_t stride = 1; | ||
size_t padding = 0; | ||
size_t outputHeight = | ||
(inputHeight - filterHeight + 2 * padding + stride) / | ||
stride; | ||
size_t outputWidth = | ||
(inputWidth - filterWidth + 2 * padding + stride) / | ||
stride; | ||
VLOG(3) << " batchSize=" << batchSize | ||
<< " inputChannels=" << inputChannels | ||
<< " inputHeight=" << inputHeight | ||
<< " inputWidth=" << inputWidth | ||
<< " outputChannels=" << outputChannels | ||
<< " filterHeight=" << filterHeight | ||
<< " filterWidth=" << filterWidth | ||
<< " outputHeight=" << outputHeight | ||
<< " outputWidth=" << outputWidth | ||
<< " stride=" << stride << " padding=" << padding; | ||
|
||
std::vector<size_t> paddings = {padding, padding}; | ||
std::vector<size_t> strides = {stride, stride}; | ||
Compare2Function<DType1, DType2> test( | ||
conv1, | ||
conv2, | ||
FuncConfig() | ||
.set("paddings", paddings) | ||
.set("strides", strides) | ||
.set("groups", (size_t)1) | ||
.set("algo", algo)); | ||
|
||
TensorShape input{ | ||
batchSize, inputChannels, inputHeight, inputWidth}; | ||
TensorShape filter{ | ||
outputChannels, inputChannels, filterHeight, filterWidth}; | ||
TensorShape output{ | ||
batchSize, outputChannels, outputHeight, outputWidth}; | ||
|
||
if (type == kForwardTest) { | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, input)); | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, filter)); | ||
test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, output)); | ||
test.run(); | ||
} else if (type == kBackwardInputTest) { | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, output)); | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, filter)); | ||
test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, input), ADD_TO); | ||
test.run(); | ||
} else if (type == kBackwardFilterTest) { | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, output)); | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, input)); | ||
test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, filter)); | ||
test.run(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
TEST(Forward, GEMM) { | ||
ConvolutionTest<DEVICE_TYPE_CPU, DEVICE_TYPE_CPU> test( | ||
"NaiveConv-CPU", "GemmConv-CPU", kForwardTest); | ||
ConvolutionTest2<DEVICE_TYPE_CPU, DEVICE_TYPE_CPU> test2( | ||
"NaiveConv-CPU", "GemmConv-CPU", kForwardTest); | ||
} | ||
|
||
#ifndef PADDLE_ONLY_CPU | ||
TEST(Forward, GEMM2) { | ||
ConvolutionTest<DEVICE_TYPE_CPU, DEVICE_TYPE_GPU> test( | ||
"GemmConv-CPU", "GemmConv-GPU", kForwardTest); | ||
ConvolutionTest2<DEVICE_TYPE_CPU, DEVICE_TYPE_GPU> test2( | ||
"GemmConv-CPU", "GemmConv-GPU", kForwardTest); | ||
} | ||
|
||
TEST(BackwardInput, GEMM) { | ||
ConvolutionTest<DEVICE_TYPE_CPU, DEVICE_TYPE_GPU> test( | ||
"GemmConvGradInput-CPU", "GemmConvGradInput-GPU", kBackwardInputTest); | ||
ConvolutionTest2<DEVICE_TYPE_CPU, DEVICE_TYPE_GPU> test2( | ||
"GemmConvGradInput-CPU", "GemmConvGradInput-GPU", kBackwardInputTest); | ||
} | ||
|
||
TEST(BackwardFilter, GEMM) { | ||
ConvolutionTest<DEVICE_TYPE_CPU, DEVICE_TYPE_GPU> test( | ||
"GemmConvGradFilter-CPU", "GemmConvGradFilter-GPU", kBackwardFilterTest); | ||
ConvolutionTest2<DEVICE_TYPE_CPU, DEVICE_TYPE_GPU> test2( | ||
"GemmConvGradFilter-CPU", "GemmConvGradFilter-GPU", kBackwardFilterTest); | ||
} | ||
#endif | ||
|
||
} // namespace paddle |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是否需要增加长方形input,output, filter的单测?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在这里加会让TEST变的时间很长,后续我单独增加针对长方形的测试吧。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.