xref: /aosp_15_r20/external/ComputeLibrary/examples/neon_cnn.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2016-2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "arm_compute/runtime/NEON/NEFunctions.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/Types.h"
27*c217d954SCole Faust #include "arm_compute/runtime/Allocator.h"
28*c217d954SCole Faust #include "arm_compute/runtime/BlobLifetimeManager.h"
29*c217d954SCole Faust #include "arm_compute/runtime/MemoryManagerOnDemand.h"
30*c217d954SCole Faust #include "arm_compute/runtime/PoolManager.h"
31*c217d954SCole Faust #include "utils/Utils.h"
32*c217d954SCole Faust 
33*c217d954SCole Faust using namespace arm_compute;
34*c217d954SCole Faust using namespace utils;
35*c217d954SCole Faust 
36*c217d954SCole Faust class NEONCNNExample : public Example
37*c217d954SCole Faust {
38*c217d954SCole Faust public:
do_setup(int argc,char ** argv)39*c217d954SCole Faust     bool do_setup(int argc, char **argv) override
40*c217d954SCole Faust     {
41*c217d954SCole Faust         ARM_COMPUTE_UNUSED(argc);
42*c217d954SCole Faust         ARM_COMPUTE_UNUSED(argv);
43*c217d954SCole Faust 
44*c217d954SCole Faust         // Create memory manager components
45*c217d954SCole Faust         // 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))
46*c217d954SCole Faust         auto lifetime_mgr0  = std::make_shared<BlobLifetimeManager>();                           // Create lifetime manager
47*c217d954SCole Faust         auto lifetime_mgr1  = std::make_shared<BlobLifetimeManager>();                           // Create lifetime manager
48*c217d954SCole Faust         auto pool_mgr0      = std::make_shared<PoolManager>();                                   // Create pool manager
49*c217d954SCole Faust         auto pool_mgr1      = std::make_shared<PoolManager>();                                   // Create pool manager
50*c217d954SCole Faust         auto mm_layers      = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr0, pool_mgr0); // Create the memory manager
51*c217d954SCole Faust         auto mm_transitions = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr1, pool_mgr1); // Create the memory manager
52*c217d954SCole Faust 
53*c217d954SCole Faust         // The weights and biases tensors should be initialized with the values inferred with the training
54*c217d954SCole Faust 
55*c217d954SCole Faust         // Set memory manager where allowed to manage internal memory requirements
56*c217d954SCole Faust         conv0   = std::make_unique<NEConvolutionLayer>(mm_layers);
57*c217d954SCole Faust         conv1   = std::make_unique<NEConvolutionLayer>(mm_layers);
58*c217d954SCole Faust         fc0     = std::make_unique<NEFullyConnectedLayer>(mm_layers);
59*c217d954SCole Faust         softmax = std::make_unique<NESoftmaxLayer>(mm_layers);
60*c217d954SCole Faust 
61*c217d954SCole Faust         /* [Initialize tensors] */
62*c217d954SCole Faust 
63*c217d954SCole Faust         // Initialize src tensor
64*c217d954SCole Faust         constexpr unsigned int width_src_image  = 32;
65*c217d954SCole Faust         constexpr unsigned int height_src_image = 32;
66*c217d954SCole Faust         constexpr unsigned int ifm_src_img      = 1;
67*c217d954SCole Faust 
68*c217d954SCole Faust         const TensorShape src_shape(width_src_image, height_src_image, ifm_src_img);
69*c217d954SCole Faust         src.allocator()->init(TensorInfo(src_shape, 1, DataType::F32));
70*c217d954SCole Faust 
71*c217d954SCole Faust         // Initialize tensors of conv0
72*c217d954SCole Faust         constexpr unsigned int kernel_x_conv0 = 5;
73*c217d954SCole Faust         constexpr unsigned int kernel_y_conv0 = 5;
74*c217d954SCole Faust         constexpr unsigned int ofm_conv0      = 8;
75*c217d954SCole Faust 
76*c217d954SCole Faust         const TensorShape weights_shape_conv0(kernel_x_conv0, kernel_y_conv0, src_shape.z(), ofm_conv0);
77*c217d954SCole Faust         const TensorShape biases_shape_conv0(weights_shape_conv0[3]);
78*c217d954SCole Faust         const TensorShape out_shape_conv0(src_shape.x(), src_shape.y(), weights_shape_conv0[3]);
79*c217d954SCole Faust 
80*c217d954SCole Faust         weights0.allocator()->init(TensorInfo(weights_shape_conv0, 1, DataType::F32));
81*c217d954SCole Faust         biases0.allocator()->init(TensorInfo(biases_shape_conv0, 1, DataType::F32));
82*c217d954SCole Faust         out_conv0.allocator()->init(TensorInfo(out_shape_conv0, 1, DataType::F32));
83*c217d954SCole Faust 
84*c217d954SCole Faust         // Initialize tensor of act0
85*c217d954SCole Faust         out_act0.allocator()->init(TensorInfo(out_shape_conv0, 1, DataType::F32));
86*c217d954SCole Faust 
87*c217d954SCole Faust         // Initialize tensor of pool0
88*c217d954SCole Faust         TensorShape out_shape_pool0 = out_shape_conv0;
89*c217d954SCole Faust         out_shape_pool0.set(0, out_shape_pool0.x() / 2);
90*c217d954SCole Faust         out_shape_pool0.set(1, out_shape_pool0.y() / 2);
91*c217d954SCole Faust         out_pool0.allocator()->init(TensorInfo(out_shape_pool0, 1, DataType::F32));
92*c217d954SCole Faust 
93*c217d954SCole Faust         // Initialize tensors of conv1
94*c217d954SCole Faust         constexpr unsigned int kernel_x_conv1 = 3;
95*c217d954SCole Faust         constexpr unsigned int kernel_y_conv1 = 3;
96*c217d954SCole Faust         constexpr unsigned int ofm_conv1      = 16;
97*c217d954SCole Faust 
98*c217d954SCole Faust         const TensorShape weights_shape_conv1(kernel_x_conv1, kernel_y_conv1, out_shape_pool0.z(), ofm_conv1);
99*c217d954SCole Faust 
100*c217d954SCole Faust         const TensorShape biases_shape_conv1(weights_shape_conv1[3]);
101*c217d954SCole Faust         const TensorShape out_shape_conv1(out_shape_pool0.x(), out_shape_pool0.y(), weights_shape_conv1[3]);
102*c217d954SCole Faust 
103*c217d954SCole Faust         weights1.allocator()->init(TensorInfo(weights_shape_conv1, 1, DataType::F32));
104*c217d954SCole Faust         biases1.allocator()->init(TensorInfo(biases_shape_conv1, 1, DataType::F32));
105*c217d954SCole Faust         out_conv1.allocator()->init(TensorInfo(out_shape_conv1, 1, DataType::F32));
106*c217d954SCole Faust 
107*c217d954SCole Faust         // Initialize tensor of act1
108*c217d954SCole Faust         out_act1.allocator()->init(TensorInfo(out_shape_conv1, 1, DataType::F32));
109*c217d954SCole Faust 
110*c217d954SCole Faust         // Initialize tensor of pool1
111*c217d954SCole Faust         TensorShape out_shape_pool1 = out_shape_conv1;
112*c217d954SCole Faust         out_shape_pool1.set(0, out_shape_pool1.x() / 2);
113*c217d954SCole Faust         out_shape_pool1.set(1, out_shape_pool1.y() / 2);
114*c217d954SCole Faust         out_pool1.allocator()->init(TensorInfo(out_shape_pool1, 1, DataType::F32));
115*c217d954SCole Faust 
116*c217d954SCole Faust         // Initialize tensor of fc0
117*c217d954SCole Faust         constexpr unsigned int num_labels = 128;
118*c217d954SCole Faust 
119*c217d954SCole Faust         const TensorShape weights_shape_fc0(out_shape_pool1.x() * out_shape_pool1.y() * out_shape_pool1.z(), num_labels);
120*c217d954SCole Faust         const TensorShape biases_shape_fc0(num_labels);
121*c217d954SCole Faust         const TensorShape out_shape_fc0(num_labels);
122*c217d954SCole Faust 
123*c217d954SCole Faust         weights2.allocator()->init(TensorInfo(weights_shape_fc0, 1, DataType::F32));
124*c217d954SCole Faust         biases2.allocator()->init(TensorInfo(biases_shape_fc0, 1, DataType::F32));
125*c217d954SCole Faust         out_fc0.allocator()->init(TensorInfo(out_shape_fc0, 1, DataType::F32));
126*c217d954SCole Faust 
127*c217d954SCole Faust         // Initialize tensor of act2
128*c217d954SCole Faust         out_act2.allocator()->init(TensorInfo(out_shape_fc0, 1, DataType::F32));
129*c217d954SCole Faust 
130*c217d954SCole Faust         // Initialize tensor of softmax
131*c217d954SCole Faust         const TensorShape out_shape_softmax(out_shape_fc0.x());
132*c217d954SCole Faust         out_softmax.allocator()->init(TensorInfo(out_shape_softmax, 1, DataType::F32));
133*c217d954SCole Faust 
134*c217d954SCole Faust         constexpr auto data_layout = DataLayout::NCHW;
135*c217d954SCole Faust 
136*c217d954SCole Faust         /* -----------------------End: [Initialize tensors] */
137*c217d954SCole Faust 
138*c217d954SCole Faust         /* [Configure functions] */
139*c217d954SCole Faust 
140*c217d954SCole Faust         // in:32x32x1: 5x5 convolution, 8 output features maps (OFM)
141*c217d954SCole Faust         conv0->configure(&src, &weights0, &biases0, &out_conv0, PadStrideInfo(1 /* stride_x */, 1 /* stride_y */, 2 /* pad_x */, 2 /* pad_y */));
142*c217d954SCole Faust 
143*c217d954SCole Faust         // in:32x32x8, out:32x32x8, Activation function: relu
144*c217d954SCole Faust         act0.configure(&out_conv0, &out_act0, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
145*c217d954SCole Faust 
146*c217d954SCole Faust         // in:32x32x8, out:16x16x8 (2x2 pooling), Pool type function: Max
147*c217d954SCole Faust         pool0.configure(&out_act0, &out_pool0, PoolingLayerInfo(PoolingType::MAX, 2, data_layout, PadStrideInfo(2 /* stride_x */, 2 /* stride_y */)));
148*c217d954SCole Faust 
149*c217d954SCole Faust         // in:16x16x8: 3x3 convolution, 16 output features maps (OFM)
150*c217d954SCole Faust         conv1->configure(&out_pool0, &weights1, &biases1, &out_conv1, PadStrideInfo(1 /* stride_x */, 1 /* stride_y */, 1 /* pad_x */, 1 /* pad_y */));
151*c217d954SCole Faust 
152*c217d954SCole Faust         // in:16x16x16, out:16x16x16, Activation function: relu
153*c217d954SCole Faust         act1.configure(&out_conv1, &out_act1, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
154*c217d954SCole Faust 
155*c217d954SCole Faust         // in:16x16x16, out:8x8x16 (2x2 pooling), Pool type function: Average
156*c217d954SCole Faust         pool1.configure(&out_act1, &out_pool1, PoolingLayerInfo(PoolingType::AVG, 2, data_layout, PadStrideInfo(2 /* stride_x */, 2 /* stride_y */)));
157*c217d954SCole Faust 
158*c217d954SCole Faust         // in:8x8x16, out:128
159*c217d954SCole Faust         fc0->configure(&out_pool1, &weights2, &biases2, &out_fc0);
160*c217d954SCole Faust 
161*c217d954SCole Faust         // in:128, out:128, Activation function: relu
162*c217d954SCole Faust         act2.configure(&out_fc0, &out_act2, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
163*c217d954SCole Faust 
164*c217d954SCole Faust         // in:128, out:128
165*c217d954SCole Faust         softmax->configure(&out_act2, &out_softmax);
166*c217d954SCole Faust 
167*c217d954SCole Faust         /* -----------------------End: [Configure functions] */
168*c217d954SCole Faust 
169*c217d954SCole Faust         /*[ Add tensors to memory manager ]*/
170*c217d954SCole Faust 
171*c217d954SCole Faust         // We need 2 memory groups for handling the input and output
172*c217d954SCole Faust         // We call explicitly allocate after manage() in order to avoid overlapping lifetimes
173*c217d954SCole Faust         memory_group0 = std::make_unique<MemoryGroup>(mm_transitions);
174*c217d954SCole Faust         memory_group1 = std::make_unique<MemoryGroup>(mm_transitions);
175*c217d954SCole Faust 
176*c217d954SCole Faust         memory_group0->manage(&out_conv0);
177*c217d954SCole Faust         out_conv0.allocator()->allocate();
178*c217d954SCole Faust         memory_group1->manage(&out_act0);
179*c217d954SCole Faust         out_act0.allocator()->allocate();
180*c217d954SCole Faust         memory_group0->manage(&out_pool0);
181*c217d954SCole Faust         out_pool0.allocator()->allocate();
182*c217d954SCole Faust         memory_group1->manage(&out_conv1);
183*c217d954SCole Faust         out_conv1.allocator()->allocate();
184*c217d954SCole Faust         memory_group0->manage(&out_act1);
185*c217d954SCole Faust         out_act1.allocator()->allocate();
186*c217d954SCole Faust         memory_group1->manage(&out_pool1);
187*c217d954SCole Faust         out_pool1.allocator()->allocate();
188*c217d954SCole Faust         memory_group0->manage(&out_fc0);
189*c217d954SCole Faust         out_fc0.allocator()->allocate();
190*c217d954SCole Faust         memory_group1->manage(&out_act2);
191*c217d954SCole Faust         out_act2.allocator()->allocate();
192*c217d954SCole Faust         memory_group0->manage(&out_softmax);
193*c217d954SCole Faust         out_softmax.allocator()->allocate();
194*c217d954SCole Faust 
195*c217d954SCole Faust         /* -----------------------End: [ Add tensors to memory manager ] */
196*c217d954SCole Faust 
197*c217d954SCole Faust         /* [Allocate tensors] */
198*c217d954SCole Faust 
199*c217d954SCole Faust         // Now that the padding requirements are known we can allocate all tensors
200*c217d954SCole Faust         src.allocator()->allocate();
201*c217d954SCole Faust         weights0.allocator()->allocate();
202*c217d954SCole Faust         weights1.allocator()->allocate();
203*c217d954SCole Faust         weights2.allocator()->allocate();
204*c217d954SCole Faust         biases0.allocator()->allocate();
205*c217d954SCole Faust         biases1.allocator()->allocate();
206*c217d954SCole Faust         biases2.allocator()->allocate();
207*c217d954SCole Faust 
208*c217d954SCole Faust         /* -----------------------End: [Allocate tensors] */
209*c217d954SCole Faust 
210*c217d954SCole Faust         // Populate the layers manager. (Validity checks, memory allocations etc)
211*c217d954SCole Faust         mm_layers->populate(allocator, 1 /* num_pools */);
212*c217d954SCole Faust 
213*c217d954SCole Faust         // Populate the transitions manager. (Validity checks, memory allocations etc)
214*c217d954SCole Faust         mm_transitions->populate(allocator, 2 /* num_pools */);
215*c217d954SCole Faust 
216*c217d954SCole Faust         return true;
217*c217d954SCole Faust     }
do_run()218*c217d954SCole Faust     void do_run() override
219*c217d954SCole Faust     {
220*c217d954SCole Faust         // Acquire memory for the memory groups
221*c217d954SCole Faust         memory_group0->acquire();
222*c217d954SCole Faust         memory_group1->acquire();
223*c217d954SCole Faust 
224*c217d954SCole Faust         conv0->run();
225*c217d954SCole Faust         act0.run();
226*c217d954SCole Faust         pool0.run();
227*c217d954SCole Faust         conv1->run();
228*c217d954SCole Faust         act1.run();
229*c217d954SCole Faust         pool1.run();
230*c217d954SCole Faust         fc0->run();
231*c217d954SCole Faust         act2.run();
232*c217d954SCole Faust         softmax->run();
233*c217d954SCole Faust 
234*c217d954SCole Faust         // Release memory
235*c217d954SCole Faust         memory_group0->release();
236*c217d954SCole Faust         memory_group1->release();
237*c217d954SCole Faust     }
238*c217d954SCole Faust 
239*c217d954SCole Faust private:
240*c217d954SCole Faust     // The src tensor should contain the input image
241*c217d954SCole Faust     Tensor src{};
242*c217d954SCole Faust 
243*c217d954SCole Faust     // Intermediate tensors used
244*c217d954SCole Faust     Tensor weights0{};
245*c217d954SCole Faust     Tensor weights1{};
246*c217d954SCole Faust     Tensor weights2{};
247*c217d954SCole Faust     Tensor biases0{};
248*c217d954SCole Faust     Tensor biases1{};
249*c217d954SCole Faust     Tensor biases2{};
250*c217d954SCole Faust     Tensor out_conv0{};
251*c217d954SCole Faust     Tensor out_conv1{};
252*c217d954SCole Faust     Tensor out_act0{};
253*c217d954SCole Faust     Tensor out_act1{};
254*c217d954SCole Faust     Tensor out_act2{};
255*c217d954SCole Faust     Tensor out_pool0{};
256*c217d954SCole Faust     Tensor out_pool1{};
257*c217d954SCole Faust     Tensor out_fc0{};
258*c217d954SCole Faust     Tensor out_softmax{};
259*c217d954SCole Faust 
260*c217d954SCole Faust     // Allocator
261*c217d954SCole Faust     Allocator allocator{};
262*c217d954SCole Faust 
263*c217d954SCole Faust     // Memory groups
264*c217d954SCole Faust     std::unique_ptr<MemoryGroup> memory_group0{};
265*c217d954SCole Faust     std::unique_ptr<MemoryGroup> memory_group1{};
266*c217d954SCole Faust 
267*c217d954SCole Faust     // Layers
268*c217d954SCole Faust     std::unique_ptr<NEConvolutionLayer>    conv0{};
269*c217d954SCole Faust     std::unique_ptr<NEConvolutionLayer>    conv1{};
270*c217d954SCole Faust     std::unique_ptr<NEFullyConnectedLayer> fc0{};
271*c217d954SCole Faust     std::unique_ptr<NESoftmaxLayer>        softmax{};
272*c217d954SCole Faust     NEPoolingLayer                         pool0{};
273*c217d954SCole Faust     NEPoolingLayer                         pool1{};
274*c217d954SCole Faust     NEActivationLayer                      act0{};
275*c217d954SCole Faust     NEActivationLayer                      act1{};
276*c217d954SCole Faust     NEActivationLayer                      act2{};
277*c217d954SCole Faust };
278*c217d954SCole Faust 
279*c217d954SCole Faust /** Main program for cnn test
280*c217d954SCole Faust  *
281*c217d954SCole Faust  * The example implements the following CNN architecture:
282*c217d954SCole Faust  *
283*c217d954SCole Faust  * Input -> conv0:5x5 -> act0:relu -> pool:2x2 -> conv1:3x3 -> act1:relu -> pool:2x2 -> fc0 -> act2:relu -> softmax
284*c217d954SCole Faust  *
285*c217d954SCole Faust  * @param[in] argc Number of arguments
286*c217d954SCole Faust  * @param[in] argv Arguments
287*c217d954SCole Faust  */
main(int argc,char ** argv)288*c217d954SCole Faust int main(int argc, char **argv)
289*c217d954SCole Faust {
290*c217d954SCole Faust     return utils::run_example<NEONCNNExample>(argc, argv);
291*c217d954SCole Faust }
292