
    i                        S SK r S SKrS SKrS SKJrJrJrJr  S SKJ	s  J
r  S SK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\R:                  S\S\\    4S jjr!  SS\S\S\\\RD                        S\\    S\4
S jjr#   SS\S\S\ S\$S\$SS4S jjr%    SS\S\S\\\RD                        S\\    S\$S\$S\4S jjr&S\\'\RP                  \\RD                  4   S\R4                  4S jr)g)     N)AnyOptionalSequenceUnion)utils)CadenceETDump)Executor)ExecutorchProgramManager)deserialize_pte_binary)DataLocation)ndarray)TreeSpecprogramexecution_plan_idreturnc                 *   U R                   U   R                   Vs1 s H  nUR                   SUR                   3iM      nnU R                   U   R                   H  n[
        R                  " SUR                   35        UR                  S:X  d  M8  UR                  R                  [        R                  :X  d   eU[        [        U R                  UR                  R                     R                   5      R"                  5      -  nM     U$ s  snf )z*
Get the list of operators from a Program
.z
Delegate: CadenceExecutorchBackend)execution_plan	operatorsnameoverload	delegatesloggingdebugid	processedlocationr   INLINEget_op_namesr   backend_delegate_dataindexdatar   )r   r   opop_namesdelegates        j/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/backends/cadence/runtime/runtime.pyr    r        s     (():;EEEB 77)1R[[M"E   **+<=GG
8;;-01;;44%%..,2E2EEEE&11(2D2D2J2JKPP' H	 H Os   %Dexecutorch_proginputsref_outputsworking_dirc                    U R                   nU R                  5       R                  R                  n[	        U[
        R                  5      (       d   S[        U5       S35       eUc  [        R                  " SS9n[        U5      nU" 5         [        US9nUR                  5       nUR                  5         [	        U[        5      (       d   e[        R                   R"                  R%                  X5      nU$ )Nzprogram must be Program. Got 	 instead.z/tmp)dir)
output_dir)executorch_programexported_program	call_specout_spec
isinstance	et_schemaProgramtypetempfilemkdtempr	   r   get_outputsprint_summaryr   torchr   _pytreetree_unflatten)	r(   r)   r*   r+   r   r3   executoretdumpoutputss	            r'   runrB   6   s     00G//1;;DDH""  @	&tG}oY?@  &&62 $H Jk2F  "G h))))kk!!00CGN    rA   r   	eps_erroreps_warnc                 z   [        U[        5      (       a/  U R                  5        H  u  pV[        XaU   U SU 3X45        M     g [        U[        [
        45      (       a3  [        [        X5      5       H  u  nu  p[        XU SU 3X45        M     g [        U[        R                  5      (       d   S[        U5       S35       e[        U5      n[        U 5      n [        R                  " X5      n
[        R                  " X5      n[        R                  " X5      n[        R                   " X5      nSU
< SU< SU< SUS S	U R"                  < S
U R$                   SUR"                  < S
UR$                   S3n[&        R(                  " U
5      (       d  X:  aa  [*        R,                  " SU SU 35        [*        R,                  " SU S35        [*        R,                  " SU  S35        [/        SU SU 35      eX:  a  [*        R0                  " SU SU 35        g [*        R2                  " SU SU 35        g )N/zGot r-   zrms = z, norm_rms = z, max_abs_diff = z, max_rel_diff = z.2fz%, outputs.shape = [z], ref_outputs.shape = ]z[31m[Error][0m Output z mismatched! z
Expected: 
zGot instead: z[33m[Warning][0m Output z mismatched!. z[32m[Passed][0m Output z
 matched. )r4   dictitemscomparelisttuple	enumeratezipr<   Tensorr7   to_nd_arrayr   rmsnormalized_rmsmax_abs_diffmax_rel_diffshapedtypenpisnanr   errorRuntimeErrorwarninginfo)rA   r*   r   rD   rE   kvioutput
ref_outputrT   norm_rmsrV   rW   statss                  r'   rM   rM   Y   s    +t$$MMODAA1~$q}iJ $+e}--'0W1J'K#A#F4&!yK (Lk5<<00UDk9J8K92UU0k*K'"G ))G
)C##G9H%%g;L%%g;L3(.XM!3L#44F|s6K L==
Agmm_,D0A0A/EQ{GXGXFYYZ	\ 

 
xx}}4TF-wOP
;-r23gYb129$}UGTUU	8nUGTU4TF*UGLMrC   c                 0    [        XX#5      n[        XbXES9  g )N)rD   rE   )rB   rM   )r(   r)   r*   r+   rD   rE   rA   s          r'   run_and_comparerh      s     /;DGGIIrC   ra   c                    [        U [        R                  5      (       a  U $ [        U [        R                  5      (       aO  U R
                  (       a  U R                  5       OU n U R                  5       R                  5       R                  5       $ [        U [        R                  [        45      (       a  [        R                  " U /5      $ [        S[        U 5       35      e)NzUnknown type )r4   rZ   r   r<   rR   is_quantizedint_reprcpudetachnumpynumbersNumberboolarrayr]   r7   )ra   s    r'   rS   rS      s    !RZZ  !U\\""NNAJJLuuw~~%%''!gnnd+,,xx}
tAwi0
11rC   )r   )NN) 皙?h㈵>)NNrt   ru   )*r   ro   r8   typingr   r   r   r   executorch.exir.schemaexirschemar5   rn   rZ   r<   #executorch.backends.cadence.runtimer   *executorch.backends.cadence.runtime.etdumpr   ,executorch.backends.cadence.runtime.executorr	   executorch.exirr
   #executorch.exir._serialize._programr   r   r   torch.utils._pytreer   r6   intsetstrr    rR   rB   floatrM   rh   rq   rp   rS    rC   r'   <module>r      s      1 1 * *   5 D A 4 F /  ()++  CPSH 2 59!%	 -   (5<<01  #	 
 	 L (N(N(N (N 	(N
 (N 
(N\ 59!%	J-	J	J (5<<01	J #		J
 	J 	J 		J25w~~wDE 2"** 2rC   