| 342 | } |
| 343 | |
| 344 | phi::DenseTensor TransformData(const phi::DenseTensor& tensor, |
| 345 | const phi::TensorArgDef& target_args_def, |
| 346 | const TransformFlag& transform_flag, |
| 347 | bool is_stride_kernel) { |
| 348 | phi::DenseTensor out = tensor; |
| 349 | bool trans_layout = false; |
| 350 | bool trans_dtype = false; |
| 351 | |
| 352 | if (NeedTransform2Contiguous(is_stride_kernel, out.meta().is_contiguous())) { |
| 353 | out = Trans2Contiguous(out); |
| 354 | } |
| 355 | |
| 356 | if (NeedTransformLayout(tensor.layout(), |
| 357 | target_args_def.layout, |
| 358 | tensor.place(), |
| 359 | transform_flag) && |
| 360 | tensor.dims().size() != 1) { |
| 361 | if (NeedTransform2Contiguous(false, out.meta().is_contiguous())) { |
| 362 | out = Trans2Contiguous(out); |
| 363 | } |
| 364 | out = TransDataLayout(out, target_args_def.layout); |
| 365 | trans_layout = true; |
| 366 | } |
| 367 | |
| 368 | if (NeedTransformDataType( |
| 369 | tensor.dtype(), target_args_def.dtype, transform_flag)) { |
| 370 | if (NeedTransform2Contiguous(false, out.meta().is_contiguous())) { |
| 371 | out = Trans2Contiguous(out); |
| 372 | } |
| 373 | out = TransDataType(out, target_args_def.dtype); |
| 374 | trans_dtype = true; |
| 375 | } |
| 376 | |
| 377 | if (NeedTransformPlace( |
| 378 | out.place(), target_args_def.backend, transform_flag)) { |
| 379 | out = TransDataPlace(out, phi::TransToPhiPlace(target_args_def.backend)); |
| 380 | if (!trans_layout && !trans_dtype && |
| 381 | tensor.place().GetType() == AllocationType::GPUPINNED) { |
| 382 | // Sharing buffer on GPUPINNED place is a special case due to historical |
| 383 | // reasons, and it should not be implemented in this way from a |
| 384 | // reasonable point of view, but because the performance of the previous |
| 385 | // model depends on the inplace operation here, the model performance |
| 386 | // will deteriorate after reverting to non-place impl, so it needs to be |
| 387 | // retained here and need to use `const_cast` |
| 388 | const_cast<phi::DenseTensor&>(tensor).ShareBufferWith(out); |
| 389 | } |
| 390 | } |
| 391 | return out; |
| 392 | } |
| 393 | |
| 394 | std::shared_ptr<phi::DenseTensor> PrepareData( |
| 395 | const Tensor& input, |
no test coverage detected