
    Ǆg                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl m	Z	 d dl
mZmZmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZ d dlmZmZ d d	lm Z m!Z! d d
l"m#Z# er
d dl$Z$d dl%m&Z& d dl'Z'd dl(m)c m*Z+ d dl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z=m>Z> d dl?m@Z@ d dlAmBZB d dlCmDZD ddlEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZP g dZQee'j                  j                  geeB   f   ZTej                   G d d             ZVej                   G d d             ZWd ZXdeeY   de+j                  deeY   d e+j                  d!e[f
d"Z\d# Z]e'j                  j                  j                  e'j                  j                  j                  e'j                  j                  j                  e'j                  j                  j                  e'j                  j                  j                  e'j                  j                  j                  e'j                  j                  j                  e'j                  j                  j                  e'j                  j                  j                  e'j                  j                  j                  e'j                  j                  j                  e'j                  j                  j                  gZle	d7d$       Zme	d%        Znd&ee'j                  j                  ef   d'ee'j                  j                     d(eer   fd)Zsd*e'j                  j                  d+eId!ee'j                  j                  eIf   fd,Ztd*e'j                  j                  fd-Zud&ee'j                  j                  ef   d'ee'j                  j                     d(eer   fd.Zv G d/ d0      Zwd1 Zx	 d8d*e'j                  j                  d2d3d!d4fd5Zyd6 Zzy)9    N)
namedtuple)contextmanager)AnyCallableDictfinalIteratorListOptionalTupleTypeTYPE_CHECKINGUnion)autograd_not_implemented)FakeScriptObject)#first_call_function_nn_module_stack)_PyTreeCodeGen_PyTreeInfo)immutable_dictimmutable_list)insert_deferred_runtime_asserts)ValueRanges)_collect_and_set_constant_attrs_collect_param_buffer_metadata_detect_fake_mode_from_gm_name_hoo_subgraph_placeholders/_overwrite_signature_for_non_persistent_buffers)_populate_param_buffer_metadata_to_new_gm_rename_without_collisions)Verifierdetect_fake_modeunset_fake_temporarily)FunctionalTensor)is_equivalentreorder_kwargs)compatibility)
PassResult)PassManager   )ArgumentSpecConstantArgumentCustomObjArgumentExportGraphSignature	InputKind	InputSpec
OutputKind
OutputSpecSymIntArgumentTensorArgumentTokenArgument)ExportedProgramModuleCallEntryModuleCallSignaturec                   p    e Zd ZU ee   ed<   ee   ed<   ej                  ed<   ej                  ed<   d Zy)r9   inputsoutputsin_specout_specc                     | j                   D ]-  }|j                  |j                  k(  s|j                  |_        / | j                  D ]-  }|j                  |j                  k(  s|j                  |_        / y N)r;   namer<   )selforiginal_nodenew_nodeios        e/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/export/exported_program.pyreplace_all_uses_withz)ModuleCallSignature.replace_all_uses_with_   sd     	'Avv+++!	'  	'Avv+++!	'    N)	__name__
__module____qualname__r
   r,   __annotations__pytreeTreeSpecrH    rI   rG   r9   r9   X   s1    ,__oo'rI   r9   c                   ,    e Zd ZU eed<   dZee   ed<   y)r8   fqnN	signature)rJ   rK   rL   strrM   rS   r   r9   rP   rI   rG   r8   r8   h   s    	H/3Ix+,3rI   r8   c                 B     t        j                          fd       }|S )Nc                  T    t               5   | i |cd d d        S # 1 sw Y   y xY wr@   r#   )argskwargsfns     rG   wrapperz/_disable_prexisiting_fake_mode.<locals>.wrappero   s+    #% 	't&v&	' 	' 	's   ')	functoolswraps)rY   rZ   s   ` rG   _disable_prexisiting_fake_moder]   n   s%    __R' ' NrI   
spec1_typespec1_context
spec2_typespec2_contextreturnc                     | || |u xr ||k(  S t        | t        t        f      rt        |t        t        f      r||k(  S t        | t        t        f      rt        |t        t        f      r||k(  S | |u xr ||k(  S )zeTreat containers and their immutable variants as the same type. Otherwise
    compare as normal.
    )
issubclassdictr   listr   )r^   r_   r`   ra   s       rG   _fx_collection_equivalence_fnrg   w   s     Z/Z'JM],JJ*t^45*T>*; --*t^45*T>*; --#F(FFrI   c                  &   |d   }|d= t         j                  j                  |j                         t         j                  j                  j
                        sJ  |j                  t         j                  j                  j
                  g| i |S )Nkernel)torch_C%_dispatch_has_kernel_for_dispatch_keyrA   DispatchKeyCompositeImplicitAutograd_op_dk)rW   rX   ri   s      rG   _register_cia_to_metarp      s    HFx8899uxx++EE   6==669=AG rI   c              #   t  K   i }t               }i }| D ]  }d }|r ||       |j                  j                         ||<   |j                  |       t        D ]3  }||j                  vs |j                  |      t        |d             5 t        j                  j                  j                  |j                  v r/|j                  t        j                  j                  j                  = |r<d }	 |j                  t        j                  j                  j                        |	       t        j                  j                  j                  |j                  vrR |j                  t        j                  j                  j                        t        j                  t        |             ||v s||   ||<   ||=  	 d  |D ]T  }
