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

Function TEST

test/cpp/fluid/framework/tensor_util_test.cc:24–124  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22namespace framework {
23
24TEST(TensorCopy, Tensor) {
25 phi::DenseTensor src_tensor;
26 phi::DenseTensor dst_tensor;
27 phi::CPUContext cpu_ctx((phi::CPUPlace()));
28
29 int* src_ptr =
30 src_tensor.mutable_data<int>(common::make_ddim({3, 3}), phi::CPUPlace());
31
32 std::array<int, 9> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
33 memcpy(src_ptr, arr.data(), 9 * sizeof(int));
34 src_tensor.set_layout(DataLayout::kAnyLayout);
35
36 auto cpu_place = new phi::CPUPlace();
37 TensorCopy(src_tensor, *cpu_place, &dst_tensor);
38
39 const int* dst_ptr = dst_tensor.data<int>();
40 EXPECT_NE(src_ptr, dst_ptr);
41 for (size_t i = 0; i < 9; ++i) {
42 EXPECT_EQ(src_ptr[i], dst_ptr[i]);
43 }
44
45 TensorCopy(dst_tensor, *cpu_place, &dst_tensor);
46 for (size_t i = 0; i < 9; ++i) {
47 EXPECT_EQ(src_ptr[i], dst_ptr[i]);
48 }
49
50 EXPECT_TRUE(dst_tensor.layout() == src_tensor.layout());
51
52 phi::DenseTensor slice_tensor = src_tensor.Slice(1, 2);
53 TensorCopy(slice_tensor, *cpu_place, &dst_tensor);
54 const int* slice_ptr = slice_tensor.data<int>();
55 dst_ptr = dst_tensor.data<int>();
56 EXPECT_NE(dst_ptr, slice_ptr);
57 for (size_t i = 0; i < 3; ++i) {
58 EXPECT_EQ(dst_ptr[i], slice_ptr[i]);
59 }
60 EXPECT_TRUE(dst_tensor.layout() == src_tensor.layout());
61
62#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
63 {
64 phi::DenseTensor src_tensor;
65 phi::DenseTensor gpu_tensor;
66 phi::DenseTensor dst_tensor;
67
68 int* src_ptr = src_tensor.mutable_data<int>(common::make_ddim({3, 3}),
69 phi::CPUPlace());
70
71 std::array<int, 9> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
72 memcpy(src_ptr, arr.data(), 9 * sizeof(int));
73
74 // CPU phi::DenseTensor to GPU phi::DenseTensor
75 auto gpu_place = new phi::GPUPlace(0);
76 phi::GPUContext gpu_ctx(*gpu_place);
77 gpu_ctx.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance()
78 .GetAllocator(*gpu_place, gpu_ctx.stream())
79 .get());
80 gpu_ctx.PartialInitWithAllocator();
81 TensorCopy(src_tensor, *gpu_place, gpu_ctx, &gpu_tensor);

Callers

nothing calls this directly

Calls 15

make_ddimFunction · 0.85
TensorCopyFunction · 0.85
InstanceFunction · 0.85
ToDLPackFunction · 0.85
FromDLPackFunction · 0.85
TensorFromDLPackFunction · 0.85
TensorContainsNANFunction · 0.85
TensorContainsInfFunction · 0.85
TensorIsfiniteFunction · 0.85
GetByPlaceMethod · 0.80
CPUPlaceClass · 0.50
TensorToStreamFunction · 0.50

Tested by

no test coverage detected