1*89c4ff92SAndroid Build Coastguard Worker# Arm NN Build Tool 2*89c4ff92SAndroid Build Coastguard WorkerThe **_Arm NN Build Tool_** provides a collection of **parameterized bash scripts** accompanied by a **Dockerfile** for building Arm NN and its dependencies, including **[Arm Compute Library (ACL)](https://github.com/ARM-software/ComputeLibrary/)**. 3*89c4ff92SAndroid Build Coastguard WorkerThis tool replaces/supersedes the majority of the existing Arm NN build guides as a user-friendly way to build Arm NN from scratch. 4*89c4ff92SAndroid Build Coastguard WorkerThe main benefit of building Arm NN from scratch is the ability to **exactly choose which components to build, targeted for your ML project**.<br> 5*89c4ff92SAndroid Build Coastguard WorkerThe Arm NN Build Tool is tested on **x86_64 (Intel) and aarch64 (Arm) build hosts** for the **Ubuntu** platform. 6*89c4ff92SAndroid Build Coastguard WorkerOther host platforms such as Windows and Mac **should** work (with Docker installed), but have not been officially tested.<br> 7*89c4ff92SAndroid Build Coastguard WorkerAt present, the tool supports **targeting Linux devices (from Ubuntu 18.04 onwards) on x86_64 and aarch64** architectures.<br> 8*89c4ff92SAndroid Build Coastguard WorkerWe recommend using the Arm NN Build Tool through the use of Docker. However, the scripts may be [executed directly on your machine](#build-arm-nn-without-docker) if desired. 9*89c4ff92SAndroid Build Coastguard Worker 10*89c4ff92SAndroid Build Coastguard Worker**If you already have Docker installed** and want to quickly build the Arm NN Dockerfile with some default build arguments, please follow the [Docker Build Steps](#docker-build-steps). 11*89c4ff92SAndroid Build Coastguard Worker 12*89c4ff92SAndroid Build Coastguard Worker* [Overview](#overview) 13*89c4ff92SAndroid Build Coastguard Worker* [System Requirements](#system-requirements) 14*89c4ff92SAndroid Build Coastguard Worker* [Install Docker](#install-docker) 15*89c4ff92SAndroid Build Coastguard Worker * [Ubuntu / Linux](#ubuntu--linux) 16*89c4ff92SAndroid Build Coastguard Worker * [Windows and Mac](#windows-and-mac) 17*89c4ff92SAndroid Build Coastguard Worker* [Docker Build Arguments Reference](#docker-build-arguments-reference) 18*89c4ff92SAndroid Build Coastguard Worker * [SETUP_ARGS](#setup_args) 19*89c4ff92SAndroid Build Coastguard Worker * [BUILD_ARGS](#build_args) 20*89c4ff92SAndroid Build Coastguard Worker* [Docker Build Steps](#docker-build-steps) 21*89c4ff92SAndroid Build Coastguard Worker* [Build Arm NN Without Docker](#build-arm-nn-without-docker) 22*89c4ff92SAndroid Build Coastguard Worker* [Advanced Usage](#advanced-usage) 23*89c4ff92SAndroid Build Coastguard Worker * [Use custom Arm NN and ACL repository versions during build](#use-custom-arm-nn-and-acl-repository-versions-during-build) 24*89c4ff92SAndroid Build Coastguard Worker * [Additional Docker Build Arguments](#additional-docker-build-arguments) 25*89c4ff92SAndroid Build Coastguard Worker * [Inspect the contents of the Docker Image](#inspect-the-contents-of-the-docker-image) 26*89c4ff92SAndroid Build Coastguard Worker * [Bind Mounts](#bind-mounts) 27*89c4ff92SAndroid Build Coastguard Worker 28*89c4ff92SAndroid Build Coastguard Worker## Overview 29*89c4ff92SAndroid Build Coastguard WorkerThe Arm NN Build Tool consists of an official Arm NN **Dockerfile** with associated **scripts** for building Arm NN: 30*89c4ff92SAndroid Build Coastguard Worker1. **```install-packages.sh```:** builds CMake from source and installs system-wide **apt packages** 31*89c4ff92SAndroid Build Coastguard Worker2. **```setup-armnn.sh```:** downloads and builds Arm NN **dependencies** (e.g. Flatbuffers, TF Lite, Protobuf) 32*89c4ff92SAndroid Build Coastguard Worker3. **```build-armnn.sh```:** builds Arm NN and ACL 33*89c4ff92SAndroid Build Coastguard Worker4. **```common.sh```:** common variables and functions used by ```setup-armnn.sh``` and ```build-armnn.sh``` 34*89c4ff92SAndroid Build Coastguard Worker5. **```validation.sh```:** validation of common command-line arguments to ```setup-armnn.sh``` and ```build-armnn.sh``` 35*89c4ff92SAndroid Build Coastguard Worker6. **```docker-copy-to-host```:** helpful script to copy contents of a Docker Image to the host machine 36*89c4ff92SAndroid Build Coastguard Worker 37*89c4ff92SAndroid Build Coastguard WorkerA **Dockerfile** is a text document which outlines the instructions for building a **Docker Image**. Running ```docker build``` 38*89c4ff92SAndroid Build Coastguard Workeron the Arm NN Dockerfile will result in a Docker Image that contains: 39*89c4ff92SAndroid Build Coastguard Worker1. The Ubuntu operating system 40*89c4ff92SAndroid Build Coastguard Worker2. Installed system-wide packages required by Arm NN 41*89c4ff92SAndroid Build Coastguard Worker3. Source code for Arm NN and its dependencies 42*89c4ff92SAndroid Build Coastguard Worker4. The build for Arm NN and its dependencies 43*89c4ff92SAndroid Build Coastguard Worker 44*89c4ff92SAndroid Build Coastguard WorkerAll scripts except ```docker-copy-to-host.sh``` are used during the ```docker build``` process. 45*89c4ff92SAndroid Build Coastguard Worker 46*89c4ff92SAndroid Build Coastguard WorkerThe ```docker-copy-to-host.sh``` script creates a temporary [**Docker Container**](https://www.docker.com/resources/what-container/) which is required to retrieve files from the 47*89c4ff92SAndroid Build Coastguard WorkerDocker Image. A Docker Container is a **runtime** version of a Docker Image. It can be interacted with through a command-line 48*89c4ff92SAndroid Build Coastguard Workerterminal, like a lightweight virtual machine (VM). The ```docker-copy-to-host.sh``` script is designed to run on the host 49*89c4ff92SAndroid Build Coastguard Workermachine after ```docker build``` to copy the Arm NN build to the host. 50*89c4ff92SAndroid Build Coastguard Worker 51*89c4ff92SAndroid Build Coastguard WorkerThe **diagram below** outlines the operation of the Arm NN **Dockerfile** and the contents of the resulting **Docker Image** after 52*89c4ff92SAndroid Build Coastguard Workerrunning ```docker build```. During ```docker build```, the provided **```SETUP_ARGS```** determine which of Arm NN's 53*89c4ff92SAndroid Build Coastguard Workerdependencies to download and build. The **```BUILD_ARGS```** Docker argument determines which components of Arm NN to build. 54*89c4ff92SAndroid Build Coastguard Worker 55*89c4ff92SAndroid Build Coastguard WorkerThe resulting Docker Image contains the Arm NN build (along with everything required to build it) in the home directory ```/home/arm-user```. 56*89c4ff92SAndroid Build Coastguard WorkerThere are two directories: **```source```** which contains the source code of Arm NN and its dependencies, and **```build```** 57*89c4ff92SAndroid Build Coastguard Workerwhich contains the Arm NN build and the builds of each dependency of Arm NN. 58*89c4ff92SAndroid Build Coastguard Worker 59*89c4ff92SAndroid Build Coastguard WorkerThe **```armnn_aarch64_build.tar.gz``` tarball** in the home directory is a compressed archive of the Arm NN build which is located at ```/home/arm-user/build/armnn```. 60*89c4ff92SAndroid Build Coastguard WorkerThe home directory also contains the **scripts** mentioned above (not pictured in the diagram), which are copied into the 61*89c4ff92SAndroid Build Coastguard WorkerDocker Image during the ```docker build``` process. 62*89c4ff92SAndroid Build Coastguard Worker 63*89c4ff92SAndroid Build Coastguard Worker<div align="center"> 64*89c4ff92SAndroid Build Coastguard Worker <img src="dockerfile_armnn.png" class="center" alt="Arm NN Dockerfile" width="800"/> 65*89c4ff92SAndroid Build Coastguard Worker</div> 66*89c4ff92SAndroid Build Coastguard Worker 67*89c4ff92SAndroid Build Coastguard Worker<br> 68*89c4ff92SAndroid Build Coastguard Worker 69*89c4ff92SAndroid Build Coastguard Worker**Benefits of using Docker:** 70*89c4ff92SAndroid Build Coastguard Worker1. Run one ```docker build``` command instead of executing multiple scripts, saving developer time 71*89c4ff92SAndroid Build Coastguard Worker2. Can easily manage multiple different build configurations at once, each in their own Docker Image 72*89c4ff92SAndroid Build Coastguard Worker3. System packages are installed in the Docker Image only, isolated from the host machine 73*89c4ff92SAndroid Build Coastguard Worker4. Build in the exact environment the Arm NN Team used during testing, ensuring consistency in builds 74*89c4ff92SAndroid Build Coastguard Worker5. Allows Arm NN to be built on a wide range of hosts, as long as they support Docker 75*89c4ff92SAndroid Build Coastguard Worker 76*89c4ff92SAndroid Build Coastguard Worker**Disadvantages of using Docker:** 77*89c4ff92SAndroid Build Coastguard Worker1. Requires the developer to install Docker on their host machine 78*89c4ff92SAndroid Build Coastguard Worker2. Requires the developer to get familiar with Docker (this guide should help) 79*89c4ff92SAndroid Build Coastguard Worker3. Possible incompatibilities/limitations if the host machine's Linux Kernel version is too different than that of the 80*89c4ff92SAndroid Build Coastguard Workerbase OS in the Docker Image. This is due to the fact that Docker Containers share the Linux Kernel of the host machine 81*89c4ff92SAndroid Build Coastguard Worker(unlike VM's which each have their own Kernel). 82*89c4ff92SAndroid Build Coastguard Worker 83*89c4ff92SAndroid Build Coastguard Worker<br> 84*89c4ff92SAndroid Build Coastguard Worker 85*89c4ff92SAndroid Build Coastguard Worker## System Requirements 86*89c4ff92SAndroid Build Coastguard WorkerThis guide will not provide the system requirements for installing Docker - please refer to the Docker documentation during installation. 87*89c4ff92SAndroid Build Coastguard Worker 88*89c4ff92SAndroid Build Coastguard WorkerThe built Docker Image has disk requirements of **less than 6GB**, depending on the Arm NN components chosen and whether 89*89c4ff92SAndroid Build Coastguard Workerthe build included a cross compile (i.e. building aarch64 on x86_64). Repeated builds may cause the total disk usage to increase 90*89c4ff92SAndroid Build Coastguard Workerbeyond 6GB. The ```docker images``` command shows the disk usage of each Docker Image. To view total disk space used by all 91*89c4ff92SAndroid Build Coastguard WorkerDocker Images and Docker Containers, use the command ```docker system df```. 92*89c4ff92SAndroid Build Coastguard Worker 93*89c4ff92SAndroid Build Coastguard WorkerThis Dockerfile was built on a **Ubuntu 18.04 host machine with Docker version 20.10.14**. We recommend using a host machine 94*89c4ff92SAndroid Build Coastguard Workerwith at least Ubuntu 18.04 and a similar Docker version, if possible. Other Linux distros such as **Debian** should also work as 95*89c4ff92SAndroid Build Coastguard Workerlong as the Docker version is similar to **20.10.14**. 96*89c4ff92SAndroid Build Coastguard Worker 97*89c4ff92SAndroid Build Coastguard WorkerThe build outputs from this Dockerfile have been tested on an **Odroid N2+ target device with Ubuntu 18.04** installed. The build outputs 98*89c4ff92SAndroid Build Coastguard Workershould also work on newer versions of Ubuntu (and Debian) but this has not been exhaustively tested. 99*89c4ff92SAndroid Build Coastguard Worker 100*89c4ff92SAndroid Build Coastguard WorkerTo view the system packages that are installed during ```docker build```, please refer to the ```install-packages.sh``` script. 101*89c4ff92SAndroid Build Coastguard Worker 102*89c4ff92SAndroid Build Coastguard Worker<br> 103*89c4ff92SAndroid Build Coastguard Worker 104*89c4ff92SAndroid Build Coastguard Worker## Install Docker 105*89c4ff92SAndroid Build Coastguard WorkerBuilds of the official Arm NN Dockerfile have been tested on the **Ubuntu 18.04** host platform. 106*89c4ff92SAndroid Build Coastguard WorkerWhilst other platforms have not been tested, Docker should be able to build the Arm NN Dockerfile on other platforms such as Windows and Mac. 107*89c4ff92SAndroid Build Coastguard WorkerOn **Linux**, we recommend using **Docker Engine** which is used through the command-line interface (CLI). 108*89c4ff92SAndroid Build Coastguard WorkerFor **Windows and Mac** users, **Docker Desktop** is available which allows the use of the CLI plus a graphical interface.<br> 109*89c4ff92SAndroid Build Coastguard WorkerThis guide will **only** provide steps on using Docker through the use of the CLI and not Docker Desktop. 110*89c4ff92SAndroid Build Coastguard Worker 111*89c4ff92SAndroid Build Coastguard Worker### Ubuntu / Linux 112*89c4ff92SAndroid Build Coastguard WorkerFor Ubuntu users, we recommend installing **Docker Engine** with the following steps from the Docker documentation:<br> 113*89c4ff92SAndroid Build Coastguard Worker1. [Uninstall any old versions of Docker](https://docs.docker.com/engine/install/ubuntu/#uninstall-old-versions) 114*89c4ff92SAndroid Build Coastguard Worker2. [Install using the repository](https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository) 115*89c4ff92SAndroid Build Coastguard Worker3. [Add user to Docker Unix Group](https://docs.docker.com/engine/install/linux-postinstall/#manage-docker-as-a-non-root-user) (**recommended**: allows running of Docker commands **without ```sudo```**) 116*89c4ff92SAndroid Build Coastguard Worker 117*89c4ff92SAndroid Build Coastguard WorkerSimilar steps for installing Docker Engine on other Linux distributions can be found by clicking on your distro under the Server table on [this page](https://docs.docker.com/engine/install/#server). 118*89c4ff92SAndroid Build Coastguard Worker 119*89c4ff92SAndroid Build Coastguard Worker### Windows and Mac 120*89c4ff92SAndroid Build Coastguard WorkerWe do not officially test the building of the Arm NN Dockerfile on Windows or Mac. However, it's likely that it will work if your system supports Docker. 121*89c4ff92SAndroid Build Coastguard WorkerFor **Windows**, use the Docker Desktop download link on [this page](https://docs.docker.com/desktop/install/windows-install/). 122*89c4ff92SAndroid Build Coastguard WorkerFor **Mac** users, please use the Docker Desktop download link on [this page](https://docs.docker.com/desktop/install/mac-install/). <br> 123*89c4ff92SAndroid Build Coastguard Worker**Note:** we unfortunately cannot provide support for the installation of Docker and/or the use of the Arm NN Dockerfile on Windows and Mac platforms. 124*89c4ff92SAndroid Build Coastguard Worker 125*89c4ff92SAndroid Build Coastguard Worker<br> 126*89c4ff92SAndroid Build Coastguard Worker 127*89c4ff92SAndroid Build Coastguard Worker## Docker Build Arguments Reference 128*89c4ff92SAndroid Build Coastguard WorkerThe following describes the ```docker build``` arguments that can be used to customize the Arm NN build. 129*89c4ff92SAndroid Build Coastguard WorkerDocker build arguments such as ```SETUP_ARGS``` and ```BUILD_ARGS``` are provided in the ```docker build``` as a ```--build-arg```. 130*89c4ff92SAndroid Build Coastguard Worker 131*89c4ff92SAndroid Build Coastguard Worker### SETUP_ARGS 132*89c4ff92SAndroid Build Coastguard WorkerThese arguments are eventually passed to ```setup-armnn.sh``` which downloads and builds Arm NN dependencies. 133*89c4ff92SAndroid Build Coastguard WorkerFor ease of use (but longer initial docker build), use ```--all``` to have all Arm NN dependencies available for use during Arm NN builds. 134*89c4ff92SAndroid Build Coastguard WorkerRepeated docker builds with the same ```SETUP_ARGS``` will skip the setup process (using [caching](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#leverage-build-cache) of previous Docker build steps). 135*89c4ff92SAndroid Build Coastguard WorkerThe ```SETUP_ARGS``` string should start and end with **double quotes** ```"```. 136*89c4ff92SAndroid Build Coastguard Worker 137*89c4ff92SAndroid Build Coastguard Worker| SETUP_ARGS | Description | 138*89c4ff92SAndroid Build Coastguard Worker|-------------------|:---------------------------------------------------------------------------------| 139*89c4ff92SAndroid Build Coastguard Worker| --tflite-delegate | **flag:** setup dependencies for the Arm NN TF Lite Delegate | 140*89c4ff92SAndroid Build Coastguard Worker| --tflite-parser | **flag:** setup dependencies for the Arm NN TF Lite Parser | 141*89c4ff92SAndroid Build Coastguard Worker| --onnx-parser | **flag:** setup dependencies for the Arm NN ONNX parser | 142*89c4ff92SAndroid Build Coastguard Worker| --all | **flag:** setup dependencies for all Arm NN components listed above | 143*89c4ff92SAndroid Build Coastguard Worker| --target-arch= | **mandatory option:** specify a target architecture ```aarch64, x86_64``` | 144*89c4ff92SAndroid Build Coastguard Worker 145*89c4ff92SAndroid Build Coastguard Worker**At least one component** (e.g. ```--tflite-delegate```) must be provided **or** else provide ```--all``` to setup dependencies for all components. 146*89c4ff92SAndroid Build Coastguard Worker 147*89c4ff92SAndroid Build Coastguard Worker**Examples:**<br> 148*89c4ff92SAndroid Build Coastguard WorkerSetup for aarch64 with all Arm NN dependencies:<br> 149*89c4ff92SAndroid Build Coastguard Worker```SETUP_ARGS="--target-arch=aarch64 --all"``` 150*89c4ff92SAndroid Build Coastguard Worker 151*89c4ff92SAndroid Build Coastguard WorkerSetup for aarch64 with TF Lite Delegate and TF Lite Parser dependencies only:<br> 152*89c4ff92SAndroid Build Coastguard Worker```SETUP_ARGS="--target-arch=aarch64 --tflite-delegate --tflite-parser"``` 153*89c4ff92SAndroid Build Coastguard Worker 154*89c4ff92SAndroid Build Coastguard Worker<br> 155*89c4ff92SAndroid Build Coastguard Worker 156*89c4ff92SAndroid Build Coastguard Worker### BUILD_ARGS 157*89c4ff92SAndroid Build Coastguard WorkerThe following arguments are given to ```build-armnn.sh``` and define which components of Arm NN to include in the build. 158*89c4ff92SAndroid Build Coastguard WorkerThe ```BUILD_ARGS``` string should start and end with **double quotes** ```"```. 159*89c4ff92SAndroid Build Coastguard Worker 160*89c4ff92SAndroid Build Coastguard Worker| BUILD_ARGS | Description | 161*89c4ff92SAndroid Build Coastguard Worker|---------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 162*89c4ff92SAndroid Build Coastguard Worker| --tflite-delegate | **flag:** build the Arm NN TF Lite Delegate component | 163*89c4ff92SAndroid Build Coastguard Worker| --tflite-parser | **flag:** build the Arm NN TF Lite Parser component | 164*89c4ff92SAndroid Build Coastguard Worker| --onnx-parser | **flag:** build the Arm NN ONNX parser component | 165*89c4ff92SAndroid Build Coastguard Worker| --all | **flag:** build all Arm NN components listed above | 166*89c4ff92SAndroid Build Coastguard Worker| --target-arch= | **mandatory option:** specify a target architecture ```aarch64, x86_64``` | 167*89c4ff92SAndroid Build Coastguard Worker| --neon-backend | **flag:** build Arm NN with the NEON backend (CPU acceleration from ACL) | 168*89c4ff92SAndroid Build Coastguard Worker| --cl-backend | **flag:** build Arm NN with the OpenCL backend (GPU acceleration from ACL) | 169*89c4ff92SAndroid Build Coastguard Worker| --ref-backend | **flag:** build Arm NN with the reference backend<br/>**Should be used for verification purposes only.<br/>Does not provide any performance acceleration.** | 170*89c4ff92SAndroid Build Coastguard Worker| --debug | **flag:** build Arm NN (and ACL) with debug turned on (optional: defaults to off) | 171*89c4ff92SAndroid Build Coastguard Worker| --armnn-cmake-args= | **option:** provide additional comma-separated CMake arguments string for building Arm NN (optional)<br/>String should start and end with **single quotes** ```'```<br/>Please refer to **armnn/cmake/GlobalConfig.cmake** | 172*89c4ff92SAndroid Build Coastguard Worker| --acl-scons-params= | **option**: provide additional comma-separated scons parameters string for building ACL (optional)<br/>String should start and end with **single quotes** ```'```<br/>ACL provide [documentation](https://arm-software.github.io/ComputeLibrary/latest/how_to_build.xhtml#S1_1_build_options) for their build options | 173*89c4ff92SAndroid Build Coastguard Worker 174*89c4ff92SAndroid Build Coastguard Worker 175*89c4ff92SAndroid Build Coastguard Worker**At least one component** (i.e. ```--tflite-delegate```, ```--tflite-parser```, ```--onnx-parser```) must be provided or else provide ```--all``` to build all Arm NN components.<br> 176*89c4ff92SAndroid Build Coastguard Worker**A component given in ```BUILD_ARGS``` must also have been given in ```SETUP_ARGS``` previously**, or else Arm NN will fail to build.<br> 177*89c4ff92SAndroid Build Coastguard Worker**At least one backend** (i.e. ```--neon-backend```, ```--cl-backend```, ```--ref-backend```) must be chosen.<br> 178*89c4ff92SAndroid Build Coastguard Worker 179*89c4ff92SAndroid Build Coastguard Worker**Examples:**<br> 180*89c4ff92SAndroid Build Coastguard WorkerBuild for aarch64 with all Arm NN components, NEON enabled and OpenCL enabled:<br> 181*89c4ff92SAndroid Build Coastguard Worker```BUILD_ARGS="--target-arch=aarch64 --all --neon-backend --cl-backend"``` 182*89c4ff92SAndroid Build Coastguard Worker 183*89c4ff92SAndroid Build Coastguard WorkerBuild for aarch64 with TF Lite Delegate, OpenCL enabled and additional ACL scons params:<br> 184*89c4ff92SAndroid Build Coastguard Worker```BUILD_ARGS="--target-arch=aarch64 --tflite-delegate --cl-backend --acl-scons-params='compress_kernels=1,benchmark_examples=1'"``` 185*89c4ff92SAndroid Build Coastguard Worker 186*89c4ff92SAndroid Build Coastguard WorkerSetup for aarch64 with all Arm NN dependencies, OpenCL enabled and additional Arm NN cmake args:<br> 187*89c4ff92SAndroid Build Coastguard Worker```BUILD_ARGS="--target-arch=aarch64 --all --cl-backend --armnn-cmake-args='-DBUILD_SAMPLE_APP=1,-DBUILD_UNIT_TESTS=0'"``` 188*89c4ff92SAndroid Build Coastguard Worker 189*89c4ff92SAndroid Build Coastguard Worker**Example _valid_ combination of SETUP_ARGS and BUILD_ARGS:**<br> 190*89c4ff92SAndroid Build Coastguard Worker``` 191*89c4ff92SAndroid Build Coastguard WorkerSETUP_ARGS="--target-arch=aarch64 --all" 192*89c4ff92SAndroid Build Coastguard WorkerBUILD_ARGS="--target-arch=aarch64 --tflite-delegate --neon-backend --cl-backend" 193*89c4ff92SAndroid Build Coastguard Worker``` 194*89c4ff92SAndroid Build Coastguard Worker 195*89c4ff92SAndroid Build Coastguard Worker**Example _invalid_ combination of SETUP_ARGS and BUILD_ARGS:**<br> 196*89c4ff92SAndroid Build Coastguard Worker``` 197*89c4ff92SAndroid Build Coastguard WorkerSETUP_ARGS="--target-arch=aarch64 --tflite-delegate" 198*89c4ff92SAndroid Build Coastguard WorkerBUILD_ARGS="--target-arch=aarch64 --all --neon-backend --cl-backend" 199*89c4ff92SAndroid Build Coastguard Worker``` 200*89c4ff92SAndroid Build Coastguard Worker 201*89c4ff92SAndroid Build Coastguard WorkerThe example above is invalid since it tries to build all Arm NN components after **only** building the dependencies required by the TF Lite Delegate. 202*89c4ff92SAndroid Build Coastguard Worker 203*89c4ff92SAndroid Build Coastguard Worker<br> 204*89c4ff92SAndroid Build Coastguard Worker 205*89c4ff92SAndroid Build Coastguard Worker## Docker Build Steps 206*89c4ff92SAndroid Build Coastguard Worker 207*89c4ff92SAndroid Build Coastguard WorkerFirst clone the Arm NN repo to obtain the build-tool. This automatically downloads the latest release version of Arm NN. 208*89c4ff92SAndroid Build Coastguard Worker``` 209*89c4ff92SAndroid Build Coastguard Workergit clone https://github.com/ARM-software/armnn.git armnn 210*89c4ff92SAndroid Build Coastguard Worker``` 211*89c4ff92SAndroid Build Coastguard Worker<br> 212*89c4ff92SAndroid Build Coastguard Worker 213*89c4ff92SAndroid Build Coastguard WorkerEnter into the Arm NN build-tool directory where the Dockerfile and associated scripts are located. 214*89c4ff92SAndroid Build Coastguard Worker``` 215*89c4ff92SAndroid Build Coastguard Workercd armnn/build-tool 216*89c4ff92SAndroid Build Coastguard Worker``` 217*89c4ff92SAndroid Build Coastguard Worker<br> 218*89c4ff92SAndroid Build Coastguard Worker 219*89c4ff92SAndroid Build Coastguard WorkerRun the ```docker build```, which downloads and builds Arm NN and its dependencies. This process is isolated from the host machine's filesystem and the result is a Docker Image being created.<br> 220*89c4ff92SAndroid Build Coastguard WorkerDockerfile arguments are provided using ```--build-arg```, the relative path to the Dockerfile is given with ```--file``` and the current directory is given with ```.```<br> 221*89c4ff92SAndroid Build Coastguard WorkerProvide a descriptive name for the Image with ```--tag``` in the form of ```image_name:tag``` (example given below). The backslashes ```\``` tell Bash to expect a continuation of the command on the next line. 222*89c4ff92SAndroid Build Coastguard Worker 223*89c4ff92SAndroid Build Coastguard WorkerThis example's choice of **[SETUP_ARGS](#setup_args)** and **[BUILD_ARGS](#build_args)** builds all Arm NN components with NEON and OpenCL accelerated backends for aarch64 target. 224*89c4ff92SAndroid Build Coastguard WorkerThis process should take **less than an hour** with a modern machine but will vary depending on the arguments chosen and host machine specification. 225*89c4ff92SAndroid Build Coastguard Worker 226*89c4ff92SAndroid Build Coastguard Worker``` 227*89c4ff92SAndroid Build Coastguard Workerdocker build \ 228*89c4ff92SAndroid Build Coastguard Worker--build-arg SETUP_ARGS="--target-arch=aarch64 --all" \ 229*89c4ff92SAndroid Build Coastguard Worker--build-arg BUILD_ARGS="--target-arch=aarch64 --all --neon-backend --cl-backend" \ 230*89c4ff92SAndroid Build Coastguard Worker--tag armnn:aarch64 \ 231*89c4ff92SAndroid Build Coastguard Worker--file docker/Dockerfile . 232*89c4ff92SAndroid Build Coastguard Worker``` 233*89c4ff92SAndroid Build Coastguard Worker 234*89c4ff92SAndroid Build Coastguard Worker<br> 235*89c4ff92SAndroid Build Coastguard Worker 236*89c4ff92SAndroid Build Coastguard WorkerArm NN and all of its dependencies are now built within a Docker Image. Built Docker images can be listed with ```docker images```. 237*89c4ff92SAndroid Build Coastguard Worker``` 238*89c4ff92SAndroid Build Coastguard Workerdocker images 239*89c4ff92SAndroid Build Coastguard Worker 240*89c4ff92SAndroid Build Coastguard WorkerREPOSITORY TAG IMAGE ID CREATED SIZE 241*89c4ff92SAndroid Build Coastguard Workerarmnn aarch64 cc623174fd98 7 seconds ago 5.02GB 242*89c4ff92SAndroid Build Coastguard Worker``` 243*89c4ff92SAndroid Build Coastguard Worker 244*89c4ff92SAndroid Build Coastguard Worker<br> 245*89c4ff92SAndroid Build Coastguard Worker 246*89c4ff92SAndroid Build Coastguard WorkerA tarball archive of the Arm NN build is located inside the Docker home directory (```/home/arm-user/```), named ```armnn_<target-arch>_build.tar.gz```. 247*89c4ff92SAndroid Build Coastguard WorkerIf the ```--debug``` build flag is provided in the ```BUILD_ARGS``` above, the tarball will be named ```armnn_<target-arch>_build_debug.tar.gz```.<br> 248*89c4ff92SAndroid Build Coastguard WorkerThe ```docker-copy-to-host.sh``` script will copy a file from the Docker Image (in arm-user's home directory) to the host machine.<br> 249*89c4ff92SAndroid Build Coastguard WorkerThe script copies the tarball into a new directory on the host in ```build-tool/docker_output```. It takes two arguments: ```image_name:tag``` and ```filename```.<br> 250*89c4ff92SAndroid Build Coastguard WorkerThe filename is a relative path from the home directory created inside the Docker Image (```/home/arm-user/```). 251*89c4ff92SAndroid Build Coastguard Worker``` 252*89c4ff92SAndroid Build Coastguard Worker./scripts/docker-copy-to-host.sh armnn:aarch64 armnn_aarch64_build.tar.gz 253*89c4ff92SAndroid Build Coastguard Worker``` 254*89c4ff92SAndroid Build Coastguard Worker 255*89c4ff92SAndroid Build Coastguard Worker<br> 256*89c4ff92SAndroid Build Coastguard Worker 257*89c4ff92SAndroid Build Coastguard WorkerThis tarball can now be used for integration in an ML application. The method of extraction is given below.<br> 258*89c4ff92SAndroid Build Coastguard WorkerIf the ```--target-arch``` chosen in the arguments above matches the host machine, the build can be tested locally (else copy the tarball to a remote device). 259*89c4ff92SAndroid Build Coastguard Worker``` 260*89c4ff92SAndroid Build Coastguard Workercd docker_output 261*89c4ff92SAndroid Build Coastguard Worker 262*89c4ff92SAndroid Build Coastguard Worker# Extract the tarball into a directory called <target_arch>_build 263*89c4ff92SAndroid Build Coastguard Worker# If --debug is enabled, the extracted build directory will be called <target_arch>_build_debug 264*89c4ff92SAndroid Build Coastguard Workertar -xzf armnn_aarch64_build.tar.gz 265*89c4ff92SAndroid Build Coastguard Workercd aarch64_build 266*89c4ff92SAndroid Build Coastguard Worker 267*89c4ff92SAndroid Build Coastguard Worker# Set LD_LIBRARY_PATH to the current aarch64_build directory (.) 268*89c4ff92SAndroid Build Coastguard Workerexport LD_LIBRARY_PATH=.; ./UnitTests 269*89c4ff92SAndroid Build Coastguard Worker 270*89c4ff92SAndroid Build Coastguard Worker# If the Arm NN TF Lite Delegate is built, we can also run DelegateUnitTests 271*89c4ff92SAndroid Build Coastguard Workercd delegate 272*89c4ff92SAndroid Build Coastguard Worker 273*89c4ff92SAndroid Build Coastguard Worker# Set LD_LIBRARY_PATH to the current delegate directory (.) and the aarch64_build directory (..) 274*89c4ff92SAndroid Build Coastguard Workerexport LD_LIBRARY_PATH=.:..; ./DelegateUnitTests 275*89c4ff92SAndroid Build Coastguard Worker``` 276*89c4ff92SAndroid Build Coastguard Worker 277*89c4ff92SAndroid Build Coastguard Worker<br> 278*89c4ff92SAndroid Build Coastguard Worker 279*89c4ff92SAndroid Build Coastguard Worker**Note:** Repeated Docker builds may result in an accumulation of unwanted, dangling images. **To remove dangling images** (good practice), run the following command: ```docker image prune``` 280*89c4ff92SAndroid Build Coastguard Worker 281*89c4ff92SAndroid Build Coastguard Worker<br> 282*89c4ff92SAndroid Build Coastguard Worker 283*89c4ff92SAndroid Build Coastguard Worker## Build Arm NN without Docker 284*89c4ff92SAndroid Build Coastguard WorkerFor ease of use, we recommend that the official Arm NN Dockerfile is used to build Arm NN. 285*89c4ff92SAndroid Build Coastguard WorkerIf you'd like to build locally on a Ubuntu host machine without Docker, execute the build-tool scripts directly instead. 286*89c4ff92SAndroid Build Coastguard WorkerThis involves running ```install-packages.sh``` followed by ```setup-armnn.sh``` and ```build-armnn.sh```. 287*89c4ff92SAndroid Build Coastguard WorkerThe arguments for ```setup-armnn.sh``` and ```build-armnn.sh``` are very much the same as ```SETUP_ARGS``` and ```BUILD_ARGS```. 288*89c4ff92SAndroid Build Coastguard WorkerPlease use ```--help``` for more specific information. 289*89c4ff92SAndroid Build Coastguard Worker 290*89c4ff92SAndroid Build Coastguard Worker``` 291*89c4ff92SAndroid Build Coastguard Workercd build-tool/scripts 292*89c4ff92SAndroid Build Coastguard Worker 293*89c4ff92SAndroid Build Coastguard Workersudo ./install-packages.sh 294*89c4ff92SAndroid Build Coastguard Worker 295*89c4ff92SAndroid Build Coastguard Worker./setup-armnn.sh --target-arch=aarch64 --all 296*89c4ff92SAndroid Build Coastguard Worker./build-armnn.sh --target-arch=aarch64 --all --neon-backend --cl-backend 297*89c4ff92SAndroid Build Coastguard Worker 298*89c4ff92SAndroid Build Coastguard Worker./setup-armnn.sh --help 299*89c4ff92SAndroid Build Coastguard Worker./build-armnn.sh --help 300*89c4ff92SAndroid Build Coastguard Worker``` 301*89c4ff92SAndroid Build Coastguard Worker 302*89c4ff92SAndroid Build Coastguard Worker<br> 303*89c4ff92SAndroid Build Coastguard Worker 304*89c4ff92SAndroid Build Coastguard Worker## Advanced Usage 305*89c4ff92SAndroid Build Coastguard Worker 306*89c4ff92SAndroid Build Coastguard Worker### Use custom Arm NN and ACL repository versions during build 307*89c4ff92SAndroid Build Coastguard WorkerBy default, the docker build process (specifically, during ```build-armnn.sh```) will download the latest release versions of Arm NN and ACL. 308*89c4ff92SAndroid Build Coastguard WorkerIf you'd like to use different versions during the build, check them out in the ```build-tool``` directory on the host.<br> 309*89c4ff92SAndroid Build Coastguard WorkerWhen providing custom repositories, the following ```docker build``` argument must be provided ```--build-arg BUILD_TYPE=dev```. 310*89c4ff92SAndroid Build Coastguard WorkerThis will trigger Docker to copy the custom repos into the Docker Image during build. The ACL repo is only required if 311*89c4ff92SAndroid Build Coastguard Workersupplying the ```--neon-backend``` or ```--cl-backend``` BUILD_ARGS options. 312*89c4ff92SAndroid Build Coastguard Worker 313*89c4ff92SAndroid Build Coastguard Worker**Note:** the Arm NN version used for build-tool (Dockerfile and scripts) is not the same version of Arm NN that is used during the build. 314*89c4ff92SAndroid Build Coastguard WorkerThis means that separate versions of Arm NN can be used for the build-tool and for building Arm NN itself.<br> 315*89c4ff92SAndroid Build Coastguard WorkerIf you repeat the ```docker build``` process multiple times (e.g. with different ```BUILD_ARGS```), the setup process will be skipped as long as ```SETUP_ARGS``` remains the same. 316*89c4ff92SAndroid Build Coastguard Worker``` 317*89c4ff92SAndroid Build Coastguard Workercd build-tool 318*89c4ff92SAndroid Build Coastguard Worker 319*89c4ff92SAndroid Build Coastguard Workergit clone https://github.com/ARM-software/armnn.git armnn 320*89c4ff92SAndroid Build Coastguard Workercd armnn 321*89c4ff92SAndroid Build Coastguard Workergit checkout <branch or SHA> 322*89c4ff92SAndroid Build Coastguard Worker 323*89c4ff92SAndroid Build Coastguard Workercd .. 324*89c4ff92SAndroid Build Coastguard Worker 325*89c4ff92SAndroid Build Coastguard Worker# custom ACL repo only required when supplying --neon-backend or --cl-backend BUILD_ARGS options 326*89c4ff92SAndroid Build Coastguard Workergit clone https://github.com/ARM-software/ComputeLibrary.git acl 327*89c4ff92SAndroid Build Coastguard Workercd acl 328*89c4ff92SAndroid Build Coastguard Workergit checkout <tag or SHA> 329*89c4ff92SAndroid Build Coastguard Worker 330*89c4ff92SAndroid Build Coastguard Workercd .. 331*89c4ff92SAndroid Build Coastguard Worker 332*89c4ff92SAndroid Build Coastguard Worker# Example docker build with BUILD_TYPE=dev, ran inside the build-tool directory 333*89c4ff92SAndroid Build Coastguard Workerdocker build \ 334*89c4ff92SAndroid Build Coastguard Worker--build-arg BUILD_TYPE=dev \ 335*89c4ff92SAndroid Build Coastguard Worker--build-arg SETUP_ARGS="--target-arch=aarch64 --all" \ 336*89c4ff92SAndroid Build Coastguard Worker--build-arg BUILD_ARGS="--target-arch=aarch64 --all --neon-backend --cl-backend" \ 337*89c4ff92SAndroid Build Coastguard Worker--tag armnn:aarch64 \ 338*89c4ff92SAndroid Build Coastguard Worker--file docker/Dockerfile . 339*89c4ff92SAndroid Build Coastguard Worker``` 340*89c4ff92SAndroid Build Coastguard Worker 341*89c4ff92SAndroid Build Coastguard Worker<br> 342*89c4ff92SAndroid Build Coastguard Worker 343*89c4ff92SAndroid Build Coastguard Worker### Additional Docker Build Arguments 344*89c4ff92SAndroid Build Coastguard Worker 345*89c4ff92SAndroid Build Coastguard Worker#### UBUNTU_VERSION 346*89c4ff92SAndroid Build Coastguard WorkerThe default base Image used during ```docker build``` is ```ubuntu:18.04```. Building Arm NN with this default image should be sufficient for a wide range of target devices. 347*89c4ff92SAndroid Build Coastguard WorkerTo use a different Ubuntu base Image, provide ```UBUNTU_VERSION``` during ```docker build``` e.g. ```--build-arg UBUNTU_VERSION=20.04```. 348*89c4ff92SAndroid Build Coastguard Worker 349*89c4ff92SAndroid Build Coastguard Worker#### BUILD_TYPE 350*89c4ff92SAndroid Build Coastguard WorkerBy default, ```BUILD_TYPE``` is set to ```production```. This means that the latest release versions of Arm NN and ACL are used during builds. 351*89c4ff92SAndroid Build Coastguard WorkerTo use custom repositories as explained [above](#Use-custom-Arm-NN-and-ACL-repository-versions-during-build), provide ```--build-arg BUILD_TYPE=dev```. 352*89c4ff92SAndroid Build Coastguard Worker 353*89c4ff92SAndroid Build Coastguard Worker#### Additional SETUP_ARGS and BUILD_ARGS 354*89c4ff92SAndroid Build Coastguard WorkerThe following are additional ```SETUP_ARGS``` and ```BUILD_ARGS``` that can be provided to the ```docker build```. 355*89c4ff92SAndroid Build Coastguard Worker 356*89c4ff92SAndroid Build Coastguard Worker| SETUP_ARGS / BUILD_ARGS | Description | 357*89c4ff92SAndroid Build Coastguard Worker|-------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------| 358*89c4ff92SAndroid Build Coastguard Worker| --num-threads= | **option:** specify number of threads/cores to build Arm NN and its dependencies with (this defaults to number of online cores on the host)<br/>e.g. --num-threads=4 | 359*89c4ff92SAndroid Build Coastguard Worker| -x | **flag:** enable shell tracing during setup-armnn.sh and build-armnn.sh script execution | 360*89c4ff92SAndroid Build Coastguard Worker 361*89c4ff92SAndroid Build Coastguard Worker#### USER_ID and GROUP_ID 362*89c4ff92SAndroid Build Coastguard WorkerBy default, the Docker Image creates a user called ```arm-user``` with user/group ID of 1000. This ensures the build outputs can be used by a wide range of users on target devices.<br> 363*89c4ff92SAndroid Build Coastguard WorkerTo provide a more specific user and group ID to be associated with the build, provide ```--build-arg USER_ID=XXXX``` and ```--build-arg GROUP_ID=YYYY```. 364*89c4ff92SAndroid Build Coastguard Worker 365*89c4ff92SAndroid Build Coastguard Worker<br> 366*89c4ff92SAndroid Build Coastguard Worker 367*89c4ff92SAndroid Build Coastguard Worker### Inspect the contents of the Docker Image 368*89c4ff92SAndroid Build Coastguard WorkerTo inspect the full contents of the Docker Image, the Image must be run as a **Docker Container**. 369*89c4ff92SAndroid Build Coastguard WorkerA Container is a runtime version of the Image, like a **lightweight virtual machine** with its own filesystem. 370*89c4ff92SAndroid Build Coastguard Worker 371*89c4ff92SAndroid Build Coastguard WorkerThe following shows how to run a container with an interactive terminal based on a specified Docker Image ```image_name:tag```. 372*89c4ff92SAndroid Build Coastguard WorkerThe ID of the generated container in this example is ```4da7c575a95a```. An ```ls``` command shows the contents of the ```/home/arm-user``` directory. 373*89c4ff92SAndroid Build Coastguard Worker``` 374*89c4ff92SAndroid Build Coastguard Workerdocker run --interactive --tty armnn:aarch64 375*89c4ff92SAndroid Build Coastguard Worker 376*89c4ff92SAndroid Build Coastguard Workerarm-user@4da7c575a95a:~$ ls 377*89c4ff92SAndroid Build Coastguard Workerarmnn_aarch64_build.tar.gz build build-armnn.sh common.sh setup-armnn.sh source validation.sh 378*89c4ff92SAndroid Build Coastguard Worker``` 379*89c4ff92SAndroid Build Coastguard WorkerThe ```source``` directory contains the downloaded dependencies for building Arm NN.<br> 380*89c4ff92SAndroid Build Coastguard WorkerThe ```build``` directory contains the built Arm NN dependencies and the Arm NN build itself.<br> 381*89c4ff92SAndroid Build Coastguard WorkerThe ```armnn_aarch64_build.tar.gz``` tarball is an archive of the build located at ```build/armnn/aarch64_build```.<br> 382*89c4ff92SAndroid Build Coastguard WorkerThe scripts (e.g. ```build-armnn.sh```) in this directory can be run as you wish, changes will persist within the container but not the Docker Image. 383*89c4ff92SAndroid Build Coastguard WorkerTo exit the docker container, run ```CTRL-D```. A list of Docker containers can be obtained with the following command: 384*89c4ff92SAndroid Build Coastguard Worker``` 385*89c4ff92SAndroid Build Coastguard Worker# --all shows both running and non-running containers 386*89c4ff92SAndroid Build Coastguard Workerdocker ps --all 387*89c4ff92SAndroid Build Coastguard Worker 388*89c4ff92SAndroid Build Coastguard WorkerCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 389*89c4ff92SAndroid Build Coastguard Worker4da7c575a95a armnn:aarch64 "bash" 7 minutes ago Exited (0) 7 seconds ago pedantic_joliot 390*89c4ff92SAndroid Build Coastguard Worker``` 391*89c4ff92SAndroid Build Coastguard WorkerTo restart the same container (and any changes made to it) in a terminal again, use the following commands: 392*89c4ff92SAndroid Build Coastguard Worker``` 393*89c4ff92SAndroid Build Coastguard Worker# 4da7c575a95a in this case is the CONTAINER ID from above 394*89c4ff92SAndroid Build Coastguard Worker 395*89c4ff92SAndroid Build Coastguard Workerdocker start 4da7c575a95a # restart container in background 396*89c4ff92SAndroid Build Coastguard Workerdocker attach 4da7c575a95a # re-attach to terminal 397*89c4ff92SAndroid Build Coastguard Worker 398*89c4ff92SAndroid Build Coastguard Worker# After exiting with CTRL-D 399*89c4ff92SAndroid Build Coastguard Workerdocker stop 4da7c575a95a 400*89c4ff92SAndroid Build Coastguard Worker``` 401*89c4ff92SAndroid Build Coastguard WorkerTo save space over time, stopped Docker containers can be deleted with the following command:<br> 402*89c4ff92SAndroid Build Coastguard Worker```docker container prune```. 403*89c4ff92SAndroid Build Coastguard Worker 404*89c4ff92SAndroid Build Coastguard Worker<br> 405*89c4ff92SAndroid Build Coastguard Worker 406*89c4ff92SAndroid Build Coastguard Worker### Bind Mounts 407*89c4ff92SAndroid Build Coastguard Worker**Bind Mounts** may be used to mount directories from the host machine to inside a Docker container. This could be useful when 408*89c4ff92SAndroid Build Coastguard Workerfrequently changing the contents of the Arm NN or ACL source repositories (in ```/home/arm-user/source```) between runs of 409*89c4ff92SAndroid Build Coastguard Workerbuild-armnn.sh inside the container. Note that bind mounts are a feature that apply to runtime versions of Docker 410*89c4ff92SAndroid Build Coastguard Workerimages i.e. Docker Containers (```docker run```, not ```docker build```). 411*89c4ff92SAndroid Build Coastguard WorkerPlease refer to the [Docker documentation for more information about bind mounts](https://docs.docker.com/storage/bind-mounts/).