|
j                  j                          |
j                  j!                  ||
          |
j"                  j                          V |j%                         D ]
  \  }
}|||
<    y # |D ]T  }
|
j                  j                          |
j                  j!                  ||
          |
j"                  j                          V |j%                         D ]
  \  }
}|||
<    w xY ww)Nc                    | t         j                  v rt        d|  d      | t         j                  v rt        |  d      t	        | j
                  j                  D cg c]  }|j                  | c}      }|dk7  xs | j
                  j                  }|rt        |  d      t        j                  j                  | j                               st        |  d      yc c}w )NzWe can't detect z7 as a functional op statically, so we can't preserve itz is a metadata query function, it will be preserved implicitly in our tracing system. Please file an issue on github if you see otherwiser   z6 is a mutating/aliasing op, we can't preserve it as isz0 is a TorchScript op, we can't preserve it as isT)r%   maybe_aliasing_or_mutating_opsRuntimeErrormetadata_fnslen_schema	arguments
alias_info
is_mutablerj   rk   _dispatch_has_kernelrA   )op_overloadrE   ry   is_mutating_or_aliasings       rG   assert_valid_to_preservezE_override_composite_implicit_decomp.<locals>.assert_valid_to_preserve   s   .MMM"&{m3jk  .;;;""m $J J  '//99VqQ\\=UVJ '1Ao&W9L9L9W9W#&""m#YZ  88001A1A1CD""m#ST   Ws   C%1C%T)deferred_errorc                      t         S r@   )NotImplemented)rW   rX   s     rG   _z._override_composite_implicit_decomp.<locals>._   s    %%rI   )ri   )set
py_kernelscopyadd(_AUTOGRAD_ALIAS_BACKEND_KEYS_TO_OVERRIDEpy_implr   rj   rk   rm   rn   Metar[   partialrp   clearupdate_dispatch_cacheitems)ops_to_preservedecomp_tablesafesaved_tablespatched_opsremoved_decompsr|   r~   override_dispatch_keyr   opdecomps               rG   #_override_composite_implicit_decompr      sr    " L%KO& D*	< $[1$/$:$:$?$?$A[!$%M 	!$K,B,BB:##$9:,[N	 8899[=S=SS&&uxx';';'U'UV& PK 4 4 N NOPQR 88$$K,B,BB:K 4 4 9 9:!!"7L ,&+7+DOK([)ID*L	& 	'BMM!MM  b!12$$&	'
 *//1 	&JB%L	&  	'BMM!MM  b!12$$&	'
 *//1 	&JB%L	&s-   A'J8*EJ83J8H= A7J8=A8J55J8c               #     K   t        t        j                  j                  j                  j
                  t        j                  j                  j                  j                  fi d      5  d  d d d        y # 1 sw Y   y xY ww)NF)r   )r   rj   opsatentodtype_layoutdtyperP   rI   rG   !_override_decomp_aten_to_variantsr     s_     
 
-				'	'):):)@)@A

 
 	  s   A)B+A90	B9B>Br   _preserve_opsjoint_loss_indexc                2   ddl m} ddlm} ddlm}m}m}m}	m	}
m
} ddlm} | j                  j                  dk(  r@| j                         }g }|j                   j"                  D ]0  }|j$                  dk(  s|j'                  |j(                  d          2 t+        j,                  ||j.                        }t1        |      }| | |       d	
      }|j2                  }|j                   j4                  j6                  j8                  }|j:                  t<        t>        fvrt+        j@                  t>        d |g      }tC        tE        ||j.                  |            |j                   _        |jG                          tI        | jJ                  | jL                  |      } |||      }tO        |      } |       5  |5  tQ               5  tS        ||      5   ||g |d   |d   jU                         i |||d      }d d d        d d d        d d d        d d d        jV                  }|jX                  }t[        |||       t]        | jJ                  |      } |	|        ||        |
