
    ii                     x   S SK r S SKrS SKJrJrJrJrJr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  SSKJrJrJr  SSK J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)J*r*  SSK+J,r,  \" S5             SS\
\RZ                  \\.\RZ                  4   \\\.\4   \\\.\4   \.4   S\\
\\/\R`                  S4      \\.\\/\R`                  S4      4   4      S\S\\.   S\\
\\\.\4   4      S\\
\\.\4         S\\.   S\1SS4S jj5       r2\" S5       " S S5      5       r3g)    N)AnyCallableDictListOptionalSequenceTupleUnion)EdgeProgramManager)experimental)ExecutorchProgramManager)Program)save_pte_program)tabulate)nn)ExportedProgram)GraphModule   )ExportRecipeLoweringRecipeQuantizationRecipe)
 EdgeProgramManagerTransformStageEdgeTransformAndLowerStageExecutorchStagePipelineArtifactQuantizeStageSourceTransformStageStageToBackendStageToEdgeStageTorchExportStage)	StageTypezfThis API and all of its related functionality such as ExportSession and ExportRecipe are experimental.modelexample_inputs.export_recipenamedynamic_shapesconstant_methodsartifact_dirgenerate_etrecordreturnExportSessionc                 F    [        U UUUUUUUS9nUR                  5         U$ )a  
Create and configure an ExportSession with the given parameters.

This function provides a convenient way to create an ExportSession and
optionally run the export process in one step.

Args:
    model: The PyTorch model(s) to export. Can be:
          - nn.Module or Dict[str, nn.Module]: Eager PyTorch model(s)
          - GraphModule or Dict[str, GraphModule]: Quantized model(s) (e.g., from prepare/convert)
          - ExportedProgram or Dict[str, ExportedProgram]: Already exported model(s)
          - str: Path to load an ExportedProgram from disk
    example_inputs: Example inputs for the model(s), either a list of input tuples
                  or a dictionary mapping method names to lists of input tuples.
                  First sample (index 0) is used for torch.export.export() to export the model.
                  All samples are used as calibration dataset in PT2E Quantize stage.
                  Optional when model is ExportedProgram (not needed).
    export_recipe: Contains the configuration for the export process
    name: Optional name for the export
    dynamic_shapes: Optional dynamic shape specifications
    constant_methods: Optional dictionary of constant methods
    artifact_dir: Optional directory to store artifacts
    generate_etrecord: Optional flag to generate an etrecord

Returns:
    A configured ExportSession instance with the export process completed if requested
)r#   r$   r%   r&   r'   r(   r)   r*   )r,   export)	r#   r$   r%   r&   r'   r(   r)   r*   sessions	            W/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/export/export.pyr.   r.   '   s8    j %#%)!+	G NNN    c                   <   \ rS rSrSr       S)S\\R                  \\	\R                  4   \
\\	\
4   \\\	\4   \	4   S\\\\\R                   S4      \\	\\\R                   S4      4   4      S\S\\	   S	\\\\\	\4   4      S
\\\\	\4         S\\	   S\\   SS4S jjrS\\R                  \
\\4   S\	4S jrS\\   4S jrS\\   S\\\4   4S jrS\S\SS4S jrS\S\\   4S jrS\\\4   4S jrS\\   SS4S jrS*S jrS*S jr S\\\!4   4S jr"S+S\	S\4S jjr#S,S jr$S\%4S jr&S\'4S  jr(S\)4S! jr*S"\	SS4S# jr+ S+S\	S\,\R                   S4   4S$ jjr-  S-S\	S\\,\R                   S4      S\.\   4S% jjr/S*S& jr0S\4S' jr1S(r2g).r,   k   a  
Manages the export process for ExecuTorch models.

