// 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/array_kernel.h" #include "paddle/common/layout.h" #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/concat_kernel.h" #include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/stack_kernel.h" namespace phi { template void CreateArrayKernel(const Context& dev_ctx, DataType dtype, TensorArray* out) {} template void CreateArrayLikeKernel(const Context& dev_ctx, const TensorArray& input, float val, TensorArray* out) { out->resize(input.size()); for (size_t i = 0; i < input.size(); i++) { DenseTensor input_i = input[i]; out->at(i).Resize(input_i.dims()); FullLikeKernel( dev_ctx, input_i, val, input_i.dtype(), &out->at(i)); } } template void ArrayLengthKernel(const Context& dev_ctx, const TensorArray& x, DenseTensor* out) { out->Resize({1}); dev_ctx.template Alloc(out); *out->data() = static_cast(x.size()); } template void ArrayReadKernel(const Context& dev_ctx, const TensorArray& array, const Scalar& i, DenseTensor* out) { size_t offset = i.to(); PADDLE_ENFORCE_EQ( offset < array.size(), true, errors::InvalidArgument( "index %d exceed array size %d.", offset, array.size())); phi::Copy(dev_ctx, array[offset], dev_ctx.GetPlace(), false, out); out->set_lod(array[offset].lod()); } template void ArrayWriteKernel(const Context& dev_ctx, const TensorArray& array, const DenseTensor& x, const Scalar& i, TensorArray* out) { size_t offset = i.to(); if (offset >= out->size()) { out->resize(offset + 1); } auto* out_tensor = &out->at(offset); out_tensor->set_lod(x.lod()); if (x.memory_size() > 0) { phi::Copy(dev_ctx, x, dev_ctx.GetPlace(), false, out_tensor); } } template void ArrayToTensorKernel(const Context& dev_ctx, const TensorArray& x, int axis, bool use_stack, DenseTensor* out, DenseTensor* out_index) { const size_t n = x.size(); PADDLE_ENFORCE_GT( n, 0, common::errors::InvalidArgument("Input tensor array size should > 0," "but the received is %d", n)); std::vector tmp_inputs(x.size()); std::vector inputs; std::vector tmp_indices(x.size()); std::vector indices; for (size_t i = 0; i < x.size(); i++) { tmp_inputs[i].ShareDataWith(x[i]); inputs.push_back(&tmp_inputs[i]); FullKernel( dev_ctx, {1}, x[i].dims()[axis], DataType::INT32, &tmp_indices[i]); indices.push_back(&tmp_indices[i]); } if (use_stack) { auto vec = vectorize(x[0].dims()); vec.insert(vec.begin() + axis, x.size()); // NOLINT out->Resize(vec); StackKernel(dev_ctx, inputs, axis, out); } else { auto out_dims = x[0].dims(); size_t in_zero_dims_size = out_dims.size(); for (size_t i = 1; i < n; i++) { for (size_t j = 0; j < in_zero_dims_size; j++) { if (j == static_cast(axis)) { out_dims[axis] += x[i].dims()[static_cast(j)]; } } } auto vec = vectorize(out_dims); out->Resize(vec); ConcatKernel(dev_ctx, inputs, axis, out); } out_index->Resize({static_cast(x.size())}); StackKernel(dev_ctx, indices, 0, out_index); } template void ArrayPopKernel(const Context& dev_ctx, const TensorArray& array, int index, TensorArray* array_out, DenseTensor* out) { PADDLE_ENFORCE_GT( array.size(), 0, common::errors::InvalidArgument("Input tensorarray size should > 0," "but the received is %d", array.size())); if (index < 0) { index += array.size(); } out->ShareDataWith(array[index]); array_out->pop(static_cast(index)); } } // namespace phi PD_REGISTER_KERNEL(create_array, CPU, ALL_LAYOUT, phi::CreateArrayKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PD_REGISTER_KERNEL(create_array, GPU, ALL_LAYOUT, phi::CreateArrayKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} #endif #if defined(PADDLE_WITH_XPU) PD_REGISTER_KERNEL(create_array, XPU, ALL_LAYOUT, phi::CreateArrayKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16) {} #endif PD_REGISTER_KERNEL(create_array_like, CPU, ALL_LAYOUT, phi::CreateArrayLikeKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PD_REGISTER_KERNEL(create_array_like, GPU, ALL_LAYOUT, phi::CreateArrayLikeKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} #endif #if defined(PADDLE_WITH_XPU) PD_REGISTER_KERNEL(create_array_like, XPU, ALL_LAYOUT, phi::CreateArrayLikeKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16) {} #endif PD_REGISTER_KERNEL(array_length, CPU, ALL_LAYOUT, phi::ArrayLengthKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} PD_REGISTER_KERNEL(array_read, CPU, ALL_LAYOUT, phi::ArrayReadKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PD_REGISTER_KERNEL(array_read, GPU, ALL_LAYOUT, phi::ArrayReadKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} #endif #if defined(PADDLE_WITH_XPU) PD_REGISTER_KERNEL(array_read, XPU, ALL_LAYOUT, phi::ArrayReadKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16) {} #endif PD_REGISTER_KERNEL(array_write, CPU, ALL_LAYOUT, phi::ArrayWriteKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PD_REGISTER_KERNEL(array_write, GPU, ALL_LAYOUT, phi::ArrayWriteKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} #endif #if defined(PADDLE_WITH_XPU) PD_REGISTER_KERNEL(array_write, XPU, ALL_LAYOUT, phi::ArrayWriteKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16) {} #endif PD_REGISTER_KERNEL(array_to_tensor, CPU, ALL_LAYOUT, phi::ArrayToTensorKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PD_REGISTER_KERNEL(array_to_tensor, GPU, ALL_LAYOUT, phi::ArrayToTensorKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} #endif #if defined(PADDLE_WITH_XPU) PD_REGISTER_KERNEL(array_to_tensor, XPU, ALL_LAYOUT, phi::ArrayToTensorKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16) {} #endif PD_REGISTER_KERNEL(array_pop, CPU, ALL_LAYOUT, phi::ArrayPopKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PD_REGISTER_KERNEL(array_pop, GPU, ALL_LAYOUT, phi::ArrayPopKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16, phi::complex64, phi::complex128) {} #endif #if defined(PADDLE_WITH_XPU) PD_REGISTER_KERNEL(array_pop, XPU, ALL_LAYOUT, phi::ArrayPopKernel, bool, int, int64_t, float, double, phi::float16, phi::bfloat16) {} #endif