Skip to content

Commit d2ee3c9

Browse files
author
sweetsky0901
committed
format code
1 parent 2d42fa7 commit d2ee3c9

File tree

6 files changed

+110
-100
lines changed

6 files changed

+110
-100
lines changed

paddle/operators/math/unpooling.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ namespace math {
1919
template <typename T>
2020
class Unpool2dMaxFunctor<platform::CPUPlace, T> {
2121
public:
22-
void operator()(
23-
const platform::DeviceContext& context, const framework::Tensor& input,
24-
const framework::Tensor& indices, framework::Tensor* output) {
22+
void operator()(const platform::DeviceContext& context,
23+
const framework::Tensor& input,
24+
const framework::Tensor& indices, framework::Tensor* output) {
2525
const int batch_size = input.dims()[0];
2626
const int input_height = input.dims()[2];
2727
const int input_width = input.dims()[3];
@@ -50,10 +50,12 @@ class Unpool2dMaxFunctor<platform::CPUPlace, T> {
5050
template <class T>
5151
class Unpool2dMaxGradFunctor<platform::CPUPlace, T> {
5252
public:
53-
void operator()(
54-
const platform::DeviceContext& context, const framework::Tensor& input,
55-
const framework::Tensor& indices, const framework::Tensor& output,
56-
const framework::Tensor& output_grad, framework::Tensor* input_grad) {
53+
void operator()(const platform::DeviceContext& context,
54+
const framework::Tensor& input,
55+
const framework::Tensor& indices,
56+
const framework::Tensor& output,
57+
const framework::Tensor& output_grad,
58+
framework::Tensor* input_grad) {
5759
const int batch_size = input.dims()[0];
5860
const int input_height = input.dims()[2];
5961
const int input_width = input.dims()[3];

paddle/operators/math/unpooling.cu

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ namespace paddle {
1919
namespace operators {
2020
namespace math {
2121
template <typename T>
22-
__global__ void KernelUnpool2dMax(
23-
const int nthreads, const T* input_data, const int* indices_data,
24-
const int input_height, const int input_width, const int channels,
25-
T* output_data, const int output_height, const int output_width) {
22+
__global__ void KernelUnpool2dMax(const int nthreads, const T* input_data,
23+
const int* indices_data,
24+
const int input_height, const int input_width,
25+
const int channels, T* output_data,
26+
const int output_height,
27+
const int output_width) {
2628
int in_n_stride = input_height * input_width * channels;
2729
int in_c_stride = input_height * input_width;
2830
int out_n_stride = output_height * output_width * channels;
@@ -40,11 +42,12 @@ __global__ void KernelUnpool2dMax(
4042
}
4143
}
4244
template <typename T>
43-
__global__ void KernelUnpool2dMaxGrad(
44-
const int nthreads, const T* input_data, const int* indices_data,
45-
const int input_height, const int input_width, const int channels,
46-
const T* output_data, const T* output_grad, const int output_height,
47-
const int output_width, T* input_grad) {
45+
__global__ void KernelUnpool2dMaxGrad(const int nthreads, const T* input_data,
46+
const int* indices_data,
47+
const int input_height, const int input_width,
48+
const int channels, const T* output_data,
49+
const T* output_grad, const int output_height,
50+
const int output_width, T* input_grad) {
4851
int in_n_stride = input_height * input_width * channels;
4952
int in_c_stride = input_height * input_width;
5053
int out_n_stride = output_height * output_width * channels;
@@ -67,9 +70,9 @@ __global__ void KernelUnpool2dMaxGrad(
6770
template <typename T>
6871
class Unpool2dMaxFunctor<platform::GPUPlace, T> {
6972
public:
70-
void operator()(
71-
const platform::DeviceContext& context, const framework::Tensor& input,
72-
const framework::Tensor& indices, framework::Tensor* output) {
73+
void operator()(const platform::DeviceContext& context,
74+
const framework::Tensor& input, const framework::Tensor& indices,
75+
framework::Tensor* output) {
7376
const int batch_size = input.dims()[0];
7477
const int input_height = input.dims()[2];
7578
const int input_width = input.dims()[3];
@@ -81,11 +84,12 @@ class Unpool2dMaxFunctor<platform::GPUPlace, T> {
8184
T* output_data = output->mutable_data<T>(context.GetPlace());
8285
int threads = 1024;
8386
int grid = (input.numel() + threads - 1) / threads;
84-
KernelUnpool2dMax<T><<<grid, threads, 0,
85-
reinterpret_cast<const platform::CUDADeviceContext&>(context)
86-
.stream()>>>(input.numel(), input_data, indices_data,
87-
input_height, input_width, output_channels,
88-
output_data, output_height, output_width);
87+
KernelUnpool2dMax<
88+
T><<<grid, threads, 0,
89+
reinterpret_cast<const platform::CUDADeviceContext&>(context)
90+
.stream()>>>(input.numel(), input_data, indices_data,
91+
input_height, input_width, output_channels,
92+
output_data, output_height, output_width);
8993
}
9094
};
9195
/*
@@ -113,11 +117,13 @@ class Unpool2dMaxGradFunctor<platform::GPUPlace, T> {
113117
T* input_grad_data = input_grad->mutable_data<T>(context.GetPlace());
114118
int threads = 1024;
115119
int grid = (input.numel() + threads - 1) / threads;
116-
KernelUnpool2dMaxGrad<T><<<grid, threads, 0,
117-
reinterpret_cast<const platform::CUDADeviceContext&>(context)
118-
.stream()>>>(input.numel(), input_data, indices_data,
119-
input_height, input_width, output_channels, output_data,
120-
output_grad_data, output_height, output_width, input_grad_data);
120+
KernelUnpool2dMaxGrad<
121+
T><<<grid, threads, 0,
122+
reinterpret_cast<const platform::CUDADeviceContext&>(context)
123+
.stream()>>>(input.numel(), input_data, indices_data,
124+
input_height, input_width, output_channels, output_data,
125+
output_grad_data, output_height, output_width,
126+
input_grad_data);
121127
}
122128
};
123129
template class Unpool2dMaxGradFunctor<platform::GPUPlace, float>;

paddle/operators/math/unpooling.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,20 @@ namespace math {
2121
template <typename Place, typename T>
2222
class Unpool2dMaxFunctor {
2323
public:
24-
void operator()(
25-
const platform::DeviceContext& context, const framework::Tensor& input,
26-
const framework::Tensor& indices, framework::Tensor* output);
24+
void operator()(const platform::DeviceContext& context,
25+
const framework::Tensor& input,
26+
const framework::Tensor& indices,
27+
framework::Tensor* output);
2728
};
2829
template <typename Place, class T>
2930
class Unpool2dMaxGradFunctor {
3031
public:
31-
void operator()(
32-
const platform::DeviceContext& context, const framework::Tensor& input,
33-
const framework::Tensor& indices, const framework::Tensor& output,
34-
const framework::Tensor& output_grad, framework::Tensor* input_grad);
32+
void operator()(const platform::DeviceContext& context,
33+
const framework::Tensor& input,
34+
const framework::Tensor& indices,
35+
const framework::Tensor& output,
36+
const framework::Tensor& output_grad,
37+
framework::Tensor* input_grad);
3538
};
3639
} // namespace math
3740
} // namespace operators

paddle/operators/unpool_op.cc

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,22 @@ class Unpool2dOpMaker : public framework::OpProtoAndCheckerMaker {
3232
"The format of input tensor is NCHW. Where N is batch size, C is the "
3333
"number of channels, H and W is the height and width of feature.");
3434
AddOutput("Out",
35-
"(Tensor) The output tensor of unpool operator."
36-
"The format of output tensor is also NCHW."
37-
"Where N is batch size, C is "
38-
"the number of channels, H and W is the height and "
39-
"width of feature.");
35+
"(Tensor) The output tensor of unpool operator."
36+
"The format of output tensor is also NCHW."
37+
"Where N is batch size, C is "
38+
"the number of channels, H and W is the height and "
39+
"width of feature.");
4040
AddAttr<std::vector<int>>(
4141
"ksize",
4242
"(vector), the unpooling window size(height, width) "
4343
"of unpooling operator.");
44-
AddAttr<std::vector<int>>(
45-
"strides",
46-
"(vector, default:{1, 1}), "
47-
"strides (height, width) of unpooling operator.")
44+
AddAttr<std::vector<int>>("strides",
45+
"(vector, default:{1, 1}), "
46+
"strides (height, width) of unpooling operator.")
4847
.SetDefault({1, 1});
49-
AddAttr<std::vector<int>>(
50-
"paddings",
51-
"(vector defalut:{0,0}), "
52-
"paddings (height, width) of unpooling operator.")
48+
AddAttr<std::vector<int>>("paddings",
49+
"(vector defalut:{0,0}), "
50+
"paddings (height, width) of unpooling operator.")
5351
.SetDefault({0, 0});
5452
AddAttr<std::string>(
5553
"unpooling_type",
@@ -75,71 +73,71 @@ int OutputSize(int input_size, int ksize, int padding, int stride) {
7573
}
7674

7775
class UnpoolOp : public framework::OperatorWithKernel {
78-
protected:
79-
framework::OpKernelType GetKernelType(
80-
const framework::ExecutionContext& ctx) const override {
76+
protected:
77+
framework::OpKernelType GetKernelType(
78+
const framework::ExecutionContext& ctx) const override {
8179
return framework::OpKernelType(
82-
framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
80+
framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
8381
ctx.device_context());
8482
}
8583

86-
public:
87-
using framework::OperatorWithKernel::OperatorWithKernel;
88-
void InferShape(framework::InferShapeContext* ctx) const override {
89-
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of UnpoolOp"
84+
public:
85+
using framework::OperatorWithKernel::OperatorWithKernel;
86+
void InferShape(framework::InferShapeContext* ctx) const override {
87+
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of UnpoolOp"
9088
"should not be null.");
91-
PADDLE_ENFORCE(ctx->HasInput("Indices"), "Input(Indices) of UnpoolOp"
89+
PADDLE_ENFORCE(ctx->HasInput("Indices"), "Input(Indices) of UnpoolOp"
9290
"should not be null.");
93-
PADDLE_ENFORCE(ctx->HasOutput("Out"),
91+
PADDLE_ENFORCE(ctx->HasOutput("Out"),
9492
"Output(Out) of UnpoolOp should not be null.");
95-
auto in_x_dims = ctx->GetInputDim("X");
96-
auto in_y_dims = ctx->GetInputDim("Indices");
97-
std::string unpooling_type =
93+
auto in_x_dims = ctx->GetInputDim("X");
94+
auto in_y_dims = ctx->GetInputDim("Indices");
95+
std::string unpooling_type =
9896
ctx->Attrs().Get<std::string>("unpooling_type");
99-
std::vector<int> ksize = ctx->Attrs().Get<std::vector<int>>("ksize");
100-
std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
101-
std::vector<int> paddings =
97+
std::vector<int> ksize = ctx->Attrs().Get<std::vector<int>>("ksize");
98+
std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
99+
std::vector<int> paddings =
102100
ctx->Attrs().Get<std::vector<int>>("paddings");
103-
PADDLE_ENFORCE(in_x_dims.size() == 4,
101+
PADDLE_ENFORCE(in_x_dims.size() == 4,
104102
"Unpooling intput must be of 4-dimensional.");
105-
PADDLE_ENFORCE_EQ(in_x_dims, in_y_dims);
106-
std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]});
107-
for (size_t i = 0; i < ksize.size(); ++i) {
108-
output_shape.push_back(
109-
OutputSize(in_x_dims[i + 2], ksize[i], paddings[i], strides[i]));
110-
}
111-
ctx->SetOutputDim("Out", framework::make_ddim(output_shape));
112-
}
103+
PADDLE_ENFORCE_EQ(in_x_dims, in_y_dims);
104+
std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]});
105+
for (size_t i = 0; i < ksize.size(); ++i) {
106+
output_shape.push_back(
107+
OutputSize(in_x_dims[i + 2], ksize[i], paddings[i], strides[i]));
108+
}
109+
ctx->SetOutputDim("Out", framework::make_ddim(output_shape));
110+
}
113111
};
114112

115113
class UnpoolOpGrad : public framework::OperatorWithKernel {
116-
protected:
117-
framework::OpKernelType GetKernelType(
118-
const framework::ExecutionContext& ctx) const override {
119-
return framework::OpKernelType(
120-
framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
121-
ctx.device_context());
122-
}
114+
protected:
115+
framework::OpKernelType GetKernelType(
116+
const framework::ExecutionContext& ctx) const override {
117+
return framework::OpKernelType(
118+
framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
119+
ctx.device_context());
120+
}
123121

124-
public:
125-
using framework::OperatorWithKernel::OperatorWithKernel;
126-
void InferShape(framework::InferShapeContext* ctx) const override {
127-
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must not be null.");
128-
PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
122+
public:
123+
using framework::OperatorWithKernel::OperatorWithKernel;
124+
void InferShape(framework::InferShapeContext* ctx) const override {
125+
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must not be null.");
126+
PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
129127
"Input(X@GRAD) should not be null.");
130-
ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
131-
}
128+
ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
129+
}
132130
};
133-
} // namespace operators
134-
} // namespace paddle
131+
} // namespace operators
132+
} // namespace paddle
135133

136134
namespace ops = paddle::operators;
137135
REGISTER_OP(unpool, ops::UnpoolOp, ops::Unpool2dOpMaker, unpool_grad,
138136
ops::UnpoolOpGrad);
139137
REGISTER_OP_CPU_KERNEL(
140-
unpool, ops::UnpoolKernel<paddle::platform::CPUPlace, float>,
141-
ops::UnpoolKernel<paddle::platform::CPUPlace, double>);
138+
unpool, ops::UnpoolKernel<paddle::platform::CPUPlace, float>,
139+
ops::UnpoolKernel<paddle::platform::CPUPlace, double>);
142140
REGISTER_OP_CPU_KERNEL(
143-
unpool_grad, ops::UnpoolGradKernel<paddle::platform::CPUPlace, float>,
144-
ops::UnpoolGradKernel<paddle::platform::CPUPlace, double>);
141+
unpool_grad, ops::UnpoolGradKernel<paddle::platform::CPUPlace, float>,
142+
ops::UnpoolGradKernel<paddle::platform::CPUPlace, double>);
145143

paddle/operators/unpool_op.cu.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ limitations under the License. */
1616

1717
namespace ops = paddle::operators;
1818
REGISTER_OP_GPU_KERNEL(
19-
unpool, ops::UnpoolKernel<paddle::platform::GPUPlace, float>,
20-
ops::UnpoolKernel<paddle::platform::GPUPlace, double>);
19+
unpool, ops::UnpoolKernel<paddle::platform::GPUPlace, float>,
20+
ops::UnpoolKernel<paddle::platform::GPUPlace, double>);
2121
REGISTER_OP_GPU_KERNEL(
22-
unpool_grad, ops::UnpoolGradKernel<paddle::platform::GPUPlace, float>,
23-
ops::UnpoolGradKernel<paddle::platform::GPUPlace, double>);
22+
unpool_grad, ops::UnpoolGradKernel<paddle::platform::GPUPlace, float>,
23+
ops::UnpoolGradKernel<paddle::platform::GPUPlace, double>);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ def setUp(self):
5555
self.inputs = {
5656
'X': input.astype('float32'),
5757
'Indices': indices.astype('int32')
58-
}
58+
}
5959
self.attrs = {
6060
'strides': self.strides,
6161
'paddings': self.paddings,
6262
'ksize': self.ksize,
6363
'unpooling_type': self.unpooling_type,
64-
}
64+
}
6565
self.outputs = {'Out': output.astype('float32')}
6666

6767
def test_check_output(self):
@@ -78,5 +78,6 @@ def init_test_case(self):
7878
self.strides = [2, 2]
7979
self.paddings = [0, 0]
8080

81+
8182
if __name__ == '__main__':
8283
unittest.main()

0 commit comments

Comments
 (0)