// Copyright (c) 2022 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. #include "paddle/phi/kernels/argsort_kernel.h" #include "paddle/phi/backends/xpu/enforce_xpu.h" #include "paddle/phi/backends/xpu/xpu_context.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/funcs/math_function.h" namespace phi { template static inline void xpu_argsort(xpu::Context* xpu_ctx, const T* input_data, T* output_data, TID* indices_data, int64_t m, int64_t n, bool descending, bool stable) { int ret; if (stable) { ret = xpu::stable_sort( xpu_ctx, input_data, output_data, indices_data, m, n, descending); PADDLE_ENFORCE_XDNN_SUCCESS(ret, "stable_sort"); } else { ret = xpu::sort( xpu_ctx, input_data, output_data, indices_data, m, n, descending); PADDLE_ENFORCE_XDNN_SUCCESS(ret, "sort"); } } template static inline void xpu_transpose(xpu::Context* xpu_ctx, const T* x, T* y, const std::vector& xshape, const std::vector& permute) { int ret = xpu::transpose(xpu_ctx, x, y, xshape, permute); PADDLE_ENFORCE_XDNN_SUCCESS(ret, "transpose"); } template struct XPUArgsort { void operator()(xpu::Context* xpu_ctx, const T* input_data, T* output_data, int64_t* indices_data, const std::vector& data_shape, const std::vector& permute, bool descending, bool stable) { xpu::ctx_guard RAII_GUARD(xpu_ctx); int64_t m = data_shape[0] * data_shape[2]; int64_t n = data_shape[1]; int64_t len = data_shape[0] * data_shape[1] * data_shape[2]; std::vector trans_data_shape{ data_shape[0], data_shape[2], data_shape[1]}; T* input_data_trans = RAII_GUARD.alloc_l3_or_gm(len); T* output_data_trans = RAII_GUARD.alloc_l3_or_gm(len); int64_t* indices_data_trans = RAII_GUARD.alloc_l3_or_gm(len); xpu_transpose(xpu_ctx, input_data, input_data_trans, data_shape, permute); xpu_argsort(xpu_ctx, input_data_trans, output_data_trans, indices_data_trans, m, n, descending, stable); xpu_transpose( xpu_ctx, output_data_trans, output_data, trans_data_shape, permute); xpu_transpose( xpu_ctx, indices_data_trans, indices_data, trans_data_shape, permute); } }; template void ArgsortKernel(const Context& dev_ctx, const DenseTensor& input, int axis, bool descending, bool stable, DenseTensor* output, DenseTensor* indices) { auto in_dims = input.dims(); auto rank = in_dims.size(); if (input.numel() == 0) { output->Resize(in_dims); indices->Resize(in_dims); dev_ctx.template Alloc(output); dev_ctx.template Alloc(indices); return; } axis = (axis < 0) ? (in_dims.size() + axis) : axis; int64_t n = in_dims[axis]; auto input_data = input.data(); auto output_data = dev_ctx.template Alloc(output); auto indices_data = dev_ctx.template Alloc(indices); if (rank == 0) { Copy(dev_ctx, input, dev_ctx.GetPlace(), false, output); funcs::set_constant(dev_ctx, indices, static_cast(0)); return; } int64_t len_before = common::product(slice_ddim(in_dims, 0, axis)); int64_t len_after = common::product(slice_ddim(in_dims, axis + 1, in_dims.size())); std::vector permute_vec{0, 2, 1}; std::vector data_shape{len_before, n, len_after}; using XPUType = typename XPUTypeTrait::Type; XPUArgsort()(dev_ctx.x_context(), reinterpret_cast(input_data), reinterpret_cast(output_data), indices_data, data_shape, permute_vec, descending, stable); } } // namespace phi PD_REGISTER_KERNEL(argsort, XPU, ALL_LAYOUT, phi::ArgsortKernel, float, int, int64_t, phi::float16) { kernel->OutputAt(1).SetDataType(phi::DataType::INT64); }