blob: 13be712549fdc5abad50e88b576e68387b40c30e [file] [log] [blame]
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +00001///
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002/// Copyright (c) 2018-2019 Arm Limited.
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +00003///
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///
24
25namespace arm_compute
26{
27/**
28@page add_operator Adding new operators
29
30@tableofcontents
31
32@section S4_1_introduction Introduction
Michele Di Giorgio57f30a92020-09-08 14:03:51 +010033In Compute Library there are two main parts or modules:
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000034- The core library consists of a low-level collection of algorithms implemented in C++ and optimized for Arm CPUs and GPUs. The core module is designed to be embedded in other projects and it doesn't perform any memory management or scheduling.
35- The runtime library is a wrapper of the core library and provides other additional features like memory management, multithreaded execution of workloads and allocation of the intermediate tensors.
36
37The library can be integrated in an existing external library or application that provides its own scheduler or a specific memory manager. In that case, the right solution is to use only the core library which means that the user must also manage all the memory allocation not only for the input/output tensor but also for the intermediate tensors/variables necessary. On the other hand, if the user doesn't want to care about allocation and multithreading then the right choice is to use the functions from the runtime library.
38
39Apart from these components that get linked into the application, the sources also include the validation test suite and the C++ reference implementations against which all the operators are validated.
40
41
42@section S4_1_supporting_new_operators Supporting new operators
43
Michele Di Giorgio57f30a92020-09-08 14:03:51 +010044Following are the steps involved in adding support for a new operator in Compute Library
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000045- Add new data types (if required)
46- Add the kernel to the core library.
47- Add the function to the runtime library.
48- Add validation tests.
49 - Add the reference implementation.
50 - Add the fixture
51 - register the tests.
52
53@subsection S4_1_1_add_datatypes Adding new data types
54
Michele Di Giorgio57f30a92020-09-08 14:03:51 +010055Compute Library declares a few new datatypes related to its domain, kernels, and functions in the library process Tensors and Images (Computer Vision functions). Tensors are multi-dimensional arrays with a maximum of Coordinates::num_max_dimensions dimensions; depending on the number of dimensions tensors can be interpreted as various objects. A scalar can be represented as a zero-dimensional tensor and a vector of numbers can be represented as a one-dimensional tensor. Furthermore, an image is just a 2D tensor, a 3D tensor can be seen as an array of images and a 4D tensor as a 2D array of images, etc.
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000056All the datatype classes or structures are grouped in the core library folder arm_compute/core like the @ref ITensor, @ref ITensorInfo (all the information of a tensor), TensorShape and simpler types are in arm_compute/core/Types.h.
57
58If an operator handles a new datatype, it must be added to the library. While adding a new data type to the library, it's necessary to implement the function to enable printing, the to_string() method and the output stream insertion (<<) operator. Every datatype implements these two functions in utils/TypePrinter.h
59
60A quick example, in <a href="https://github.com/ARM-software/ComputeLibrary/blob/master/arm_compute/core/Types.h">Types.h</a> we add:
61
62@snippet arm_compute/core/Types.h DataLayout enum definition
63
64And for printing:
65
66@snippet utils/TypePrinter.h Print DataLayout type
67
Michele Di Giorgio57f30a92020-09-08 14:03:51 +010068In Compute Library, we use namespaces to group all the operators, functions, classes and interfaces. The main namespace to use is arm_compute. In the test suite, the test framework and the individual tests use nested namespaces like @ref test::validation or @ref test::benchmark to group the different purposes of various parts of the suite.
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000069Utility functions like conversion or type cast operators, that are shared by multiple operators are in arm_compute/core/Utils.h. Non-inlined function definitions go in the corresponding .cpp files in the src folder.
70Similarly, all common functions that process shapes, like calculating output shapes of an operator or shape conversions etc are in arm_compute/core/utils/misc/ShapeCalculator.h.
71
72
73@subsection S4_1_2_add_kernel Add a kernel
Michele Di Giorgio57f30a92020-09-08 14:03:51 +010074As we mentioned at the beginning, the kernel is the implementation of the operator or algorithm partially using a specific programming language related to the backend we want to use. Adding a kernel in the library means implementing the algorithm in a SIMD technology like NEON or OpenCL. All kernels in Compute Library must implement a common interface IKernel or one of the specific subinterfaces.
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000075IKernel is the common interface for all the kernels in the core library, it contains the main methods for configure and run the kernel itself, such as window() that return the maximum window the kernel can be executed on or is_parallelisable() for indicate whether or not the kernel is parallelizable. If the kernel is parallelizable then the window returned by the window() method can be split into sub-windows which can then be run in parallel, in the other case, only the window returned by window() can be passed to the run method.
76There are specific interfaces for OpenCL and Neon: @ref ICLKernel, INEKernel (using INEKernel = @ref ICPPKernel).
77
78- @ref ICLKernel is the common interface for all the OpenCL kernels. It implements the inherited methods and adds all the methods necessary to configure the CL kernel, such as set/return the Local-Workgroup-Size hint, add single, array or tensor argument, set the targeted GPU architecture according to the CL device. All these methods are used during the configuration and the run of the operator.
79- INEKernel inherits from @ref IKernel as well and it's the common interface for all kernels implemented in NEON, it adds just the run and the name methods.
80
81There are two others implementation of @ref IKernel called @ref ICLSimpleKernel and INESimpleKernel, they are the interface for simple kernels that have just one input tensor and one output tensor.
82Creating a new kernel implies adding new files:
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010083- src/core/CL/kernels/CLReshapeLayerKernel.h
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000084- src/core/CL/cl_kernels/reshape_layer.cl
85- src/core/CL/kernels/CLReshapeLayerKernel.cpp
86- src/core/CL/CLKernelLibrary.cpp
87
88Neon kernel
89- arm_compute/core/NEON/kernels/NEReshapeLayerKernel.h
90- src/core/NEON/kernels/NEReshapeLayerKernel.cpp
91
92We must register the new layer in the respective libraries:
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010093- src/core/CL/CLKernels.h
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000094- arm_compute/core/NEON/NEKernels.h
95
Michele Di Giorgio57f30a92020-09-08 14:03:51 +010096These files contain the list of all kernels available in the corresponding Compute Library's backend, for example CLKernels:
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000097@code{.cpp}
98...
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010099#include "src/core/CL/kernels/CLMinMaxLayerKernel.h"
100#include "src/core/CL/kernels/CLMinMaxLocationKernel.h"
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000101...
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100102#include "src/core/CL/kernels/CLReshapeLayerKernel.h"
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000103...
104
105@endcode
106
107For OpenCL we need to update the CLKernelLibrary.cpp, adding the appropriate code to embed the .cl kernel in the library. The OpenCL code can be compiled offline and embed in the library as binary.
108The essential operation we want to do with a kernel will be
109- create the kernel object
110- initialize the kernel with the input/output and any other parameters that may be required
111- retrieve the execution window of the kernel and run the whole kernel window in the current thread or use the multithreading.
112
113Each kernel will have to implement the method:
114- %validate: is a static function that checks if the given info will lead to a valid configuration of the kernel.
115- configure: configure the kernel, its window, accessor, valid region, etc for the given set of tensors and other parameters.
116- run: execute the kernel in the window
117
118The structure of the kernel .cpp file should be similar to the next ones.
119For OpenCL:
120@snippet src/core/CL/kernels/CLReshapeLayerKernel.cpp CLReshapeLayerKernel Kernel
121The run will call the function defined in the .cl file.
122
123For the NEON backend case:
124@snippet src/core/NEON/kernels/NEReshapeLayerKernel.cpp NEReshapeLayerKernel Kernel
125
126In the NEON case, there is no need to add an extra file and we implement the kernel in the same NEReshapeLayerKernel.cpp file.
127If the tests are already in place, the new kernel can be tested using the existing tests by adding the configure and run of the kernel to the compute_target() in the fixture.
128
129
130@subsection S4_1_3_add_function Add a function
131
132%Memory management and scheduling the underlying kernel(s) must be handled by the function implementation. A kernel class must support window() API which return the execute window for the configuration that the kernel is configured for. A window specifies the dimensions of a workload. It has a start and end on each of the dimension. A maximum of Coordinates::num_max_dimensions is supported. The run time layer is expected to query the kernel for the window size and schedule the window as it sees fit. It could choose to split the window into sub windows so that it could be run in parallel. The split must adhere to the following rules
133
134- max[n].start() <= sub[n].start() < max[n].end()
135- sub[n].start() < sub[n].end() <= max[n].end()
136- max[n].step() == sub[n].step()
137- (sub[n].start() - max[n].start()) % max[n].step() == 0
138- (sub[n].end() - sub[n].start()) % max[n].step() == 0
139
140@ref CPPScheduler::schedule provides a sample implementation that is used for NEON kernels.
Michele Di Giorgio57f30a92020-09-08 14:03:51 +0100141%Memory management is the other aspect that the runtime layer is supposed to handle. %Memory management of the tensors is abstracted using TensorAllocator. Each tensor holds a pointer to a TensorAllocator object, which is used to allocate and free the memory at runtime. The implementation that is currently supported in Compute Library allows memory blocks, required to be fulfilled for a given operator, to be grouped together under a @ref MemoryGroup. Each group can be acquired and released. The underlying implementation of memory groups vary depending on whether NEON or CL is used. The memory group class uses memory pool to provide the required memory. It also uses the memory manager to manage the lifetime and a IPoolManager to manage the memory pools registered with the memory manager.
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000142
143
144We have seen the various interfaces for a kernel in the core library, the same structure the same file structure design exists in the runtime module. IFunction is the base class for all the functions, it has two child interfaces: ICLSimpleFunction and INESimpleFunction that are used as base class for functions which call a single kernel.
145
146The new operator has to implement %validate(), configure() and run(), these methods will call the respective function in the kernel considering that the multi-threading is used for the kernels which are parallelizable, by default std::thread::hardware_concurrency() threads are used. For NEON function can be used CPPScheduler::set_num_threads() to manually set the number of threads, whereas for OpenCL kernels all the kernels are enqueued on the queue associated with CLScheduler and the queue is then flushed.
147For the runtime functions, there is an extra method implemented: prepare(), this method prepares the function for the run, it does all the heavy operations that are done only once (reshape the weight, release the memory not necessary after the reshape, etc). The prepare method can be called standalone or in the first run, if not called before, after then the function will be marked as prepared.
148The files we add are:
149
150OpenCL function
151- arm_compute/runtime/CL/functions/CLReshapeLayer.h
152- src/runtime/CL/functions/CLReshapeLayer.cpp
153
154Neon function
155- arm_compute/runtime/NEON/functions/NEReshapeLayer.h
156- src/runtime/NEON/functions/NEReshapeLayer.cpp
157
158As we did in the kernel we have to edit the runtime libraries to register the new operator modifying the relative library file:
159- arm_compute/runtime/CL/CLFunctions.h
160- arm_compute/runtime/NEON/NEFunctions.h
161
162For the special case where the new function calls only one kernel, we could use as base class ICLSimpleFunction or INESimpleFunction. The configure and the validate methods will simply call the corresponding functions. The structure will be:
163@snippet src/runtime/CL/functions/CLReshapeLayer.cpp CLReshapeLayer snippet
164
165
166If the function is more complicated and calls more than one kernel we have to use the memory manager to manage the intermediate tensors; in the configure() method we call the manage() function passing the tensor to keep track, in the run method we will have to acquire all the buffer managed and released at the end.
167For OpenCL if we want to add two tensor input and reshape the result:
168
169@code{.cpp}
170using namespace arm_compute;
171
172CLAddReshapeLayer:: CLAddReshapeLayer(std::shared_ptr<IMemoryManager> memory_manager)
173 : _memory_group(std::move(memory_manager))
174{
175}
176
177void CLAddReshapeLayer::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output)
178{
179 // Allocate memory
180 TensorInfo info();
181 add_output.allocator()->init(info);
182
183 // Manage intermediate buffers
184 memory_group.manage(&_addOutput);
185
186 // Initialise kernel
187 _add_kernel.configure(input1, input2, &add_output);
188 _reshape_kernel.configure(&add_output, output);
189
190 // Allocate intermediate tensors
191 add_output.allocator()->allocate();
192}
193
194Status CLAddReshapeLayer::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output)
195{
196 TensorInfo add_output();
197 ARM_COMPUTE_RETURN_ERROR_ON(CLAddLayerKernel::validate(input1, input2, add_output));
198 ARM_COMPUTE_RETURN_ERROR_ON(CLReshapeLayerKernel::validate(add_output, output));
199 return Status{};
200}
201
202void CLAddReshapeLayer::run()
203{
204 memory_group.acquire();
205
206 // Run Add
207 add_kernel.run();
208
209 // Run Reshape
210 CLScheduler::get().enqueue(reshape_kernel);
211
212 memory_group.release();
213}
214
215@endcode
216
217For NEON:
218
219@code{.cpp}
220using namespace arm_compute;
221
222NEAddReshapeLayer:: NEAddReshapeLayer (std::shared_ptr<IMemoryManager> memory_manager)
223 : _memory_group(std::move(memory_manager))
224{
225}
226
227void NEAddReshapeLayer::configure(const ITensor *input1, const ITensor *input2, ITensor *output)
228{
229 // Allocate memory
230 TensorInfo info();
231 add_output.allocator()->init(info);
232
233 // Manage intermediate buffers
234 memory_group.manage(&_addOutput);
235
236 // Initialise kernel
237 add_kernel.configure(input1, input2, &addOutput);
238 reshape_kernel.configure(&addOutput, output);
239
240 // Allocate intermediate tensors
241 add_output.allocator()->allocate();
242}
243
244void NEAddReshapeLayer::run()
245{
246 memory_group.acquire();
247
248 // Run Add
249 add_kernel.run();
250
251 // Run Reshape
252 NEScheduler::get().schedule(_reshape_kernel.get(), Window::DimY);
253
254 memory_group.release();
255}
256@endcode
257
258
259At this point, everything is in place at the library level. If you are following an tests driven implementation and all the tests are already in place, we can call the function configuration in the fixture and remove any redundant code like the allocation of the intermediate tensors since it's done in the function. Run the final tests to check the results match with the expected results from the reference implementation.
260
261@subsection S4_1_4_add_validation Add validation artifacts
262
263@subsubsection S4_1_4_1_add_reference Add the reference implementation and the tests
264As mentioned in the introduction, the reference implementation is a pure C++ implementation without any optimization or backend specific instruction.
265The refence implementation consist of two files into the folder tests/validation/reference:
266- tests/validation/reference/ReshapeLayer.h
267- tests/validation/reference/ReshapeLayer.cpp
268
269where we will put respectively the declaration and definition of the new operator.
270All the utility functions that are used ONLY in the tests are in test/validation/helpers.h, for all the others, as mentioned before, there are helpers in the library.
Michele Di Giorgio57f30a92020-09-08 14:03:51 +0100271Compute Library and the tests do use templates, the reference implementation is a generic implementation independent from the datatype and we use the templates to generalize the datatype concept.
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000272Following the example, let's have a look at the ReshapeLayer operator:
273
274- tests/validation/reference/ReshapeLayer.h
275
276@snippet tests/validation/reference/ReshapeLayer.h ReshapeLayer
277
278- tests/validation/reference/ReshapeLayer.cpp
279
280@snippet tests/validation/reference/ReshapeLayer.cpp ReshapeLayer
281
282An explicit instantiation of the template for the required datatypes must be added in the .cpp file.
283
284@subsubsection S4_1_4_2_add_dataset Add dataset
285One of the parameters of the tests is the dataset, it will be used to generate versions of the test case with different inputs.
286To pass the dataset at the fixture data test case we have three cases
287- the operator dataset is simple so it can be added directly in the test case data declaration
288- we can create a class that return tuples at the test framework
289
290@snippet tests/datasets/PoolingTypesDataset.h PoolingTypes datasets
291
292- if we want to create dynamically the dataset combining different parameter, we can create the dataset using iterators.
293For example, dataset for ReshapeLayer:
294
295@snippet tests/datasets/ReshapeLayerDataset.h ReshapeLayer datasets
296
297@subsubsection S4_1_4_3_add_fixture Add a fixture and a data test case
298
299Benchmark and validation tests are based on the same framework to setup and run the tests. In addition to running simple, self-contained test functions the framework supports fixtures and data test cases.
300Fixtures can be used to share common setup, teardown or even run tasks among multiple test cases, for that purpose a fixture can define a "setup", "teardown" and "run" method.
301Adding tests for the new operator in the runtime library we need to implement at least the setup method, that is used to call two methods for configure, run and return the output respectively of the target (CL or Neon) and the reference (C++ implementation).
302
303For example let's have a look at Reshape Layer Fixture :
304
305@snippet tests/validation/fixtures/ReshapeLayerFixture.h ReshapeLayer fixture
306
307In the fixture class above we can see that the setup method computes the target and reference and store them in the two members _target and _reference which will be used later to check for correctness.
308The compute_target method reflects the exact behavior expected when we call a function. The input and output tensor must be declared, function configured, tensors allocated, the input tensor filled with required data, and finally, the function must be run and the results returned.
309This fixture is used in the test case, that is a parameterized test case that inherits from a fixture. The test case will have access to all public and protected members of the fixture. Only the setup and teardown methods of the fixture will be used. The setup method of the fixture needs to be a template and must accept inputs from the dataset as arguments.
310The body of this function will be used as a test function.
311For the fixture test case the first argument is the name of the test case (has to be unique within the enclosing test suite), the second argument is the class name of the fixture, the third argument is the dataset mode in which the test will be active (PRECOMMIT or NIGTHLY) and the fourth argument is the dataset.
312For example:
313
314@snippet tests/validation/CL/ActivationLayer.cpp CLActivationLayerFixture snippet
315
316@code{.cpp}
317TEST_SUITE(CL)
318TEST_SUITE(ActivationLayer)
319TEST_SUITE(Float)
320TEST_SUITE(FP16)
321@endcode
322@snippet tests/validation/CL/ActivationLayer.cpp CLActivationLayer Test snippet
323@code{.cpp}
324TEST_SUITE_END()
325TEST_SUITE_END()
326TEST_SUITE_END()
327TEST_SUITE_END()
328@endcode
329
330This will produce a set of tests that can be filtered with "CL/ReshapeLayer/Float/FP16/RunSmall". Each test produced from the cartesian product of the dataset is associated to a number and can be filtered specifying all the parameters.
331*/
332} // namespace arm_compute