| 270 | } |
| 271 | |
| 272 | phi::DenseTensor Trans2Contiguous(const phi::DenseTensor& tensor) { |
| 273 | auto& pool = phi::DeviceContextPool::Instance(); |
| 274 | |
| 275 | VLOG(3) << "Trans2Contiguous..."; |
| 276 | |
| 277 | if (tensor.place().GetType() == phi::AllocationType::CPU) { |
| 278 | auto* dev_ctx = static_cast<phi::CPUContext*>(pool.Get(tensor.place())); |
| 279 | return TensorContiguous<phi::CPUContext>(*dev_ctx, tensor); |
| 280 | #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) |
| 281 | } else if (tensor.place().GetType() == phi::AllocationType::GPU) { |
| 282 | auto* dev_ctx = static_cast<phi::GPUContext*>(pool.Get(tensor.place())); |
| 283 | return TensorContiguous<phi::GPUContext>(*dev_ctx, tensor); |
| 284 | #endif |
| 285 | #ifdef PADDLE_WITH_XPU |
| 286 | } else if (tensor.place().GetType() == phi::AllocationType::XPU) { |
| 287 | auto* dev_ctx = static_cast<phi::XPUContext*>(pool.Get(tensor.place())); |
| 288 | return TensorContiguous<phi::XPUContext>(*dev_ctx, tensor); |
| 289 | #endif |
| 290 | #ifdef PADDLE_WITH_CUSTOM_DEVICE |
| 291 | } else if (tensor.place().GetType() == phi::AllocationType::CUSTOM) { |
| 292 | auto* dev_ctx = static_cast<phi::CustomContext*>(pool.Get(tensor.place())); |
| 293 | phi::DenseTensor dense_out; |
| 294 | phi::MetaTensor meta_input(tensor); |
| 295 | phi::MetaTensor meta_out(&dense_out); |
| 296 | UnchangedInferMeta(meta_input, &meta_out); |
| 297 | const phi::KernelKey& kernel_key = {phi::TransToPhiBackend(tensor.place()), |
| 298 | phi::DataLayout::ALL_LAYOUT, |
| 299 | tensor.dtype()}; |
| 300 | using kernel_signature = void (*)( |
| 301 | const phi::DeviceContext&, const phi::DenseTensor&, phi::DenseTensor*); |
| 302 | PD_VISIT_KERNEL("contiguous", |
| 303 | kernel_key, |
| 304 | kernel_signature, |
| 305 | false, |
| 306 | *dev_ctx, |
| 307 | tensor, |
| 308 | &dense_out); |
| 309 | return dense_out; |
| 310 | #endif |
| 311 | } else { |
| 312 | PADDLE_THROW(common::errors::Unimplemented( |
| 313 | "Place type is not supported when casting data type.")); |
| 314 | } |
| 315 | |
| 316 | return tensor; |
| 317 | } |
| 318 | |
| 319 | void CheckAndTrans2Contiguous(phi::DenseTensor* tensor) { |
| 320 | if (!tensor->meta().is_contiguous()) { |
no test coverage detected