
    i#                     6   S SK r S SKJr  S SKJrJrJr  S SKrS SKJ	r	  S SK
Jr  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJr  S SKJrJrJr  S SKJr  SrSr\ R@                  " \!5      r"\"RG                  \ RH                  5        \ " S S\5      5       r%g)    N)defaultdict)DictfinalList)QnnPassManager)get_node_visitors)OpContextLoader)generate_qnn_executorch_option)QnnExecuTorchOpPackageInfo)flatbuffer_to_option)QCOM_AXIS_ORDER)get_current_qnn_manager)BackendDetailsCompileSpecPreprocessResult)ExportedProgrami  forwardc            	           \ rS rSr\S\S\S\\   S\4S j5       r	\S\S\\
   S\4S	 j5       r\S
\\\\   4   S\\\\\
      4   S\4S j5       rSrg)
QnnBackend(   edge_programenable_tensor_dumpop_package_infosuse_mha2shac                    U R                   R                  R                   H6  n[        US5      (       d  M  UR                  R                  [        S5        M8     [        5       R                  XS9nUc   e[        [        5      n[        U UUS9n/ nUR                  R                   GH  nUR                  S:X  Gaj  [        R                  SU SUR                  R                    35        UR                  R                   U;   ag  XtR                  R                      R#                  XF5      n	U	b;  [%        U	[&        5      (       a  UR)                  U	5        M  UR+                  U	5        M  M  SU SUR                   S	UR                  R                    S
3n
 [-        S[.        R0                   SUR                  R                    3[3        5       R5                  [6        R8                  5      5      nUR                  U:X  d   U
5       eUR                  [.        R:                     s  $ UR                  S;   a  GM  [=        UR                   S35      e   U$ !   [=        U
5      e= f)Nmeta )r   )r   r   call_functionz
Visiting: z, zFor :z! is not supported in Qnn Delegatez
torch.ops..)get_attrplaceholderoutputz is not supported in Qnn)graph_modulegraphnodeshasattrr   popr   r   !transform_for_preprocess_pipeliner   dictr   oploggerinfotarget__name__define_node
isinstancer   extendappendevalr	   	namespaceglobalsupdatetorch__dict__meta_ctx_binRuntimeError)r   r   r   r   noder$   nodes_to_wrappersnode_visitorspy_op_wrapper_listpy_op_wrappererr_msgcontext_loader_targets               j/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/backends/qualcomm/qnn_preprocess.py_build_op_wrappersQnnBackend._build_op_wrappers*   s3    !--3399DtV$$		or2 :
 &'II J 
 ''''-)1-

   &&,,Dww/)jb1E1E0FGH;;''=8$1++2F2F$G$S$S%M %0%mT::.55mD.55mD	 1 tfBtwwiq1E1E0F G; ; 	404()B)B(C1T[[EYEYDZ[#I,,U^^<1-  ${{.CCLWLC#yy)E)EFF   
 "dggY.F#GHHG -J "!4*733s   BII$compile_specsreturnc                 n   [        U5      n[        U5      n[        UR                  R                  U5      nUR                  [        /5        [        R                  U UR                  5       UR                  R                  UR                  5      nUR                  UR                  5       U Vs/ s H  ofR                  5       PM     sn/5      nUR                   (       a  [#        SUR$                   35        ['        U5      S:w  d   S5       eUR)                  5         [+        [-        U5      0 S9$ s  snf )N0Record all QNN API calls from saver backend at: r   &Failed to generate Qnn context binary.processed_bytesdebug_handle_map)r
   r   r   backend_optionsbackend_typeInitContextDEFAULT_GRAPH_NAMEr   rD   IsTensorDumpop_package_optionsr   r   CompileGetGraphNamesGetOpWrappersaverexitsaver_output_dirlenDestroyContextr   bytes)r   rF   optionobj_optionsqnn_managerr?   r@   qnn_context_binarys           rC   
preprocessQnnBackend.preprocessi   s'   
 0>*62-''44m
 	!3 45'::$$&**;;##	
 )00%%'@RS@R}((*@RST

 B;C_C_B`a %&!+U-UU+""$!"45
 	
 Ts   /D2
edge_programsc                 ~   [        U R                  5       5      n[        UR                  5       5      S   S   n[        US   R                  5      n[        5       nU R                  5        H  nUR                  [        U5      5        M     [        U5      S:X  d   S5       eU R                  5        Vs0 s H  ow/ _M     nn[        [        U5      5      n[        UR                  R                  U5      n	[        U5       GH-  n
U	R                  U5        / / p[        U R                  5       5       H  u  p[         R#                  SU SU
S-    SU S35        [$        R'                  X   U	R)                  5       UR*                  R,                  UR.                  5      n[1        U[2        5      (       a  UR5                  U5        M  UR5                  U Vs/ s H  nUR7                  5       PM     sn5        M     [        U5      [        U R                  5       5      :X  a  U	R9                  X+5      nUR:                  (       a  [=        SUR>                   35        [        U5      S:w  d   S	5       eU	RA                  5         U R                  5        H'  nX   R5                  [C        [3        U5      0 S
95        M)     GM  [        U5      [        U R                  5       5      :X  aA  [        U R                  5       5       H!  u  pX   R5                  [C        X   S95        M#     GM%  [E        S5      e   U$ s  snf s  snf )Nr      z<Only graphs with the same number of partitions could be usedzProcessing Method(z): (/)rI   rJ   rK   )rL   z#Hybrid compilation is not supported)#listkeysvaluesr   valuesetaddrZ   nextiterr   rN   rO   rangerP   	enumerater,   r-   r   rD   rR   rS   r   r   r1   r\   r3   rV   rT   rW   rX   rY   r[   r   r;   )rc   rF   graph_namescompile_specr]   num_sub_graphsr   keyall_processed_resultsr_   ir?   ctx_binary_listjprogramspy_op_wrappersr@   r`   s                     rC   preprocess_multimethod!QnnBackend.preprocess_multimethod   s	    =--/0M0023A6q9%l1o&;&;<)002Ls<01 3 1$	JI	J$ 5B4F4F4H I4HSb4H Id>23-""//
 ~&A
 ##K024b()=)=)?@04!uAn=MQOP!+!>!>K,,.-->>&&	" ne44#**>:&-- 2@1? *6681?  A$ %&#m.B.B.D*EE%0%8%8&" <<J6KbKbJcd *+q0<;<0**,(--/C).55(,12D,E-/ 0 _%]-A-A-C)DD'(:(:(<=FA).55(9KL >
 ##HIIi 'l %$w !J0s   4L5L: N)r/   
__module____qualname____firstlineno__staticmethodr   boolr   r   rD   r   r   ra   r   strr|   __static_attributes__r~       rC   r   r   (   s    <"%<" <" 9:<" 	<" <"|  
% 
K( 
 
 
  
D M%Co!667M%Cd;&7!889M% 
M% M%r   r   )&loggingcollectionsr   typingr   r   r   r8   5executorch.backends.qualcomm._passes.qnn_pass_managerr   :executorch.backends.qualcomm.builders.node_visitor_managerr   3executorch.backends.qualcomm.builders.qnn_constantsr	   ,executorch.backends.qualcomm.partition.utilsr
   4executorch.backends.qualcomm.serialization.qc_schemar   >executorch.backends.qualcomm.serialization.qc_schema_serializer   ,executorch.backends.qualcomm.utils.constantsr   8executorch.backends.qualcomm.utils.qnn_manager_lifecycler   'executorch.exir.backend.backend_detailsr   r   r   torch.export.exported_programr   DEFAULT_DEBUG_HANDLErQ   	getLoggerr/   r,   setLevelDEBUGr   r~   r   rC   <module>r      s     # $ $  P X O W I 
 :  			8	$   q% q% q%r   