Skip to content

Commit 76fc1a8

Browse files
author
wanghaox
committed
for code review 4
1 parent 52f2366 commit 76fc1a8

File tree

7 files changed

+20
-29
lines changed

7 files changed

+20
-29
lines changed

paddle/operators/math/maxouting.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ namespace paddle {
1818
namespace operators {
1919
namespace math {
2020

21-
/*
22-
* All tensors are in NCHW format.
23-
* groups mustbe > 1
24-
*/
21+
// All tensors are in NCHW format, and the groups must be greater than 1
2522
template <typename T>
2623
class MaxOutFunctor<platform::CPUPlace, T> {
2724
public:
@@ -44,7 +41,6 @@ class MaxOutFunctor<platform::CPUPlace, T> {
4441
for (int c = 0; c < output_channels; ++c) {
4542
int new_cindex = fea_size * c;
4643
for (int f = 0; f < fea_size; ++f) {
47-
// T ele = maxout_process.initial();
4844
T ele = static_cast<T>(-FLT_MAX);
4945
for (int ph = 0; ph < groups; ++ph) {
5046
T x = input_data[(new_bindex + new_cindex) * groups
@@ -65,7 +61,7 @@ class MaxOutGradFunctor<platform::CPUPlace, T> {
6561
public:
6662
void operator()(const platform::DeviceContext& context,
6763
const framework::Tensor& input,
68-
framework::Tensor& input_grad,
64+
framework::Tensor * input_grad,
6965
const framework::Tensor& output,
7066
const framework::Tensor& output_grad,
7167
int groups) {
@@ -77,7 +73,7 @@ class MaxOutGradFunctor<platform::CPUPlace, T> {
7773
const T* input_data = input.data<T>();
7874
const T* output_data = output.data<T>();
7975
const T* output_grad_data = output_grad.data<T>();
80-
T* input_grad_data = input_grad.mutable_data<T>(context.GetPlace());
76+
T* input_grad_data = input_grad->mutable_data<T>(context.GetPlace());
8177

8278
for (int i = 0; i < batch_size; ++i) {
8379
int blen = fea_size * output_channels * i;

paddle/operators/math/maxouting.cu renamed to paddle/operators/math/maxouting.cu.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ template <typename T>
112112
class MaxOutGradFunctor<platform::GPUPlace, T> {
113113
public:
114114
void operator()(const platform::DeviceContext& context,
115-
const framework::Tensor& input, framework::Tensor& input_grad,
115+
const framework::Tensor& input,
116+
framework::Tensor * input_grad,
116117
const framework::Tensor& output,
117118
const framework::Tensor& output_grad,
118119
int groups) {
@@ -127,7 +128,7 @@ class MaxOutGradFunctor<platform::GPUPlace, T> {
127128
const T* input_data = input.data<T>();
128129
const T* output_data = output.data<T>();
129130
const T* output_grad_data = output_grad.data<T>();
130-
T* input_grad_data = input_grad.mutable_data<T>(context.GetPlace());
131+
T* input_grad_data = input_grad->mutable_data<T>(context.GetPlace());
131132
int nthreads = output.numel();
132133
int blocks = (nthreads + 1024 - 1) / 1024;
133134
dim3 threads(1024, 1);

paddle/operators/math/maxouting.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MaxOutGradFunctor {
3838
public:
3939
void operator()(const platform::DeviceContext& context,
4040
const framework::Tensor& input,
41-
framework::Tensor& input_grad,
41+
framework::Tensor * input_grad,
4242
const framework::Tensor& output,
4343
const framework::Tensor& output_grad, int groups);
4444
};

paddle/operators/maxout_op.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ class MaxOutOpMaker : public framework::OpProtoAndCheckerMaker {
3434
"width of feature.");
3535
AddAttr<int>(
3636
"groups",
37-
R"DOC(The group number of input layer.
37+
R"DOC("Specifies how many groups the input tensor will be split"
38+
"in the channel dimension. And the number of output channel is "
39+
"the number of channels divided by groups.."
3840
)DOC");
3941
AddComment(R"DOC(
40-
- Input: NCHW.
41-
- Output: The feature map size of output is the same as the input.
42-
The output_channel is (input channel) / groups
43-
So groups should be larger than 1, and the num of channels should be able
44-
to be devided by groups.
42+
Assumed the input shape is (N, Ci, H, W).
43+
The output shape is (N, Co, H, W). Then `Co = Ci / groups`.
4544
4645
math:
4746
y_{si+j} = \max_k x_{gsi + sk + j}
@@ -65,10 +64,10 @@ class MaxOutOp : public framework::OperatorWithKernel {
6564
public:
6665
using framework::OperatorWithKernel::OperatorWithKernel;
6766
void InferShape(framework::InferShapeContext* ctx) const override {
68-
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of maxoutOp"
67+
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of MaxoutOp"
6968
"should not be null.");
7069
PADDLE_ENFORCE(ctx->HasOutput("Out"),
71-
"Output(Out) of maxoutOp should not be null.");
70+
"Output(Out) of MaxoutOp should not be null.");
7271
auto in_x_dims = ctx->GetInputDim("X");
7372
int groups = ctx->Attrs().Get<int>("groups");
7473
// check groups > 1

paddle/operators/maxout_op.cu renamed to paddle/operators/maxout_op.cu.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#define EIGEN_USE_GPU
1615
#include "paddle/operators/maxout_op.h"
1716

1817
namespace ops = paddle::operators;

paddle/operators/maxout_op.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ class MaxOutKernel : public framework::OpKernel<T> {
3131
Tensor* out = context.Output<Tensor>("Out");
3232
int groups = context.template Attr<int>("groups");
3333

34-
paddle::operators::math::MaxOutFunctor<
35-
Place, T>
36-
maxout_forward;
34+
math::MaxOutFunctor<Place, T> maxout_forward;
3735
maxout_forward(context.device_context(), *in_x, out, groups);
3836
}
3937
};
@@ -53,10 +51,9 @@ class MaxOutGradKernel : public framework::OpKernel<T> {
5351
if (in_x_grad) {
5452
in_x_grad->mutable_data<T>(context.GetPlace());
5553
zero(device_ctx, in_x_grad, static_cast<T>(0.0));
56-
paddle::operators::math::MaxOutGradFunctor<Place, T>
57-
maxout_backward;
58-
maxout_backward(context.device_context(), *in_x, *in_x_grad, *out,
59-
*out_grad, groups);
54+
math::MaxOutGradFunctor<Place, T> maxout_backward;
55+
maxout_backward(context.device_context(), *in_x, in_x_grad, *out,
56+
*out_grad, groups);
6057
}
6158
}
6259
};

python/paddle/v2/fluid/tests/test_maxout_op.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from op_test import OpTest
44

55

6-
def maxout_forward_naive(input, groups,num_channels):
6+
def maxout_forward_naive(input, groups):
77
s0, s1, s2, s3 = input.shape
88
return np.ndarray([s0, s1 / groups, groups, s2, s3], \
99
buffer = input, dtype=input.dtype).max(axis=(2))
@@ -18,7 +18,7 @@ def setUp(self):
1818
self.num_channels).astype("float32")
1919

2020
self.inputs = {'X': input}
21-
self.attrs = {'groups': self.groups, 'num_channels': self.num_channels}
21+
self.attrs = {'groups': self.groups}
2222

2323
self.outputs = {'Out': output.astype('float32')}
2424

@@ -32,7 +32,6 @@ def init_test_case(self):
3232
self.MaxOut_forward_naive = maxout_forward_naive
3333
self.shape = [100, 6, 2, 2]
3434
self.groups=2
35-
self.num_channels=6
3635

3736

3837

0 commit comments

Comments
 (0)