| 204 | |
| 205 | |
| 206 | def compile(args): |
| 207 | logger = get_logger() |
| 208 | |
| 209 | # setup memory planning |
| 210 | memory_planning_pass = MemoryPlanningPass( |
| 211 | alloc_graph_input=args.shared_buffer is None, |
| 212 | alloc_graph_output=args.shared_buffer is None, |
| 213 | ) |
| 214 | |
| 215 | file_name, extension = Path(args.artifact).stem, Path(args.artifact).suffix |
| 216 | os.makedirs(args.output_folder, exist_ok=True) |
| 217 | # setup compiler spec |
| 218 | backend_type = get_backend_type(args.backend) |
| 219 | match backend_type: |
| 220 | case QnnExecuTorchBackendType.kHtpBackend: |
| 221 | backend_options = generate_htp_compiler_spec(use_fp16=True) |
| 222 | case QnnExecuTorchBackendType.kLpaiBackend: |
| 223 | backend_options = generate_lpai_compiler_spec( |
| 224 | target_env=QnnExecuTorchLpaiTargetEnv.kArm |
| 225 | ) |
| 226 | case _: |
| 227 | raise ValueError("Backend is not implemented yet") |
| 228 | # setup general compiler spec for QNN |
| 229 | compiler_specs = generate_qnn_executorch_compiler_spec( |
| 230 | soc_model=getattr(QcomChipset, args.soc_model), |
| 231 | backend_options=backend_options, |
| 232 | is_from_context_binary=extension == "bin", |
| 233 | ) |
| 234 | if extension == ".bin": |
| 235 | custom_op_name = f"ctx_loader_{file_name}" |
| 236 | # step 1: generate ExportedProgram with custom op as a binary loader & lower it w/QnnBackend |
| 237 | logger.info(f"exporting program for {args.artifact}") |
| 238 | prog_info = from_context_binary( |
| 239 | args.artifact, custom_op_name, getattr(QcomChipset, args.soc_model) |
| 240 | ) |
| 241 | # step 2: write pte files and store final graph |
| 242 | logger.info(f"exporting {file_name}.pte") |
| 243 | with open(f"{args.output_folder}/{file_name}.pte", "wb") as f: |
| 244 | prog_info["edge_program_manager"].to_executorch( |
| 245 | config=ExecutorchBackendConfig( |
| 246 | memory_planning_pass=memory_planning_pass |
| 247 | ) |
| 248 | ).write_to_file(f) |
| 249 | logger.info(f"exporting network graph with {file_name}.svg") |
| 250 | draw_graph(file_name, args.output_folder, prog_info["exported_program"]) |
| 251 | elif extension == ".pt2": |
| 252 | # step 0: prepare exported_program |
| 253 | ep = torch.export.load(args.artifact) |
| 254 | sample_inputs = ep.example_inputs[0] |
| 255 | # step 1: start lowering to QnnBackend |
| 256 | logger.info(f"start lowering program for {args.artifact}") |
| 257 | passes, user_passes = get_capture_program_passes(), [] |
| 258 | if args.pass_job is not None: |
| 259 | for job in args.pass_job: |
| 260 | try: |
| 261 | user_passes.append( |
| 262 | importlib.import_module( |
| 263 | "executorch.backends.qualcomm._passes", job |