| 149 | |
| 150 | |
| 151 | def to_context_binary( |
| 152 | model_lib, |
| 153 | soc_model, |
| 154 | device, |
| 155 | host, |
| 156 | target, |
| 157 | build_folder, |
| 158 | output_folder, |
| 159 | logger, |
| 160 | ): |
| 161 | ext = Path(model_lib).suffix |
| 162 | if ext == ".bin": |
| 163 | return model_lib |
| 164 | |
| 165 | assert ( |
| 166 | device is not None |
| 167 | ), "Please assign device serial for model library conversion." |
| 168 | logger.info(f"Generating context binary for {model_lib}") |
| 169 | # leverage SimpleADB for model library conversion |
| 170 | lib_name = Path(model_lib).stem |
| 171 | sdk_root = os.getenv("QNN_SDK_ROOT") |
| 172 | qnn_config = QnnConfig( |
| 173 | soc_model=soc_model, |
| 174 | build_folder=build_folder, |
| 175 | device=device, |
| 176 | host=host, |
| 177 | target=target, |
| 178 | ) |
| 179 | adb = SimpleADB( |
| 180 | qnn_config=qnn_config, |
| 181 | pte_path=model_lib, |
| 182 | workspace=f"/data/local/tmp/executorch/{lib_name}", |
| 183 | ) |
| 184 | |
| 185 | logger.info("pushing QNN libraries & tool") |
| 186 | arch = adb.arch_table[soc_model] |
| 187 | files = [ |
| 188 | f"{sdk_root}/bin/aarch64-android/qnn-context-binary-generator", |
| 189 | f"{sdk_root}/lib/aarch64-android/libQnnHtp.so", |
| 190 | f"{sdk_root}/lib/aarch64-android/libQnnHtpV{arch}Stub.so", |
| 191 | f"{sdk_root}/lib/aarch64-android/libQnnHtpPrepare.so", |
| 192 | f"{sdk_root}/lib/hexagon-v{arch}/unsigned/libQnnHtpV{arch}Skel.so", |
| 193 | ] |
| 194 | adb.push(files=files) |
| 195 | |
| 196 | logger.info("starting conversion") |
| 197 | commands = " ".join( |
| 198 | [ |
| 199 | f"cd {adb.workspace} &&", |
| 200 | "export LD_LIBRARY_PATH=. &&", |
| 201 | "./qnn-context-binary-generator", |
| 202 | f"--model {Path(model_lib).name}", |
| 203 | "--backend libQnnHtp.so", |
| 204 | f"--binary_file {lib_name}", |
| 205 | ] |
| 206 | ) |
| 207 | adb.execute(custom_runner_cmd=commands) |
| 208 | |