MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / TransformData

Function TransformData

paddle/phi/api/lib/data_transform.cc:344–392  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

342}
343
344phi::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
394std::shared_ptr<phi::DenseTensor> PrepareData(
395 const Tensor& input,

Callers 2

PrepareDataFunction · 0.70
PrepareDataForDistTensorFunction · 0.70

Calls 15

Trans2ContiguousFunction · 0.85
NeedTransformPlaceFunction · 0.85
TransDataPlaceFunction · 0.85
TransToPhiPlaceFunction · 0.85
metaMethod · 0.80
ShareBufferWithMethod · 0.80
NeedTransform2ContiguousFunction · 0.70
NeedTransformLayoutFunction · 0.70
TransDataLayoutFunction · 0.70
NeedTransformDataTypeFunction · 0.70
TransDataTypeFunction · 0.70
is_contiguousMethod · 0.45

Tested by

no test coverage detected