blob: dadd1de1f210c9e8bee98e9a1f1095e1a74553af [file] [log] [blame]
Matthew Bentham7c1603a2019-06-21 17:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#include <reference/RefTensorHandle.hpp>
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +01006#include <reference/RefTensorHandleFactory.hpp>
Matthew Bentham7c1603a2019-06-21 17:22:23 +01007
8#include <boost/test/unit_test.hpp>
9
10BOOST_AUTO_TEST_SUITE(RefTensorHandleTests)
11using namespace armnn;
12
13BOOST_AUTO_TEST_CASE(AcquireAndRelease)
14{
15 std::shared_ptr<RefMemoryManager> memoryManager = std::make_shared<RefMemoryManager>();
16
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010017 TensorInfo info({ 1, 1, 1, 1 }, DataType::Float32);
Matthew Bentham7c1603a2019-06-21 17:22:23 +010018 RefTensorHandle handle(info, memoryManager);
19
20 handle.Manage();
21 handle.Allocate();
22
23 memoryManager->Acquire();
24 {
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010025 float* buffer = reinterpret_cast<float*>(handle.Map());
Matthew Bentham7c1603a2019-06-21 17:22:23 +010026
27 BOOST_CHECK(buffer != nullptr); // Yields a valid pointer
28
29 buffer[0] = 2.5f;
30
31 BOOST_CHECK(buffer[0] == 2.5f); // Memory is writable and readable
32
33 }
34 memoryManager->Release();
35
36 memoryManager->Acquire();
37 {
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010038 float* buffer = reinterpret_cast<float*>(handle.Map());
Matthew Bentham7c1603a2019-06-21 17:22:23 +010039
40 BOOST_CHECK(buffer != nullptr); // Yields a valid pointer
41
42 buffer[0] = 3.5f;
43
44 BOOST_CHECK(buffer[0] == 3.5f); // Memory is writable and readable
45 }
46 memoryManager->Release();
47}
48
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010049BOOST_AUTO_TEST_CASE(RefTensorHandleFactoryMemoryManaged)
50{
51 std::shared_ptr<RefMemoryManager> memoryManager = std::make_shared<RefMemoryManager>();
52 RefTensorHandleFactory handleFactory(memoryManager);
53 TensorInfo info({ 1, 1, 2, 1 }, DataType::Float32);
54
55 // create TensorHandle with memory managed
56 auto handle = handleFactory.CreateTensorHandle(info, true);
57 handle->Manage();
58 handle->Allocate();
59
60 memoryManager->Acquire();
61 {
62 float* buffer = reinterpret_cast<float*>(handle->Map());
63 BOOST_CHECK(buffer != nullptr); // Yields a valid pointer
64 buffer[0] = 1.5f;
65 buffer[1] = 2.5f;
66 BOOST_CHECK(buffer[0] == 1.5f); // Memory is writable and readable
67 BOOST_CHECK(buffer[1] == 2.5f); // Memory is writable and readable
68 }
69 memoryManager->Release();
70
71 memoryManager->Acquire();
72 {
73 float* buffer = reinterpret_cast<float*>(handle->Map());
74 BOOST_CHECK(buffer != nullptr); // Yields a valid pointer
75 buffer[0] = 3.5f;
76 buffer[1] = 4.5f;
77 BOOST_CHECK(buffer[0] == 3.5f); // Memory is writable and readable
78 BOOST_CHECK(buffer[1] == 4.5f); // Memory is writable and readable
79 }
80 memoryManager->Release();
81
82 float testPtr[2] = { 2.5f, 5.5f };
83 // Cannot import as import is disabled
84 BOOST_CHECK(!handle->Import(static_cast<void*>(testPtr), MemorySource::Malloc));
85}
86
87BOOST_AUTO_TEST_CASE(RefTensorHandleFactoryImport)
88{
89 std::shared_ptr<RefMemoryManager> memoryManager = std::make_shared<RefMemoryManager>();
90 RefTensorHandleFactory handleFactory(memoryManager);
91 TensorInfo info({ 1, 1, 2, 1 }, DataType::Float32);
92
93 // create TensorHandle without memory managed
94 auto handle = handleFactory.CreateTensorHandle(info, false);
95 handle->Manage();
96 handle->Allocate();
97 memoryManager->Acquire();
98
99 // No buffer allocated when import is enabled
100 BOOST_CHECK_THROW(handle->Map(), armnn::NullPointerException);
101
102 float testPtr[2] = { 2.5f, 5.5f };
103 // Correctly import
104 BOOST_CHECK(handle->Import(static_cast<void*>(testPtr), MemorySource::Malloc));
105 float* buffer = reinterpret_cast<float*>(handle->Map());
106 BOOST_CHECK(buffer != nullptr); // Yields a valid pointer after import
107 BOOST_CHECK(buffer == testPtr); // buffer is pointing to testPtr
108 // Memory is writable and readable with correct value
109 BOOST_CHECK(buffer[0] == 2.5f);
110 BOOST_CHECK(buffer[1] == 5.5f);
111 buffer[0] = 3.5f;
112 buffer[1] = 10.0f;
113 BOOST_CHECK(buffer[0] == 3.5f);
114 BOOST_CHECK(buffer[1] == 10.0f);
115 memoryManager->Release();
116}
117
118BOOST_AUTO_TEST_CASE(RefTensorHandleImport)
119{
120 TensorInfo info({ 1, 1, 2, 1 }, DataType::Float32);
121 RefTensorHandle handle(info, static_cast<unsigned int>(MemorySource::Malloc));
122
123 handle.Manage();
124 handle.Allocate();
125
126 // No buffer allocated when import is enabled
127 BOOST_CHECK_THROW(handle.Map(), armnn::NullPointerException);
128
129 float testPtr[2] = { 2.5f, 5.5f };
130 // Correctly import
131 BOOST_CHECK(handle.Import(static_cast<void*>(testPtr), MemorySource::Malloc));
132 float* buffer = reinterpret_cast<float*>(handle.Map());
133 BOOST_CHECK(buffer != nullptr); // Yields a valid pointer after import
134 BOOST_CHECK(buffer == testPtr); // buffer is pointing to testPtr
135 // Memory is writable and readable with correct value
136 BOOST_CHECK(buffer[0] == 2.5f);
137 BOOST_CHECK(buffer[1] == 5.5f);
138 buffer[0] = 3.5f;
139 buffer[1] = 10.0f;
140 BOOST_CHECK(buffer[0] == 3.5f);
141 BOOST_CHECK(buffer[1] == 10.0f);
142}
143
Narumol Prangnawaratd6568772020-07-22 12:46:51 +0100144BOOST_AUTO_TEST_CASE(RefTensorHandleGetCapabilities)
145{
146 std::shared_ptr<RefMemoryManager> memoryManager = std::make_shared<RefMemoryManager>();
147 RefTensorHandleFactory handleFactory(memoryManager);
148
149 // Builds up the structure of the network.
150 INetworkPtr net(INetwork::Create());
151 IConnectableLayer* input = net->AddInputLayer(0);
152 IConnectableLayer* output = net->AddOutputLayer(0);
153 input->GetOutputSlot(0).Connect(output->GetInputSlot(0));
154
155 std::vector<Capability> capabilities = handleFactory.GetCapabilities(input,
156 output,
157 CapabilityClass::PaddingRequired);
158 BOOST_CHECK(capabilities.empty());
159}
160
Sadik Armaganab3bd4d2020-08-25 11:48:00 +0100161BOOST_AUTO_TEST_CASE(RefTensorHandleSupportsInPlaceComputation)
162{
163 std::shared_ptr<RefMemoryManager> memoryManager = std::make_shared<RefMemoryManager>();
164 RefTensorHandleFactory handleFactory(memoryManager);
165
166 // RefTensorHandleFactory does not support InPlaceComputation
167 ARMNN_ASSERT(!(handleFactory.SupportsInPlaceComputation()));
168}
169
Francis Murtagh4af56162021-04-20 16:37:55 +0100170BOOST_AUTO_TEST_CASE(TestManagedConstTensorHandle)
171{
172 // Initialize arguments
173 void* mem = nullptr;
174 TensorInfo info;
175
James Conroy1f58f032021-04-27 17:13:27 +0100176 // Use PassthroughTensor as others are abstract
177 auto passThroughHandle = std::make_shared<PassthroughTensorHandle>(info, mem);
Francis Murtagh4af56162021-04-20 16:37:55 +0100178
179 // Test managed handle is initialized with m_Mapped unset and once Map() called its set
180 ManagedConstTensorHandle managedHandle(passThroughHandle);
181 BOOST_CHECK(!managedHandle.IsMapped());
182 managedHandle.Map();
183 BOOST_CHECK(managedHandle.IsMapped());
184
185 // Test it can then be unmapped
186 managedHandle.Unmap();
187 BOOST_CHECK(!managedHandle.IsMapped());
188
189 // Test member function
190 BOOST_CHECK(managedHandle.GetTensorInfo() == info);
191
192 // Test that nullptr tensor handle doesn't get mapped
193 ManagedConstTensorHandle managedHandleNull(nullptr);
194 BOOST_CHECK(!managedHandleNull.IsMapped());
195 BOOST_CHECK_THROW(managedHandleNull.Map(), armnn::Exception);
196 BOOST_CHECK(!managedHandleNull.IsMapped());
197
198 // Check Unmap() when m_Mapped already false
199 managedHandleNull.Unmap();
200 BOOST_CHECK(!managedHandleNull.IsMapped());
201}
202
Ferran Balaguerc33882d2019-08-21 13:59:13 +0100203#if !defined(__ANDROID__)
204// Only run these tests on non Android platforms
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100205BOOST_AUTO_TEST_CASE(CheckSourceType)
206{
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100207 TensorInfo info({1}, DataType::Float32);
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +0100208 RefTensorHandle handle(info, static_cast<unsigned int>(MemorySource::Malloc));
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100209
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100210 int* testPtr = new int(4);
211
212 // Not supported
213 BOOST_CHECK(!handle.Import(static_cast<void *>(testPtr), MemorySource::DmaBuf));
214
215 // Not supported
216 BOOST_CHECK(!handle.Import(static_cast<void *>(testPtr), MemorySource::DmaBufProtected));
217
218 // Supported
219 BOOST_CHECK(handle.Import(static_cast<void *>(testPtr), MemorySource::Malloc));
Ferran Balaguer1cd451c2019-08-22 14:09:44 +0100220
221 delete testPtr;
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100222}
223
224BOOST_AUTO_TEST_CASE(ReusePointer)
225{
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100226 TensorInfo info({1}, DataType::Float32);
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +0100227 RefTensorHandle handle(info, static_cast<unsigned int>(MemorySource::Malloc));
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100228
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100229 int* testPtr = new int(4);
230
231 handle.Import(static_cast<void *>(testPtr), MemorySource::Malloc);
232
233 // Reusing previously Imported pointer
234 BOOST_CHECK(handle.Import(static_cast<void *>(testPtr), MemorySource::Malloc));
Ferran Balaguer1cd451c2019-08-22 14:09:44 +0100235
236 delete testPtr;
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100237}
238
239BOOST_AUTO_TEST_CASE(MisalignedPointer)
240{
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100241 TensorInfo info({2}, DataType::Float32);
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +0100242 RefTensorHandle handle(info, static_cast<unsigned int>(MemorySource::Malloc));
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100243
Aron Virginas-Tard9f7c8b2019-09-13 13:37:03 +0100244 // Allocate a 2 int array
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100245 int* testPtr = new int[2];
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100246
Aron Virginas-Tard9f7c8b2019-09-13 13:37:03 +0100247 // Increment pointer by 1 byte
248 void* misalignedPtr = static_cast<void*>(reinterpret_cast<char*>(testPtr) + 1);
249
250 BOOST_CHECK(!handle.Import(misalignedPtr, MemorySource::Malloc));
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100251
252 delete[] testPtr;
253}
254
Ferran Balaguerc33882d2019-08-21 13:59:13 +0100255#endif
256
Aron Virginas-Tard9f7c8b2019-09-13 13:37:03 +0100257BOOST_AUTO_TEST_SUITE_END()