
    i3>                     
   % S SK r S SKJr  S SKJr  S SKrS SKrS SKJ	r
JrJr  S SKJrJr  S SKJr  S SKJrJr  S SKJrJr  S S	KJr  S S
KJrJrJrJr  S SK J!r!  S SK"J#r#  S SK$J%r%J&r&  S SK'J(r(  S SK)J*r*  SSK+J,r,J-r-  SSK.J/r/  \" 5       r0  S=S\Rb                  Rd                  S\3\4S4   S\5S\\6\Rn                  Rp                        S\(4
S jjr S>S\Rb                  Rd                  S\3\4S4   S\S\5S\Rr                  Rt                  4
S jjr; S>S\(S\S\5S\Rr                  Rt                  4S jjr< S>S\Rr                  Rt                  S\5S\Rr                  Rt                  4S jjr=S \(S\S\(4S! jr>  S?S\Rb                  Rd                  S\3\4S4   S\S"\\6\3\4S4         S\5S\Rr                  Rt                  4S# jjr?    S@S\Rb                  Rd                  S\3\4S4   S\\   S"\\6\3\4S4         S\5S$\\6\@      S\(4S% jjrA\R                  R                  R                  R                  \R                  R                  R                  R                  \R                  R                  R                  R                  \R                  R                  R                  R                  \R                  R                  R                  R                  \R                  R                  R                  R                  \R                  R                  R                  R                  \R                  R                  R                  R                  /rM\6\Rn                  Rp                     \NS&'   \R                  R                  R                  R                  /rO\6\Rn                  Rp                     \NS''      SAS(\(S\5S)\\P\@\44      S*\\6\Rn                  Rp                        S\4
S+ jjrQ   SAS\Rb                  Rd                  S\3\4S4   S\5S)\\P\@\44      S*\\6\Rn                  Rp                        S\4S, jjrR     SBS\Rb                  Rd                  S\3\4S4   S\\   S\5S)\\P\@\44      S"\\6\3\4S4         S*\\6\Rn                  Rp                        S\4S- jjrS  SCS\(S\5S.\TS\4S/ jjrU  SCS\Rb                  Rd                  S\3\4S4   S\5S.\TS\4
S0 jjrV  SCS\Rb                  Rd                  S\3\4S4   S\5S.\TS\4
S1 jjrW       SDS\Rb                  Rd                  S\3\4S4   S2\\@   S.\TS3\TS4\5S5\5S6\\   S\5S\4S7 jjrX       SDS8\(S2\\@   S.\TS3\TS4\5S5\5S6\\   S\5S\4S9 jjrYS:\S;\S2\SS4S< jrZg)E    N)Path)Optional)prepareQuantizedInputWrappertrace)CadenceMemoryPlanningprint_memory_planning_info)QuantFusion)CadenceDefaultQuantizerCadenceQuantizer)get_default_memory_configMemoryConfig)generate_etrecord)EdgeCompileConfigEdgeProgramManagerExecutorchBackendConfigExecutorchProgramManager)ToOutVarPass)ConstraintBasedSymShapeEvalPass)
_transformto_edge)ExportedProgram)convert_pt2e   )apply_exir_ops_passesapply_torch_ops_passes)print_ops_infomodelinputs.dump_graphsops_to_keepreturnc                     Uc  / n[        XSSUS9nU(       aN  [        R                  " S5        [        R                  " UR                  R                  R                  5       5        U$ )z<
Trace the model with export and return an ExportedProgram.
FT)is_qatstrictr!   zGraph before quantization:)trace_fnlogginginfograph_modulegraphprint_tabular)r   r   r    r!   programs        g/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/backends/cadence/aot/compiler.pyr   r   4   sZ     eDkG 12W))//==?@N    	quantizerc                 N    UR                  5       n[        XX4S9n[        XRUS9nU$ )aO  
Trace and Prepare a model using the given quantizer.
The quantizer must be supplied and be the same as the one used to
fuse the model later, if applicable. If you do not expect that behavior,
please use quantize_pt2 instead, which will instantiate a
default quantizer for you if needed.
Returns a GraphModule with the prepared model.
r    r!   r    )&get_ops_to_preserve_from_decompositionr   prepare_traced_pt2)r   r   r/   r    r!   traced_programprepared_programs          r-   prepare_pt2r7   J   s;     BBDK;N *{ r.   r,   c                     [        XSS9nU(       aD  [        R                  " S5        [        R                  " UR                  R	                  5       5        U$ )aE  
Prepare a model using the given quantizer.
The quantizer must be supplied and be the same as the one used to
fuse the model later, if applicable. If you do not expect that behavior,
please use quantize_pt2 instead, which will instantiate a
default quantizer for you if needed.
Returns a GraphModule with the prepared model.
F)r$   zGraph after preparation:)
prepare_fnr'   r(   r*   r+   )r,   r/   r    prepared_models       r-   r4   r4   d   sA      5AN/0^))779:r.   r)   c                     [        U 5      nU(       aD  [        R                  " S5        [        R                  " UR                  R	                  5       5        U$ )zC
Convert the model
Returns a GraphModule with the converted model.
zGraph after convert:)r   r'   r(   r*   r+   )r)   r    converted_models      r-   convert_pt2r=   {   s?     #<0O+,_**88:;r.   converted_programc                     UR                    Vs/ s H  o"R                  PM     nn[        U [        U5      5      nU$ s  snf )a7  
Fuse a converted exported program using the given quantizer.
The quantizer must be the same as the one used to convert the model.
If you do not expect that behavior, please use quantize_pt2 instead,
which will instantiate a default quantizer for you if needed.
Returns an ExportedProgram with the fused model.
)
quantizerspatternr   r
   )r>   r/   qpatternsfused_programs        r-   apply_pre_edge_transform_passesrE      s?     $-#7#78#7a		#7H80+h2GHM 9s   =calibration_datac                 >   U R                  5         UR                  5       n[        XXES9nU(       aD  [        R                  " S5        [        R                  " UR
                  R                  5       5        [        XbUS9nUc  U/nU H  nU" U6   M
     [        XtS9n	U	$ )Nr1   zGraph after trace:r2   )	evalr3   r   r'   r(   r*   r+   r4   r=   )
