/* Copyright (c) 2018 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/tensor.h" namespace paddle { namespace operators { namespace math { template using EigenTensor = framework::EigenTensor; template void PadFunction(const framework::ExecutionContext& context, const std::vector& pads, const framework::Tensor& src, T pad_value, framework::Tensor* out) { 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 src_tensor = EigenTensor::From(src); auto out_tensor = EigenTensor::From(*out); auto& place = *context.template device_context().eigen_device(); out_tensor.device(place) = src_tensor.pad(paddings, pad_value); } template void PadGradFunction(const framework::ExecutionContext& context, const std::vector& pads, const framework::Tensor& src, framework::Tensor* d_out) { 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_tensor = EigenTensor::From(*d_out); auto src_tensor = EigenTensor::From(src); auto& place = *context.template device_context().eigen_device(); d_out_tensor.device(place) = src_tensor.pad(paddings, static_cast(0)); } template void PaddingFunctor(int rank, const framework::ExecutionContext& context, const std::vector& pads, T pad_value, const framework::Tensor& src, framework::Tensor* out) { switch (rank) { case 1: PadFunction(context, pads, src, pad_value, out); break; case 2: PadFunction(context, pads, src, pad_value, out); break; case 3: PadFunction(context, pads, src, pad_value, out); break; case 4: PadFunction(context, pads, src, pad_value, out); break; case 5: PadFunction(context, pads, src, pad_value, out); break; case 6: PadFunction(context, pads, src, pad_value, out); break; default: PADDLE_THROW( "PadOp only support tensors with no more than 6 dimensions."); } } template void PaddingGradFunctor(int rank, const framework::ExecutionContext& context, const std::vector& pads, const framework::Tensor& src, framework::Tensor* out) { switch (rank) { case 1: PadGradFunction(context, pads, src, out); break; case 2: PadGradFunction(context, pads, src, out); break; case 3: PadGradFunction(context, pads, src, out); break; case 4: PadGradFunction(context, pads, src, out); break; case 5: PadGradFunction(context, pads, src, out); break; case 6: PadGradFunction(context, pads, src, out); break; default: PADDLE_THROW( "PadOp only support tensors with no more than 6 dimensions."); } } } // namespace math } // namespace operators } // namespace paddle