/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. 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 "paddle/operators/math/math_function.h" #include "paddle/framework/eigen.h" #include "paddle/framework/op_registry.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; template using EigenTensor = framework::EigenTensor; template void PadFunction(const framework::ExecutionContext& context) { auto pads = context.op_.GetAttr>>("paddings"); Eigen::array, D> paddings; for (int i = 0; i < pads.size(); ++i) { paddings[i] = pads[i]; } T pad_value = context.op_.GetAttr("pad_value"); auto* X = context.Input("X"); auto* Out = context.Output("Out"); Out->mutable_data(context.GetPlace()); auto dims = X->dims(); auto X_tensor = EigenTensor::From(*X); auto Out_tensor = EigenTensor::From(*Out); auto place = context.GetEigenDevice(); Out_tensor.device(place) = X_tensor.pad(paddings, pad_value); } template class PadKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { int dim = context.Input("X")->dims().size(); switch (dim) { case 1: PadFunction(context); break; case 2: PadFunction(context); break; case 3: PadFunction(context); break; case 4: PadFunction(context); break; case 5: PadFunction(context); break; case 6: PadFunction(context); break; default: LOG(ERROR) << "Only ranks up to 6 supported."; } } }; template void PadGradFunction(const framework::ExecutionContext& context) { auto pads = context.op_.GetAttr>>("paddings"); Eigen::array, D> paddings; for (int i = 0; i < pads.size(); ++i) { paddings[0].first = -paddings[0].first; paddings[1].second = -paddings[1].second; } auto* dOut = context.Input(framework::GradVarName("Out")); auto* dX = context.Output(framework::GradVarName("X")); dX->mutable_data(context.GetPlace()); auto dX_tensor = EigenTensor::From(*dX); auto dOut_tensor = EigenTensor::From(*dOut); auto place = context.GetEigenDevice(); dX_tensor.device(place) = dOut_tensor.pad(paddings, 0); } template class PadGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { size_t dim = context.Input(framework::GradVarName("Out"))->dims().size(); switch (dim) { case 1: PadGradFunction(context); break; case 2: PadGradFunction(context); break; case 3: PadGradFunction(context); break; case 4: PadGradFunction(context); break; case 5: PadGradFunction(context); break; case 6: PadGradFunction(context); break; default: LOG(ERROR) << "Only ranks up to 6 supported."; } } }; } // namespace operators } // namespace paddle