MCPcopy Create free account
hub / github.com/apache/tvm-ffi / TEST

Function TEST

tests/cpp/test_tensor.cc:52–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

50}
51
52TEST(Tensor, Basic) {
53 Tensor nd = Empty({1, 2, 3}, DLDataType({kDLFloat, 32, 1}), DLDevice({kDLCPU, 0}));
54 Shape shape = nd.shape();
55 Shape strides = nd.strides();
56 EXPECT_EQ(shape.size(), 3);
57 EXPECT_EQ(shape[0], 1);
58 EXPECT_EQ(shape[1], 2);
59 EXPECT_EQ(shape[2], 3);
60 EXPECT_EQ(strides.size(), 3);
61 EXPECT_EQ(strides[0], 6);
62 EXPECT_EQ(strides[1], 3);
63 EXPECT_EQ(strides[2], 1);
64 EXPECT_EQ(nd.dtype(), DLDataType({kDLFloat, 32, 1}));
65 for (int64_t i = 0; i < shape.Product(); ++i) {
66 reinterpret_cast<float*>(nd.data_ptr())[i] = static_cast<float>(i);
67 }
68
69 EXPECT_EQ(nd.numel(), 6);
70 EXPECT_EQ(nd.ndim(), 3);
71 EXPECT_EQ(nd.data_ptr(), nd.GetDLTensorPtr()->data);
72
73 Any any0 = nd;
74 Tensor nd2 = any0.as<Tensor>().value(); // NOLINT(bugprone-unchecked-optional-access)
75 EXPECT_EQ(nd2.dtype(), DLDataType({kDLFloat, 32, 1}));
76 for (int64_t i = 0; i < shape.Product(); ++i) {
77 EXPECT_EQ(reinterpret_cast<float*>(nd2.data_ptr())[i], i);
78 }
79
80 EXPECT_EQ(nd.IsContiguous(), true);
81 EXPECT_EQ(nd2.use_count(), 3);
82
83 Tensor nd3 = EmptyStrided({2, 3}, {1, 2}, DLDataType({kDLFloat, 32, 1}), DLDevice({kDLCPU, 0}));
84 Shape shape3 = nd3.shape();
85 Shape strides3 = nd3.strides();
86 EXPECT_EQ(shape3.size(), 2);
87 EXPECT_EQ(shape3[0], 2);
88 EXPECT_EQ(shape3[1], 3);
89 EXPECT_EQ(strides3.size(), 2);
90 EXPECT_EQ(strides3[0], 1);
91 EXPECT_EQ(strides3[1], 2);
92}
93
94TEST(Tensor, EmptyTensorIsContiguous) {
95 // An empty tensor (any shape dim == 0) is trivially contiguous regardless of

Callers

nothing calls this directly

Calls 15

DLDataTypeClass · 0.85
DLDeviceClass · 0.85
EmptyStridedFunction · 0.85
GetDataSizeFunction · 0.85
GetDLTensorPtrMethod · 0.80
EmptyFunction · 0.70
shapeMethod · 0.45
stridesMethod · 0.45
sizeMethod · 0.45
dtypeMethod · 0.45
ProductMethod · 0.45
data_ptrMethod · 0.45

Tested by

no test coverage detected