提交 d2ee3c98 编写于 作者: S sweetsky0901

format code

上级 2d42fa74
...@@ -19,9 +19,9 @@ namespace math { ...@@ -19,9 +19,9 @@ namespace math {
template <typename T> template <typename T>
class Unpool2dMaxFunctor<platform::CPUPlace, T> { class Unpool2dMaxFunctor<platform::CPUPlace, T> {
public: public:
void operator()( void operator()(const platform::DeviceContext& context,
const platform::DeviceContext& context, const framework::Tensor& input, const framework::Tensor& input,
const framework::Tensor& indices, framework::Tensor* output) { const framework::Tensor& indices, framework::Tensor* output) {
const int batch_size = input.dims()[0]; const int batch_size = input.dims()[0];
const int input_height = input.dims()[2]; const int input_height = input.dims()[2];
const int input_width = input.dims()[3]; const int input_width = input.dims()[3];
...@@ -50,10 +50,12 @@ class Unpool2dMaxFunctor<platform::CPUPlace, T> { ...@@ -50,10 +50,12 @@ class Unpool2dMaxFunctor<platform::CPUPlace, T> {
template <class T> template <class T>
class Unpool2dMaxGradFunctor<platform::CPUPlace, T> { class Unpool2dMaxGradFunctor<platform::CPUPlace, T> {
public: public:
void operator()( void operator()(const platform::DeviceContext& context,
const platform::DeviceContext& context, const framework::Tensor& input, const framework::Tensor& input,
const framework::Tensor& indices, const framework::Tensor& output, const framework::Tensor& indices,
const framework::Tensor& output_grad, framework::Tensor* input_grad) { const framework::Tensor& output,
const framework::Tensor& output_grad,
framework::Tensor* input_grad) {
const int batch_size = input.dims()[0]; const int batch_size = input.dims()[0];
const int input_height = input.dims()[2]; const int input_height = input.dims()[2];
const int input_width = input.dims()[3]; const int input_width = input.dims()[3];
......
...@@ -19,10 +19,12 @@ namespace paddle { ...@@ -19,10 +19,12 @@ namespace paddle {
namespace operators { namespace operators {
namespace math { namespace math {
template <typename T> template <typename T>
__global__ void KernelUnpool2dMax( __global__ void KernelUnpool2dMax(const int nthreads, const T* input_data,
const int nthreads, const T* input_data, const int* indices_data, const int* indices_data,
const int input_height, const int input_width, const int channels, const int input_height, const int input_width,
T* output_data, const int output_height, const int output_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_n_stride = input_height * input_width * channels;
int in_c_stride = input_height * input_width; int in_c_stride = input_height * input_width;
int out_n_stride = output_height * output_width * channels; int out_n_stride = output_height * output_width * channels;
...@@ -40,11 +42,12 @@ __global__ void KernelUnpool2dMax( ...@@ -40,11 +42,12 @@ __global__ void KernelUnpool2dMax(
} }
} }
template <typename T> template <typename T>
__global__ void KernelUnpool2dMaxGrad( __global__ void KernelUnpool2dMaxGrad(const int nthreads, const T* input_data,
const int nthreads, const T* input_data, const int* indices_data, const int* indices_data,
const int input_height, const int input_width, const int channels, const int input_height, const int input_width,
const T* output_data, const T* output_grad, const int output_height, const int channels, const T* output_data,
const int output_width, T* input_grad) { 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_n_stride = input_height * input_width * channels;
int in_c_stride = input_height * input_width; int in_c_stride = input_height * input_width;
int out_n_stride = output_height * output_width * channels; int out_n_stride = output_height * output_width * channels;
...@@ -67,9 +70,9 @@ __global__ void KernelUnpool2dMaxGrad( ...@@ -67,9 +70,9 @@ __global__ void KernelUnpool2dMaxGrad(
template <typename T> template <typename T>
class Unpool2dMaxFunctor<platform::GPUPlace, T> { class Unpool2dMaxFunctor<platform::GPUPlace, T> {
public: public:
void operator()( void operator()(const platform::DeviceContext& context,
const platform::DeviceContext& context, const framework::Tensor& input, const framework::Tensor& input, const framework::Tensor& indices,
const framework::Tensor& indices, framework::Tensor* output) { framework::Tensor* output) {
const int batch_size = input.dims()[0]; const int batch_size = input.dims()[0];
const int input_height = input.dims()[2]; const int input_height = input.dims()[2];
const int input_width = input.dims()[3]; const int input_width = input.dims()[3];
...@@ -81,11 +84,12 @@ class Unpool2dMaxFunctor<platform::GPUPlace, T> { ...@@ -81,11 +84,12 @@ class Unpool2dMaxFunctor<platform::GPUPlace, T> {
T* output_data = output->mutable_data<T>(context.GetPlace()); T* output_data = output->mutable_data<T>(context.GetPlace());
int threads = 1024; int threads = 1024;
int grid = (input.numel() + threads - 1) / threads; int grid = (input.numel() + threads - 1) / threads;
KernelUnpool2dMax<T><<<grid, threads, 0, KernelUnpool2dMax<
reinterpret_cast<const platform::CUDADeviceContext&>(context) T><<<grid, threads, 0,
.stream()>>>(input.numel(), input_data, indices_data, reinterpret_cast<const platform::CUDADeviceContext&>(context)
input_height, input_width, output_channels, .stream()>>>(input.numel(), input_data, indices_data,
output_data, output_height, output_width); input_height, input_width, output_channels,
output_data, output_height, output_width);
} }
}; };
/* /*
...@@ -113,11 +117,13 @@ class Unpool2dMaxGradFunctor<platform::GPUPlace, T> { ...@@ -113,11 +117,13 @@ class Unpool2dMaxGradFunctor<platform::GPUPlace, T> {
T* input_grad_data = input_grad->mutable_data<T>(context.GetPlace()); T* input_grad_data = input_grad->mutable_data<T>(context.GetPlace());
int threads = 1024; int threads = 1024;
int grid = (input.numel() + threads - 1) / threads; int grid = (input.numel() + threads - 1) / threads;
KernelUnpool2dMaxGrad<T><<<grid, threads, 0, KernelUnpool2dMaxGrad<
reinterpret_cast<const platform::CUDADeviceContext&>(context) T><<<grid, threads, 0,
.stream()>>>(input.numel(), input_data, indices_data, reinterpret_cast<const platform::CUDADeviceContext&>(context)
input_height, input_width, output_channels, output_data, .stream()>>>(input.numel(), input_data, indices_data,
output_grad_data, output_height, output_width, input_grad_data); input_height, input_width, output_channels, output_data,
output_grad_data, output_height, output_width,
input_grad_data);
} }
}; };
template class Unpool2dMaxGradFunctor<platform::GPUPlace, float>; template class Unpool2dMaxGradFunctor<platform::GPUPlace, float>;
......
...@@ -21,17 +21,20 @@ namespace math { ...@@ -21,17 +21,20 @@ namespace math {
template <typename Place, typename T> template <typename Place, typename T>
class Unpool2dMaxFunctor { class Unpool2dMaxFunctor {
public: public:
void operator()( void operator()(const platform::DeviceContext& context,
const platform::DeviceContext& context, const framework::Tensor& input, const framework::Tensor& input,
const framework::Tensor& indices, framework::Tensor* output); const framework::Tensor& indices,
framework::Tensor* output);
}; };
template <typename Place, class T> template <typename Place, class T>
class Unpool2dMaxGradFunctor { class Unpool2dMaxGradFunctor {
public: public:
void operator()( void operator()(const platform::DeviceContext& context,
const platform::DeviceContext& context, const framework::Tensor& input, const framework::Tensor& input,
const framework::Tensor& indices, const framework::Tensor& output, const framework::Tensor& indices,
const framework::Tensor& output_grad, framework::Tensor* input_grad); const framework::Tensor& output,
const framework::Tensor& output_grad,
framework::Tensor* input_grad);
}; };
} // namespace math } // namespace math
} // namespace operators } // namespace operators
......
...@@ -32,24 +32,22 @@ class Unpool2dOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -32,24 +32,22 @@ class Unpool2dOpMaker : public framework::OpProtoAndCheckerMaker {
"The format of input tensor is NCHW. Where N is batch size, C is the " "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."); "number of channels, H and W is the height and width of feature.");
AddOutput("Out", AddOutput("Out",
"(Tensor) The output tensor of unpool operator." "(Tensor) The output tensor of unpool operator."
"The format of output tensor is also NCHW." "The format of output tensor is also NCHW."
"Where N is batch size, C is " "Where N is batch size, C is "
"the number of channels, H and W is the height and " "the number of channels, H and W is the height and "
"width of feature."); "width of feature.");
AddAttr<std::vector<int>>( AddAttr<std::vector<int>>(
"ksize", "ksize",
"(vector), the unpooling window size(height, width) " "(vector), the unpooling window size(height, width) "
"of unpooling operator."); "of unpooling operator.");
AddAttr<std::vector<int>>( AddAttr<std::vector<int>>("strides",
"strides", "(vector, default:{1, 1}), "
"(vector, default:{1, 1}), " "strides (height, width) of unpooling operator.")
"strides (height, width) of unpooling operator.")
.SetDefault({1, 1}); .SetDefault({1, 1});
AddAttr<std::vector<int>>( AddAttr<std::vector<int>>("paddings",
"paddings", "(vector defalut:{0,0}), "
"(vector defalut:{0,0}), " "paddings (height, width) of unpooling operator.")
"paddings (height, width) of unpooling operator.")
.SetDefault({0, 0}); .SetDefault({0, 0});
AddAttr<std::string>( AddAttr<std::string>(
"unpooling_type", "unpooling_type",
...@@ -75,71 +73,71 @@ int OutputSize(int input_size, int ksize, int padding, int stride) { ...@@ -75,71 +73,71 @@ int OutputSize(int input_size, int ksize, int padding, int stride) {
} }
class UnpoolOp : public framework::OperatorWithKernel { class UnpoolOp : public framework::OperatorWithKernel {
protected: protected:
framework::OpKernelType GetKernelType( framework::OpKernelType GetKernelType(
const framework::ExecutionContext& ctx) const override { const framework::ExecutionContext& ctx) const override {
return framework::OpKernelType( return framework::OpKernelType(
framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()), framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
ctx.device_context()); ctx.device_context());
} }
public: public:
using framework::OperatorWithKernel::OperatorWithKernel; using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override { void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of UnpoolOp" PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of UnpoolOp"
"should not be null."); "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."); "should not be null.");
PADDLE_ENFORCE(ctx->HasOutput("Out"), PADDLE_ENFORCE(ctx->HasOutput("Out"),
"Output(Out) of UnpoolOp should not be null."); "Output(Out) of UnpoolOp should not be null.");
auto in_x_dims = ctx->GetInputDim("X"); auto in_x_dims = ctx->GetInputDim("X");
auto in_y_dims = ctx->GetInputDim("Indices"); auto in_y_dims = ctx->GetInputDim("Indices");
std::string unpooling_type = std::string unpooling_type =
ctx->Attrs().Get<std::string>("unpooling_type"); ctx->Attrs().Get<std::string>("unpooling_type");
std::vector<int> ksize = ctx->Attrs().Get<std::vector<int>>("ksize"); std::vector<int> ksize = ctx->Attrs().Get<std::vector<int>>("ksize");
std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides"); std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
std::vector<int> paddings = std::vector<int> paddings =
ctx->Attrs().Get<std::vector<int>>("paddings"); ctx->Attrs().Get<std::vector<int>>("paddings");
PADDLE_ENFORCE(in_x_dims.size() == 4, PADDLE_ENFORCE(in_x_dims.size() == 4,
"Unpooling intput must be of 4-dimensional."); "Unpooling intput must be of 4-dimensional.");
PADDLE_ENFORCE_EQ(in_x_dims, in_y_dims); PADDLE_ENFORCE_EQ(in_x_dims, in_y_dims);
std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]}); std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]});
for (size_t i = 0; i < ksize.size(); ++i) { for (size_t i = 0; i < ksize.size(); ++i) {
output_shape.push_back( output_shape.push_back(
OutputSize(in_x_dims[i + 2], ksize[i], paddings[i], strides[i])); OutputSize(in_x_dims[i + 2], ksize[i], paddings[i], strides[i]));
} }
ctx->SetOutputDim("Out", framework::make_ddim(output_shape)); ctx->SetOutputDim("Out", framework::make_ddim(output_shape));
} }
}; };
class UnpoolOpGrad : public framework::OperatorWithKernel { class UnpoolOpGrad : public framework::OperatorWithKernel {
protected: protected:
framework::OpKernelType GetKernelType( framework::OpKernelType GetKernelType(
const framework::ExecutionContext& ctx) const override { const framework::ExecutionContext& ctx) const override {
return framework::OpKernelType( return framework::OpKernelType(
framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()), framework::ToDataType(ctx.Input<framework::Tensor>("X")->type()),
ctx.device_context()); ctx.device_context());
} }
public: public:
using framework::OperatorWithKernel::OperatorWithKernel; using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override { void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must not be null."); PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must not be null.");
PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")), PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
"Input(X@GRAD) should not be null."); "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 operators
} // namespace paddle } // namespace paddle
namespace ops = paddle::operators; namespace ops = paddle::operators;
REGISTER_OP(unpool, ops::UnpoolOp, ops::Unpool2dOpMaker, unpool_grad, REGISTER_OP(unpool, ops::UnpoolOp, ops::Unpool2dOpMaker, unpool_grad,
ops::UnpoolOpGrad); ops::UnpoolOpGrad);
REGISTER_OP_CPU_KERNEL( REGISTER_OP_CPU_KERNEL(
unpool, ops::UnpoolKernel<paddle::platform::CPUPlace, float>, unpool, ops::UnpoolKernel<paddle::platform::CPUPlace, float>,
ops::UnpoolKernel<paddle::platform::CPUPlace, double>); ops::UnpoolKernel<paddle::platform::CPUPlace, double>);
REGISTER_OP_CPU_KERNEL( REGISTER_OP_CPU_KERNEL(
unpool_grad, ops::UnpoolGradKernel<paddle::platform::CPUPlace, float>, unpool_grad, ops::UnpoolGradKernel<paddle::platform::CPUPlace, float>,
ops::UnpoolGradKernel<paddle::platform::CPUPlace, double>); ops::UnpoolGradKernel<paddle::platform::CPUPlace, double>);
...@@ -16,8 +16,8 @@ limitations under the License. */ ...@@ -16,8 +16,8 @@ limitations under the License. */
namespace ops = paddle::operators; namespace ops = paddle::operators;
REGISTER_OP_GPU_KERNEL( REGISTER_OP_GPU_KERNEL(
unpool, ops::UnpoolKernel<paddle::platform::GPUPlace, float>, unpool, ops::UnpoolKernel<paddle::platform::GPUPlace, float>,
ops::UnpoolKernel<paddle::platform::GPUPlace, double>); ops::UnpoolKernel<paddle::platform::GPUPlace, double>);
REGISTER_OP_GPU_KERNEL( REGISTER_OP_GPU_KERNEL(
unpool_grad, ops::UnpoolGradKernel<paddle::platform::GPUPlace, float>, unpool_grad, ops::UnpoolGradKernel<paddle::platform::GPUPlace, float>,
ops::UnpoolGradKernel<paddle::platform::GPUPlace, double>); ops::UnpoolGradKernel<paddle::platform::GPUPlace, double>);
...@@ -55,13 +55,13 @@ class TestUnpoolOp(OpTest): ...@@ -55,13 +55,13 @@ class TestUnpoolOp(OpTest):
self.inputs = { self.inputs = {
'X': input.astype('float32'), 'X': input.astype('float32'),
'Indices': indices.astype('int32') 'Indices': indices.astype('int32')
} }
self.attrs = { self.attrs = {
'strides': self.strides, 'strides': self.strides,
'paddings': self.paddings, 'paddings': self.paddings,
'ksize': self.ksize, 'ksize': self.ksize,
'unpooling_type': self.unpooling_type, 'unpooling_type': self.unpooling_type,
} }
self.outputs = {'Out': output.astype('float32')} self.outputs = {'Out': output.astype('float32')}
def test_check_output(self): def test_check_output(self):
...@@ -78,5 +78,6 @@ class TestUnpoolOp(OpTest): ...@@ -78,5 +78,6 @@ class TestUnpoolOp(OpTest):
self.strides = [2, 2] self.strides = [2, 2]
self.paddings = [0, 0] self.paddings = [0, 0]
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册