§
    l¬¸jg  ã                  óX   — d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	 dd„Z
	 	 	 	 ddd„Zd	S )é    )Úannotations)ÚPath)ÚLOGGERÚQNN_HTP_TARGETSÚWINDOWS)Úcheck_requirementsÚreturnútuple[str | None, str]c                 ój  — 	 ddl } |                      ¦   «         |                      ¦   «         fS # t          $ rz ddl}t          |j        ¦  «        j        dz  }d|                     ¦   «         v rd}n|t          rdndz  }t          rdnd}|rt          |¦  «        ndt          ||z  ¦  «        fcY S w xY w)	a/  Resolve the QNN Execution Provider and HTP backend library paths for the installed onnxruntime-qnn build.

    onnxruntime-qnn ships two ways: plugin builds expose an `onnxruntime_qnn` helper module, while monolithic builds
    expose `QNNExecutionProvider` directly and bundle the QNN backend libraries in `onnxruntime/capi`.

    Returns:
        (tuple[str | None, str]): `(ep_library_path, htp_backend_path)`. `ep_library_path` is `None` when QNN is already
            built into ONNX Runtime and does not need `register_execution_provider_library`.
    r   NÚcapiÚQNNExecutionProviderzonnxruntime_providers_qnn.dllzlibonnxruntime_providers_qnn.soz
QnnHtp.dllzlibQnnHtp.so)Úonnxruntime_qnnÚget_library_pathÚget_qnn_htp_pathÚImportErrorÚonnxruntimer   Ú__file__ÚparentÚget_available_providersr   Ústr)Úqnn_epr   r   Úep_libÚhtp_libs        úl/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/utils/export/qnn.pyÚqnn_library_pathsr      sè   € ðDØ(Ð(Ð(Ð(à×&Ò&Ñ(Ô(¨&×*AÒ*AÑ*CÔ*CÐCÐCøÝð 	Dð 	Dð 	DØÐÐÐå�KÔ(Ñ)Ô)Ô0°6Ñ9ˆØ! [×%HÒ%HÑ%JÔ%JÐJÐJØˆFˆFàÅÐnÐ<Ð<ÐMnÑoˆFÝ")Ð=�,�,¨~ˆØ$Ð.�s�6‰{Œ{ˆ{¨$µ°D¸7±NÑ0CÔ0CÐCÐCÐCÐCð	Døøøs   ‚+. ®BB2Â1B2Ú73NÚ Ú	onnx_fileú
str | PathÚoutput_fileú
Path | strÚnamer   Úmetadataúdict | NoneÚbatchÚintÚprefixc                ó	  ‡!‡"‡#— t          d¦  «         ddl}ddlm}	m}
 ddlm} ddlm} ddl	m
} t          ¦   «         \  }}t          | ¦  «        } t          |¦  «        }|j                             dd¬	¦  «         |                     | j        › d
�¦  «        }|                     | j        › d�¦  «        }t#          j        d|› d|› d�¦  «         ddl}d„ |                     t+          | ¦  «        ¦  «        j        j        d         j        j        j        j        D ¦   «         }t9          |¦  «        dk    r|d         dv r|d         dvr|Š#ˆ#fd„}	  |t+          | ¦  «        t+          |¦  «        ¦  «          |t+          |¦  «         ||||¬¦  «        |	j        |	j        ¬¦  «        } |
t+          |¦  «        t+          |¦  «        |¦  «         dŠ!|dddœ}t>          |         \  }}|||<   |                      ¦   «         }| !                    dd¦  «         | !                    dt+          |¦  «        ¦  «         | !                    dd¦  «         |r| "                    ‰!|¦  «         	 |rlˆ!fd„| #                    ¦   «         D ¦   «         }|stI          d ¦  «        ‚| %                    ||¦  «         | &                    t+          |¦  «        |¬!¦  «         n(| &                    t+          |¦  «        |‰!g|g¬"¦  «         |r| '                    ‰!¦  «         n# |r| '                    ‰!¦  «         w w xY w||fD ]}| (                    d¬#¦  «         Œn"# ||fD ]}| (                    d¬#¦  «         Œw xY w| )                    ¦   «         stI          d$|› d%|› d&�¦  «        ‚|rØddl}|                     t+          |¦  «        ¦  «        }d'„ |j*        D ¦   «         Š"ˆ"fd(„| +                    ¦   «         D ¦   «         x}r| +                    ¦   «         D ]G\  }}|j*         ,                    ¦   «         } t+          |¦  «        t+          |¦  «        c| _-        | _.        ŒH| /                    |t+          |¦  «        ¦  «         t+          |¦  «        S ))uâ  Convert an ONNX model to a Qualcomm QNN context binary using the ONNX Runtime QNN Execution Provider.

    The conversion runs entirely on the host with no Qualcomm account or cloud upload. The model is quantized with ONNX
    Runtime's QNN QDQ flow to 16-bit activations and 8-bit weights (the recommended accuracy/performance balance for the
    Hexagon NPU), then the `onnxruntime-qnn` Execution Provider â€” which bundles the Qualcomm AI Runtime (QAIRT)
    libraries â€” compiles the quantized graph into a QNN context binary embedded in `<stem>_qnn.onnx`. No inference is
    run.

    Args:
        onnx_file (str | Path): Path to the source ONNX file (already exported).
        output_file (Path | str): Path to save the exported QNN ONNX context-binary model.
        dataset (DataLoader): Calibration dataloader (from `Exporter.get_int8_calibration_dataloader`) used for INT8
            quantization.
        transform_fn (Callable): Preprocessing transform (`Exporter._transform_fn`) converting a calibration item to a
            normalized `float32` NCHW array.
        name (str): Target Hexagon Tensor Processor (HTP) architecture version, e.g. `"73"` (Snapdragon 8 Gen 2), `"75"`
            (8 Gen 3), `"79"` (8 Elite), or supported SoC name such as `"iq-8275"`. Finalizes the graph for the target
            chip when exporting on a host without a Snapdragon NPU.
        metadata (dict | None): Ultralytics model metadata ensured present in the context model's `metadata_props` (ONNX
            Runtime normally carries the source model's metadata through, but this is not a documented guarantee).
        batch (int): Static batch dimension of the ONNX graph used to tile undersized calibration batches, or 0 for
            dynamic-batch models.
        prefix (str): Prefix for log messages.

    Returns:
        (str): Path to the exported `*_qnn.onnx` file.

    Notes:
        `onnxruntime-qnn` wheels may expose QNN either as a plugin library or as a built-in ONNX Runtime provider.
    zonnxruntime-qnnr   N)Ú	QuantTypeÚquantize)Úget_qnn_qdq_config)Úquant_pre_process)Úonnx_calibration_readerT)ÚparentsÚexist_okz_qnn_preprocessed.onnxz_qnn_qdq.onnxú
