+
    9i4                         ^ RI t ^ RIHt ^ RIt^ RIHtHt R t ! R R4      tR tR t	R	 t
R
 tR tR t]R8X  d
   ]! 4        R# R# )    N)ArgumentParser)TensorProtohelperc                    ^ .\        V P                  4      ,          p/ p. p\        V P                  4       F  w  rE\        R VP                   4       4      W&   W,          ^ 8X  d%   VP                  V P                  V,          4       KT  VP                   F'  pWb9  d   V.W&&   K  W&,          P                  V4       K)  	  K  	  V P                   Uu. uF  qwP                  NK  	  ppV P                   U	u. uF  qP                  NK  	  p
p	W,           pVP                  4        RpV Ff  pW8X  d   K  TpWb9   g   K  W&,           FE  pW,          ^,
          W&   W,          ^ 8X  g   K#  VP                  V P                  V,          4       KG  	  Kh  	  ^ p\        V4      pW8  d   W=,          P                   Fe  pW9   g   K  W/,           FN  pW,          ^,
          W&   W,          ^ 8X  g   K#  VP                  V P                  V,          4       V^,           pKP  	  Kg  	  V^,           pK  V\        V P                  4      8X  g   Q R4       hV P                  R4       V P                  P                  V4       R# u upi u up	i )r   c              3   8   "   T F  q'       g   K  ^x  K  	  R# 5i)   N ).0_s   & b/var/www/html/photoedit/myenv/lib/python3.14/site-packages/onnxruntime/tools/qnn/add_trans_cast.py	<genexpr>)graph_topological_sort.<locals>.<genexpr>   s     ">jA11js   	
NzGraph is not a DAGnode)lenr   	enumeratesuminputappendinitializernamesortoutput
ClearFieldextend)graph
deps_countdeps_to_nodessorted_nodesnode_idxr   
input_nameinitinitializer_namesr   graph_input_namesinput_namesprev_input_namestartendr   s   &               r   graph_topological_sortr(      s   s5::&JML#EJJ/"">djj">>
1$

8 45**J.-5J))00:	 % 0 05/@/@A/@t/@A16==#7KO!
($&)55'1';a'?
$'1, ''

8(<= 6 " E
l
C
+")00F& - 5 5H+5+?!+CJ(!+q0$++EJJx,@A!Ag	 !6 1 	#ejj/!7#77!	V	JJl#? B=s   I<8Jc                   &   a  ] tR t^At o R tRtV tR# )QnnTensorStructc                L    R V n         \        P                  V n        . V n        R# ) N)r   r   FLOATonnx_data_typedim)selfs   &r   __init__QnnTensorStruct.__init__B   s    	)//    )r/   r   r.   N)__name__
__module____qualname____firstlineno__r1   __static_attributes____classdictcell__)__classdict__s   @r   r*   r*   A   s      r3   r*   c                 p   V R 8X  g   V R8X  d   \         P                  # V R8X  g   V R8X  d   \         P                  # V R8X  g   V R8X  d   \         P                  # V R8X  d   \         P                  # V R8X  g   V ^8X  d   \         P
                  # V R8X  g   V ^8X  d   \         P                  # V R	8X  g   V ^28X  d   \         P                  # V ^d8X  d   \         P                  # V R
