/* Copyright (c) 2019 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 "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/pten_utils.h" #include "paddle/fluid/operators/math/pooling.h" #include "paddle/fluid/platform/device_context.h" #include "paddle/pten/kernels/empty_kernel.h" #include "paddle/pten/kernels/flatten_grad_kernel.h" #include "paddle/pten/kernels/flatten_kernel.h" #include "paddle/pten/kernels/funcs/blas/blas.h" #include "paddle/pten/kernels/funcs/math_function.h" namespace paddle { namespace operators { template class FlattenKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &context) const override { auto *in = context.Input("X"); auto *out = context.Output("Out"); auto &axes = context.Attr("axis"); auto x_dims = in->dims(); auto out_dims = pten::make_ddim(GetOutputShape(axes, x_dims)); out->mutable_data(context.GetPlace(), in->type()); framework::TensorCopy( *in, context.GetPlace(), context.template device_context(), out); out->Resize(out_dims); } static std::vector GetOutputShape(const int axis, const framework::DDim &in_dims) { int64_t outer = 1, inner = 1; for (int i = 0; i < in_dims.size(); ++i) { if (i < axis) { outer *= in_dims[i]; } else { inner *= in_dims[i]; } } std::vector out_shape(2); out_shape[0] = outer; out_shape[1] = inner; return out_shape; } }; template class FlattenGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { auto *d_x = ctx.Output(framework::GradVarName("X")); auto *d_out = ctx.Input(framework::GradVarName("Out")); auto in_dims = ctx.Input("X")->dims(); d_x->mutable_data(ctx.GetPlace(), d_out->type()); framework::TensorCopy( *d_out, ctx.GetPlace(), ctx.template device_context(), d_x); d_x->Resize(in_dims); } }; template class Flatten2Kernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &context) const override { auto &axes = context.Attr("axis"); auto *in = context.Input("X"); auto x_dims = in->dims(); auto *out = context.Output("Out"); auto out_dims = pten::make_ddim( FlattenKernel::GetOutputShape(axes, x_dims)); out->mutable_data(context.GetPlace(), in->type()); framework::TensorCopy( *in, context.GetPlace(), context.template device_context(), out); out->Resize(out_dims); } }; template class Flatten2GradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { auto *d_x = ctx.Output(framework::GradVarName("X")); auto *d_out = ctx.Input(framework::GradVarName("Out")); auto xshape_dims = ctx.Input("XShape")->dims(); auto x_dims = pten::slice_ddim(xshape_dims, 1, xshape_dims.size()); d_x->mutable_data(ctx.GetPlace(), d_out->type()); framework::TensorCopy( *d_out, ctx.GetPlace(), ctx.template device_context(), d_x); d_x->Resize(x_dims); } }; template class FlattenContiguousRangeKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &context) const override { auto *in = context.Input("X"); auto *out = context.Output("Out"); out->mutable_data(context.GetPlace(), in->type()); auto &start_axis = context.Attr("start_axis"); auto &stop_axis = context.Attr("stop_axis"); auto &dev_ctx = context.device_context(); // call new kernel pten::FlattenKernel::TYPE>( static_cast::TYPE &>(dev_ctx), *in, start_axis, stop_axis, out); } }; template class FlattenContiguousRangeGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { auto *d_x = ctx.Output(framework::GradVarName("X")); auto *d_out = ctx.Input(framework::GradVarName("Out")); auto *xshape = ctx.Input("XShape"); d_x->mutable_data(ctx.GetPlace(), d_out->type()); auto &dev_ctx = ctx.device_context(); // call new kernel pten::FlattenGradKernel::TYPE>( static_cast::TYPE &>(dev_ctx), *d_out, *xshape, d_x); } }; } // namespace operators } // namespace paddle