From d2ee3c98df1203ca68e711a1fb04ddbd6d048b33 Mon Sep 17 00:00:00 2001 From: sweetsky0901 Date: Wed, 29 Nov 2017 11:23:46 +0800 Subject: [PATCH] format code --- paddle/operators/math/unpooling.cc | 16 +-- paddle/operators/math/unpooling.cu | 50 ++++---- paddle/operators/math/unpooling.h | 17 +-- paddle/operators/unpool_op.cc | 114 +++++++++--------- paddle/operators/unpool_op.cu.cc | 8 +- .../paddle/v2/fluid/tests/test_unpool_op.py | 5 +- 6 files changed, 110 insertions(+), 100 deletions(-) diff --git a/paddle/operators/math/unpooling.cc b/paddle/operators/math/unpooling.cc index 71928314ba..9017ffaab1 100644 --- a/paddle/operators/math/unpooling.cc +++ b/paddle/operators/math/unpooling.cc @@ -19,9 +19,9 @@ namespace math { template class Unpool2dMaxFunctor { public: - void operator()( - const platform::DeviceContext& context, const framework::Tensor& input, - const framework::Tensor& indices, framework::Tensor* output) { + void operator()(const platform::DeviceContext& context, + const framework::Tensor& input, + const framework::Tensor& indices, framework::Tensor* output) { const int batch_size = input.dims()[0]; const int input_height = input.dims()[2]; const int input_width = input.dims()[3]; @@ -50,10 +50,12 @@ class Unpool2dMaxFunctor { template class Unpool2dMaxGradFunctor { public: - void operator()( - const platform::DeviceContext& context, const framework::Tensor& input, - const framework::Tensor& indices, const framework::Tensor& output, - const framework::Tensor& output_grad, framework::Tensor* input_grad) { + void operator()(const platform::DeviceContext& context, + const framework::Tensor& input, + const framework::Tensor& indices, + const framework::Tensor& output, + const framework::Tensor& output_grad, + framework::Tensor* input_grad) { const int batch_size = input.dims()[0]; const int input_height = input.dims()[2]; const int input_width = input.dims()[3]; diff --git a/paddle/operators/math/unpooling.cu b/paddle/operators/math/unpooling.cu index 4c6cb7bbca..f3a317b3b3 100644 --- a/paddle/operators/math/unpooling.cu +++ b/paddle/operators/math/unpooling.cu @@ -19,10 +19,12 @@ namespace paddle { namespace operators { namespace math { template -__global__ void KernelUnpool2dMax( - const int nthreads, const T* input_data, const int* indices_data, - const int input_height, const int input_width, const int channels, - T* output_data, const int output_height, const int output_width) { +__global__ void KernelUnpool2dMax(const int nthreads, const T* input_data, + const int* indices_data, + const int input_height, const int input_width, + const int channels, T* output_data, + const int output_height, + const int output_width) { int in_n_stride = input_height * input_width * channels; int in_c_stride = input_height * input_width; int out_n_stride = output_height * output_width * channels; @@ -40,11 +42,12 @@ __global__ void KernelUnpool2dMax( } } template -__global__ void KernelUnpool2dMaxGrad( - const int nthreads, const T* input_data, const int* indices_data, - const int input_height, const int input_width, const int channels, - const T* output_data, const T* output_grad, const int output_height, - const int output_width, T* input_grad) { +__global__ void KernelUnpool2dMaxGrad(const int nthreads, const T* input_data, + const int* indices_data, + const int input_height, const int input_width, + const int channels, const T* output_data, + const T* output_grad, const int output_height, + const int output_width, T* input_grad) { int in_n_stride = input_height * input_width * channels; int in_c_stride = input_height * input_width; int out_n_stride = output_height * output_width * channels; @@ -67,9 +70,9 @@ __global__ void KernelUnpool2dMaxGrad( template class Unpool2dMaxFunctor { public: - void operator()( - const platform::DeviceContext& context, const framework::Tensor& input, - const framework::Tensor& indices, framework::Tensor* output) { + void operator()(const platform::DeviceContext& context, + const framework::Tensor& input, const framework::Tensor& indices, + framework::Tensor* output) { const int batch_size = input.dims()[0]; const int input_height = input.dims()[2]; const int input_width = input.dims()[3]; @@ -81,11 +84,12 @@ class Unpool2dMaxFunctor { T* output_data = output->mutable_data(context.GetPlace()); int threads = 1024; int grid = (input.numel() + threads - 1) / threads; - KernelUnpool2dMax<<(context) - .stream()>>>(input.numel(), input_data, indices_data, - input_height, input_width, output_channels, - output_data, output_height, output_width); + KernelUnpool2dMax< + T><<(context) + .stream()>>>(input.numel(), input_data, indices_data, + input_height, input_width, output_channels, + output_data, output_height, output_width); } }; /* @@ -113,11 +117,13 @@ class Unpool2dMaxGradFunctor { T* input_grad_data = input_grad->mutable_data(context.GetPlace()); int threads = 1024; int grid = (input.numel() + threads - 1) / threads; - KernelUnpool2dMaxGrad<<(context) - .stream()>>>(input.numel(), input_data, indices_data, - input_height, input_width, output_channels, output_data, - output_grad_data, output_height, output_width, input_grad_data); + KernelUnpool2dMaxGrad< + T><<(context) + .stream()>>>(input.numel(), input_data, indices_data, + input_height, input_width, output_channels, output_data, + output_grad_data, output_height, output_width, + input_grad_data); } }; template class Unpool2dMaxGradFunctor; diff --git a/paddle/operators/math/unpooling.h b/paddle/operators/math/unpooling.h index 43e32bf4fb..61eadcdcd5 100644 --- a/paddle/operators/math/unpooling.h +++ b/paddle/operators/math/unpooling.h @@ -21,17 +21,20 @@ namespace math { template class Unpool2dMaxFunctor { public: - void operator()( - const platform::DeviceContext& context, const framework::Tensor& input, - const framework::Tensor& indices, framework::Tensor* output); + void operator()(const platform::DeviceContext& context, + const framework::Tensor& input, + const framework::Tensor& indices, + framework::Tensor* output); }; template class Unpool2dMaxGradFunctor { public: - void operator()( - const platform::DeviceContext& context, const framework::Tensor& input, - const framework::Tensor& indices, const framework::Tensor& output, - const framework::Tensor& output_grad, framework::Tensor* input_grad); + void operator()(const platform::DeviceContext& context, + const framework::Tensor& input, + const framework::Tensor& indices, + const framework::Tensor& output, + const framework::Tensor& output_grad, + framework::Tensor* input_grad); }; } // namespace math } // namespace operators diff --git a/paddle/operators/unpool_op.cc b/paddle/operators/unpool_op.cc index a51df3aa42..a40aadcccc 100644 --- a/paddle/operators/unpool_op.cc +++ b/paddle/operators/unpool_op.cc @@ -32,24 +32,22 @@ class Unpool2dOpMaker : public framework::OpProtoAndCheckerMaker { "The format of input tensor is NCHW. Where N is batch size, C is the " "number of channels, H and W is the height and width of feature."); AddOutput("Out", - "(Tensor) The output tensor of unpool operator." - "The format of output tensor is also NCHW." - "Where N is batch size, C is " - "the number of channels, H and W is the height and " - "width of feature."); + "(Tensor) The output tensor of unpool operator." + "The format of output tensor is also NCHW." + "Where N is batch size, C is " + "the number of channels, H and W is the height and " + "width of feature."); AddAttr>( "ksize", "(vector), the unpooling window size(height, width) " "of unpooling operator."); - AddAttr>( - "strides", - "(vector, default:{1, 1}), " - "strides (height, width) of unpooling operator.") + AddAttr>("strides", + "(vector, default:{1, 1}), " + "strides (height, width) of unpooling operator.") .SetDefault({1, 1}); - AddAttr>( - "paddings", - "(vector defalut:{0,0}), " - "paddings (height, width) of unpooling operator.") + AddAttr>("paddings", + "(vector defalut:{0,0}), " + "paddings (height, width) of unpooling operator.") .SetDefault({0, 0}); AddAttr( "unpooling_type", @@ -75,71 +73,71 @@ int OutputSize(int input_size, int ksize, int padding, int stride) { } class UnpoolOp : public framework::OperatorWithKernel { - protected: - framework::OpKernelType GetKernelType( - const framework::ExecutionContext& ctx) const override { + protected: + framework::OpKernelType GetKernelType( + const framework::ExecutionContext& ctx) const override { return framework::OpKernelType( - framework::ToDataType(ctx.Input("X")->type()), + framework::ToDataType(ctx.Input("X")->type()), ctx.device_context()); } - public: - using framework::OperatorWithKernel::OperatorWithKernel; - void InferShape(framework::InferShapeContext* ctx) const override { - PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of UnpoolOp" + public: + using framework::OperatorWithKernel::OperatorWithKernel; + void InferShape(framework::InferShapeContext* ctx) const override { + PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of UnpoolOp" "should not be null."); - PADDLE_ENFORCE(ctx->HasInput("Indices"), "Input(Indices) of UnpoolOp" + PADDLE_ENFORCE(ctx->HasInput("Indices"), "Input(Indices) of UnpoolOp" "should not be null."); - PADDLE_ENFORCE(ctx->HasOutput("Out"), + PADDLE_ENFORCE(ctx->HasOutput("Out"), "Output(Out) of UnpoolOp should not be null."); - auto in_x_dims = ctx->GetInputDim("X"); - auto in_y_dims = ctx->GetInputDim("Indices"); - std::string unpooling_type = + auto in_x_dims = ctx->GetInputDim("X"); + auto in_y_dims = ctx->GetInputDim("Indices"); + std::string unpooling_type = ctx->Attrs().Get("unpooling_type"); - std::vector ksize = ctx->Attrs().Get>("ksize"); - std::vector strides = ctx->Attrs().Get>("strides"); - std::vector paddings = + std::vector ksize = ctx->Attrs().Get>("ksize"); + std::vector strides = ctx->Attrs().Get>("strides"); + std::vector paddings = ctx->Attrs().Get>("paddings"); - PADDLE_ENFORCE(in_x_dims.size() == 4, + PADDLE_ENFORCE(in_x_dims.size() == 4, "Unpooling intput must be of 4-dimensional."); - PADDLE_ENFORCE_EQ(in_x_dims, in_y_dims); - std::vector output_shape({in_x_dims[0], in_x_dims[1]}); - for (size_t i = 0; i < ksize.size(); ++i) { - output_shape.push_back( - OutputSize(in_x_dims[i + 2], ksize[i], paddings[i], strides[i])); - } - ctx->SetOutputDim("Out", framework::make_ddim(output_shape)); - } + PADDLE_ENFORCE_EQ(in_x_dims, in_y_dims); + std::vector output_shape({in_x_dims[0], in_x_dims[1]}); + for (size_t i = 0; i < ksize.size(); ++i) { + output_shape.push_back( + OutputSize(in_x_dims[i + 2], ksize[i], paddings[i], strides[i])); + } + ctx->SetOutputDim("Out", framework::make_ddim(output_shape)); + } }; class UnpoolOpGrad : public framework::OperatorWithKernel { - protected: - framework::OpKernelType GetKernelType( - const framework::ExecutionContext& ctx) const override { - return framework::OpKernelType( - framework::ToDataType(ctx.Input("X")->type()), - ctx.device_context()); - } + protected: + framework::OpKernelType GetKernelType( + const framework::ExecutionContext& ctx) const override { + return framework::OpKernelType( + framework::ToDataType(ctx.Input("X")->type()), + ctx.device_context()); + } - public: - using framework::OperatorWithKernel::OperatorWithKernel; - void InferShape(framework::InferShapeContext* ctx) const override { - PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must not be null."); - PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")), + public: + using framework::OperatorWithKernel::OperatorWithKernel; + void InferShape(framework::InferShapeContext* ctx) const override { + PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must not be null."); + PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")), "Input(X@GRAD) should not be null."); - ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); - } + ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); + } }; -} // namespace operators -} // namespace paddle +} // namespace operators +} // namespace paddle namespace ops = paddle::operators; REGISTER_OP(unpool, ops::UnpoolOp, ops::Unpool2dOpMaker, unpool_grad, ops::UnpoolOpGrad); REGISTER_OP_CPU_KERNEL( - unpool, ops::UnpoolKernel, - ops::UnpoolKernel); + unpool, ops::UnpoolKernel, + ops::UnpoolKernel); REGISTER_OP_CPU_KERNEL( - unpool_grad, ops::UnpoolGradKernel, - ops::UnpoolGradKernel); + unpool_grad, ops::UnpoolGradKernel, + ops::UnpoolGradKernel); diff --git a/paddle/operators/unpool_op.cu.cc b/paddle/operators/unpool_op.cu.cc index 8ee9e2b373..29b393f474 100644 --- a/paddle/operators/unpool_op.cu.cc +++ b/paddle/operators/unpool_op.cu.cc @@ -16,8 +16,8 @@ limitations under the License. */ namespace ops = paddle::operators; REGISTER_OP_GPU_KERNEL( - unpool, ops::UnpoolKernel, - ops::UnpoolKernel); + unpool, ops::UnpoolKernel, + ops::UnpoolKernel); REGISTER_OP_GPU_KERNEL( - unpool_grad, ops::UnpoolGradKernel, - ops::UnpoolGradKernel); + unpool_grad, ops::UnpoolGradKernel, + ops::UnpoolGradKernel); diff --git a/python/paddle/v2/fluid/tests/test_unpool_op.py b/python/paddle/v2/fluid/tests/test_unpool_op.py index 321cd9fab8..e87f283042 100644 --- a/python/paddle/v2/fluid/tests/test_unpool_op.py +++ b/python/paddle/v2/fluid/tests/test_unpool_op.py @@ -55,13 +55,13 @@ class TestUnpoolOp(OpTest): self.inputs = { 'X': input.astype('float32'), 'Indices': indices.astype('int32') - } + } self.attrs = { 'strides': self.strides, 'paddings': self.paddings, 'ksize': self.ksize, 'unpooling_type': self.unpooling_type, - } + } self.outputs = {'Out': output.astype('float32')} def test_check_output(self): @@ -78,5 +78,6 @@ class TestUnpoolOp(OpTest): self.strides = [2, 2] self.paddings = [0, 0] + if __name__ == '__main__': unittest.main() -- GitLab