
    i"                         S SK r S SKrS SKrS SKJrJrJr  S SKrS SKrS SK	r	S SK
Jr  S SKJrJrJr  S SKJr  S1rSrSrS	rS
1r1 SkrS\	R0                  R2                  SS4S jrS rS r\ " S S\5      5       rg)    N)DictfinalList)NamedDataStore)BackendDetailsExportedProgramPreprocessResult)CompileSpecImportForeverExtractSharedBlobKey      platform-config>   mt6989mt6991mt6993edge_graph_modulereturnc           
         U R                   R                   H  nUR                  S:w  a  M  UR                  R	                  SS 5      nUc  M6  [        USS 5      c  MF  [        [        UR                  5       5      5      nUR                  5       U:w  d  M~  [        SU SUR                  S   R                  5        SU S35      e   g )Nplaceholderval	dim_orderzZNeuropilot backend only supports contiguous memory format for inputs.Expecting dim_order: z
, but got z for a placeholder node .)graphnodesopmetagetgetattrtuplerangedimr   RuntimeError)r   nodetdefault_dim_orders       f/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/backends/mediatek/preprocess.pyassert_default_dim_orderr(       s    !''--77m# IIMM%&=WQT:F %eAEEGn 5{{} 11",,=+>jyy'11344LTFRSU  .    c                 j    [         R                  " S[        XU5      n[        U5      [        :X  d   eU$ Nz<BIII)structpackHEADER_VERSIONlenHEADER_SIZE)
num_inputsnum_outputsmodel_bytes_sizeheader_bytess       r'   _pack_headerr5   1   s5    ;;:JL |+++r)   c                     [        U 5      [        :X  d   e[        R                  " SU 5      u  pp4U[        :X  d   eX#U4$ r+   )r/   r0   r,   unpackr.   )r4   versionr1   r2   buffer_sizes        r'   _unpack_headerr:   9   sC    |+++4:MM'<4X1Gn$$$K//r)   c            
           \ rS rSr\S\S\\   S\4S j5       r	\S\
\\\   4   S\
\\\\      4   S\
\\\   4   4S j5       rS	rg
)NeuropilotBackend@   edge_programmodule_compile_specr   c                    U Vs/ s H  o3R                   PM     nn[        U5      [        [        U5      5      :w  a  [        S5      e[        R                  [        U5      5      (       d  [        SR                  [        5      5      eU Vs/ s H0  nUR                   S:X  d  M  UR                  R                  S5      PM2     snS   nU[        ;  a  [        SR                  U[        5      5      e[        UR                  5        UR                  R                   Vs0 s H  ofR                  U_M     nnUR                   R"                  nUR                   R$                  n	['        U5       V
Vs/ s H6  u  pX{   R(                  S   R*                  [,        R.                  :X  d  M4  U
PM8     nn
n['        U	5       V
Vs/ s H6  u  pX{   R(                  S   R*                  [,        R.                  :X  d  M4  U
PM8     nn
nSS	/nU H  nUR                   [0        ;   a  M  UR                   [2        :X  a  UR5                  S
5        M@  UR                  S:X  a   UR5                  SUR                    35        Mp  UR                  R                  S5      nUR5                  SUR                    SU 35        M     [6        R8                  R;                  U5      nSUl        S Ul        SUl         SUl!        UUl"        SUl#        UUl$        [J        RL                  " S 5         URO                  5       n[P        RR                  " USRU                  U5      5      nS S S 5        [        U5      n[        U	5      n[W        UU[        W5      5      n[Y        [[        UU-   5      S9$ s  snf s  snf s  snf s  snn
f s  snn
f ! , (       d  f       Nj= f)Nz8Unsupported duplicated keys in the CompileSpec settings.z<Following keys are required in the CompileSpec settings: {}.r   utf-8r   zXUnsupported value of platform-config CompileSpec. Given {} but expected to be one of {}.r   z--relax-fp32z--opt=3z--dla-opt=0r)   z--=T )processed_bytes).keyr/   setr#   REQUIRED_COMPILE_SPEC_KEYSissubsetformatvaluedecodeSUPPORTED_PLATFORM_CONFIGS
ValueErrorr(   graph_moduler   r   namegraph_signatureuser_inputsuser_outputs	enumerater   dtypetorchfloat32SKIP_COMPILE_SPEC_KEYSEXTRACT_SHARED_BLOB_KEYappendmtk_converterPyTorchV2Converterfrom_exported_programquantizeinput_quantization_bitwidths!allow_missing_quantization_rangesprepend_input_quantize_ops"prepend_input_quantize_ops_indicesappend_output_dequantize_ops$append_output_dequantize_ops_indices
contextlibredirect_stdoutconvert_to_mlir
mtk_neuroncompilejoinr5   r	   bytes)clsr>   r?   speccompile_spec_keysplatformr$   name_to_node_mappingsinput_namesoutput_namesidxrO   fp_input_indicesfp_output_indicescompile_optionsrJ   	convertermlir_strmodel_bytesr1   r2   r4   s                         r'   
preprocessNeuropilotBackend.preprocessC   sg    3FF2E$XX2EF !S->)?%@@J  *2237H3IJJ645  ,
+xx,, 'DJJg&+
 	
 55*DE  	!!:!:;=I=O=O=U=U V=UTD=U V"22>>#33@@ '{3
3	$*//6<<M 3 	 
 '|4
4	$*//6<<M 4 	 
 *95'Dxx11xx22&&}5 zzS &&DHH:7

))'2&&DHH:Qug'>? ( "44JJ<X	!	15	.6:	3/3	,7G	415	.9J	6''- 002H$,,Xsxx7PQK . %
,'#JS=MNl[6P0QRRK G
 !W


< .-s;   NN!%N! N&23N+)N+ 3N17N17N77
Oedge_programscompile_specsc           
         0 nUR                  5        H  u  pEXB;   d
   SU 35       eX$   n[        U5      [        U5      :X  d"   SU S[        U5       S[        U5       35       e/ n[        XV5       H'  u  pU R                  X5      n
UR	                  U
5        M)     XsU'   M     [
        R                  " [        5      n[
        R                  " [        5      n[
        R                  " [        5      nUR                  5        H  u  pN[        U5       H  u  nnS nX$   U    H4  nUR                  [        :X  d  M  UR                  R                  S5      nM6     Uc  ML  UR                  S [         nUR                  [        S  n[        U5      u  nnn[        U5      U:X  d   eUU   R	                  UU45        UU   R	                  U5        UU   R	                  U5        M     M     0 nUR                  5        Hw  u  nn[!        5       n["        R$                  " USS9u  nnUR'                  U[)        U5      5        UR+                  5       UU'   UR-                  5         UR/                  U5        My     UR                  5        Ho  u  nn[        [        UU   UU   5      5       HJ  u  nu  nnUu  nn[1        UU[        U5      5      nUUU   U   l        [)        UU-   5      UU   U   l        ML     Mq     U$ )Nz!Error: missing compile specs for zError: method z has z partitions but only rA   z-e union)options)itemsr/   zipry   rY   collectionsdefaultdictlistrS   rE   rX   rJ   rK   rD   r0   r:   r   rg   extract_shared_dataadd_named_datarj   get_named_data_store_outputclearextendr5   data_store_output) rk   r{   r|   preprocess_resultsmethod_nameprogramscompile_specs_for_methodresults_for_methodprogramcompile_spec_for_programpreprocess_result
infos_dictmodels_dictresult_dictmethod_resultsrr   resultshared_blob_keyrl   r4   rx   r1   r2   r3   data_store_output_dictrE   modelsndmblob
new_modelsr   
model_infos                                    r'   preprocess_multimethod(NeuropilotBackend.preprocess_multimethod   s#     %2%8%8%:!K,A2;-@A,'4'A$/0C5  v}E#h-@UVYZrVsUtuv  "$5861 %(NN7$U!"))*;<	6 /A{+ &;$ !,,T2
!--d3!--d3+=+C+C+E'K(8V"&)6s;Dxx#::*.***;*;G*D < #*%55l{C$44[\B<J<<X9
K)9;'+;;;;?+22J3LMO,33K@O,33F;  9 ,F$ "$&,,.KC "C)==
 D* sE$K0*-*I*I*K"3'LLNMM*% / '=&B&B&D"C"2;JsO[%563..j+ +5'
K+JSEUV:KC %78= ;.9C %53 'E "!r)    N)__name__
__module____qualname____firstlineno__classmethodr   r   r
   r	   ry   r   strr   r   __static_attributes__r   r)   r'   r<   r<   @   s     JS*JSAEkARJS	JS JSX E"Co!667E" Cd;&7!889E" 
c4())	*	E" E"r)   r<   )r   rd   r,   typingr   r   r   rZ   rg   rU   ,executorch.exir._serialize._named_data_storer   'executorch.exir.backend.backend_detailsr   r   r	   +executorch.exir.backend.compile_spec_schemar
   rW   rX   r0   r.   rG   rL   fxGraphModuler(   r5   r:   r<   r   r)   r'   <module>r      s       $ $    G 
 D)* 0 /0 ; 0D0D  "0 U" U" U"r)   