1*523fa7a6SAndroid Build Coastguard Worker# ExecuTorch Arm/TOSA Delegate 2*523fa7a6SAndroid Build Coastguard Worker 3*523fa7a6SAndroid Build Coastguard WorkerThis subtree contains the Arm(R) Delegate implementation for ExecuTorch. 4*523fa7a6SAndroid Build Coastguard Worker 5*523fa7a6SAndroid Build Coastguard WorkerThis delegate is structured to, over time, support a number of different Arm devices 6*523fa7a6SAndroid Build Coastguard Workerthrough an AoT flow which targets multiple Arm IP using the TOSA standard. 7*523fa7a6SAndroid Build Coastguard Worker 8*523fa7a6SAndroid Build Coastguard WorkerThe expected flow is: 9*523fa7a6SAndroid Build Coastguard Worker * torch.nn.module -> TOSA -> command_stream for fully AoT flows e.g. embedded. 10*523fa7a6SAndroid Build Coastguard Worker * torch.nn.module -> TOSA for flows supporting a JiT compilation step. 11*523fa7a6SAndroid Build Coastguard Worker 12*523fa7a6SAndroid Build Coastguard WorkerCurrent backend support is being developed for TOSA to Ethos(TM)-U55/65/85 via the 13*523fa7a6SAndroid Build Coastguard Workerethos-u-vela compilation stack. which follows the fully AoT flow. 14*523fa7a6SAndroid Build Coastguard Worker 15*523fa7a6SAndroid Build Coastguard Worker## Layout 16*523fa7a6SAndroid Build Coastguard Worker 17*523fa7a6SAndroid Build Coastguard WorkerExport: 18*523fa7a6SAndroid Build Coastguard Worker- `arm_backend.py` - Main entrypoint for the ArmPartitioner and ArmBackend. For more information see the section on 19*523fa7a6SAndroid Build Coastguard Worker[Arm Backend Architecture](#arm-backend-architecture). For examples of use see `executorch/examples/arm`. 20*523fa7a6SAndroid Build Coastguard Worker- `tosa_mapping.py` - utilities for mapping edge dialect to TOSA 21*523fa7a6SAndroid Build Coastguard Worker- `tosa_quant_utils.py` - utilities for mapping quantization information to TOSA encoding 22*523fa7a6SAndroid Build Coastguard Worker 23*523fa7a6SAndroid Build Coastguard WorkerOperators: 24*523fa7a6SAndroid Build Coastguard Worker- `node_visitor.py` - Base class for edge operator lowering 25*523fa7a6SAndroid Build Coastguard Worker- `op_*.py` - Edge operator lowering/serialization to TOSA 26*523fa7a6SAndroid Build Coastguard Worker 27*523fa7a6SAndroid Build Coastguard WorkerPasses: 28*523fa7a6SAndroid Build Coastguard Worker- `arm_pass_manager.py` - Pass manager. Will decide which passes need to be applied depending on the compile_spec. 29*523fa7a6SAndroid Build Coastguard Worker- `*_pass.py` - Compiler passes derived from ExportPass 30*523fa7a6SAndroid Build Coastguard Worker 31*523fa7a6SAndroid Build Coastguard WorkerQuantization: 32*523fa7a6SAndroid Build Coastguard Worker- `arm_quantizer.py` - Quantizer for Arm backend 33*523fa7a6SAndroid Build Coastguard Worker- `arm_quantizer_utils.py` - Utilities for quantization 34*523fa7a6SAndroid Build Coastguard Worker 35*523fa7a6SAndroid Build Coastguard WorkerRuntime: 36*523fa7a6SAndroid Build Coastguard Worker- `runtime/ArmBackendEthosU.cpp` - The Arm backend implementation of the ExecuTorch runtime backend (BackendInterface) for Ethos-U 37*523fa7a6SAndroid Build Coastguard Worker 38*523fa7a6SAndroid Build Coastguard WorkerOther: 39*523fa7a6SAndroid Build Coastguard Worker- `third-party/` - Dependencies on other code - in particular the TOSA serialization_lib for compiling to TOSA and the ethos-u-core-driver for the bare-metal backend supporting Ethos-U 40*523fa7a6SAndroid Build Coastguard Worker- `test/` - Unit test and test support functions 41*523fa7a6SAndroid Build Coastguard Worker 42*523fa7a6SAndroid Build Coastguard Worker## Unit tests 43*523fa7a6SAndroid Build Coastguard WorkerThis is the structure of the test directory 44*523fa7a6SAndroid Build Coastguard Worker 45*523fa7a6SAndroid Build Coastguard Worker``` 46*523fa7a6SAndroid Build Coastguard Workertest # Root test folder 47*523fa7a6SAndroid Build Coastguard Worker├── misc # Testing of debug features 48*523fa7a6SAndroid Build Coastguard Worker├── models # Full model tests 49*523fa7a6SAndroid Build Coastguard Worker├── ops # Single op tests 50*523fa7a6SAndroid Build Coastguard Worker├── passes # Compiler passes tests 51*523fa7a6SAndroid Build Coastguard Worker├── tester # Arm Tester class 52*523fa7a6SAndroid Build Coastguard Worker├── tosautil # Utility functions for TOSA artifacts 53*523fa7a6SAndroid Build Coastguard Worker├ common.py # Common functions and definitions used by many tests 54*523fa7a6SAndroid Build Coastguard Worker``` 55*523fa7a6SAndroid Build Coastguard Worker 56*523fa7a6SAndroid Build Coastguard WorkerSome example commands to run these tests follow. Run a single test: 57*523fa7a6SAndroid Build Coastguard Worker 58*523fa7a6SAndroid Build Coastguard Worker``` 59*523fa7a6SAndroid Build Coastguard Workerpython -m unittest backends.arm.test.ops.test_add.TestSimpleAdd -k test_add2_tosa_BI 60*523fa7a6SAndroid Build Coastguard Worker``` 61*523fa7a6SAndroid Build Coastguard Worker 62*523fa7a6SAndroid Build Coastguard WorkerOr all tests in "TestSimpleAdd": 63*523fa7a6SAndroid Build Coastguard Worker 64*523fa7a6SAndroid Build Coastguard Worker``` 65*523fa7a6SAndroid Build Coastguard Workerpython -m unittest backends.arm.test.ops.test_add.TestSimpleAdd 66*523fa7a6SAndroid Build Coastguard Worker``` 67*523fa7a6SAndroid Build Coastguard Worker 68*523fa7a6SAndroid Build Coastguard WorkerOr discover and run many tests: 69*523fa7a6SAndroid Build Coastguard Worker 70*523fa7a6SAndroid Build Coastguard Worker``` 71*523fa7a6SAndroid Build Coastguard Workerpython -m unittest discover -s backends/arm/test/ops/ 72*523fa7a6SAndroid Build Coastguard Worker``` 73*523fa7a6SAndroid Build Coastguard Worker 74*523fa7a6SAndroid Build Coastguard Worker### A note on unit tests 75*523fa7a6SAndroid Build Coastguard Worker 76*523fa7a6SAndroid Build Coastguard WorkerThere are currently 3 ways we unit test our code. 77*523fa7a6SAndroid Build Coastguard Worker1. TOSA main inference. These tests are using non-quantized data and ops. Edge IR representation of the module is lowered to a TOSA flatbuffer, which is tested for numerical correcteness using the ```tosa_reference_model``` tool. 78*523fa7a6SAndroid Build Coastguard Worker2. TOSA base inference. Same as above, but data and ops are quantized. 79*523fa7a6SAndroid Build Coastguard Worker3. Ethos-U55. These tests use quantized data and ops (aka TOSA base inference). Edge IR is lowered to a TOSA flatbuffer, which is fed into the Vela compiler. Theses tests are functional tests and do not test numerical correctness, since that should be guaranteed by TOSA. 80*523fa7a6SAndroid Build Coastguard Worker 81*523fa7a6SAndroid Build Coastguard WorkerIn order to distinguise between the different tests, the following suffixes have been added to the respective test case. 82*523fa7a6SAndroid Build Coastguard Worker* ```_MI``` for main inference 83*523fa7a6SAndroid Build Coastguard Worker* ```_BI``` for base inference 84*523fa7a6SAndroid Build Coastguard Worker* ```_U55_BI``` for base inference on U55 85*523fa7a6SAndroid Build Coastguard Worker 86*523fa7a6SAndroid Build Coastguard Worker## Help & Improvements 87*523fa7a6SAndroid Build Coastguard WorkerIf you have problems or questions, or have suggestions for ways to make 88*523fa7a6SAndroid Build Coastguard Workerimplementation and testing better, please reach out to the Arm team developing this delegate, or 89*523fa7a6SAndroid Build Coastguard Workercreate an issue on [github](https://www.github.com/pytorch/executorch/issues). 90*523fa7a6SAndroid Build Coastguard Worker 91*523fa7a6SAndroid Build Coastguard Worker# Arm Backend Architecture 92*523fa7a6SAndroid Build Coastguard Worker 93*523fa7a6SAndroid Build Coastguard WorkerThe broad principle with the Arm backend implemention for ExecuTorch is to support multiple Arm devices and device configurations through a largely Homogeneous flow with maximal sharing of class logic. 94*523fa7a6SAndroid Build Coastguard Worker 95*523fa7a6SAndroid Build Coastguard WorkerIn practice for compilation, this means that the flow goes via [Arm TOSA](https://www.mlplatform.org/tosa/tosa_spec.html) to produce a common IR and quantization behaviour compatible with our various IP, and typically, device-specific backends to further lower to a device specific binary which can happen ahead of time (within the Python development flow) or at runtime (during a JIT compilation stage). 96*523fa7a6SAndroid Build Coastguard Worker 97*523fa7a6SAndroid Build Coastguard WorkerIn practice for the runtime, this means we will share common runtime backend functionality, with the aim for features like debugging to be available through common tooling. 98*523fa7a6SAndroid Build Coastguard Worker 99*523fa7a6SAndroid Build Coastguard Worker 100*523fa7a6SAndroid Build Coastguard Worker## Arm Backend Status and Maturity 101*523fa7a6SAndroid Build Coastguard Worker 102*523fa7a6SAndroid Build Coastguard WorkerThe Arm Backend should be considered a prototype quality at this point, likely subject to significant change and improvement, and with a limited coverage of functionality. We are actively developing this codebase. 103*523fa7a6SAndroid Build Coastguard Worker 104*523fa7a6SAndroid Build Coastguard Worker## Current flows 105*523fa7a6SAndroid Build Coastguard Worker 106*523fa7a6SAndroid Build Coastguard WorkerThe ArmBackend has a two stage process, 107*523fa7a6SAndroid Build Coastguard Worker- Compile to TOSA to rationalise the graph into known hardware support profiles. Currently this is to v0.80.0 TOSA BI with specific concern to a subset which gives support on Ethos-U55, the target of the initial prototype efforts. 108*523fa7a6SAndroid Build Coastguard Worker- Lower via the ethos-u-vela compilation flow which takes TOSA v0.80.0 as an input and produces a low level commandstream for the hardware which is then passed via the delegate to the ethos-u-core-driver for direct execution. 109*523fa7a6SAndroid Build Coastguard Worker 110*523fa7a6SAndroid Build Coastguard WorkerThe ArmPartitioner is currenly used to ensure the operations converted are Ethos-U compatible, but will be extended to offer spec-correct TOSA Base inference and TOSA Main Inference generation in future. 111*523fa7a6SAndroid Build Coastguard Worker 112*523fa7a6SAndroid Build Coastguard Worker### Controlling compilation 113*523fa7a6SAndroid Build Coastguard Worker 114*523fa7a6SAndroid Build Coastguard WorkerIt is possible to control the compilation flow to aid in development and debug of both networks and the code itself. 115*523fa7a6SAndroid Build Coastguard Worker 116*523fa7a6SAndroid Build Coastguard WorkerConfiguration of the ArmBackend export flow is controlled by CompileSpec information (essentially used as compilation flags) to determine which of these outputs is produced. In particular this allows for use of the tosa_reference_model to run intermediate output to check for correctness and quantization accuracy without a full loop via hardware implemntation. 117*523fa7a6SAndroid Build Coastguard Worker 118*523fa7a6SAndroid Build Coastguard WorkerAs this is in active development see the ArmBackend for accurate information on [compilation flags](https://github.com/pytorch/executorch/blob/29f6dc9353e90951ed3fae3c57ae416de0520067/backends/arm/arm_backend.py#L319-L324) 119*523fa7a6SAndroid Build Coastguard Worker 120*523fa7a6SAndroid Build Coastguard WorkerYou can also refer to the [example TOSA end-to-end code](/examples/arm/arm_tosa_e2e.py) 121