/* Copyright (c) 2020 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. */ #ifdef PADDLE_WITH_XPU #include "paddle/fluid/operators/transpose_op.h" #include #include #include #include "paddle/fluid/platform/xpu/xpu_header.h" namespace paddle { namespace operators { using framework::Tensor; template class TransposeXPUKernel : public framework::OpKernel { using XPUType = typename XPUTypeTrait::Type; public: void Compute(const framework::ExecutionContext& context) const override { auto x = context.Input("X"); auto out = context.Output("Out"); // axis is permute auto axis = context.Attr>("axis"); int ndims = axis.size(); const auto x_dims = x->dims(); const T* x_data = x->data(); T* y_data = out->mutable_data(context.GetPlace()); if (out->numel() == 0) { return; } std::vector x_shape_host(ndims, 0); for (int i = 0; i < ndims; ++i) { x_shape_host[i] = x_dims[i]; } auto& dev_ctx = context.template device_context(); int r = xpu::transpose( dev_ctx.x_context(), reinterpret_cast(x_data), reinterpret_cast(y_data), x_shape_host, axis); PADDLE_ENFORCE_EQ( r, xpu::Error_t::SUCCESS, platform::errors::External("XPU kernel error! error code=%d", r)); } }; template class TransposeGradXPUKernel : public framework::OpKernel { using XPUType = typename XPUTypeTrait::Type; public: void Compute(const framework::ExecutionContext& context) const override { auto* out_grad = context.Input(framework::GradVarName("Out")); auto* x_grad = context.Output(framework::GradVarName("X")); if (!x_grad) return; x_grad->mutable_data(context.GetPlace()); std::vector axis = context.Attr>("axis"); std::vector reversed_axis(axis); for (size_t i = 0; i < axis.size(); i++) { reversed_axis[axis[i]] = i; } int ndims = axis.size(); std::vector out_shape_host(ndims, 0); for (int i = 0; i < ndims; ++i) { out_shape_host[i] = out_grad->dims()[i]; } auto& dev_ctx = context.template device_context(); int r = xpu::transpose( dev_ctx.x_context(), reinterpret_cast(out_grad->data()), reinterpret_cast(x_grad->data()), out_shape_host, reversed_axis); PADDLE_ENFORCE_EQ( r, xpu::Error_t::SUCCESS, platform::errors::External("XPU kernel error! error code=%d", r)); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_XPU_KERNEL( transpose, ops::TransposeXPUKernel, ops::TransposeXPUKernel); REGISTER_OP_XPU_KERNEL( transpose_grad, ops::TransposeGradXPUKernel, ops::TransposeGradXPUKernel); REGISTER_OP_XPU_KERNEL( transpose2, ops::TransposeXPUKernel, ops::TransposeXPUKernel); REGISTER_OP_XPU_KERNEL( transpose2_grad, ops::TransposeGradXPUKernel, ops::TransposeGradXPUKernel); #endif // PADDLE_WITH_XPU