/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #pragma once #include #include #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/math/im2col.h" #include "paddle/fluid/operators/math/math_function.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; template class FoldOpKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { const Tensor* input = ctx.Input("X"); const int batch_size = static_cast(input->dims()[0]); Tensor* output = ctx.Output("Y"); output->mutable_data(ctx.GetPlace()); std::vector output_sizes = ctx.Attr>("output_sizes"); std::vector kernel_sizes = ctx.Attr>("kernel_sizes"); std::vector strides = ctx.Attr>("strides"); std::vector paddings = ctx.Attr>("paddings"); std::vector dilations = ctx.Attr>("dilations"); math::Col2ImFunctor col2im; auto& dev_ctx = ctx.template device_context(); auto input_dims = input->dims(); int output_height = (output_sizes[0] + 2 * paddings[0] - (dilations[0] * (kernel_sizes[0] - 1) + 1)) / strides[0] + 1; int output_width = (output_sizes[1] + 2 * paddings[1] - (dilations[1] * (kernel_sizes[1] - 1) + 1)) / strides[1] + 1; int n_input_plane = input_dims[1]; int n_output_plane = n_input_plane / (kernel_sizes[0] * kernel_sizes[1]); framework::DDim output_shape( {n_output_plane, output_sizes[0], output_sizes[1]}); framework::DDim input_matrix_shape({input_dims[0], kernel_sizes[0], kernel_sizes[1], output_height, output_width}); math::SetConstant set_zero; set_zero(dev_ctx, output, static_cast(0)); for (int i = 0; i < batch_size; i++) { Tensor out_batch = output->Slice(i, i + 1).Resize(output_shape); // im size=3 Tensor in_batch = input->Slice(i, i + 1).Resize(input_matrix_shape); // col size=5 col2im(dev_ctx, in_batch, dilations, strides, paddings, &out_batch); } } }; template class FoldGradOpKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { const Tensor* output_grad = ctx.Input(framework::GradVarName("Y")); Tensor* input_grad = ctx.Output(framework::GradVarName("X")); input_grad->mutable_data(ctx.GetPlace()); if ((!output_grad) || (!input_grad)) return; std::vector output_sizes = ctx.Attr>("output_sizes"); std::vector kernel_sizes = ctx.Attr>("kernel_sizes"); std::vector strides = ctx.Attr>("strides"); std::vector paddings = ctx.Attr>("paddings"); std::vector dilations = ctx.Attr>("dilations"); const int batch_size = static_cast(input_grad->dims()[0]); auto input_dims = input_grad->dims(); int output_height = (output_sizes[0] + 2 * paddings[0] - (dilations[0] * (kernel_sizes[0] - 1) + 1)) / strides[0] + 1; int output_width = (output_sizes[1] + 2 * paddings[1] - (dilations[1] * (kernel_sizes[1] - 1) + 1)) / strides[1] + 1; int n_input_plane = input_dims[1]; int n_output_plane = n_input_plane / (kernel_sizes[0] * kernel_sizes[1]); framework::DDim output_shape( {n_output_plane, output_sizes[0], output_sizes[1]}); framework::DDim input_matrix_shape({input_dims[0], kernel_sizes[0], kernel_sizes[1], output_height, output_width}); math::Im2ColFunctor im2col; auto& dev_ctx = ctx.template device_context(); for (int i = 0; i < batch_size; i++) { Tensor out_grad_batch = output_grad->Slice(i, i + 1).Resize(output_shape); Tensor in_grad_batch = input_grad->Slice(i, i + 1).Resize(input_matrix_shape); im2col(dev_ctx, out_grad_batch, dilations, strides, paddings, &in_grad_batch); } } }; } // namespace operators } // namespace paddle