-
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
Add convolution Function #2282
Changes from 16 commits
3b65bc7
b6de52c
1846d9e
1879332
455888c
048b14a
3ce974b
3c0aa0c
c70d3e1
3408b4b
afbe556
6a93f0f
9032619
d99faf3
9885c57
7aac38c
784e218
95a7bc0
e039410
1e0cc74
01d52eb
2608c48
c6e010d
1ed31b4
9c47c42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* 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. | ||
*/ | ||
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(filter.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]); | ||
|
||
// The input and output channel dimensions are the second and first | ||
// dimensions of the filter shape. | ||
CHECK_EQ(input[1] / groups_, filter[1]); | ||
CHECK_EQ(output[1], filter[0]); | ||
} | ||
|
||
protected: | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* 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 { | ||
FORWARD_TEST = 0, | ||
BACKWARD_INPUT_TEST = 1, | ||
BACKWARD_FILTER_TEST = 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}) { | ||
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. 是否需要增加长方形input,output, 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. 在这里加会让TEST变的时间很长,后续我单独增加针对长方形的测试吧。 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. Done. |
||
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; | ||
LOG(INFO) << " batchSize=" << batchSize | ||
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. LOG(INFO) -> VLOG 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. Done. |
||
<< " 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 == FORWARD_TEST) { | ||
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 == BACKWARD_INPUT_TEST) { | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, output)); | ||
test.addInputs(BufferArg(VALUE_TYPE_FLOAT, filter)); | ||
test.addOutputs(BufferArg(VALUE_TYPE_FLOAT, input)); | ||
test.run(); | ||
} else if (type == BACKWARD_FILTER_TEST) { | ||
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", FORWARD_TEST); | ||
} | ||
|
||
#ifndef PADDLE_ONLY_CPU | ||
TEST(Forward, GEMM2) { | ||
ConvolutionTest<DEVICE_TYPE_CPU, DEVICE_TYPE_GPU> test( | ||
"GemmConv-CPU", "GemmConv-GPU", FORWARD_TEST); | ||
} | ||
|
||
TEST(BackwardInput, GEMM) { | ||
ConvolutionTest<DEVICE_TYPE_CPU, DEVICE_TYPE_GPU> test( | ||
"GemmConvGradInput-CPU", "GemmConvGradInput-GPU", BACKWARD_INPUT_TEST); | ||
} | ||
|
||
TEST(BackwardFilter, GEMM) { | ||
ConvolutionTest<DEVICE_TYPE_CPU, DEVICE_TYPE_GPU> test( | ||
"GemmConvGradFilter-CPU", "GemmConvGradFilter-GPU", BACKWARD_FILTER_TEST); | ||
} | ||
#endif | ||
|
||
} // namespace paddle |
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.
命名约定: https://github.com/PaddlePaddle/cpp-primer-digest/pull/1/files#diff-04c6e90faac2675aa89e2176d2eec7d8R264
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.