Compute Library
 20.08
TensorAllocator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
25 
31 
32 #include "support/MemorySupport.h"
33 
34 #include "tests/Globals.h"
35 #include "tests/Utils.h"
37 #include "tests/framework/Macros.h"
40 
41 #include <memory>
42 #include <random>
43 
44 namespace arm_compute
45 {
46 namespace test
47 {
48 namespace validation
49 {
50 TEST_SUITE(NEON)
51 TEST_SUITE(UNIT)
52 TEST_SUITE(TensorAllocator)
53 
54 TEST_CASE(ImportMemory, framework::DatasetMode::ALL)
55 {
56  // Init tensor info
58 
59  // Allocate memory buffer
60  const size_t total_size = info.total_size();
61  auto data = support::cpp14::make_unique<uint8_t[]>(total_size);
62 
63  // Negative case : Import nullptr
64  Tensor t1;
65  t1.allocator()->init(info);
68 
69  // Negative case : Import misaligned pointer
70  Tensor t2;
71  const size_t required_alignment = 339;
72  t2.allocator()->init(info, required_alignment);
73  ARM_COMPUTE_EXPECT(!bool(t2.allocator()->import_memory(data.get())), framework::LogLevel::ERRORS);
74  ARM_COMPUTE_EXPECT(t2.info()->is_resizable(), framework::LogLevel::ERRORS);
75 
76  // Negative case : Import memory to a tensor that is memory managed
77  Tensor t3;
78  MemoryGroup mg;
79  t3.allocator()->set_associated_memory_group(&mg);
80  ARM_COMPUTE_EXPECT(!bool(t3.allocator()->import_memory(data.get())), framework::LogLevel::ERRORS);
81  ARM_COMPUTE_EXPECT(t3.info()->is_resizable(), framework::LogLevel::ERRORS);
82 
83  // Positive case : Set raw pointer
84  Tensor t4;
85  t4.allocator()->init(info);
86  ARM_COMPUTE_EXPECT(bool(t4.allocator()->import_memory(data.get())), framework::LogLevel::ERRORS);
87  ARM_COMPUTE_EXPECT(!t4.info()->is_resizable(), framework::LogLevel::ERRORS);
88  ARM_COMPUTE_EXPECT(t4.buffer() == reinterpret_cast<uint8_t *>(data.get()), framework::LogLevel::ERRORS);
89  t4.allocator()->free();
90  ARM_COMPUTE_EXPECT(t4.info()->is_resizable(), framework::LogLevel::ERRORS);
91  ARM_COMPUTE_EXPECT(t4.buffer() == nullptr, framework::LogLevel::ERRORS);
92 }
93 
94 TEST_CASE(ImportMemoryMalloc, framework::DatasetMode::ALL)
95 {
97  const TensorShape shape = TensorShape(24U, 16U, 3U);
99 
100  // Create tensor
101  const TensorInfo info(shape, 1, data_type);
102  const size_t required_alignment = 64;
103  Tensor tensor;
104  tensor.allocator()->init(info, required_alignment);
105 
106  // Create and configure activation function
107  NEActivationLayer act_func;
108  act_func.configure(&tensor, nullptr, act_info);
109 
110  // Allocate and import tensor
111  const size_t total_size_in_elems = tensor.info()->tensor_shape().total_size();
112  const size_t total_size_in_bytes = tensor.info()->total_size();
113  size_t space = total_size_in_bytes + required_alignment;
114  auto raw_data = support::cpp14::make_unique<uint8_t[]>(space);
115 
116  void *aligned_ptr = raw_data.get();
117  support::cpp11::align(required_alignment, total_size_in_bytes, aligned_ptr, space);
118 
121 
122  // Fill tensor
123  std::uniform_real_distribution<float> distribution(-5.f, 5.f);
124  std::mt19937 gen(library->seed());
125  auto *typed_ptr = reinterpret_cast<float *>(aligned_ptr);
126  for(unsigned int i = 0; i < total_size_in_elems; ++i)
127  {
128  typed_ptr[i] = distribution(gen);
129  }
130 
131  // Execute function and sync
132  act_func.run();
133 
134  // Validate result by checking that the input has no negative values
135  for(unsigned int i = 0; i < total_size_in_elems; ++i)
136  {
138  }
139 
140  // Release resources
141  tensor.allocator()->free();
143 }
144 
145 TEST_CASE(ImportMemoryMallocPadded, framework::DatasetMode::ALL)
146 {
147  // Create tensor
148  Tensor tensor;
149  tensor.allocator()->init(TensorInfo(TensorShape(24U, 16U, 3U), 1, DataType::F32));
150 
151  // Enforce tensor padding and validate that meta-data were updated
152  // Note: Padding might be updated after the function configuration in case of increased padding requirements
153  const PaddingSize enforced_padding(3U, 5U, 2U, 4U);
154  tensor.info()->extend_padding(enforced_padding);
155  validate(tensor.info()->padding(), enforced_padding);
156 
157  // Create and configure activation function
158  NEActivationLayer act_func;
160 
161  // Allocate and import tensor
162  const size_t total_size_in_bytes = tensor.info()->total_size();
163  auto raw_data = support::cpp14::make_unique<uint8_t[]>(total_size_in_bytes);
164 
165  ARM_COMPUTE_EXPECT(bool(tensor.allocator()->import_memory(raw_data.get())), framework::LogLevel::ERRORS);
167 
168  // Fill tensor while accounting padding
169  std::uniform_real_distribution<float> distribution(-5.f, 5.f);
170  std::mt19937 gen(library->seed());
171 
172  Window tensor_window;
173  tensor_window.use_tensor_dimensions(tensor.info()->tensor_shape());
174  Iterator tensor_it(&tensor, tensor_window);
175 
176  execute_window_loop(tensor_window, [&](const Coordinates &)
177  {
178  *reinterpret_cast<float *>(tensor_it.ptr()) = distribution(gen);
179  },
180  tensor_it);
181 
182  // Execute function and sync
183  act_func.run();
184 
185  // Validate result by checking that the input has no negative values
186  execute_window_loop(tensor_window, [&](const Coordinates &)
187  {
188  const float val = *reinterpret_cast<float *>(tensor_it.ptr());
190  },
191  tensor_it);
192 
193  // Release resources
194  tensor.allocator()->free();
196 }
197 
198 #if !defined(BARE_METAL)
199 TEST_CASE(ImportMemoryMappedFile, framework::DatasetMode::ALL)
200 {
202  const TensorShape shape = TensorShape(24U, 16U, 3U);
204 
205  // Create tensor
206  const TensorInfo info(shape, 1, data_type);
207  Tensor tensor;
208  tensor.allocator()->init(info);
209 
210  // Create and configure activation function
211  NEActivationLayer act_func;
212  act_func.configure(&tensor, nullptr, act_info);
213 
214  // Get number of elements
215  const size_t total_size_in_elems = tensor.info()->tensor_shape().total_size();
216  const size_t total_size_in_bytes = tensor.info()->total_size();
217 
218  // Create file
219  std::ofstream output_file("test_mmap_import.bin", std::ios::binary | std::ios::out);
220  output_file.seekp(total_size_in_bytes - 1);
221  output_file.write("", 1);
222  output_file.close();
223 
224  // Map file
225  utils::mmap_io::MMappedFile mmapped_file("test_mmap_import.bin", 0 /** Whole file */, 0);
226  ARM_COMPUTE_EXPECT(mmapped_file.is_mapped(), framework::LogLevel::ERRORS);
227  unsigned char *data = mmapped_file.data();
228 
229  // Import memory mapped memory
232 
233  // Fill tensor
234  std::uniform_real_distribution<float> distribution(-5.f, 5.f);
235  std::mt19937 gen(library->seed());
236  auto *typed_ptr = reinterpret_cast<float *>(data);
237  for(unsigned int i = 0; i < total_size_in_elems; ++i)
238  {
239  typed_ptr[i] = distribution(gen);
240  }
241 
242  // Execute function and sync
243  act_func.run();
244 
245  // Validate result by checking that the input has no negative values
246  for(unsigned int i = 0; i < total_size_in_elems; ++i)
247  {
249  }
250 
251  // Release resources
252  tensor.allocator()->free();
254 }
255 #endif // !defined(BARE_METAL)
256 
258 {
259  // Init tensor info
261  const size_t requested_alignment = 1024;
262 
263  Tensor t;
264  t.allocator()->init(info, requested_alignment);
265  t.allocator()->allocate();
266 
268  ARM_COMPUTE_EXPECT(t.allocator()->alignment() == requested_alignment, framework::LogLevel::ERRORS);
269  ARM_COMPUTE_EXPECT(arm_compute::utility::check_aligned(reinterpret_cast<void *>(t.buffer()), requested_alignment),
271 }
272 
276 } // namespace validation
277 } // namespace test
278 } // namespace arm_compute
Shape of a tensor.
Definition: TensorShape.h:39
void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info)
Shares the same backing memory with another tensor allocator, while the tensor info might be differen...
Container for 2D border size.
Definition: Types.h:272
1 channel, 1 F32 per channel
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
Activation Layer Information class.
Definition: Types.h:1517
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor's dimensions to fill the window dimensions.
Definition: Window.inl:276
Copyright (c) 2017-2020 Arm Limited.
TensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: Tensor.cpp:48
ITensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: Tensor.cpp:33
virtual bool is_resizable() const =0
Flag indicating whether the size of the tensor can be changed.
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:78
void run() override
Run the kernels contained in the function.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
Coordinates of an item.
Definition: Coordinates.h:37
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:171
size_t alignment() const
Return underlying's tensor buffer alignment.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:190
Basic implementation of the tensor interface.
Definition: Tensor.h:37
virtual PaddingSize padding() const =0
Padding of tensor.
void * align(std::size_t alignment, std::size_t size, void *&ptr, std::size_t &space)
Definition: MemorySupport.h:37
std::uniform_real_distribution< float > distribution(-5.f, 5.f)
void free() override
Free allocated CPU memory.
Basic function to run NEActivationLayerKernel.
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
validate(dst.info() ->valid_region(), valid_region)
void configure(ITensor *input, ITensor *output, ActivationLayerInfo activation_info)
[NEActivationLayer snippet]
uint8_t * buffer() const override
Interface to be implemented by the child class to return a pointer to CPU memory.
Definition: Tensor.cpp:43
Status import_memory(void *memory)
Import an existing memory as a tensor's backing memory.
Store the tensor's metadata.
Definition: TensorInfo.h:45
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:128
TEST_CASE(FusedActivation, framework::DatasetMode::ALL)
Validate fused activation expecting the following behaviours:
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:353
DataType
Available data types.
Definition: Types.h:77
virtual bool extend_padding(const PaddingSize &padding)=0
Update the offset to the first element, the strides and the total size.
Describe a multidimensional execution window.
Definition: Window.h:39
TEST_SUITE(U8_to_S8) DATA_TEST_CASE(Configuration
bool check_aligned(void *ptr, const size_t alignment)
Checks if a pointer complies with a given alignment.
Definition: Utility.h:192