||       t_        ||      S | j`                  j                   j"                  D cg c]  }|j$                  dk(  s| }}|D cg c]  }|j(                  d    }}| j`                  jc                         D cg c]  \  }}|	 }}}|D ]  }te        | j`                  |        tg        |      }|ti        jj                         n|} |       5  |5  tS        ||      5   || j`                  |||d	nd||nd       \  }}d d d        d d d        d d d        d }j                   j"                  D cg c]  }|j$                  dk(  s| } }t=        |j                   j"                        d   jl                  d   }!to        |       to        |      k(  sJ tq        ||       D ]  \  }"}#|"jr                  x|#_9        |#_:         | D $ci c]  }$|$jr                  |$jr                   }%}$|j                   j"                  D ]8  }|j$                  dk(  rtw        |%|jr                  |jr                        |_9        : ty        |       ddl=m>}&m?}' ddl@mA}( t        j                  j                  j                  s[d})t        |      }*|*L |'|t        j                  |&|)            5  t        ||*dt        |j                          d	       d d d        |jG                          t         |(|            D ]7  \  }+}t        |!|+   t        j                  j                        s.||!|+   _9        9 t        | jJ                  j                        D +,ci c]C  \  }+},t        |,j                  t              s$|,j                  jr                  | |+   jr                  E }-}+},t        | jJ                  j                        D +,cg c]E  \  }+},t        |,j                   ||,j                  | |+         |,jt                  |,j                        G }.}+},t        | jJ                  j                        D +,cg c]T  \  }+},t        |,j                   ||,j                  |!|+         |-j                  |,jt                  |,jt                              V }/}+},|j                  J |j                  j                  }0to        |j                        to        | jJ                  j                        k(  sJ t        | jJ                  j                        D +,ci c]/  \  }+},t        |,j                  t              r|j                  |+   |,1 }1}+},t        |!to        |/      d        D ]  \  }+}|0|jr                     }2|1|2   },|,j                  t        j                  k(  rt        j                  }3|,jt                  }4nH|,j                  t        j                  k(  rt        j                  }3|2}4nt        d|,j                         |/j'                  t        |3t        |jr                        |4              to        |       to        |      k(  sJ t        |.|/      }tq        ||       D ]  \  }5}6t        |5j(                  d   t        j                        s|5j(                  d   |6j(                  d<   |6jt                  |j                  v s|6jt                  |j                  v sz|5j(                  j                         D ]  \  }7}8|8|6j(                  |7<     ||fS # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY wc c}w c c}w c c}}w # 1 sw Y   IxY w# 1 sw Y   NxY w# 1 sw Y   SxY wc c}w c c}$w # 1 sw Y   xY wc c},}+w c c},}+w c c},}+w c c},}+w )Nr   )aot_export_module)FakeTensorMode)_export_to_aten_ir_fakify_params_buffers_ignore_backend_decomps_verify_nn_module_stack_verify_placeholder_names_verify_stack_trace)ShapeEnvTRAININGplaceholdervalT)	shape_envexportr+   F)r   _check_autograd_state)decompositionstrace_jointoutput_loss_indexc                     t        | t              r| S t        | t              rt        |j                        S t        | t              rt	        |j                        S t        dt        |              )NrA   zType of old_arg not supported: )
isinstancer-   r5   rA   r4   rt   type)old_argnew_phs     rG   
update_argzF_decompose_and_get_gm_with_new_signature_constants.<locals>.update_arg  s[    g/0N0!v{{330!v{{33<T']OLMMrI   )_node_metadata_hook_set_node_metadata_hook)_graph_output_nameszUFile "torch/fx/passes/runtime_assert.py", line 24, in insert_deferred_runtime_asserts)stack_tracezexported program: )r   zUnknown input kind: r   input_specsoutput_specs)htorch._functorch.aot_autogradr   torch._subclasses.fake_tensorr   torch.export._tracer   r   r   r   r   r   %torch.fx.experimental.symbolic_shapesr   verifierdialectmodulegraphnodesr   appendmetarN   tree_unflatten_in_specr   	_out_spec_codegenpytree_info	orig_argsr   rf   tuplerO   r   r   	recompiler   graph_signature	constantsr   r   r   valuesgmsigr   r   !_remove_unneccessary_copy_op_passgraph_modulenamed_buffersdelattrr"   
contextlibnullcontextrW   rv   ziprA   targetr   r   (torch._export.passes._node_metadata_hookr   r   4torch._functorch._aot_autograd.input_output_analysisr   rj   _dynamoconfigdo_not_emit_runtime_asserts_get_shape_envr[   r   r   r   	enumerater   fxNoder   argr-   r1   kind
persistentr   r3   getbackward_signaturegradients_to_user_inputsuser_inputsr5   r0   	PARAMETERr2   GRADIENT_TO_PARAMETER
USER_INPUTGRADIENT_TO_USER_INPUTAssertionErrorr/   Tensorinputs_to_parametersinputs_to_buffersr   )9epr   r   r   r   r   r   r   r   r   r   r   r   mod	fake_argsnodefake_args_unwrapped	fake_moder>   orig_arg_namesconstant_attrsfake_params_buffersparams_buffers_to_node_metaaten_export_artifactr   new_graph_signatureold_placeholdersrA   r   buffers_to_remover   r   new_placeholdersnew_outputsold_phr   phname_mapr   r   r   r   r   rE   specold_new_placeholder_mapr   r   	gradientsspecssourcer   r   old_noderD   kvs9                                                            rG   2_decompose_and_get_gm_with_new_signature_constantsr  #  sU	    @<  ? 
{{j(iik	IIOO 	3Dww-'  5!12	3 %33Is||L-c2	&DII ==++77AA ==u-udXJ?H+
		 	
 9c

 5YD&DS&I#$& 		,.	0S1
	 $6(+(+224 #)&+$ 	 	 	 	( "$$2661'-@	

 N 3
 	 #B!"&9:05HII ..44=8P  /??d5!?I?-/__-J-J-LM'$MM! '&' !+I,5,=
&&(9I	 	" 
I 
/R0 
 0OO' 0 <%+ /
O	
 
 
 N *,T477m;STTrxx~~&r*//2K  C(8$9999.0@A 2&,kk1fm2 ,<<R <H< O77m#.xDIIN	O $B' Y==;;1 	 #2&	 (I%%&9{S  0()LRXX)V(WX	 LLN045 '4k!nehhmm4"&KN' !!3!3!?!?@At$(($45 	'*///  !!3!3!?!?@ At 	IItxx!1!!45KKOO		
K  !!3!3!@!@A At 	IItxxQ0#''T[[A	
L  #11===#66OO	?../3r7I7I7U7U3VVVV %R%7%7%C%CD
4$((N3 ''*D0
 

 !S->-@!AB 	GAttyy)F=DyyI///!77i222!88$';DII;%GHH"		2	&  C(8$9999.l ""24DE 	%((--.=#+==#7HMM%  OO2GGG"5"G"GG ++- %1#$a %	% """K	 	 	 	 	 	 	 	L @M
 
 
 
 
 
2 U =0 $
s   9f&<ff)e?	=fff&f34f3>f83f=	gg#g<gg/g*g*g/&g4Ah5A
h$Ah 4h?f	ffff#	f&&f0ggg	gg'4g>r   r  c                    | j                  |j                               5  | j                  j                  D ]  }|j                  dk(  st        j                  |j                        \  }}|D ]  }t        |t        j                  j                        s(|j                  |j                  v sA|j                  dk(  sQ|j                  t        j                  j                   j"                  j$                  k(  s|j'                  |j                  d          | j                  j)                  |         | j+                          ddd       | |fS # 1 sw Y   | |fS xY w)zQ
    Removes redundant copy_ node that was introduced due to mutated buffer.
    outputcall_functionr+   N)_set_replace_hookget_replace_hookr   r   r   rN   tree_flattenrW   r   rj   r   r   rA   buffers_to_mutater   r   r   r   defaultrH   
erase_noder   )r   r  r   rW   r   outs         rG   r   r   ,  s    
		1BBD	E HHNN 	5Dww(" --dii8a 
5C"36HH(;(M(MM  FFo5 #

eiinn.A.A.I.I I55chhqkBHH//4
5	5 	  """!  """s+   )E
AEE/E?;E;AEE!c           	         | j                  |j                               5  | j                         D ]  }t        |t        j
                  j                        s)i }i }t        |j                  j                        D ]  }|j                  dk(  r|j                  t        j                  k(  r|j                  \  }}||    d| }	|	|v rc|j                  ||	          |D ].  }
