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

Function TensorFromVector

paddle/phi/core/tensor_utils.cc:496–543  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

494
495template <typename T>
496void TensorFromVector(const std::vector<T>& src,
497 const phi::DeviceContext& ctx,
498 DenseTensor* dst) {
499 auto dst_place = ctx.GetPlace();
500 auto src_ptr = static_cast<const void*>(src.data());
501 phi::CPUPlace src_place;
502 dst->Resize({static_cast<int64_t>(src.size())});
503 ctx.template Alloc<T>(dst);
504 auto dst_ptr = static_cast<void*>(dst->data<T>());
505 auto size = src.size() * sizeof(T);
506
507 if (dst_place.GetType() == AllocationType::CPU) {
508 memory_utils::Copy(dst_place, dst_ptr, src_place, src_ptr, size);
509 }
510#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
511 else if (dst_place.GetType() == AllocationType::GPU) { // NOLINT
512 const void* stable_src_ptr =
513 backends::gpu::RestoreHostMemIfCapturingCUDAGraph(
514 const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(src_ptr)),
515 size);
516 memory_utils::Copy(dst_place,
517 dst_ptr,
518 src_place,
519 stable_src_ptr,
520 size,
521 reinterpret_cast<const phi::GPUContext&>(ctx).stream());
522 }
523#endif
524#ifdef PADDLE_WITH_CUSTOM_DEVICE
525 else if (dst_place.GetType() == AllocationType::CUSTOM) { // NOLINT
526 memory_utils::Copy(
527 dst_place,
528 dst_ptr,
529 src_place,
530 src_ptr,
531 size,
532 reinterpret_cast<const phi::CustomContext&>(ctx).stream());
533 }
534#endif
535#ifdef PADDLE_WITH_XPU
536 else if (dst_place.GetType() == AllocationType::XPU) { // NOLINT
537 memory_utils::Copy(dst_place, dst_ptr, src_place, src_ptr, size);
538 }
539#endif
540 else { // NOLINT
541 PADDLE_THROW(common::errors::Unimplemented(
542 "TensorFromVector on %s is not supported.", dst_place));
543 }
544}
545
546template <>

Callers 15

CopyVectorToTensorFunction · 0.50
IndexSelectFunction · 0.50
UniqueDimFunction · 0.50
UniqueConsecutiveDimFunction · 0.50
IndexSampleInnerFunction · 0.50
SetValueKernelFunction · 0.50
RollGradKernelFunction · 0.50
RollKernelFunction · 0.50
CrossGradKernelFunction · 0.50
CrossKernelFunction · 0.50
IndexSampleGradInnerFunction · 0.50
GetClassIntervalFunction · 0.50

Calls 8

CopyFunction · 0.70
GetPlaceMethod · 0.45
dataMethod · 0.45
ResizeMethod · 0.45
sizeMethod · 0.45
GetTypeMethod · 0.45
streamMethod · 0.45

Tested by 13

BroadcastFunction · 0.40
AllReduceByStreamFunction · 0.40
CompareFunction · 0.40
DropoutFunction · 0.40
DropoutGradFunction · 0.40
LayerNormFunction · 0.40
PrepareInputOutputMethod · 0.40
PrepareShapeInputMethod · 0.40
PrepareInputOutputMethod · 0.40
PrepareInputOutputMethod · 0.40
PrepareInputOutputMethod · 0.40
PrepareInputOutputMethod · 0.40