zJ starting W8A16 quantization and export with ONNX Runtime QNN (HTP target z)...c                ó   — g | ]	}|j         ‘Œ
S © )Ú	dim_value)Ú.0Úds     r   ú
<listcomp>zonnx2qnn.<locals>.<listcomp>`   s   € ÐeÐeÐe˜AˆAŒKÐeÐeÐeó    é   é   >   é   r9   r:   c                óF   •—  ‰| ¦  «                              dddd¦  «        S )z-Transform calibration data from NCHW to NHWC.r   é   r9   r:   )Ú	transpose)Ú	data_itemÚnchw_transforms    €r   Útransform_fnzonnx2qnn.<locals>.transform_fnd   s'   ø€ à!�> )Ñ,Ô,×6Ò6°q¸!¸QÀÑBÔBÐBr7   )r%   )Úactivation_typeÚweight_typer   Ú3Ú0)Úbackend_pathÚ(htp_graph_finalization_optimization_modeÚ"enable_htp_shared_memory_allocatorzep.context_enableÚ1zep.context_file_pathzep.context_embed_modec                ó*   •— g | ]}|j         ‰k    ¯|‘ŒS r2   )Úep_name)r4   r5   rJ   s     €r   r6   zonnx2qnn.<locals>.<listcomp>‡   s%   ø€ ÐSÐSÐS ¸a¼iÈ7Ò>RÐ>R˜1Ð>RÐ>RÐ>Rr7   z@QNN EP registered but no QNN devices were found by ONNX Runtime.)Úsess_options)rK   Ú	providersÚprovider_options)Ú
missing_okz(QNN context binary was not generated at z. See z logs for details.c                ó   — h | ]	}|j         ’Œ
S r2   )Úkey)r4   Úps     r   ú	<setcomp>zonnx2qnn.<locals>.<setcomp>ž   s   € Ð<Ð<Ð<˜a�A”EÐ<Ð<Ð<r7   c                ó>   •— i | ]\  }}t          |¦  «        ‰v¯||“ŒS r2   )r   )r4   ÚkÚvÚexistings      €r   ú
<dictcomp>zonnx2qnn.<locals>.<dictcomp>Ÿ   s/   ø€ ÐSÐSÐS¡  1½CÀ¹F¼FÈ(Ð<RÐ<R�q˜!Ð<RÐ<RÐ<Rr7   )0r   r   Úonnxruntime.quantizationr)   r*   Ú0onnxruntime.quantization.execution_providers.qnnr+   Ú(onnxruntime.quantization.shape_inferencer,   Úultralytics.utils.export.onnxr-   r   r   r   ÚmkdirÚ	with_nameÚstemr   ÚinfoÚonnxÚloadr   ÚgraphÚinputÚtypeÚtensor_typeÚshapeÚdimÚlenÚQUInt16ÚQUInt8r   ÚSessionOptionsÚadd_session_config_entryÚ#register_execution_provider_libraryÚget_ep_devicesÚRuntimeErrorÚadd_provider_for_devicesÚInferenceSessionÚ%unregister_execution_provider_libraryÚunlinkÚexistsÚmetadata_propsÚitemsÚaddrP   ÚvalueÚsave)$r   r    Údatasetr@   r"   r#   r%   r'   Úortr)   r*   r+   r,   r-   Ú
ep_libraryÚhtp_backendÚctx_fileÚpre_fileÚqdq_filer`   ÚdimsÚ
qdq_configÚ
ep_optionsÚoptionrx   ÚoptionsÚdevicesÚfÚ	ctx_modelÚmissingrT   rU   ÚentryrJ   rV   r?   s$                                    @@@r   Úonnx2qnnr‹   %   sZ  øøø€ õP Ð(Ñ)Ô)Ð)ØÐÐÐØ<Ð<Ð<Ð<Ð<Ð<Ð<Ð<ØSÐSÐSÐSÐSÐSØJÐJÐJÐJÐJÐJàEÐEÐEÐEÐEÐEå/Ñ1Ô1Ñ€J�å�Y‘”€IÝ�KÑ Ô €HØ„O×Ò $°ÐÑ6Ô6Ð6Ø×!Ò! Y¤^Ð"KÐ"KÐ"KÑLÔL€HØ×!Ò! Y¤^Ð"BÐ"BÐ"BÑCÔC€Hå
„KÐq�VÐqÐqÐgkÐqÐqÐqÑrÔrÐrØ€K€K€KàeÐe §¢­3¨y©>¬>Ñ!:Ô!:Ô!@Ô!FÀqÔ!IÔ!NÔ!ZÔ!`Ô!dÐeÑeÔe€DÝ
ˆ4�y„y�A‚~€~˜$˜qœ' VÐ+Ð+°°Q´¸vÐ0EÐ0EØ%ˆð	Cð 	Cð 	Cð 	Cð 	Cð-&ØÐ�#˜i™.œ.­#¨h©-¬-Ñ8Ô8Ð8à'Ð'Ý�‰MŒMØ#Ð# G¨\ÀÐGÑGÔGØ%Ô-Ø!Ô(ð	
ñ 
ô 
ˆ
ð 	ˆ•�X‘”¥ H¡¤¨zÑ:Ô:Ð:ð )ˆà'Ø8;Ø25ð
ð 
ˆ
õ
 (¨Ô-‰ˆ�Ø"ˆ
�6ÑØ×$Ò$Ñ&Ô&ˆØ×(Ò(Ð)<¸cÑBÔBÐBØ×(Ò(Ð)?ÅÀXÁÄÑOÔOÐOØ×(Ò(Ð)@À#ÑFÔFÐFØð 	IØ×3Ò3°G¸ZÑHÔHÐHð	CØð 	ØSÐSÐSÐS c×&8Ò&8Ñ&:Ô&:ÐSÑSÔS�Øð kÝ&Ð'iÑjÔjÐjØ×0Ò0°¸*ÑEÔEÐEØ×$Ò$¥S¨¡]¤]ÀÐ$ÑIÔIÐIÐIà×$Ò$Ý˜‘M”M°ÀGÀ9Ð`jÐ_kð %ñ ô ð ð ð CØ×9Ò9¸'ÑBÔBÐBøøð ð CØ×9Ò9¸'ÑBÔBÐBÐBðCøøøð ˜HÐ%ð 	&ð 	&ˆAØ�HŠH ˆHÑ%Ô%Ð%Ð%ð	&ø�(˜HÐ%ð 	&ð 	&ˆAØ�HŠH ˆHÑ%Ô%Ð%Ð%ð	&øøøð �?Š?ÑÔð rÝÐpÀhÐpÐpÐV\ÐpÐpÐpÑqÔqÐqàð 	0Øˆˆˆà—I’I�c (™mœmÑ,Ô,ˆ	Ø<Ð< 9Ô#;Ð<Ñ<Ô<ˆØSÐSÐSÐS¨¯ªÑ(8Ô(8ÐSÑSÔSÐSˆ7ð 	0ØŸš™œð 8ð 8‘��1Ø!Ô0×4Ò4Ñ6Ô6�Ý),¨Q©¬µ°Q±´Ð&�”	˜5œ;˜;Ø�IŠI�i¥ X¡¤Ñ/Ô/Ð/Ýˆx‰=Œ=Ðs&   ÅDM É,BL ÌM ÌL4Ì4M ÍM4)r	   r
   )r   Nr   r   )r   r   r    r!   r"   r   r#   r$   r%   r&   r'   r   r	   r   )Ú
__future__r   Úpathlibr   Úultralytics.utilsr   r   r   Úultralytics.utils.checksr   r   r‹   r2   r7   r   ú<module>r�      s«   ðð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð à >Ð >Ð >Ð >Ð >Ð >Ð >Ð >Ð >Ð >Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7ðDð Dð Dð Dð> Ø ØØðð ð ð ð ð ð r7   