|
j                   |
j                   j                  |||	          0 |j                  j#                  |       |||	<   |	||<   |j$                  ||<     	 d d d        y # 1 sw Y   y xY w)Nr  .)r  r  modulesr   rj   r   GraphModulerf   r   r   r   r   operatorgetitemrW   rH   rS   r   rA   )r   r   module_call_graphr   node_idgetitemsr   r  idxnew_identrys              rG    _common_getitem_elimination_passr.  E  sG    
		o>>@	A .jjl 	.Ffehh&:&:;02G13HV\\//0 .77o-$++AQAQ2Q"&))KFC '0#7F)228F3CD%6 "E$: % E E$((6*:!""
 //5+/((.$(IIGDM!.	.. . .s   CE/AEEc          
      d   t        | |||      \  }}|j                  j                  | j                  j                         t	        || j
                        }t        ||j                  || j                  |t        j                  | j                        | j                  | j                        }|S )Nr   r   r   )rootr   r   
state_dictrange_constraintsr(  example_inputsr   )r  r   r   r   _get_updated_range_constraintsr3  r7   r   r2  r   deepcopyr(  r4  r   )r   r   r   r   r   r  new_range_constraintsexported_programs           rG   _decompose_exported_programr9  b  s     Q
!#)	B GGNN2??''(:


 'hh+==/--(<(<=((,,	 rI   c                   6   e Zd ZdZ	 	 d7dddeej                  j                  ee	e
f   f   dej                  j                  dedee	eej                  ej                  j                  f   f   dd	d
ee   deeee
df   ee	e
f   f      deee	eej                  eej*                  j,                  f   f      deeee         fdZe ed      d               Ze ed      d               Ze ed      d               Ze ed      d               Z ed      de ej                  j                     fd       Z! ed      de ee	ej                  j                  f      fd       Z" ed      de ej                     fd       Z# ed      de ee	ej                  f      fd       Z$e ed      d               Z%e ed      d               Z&e ed      d               Z'e ed      d               Z(e ed      de
fd              Z)e ed      de	fd               Z*e ed      d!               Z+e ed      d"               Z,e ed      d#               Z-d$ Z.d%e
d&e
de
fd'Z/d%e
d&e
de
fd(Z0d) Z1de	fd*Z2dej                  j                  fd+Z3d, Z4e5	 	 d8d-eeejl                  jn                  e8f      d.eejl                  jr                  df   dd fd/       Z:d0e;dd fd1Z<d2 Z= ed      d3        Z>e?d4        Z@ddd5	 d9d6ZAy):r7   a  
    Package of a program from :func:`export`. It contains
    an :class:`torch.fx.Graph` that represents Tensor computation, a state_dict containing
    tensor values of all lifted parameters and buffers, and various metadata.

    You can call an ExportedProgram like the original callable traced by
    :func:`export` with the same calling convention.

    To perform transformations on the graph, use ``.module`` property to access
    an :class:`torch.fx.GraphModule`. You can then use
    `FX transformation <https://pytorch.org/docs/stable/fx.html#writing-transformations>`_
    to rewrite the graph. Afterwards, you can simply use :func:`export`
    again to construct a correct ExportedProgram.
    N)	verifiersr1  r   r   r2  r3  Dict[sympy.Symbol, Any]r(  r4  .r   r;  c	                $   t         j                  j                  j                         |_        t        ||      | _        t        |t         j                  j                        r/| j                  j                  j                  |j                         t        | j                  ||       || _        || _        || _        |J || _        || _        |xs i | _        |	xs t$        g}	t'        d |	D              sJ |	| _        | j+                          y )Nc              3   <   K   | ]  }t        |t                y wr@   )rd   r    ).0r  s     rG   	<genexpr>z+ExportedProgram.__init__.<locals>.<genexpr>  s     >q:a*>s   )rj   r   r   CodeGenr   _create_graph_module_for_export_graph_moduler   r%  r   r   r.  _graph_signature_state_dict_range_constraints_module_call_graph_example_inputs
