/* 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 #include "paddle/framework/eigen.h" #include "paddle/framework/op_registry.h" namespace paddle { namespace operators { template void NaiveCpuTranspose(const framework::ExecutionContext& context, const framework::Tensor& in, framework::Tensor& out, std::vector axis) { auto in_data = in.data(); auto out_data = out.mutable_data(context.GetPlace()); auto in_dim = in.dims(); auto out_dim = out.dims(); size_t ndims = in_dim.size(); std::vector in_offset(ndims, 1); std::vector out_offset(ndims, 1); for (int i = ndims - 2; i >= 0; i--) { in_offset[i] = in_offset[i + 1] * in_dim[i + 1]; out_offset[i] = out_offset[i + 1] * out_dim[i + 1]; } size_t data_size = product(in_dim); for (size_t to_index = 0; to_index < data_size; to_index++) { int from_index = 0; int temp = to_index; for (size_t i = 0; i < ndims; i++) { from_index += (temp / out_offset[i]) * in_offset[axis[i]]; temp = temp % out_offset[i]; } out_data[to_index] = in_data[from_index]; } } template void DoTranspose(const framework::ExecutionContext& context, const framework::Tensor& in, framework::Tensor& out, std::vector axis) { Eigen::array permute; for (int i = 0; i < Dims; i++) { permute[i] = axis[i]; } auto in_dim = in.dims(); auto out_dim = out.dims(); auto eigen_in = framework::EigenTensor::From(in); auto eigen_out = framework::EigenTensor::From(out); auto& dev = context.GetEigenDevice(); eigen_out.device(dev) = eigen_in.shuffle(permute); } template class TransposeKernel : 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()); auto axis = context.GetAttr>("axis"); int ndims = axis.size(); switch (ndims) { case 2: DoTranspose(context, *in, *out, axis); break; case 3: DoTranspose(context, *in, *out, axis); break; case 4: DoTranspose(context, *in, *out, axis); break; case 5: DoTranspose(context, *in, *out, axis); break; default: NaiveCpuTranspose(context, *in, *out, axis); break; } } }; template class TransposeGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* in = context.Input(framework::GradVarName("Out")); auto* out = context.Output(framework::GradVarName("X")); out->mutable_data(context.GetPlace()); auto axis_temp = context.GetAttr>("axis"); std::vector axis(axis_temp); for (size_t i = 0; i < axis.size(); i++) { axis[axis_temp[i]] = i; } int ndims = axis.size(); switch (ndims) { case 2: DoTranspose(context, *in, *out, axis); break; case 3: DoTranspose(context, *in, *out, axis); break; case 4: DoTranspose(context, *in, *out, axis); break; case 5: DoTranspose(context, *in, *out, axis); break; default: NaiveCpuTranspose(context, *in, *out, axis); break; } } }; } // namespace operators } // namespace paddle