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

Function TEST

test/cpp/phi/api/test_from_blob.cc:42–79  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40} // namespace paddle
41
42TEST(from_blob, CPU) {
43 // 1. create data
44 int64_t data[] = {4, 3, 2, 1}; // NOLINT
45
46 ASSERT_EQ(paddle::GetPlaceFromPtr(data), phi::CPUPlace());
47
48 // 2. test API
49 auto test_tensor = from_blob(data, {1, 2, 2}, DataType::INT64);
50
51 // 3. check result
52 // 3.1 check tensor attributes
53 ASSERT_EQ(test_tensor.dims().size(), 3);
54 ASSERT_EQ(test_tensor.dims()[0], 1);
55 ASSERT_EQ(test_tensor.dims()[1], 2);
56 ASSERT_EQ(test_tensor.dims()[2], 2);
57 ASSERT_EQ(test_tensor.numel(), 4);
58 ASSERT_EQ(test_tensor.is_cpu(), true);
59 ASSERT_EQ(test_tensor.dtype(), DataType::INT64);
60 ASSERT_EQ(test_tensor.layout(), phi::DataLayout::NCHW);
61 ASSERT_EQ(test_tensor.is_dense_tensor(), true);
62
63 // 3.2 check tensor values
64 auto* test_tensor_data = test_tensor.template data<int64_t>();
65 for (int64_t i = 0; i < 4; i++) {
66 ASSERT_EQ(test_tensor_data[i], 4 - i);
67 }
68
69 // 3.3 check whether memory is shared
70 ASSERT_EQ(data, test_tensor_data);
71
72 // 3.4 test other API
73 auto test_tensor_pow = paddle::experimental::pow(test_tensor, 2);
74 auto* test_tensor_pow_data = test_tensor_pow.template data<int64_t>();
75 for (int64_t i = 0; i < 4; i++) {
76 ASSERT_EQ(test_tensor_pow_data[i],
77 static_cast<int64_t>(std::pow(4 - i, 2)));
78 }
79}
80
81#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
82

Callers

nothing calls this directly

Calls 15

GetPlaceFromPtrFunction · 0.85
GPUPlaceClass · 0.85
InstanceFunction · 0.85
is_dense_tensorMethod · 0.80
CPUPlaceClass · 0.50
from_blobFunction · 0.50
powFunction · 0.50
GetAllocatorFunction · 0.50
GetGPUDeviceCountFunction · 0.50
CopyFunction · 0.50
sizeMethod · 0.45
dimsMethod · 0.45

Tested by

no test coverage detected