_constantsr    all
_verifiersvalidate)
rB   r1  r   r   r2  r3  r(  r4  r   r;  s
             rG   __init__zExportedProgram.__init__  s      //1<T5IdEHH001##**4995(1B	
 7F+5CT ,,,9J-#/r+(	>I>>>>#rI   F)is_backward_compatiblec                     | j                   S r@   )rC  rB   s    rG   r   zExportedProgram.graph_module  s     !!!rI   c                 .    | j                   j                  S r@   )r   r   rP  s    rG   r   zExportedProgram.graph  s       &&&rI   c                     | j                   S r@   )rD  rP  s    rG   r   zExportedProgram.graph_signature  s     $$$rI   c                     | j                   S r@   )rE  rP  s    rG   r2  zExportedProgram.state_dict  s     rI   rb   c              #   D   K   | j                         D ]	  \  }}|  yw)zH
        Returns an iterator over original module's parameters.
        N)named_parameters)rB   r   params      rG   
parameterszExportedProgram.parameters  s)     
 --/ 	HAuK	    c              #   h   K   | j                   j                  D ]  }|| j                  |   f  yw)z
        Returns an iterator over original module parameters, yielding
        both the name of the parameter as well as the parameter itself.
        N)r   rW  r2  )rB   
param_names     rG   rU  z ExportedProgram.named_parameters  s7      ..99 	:Jdooj999	:s   02c              #   D   K   | j                         D ]	  \  }}|  yw)zC
        Returns an iterator over original module buffers.
        N)r   )rB   r   bufs      rG   bufferszExportedProgram.buffers  s)     
 ((* 	FAsI	rX  c              #      K   t        | j                  j                        }| j                  j                  D ]-  }||v r|| j                  |   f || j
                  |   f / yw)z
        Returns an iterator over original module buffers, yielding
        both the name of the buffer as well as the buffer itself.
        N)r   r   non_persistent_buffersr]  r   r2  )rB   r_  buffer_names      rG   r   zExportedProgram.named_buffers  sm      "%T%9%9%P%P!Q//77 	@K44!4>>+#>>>!4??;#???		@s   A'A)c                     | j                   S r@   )rF  rP  s    rG   r3  z!ExportedProgram.range_constraints       &&&rI   c                     | j                   S r@   )rG  rP  s    rG   r(  z!ExportedProgram.module_call_graph  rb  rI   c                     | j                   S r@   )rH  rP  s    rG   r4  zExportedProgram.example_inputs  s     ###rI   c                 6   t        dddg      }t        | j                        dk(  r
 |d d       S | j                  d   j                  dk(  sJ  || j                  d   j                  j
                  | j                  d   j                  j                        S )NCallSpecr=   r>   r   )r=   r>    )r   rv   r(  rR   rS   r=   r>   )rB   rf  s     rG   	call_speczExportedProgram.call_spec  s     j9j*ABt%%&!+D488%%a(,,222**1-77??++A.88AA
 	
rI   c                      | j                   d   S Nr   rK  rP  s    rG   r   zExportedProgram.verifier  s     q!!rI   c                 P    | j                   J | j                   d   j                  S rj  )rK  r   rP  s    rG   r   zExportedProgram.dialect  s)     ***q!)))rI   c                     | j                   S r@   rk  rP  s    rG   r;  zExportedProgram.verifiers       rI   c                     | j                   S r@   rI  rP  s    rG   tensor_constantsz ExportedProgram.tensor_constants$  rn  rI   c                     | j                   S r@   rp  rP  s    rG   r   zExportedProgram.constants)  rn  rI   c                     | j                   j                  }|t        ||      }t        j                  ||f      \  }}| j                  |       t        d |D              }||fS )a  Flatten args, kwargs using pytree, then, check specs.

        Args:
            args: List[Any] original args passed to __call__
            kwargs: Dict[str, Any] original kwargs passed to __call

        Returns:
            A tuple of (flat_args, received_spec)
            flat_args is flattend args / kwargs
            received_spec is the pytree spec produced while flattening the
            tuple (args, kwargs)
        c              3   &   K   | ]	  }|d      yw)r+   NrP   )r?  xs     rG   r@  z<ExportedProgram._get_flat_args_with_check.<locals>.<genexpr>B  s     <1!A$<s   )rh  r=   r'   rN   tree_flatten_with_path_check_input_constraintsr   )rB   rW   rX   r=   flat_args_with_pathreceived_spec	flat_argss          rG   _get_flat_args_with_checkz)ExportedProgram._get_flat_args_with_check.  so     ..((#FG4F-3-J-J6N.