8X  d   \         P                  # V R8X  d   \         P                  # V R8X  d   \         P                  # \         P                  # )i  i  i  i  i2  i2  id  i  i  i2  i  i2  i  )r   UINT8UINT16UINT32UINT64INT8INT16INT32INT64FLOAT16r-   BOOL	UNDEFINED)qnn_data_types   &r   qnn_data_type_to_onnx_data_typerH   H   s   -6"9   	&	 MV$;!!!	&	 MV$;!!!	&	 !!!	&	 MV$;	&	 MV$;   	&	 MV$;   	&	    	&	 """	&	    	&	 $$$r3   c                 8   \        V 4      ;_uu_ 4       p\        P                  ! V4      pR V9   g   Q R4       hRVR ,          9   g   Q R4       hVR ,          R,          P                  4        Fx  w  rERV9   d   RV9   d   RV9   g   Q R4       hVR,          ^ 8X  g   VR,          ^8X  g   K?  \	        4       pWFn        \        VR,          4      Vn        VR,          Vn        WaV&   Kz  	  RRR4       \        V4      ^8  g   Q R	4       hR#   + '       g   i     L); i)
r   z4QNN converted json file not valid. Can't find graph.tensorsz6QNN converted json file not valid. Can't find tensors.type	data_typedimszDQNN converted json file not valid. Can't find some keys from tensorsNzJConverted QNN model not valid. It should have at least 1 input & 1 output.)
openjsonloaditemsr*   r   rH   r.   r/   r   )qnn_json_file_pathqnn_input_output_tensor_dicqnn_json_fileqnn_jsonqnn_tensor_nameqnn_tensor_attribute
qnn_tensors   &&     r   parse_qnn_json_filerY   n   s    	 	!	!]99]+("Z$ZZ"HW--g/gg-5=g5Fy5Q5W5W5Y1O ..#7722V V	V3 $F+q04H4PTU4U,.
"1,KL`alLm,n
)!5f!=
?IO< 6Z	 
"$ *+a/ T/% 
"	!s   BD	+=D		D	c                   a a \        S 4      \        S4      8X  g   Q R 4       h\        ;QJ d2    V V3R l\        \        S 4      4       4       F  '       d   K   R# 	  R# ! V V3R l\        \        S 4      4       4       4      # )z,Onnx shape and Qnn shape has different rank.c              3   ^   <"   T F"  pSV,          P                   SV,          8H  x  K$  	  R # 5i)N)	dim_value)r
   i	onnx_dimsqnn_dimss   & r   r   4compare_onnx_shape_with_qnn_shape.<locals>.<genexpr>   s&     T>Sy|%%!4>Ss   *-FT)r   allrange)r^   r_   s   ffr   !compare_onnx_shape_with_qnn_shaperc      sZ    y>S]*Z,ZZ*3TeC	N>ST33T3T3TeC	N>STTTr3   c                     V ^8  g   Q R4       h. pVP                  ^ 4       VP                  V ^,
          4       \        ^V ^,
          4       F  pVP                  V4       K  	  V#    z,Shape rank should >2 for the Transpose node.r   rb   rankpermr]   s   &  r   gen_to_channel_first_permrk      s[    !8CCC8DKKNKKq1dQhA   Kr3   c                     V ^8  g   Q R4       h. pVP                  ^ 4       \        ^V 4       F  pVP                  V4       K  	  VP                  ^4       V# re   rg   rh   s   &  r   gen_to_channel_last_permrm      sO    !8CCC8DKKN1d^A KKNKr3   c            	      .   \        R 4      p V P                  RRRR\        R7       V P                  RRRR\        R7       V P                  4       p/ p\	        VP
                  V4       \        P                  ! VP                  4      p. p/ pVP                  P                   EFi  pVP                  V9   Ed4   VP                  pW&P                  ,          pVP                  P                  P                  VP                  8w  d   Tp	V	R	,           p
