/* 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/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.Attr>("paddings"); Eigen::array, D> paddings; for (size_t i = 0; i < paddings.size(); ++i) { paddings[i].first = pads[i * 2]; paddings[i].second = pads[i * 2 + 1]; } T pad_value = context.Attr("pad_value"); auto* x = context.Input("X"); auto* out = context.Output("Out"); out->mutable_data(context.GetPlace()); 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 rank = context.Input("X")->dims().size(); switch (rank) { 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: PADDLE_THROW( "PadOp only support tensors with no more than 6 dimensions."); } } }; template void PadGradFunction(const framework::ExecutionContext& context) { auto pads = context.Attr>("paddings"); Eigen::array, D> paddings; for (size_t i = 0; i < paddings.size(); ++i) { paddings[i].first = -pads[i * 2]; paddings[i].second = -pads[i * 2 + 1]; } auto* d_out = context.Input(framework::GradVarName("Out")); auto* d_x = context.Output(framework::GradVarName("X")); if (d_x != nullptr) { d_x->mutable_data(context.GetPlace()); auto d_x_tensor = EigenTensor::From(*d_x); auto d_out_tensor = EigenTensor::From(*d_out); auto place = context.GetEigenDevice(); d_x_tensor.device(place) = d_out_tensor.pad(paddings, 0); } } template class PadGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { size_t rank = context.Input(framework::GradVarName("Out"))->dims().size(); switch (rank) { 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: PADDLE_THROW( "PadOp only support tensors with no more than 6 dimensions."); } } }; } // namespace operators } // namespace paddle