r   r   r/   rF   r    r!   r,   prepared_gmsamplesconverted_gms
             r-   get_fake_quant_modelrL      s     
JJLBBDKE{TG)*W]]0023 %W[QK "8#W $ {DLr.   quant_input_argsc                 Z   U(       d
  [        5       n[        U UUUUS9nUb  [        Xe5      n[        R                  R	                  XaSS9n[        Xr5      nU(       aN  [        R                  " S5        [        R                  " UR                  R                  R                  5       5        U$ )a  
Trace, prepare, convert and fuse the model using the given quantizer.
If calibration data is provided, it will be used to calibrate the model. If
not, the inputs will be used for calibration instead, which is useful for
unit tests but should not be used for end-to-end use cases.
Returns an ExportedProgram with the quantized model.
Note: this function should not be called directly in general. Please use
quantize_and_export_to_executorch for most needs.
r/   rF   r    Tr%   z$Graph after quantization and fusion:)r   rL   r   torchexportrE   r'   r(   r)   r*   r+   )	r   r   r/   rF   r    rM   rK   r,   rD   s	            r-   quantize_pt2rS      s    $ +-	 ()L #,\L ll!!,t!DG3GGM;<]//55CCEFr.   TO_EDGE_OP_EXCEPTION_LISTTO_EDGE_PRESERVE_OPSexpo_programconstant_methodscore_aten_exceptionsc           	      2   [        U 5      n [        U [        S[        U=(       d    / -   [        S9US9nU(       a\  [
        R                  " S5        [
        R                  " UR                  5       R                  R                  R                  5       5        U$ )zA
Lower an ExportedProgram to an EdgeProgramManager (in edge IR).
T)_skip_dim_order_core_aten_ops_exception_listpreserve_ops)compile_configrW   zGraph after Edge lowering:)r   r   r   rT   rU   r'   r(   exported_programr)   r*   r+   )rV   r    rW   rX   edge_prog_managers        r-   _lower_ep_to_edger`     s     *,7L  ( *C#)r++-
 *
 12..0==CCQQS	
 r.   c                     [        U [        R                  R                  5      (       d   S5       e[	        X5      n[        XRX45      nU$ )Nzmodel should be an nn.Module)
isinstancerQ   nnModuler   r`   )r   r   r    rW   rX   rV   r_   s          r-   export_to_edgere   &  sL     eUXX__--M/MM- 'L *#3 r.   c                 4    [        U UUUUS9n[        UUUUS9$ )z;
Trace, quantize and lower a model/inputs pair to edge IR.
rO   )r    rW   rX   )rS   r`   )r   r   r/   r    rW   rF   rX   quantized_models           r-   quantize_and_export_to_edgerh   :  s8     #)O )1	 r.   	opt_levelc                 .    [        XS9n[        X#5      nU$ )zV
Lower an existing ExportedProgram to edge IR and apply frontend optimization passes.
r2   )r`   r   )r,   r    ri   r_   cadence_prog_managers        r-   _lower_ep_to_cadencerl   V  s      *'K0Nr.   c                 0    [        XUS9n[        X45      nU$ )Nr2   )re   r   )r   r   r    ri   r_   rk   s         r-   export_to_cadencern   c  s"     'u+N0Nr.   c                 .    [        X5      n[        UUUS9$ )z_
Trace, quantize, lower a model/inputs pair to edge IR and apply frontend
optimization passes.
)ri   r    )rS   rl   )r   r   r    ri   rg   s        r-   quantize_and_export_to_cadencerp   n  s$     #51O r.   
output_dirmem_algoalloc_graph_inputalloc_graph_outputmemory_configc	                 ^    [         R                  R                  XSS9n	[        U	UUUUUUUS9$ )NTrP   )rq   ri   rr   rs   rt   ru   r    )rQ   rR   !_lower_ep_to_cadence_gen_etrecord)
r   r   rq   ri   rr   rs   rt   ru   r    eps
             r-   !export_to_executorch_gen_etrecordry     sB     
		U4		8B,
+-#	 	r.   rx   c           
         [        X5      n[        X(5      n	[        UR                  5       R                  U	R                  5       R                  5        Uc
  [        5       n[        UUUUUS9n
U	R                  [        U
S[        5       S[        5       S95      n[        UUUUU5        U(       a  [        X[        U5      5        U$ [        R                  " S5        U$ )N)ri   rr   rs   rt   F)memory_planning_passemit_stacktraceto_out_var_passextract_delegate_segmentssym_shape_eval_passz:No output directory provided, skipping ETRecord generation)r`   r   r   r^   r)   r   r   to_executorchr   r   r   r	   _gen_etrecordr   r'   warning)rx   rq   ri   rr   rs   rt   ru   r    r_   rk   r{   	exec_progs               r-   rw   rw     s     *":0N **,99--/<<
 130+- +?*L*L!5!(N&+ ? A	
+I  'D4DE  	TUr.   edge_program
et_programc                     US-  n [        UU US9  [        R                  " SU 35        g ! [         a    [        R                  " S5         g f = f)Nzetrecord.bin)	et_recordedge_dialect_programexecutorch_programzGenerated ETRecord at z/Encountered exception while generating ETRecord)r   r'   r(   	Exception	exception)r   r   rq   
etrec_paths       r-   r   r     s[    
 n,J	M !-)	

 	-j\:; MKLMs   $,  AA)FN)F)NF)NNFN)FNN)NFNNN)Fr   )Nr   r   TTNF)[r'   pathlibr   typingr   1executorch.backends.cadence.aot.ops_registrations
executorchrQ   .executorch.backends.cadence.aot.compiler_funcsr   r9   r   r   r&   /executorch.backends.cadence.aot.memory_planningr   r	   5executorch.backends.cadence.aot.quantizer.fusion_passr
   3executorch.backends.cadence.aot.quantizer.quantizerr   r   %executorch.backends.cadence.aot.utilsr   r   executorch.devtoolsr   executorch.exirr   r   r   r   executorch.exir.passesr   *executorch.exir.passes.sym_shape_eval_passr    executorch.exir.program._programr   r   torch.export.exported_programr   'torchao.quantization.pt2e.quantize_pt2er   passesr   r   utilsr   default_quantizerrc   rd   tupleobjectboollist_ops
OpOverloadfxGraphModuler7   r4   r=   rE   rL   strrS   opsaten_linalg_detdefault_linalg_svd#_native_batch_norm_legit_functionallinearlinalg_vector_normunfoldanglerms_normrT   __annotations__rU   dictr`   re   rh   intrl   rn   rp   ry   rw   r    r.   r-   <module>r      sF      8  
 N 2  0 V @ 9 @ A !+-  9=	88??&#+  $uzz4456	
 4 	88??&#+   	
 XX:   XX	2 ((&& XX.& 2 <@88??&#+   tE&#+$678	
  XXH -1;?,0)88??)&#+) ()) tE&#+$678	)
 ) tCy)) )Z 
IINN&&	IINN&&	IINN66>>	IINN!!	IINN%%--	IINN!!	IINN  	IINN##	: 4

 5 56 	 
IINN##5 d5::001  48BF	! tCK01 #4

(=(=#>?	
 L 48BF88??&#+  tCK01	
 #4

(=(=#>? . -148;?BF88??&#+ () 	
 tCK01 tE&#+$678 #4

(=(=#>? < 
 
 
  
  	
   	 88?? &#+    	 
   	88??&#+  	
 , !%"#,088??&#+  	
    L)  : !%"#,0666 6 	6
 6 6 L)6 6 6rM$M(M M 
	Mr.   