\         P"                  ! R
V
V	.V
.VP                  P                  P                  R7      pVP                  VP                  P                  n        VP%                  V.4       T
pWVP                  &   \'        VP                  P                  P(                  P*                  VP*                  4      '       Eg   \-        \/        VP                  P                  P(                  P*                  4      4      pTpVR,           p\         P"                  ! RVV.V.VR7      pVP%                  V.4       WVP                  &   \1        \/        VP                  P                  P(                  P*                  4      4       FJ  pVP*                  V,          VP                  P                  P(                  P*                  V,          n        KL  	  EKF  EKI  \5        RVP                  ,           R,           4      h	  VP                  P6                   EFg  pVP                  V9   Ed2   VP                  pVVP                  ,          pVP                  P                  P                  VP                  8w  dz   Tp
V
R	,           p	\         P"                  ! R
V	V	.V
.VP                  R7      pVP                  VP                  P                  n        VP%                  V.4       T	pWVP                  &   \'        VP                  P                  P(                  P*                  VP*                  4      '       Eg#   \9        \/        VP                  P                  P(                  P*                  4      4      pTpVR,           p\         P"                  ! RVV.V.VR7      pVP%                  V.4       WVP                  &   \1        \/        VP                  P                  P(                  P*                  4      4       F[  pVVP                  ,          P*                  V,          VP                  P                  P(                  P*                  V,          n        K]  	  EKD  EKG  \5        RVP                  ,           R,           4      h	  VP                  P:                   F  p\=        VP                  4       F%  w  ppVV9   g   K  VV,          VP                  V&   K'  	  \=        VP6                  4       F%  w  ppVV9   g   K  VV,          VP6                  V&   K'  	  K  	  VP                  P:                  P%                  V4       \?        VP                  4       \        P@                  ! W1P                  PC                  RR4      4       R# )zbInsert Cast, Transpose nodes into Onnx model to make it aligned with QNN generated context binary.z-mz--onnx_modelz"Required. Path to Onnx model file.T)helprequiredrK   z-qz
--qnn_jsonz4Required. Path to Qnn converted model_net.json file.	_qnn_castCast)r   inputsoutputsto
_qnn_trans	Transpose)r   rs   rt   rj   zError: Onnx model input: z  not exist from QNN model input.zError: Onnx model output: z! not exist from QNN model output.z.onnxz_add_trans.onnxN)"r   add_argumentstr
parse_argsrY   rU   onnxrP   
onnx_modelr   r   r   rK   tensor_type	elem_typer.   r   	make_noder   rc   shaper/   rk   r   rb   r\   AssertionErrorr   rm   r   r   r(   savereplace)parserargsrS   modelnodes_to_addgraph_input_output_name_dicgraph_inputinput_name_fater_node_insertqnn_input_tensorcast_input_namecast_output_nameinput_cast_nodetranspose_permtranspose_input_nametranspose_output_nameinput_transpose_noder]   graph_outputoutput_name_after_node_insertqnn_output_tensoroutput_cast_nodeoutput_transpose_noder   node_input_index
node_inputnode_output_indexnode_outputs                              r   mainr      s   lF n3Wbfmpq
l!Wbfmp   D #%'BCIIdoo&EL"${{((::+6+;+;(:;K;KL++559I9X9XX">#2[#@ "("2"2)+,-."''33==# :J9X9X  ,,6##_$56/?,@PK,<,<=4[5E5E5Q5Q5W5W5[5[]m]q]qrr!:3{?O?O?[?[?a?a?e?e;f!g'C$(<|(K%'-'7'7.0123'($ ##%9$:;@UK,<,<= s;#3#3#?#?#E#E#I#IJKAJZJ^J^_`JaK$$0066::1=G L! s& !!<{?O?O!ORt!tuuS )V ** ;;,8,=,=) ;L<M<M N  ,,66:K:Z:ZZ#@ "2["@#)#3#3(+,-.(77$  ;L:Z:Z!!--7##%5$670?-APL,=,=>4\5F5F5R5R5X5X5\5\^o^s^stt!9#l>O>O>[>[>a>a>e>e:f!g(E%'<|'K$(.(8(8-0123')% ##%:$;<AUL,=,=> s<#4#4#@#@#F#F#J#JKLAKf$))Lc!LL%%1177;;A>H M! u* !!=@Q@Q!QTw!wxxW +Z   ,5djj,A(j88/J:/V

+, -B
 /8.D*{991L[1Y-. /E ! 
KKL)5;;' 	IIe__,,W6GHIr3   __main__)rO   argparser   r{   r   r   r(   r*   rH   rY   rc   rk   rm   r   r4   r	   r3   r   <module>r      sZ     #  $1$h #%L0U
{J| zF r3   