You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Paddle/paddle/operators/pad_op.h

135 lines
4.3 KiB

8 years ago
/* 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
8 years ago
http://www.apache.org/licenses/LICENSE-2.0
8 years ago
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. */
8 years ago
#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
namespace paddle {
namespace operators {
using Tensor = framework::Tensor;
template <typename T, size_t D, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex>
using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;
template <typename DeviceContext, typename T, size_t D>
8 years ago
void PadFunction(const framework::ExecutionContext& context) {
8 years ago
auto pads = context.Attr<std::vector<int>>("paddings");
8 years ago
Eigen::array<std::pair<int, int>, D> paddings;
8 years ago
for (size_t i = 0; i < paddings.size(); ++i) {
paddings[i].first = pads[i * 2];
paddings[i].second = pads[i * 2 + 1];
8 years ago
}
8 years ago
T pad_value = context.Attr<T>("pad_value");
8 years ago
auto* x = context.Input<Tensor>("X");
auto* out = context.Output<Tensor>("Out");
out->mutable_data<T>(context.GetPlace());
8 years ago
auto x_tensor = EigenTensor<T, D>::From(*x);
auto out_tensor = EigenTensor<T, D>::From(*out);
auto& place =
*context.template device_context<DeviceContext>().eigen_device();
out_tensor.device(place) = x_tensor.pad(paddings, pad_value);
8 years ago
}
template <typename DeviceContext, typename T>
class PadKernel : public framework::OpKernel<T> {
8 years ago
public:
void Compute(const framework::ExecutionContext& context) const override {
8 years ago
int rank = context.Input<Tensor>("X")->dims().size();
switch (rank) {
8 years ago
case 1:
PadFunction<DeviceContext, T, 1>(context);
8 years ago
break;
case 2:
PadFunction<DeviceContext, T, 2>(context);
8 years ago
break;
case 3:
PadFunction<DeviceContext, T, 3>(context);
8 years ago
break;
case 4:
PadFunction<DeviceContext, T, 4>(context);
8 years ago
break;
case 5:
PadFunction<DeviceContext, T, 5>(context);
8 years ago
break;
case 6:
PadFunction<DeviceContext, T, 6>(context);
8 years ago
break;
default:
8 years ago
PADDLE_THROW(
"PadOp only support tensors with no more than 6 dimensions.");
8 years ago
}
8 years ago
}
};
template <typename DeviceContext, typename T, size_t D>
8 years ago
void PadGradFunction(const framework::ExecutionContext& context) {
8 years ago
auto pads = context.Attr<std::vector<int>>("paddings");
8 years ago
Eigen::array<std::pair<int, int>, D> paddings;
8 years ago
for (size_t i = 0; i < paddings.size(); ++i) {
paddings[i].first = -pads[i * 2];
paddings[i].second = -pads[i * 2 + 1];
8 years ago
}
auto* d_out = context.Input<Tensor>(framework::GradVarName("Out"));
auto* d_x = context.Output<Tensor>(framework::GradVarName("X"));
if (d_x != nullptr) {
d_x->mutable_data<T>(context.GetPlace());
auto d_x_tensor = EigenTensor<T, D>::From(*d_x);
auto d_out_tensor = EigenTensor<T, D>::From(*d_out);
auto& place =
*context.template device_context<DeviceContext>().eigen_device();
d_x_tensor.device(place) = d_out_tensor.pad(paddings, 0);
}
8 years ago
}
template <typename DeviceContext, typename T>
class PadGradKernel : public framework::OpKernel<T> {
8 years ago
public:
8 years ago
void Compute(const framework::ExecutionContext& context) const override {
8 years ago
size_t rank =
8 years ago
context.Input<Tensor>(framework::GradVarName("Out"))->dims().size();
8 years ago
switch (rank) {
8 years ago
case 1:
PadGradFunction<DeviceContext, T, 1>(context);
8 years ago
break;
case 2:
PadGradFunction<DeviceContext, T, 2>(context);
8 years ago
break;
case 3:
PadGradFunction<DeviceContext, T, 3>(context);
8 years ago
break;
case 4:
PadGradFunction<DeviceContext, T, 4>(context);
8 years ago
break;
case 5:
PadGradFunction<DeviceContext, T, 5>(context);
8 years ago
break;
case 6:
PadGradFunction<DeviceContext, T, 6>(context);
8 years ago
break;
default:
8 years ago
PADDLE_THROW(
"PadOp only support tensors with no more than 6 dimensions.");
8 years ago
}
}
};
} // namespace operators
} // namespace paddle