// Copyright (c) 2023 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/take_along_axis_kernel.h" #include "glog/logging.h" #include "paddle/common/layout.h" #include "paddle/phi/backends/xpu/enforce_xpu.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/full_kernel.h" namespace phi { template void TakeAlongAxisKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& index, int axis, DenseTensor* out) { if (index.numel() == 0) { dev_ctx.template Alloc(out); return; } if (x.numel() == 0) { phi::Full( dev_ctx, vectorize(out->dims()), static_cast(0), out); return; } out->Resize(index.dims()); dev_ctx.template Alloc(out); if (out->numel() == 0) { return; } if (x.numel() == 0 || index.numel() == 0) return; const auto& index_dtype = index.dtype(); bool index_dtype_match = index_dtype == DataType::INT32 || index_dtype == DataType::INT64; PADDLE_ENFORCE_EQ(index_dtype_match, true, errors::InvalidArgument( "Input(Index) holds the wrong type, it holds %s, but " "desires to be %s or %s", DataTypeToString(index_dtype), DataTypeToString(DataType::INT32), DataTypeToString(DataType::INT64))); std::vector x_shape(x.dims().size()); for (int i = 0; i < x.dims().size(); ++i) { x_shape[i] = x.dims()[i]; } std::vector index_shape(index.dims().size()); for (int i = 0; i < index.dims().size(); ++i) { index_shape[i] = index.dims()[i]; } if (x_shape.size() <= 1 && index_shape.size() <= 1) { for (int i = x_shape.size(); i < 2; ++i) { x_shape.push_back(1); index_shape.push_back(1); } } using XPUType = typename XPUTypeTrait::Type; int r = 0; #ifndef PADDLE_WITH_XPU_PLUGIN if (index_dtype == DataType::INT32) { r = xpu::gather(dev_ctx.x_context(), reinterpret_cast(x.data()), index.data(), reinterpret_cast(out->data()), x_shape, index_shape, axis); } else { r = xpu::gather( dev_ctx.x_context(), reinterpret_cast(x.data()), index.data(), reinterpret_cast(out->data()), x_shape, index_shape, axis); } PADDLE_ENFORCE_XDNN_SUCCESS(r, "gather"); #else if (index_dtype == DataType::INT32) { r = xpu::plugin::take_along_axis( dev_ctx.x_context(), reinterpret_cast(x.data()), index.data(), reinterpret_cast(out->data()), x_shape, index_shape, axis); } else { r = xpu::plugin::take_along_axis( dev_ctx.x_context(), reinterpret_cast(x.data()), index.data(), reinterpret_cast(out->data()), x_shape, index_shape, axis); } PADDLE_ENFORCE_XDNN_SUCCESS(r, "take_along_axis"); #endif } } // namespace phi PD_REGISTER_KERNEL(take_along_axis, XPU, ALL_LAYOUT, phi::TakeAlongAxisKernel, phi::float16, phi::bfloat16, float) {}