This class handles the export process through a pipeline of stages:
1. (Optional) Quantize - Apply post-training quantization to the model
2. Export - Export PyTorch model to ExportedProgram
3. EdgeTransformAndLower - Transform and lower to EdgeProgramManager
4. Executorch - Convert to ExecutorchProgramManager for final execution
Nr#   r$   .r%   r&   r'   r(   r)   r*   r+   c	                 
   [        U[        5      (       a8  [        R                  R	                  U5      n[
        R                  " SU 35        U R                  U5      U l        [        U[        5      (       a  UOSU0U l
        U R                  S:X  aC  U=(       d    0 U l        [        U R                  [        5      (       a  SU R                  0U l        O<Uc  [        SU R                   S35      e[        U[        5      (       a  UOSU0U l        0 U l        Ub%  [        U[        5      (       a  XPl        O	SU0U l        X0l        U R                  R                   U l        U R                  R$                  U l        U R                  R(                  =(       d    U R+                  5       U l        U R/                  U R,                  5      U l        U R                  U R                  UU R                  UUUS.U l        0 U l        g)a  
Initialize the ExportSession with model, inputs, and recipe.

Args:
    model: The PyTorch model(s) to export. Can be:
          - nn.Module or Dict[str, nn.Module]: Eager PyTorch model(s)
          - GraphModule or Dict[str, GraphModule]: Quantized model(s)
          - ExportedProgram or Dict[str, ExportedProgram]: Already exported model(s)
          - str: Path to load an ExportedProgram from disk
    example_inputs: Example inputs for the model(s), either a list of input tuples
                  or a dictionary mapping method names to lists of input tuples.
                  First sample (index 0) is used for torch.export.export() to export the model.
                  All samples are used as calibration dataset in PT2E Quantize stage,
                  Optional when model is ExportedProgram (not needed).
    export_recipe: Contains the configuration for the export process
    name: Optional name for the export
    dynamic_shapes: Optional dynamic shape specifications
    constant_methods: Optional dictionary of constant methods
    artifact_dir: Optional directory to store artifacts
    generate_etrecord: Optional flag to generate an etrecord
zLoaded ExportedProgram from forwardr   Nz*example_inputs are required when model is z6. Only ExportedProgram inputs can omit example_inputs.)r$   r'   r(   r%   session_namer)   r*   )
isinstancestrtorchr.   loadlogginginfo_detect_model_type_input_model_typedict_model_example_inputslist
ValueError_dynamic_shapes_export_recipequantization_recipe_quant_recipelowering_recipe_lowering_recipepipeline_stages_get_default_pipeline_pipeline_stages_build_stages_stage_registry_run_context_stage_to_artifacts)	selfr#   r$   r%   r&   r'   r(   r)   r*   s	            r0   __init__ExportSession.__init__y   s   \ eS!!LL%%e,ELL7w?@ "&!8!8!?  *%66eY<N !!%66#1#7RD $..55(143G3G'H$ % @AWAW@X YK L  nd33 0    "%.$//'5$(1>'B$+ 33 	
 // 	 //O43M3M3O 	
 8<7I7I!!8
 #22"22 0!00 (!2-
 GI r1   c                 X   [        U[        5      (       a3  [        [        UR	                  5       5      5      nU R                  U5      $ [        U[        5      (       a  g[        U[        5      (       a  g[        U[        R                  5      (       a  g[        S[        U5       35      e)z
Detect the type of input model.

Args:
    model: Input model in various formats

Returns:
    String indicating the model type: "nn.Module", "GraphModule", or "ExportedProgram"
r   r   	nn.ModulezUnsupported model type: )r7   r?   nextitervaluesr=   r   r   r   Module	TypeErrortype)rQ   r#   first_values      r0   r=    ExportSession._detect_model_type   s     eT""tELLN34K**;77 e_--${++ ryy))6tE{mDEEr1   c                 D   / nU R                   S:X  a/  UR                  [        R                  [        R                  /5        U R                   S:w  a  UR                  [        R                  5        UR                  [        R                  [        R                  /5        U$ )zz
Get default pipeline stages based on input model type.

Returns:
    List of stages appropriate for the input model type
rU   r   )	r>   extendr"   SOURCE_TRANSFORMQUANTIZEappendTORCH_EXPORTTO_EDGE_TRANSFORM_AND_LOWERTO_EXECUTORCH)rQ   stagess     r0   rK   #ExportSession._get_default_pipeline  s      !![0MM..&& !!%66MM)001 	55''	
 r1   rf   c                 2   0 nSnU=(       d    U R                  5        GH  nU[        R                  :X  a  [        U R                  5      nGOU[        R
                  :X  a  [        U R                  5      nGOU[        R                  :X  aW  SnU R                  R                  b  [        U R                  R                  5      n[        XPR                  R                  S9nGO!U[        R                  :X  a!  [        R                  " U R                   5      nOU[        R"                  :X  a!  [$        R                  " U R                   5      nOU[        R&                  :X  a!  [(        R                  " U R                   5      nOU[        R*                  :X  a!  [,        R                  " U R                   5      nOMU[        R.                  :X  a   [1        U R                  R2                  5      nO[4        R6                  " U S35        U(       d  GM  X2U'   GM     U$ )z/Build the stage registry from the given stages.N)strictz> is unknown, you have to register it before executing export())rK   r"   r`   r   rG   ra   r   rc   rE   aten_transform_passesrB   r!   ri   rd   r   from_reciperI   TO_EDGEr    EDGE_PROGRAM_MANAGER_TRANSFORMr   
TO_BACKENDr   re   r   executorch_backend_configr;   r<   )rQ   rf   stage_registrystage
stage_typerj   s         r0   rM   ExportSession._build_stages(  s   13 @D$>$>$@@JY777,T-?-?@y111%d&8&89y555(,%&&<<H,0++AA-) ))2E2E2L2L yDDD2>>t?T?TUy000#//0E0EFyGGG8DD)) y333&2243H3HIy666'(;(;(U(UV!l"`a u-2z*A AB r1   rr   rq   c                      X R                   U'   g)z
Register a new stage or override an existing stage implementation.

Args:
    stage_type: The type of stage to register
    stage: The stage instance to register
NrN   )rQ   rr   rq   s      r0   register_stageExportSession.register_stageP  s     ,1Z(r1   c                 8    U R                   R                  U5      $ )z
Get a registered stage by its type.

Args:
    stage_type: The type of stage to retrieve

Returns:
    The registered stage instance, or None if not found
)rN   get)rQ   rr   s     r0   get_registered_stage"ExportSession.get_registered_stageZ  s     ##''
33r1   c                     U R                   $ )z\
Get all registered stages.

Returns:
    Dictionary mapping stage types to stage instances
ru   rQ   s    r0   get_all_registered_stages'ExportSession.get_all_registered_stagesf  s     ###r1   c                    U(       d  [        S5      eU R                  S:X  a`  [        R                  [        R                  1n[        U5      U-  nU(       a*  SR                  S U 5       5      n[        SU SU S35      eO~U R                  S:X  an  [        R                  [        R                  [        R                  1n[        U5      U-  nU(       a*  SR                  S	 U 5       5      n[        SU S
U S35      eUS   nU R                  R                  U5      nUc  [        SU S35      eUR                  (       d  [        SU S35      e[        S[        U5      5       Hl  nX   nXS-
     n	U R                  R                  U5      n
U
c  [        SU S35      eU
R                  nU(       d  MP  X;  d  MW  [        SU	 SU SU SU 35      e   g )NzPipeline stages cannot be emptyr   z, c              3   8   #    U  H  oR                   v   M     g 7fNr&   .0ss     r0   	<genexpr><ExportSession._validate_pipeline_sequence.<locals>.<genexpr>|       'K8J18J   zCannot run zK stage(s) with GraphModule input. GraphModule is already quantized. Remove z- from pipeline_stages or use nn.Module input.r   c              3   8   #    U  H  oR                   v   M     g 7fr   r   r   s     r0   r   r     r   r   zR stage(s) with ExportedProgram input. ExportedProgram is already exported. Remove z9 from pipeline_stages or use nn.Module/GraphModule input.r   Stage zB not found in registry, register it using session.register_stage()z cannot start a pipeline. r   zD not found in registry, , register it using session.register_stage()zInvalid transition from z to z. Valid predecessors for z: )rC   r>   r"   r`   ra   setjoinrc   rN   ry   can_start_pipelinerangelenvalid_predecessor_stages)rQ   rf   incompatible_stagesfound_incompatiblestage_namesfirst_stagefirst_stage_instanceicurrent_stageprevious_stagestage_instancevalid_predecessorss               r0   _validate_pipeline_sequence)ExportSession._validate_pipeline_sequenceo  s'    >?? !!]2#,#=#=y?Q?Q"R!$V/B!B!"ii'K8J'KK !+ /)]*WY  " ##'88 **""&&#
 "%V/B!B!"ii'K8J'KK !+ /)]*ce  Qi#3377D'%gh  $66vk]2LMNN q#f+&A"IM#E]N "1155mDN% ]O+op  "0!H!H "!n&N .~.>d=/ R..;_B?Q>RT  'r1   c                 L   U R                  U R                  S9  [        U R                  U R                  S9nU R                   H  nU R
                  R                  U5      nUc  [        SU S35      e[        R                  " SU 35        [        R                  " 5       nUR                  U5        [        R                  " 5       U-
  S-  nUR                  5       nUR                  S[        U5      5        [        R                  " SU S35        XR                   U'   M     g )	N)rf   )datacontextr   z not found in registryzExecuting stage: i  duration_msz execution done)r   rL   r   r@   rO   rN   ry   rC   r;   r<   timeperf_counterrunget_artifactsadd_contextintrP   )rQ   current_artifactrr   rq   startelapseds         r0   _run_pipelineExportSession._run_pipeline  s   (((( 	) 	
 ,dFWFWX //J((,,Z8E} 6*5K!LMMLL,ZL9:%%'EII&'((*U2d:G$224((GELL6*_=>3C$$Z0 0r1   c                 $    U R                  5         g)a  
Execute the full export process.

This method orchestrates the export process with optional quantization:
1. (Optional) Apply quantization to the model
2. Export the PyTorch model to ExportedProgram
3. Transform and lower to EdgeProgramManager
4. Convert to ExecutorchProgramManager
N)r   r}   s    r0   r.   ExportSession.export  s     	r1   c                     U R                   $ r   )rP   r}   s    r0   get_stage_artifacts!ExportSession.get_stage_artifacts  s    '''r1   method_namec           	         U R                   R                  [        R                  5      nUb  UR                  c  [        S5      eUR                  nX;  a(  [        SU S[        UR                  5       5       35      eX1   $ )aa  
Get the ExportedProgram for a specific method after torch export.

Args:
    method_name: Name of the method to get exported program for, defaults to "forward"

Returns:
    The ExportedProgram for the specified method

Raises:
    RuntimeError: If torch export stage has not been run
    KeyError: If the method name is not found in exported programs
z@Exported program is not available. Run Torch Export Stage first.Method name 'z5' not found in exported programs. Available methods: )	rP   ry   r"   rc   r   RuntimeErrorKeyErrorrB   keys)rQ   r   artifactexported_programss       r0   get_exported_program"ExportSession.get_exported_program  s     ++//	0F0FGx}}4R  %MM/} -&&*+<+A+A+C&D%EG 
 !--r1   c                 J   [         R                  [         R                  [         R                  [         R                  4 HW  nU R
                  R                  U5      nUc  M#  UR                  c  M2  [        R                  " SU 35        UR                  s  $    [        S5      e)a  
Get the EdgeProgramManager after edge lowering stages.

This method checks multiple stages in order of preference:
1. TO_EDGE_TRANSFORM_AND_LOWER (combined stage)
2. TO_BACKEND (separate stage with backend delegation)
3. EDGE_PROGRAM_MANAGER_TRANSFORM (separate stage after TO_EDGE)
4. TO_EDGE (separate stage without backend delegation)

Returns:
    The EdgeProgramManager

Raises:
    RuntimeError: If no edge stage has been run
z*Returning edge program manager from stage zEdge program manager is not available. Run one of the edge stages first: TO_EDGE_TRANSFORM_AND_LOWER, TO_EDGE, EDGE_PROGRAM_MANAGER_TRANSFORM, or TO_BACKEND.)r"   rd   rn   rm   rl   rP   ry   r   r;   r<   r   )rQ   rr   r   s      r0   get_edge_program_manager&ExportSession.get_edge_program_manager  s    $ 11  44	
J //33J?H#(AI*VW}}$
 E
 	
r1   c                 6    U R                  5       R                  $ )z
Get the ExecutorchProgram from the ExecutorchProgramManager.

Returns:
    The ExecutorchProgram

Raises:
    RuntimeError: If the executorch program manager is not initialized
)get_executorch_program_managerexecutorch_programr}   s    r0   get_executorch_program$ExportSession.get_executorch_program  s     224GGGr1   c                     U R                   R                  [        R                  5      nUb  UR                  c  [        S5      eUR                  $ )z
Get the ExecutorchProgramManager.

Returns:
    The ExecutorchProgramManager

Raises:
    RuntimeError: If the executorch program manager is not initialized
zJExecutorch program manager is not initialized. Run Executorch Stage first.)rP   ry   r"   re   r   r   )rQ   r   s     r0   r   ,ExportSession.get_executorch_program_manager&  sK     ++//	0G0GHx}}4\  }}r1   c                 6    U R                  5       R                  $ )z
Get the PTE buffer as bytes.

Returns:
    The PTE buffer as bytes

Raises:
    RuntimeError: If the executorch program manager is not initialized
)r   bufferr}   s    r0   get_pte_bufferExportSession.get_pte_buffer7  s     224;;;r1   output_namec                 T    U(       d   S5       e[        U R                  5       U5        g)zc
Save the model to a .pte file.

Args:
    output_name (Optional[str]): The name of the .pte file.
zNeed a valid output nameN)r   r   )rQ   r   s     r0   save_to_pteExportSession.save_to_pteC  s%     666{<<>Lr1   c                     XR                   ;  a  [        SU S35      eU R                   U   nU(       d  [        SU S35      eU R                   U   S   $ )aD  
Get the example input for a specific method.

Args:
    method_name: Name of the method to get example input for, defaults to "forward"

Returns:
    Tuple of tensors representing the example input

Raises:
    KeyError: If the method name is not found in example inputs
    ValueError: If the example inputs list is empty
r   z' not found in example inputszExample inputs list for method z	 is emptyr   )rA   r   rC   )rQ   r   example_inputs_lists      r0   get_example_inputExportSession.get_example_inputM  sh      222];-7TUVV #22;?">{m9UVV ##K033r1   c                 P    SSK JnJn  UR	                  5       nUR                  U R                  5       UR                  S9nUR                  U5      nUc  [        SU S35      eUc  U R                  U5      nUR                  U5      $ ! [         a  n[        S5      UeSnAff = f)a.  
Run a specific method with the given inputs.

Args:
    method_name: Name of the method to run, defaults to "forward"
    example_inputs: Optional inputs to use, defaults to the example inputs

Returns:
    The outputs of the method execution

Raises:
    RuntimeError: If the method cannot be loaded
r   )RuntimeVerificationzexecutorch.runtime is not available. In OSS: Please ensure executorch is properly installed via pip. In fbcode: Please add //executorch/runtime:runtime to your dependencies.N)verificationzFailed to load method 'z' from the program)executorch.runtimer   r   ModuleNotFoundErrorry   load_programr   Minimalload_methodr   r   execute)	rQ   r   r$   r   r   e
et_runtimeprogramr5   s	            r0   
run_methodExportSession.run_methodh  s    &	@ [[]
))!0D0D * 
 %%k2?)+6HI  !!33K@N~..) # 	%[ 		s   B
 

B%B  B%c                    [        [        U R                  5      [        R                  [        R
                  1-  5      nU(       d  [        S5        U R                  R                  US   5      nUc  [        S5        UR                  SS5      nU(       a>  [        UR                  5       5        UR                  5       n[        [        USSS95        g[        S	5        g)
z8
Print delegation information for the exported program.
zRNo delegation info available, atleast one of the lowering stages should be presentr   Nz:No delegation info available, run the lowering stage firstdelegation_infor   
fancy_grid)headerstablefmtzNo delegation info available)rB   r   rL   r"   rd   rn   r   rP   ry   get_contextprintget_summary!get_operator_delegation_dataframer   )rQ   lowering_stagestage_artifactr   dfs        r0   print_delegation_info#ExportSession.print_delegation_info  s     %%&44i6J6JKL
 d 1155nQ6GH!UV )445FM/--/0 BBDB(2vEF01r1   c                 >    U R                  5       R                  5       $ )z
Get the etrecord from the ExecuTorchProgramManager.

Returns:
    The etrecord in the ExecuTorchProgramManager

Raises:
    RuntimeError: If the ExecuTorchManager is unavailable, or etrecord is not available in the ExecuTorchProgramManager
)r   get_etrecordr}   s    r0   r   ExportSession.get_etrecord  s     224AACCr1   )rD   rA   rE   r>   rI   r@   rL   rG   rO   rN   rP   NNNNNNF)r+   N)r5   )r+   r   )r5   N)3__name__
__module____qualname____firstlineno____doc__r
   r   rY   r   r8   r   r   r   r   tupler9   Tensorr   r   r   boolrR   r=   r"   rK   r   rM   rv   rz   r~   r   r   r.   r   r   r   r   r   r   r   r   bytesr   r   r	   r   r   r   r   r   __static_attributes__ r1   r0   r,   r,   k   s	   4 &*"?CAE&*,1-rIIIbii k!"o%&
rI !U5<<,-.S$uU\\3%67889;
rI" $#rI$ sm%rI& !sDcN':!;<'rI( #5c8m)<#=>)rI* sm+rI, $D>-rI. 
/rIhF299k?DHIF	F6tI @&DO &Y=M8N &P1 15 1T 1
4y 
4Xe_ 
4$4	50@+A $AYA 
AFD4(T)5E*E%F (. .O .:
B
H 
H0H "
< 
<Ms Mt M "+44	u||S 	!4: %=A)/)/ !u||S'8!9:)/ 
#	)/V26
Dc 
Dr1   r   )4r;   r   typingr   r   r   r   r   r   r	   r
   r9   executorch.exirr   executorch.exir._warningsr   executorch.exir.programr   executorch.exir.schemar   &executorch.extension.export_util.utilsr   r   r   torch.exportr   torch.fxr   reciper   r   r   rf   r   r   r   r   r   r   r   r   r    r!   typesr"   rY   r8   r   r   r   r.   r,   r   r1   r0   <module>r	     s     N N N  . 2 < * C   (   D D    l$ 	"&;?=A"&#+>
		S"))^S+S/!"	> u||S()*d5s!234457	
>   !>" 3-#>$ U3S#X#678%>& uT#x-%89:'>( 3-)>* +>, ->>B lJ	D J	DJ	Dr1   