*] 	%%&9:<(;<<	-''rI   rW   rX   c                    | j                   j                  }| j                  ||      \  }}|"t        ||t              st        d| d|       g }| j                  j                  D ]  }|j                  t        j                  k(  r"|j                  t        j                  t        j                  fv r`|j                  du r)|j                  | j                  |j                             |j                  | j"                  |j                             |j                  t        j$                  t        j&                  fv s|j                  | j                  |j                              t)        |      }||z   S )a,  Transform args, kwargs of __call__ to args for graph_module.

        self.graph_module takes stuff from state dict as inputs.
        The invariant is for ep: ExportedProgram is
        ep(args, kwargs) ==
          ep.postprocess(ep.graph_module(ep.graph_module_flat_inputs(args, kwargs)))
        z>Trying to flatten user inputs with exported input tree spec: 
z-
but actually got inputs with tree spec of: 
F)rh  r=   r{  r&   rg   
ValueErrorr   r   r   r0   r   r   BUFFERr   r   r   r   r2  CONSTANT_TENSOR
CUSTOM_OBJr   )rB   rW   rX   r=   rz  ry  additional_inputsinput_s           rG   _graph_module_flat_inputsz)ExportedProgram._graph_module_flat_inputsE  sf    ..((#'#A#A$#O 	=}7$A(
 Q) @ /#  **66 	HF{{i222##  !  $$- &,,T^^FMM-JK%,,T__V]]-KL))$$!  "(()FG#	H$ ""34 !9,,rI   c                     t        d      )Nz\Unable to call ExportedProgram directly. You should use `exported_program.module()` instead.)rt   )rB   rW   rX   s      rG   __call__zExportedProgram.__call__s  s    B
 	
rI   c           	      :   ddl mc m} | j                  ||      \  }}| j                  j
                  | j                  j                  }| j                  j                  }t        |      t        |      z   }	|d|	 }
| j                  j                  }|'t        t        |j                                     }|d| }||	d }	 t        j                  || j                  j
                        }	 | j                  j&                  D cg c]!  }|j(                  t*        j,                  k(  r|# }}t/        |
      D ]  \  }}| j                  j0                  |   j(                  t2        j4                  k(  r(j6                  J || j8                  j6                  <   dj(                  t2        j:                  k(  r@j6                  J t        fdt/        |      D              }||   j=                  |       t?        dj(                          |S |S # t         $ rD t        j"                  |      \  }}|j%                  d| j                  j
                   d|       w xY wc c}w # | j                  j&                  D cg c]!  }|j(                  t*        j,                  k(  r|# nc c}w }}t/        |
      D ]  \  }}| j                  j0                  |   j(                  t2        j4                  k(  r(j6                  J || j8                  j6                  <   dj(                  t2        j:                  k(  r@j6                  J t        fdt/        |      D              }||   j=                  |       t?        dj(                          w xY w)zProcess potential mutations to the input.

        Because self.graph_module is functional, so mutations has to be written
        back after execution of graph_module.
        r   Nz@Trying to flatten user outputs with exported output tree spec: 
z.
but actually got outputs with tree spec of: 
c              3   n   K   | ],  \  }}|j                   j                  j                  k(  r| . y wr@   )r   rA   r   )r?  rE   r  output_specs      rG   r@  zDExportedProgram._postprocess_graph_module_outputs.<locals>.<genexpr>  s3      % '4#xx}}0B0BB %s   25zUnexpected kind: ) torch._export.error_exporterrorr{  rh  r>   r   r  user_inputs_to_mutaterv   assertion_dep_tokennextiterkeysrN   r   	Exceptionr  InternalErrorr   r   r0   r   r   r   r2   BUFFER_MUTATIONr   r2  USER_INPUT_MUTATIONcopy_r   )rB   resr   orig_kwargsr  rz  r   buffer_mutationuser_input_mutationnum_mutatedmutated_valuesr  assertion_dep_token_indexry  r  r   rE   valueindexr  s                      @rG   !_postprocess_graph_module_outputsz1ExportedProgram._postprocess_graph_module_outputsy  s    	,+55iM	1>>""."22DDO"&"6"6"L"Lo.5H1IIK +.N #'"6"6"J"J".,06I6N6N6P1Q,R)445kl#CU++C1H1HI !% 4 4 @ @yyI$8$88  
 !*. 9 UHAu"&"6"6"C"CA"FK"'':+E+EE*11===>C(:(:;$))Z-K-KK*11=== $ %+4[+A% !
 "%(..u5,/@AQAQ@R-STTU 
s
;  #)#6#6s#; =))W~~../ 0E$o'  $ 4 4 @ @yyI$8$88   
 !*. 9 UHAu"&"6"6"C"CA"FK"'':+E+EE*11===>C(:(:;$))Z-K-KK*11=== $ %+4[+A% !
 "%(..u5,/@AQAQ@R-STTUs,   *H &I(AI%%I- -N&J-,C.Nc                     | j                   j                  dd      j                  dd      }d| d| j                   d| j                   d}|S )NF)print_outputcolored
z
    zExportedProgram:
    z
Graph signature: z
Range constraints: )r   print_readablereplacer   r3  )rB   r   strings      rG   __str__zExportedProgram.__str__  sp    ((77 8 

'$
! 	. !  $ 4 45 6""&"8"8!9= 	 rI   c                     ddl m}  ||       }ddt        fd}ddt        fd}t        j                  ||      |_        t        j                  ||      |_        |S )z_
        Returns a self contained GraphModule with all the parameters/buffers inlined.
        r+   )&_unlift_exported_program_lifted_statesmodec                     t        d      )Nz%Calling train() is not supported yet.NotImplementedErrorrB   r  s     rG   _trainz&ExportedProgram.module.<locals>._train  s    %&MNNrI   c                     t        d      )Nz$Calling eval() is not supported yet.r  r  s     rG   _evalz%ExportedProgram.module.<locals>._eval  s    %&LMMrI   T)_unliftr  booltypes
MethodTypetraineval)rB   r  r   r  r  s        rG   r   zExportedProgram.module  s]     	D7=	Ot 	O	Nd 	N ''7&&uf5rI   c                     t        d t        | j                  j                        D        t	        | j                  j                              S )Nc              3   `   K   | ]&  \  }}|j                   t        j                  k(  r| ( y wr@   )r   r0   r   )r?  rE   ss      rG   r@  z=ExportedProgram._num_lifted_params_buffers.<locals>.<genexpr>  s.      Aq66Y111 s   ,.)r  r   rD  r   rv   rP  s    rG   _num_lifted_params_buffersz*ExportedProgram._num_lifted_params_buffers  sC    %d&;&;&G&GH
 %%112
 	
