3030 SimpleADB ,
3131)
3232from executorch .backends .qualcomm .quantizer .quantizer import QuantDtype
33- from executorch .backends .qualcomm .serialization .qc_schema import QcomChipset
33+ from executorch .backends .qualcomm .serialization .qc_schema import (
34+ QcomChipset ,
35+ QnnExecuTorchBackendType ,
36+ QnnExecuTorchLpaiTargetEnv ,
37+ )
3438from executorch .backends .qualcomm .utils .constants import QCOM_PASS_ACTIVATE_KEY
3539from executorch .backends .qualcomm .utils .utils import (
3640 draw_graph ,
3741 dump_context_from_pte ,
3842 from_context_binary ,
3943 generate_htp_compiler_spec ,
44+ generate_lpai_compiler_spec ,
4045 generate_qnn_executorch_compiler_spec ,
4146 generate_qnn_executorch_option ,
4247 QNN_QUANT_TYPE_MAP ,
@@ -104,7 +109,7 @@ def fill_tensor_info(info, qnn_tensors, category):
104109 qnn_mgr = PyQnnManagerAdaptor .QnnManager (
105110 generate_qnn_executorch_option (compiler_specs ), ctx_bin
106111 )
107- assert qnn_mgr .Init ().value == 0 , "failed to load context binary "
112+ assert qnn_mgr .Init ().value == 0 , "failed to initialize backend "
108113 graph_name = qnn_mgr .GetGraphNames ()[0 ]
109114 qnn_mgr .AllocateTensor (graph_name )
110115 fill_tensor_info (tensor_info , qnn_mgr .GetGraphInputs (graph_name ), in_key )
@@ -206,8 +211,17 @@ def compile(args):
206211
207212 file_name , extension = Path (args .artifact ).stem , Path (args .artifact ).suffix
208213 os .makedirs (args .output_folder , exist_ok = True )
209- # setup compiler spec dedicated to QNN HTP backend
210- backend_options = generate_htp_compiler_spec (use_fp16 = True )
214+ # setup compiler spec
215+ backend_type = get_backend_type (args .backend )
216+ match backend_type :
217+ case QnnExecuTorchBackendType .kHtpBackend :
218+ backend_options = generate_htp_compiler_spec (use_fp16 = True )
219+ case QnnExecuTorchBackendType .kLpaiBackend :
220+ backend_options = generate_lpai_compiler_spec (
221+ target_env = QnnExecuTorchLpaiTargetEnv .kArm
222+ )
223+ case _:
224+ raise ValueError ("Backend is not implemented yet" )
211225 # setup general compiler spec for QNN
212226 compiler_specs = generate_qnn_executorch_compiler_spec (
213227 soc_model = getattr (QcomChipset , args .soc_model ),
@@ -305,8 +319,17 @@ def execute(args):
305319 user_inputs .append (inputs )
306320
307321 logger .info ("retrieving graph I/O" )
308- # setup compiler spec dedicated to QNN HTP backend
309- backend_options = generate_htp_compiler_spec (use_fp16 = True )
322+ # setup compiler spec
323+ backend_type = get_backend_type (args .backend )
324+ match backend_type :
325+ case QnnExecuTorchBackendType .kHtpBackend :
326+ backend_options = generate_htp_compiler_spec (use_fp16 = True )
327+ case QnnExecuTorchBackendType .kLpaiBackend :
328+ backend_options = generate_lpai_compiler_spec (
329+ target_env = QnnExecuTorchLpaiTargetEnv .kArm
330+ )
331+ case _:
332+ raise ValueError ("Backend is not implemented yet" )
310333 # setup general compiler spec for QNN
311334 compiler_specs = generate_qnn_executorch_compiler_spec (
312335 soc_model = getattr (QcomChipset , args .soc_model ),
@@ -332,7 +355,7 @@ def execute(args):
332355
333356 logger .info ("pushing QNN libraries & other artifacts" )
334357
335- adb .push (inputs = user_inputs )
358+ adb .push (inputs = user_inputs , backends = [ backend_type ] )
336359
337360 logger .info ("starting inference" )
338361 adb .execute ()
@@ -364,10 +387,16 @@ def post_process():
364387
365388 output_result_folder = f"{ args .output_folder } /Result_{ data_index } "
366389 os .makedirs (output_result_folder , exist_ok = True )
390+ # For the LPAI backend, a dequantize node will be retained for the output, ensuring that the output remains in float32 format.
391+ # TODO: add support for other dtypes for LPAI backend
367392 output = np .fromfile (
368393 filename ,
369- dtype = eval (
370- f"np.{ torch_to_numpy_dtype_dict [output_info [output_index ]['dtype' ]]} "
394+ dtype = (
395+ eval (
396+ f"np.{ torch_to_numpy_dtype_dict [output_info [output_index ]['dtype' ]]} "
397+ )
398+ if backend_type != QnnExecuTorchBackendType .kLpaiBackend
399+ else np .float32
371400 ),
372401 )
373402 output = torch .from_numpy (
@@ -460,9 +489,9 @@ def main():
460489 sub_quantize .add_argument (
461490 "--backend" ,
462491 type = str ,
463- choices = ["htp" , "gpu " ],
492+ choices = ["htp" , "lpai " ],
464493 default = "htp" ,
465- help = "Backend to be deployed ('htp'/'gpu ' are currently supported)." ,
494+ help = "Backend to be deployed ('htp'/'lpai ' are currently supported)." ,
466495 )
467496 sub_quantize .add_argument (
468497 "--eps" ,
@@ -514,6 +543,13 @@ def main():
514543 ),
515544 action = "store_true" ,
516545 )
546+ sub_compile .add_argument (
547+ "--backend" ,
548+ type = str ,
549+ choices = ["htp" , "lpai" ],
550+ default = "htp" ,
551+ help = "Backend to be deployed ('htp'/'lpai' are currently supported)." ,
552+ )
517553 sub_compile .set_defaults (callback = compile )
518554
519555 sub_execute = subparsers .add_parser (
@@ -590,6 +626,13 @@ def main():
590626 ),
591627 action = "store_true" ,
592628 )
629+ sub_execute .add_argument (
630+ "--backend" ,
631+ type = str ,
632+ choices = ["htp" , "lpai" ],
633+ default = "htp" ,
634+ help = "Backend to be deployed ('htp'/'lpai' are currently supported)." ,
635+ )
593636 sub_execute .set_defaults (callback = execute )
594637
595638 args = parser .parse_args ()
0 commit comments