|
| 1 | +<!--- Licensed to the Apache Software Foundation (ASF) under one --> |
| 2 | +<!--- or more contributor license agreements. See the NOTICE file --> |
| 3 | +<!--- distributed with this work for additional information --> |
| 4 | +<!--- regarding copyright ownership. The ASF licenses this file --> |
| 5 | +<!--- to you under the Apache License, Version 2.0 (the --> |
| 6 | +<!--- "License"); you may not use this file except in compliance --> |
| 7 | +<!--- with the License. You may obtain a copy of the License at --> |
| 8 | + |
| 9 | +<!--- http://www.apache.org/licenses/LICENSE-2.0 --> |
| 10 | + |
| 11 | +<!--- Unless required by applicable law or agreed to in writing, --> |
| 12 | +<!--- software distributed under the License is distributed on an --> |
| 13 | +<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY --> |
| 14 | +<!--- KIND, either express or implied. See the License for the --> |
| 15 | +<!--- specific language governing permissions and limitations --> |
| 16 | +<!--- under the License. --> |
| 17 | +# Hexagon Graph Launcher |
| 18 | + |
| 19 | +## Compilation |
| 20 | + |
| 21 | +The launcher consists of two parts: part running on Hexagon, and part running |
| 22 | +on Android. They need to be compiled separately. Since some source files are |
| 23 | +shared between these two parts, make sure to delete all object files between |
| 24 | +compilations. Compile the Hexagon code first. |
| 25 | + |
| 26 | +The supported Snapdragon architectures are 855, 865, and 888. |
| 27 | + |
| 28 | +### Prerequisites |
| 29 | + |
| 30 | +1. Android NDK version r19c or later. |
| 31 | +2. Hexagon SDK version 4.0.0 or later. |
| 32 | + |
| 33 | +Android NDK can be downloaded from https://developer.android.com/ndk. |
| 34 | +Hexagon SDK is available at //developer.qualcomm.com/software/hexagon-dsp-sdk. |
| 35 | + |
| 36 | +### Compilation of the Hexagon part |
| 37 | + |
| 38 | +1. Build the static version of TVM runtime for Hexagon. Use Hexagon clang |
| 39 | + from the Hexagon SDK. This step is the same as building the shared version, |
| 40 | + except at the cmake step, add `-DBUILD_STATIC_RUNTIME=ON`. The compilation |
| 41 | + step should create `libtvm_runtime.a`. |
| 42 | + |
| 43 | +2. Create a subdirectory for the build files, and run `cmake` with the |
| 44 | + following variables set: |
| 45 | + - `FASTRPC_LIBS=SKEL` |
| 46 | + - `USE_HEXAGON_SDK` to the path to the Hexagon SDK |
| 47 | + - `CMAKE_C_COMPILER=hexagon-clang` |
| 48 | + - `CMAKE_CXX_COMPILER=hexagon-clang++` |
| 49 | + - `USE_HEXAGON_ARCH` to one of v65, v66, v68 |
| 50 | + - `TVM_RUNTIME_HEXAGON=/path/to/libtvm_runtime.a` _statically_ linked |
| 51 | + TVM runtime |
| 52 | + |
| 53 | + Make sure to provide the path to launcher's `CMakeLists.txt` directory |
| 54 | + in `cmake` invocation. |
| 55 | + |
| 56 | +3. Run `make`. This will create `liblauncher_rpc_skel.so`. |
| 57 | + |
| 58 | +### Compilation of the Android part |
| 59 | + |
| 60 | +1. Build TVM runtime for Android, using clang for AArch64 from the Android |
| 61 | + NDK. Unlike in the Hexagon case, this should be the dynamic library (which |
| 62 | + is the default), i.e. `libtvm_runtime.so`. |
| 63 | + |
| 64 | +2. Create a subdirectory for the build files (different from the one used for |
| 65 | + Hexagon files), and run `cmake` with the following variables set: |
| 66 | + - `FASTRPC_LIBS=STUB` |
| 67 | + - `USE_HEXAGON_SDK` to the path to the Hexagon SDK |
| 68 | + - `CMAKE_C_COMPILER=aarch64-linux-android28-clang` (or later) |
| 69 | + - `CMAKE_CXX_COMPILER=aarch64-linux-android28-clang++` (or later) |
| 70 | + - `USE_HEXAGON_ARCH` to one of v65, v66, v68 (same as for the Hexagon part) |
| 71 | + - `TVM_RUNTIME_ANDROID=/path/to/libtvm_runtime.so` dynamically or |
| 72 | + statically linked TVM runtime |
| 73 | + |
| 74 | +3. Run `make`. This will create `launcher_android`. |
| 75 | + |
| 76 | +## Execution |
| 77 | + |
| 78 | +From the Android shell, do |
| 79 | +``` |
| 80 | +./launcher_android --in_config input.json --out_config output.json |
| 81 | +``` |
| 82 | + |
| 83 | +You may need to add the location of `libtvm_runtime.so` to `LD_LIBRARY_PATH`. |
| 84 | +See below for more information about the setup and launcher's inputs. |
| 85 | + |
| 86 | +### Preparation steps |
| 87 | + |
| 88 | +Copy the following binaries to the device: |
| 89 | +- `liblauncher_rpc_skel.so`: created by the compilation step for Hexagon, |
| 90 | +- `libgcc.so`: take this one from the Hexagon toolchain, |
| 91 | +- `launcher_android`: created by the compilation step for Android, |
| 92 | +- `libtvm_runtime.so`: built for Android. |
| 93 | + |
| 94 | +These are only the binaries related to the launcher itself. To run a model |
| 95 | +copy the shared object with the model and the model JSON file over to the |
| 96 | +device (both are obtained from relay). Also, copy all input files for the |
| 97 | +model as well. |
| 98 | + |
| 99 | +The following snippet illustrates how to obtain the shared object and the |
| 100 | +JSON file from a TFLite model (using Inception V3 as an example): |
| 101 | + |
| 102 | +``` |
| 103 | +# Skipped imports, etc. |
| 104 | +
|
| 105 | +with open("inception_v3.tflite", "rb") as f: |
| 106 | + tflite_model_buf = f.read() |
| 107 | +tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0) |
| 108 | +
|
| 109 | +shape_dict = { "input": [1,299,299,3] } |
| 110 | +dtype_dict = { "input": "float32" } |
| 111 | +
|
| 112 | +mod, params = relay.frontend.from_tflite( |
| 113 | + tflite_model, shape_dict=shape_dict, dtype_dict=dtype_dict |
| 114 | +) |
| 115 | +
|
| 116 | +target = tvm.target.hexagon('v68', link_params=True) |
| 117 | +with tvm.transform.PassContext(opt_level=3): |
| 118 | + lib = relay.build(mod, target, target_host=target, params=params, mod_name="default") |
| 119 | +
|
| 120 | +# Save model.so and model.json: |
| 121 | +with open('model.json', 'w') as f: |
| 122 | + f.write(lib.get_graph_json()) |
| 123 | +lib.get_lib().save('model.so') |
| 124 | +``` |
| 125 | + |
| 126 | +The final thing is to prepare a JSON configuration file for the launcher. |
| 127 | +The JSON has two attributes describing the model: `model-library` and |
| 128 | +`model-json`, and an attribute `inputs`, which is a list of records, one |
| 129 | +for each input file. |
| 130 | +An input file record has three attributes: `file`, `shape`, and `dtype`. |
| 131 | + |
| 132 | +Below is an example of the input config file for Inception V3: |
| 133 | +``` |
| 134 | +{ |
| 135 | + "model-library": "inceptionv3-float32.so", |
| 136 | + "model-json": "inceptionv3-float32.json", |
| 137 | + "inputs" : [ |
| 138 | + { |
| 139 | + "file": "panda_299x299_fp.dat", |
| 140 | + "shape": [1,299,299,3], |
| 141 | + "dtype": "float32" |
| 142 | + } |
| 143 | + ] |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +The launcher will then create the output JSON file (with the name given via |
| 148 | +`--out_config`) containing information about the execution time and the model |
| 149 | +outputs. The output JSON file has three attributes: "pcycles", "usecs" that |
| 150 | +contain the execution duration in terms of processor cycles and microseconds |
| 151 | +respectivaly, and an attribute `outputs`, which is a list of output file records |
| 152 | +whose syntax is identical to the input file records in the input file. |
| 153 | +A sample output JSON from running the Inception V3 model may look like |
| 154 | +``` |
| 155 | +{ |
| 156 | + "pcycles": 112965680178, |
| 157 | + "usecs": 79532302, |
| 158 | + "outputs": [ |
| 159 | + { |
| 160 | + "file": "output0.dat", |
| 161 | + "shape": [1, 1001], |
| 162 | + "dtype": "float32" |
| 163 | + } |
| 164 | + ] |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +# Disclaimer |
| 169 | + |
| 170 | +The launcher does not perform any correctness verification. In order to verify |
| 171 | +correctness, the user needs to copy the output files from the device and |
| 172 | +verify their contents. |
| 173 | + |
| 174 | +This launcher is intended for use with prototyping and does not utilize any |
| 175 | +performance acceleration, as such the measured performance may be very poor. |
0 commit comments