blob: 1f7a1ea6ca688ef6a38eb81f37391ed835e451e8 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2016-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010024#include "arm_compute/core/Types.h"
Gian Marco Iodicee7f7b552017-09-28 10:43:38 +010025#include "arm_compute/runtime/Allocator.h"
26#include "arm_compute/runtime/BlobLifetimeManager.h"
27#include "arm_compute/runtime/MemoryManagerOnDemand.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028#include "arm_compute/runtime/NEON/NEFunctions.h"
Gian Marco Iodicee7f7b552017-09-28 10:43:38 +010029#include "arm_compute/runtime/PoolManager.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "utils/Utils.h"
32
33using namespace arm_compute;
34using namespace utils;
35
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000036class NEONCNNExample : public Example
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000038public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010039 bool do_setup(int argc, char **argv) override
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000040 {
41 ARM_COMPUTE_UNUSED(argc);
42 ARM_COMPUTE_UNUSED(argv);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000044 // Create memory manager components
45 // We need 2 memory managers: 1 for handling the tensors within the functions (mm_layers) and 1 for handling the input and output tensors of the functions (mm_transitions))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046 auto lifetime_mgr0 = std::make_shared<BlobLifetimeManager>(); // Create lifetime manager
47 auto lifetime_mgr1 = std::make_shared<BlobLifetimeManager>(); // Create lifetime manager
48 auto pool_mgr0 = std::make_shared<PoolManager>(); // Create pool manager
49 auto pool_mgr1 = std::make_shared<PoolManager>(); // Create pool manager
50 auto mm_layers = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr0, pool_mgr0); // Create the memory manager
51 auto mm_transitions =
52 std::make_shared<MemoryManagerOnDemand>(lifetime_mgr1, pool_mgr1); // Create the memory manager
Gian Marco Iodicee7f7b552017-09-28 10:43:38 +010053
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000054 // The weights and biases tensors should be initialized with the values inferred with the training
Gian Marco Iodicee7f7b552017-09-28 10:43:38 +010055
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000056 // Set memory manager where allowed to manage internal memory requirements
Georgios Pinitas40f51a62020-11-21 03:04:18 +000057 conv0 = std::make_unique<NEConvolutionLayer>(mm_layers);
58 conv1 = std::make_unique<NEConvolutionLayer>(mm_layers);
59 fc0 = std::make_unique<NEFullyConnectedLayer>(mm_layers);
60 softmax = std::make_unique<NESoftmaxLayer>(mm_layers);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000061
62 /* [Initialize tensors] */
63
64 // Initialize src tensor
65 constexpr unsigned int width_src_image = 32;
66 constexpr unsigned int height_src_image = 32;
67 constexpr unsigned int ifm_src_img = 1;
68
69 const TensorShape src_shape(width_src_image, height_src_image, ifm_src_img);
70 src.allocator()->init(TensorInfo(src_shape, 1, DataType::F32));
71
72 // Initialize tensors of conv0
73 constexpr unsigned int kernel_x_conv0 = 5;
74 constexpr unsigned int kernel_y_conv0 = 5;
75 constexpr unsigned int ofm_conv0 = 8;
76
77 const TensorShape weights_shape_conv0(kernel_x_conv0, kernel_y_conv0, src_shape.z(), ofm_conv0);
78 const TensorShape biases_shape_conv0(weights_shape_conv0[3]);
79 const TensorShape out_shape_conv0(src_shape.x(), src_shape.y(), weights_shape_conv0[3]);
80
81 weights0.allocator()->init(TensorInfo(weights_shape_conv0, 1, DataType::F32));
82 biases0.allocator()->init(TensorInfo(biases_shape_conv0, 1, DataType::F32));
83 out_conv0.allocator()->init(TensorInfo(out_shape_conv0, 1, DataType::F32));
84
85 // Initialize tensor of act0
86 out_act0.allocator()->init(TensorInfo(out_shape_conv0, 1, DataType::F32));
87
88 // Initialize tensor of pool0
89 TensorShape out_shape_pool0 = out_shape_conv0;
90 out_shape_pool0.set(0, out_shape_pool0.x() / 2);
91 out_shape_pool0.set(1, out_shape_pool0.y() / 2);
92 out_pool0.allocator()->init(TensorInfo(out_shape_pool0, 1, DataType::F32));
93
94 // Initialize tensors of conv1
95 constexpr unsigned int kernel_x_conv1 = 3;
96 constexpr unsigned int kernel_y_conv1 = 3;
97 constexpr unsigned int ofm_conv1 = 16;
98
99 const TensorShape weights_shape_conv1(kernel_x_conv1, kernel_y_conv1, out_shape_pool0.z(), ofm_conv1);
100
101 const TensorShape biases_shape_conv1(weights_shape_conv1[3]);
102 const TensorShape out_shape_conv1(out_shape_pool0.x(), out_shape_pool0.y(), weights_shape_conv1[3]);
103
104 weights1.allocator()->init(TensorInfo(weights_shape_conv1, 1, DataType::F32));
105 biases1.allocator()->init(TensorInfo(biases_shape_conv1, 1, DataType::F32));
106 out_conv1.allocator()->init(TensorInfo(out_shape_conv1, 1, DataType::F32));
107
108 // Initialize tensor of act1
109 out_act1.allocator()->init(TensorInfo(out_shape_conv1, 1, DataType::F32));
110
111 // Initialize tensor of pool1
112 TensorShape out_shape_pool1 = out_shape_conv1;
113 out_shape_pool1.set(0, out_shape_pool1.x() / 2);
114 out_shape_pool1.set(1, out_shape_pool1.y() / 2);
115 out_pool1.allocator()->init(TensorInfo(out_shape_pool1, 1, DataType::F32));
116
117 // Initialize tensor of fc0
118 constexpr unsigned int num_labels = 128;
119
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 const TensorShape weights_shape_fc0(out_shape_pool1.x() * out_shape_pool1.y() * out_shape_pool1.z(),
121 num_labels);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000122 const TensorShape biases_shape_fc0(num_labels);
123 const TensorShape out_shape_fc0(num_labels);
124
125 weights2.allocator()->init(TensorInfo(weights_shape_fc0, 1, DataType::F32));
126 biases2.allocator()->init(TensorInfo(biases_shape_fc0, 1, DataType::F32));
127 out_fc0.allocator()->init(TensorInfo(out_shape_fc0, 1, DataType::F32));
128
129 // Initialize tensor of act2
130 out_act2.allocator()->init(TensorInfo(out_shape_fc0, 1, DataType::F32));
131
132 // Initialize tensor of softmax
133 const TensorShape out_shape_softmax(out_shape_fc0.x());
134 out_softmax.allocator()->init(TensorInfo(out_shape_softmax, 1, DataType::F32));
135
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000136 constexpr auto data_layout = DataLayout::NCHW;
137
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000138 /* -----------------------End: [Initialize tensors] */
139
140 /* [Configure functions] */
141
142 // in:32x32x1: 5x5 convolution, 8 output features maps (OFM)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100143 conv0->configure(&src, &weights0, &biases0, &out_conv0,
144 PadStrideInfo(1 /* stride_x */, 1 /* stride_y */, 2 /* pad_x */, 2 /* pad_y */));
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000145
146 // in:32x32x8, out:32x32x8, Activation function: relu
147 act0.configure(&out_conv0, &out_act0, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
148
149 // in:32x32x8, out:16x16x8 (2x2 pooling), Pool type function: Max
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100150 pool0.configure(
151 &out_act0, &out_pool0,
152 PoolingLayerInfo(PoolingType::MAX, 2, data_layout, PadStrideInfo(2 /* stride_x */, 2 /* stride_y */)));
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000153
154 // in:16x16x8: 3x3 convolution, 16 output features maps (OFM)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 conv1->configure(&out_pool0, &weights1, &biases1, &out_conv1,
156 PadStrideInfo(1 /* stride_x */, 1 /* stride_y */, 1 /* pad_x */, 1 /* pad_y */));
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000157
158 // in:16x16x16, out:16x16x16, Activation function: relu
159 act1.configure(&out_conv1, &out_act1, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
160
161 // in:16x16x16, out:8x8x16 (2x2 pooling), Pool type function: Average
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100162 pool1.configure(
163 &out_act1, &out_pool1,
164 PoolingLayerInfo(PoolingType::AVG, 2, data_layout, PadStrideInfo(2 /* stride_x */, 2 /* stride_y */)));
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000165
166 // in:8x8x16, out:128
167 fc0->configure(&out_pool1, &weights2, &biases2, &out_fc0);
168
169 // in:128, out:128, Activation function: relu
170 act2.configure(&out_fc0, &out_act2, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
171
172 // in:128, out:128
173 softmax->configure(&out_act2, &out_softmax);
174
175 /* -----------------------End: [Configure functions] */
176
177 /*[ Add tensors to memory manager ]*/
178
179 // We need 2 memory groups for handling the input and output
180 // We call explicitly allocate after manage() in order to avoid overlapping lifetimes
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000181 memory_group0 = std::make_unique<MemoryGroup>(mm_transitions);
182 memory_group1 = std::make_unique<MemoryGroup>(mm_transitions);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000183
184 memory_group0->manage(&out_conv0);
185 out_conv0.allocator()->allocate();
186 memory_group1->manage(&out_act0);
187 out_act0.allocator()->allocate();
188 memory_group0->manage(&out_pool0);
189 out_pool0.allocator()->allocate();
190 memory_group1->manage(&out_conv1);
191 out_conv1.allocator()->allocate();
192 memory_group0->manage(&out_act1);
193 out_act1.allocator()->allocate();
194 memory_group1->manage(&out_pool1);
195 out_pool1.allocator()->allocate();
196 memory_group0->manage(&out_fc0);
197 out_fc0.allocator()->allocate();
198 memory_group1->manage(&out_act2);
199 out_act2.allocator()->allocate();
200 memory_group0->manage(&out_softmax);
201 out_softmax.allocator()->allocate();
202
203 /* -----------------------End: [ Add tensors to memory manager ] */
204
205 /* [Allocate tensors] */
206
207 // Now that the padding requirements are known we can allocate all tensors
208 src.allocator()->allocate();
209 weights0.allocator()->allocate();
210 weights1.allocator()->allocate();
211 weights2.allocator()->allocate();
212 biases0.allocator()->allocate();
213 biases1.allocator()->allocate();
214 biases2.allocator()->allocate();
215
216 /* -----------------------End: [Allocate tensors] */
217
Georgios Pinitas9da19e92018-10-11 15:33:11 +0100218 // Populate the layers manager. (Validity checks, memory allocations etc)
219 mm_layers->populate(allocator, 1 /* num_pools */);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000220
Georgios Pinitas9da19e92018-10-11 15:33:11 +0100221 // Populate the transitions manager. (Validity checks, memory allocations etc)
222 mm_transitions->populate(allocator, 2 /* num_pools */);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100223
224 return true;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000225 }
226 void do_run() override
227 {
228 // Acquire memory for the memory groups
229 memory_group0->acquire();
230 memory_group1->acquire();
231
232 conv0->run();
233 act0.run();
234 pool0.run();
235 conv1->run();
236 act1.run();
237 pool1.run();
238 fc0->run();
239 act2.run();
240 softmax->run();
241
242 // Release memory
243 memory_group0->release();
244 memory_group1->release();
245 }
246
247private:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100248 // The src tensor should contain the input image
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000249 Tensor src{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100250
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000251 // Intermediate tensors used
252 Tensor weights0{};
253 Tensor weights1{};
254 Tensor weights2{};
255 Tensor biases0{};
256 Tensor biases1{};
257 Tensor biases2{};
258 Tensor out_conv0{};
259 Tensor out_conv1{};
260 Tensor out_act0{};
261 Tensor out_act1{};
262 Tensor out_act2{};
263 Tensor out_pool0{};
264 Tensor out_pool1{};
265 Tensor out_fc0{};
266 Tensor out_softmax{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100267
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000268 // Allocator
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000269 Allocator allocator{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100270
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000271 // Memory groups
272 std::unique_ptr<MemoryGroup> memory_group0{};
273 std::unique_ptr<MemoryGroup> memory_group1{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100274
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000275 // Layers
276 std::unique_ptr<NEConvolutionLayer> conv0{};
277 std::unique_ptr<NEConvolutionLayer> conv1{};
278 std::unique_ptr<NEFullyConnectedLayer> fc0{};
279 std::unique_ptr<NESoftmaxLayer> softmax{};
280 NEPoolingLayer pool0{};
281 NEPoolingLayer pool1{};
282 NEActivationLayer act0{};
283 NEActivationLayer act1{};
284 NEActivationLayer act2{};
285};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100286
287/** Main program for cnn test
288 *
289 * The example implements the following CNN architecture:
290 *
291 * Input -> conv0:5x5 -> act0:relu -> pool:2x2 -> conv1:3x3 -> act1:relu -> pool:2x2 -> fc0 -> act2:relu -> softmax
292 *
293 * @param[in] argc Number of arguments
294 * @param[in] argv Arguments
295 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000296int main(int argc, char **argv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100297{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000298 return utils::run_example<NEONCNNExample>(argc, argv);
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000299}