Add convolution Function#2282
Conversation
…wo CPU functions.
…inition of backward input and backward filter function.
…GemmConvFunction.
| FORWARD_TEST = 0, | ||
| BACKWARD_INPUT_TEST = 1, | ||
| BACKWARD_FILTER_TEST = 2, | ||
| }; |
There was a problem hiding this comment.
paddle/function/ConvOpTest.cpp
Outdated
| if (padding >= filterSize) break; | ||
| size_t outputSize = | ||
| (inputSize - filterSize + 2 * padding + stride) / stride; | ||
| LOG(INFO) << " batchSize=" << batchSize |
| 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.
是否需要增加长方形input,output, filter的单测?
There was a problem hiding this comment.
在这里加会让TEST变的时间很长,后续我单独增加针对长方形的测试吧。
| int paddingWidth, | ||
| int outputHeight, | ||
| int outputWidth, | ||
| T* colData) { |
There was a problem hiding this comment.
感觉接口直接传TensorShape会简单一些吧,就不用这么多参数了~
There was a problem hiding this comment.
修改成TensorShape的话,接口里面还需要做一次input/filter shape对应参数的检查。而且,这里没有batchSize这个维度,还不能直接用Function里面传入的参数的shape。
There was a problem hiding this comment.
Will be fixed in the next PR 2449.
class Im2ColFunctor {
public:
void operator()(float* colData,
const float* imData,
const TensorShape& colShape,
const TensorShape& imShape,
int strideHeight,
int strideWidth,
int paddingHeight,
int paddingWidth);
};
paddle/function/ConvOp.h
Outdated
| * 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, |
There was a problem hiding this comment.
��个注释里面我觉得不用修改成groups_,FuncConfig的配置参数名称上也是groups。
There was a problem hiding this comment.
上面提出修改原因是:If groups is greater than 1 不符合语法,也提醒我以后comments需要解释清楚 :)
paddle/function/ConvOp.h
Outdated
|
|
||
| protected: | ||
| size_t getFilterHeight(const TensorShape& filter) const { | ||
| if (filter.ndims() == 5) { |
paddle/function/ConvOp.h
Outdated
|
|
||
| protected: | ||
| size_t getFilterHeight(const TensorShape& filter) const { | ||
| if (filter.ndims() == 5) { |
paddle/function/ConvOp.h
Outdated
| } | ||
|
|
||
| size_t getFilterWidth(const TensorShape& filter) const { | ||
| if (filter.ndims() == 5) { |
paddle/function/ConvOp.h
Outdated
| if (filter.ndims() == 5) { | ||
| return filter[4]; | ||
| } else { | ||
| return filter[3]; |
There was a problem hiding this comment.
可以加个类似filter[-1], filter[-2] 这样的接口?
There was a problem hiding this comment.
没有理解这个comment,filter[-1],filter[-2]指的是啥?
There was a problem hiding this comment.
-1, -2指的是倒数第一个第二个值这样?
paddle/function/FunctionTest.h
Outdated
| std::shared_ptr<FunctionBase> getCpuFunction() const { return function1_; } | ||
|
|
||
| std::shared_ptr<FunctionBase> getGpuFunction() const { return gpuFunc_; } | ||
| std::shared_ptr<FunctionBase> getGpuFunction() const { return function2_; } |
There was a problem hiding this comment.
从上面看function1_不一定是CPU Type, function2_不一定是GPU Type吧
| * output_height, output_width] | ||
| */ | ||
| template <class T> | ||
| class Im2ColFunctor<DEVICE_TYPE_CPU, T> { |
There was a problem hiding this comment.
Im2Col, Col2Im是不是可以单独一个Function?可以用在其他Function里,比如BlockExpand里。
There was a problem hiding this comment.
在PR #2424 里面会移到一个单独的文件里面,但不是Function,被ImageExpandFunction调用。
| size_t filterWidth = getFilterWidth(filter); | ||
| size_t outputChannels = output[1]; | ||
| size_t outputHeight = output[2]; | ||
| size_t outputWidth = output[3]; |
There was a problem hiding this comment.
n, ci, hi, wi, kh, kw, co, ho,wo感觉命名更短些,当然现在这样容易理解 :)
There was a problem hiding this comment.
个人觉得Function的代码逻辑可读性比较重要。如果用n, ci, hi...这种缩写的话,对于不熟悉代码逻辑的人并不一定很友好。
| std::vector<size_t> paddings = {(size_t)paddingY_[i], (size_t)padding_[i]}; | ||
| std::vector<size_t> strides = {(size_t)strideY_[i], (size_t)stride_[i]}; | ||
| createFunction(forward_, | ||
| !isDeconv_ ? "GemmConv" : "GemmConvGradInput", |
There was a problem hiding this comment.
isDeconv_ -> isConvTrans_
There was a problem hiding this comment.
isDeconv_是ConvBaseLayer里面原来的名字,换成isConvTrans_更好?
There was a problem hiding this comment.
嗯,命名和对用户接口一致叫做convolution transpose layer觉得更好些,这样也可以吧~
| inputs.addArg(*weights_[i]->getW(), filterShape_[i]); | ||
| outputs.addArg(*getOutputValue(), | ||
| outputShape_[i], | ||
| !isDeconv_ && i == 0 ? ASSIGN_TO : ADD_TO); |
There was a problem hiding this comment.
主要是目前GemmConvGradInput的实现只支持ADD_TO模式。
https://github.com/PaddlePaddle/Paddle/pull/2282/files/2608c4854273e47bd0958fbb03ca67050ddfb35c#diff-b71c34e8a73d71627e2b185b4dd33aafR215
There was a problem hiding this comment.
明白了。感觉Function是不是应该和blas库类似,加scale/beta这样的系数更合理些,而不是ASSIGN_TO、ADD_TO~
paddle/function/ConvOp.h
Outdated
| * 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, |
There was a problem hiding this comment.
上面提出修改原因是:If groups is greater than 1 不符合语法,也提醒我以后comments需要解释清楚 :)
| int c_col = int(c * blockH* blockW) + \ | ||
| (h - h_col * (int)strideH) * (int)blockW + | ||
| (w - w_col * (int)strideW); | ||
| val += data_col[(c_col * height_col + h_col) * width_col + w_col]; |
| inputs.addArg(*weights_[i]->getW(), filterShape_[i]); | ||
| outputs.addArg(*getOutputValue(), | ||
| outputShape_[i], | ||
| !isDeconv_ && i == 0 ? ASSIGN_TO : ADD_TO); |
There was a problem hiding this comment.
明白了。感觉Function是不是应该和blas库类似,加scale/beta这样的系数更合理些,而不是ASSIGN_TO、ADD_TO~
| std::vector<size_t> paddings = {(size_t)paddingY_[i], (size_t)padding_[i]}; | ||
| std::vector<size_t> strides = {(size_t)strideY_[i], (size_t)stride_[i]}; | ||
| createFunction(forward_, | ||
| !isDeconv_ ? "GemmConv" : "GemmConvGradInput", |
There was a problem hiding this comment.
嗯,命名和对用户接口一致叫做convolution transpose layer觉得更好些,这样也可以吧~
hedaoyuan
left a comment
There was a problem hiding this comment.
目前来看只有ASSIGN_TO和ADD_TO两种赋值模式。修改成一个scale会增加Function的复杂度,scale需要是Function的参数,ASSIGN_TO和ADD_TO可以是BufferArg的参数;另外,结果是覆盖掉Arg中原来的值还是需要add上去,这个也是BufferArg自身的属性。
Follow comments in issue #2196
GemmConvOp.cpp.Code refactoring:
To do:
CudnnConvOp.cpp. Refactor CudnnConvLayer, ConvProjection, ConvTransProjection #2425