rI   r   r   c                 >    ddl m} | |       }t        | ||d      S )aU  
        Run a set of decompositions on the exported program and returns a new
        exported program. By default we will run the Core ATen decompositions to
        get operators in the
        `Core ATen Operator Set <https://pytorch.org/docs/stable/torch.compiler_ir.html>`_.

        For now, we do not decompose joint graphs.
        r   )core_aten_decompositionsNr0  )torch._decompr  r9  )rB   r   r   r  s       rG   run_decompositionsz"ExportedProgram.run_decompositions  s.     	;35L*%'!	
 	
rI   passesc                 T   t        t        |            }ddlm}  |       5   || j                        }d d d        |j                  n| j                  }|J || j                  u r|j
                  s| S dt        dt        j                  j                  dt        fd}t        ||j                   || j                  |      | j                  t        || j                        t!        j"                  | j$                        | j&                  | j(                  | j*                  	      }|j                  j,                  j/                  | j                  j,                         |j                  j,                  j/                  |j                  j,                         |S # 1 sw Y   jxY w)Nr   )r   old_signaturenew_gmrb   c           	         g }t        |j                  j                        D ]  \  }}|j                  dk7  r n|t	        | j
                        k  sJ d       | j
                  |   }t        |j                  t        t        f      r|j                  n$ t        |j                        |j                        }|j                  t        |j                  ||j                  |j                                 t#        |j                  j                        d   }|j                  dk(  sJ g }t        |j$                  d         D ]  \  }}|t	        | j&                        k  sJ d       | j&                  |   }	t        |	j                  t        t        f      r|	j                  n$ t        |	j                        |j                        }|j                  t)        |	j                  ||	j                                t+        ||      }
|
S )zO
            Update the graph signature's user_input/user_outputs.
            r   z-Number of inputs changed after transformationr   r  r   z.Number of outputs changed after transformationr   )r   r   r   r   rv   r   r   r   r-   r.   r   rA   r   r1   r   r   r   rf   rW   r   r3   r/   )r  r  new_input_specsrE   r   old_input_specr   output_nodenew_output_specsold_output_specnew_signatures              rG   _get_updated_graph_signaturezKExportedProgram._transform_do_not_use.<locals>._get_updated_graph_signature  s    !O$V\\%7%78 477m+3!--  CBC  "/!:!:1!= "&**-=?P,Q #&& 2n001$))<   &&&++&--&11	0 v||11226K>>X---!$[%5%5a%89 43!..  DCD  #0"<"<Q"? "'++.>@Q-R $'' 3o112499=  !''33S/:P:PQ  1+:JM ! rI   	r1  r   r   r2  r3  r(  r4  r   r;  )r*   rf   r   r   r   modifiedr/   rj   r   r%  r7   r   r   r2  r5  r3  r   r6  rG  r4  r   r;  r   r   )rB   r  pmr   r  transformed_gmr  transformed_eps           rG   _transform_do_not_usez%ExportedProgram._transform_do_not_use  so   f& 	@$& 	(T&&'C	(-0_))$BSBS)))T...s||K7	!/7	!HH((7	! "7	!r ) &&8$$n <&& #mmD,C,CD..nnnn
  	##((//0A0A0F0FG##((//0@0@0E0EFi	( 	(s   FF'c                 R   ddl m} | j                  j                  D cg c]  }|j                  dk(  s| }}t        || j                  j                        D cg c]$  \  }}|j                  t        j                  k(  r|& }}} |||| j                         y c c}w c c}}w )Nr   )"_check_input_constraints_for_graphr   )torch._export.utilsr  r   r   r   r   r   r   r   r0   r   r3  )rB   rx  r  pplaceholdersr  input_placeholderss          rG   rw  z(ExportedProgram._check_input_constraintsN  s    J#'::#3#3Maqtt}7LMM L$*>*>*J*JK
1vv--- 
 

 	+ 3T5K5K	
 N
s   BB)B#c                 $    | j                          y r@   )	_validaterP  s    rG   rL  zExportedProgram.validate[  s    rI   c                     t        | j                        dkD  sJ d       | j                  D ]  } |       j                  |         y )Nr   z0ExportedProgram must have at least one verifier.)rv   r;  check)rB   r  s     rG   r  zExportedProgram._validate`  sE     !#	>=	># 	ACIIdO	rI   )r2  r;  c                   t        ||j                  |||n| j                  t        j                  | j
                        t        j                  | j                        | j                  | j                  ||	      S | j                  	      S )Nr  )
r7   r   r2  r   r6  r3  rG  r4  r   r;  )rB   r   r   r2  r;  s        rG   _updatezExportedProgram._updatei  s~     $$+%/%;z"mmD,B,BC"mmD,C,CD..nn#,#8i

 
	
 ?Cnn

 
	
rI   )NN)NrP   )rb   r7   )BrJ   rK   rL   __doc__r   rj   nnModuler   rT   r   r   Graphr/   r   	Parameterr
   r8   r   r   r   rk   ScriptObjectr   r    rM  propertyr(   r   r   r   r2  r	   rW  rU  r]  r   r3  r(  r4  rh  r   r   r;  rq  r   r{  r  r  r  r  r   r  r]   _opsOperatorBaser   
OpOverloadr  PassTyper  rw  rL  r   r  r  rP   rI   rG   r7   r7     s   . LP % 59%EHHOOT#s(^34% xx~~% .	%
 eELL%((2D2D$DEEF% 5%  0% !uS#XS#X'F!GH% eELL*:EHH<Q<QQRRS
% Dh01%N %0" 1 " %0' 1 ' %0% 1 % %0  1   %0HUXX%7%78  1 %0:(5ehh6H6H1H+I"J : 1: %0%,,/  1 %0
@xc5<<.?(@A 
@ 1
@ %0' 1 ' %0' 1 ' %0$ 1 $ %0	
 1 	
 %0"# " 1 " %0* * 1 * %0 1  %0 1  %0 1 (.,-c ,-3 ,-3 ,-\
c 
S 
S 
5n
 
 $
 $ KO;=
tEJJ$;$;X$EFG
 UZZ22C78
 
	
 $
2ZX Z:K Zx
 %0 1   <@4
	
rI   r7   c                 P   | j                   j                  D cg c]-  }|j                  j                  dd       |j                  d   / }}ddlm}  ||      }||j                  S |D ]4  }t        |t        j                        s|j                  j                  c S  y c c}w )Nr   r   r!   )r   r   r   r   torch._guardsr"   r   r   rj   SymIntr   )r   r   valsr"   r  r  s         rG   r   r   y  s     HHNN99==%1 			%D 
 / &I""" $a&66###$s   2B#old_range_constraintsz!Optional[Dict[sympy.Symbol, Any]]r<  c                 <   |J t        |       }|i S t        j                  |      }|j                         D ci c]  \  }}||j                  vs|| }}}|j                  j                         D ]  \  }}||j                  vs||vs|||<     |S c c}}w r@   )r   r   r   replacementsvar_to_range)r   r  r   r3  r  r  s         rG   r5  r5    s     !,,,r"I			"78*002Aay?U?U6U1  &&,,. %1I***q8I/I#$a % s   BBc                    	 t         j                  j                  | |      }|S # t        $ r\ t	        j
                  d       t         j                  j                  | t         j                  j                               }||_        Y |S w xY w)NzUnable to execute the generated python source code from the graph. The graph module will no longer be directly callable, but you can still run the ExportedProgram, and if needed, you can run the graph module eagerly using torch.fx.Interpreter.)rj   r   r%  SyntaxErrorwarningswarnr  _graph)r1  r   r   s      rG   rB  rB    sx    XX!!$.  I   	G	
 XX!!$(89	Is    $ A!B	B	r  r@   ){r   r   dataclassesr[   r&  r  r  collectionsr   r   typingr   r   r   r   r	   r
   r   r   r   r   r   torch._higher_order_ops.utilsr   "torch._library.fake_class_registryr   torch.fx._utilsr   torch.fx.graphr   r   torch.fx.immutable_collectionsr   r   torch.fx.passes.runtime_assertr   sympytorch.utils._sympy.value_rangesr   rj   torch.utils._pytreeutils_pytreerN   r  r   r   r   r   r   r   r   torch._export.verifierr    r  r"   r   r$   #torch._subclasses.functional_tensorr%   torch.export._tree_utilsr&   r'   torch.fx._compatibilityr(   torch.fx.passes.infra.pass_baser)   "torch.fx.passes.infra.pass_managerr*   r   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   __all__r   r%  r  	dataclassr9   r8   r]   r   Contextr  rg   rp   rk   rm   AutogradCPUAutogradCUDAAutogradMetaAutogradXLAAutogradLazyAutogradIPUAutogradXPUAutogradMPSAutogradHPUAutogradPrivateUse1AutogradPrivateUse2AutogradPrivateUse3r   r   r   r  r  r  intr  r   r.  r9  r7   r   r5  rB  rP   rI   rG   <module>r     s          " %    C ? ? 6 I J 
 ;  $ $   , * @ @ B 1 6 :    UXX))*HZ,@@A ' ' ' 4 4 4
GG>>G G >>	G
 
G2
" 
HH$$	HH%%	HH%%	HH$$	HH%%	HH$$	HH$$	HH$$	HH$$	HH,,	HH,,	HH,,, (  b& b&J 	 	F# uzz..89F# ../	F#
 smF#R##3G#
588!556#2..:" uzz..89" ../	"
 sm"Jo
 o
d$$ BF> .rI   