
    9j                     v   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
mZ d dl mZ d dlmZmZmZmZ d dlmZmZ d dlmZ d dlmZ d d	l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/m0c m1Z2 d dl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC d dlDmEZE d dlmFZF d dlmGZG d dlHmIZImJZJ d dlKmLZL d dlMmNZN d dlOmPZP d dlQmRZR ddlSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z` g dZae
e.j                  j                  gePdz  f   Zdej                   G d d             Zfej                   G d d              Zgd! Zhd"eidz  d#e2j                  d$eidz  d%e2j                  d&ekf
d'Zle.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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                  g	Zed(        Zd)ee.j                  j                  e
f   d&eee.j                  j                  e
f   d*f   fd+ZdFd,Zd-d.d/ee.j                  j                  e
f   d0ee.j                  j                  e
f   d1edz  fd2Zd3e.j                  j                  d4eWd&ee.j                  j                  eWf   fd5Zd3e.j                  j                  fd6Zd7e.j                  j                  d8eWd3e.j                  j                  d9eWd:eeg   f
d;Zd/ee.j                  j                  e
f   d0ee.j                  j                  e
f   d1edz  d<ekfd=Z G d> d.      Zd? Z	 dGd3e.j                  j                  d@dAd&dBfdCZdD ZdE Zy)H    N)defaultdict)CallableIterator)contextmanager)Anyfinal
NamedTupleTYPE_CHECKING)tracingTracingContext)autograd_not_implemented)FakeScriptObject)_deregister_op_impl_is_op_registered_to_fake_ruleregister_op_impl)FakeTensorMode)_ConstantAttributeType)#first_call_function_nn_module_stack)_PyTreeCodeGen_PyTreeInfo)immutable_dictimmutable_list)insert_deferred_runtime_asserts)ValueRanges)_build_cache_collect_all_valid_cia_ops_collect_and_set_constant_attrs_collect_param_buffer_metadata_detect_fake_mode_from_gm_fakify_params_buffers_get_decomp_for_cia_is_preservable_cia_op_name_hoo_subgraph_placeholders7_override_graph_signature_for_temp_registered_constants/_overwrite_signature_for_non_persistent_buffers)_populate_param_buffer_metadata_to_new_gm_register_constants_as_buffers_rename_without_collisions_special_op_to_preserve_ciaplaceholder_naming_pass)Verifierdetect_fake_modeunset_fake_temporarily)is_equivalentreorder_kwargsCustomDecompTable)compatibility)
PassResult)PassManager   )ArgumentSpecConstantArgumentCustomObjArgumentExportGraphSignature	InputKind	InputSpec
OutputKind
OutputSpecSymBoolArgumentSymFloatArgumentSymIntArgumentTensorArgumentTokenArgument)ExportedProgramModuleCallEntryModuleCallSignaturedefault_decompositionsc                       e Zd ZU ee   ed<   ee   ed<   ej                  ed<   ej                  ed<   dZee	   dz  ed<   d Z
y)rG   inputsoutputsin_specout_specNforward_arg_namesc                     | j                   D ]-  }|j                  |j                  k(  s|j                  |_        / | j                  D ]-  }|j                  |j                  k(  s|j                  |_        / y N)rJ   namerK   )selforiginal_nodenew_nodeios        ]/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/export/exported_program.pyreplace_all_uses_withz)ModuleCallSignature.replace_all_uses_withi   sd     	'Avv+++!	'  	'Avv+++!	'    )__name__
__module____qualname__listr8   __annotations__pytreeTreeSpecrN   strrX    rY   rW   rG   rG   a   sE    ,__oo*.tCy4'.'rY   rG   c                   ,    e Zd ZU eed<   dZedz  ed<   y)rF   fqnN	signature)rZ   r[   r\   ra   r^   re   rG   rb   rY   rW   rF   rF   r   s    	H,0I"T)0rY   rF   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rP   r.   )argskwargsfns     rW   wrapperz/_disable_prexisiting_fake_mode.<locals>.wrappery   s+    #% 	't&v&	' 	' 	's   ')	functoolswraps)rj   rk   s   ` rW   _disable_prexisiting_fake_modern   x   s%    __R' ' NrY   
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   r]   r   )ro   rp   rq   rr   s       rW   _fx_collection_equivalence_fnrw      s     Z/Z'JM],JJ*t^45*T>*; --*t^45*T>*; --#F(FFrY   c              #     K   i }t               }| j                         D ]x  \  }}|j                  j                         ||<   |j	                  |       t
        D ]3  }||j                  vs |j                  |      t        |d             5 t        |      }t        j                  j                  j                  |j                  v r/|j                  t        j                  j                  j                  =  |j                  t        j                  j                  j                        |       d }t        |      s& t        |      t        j                   ||             t"        D ](  }||j                  vs |j                  |      |       * { 	 d  |D ]_  }	|	j                  j%                          |	j                  j'                  ||	          |	j(                  j%                          t+        |	       a y # |D ]_  }	|	j                  j%                          |	j                  j'                  ||	          |	j(                  j%                          t+        |	       a w xY ww)NT)deferred_errorc                 R    |d   }|d= | 5   ||i |cd d d        S # 1 sw Y   y xY w)Noriginal_callablerb   )fake_tensor_modeoprh   ri   orig_cia_callables        rW   $_force_dispatch_to_orig_cia_callablezQ_override_composite_implicit_decomp.<locals>._force_dispatch_to_orig_cia_callable   s<     &': ;*+! :($9&9: : :s   &)r{   )setitems
py_kernelscopyadd(_AUTOGRAD_ALIAS_BACKEND_KEYS_TO_OVERRIDEpy_implr   r!   torch_CDispatchKeyCompositeImplicitAutogradr   r   rl   partial_BACKEND_KEYS_TO_OVERRIDEclearupdate_dispatch_cacher   )
cia_ops_to_callablesaved_tablespatched_opsop_overloaddecomp_callableoverride_dispatch_keyr~   r   keyr}   s
             rW   #_override_composite_implicit_decompr      s     L%K(;(A(A(C =<$_$/$:$:$?$?$A[!$%M 	!$K,B,BB:##$9:,[N	 0<8899[=S=SS&&uxx';';'U'UVKEHH00JJK	
	: .k:)[)!!8&7 - 	<C+000  )##C():;#	<Y=<~$ 	$BMM!MM  b!12$$&#		$+ 	$BMM!MM  b!12$$&#		$s-   A)I.,DI.?I.H  A%I.A&I++I.decomp_table.c                 X   t        t                     }i }t        | j                               D ]f  }||v r| |   ||<   |j	                  |       | |= $t        |      s0|j                         }|j                  d      rt        d|       | |   ||<   h |D ]  }t        ||<    || fS )Natenz)This should be a custom op, got aten op: )
r   r   r]   keysremover"   rQ   
startswithAssertionErrorr)   )r   all_preservable_cia_opsr   r}   op_nameks         rW   ,_split_decomp_table_to_cia_and_python_decompr     s     ""<">?<$$&' 7$ ((&22&6##**2.R  $B'ggiG!!&)$?yI  '32&6#?7F % =!<A= ,,rY   c                      t               S )z
    This is the default decomposition table which contains decomposition of
    all ATEN operators to core aten opset. Use this API together with
    :func:`run_decompositions()`
    r2   rb   rY   rW   rH   rH   B  s     rY   eprE   cia_to_decomppython_decomp_tablejoint_loss_indexc                   ddl m} ddlm} ddlm}m}m}	m}
m	}m
} ddlm} d } || |      s3| j                         }i t        |j                  d            t        |j!                  d            }dd	lm}  ||       i t        |j                  d            t        |j!                  d            }t'        | j(                        }|t+         |       d
      }|j,                  }t/        |j0                  j2                  t4              s+t7        dt9        |j0                  j2                               |j0                  j2                  j:                  j<                  }|t7        d      |j8                  t>        t@        fvrtC        jD                  |g      }t5        tG        ||jH                  |            |j0                  _        |jK                          tM        | jN                  | jP                  |       tS        || jT                  | jN                  jV                        }tY        ||      }t[        |      }ddl.m/}m0} g }|j0                  jb                  D ]  }|jd                  dk(  st/        |jf                  d   th              rwd }|jf                  d   jj                  '| jP                  |jf                  d   jl                     }n#|jf                  d   jj                  jn                  }|jq                  |       |jq                  |jf                  d           ts        |      }|5  tu        |      5   || jv                        5  ty        |      5  tC        jz                  ||jH                        } ||g |d   |d   j}                         i |      5 \  } }!}"}#}$ || |!|"||#|d|      }%|%jP                  j                         D &'ci c]  \  }&}'|&t/        |'t              r|$|'   n|' c}'}&|%_(        |%j                  }(|%j                  })t        |)|      })t        ||(|)       t        | jN                  |)      }) ||(|)|#      }*t        |(|)| |!|"||*       d d d        d d d        d d d        d d d        d d d         |
(        ||(        ||()       t        |(|)      \  }(})i | jT                  |j                         D +,ci c]  \  }+},|+|vr|+|, c},}+}-|j                         D ]W  \  }+},|+|-vr3t/        |,t        j                  j                        rt7        d|+d      |+|-v sB|+|vsG|-j                  |+       Y |(|)|-fS | j(                  j0                  jb                  D cg c]  }|jd                  dk(  s| }.}|.D cg c]  }|jf                  d    }/}| j(                  j!                         D +0cg c]  \  }+}0|+	 }1}+}0|1D ]  }+t        | j(                  |+        t        |/      }2|2t        j                         n|2}3|rt        j                  n|}4 |	       5  |35  tu        |      5   |4       5   || j(                  |/||d u||nd       \  }(}5t/        |(t        j                  j                        sJ |(j0                  j                          d d d        d d d        d d d        d d d        d }6(j0                  jb                  D cg c]  }|jd                  dk(  s| }7}tA        |(j0                  j                         j                  d         }8t        |7      t        |.      k7  r#t7        dt        |7       dt        |.             t        |.|7      D ]  \  }9}:|9jl                  x|:_6        |:_W         i };t        t              }<t               }=|7D ]2  }>|>jl                  |;|>jl                  <   t        |>jl                  |<|=       4 |(j0                  jb                  D ]:  }|jd                  dk(  rt        |;|<|=|jl                  |jl                        |_6        < t        |(       ddl^m_}?m`}@ ddlamb}A t        j                  j                  j                  s]d}Bt        |(      }C|CN @|(t        j                  |?dBi            5  t        |(Cdt        |(j0                         d
       d d d        |(jK                          t        |8 A|(            D ]  \  }D}+|+	|+D_6         t        | jN                  j                        D EFci c]C  \  }E}Ft/        |Fj                  t              s$Fj                  jl                  |7E   jl                  E }G}E}Ft        | jN                  j                        D EFcg c]E  \  }E}Ft        |Fj                   |6|Fj                  |7|E         |Fj                  |Fj                        G }H}E}Fg }It        5j                        D E+ci c]  \  }E}+|+|E
 }J}E}+t?        |5j                  j                               }K|8d t        |K       D cg c]  }|jl                   }L}K|Lk7  rt7        d K d!L       |5j                  j                         D ]  \  }M}NJ|N   }E| jN                  j                  |E   }O|Oj                  t        j                  t        j                  fvrt7        d"Oj                         Oj                  t        j                  k(  rt        j                  nt        j                  }POj                  t        j                  k(  rOj                  nOj                  jl                  }QIjq                  t        Pt        M#      |Q$              t        | jN                  j                        D ]  \  }E}FIjq                  t        |E|k(  rt        j                  nFj                   |6Fj                  |8t        K      Ez            Gj                  |Fj                  |Fj                                      ||5j                   t7        d%      |5j                   j                  }Rt        |5j                        t        | jN                  j                        k7  rAt7        d&t        |5j                         d't        | jN                  j                               t        | jN                  j                        D EFci c]/  \  }E}Ft/        |Fj                  t              r|5j                  E   F1 }S}E}F|8t        I      d  D ]  }R|jl                     }TS|T   }F|Fj                  t        j                  k(  rt        j                  }UFj                  }QnIFj                  t        j                  k(  rt        j                  }UT}Qnt7        d(Fj                         Ijq                  t        Ut        |jl                  #      Q              t        |7      t        |.      k7  r#t7        dt        |7       dt        |.             t        HI)      })t        |.|7      D ]  \  }V}Wt/        |Vjf                  d   t        j                        sVjf                  d   Wjf                  d<   Wj                  |)j                  v sWj                  |)j                  v s}Vjf                  j                         D ]  \  }X}Y|YWjf                  |X<     |(|)| jT                  fS c c}'}&w # 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# 1 sw Y   
xY wc c},}+w c c}w c c}w c c}0}+w # 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 # 1 sw Y   xY wc c}F}Ew c c}F}Ew c c}+}Ew c c}w c c}F}Ew )*Nr   )_materialize_and_lift_constants)aot_export_module)2_disable_custom_triton_op_functional_decomposition_export_to_aten_ir_ignore_backend_decomps_verify_nn_module_stack_verify_placeholder_names_verify_stack_trace)ShapeEnvc                 >    |d uxs | j                   j                  d uS rP   )graph_signaturebackward_signature)r   r   s     rW   _is_joint_ir_decompzO_decompose_and_get_gm_with_new_signature_constants.<locals>._is_joint_ir_decomp_  s)    D( A!!44D@	
rY   F)remove_duplicate)!unwrap_tensor_subclass_parametersT)	shape_envexportz6expected mod.graph._codegen to be _PyTreeCodeGen, got zout_spec must not be None)&_enable_graph_inputs_of_type_nn_module_fakify_script_objectsplaceholdervalr7   )r   _prettify_placeholder_namesdecompose_custom_triton_opsz	expected z6 not to be a torch.nn.Parameter when not in state_dict)decompositionstrace_jointoutput_loss_indexc                    t        | t              r| S t        | t              rt        |j                        S t        | t              rt	        |j                        S t        | t
              rt        |j                        S t        | t              rt        |j                        S t        dt        |              )NrQ   zType of old_arg not supported: )	
isinstancer9   rC   rQ   rB   rA   r@   RuntimeErrortype)old_argnew_phs     rW   
update_argzF_decompose_and_get_gm_with_new_signature_constants.<locals>.update_argf  s    g/0N0!v{{330!v{{33!12#551"44<T']OLMMrY   znew_placeholders length z( does not match old_placeholders length )_node_metadata_hook_set_node_metadata_hook)_graph_output_nameszUFile "torch/fx/passes/runtime_assert.py", line 24, in insert_deferred_runtime_assertsstack_trace)metadatazexported program: )r   zmutation_names z does not match expected z9expected input_spec.kind to be USER_INPUT or BUFFER, got r   )kindargtargetzPgraph_signature.backward_signature must not be None when joint_loss_index is setz#graph_signature.user_inputs length z# does not match input_specs length zUnknown input kind: input_specsoutput_specs)(torch._export.passes.lift_constants_passr   torch._functorch.aot_autogradr   torch.export._tracer   r   r   r   r   r   %torch.fx.experimental.symbolic_shapesr   modulerv   named_parametersnamed_buffers7torch._functorch._aot_autograd.subclass_parametrizationr   r   graph_moduler   	_out_specr   graph_codegenr   r   r   pytree_info	orig_argsr]   tupler_   treespec_tupler   _in_spec	recompiler   r   	constantsr'   
state_dictnon_persistent_buffersr    r   torch._export.non_strict_utilsr   r   nodesr}   metar:   fake_valrQ   real_objappendr   r   example_inputsr   tree_unflattenvaluesr   r   gmsigr$   r&   r%   r*    _remove_unnecessary_copy_op_passr   nn	Parameterpopdelattrr-   
contextlibnullcontextfxGraphModuleeliminate_dead_codeoutput_noderh   lenzipr   r   intr   r   r(   r#   (torch._export.passes._node_metadata_hookr   r   4torch._functorch._aot_autograd.input_output_analysisr   _dynamoconfigdo_not_emit_runtime_asserts_get_shape_envrl   r   r   r   	enumerater   r   r9   r=   r   
persistentuser_inputsuser_inputs_to_mutater   r<   
USER_INPUTBUFFERr>   BUFFER_MUTATIONUSER_INPUT_MUTATIONr?   rC   r   LOSS_OUTPUTgetr   gradients_to_user_inputs	PARAMETERGRADIENT_TO_PARAMETERGRADIENT_TO_USER_INPUTr;   Tensorinputs_to_parametersinputs_to_buffers)Zr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   modwrapped_params_buffersr   unwrapped_params_buffers	fake_moderM   orig_arg_namestemp_registered_constantsfake_params_buffersparams_buffers_to_node_metar   r   retracing_argsnodereal_script_objtxretracing_args_unwrappedpatched_modnew_fake_argsnew_fake_kwargsnew_fake_constant_attrsmap_fake_to_realaten_export_artifactrd   objr   new_graph_signaturer   rQ   pnew_state_dictold_placeholders	fake_args_buffers_to_removefake_mode_detfake_mode_ctx#custom_triton_ops_decomposition_ctxr   r   new_placeholdersnew_outputsold_phr   name_mapfind_available
used_namesphr   r   r   r   r   outputrU   specold_new_placeholder_mapr   r   user_inputs_indexmutation_namesexpected_namesoutput_name
input_name
input_specoutput_kindr   	gradientsspecssourcer   old_noderT   r   vsZ                                                                                             rW   2_decompose_and_get_gm_with_new_signature_constantsrO  K  s    Y?  ?
 r#34iik"
3'''?@"
3$$e$<="

	
 	*#.$
3'''?@$
3$$e$<=$
  .boo>	&DII ==#)),,n= HciiN`N`IaHbc  ++77AA  !<====u-,,hZ8H+
		 	
 	((:(:BLL#N %C 2 2 I I%
!
 5YD&DS&I#
	

 IIOO 
	<Dww-'dii.0AB&*Oyy'008*,,,tyy7G7L7L*M*.))E*:*C*C*L*L"))/:"))$))E*:;
	< I& P	/P	
 323D3DEP	 BKP	 (.'<'<($ (-a0-a0779  D' '9!#'+!4050K	($$ %9$B$B$H$H$J2 !S %c+;< )- !2$. *,,&:&>&>#
 L+-F $ :/5H
 'V&&(;'# <+-D	 ('!#'yDP	 P	 P	 P	d 	 #B!"&9:"B##

mm
  8==?D!55 a
 .335 
	-GD!>)a!3!34(#D8+ab  ~%77"&&t,
	- &66 ..44=8P  /??d5!?I?-/__-J-J-LM'$MM! '&' %Y/M0=0EJ**,=M ' 	? ( 	 !'' 	,M:' 	,-	' 0OO.(4$4$@ d	
O "ehh22333
$$&#' ' ' '*N *,T477m;STT-22883G3G3I3N3Nq3Q-RK $4 55&s+;'<&==efijzf{e|}
 	
 .0@A 2&,kk1fm2 H%0%5N5J :GGRWWnj9:  
77m#.nj$))TYY
	
 $B' Y==;;1 	 #2&	 (!!'=+2N  0()LRXX)V(WX	 LLNK)<R)@A FK !!3!3!?!?@At$(($45 	'*///  !!3!3!?!?@ At 	IItxx!1!!45KKOO		
K  L 1:/:U:U0VWWQqWW/??DDFGN,78M#n:M,NODdiiONO'n--F~FVW
 	
 $3#H#H#N#N#P 
Zj)''33A6
??9#7#79I9I"JJ KJOOK\] 
 )"2"22 &&// 	 )"2"22 $$ 	
 	 "4	
#
4 R//<<= 
4*+/?*?
&&TYY488[^1Dq1H%IJ'++DKKE	

 #--5 b  $66OO	**+s23E3E3Q3Q/RR 5c/:U:U6V5W X&&)"*<*<*H*H&I%JL  %R%7%7%C%CD
4$((N3 ''*D0
 

  L 1 34 	Dtyy)F=DyyI///!77i222!88$';DII;%GHH"		2	& $4 55&s+;'<&==efijzf{e|}
 	
 /l ""24DE 	%((--.=#+==#7HMM%  OO2GGG"5"G"GG ++- %1#$a %	% "BMM11}	27D DP	 P	 P	 P	 P	 P	 P	 P	F* @M' ' ' ' ' ' ' 'D UV *" XOf
s+  =}8	}+}(A}	-4}!"|>A+}.}	6}>}+}8~~~$~~~5~(A!~	;~(~568&<A!)A
'-348>}}	}	}}}(#}++}5	0}88~~% ~((~2-~55~?	: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|j                  |j                  v sY|j                  dk(  si|j                  t        j                   j"                  j$                  j&                  u 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.
    r@  call_functionr7   N)_set_replace_hookget_replace_hookr   r   r}   r_   tree_flattenrh   r   r   r   NoderQ   buffers_to_mutateparameters_to_mutater   opsr   r   defaultrX   
erase_noder   )r   r/  r$  rh   r4  outs         rW   r   r   0  s+    
		1BBD	E HHNN 	5Dww(" --dii8a 
5C!#uxx}}5$7$I$II88':'O'OO  FFo5 #

eiinn.A.A.I.I I55chhqkBHH//4
5	5 	  """!  """s+   *E-AE-0E-E-:E-AE--E9c           	         | j                  |j                               5  | j                         D ]  }t        |t        j
                  j                        s)i }i }t        |j                  j                        D ]  }|j                  dk(  r|j                  t        j                  u 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)NrQ  .)rR  rS  modulesr   r   r   r   r]   r   r   r}   r   operatorgetitemrh   rX   re   rZ  rQ   )r   r   module_call_graphr   node_idgetitemsr$  rL  idxnew_identrys              rW    _common_getitem_elimination_passrg  I  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old_gmold_graph_signaturer   old_module_call_graphc                 h   t        j                  |      }| j                  j                  D ci c]  }|j                  | }}i j
                  j                  i |j
                  |j                  }i }	d}
| j                  j                  D cg c]  }|j                  dk(  s|j                    }}t        t        fd|            }|j                  j                  D cg c]  }|j                  dk(  s|j                    }}|j                  j                  D ]   }|j                  j                  dg       x}r|j                  |	|d   j                  <   ?|j                  dk(  sO|j                  |v r^|j                  |j                  v rw|j                  |v st        |j                  t              s!t!        dt#        |j                               ||
   }t        |t              st!        dt#        |             |j                  |	|<   |
dz  }
 t%        t              }|j'                         D ]X  \  }}|j)                  d	      s|v s||	|<   "d
j+                  |j-                  d
      dd       }||   j/                  |       Z |D ]x  }|j0                  }|g |j2                  |j4                  D ]H  }|j                  |v rt7        d|j                   d      |j                  |	v r|	|j                     |_        N|j                  |v s]||j                     }|j                  dk(  s||j                  t8        j:                  j<                  j>                  j@                  t8        j:                  j<                  j>                  jB                  t8        j:                  j<                  j>                  jD                  fv s|jF                  d   j                  }||	vrt7        d| d      |	|   |_        K { |S c c}w c c}w c c}w )Nr   r   c                 ,    | vxr | j                   vS rP   )input_tokens)xold_graph_params_buffersri  s    rW   <lambda>z0_get_updated_module_call_graph.<locals>.<lambda>  s$    a77 :,999 rY   	from_nodez"expected node.name to be str, got z!expected old_name to be str, got r7   zparametrizations.r]  zIt looks like z is a tensor subclass. Preserving submodule that takes subclass parameter is not supported in inference IR because we desugar them, resulting in more tensorsrQ  )$r   deepcopyr   r   rQ   r  r  r}   r   r]   filterr   r  rm  r   ra   r   r   r   r   r   joinsplitr   re   rJ   rK   
ValueErrorr   rX  r   todtype_layoutdevicedtyperh   )rh  ri  r   r   rj  new_module_call_graphr$  	old_nodesnew_graph_params_buffers
provenanceuser_input_counterold_user_input_namesnew_user_input_nameshistoryold_nameold_param_to_desugaredrQ   r   
old_targetrf  re   rn  rM  ro  s    `                     @rW   _get_updated_module_call_graphr  f  s    !MM*?@-3\\-?-?@TD@I@ 

2
2 

/
/  

.
. 

+
+  "$J & 2 2dgg6N   : 	
 !#$'']2J   ,iimmK4474+/99Jwr{''(
 WW%77;;/">">>;;"66%dii5,@dii@QR   44FGH%h4,?X?OP  ,099Jx(&!+&/,8 ).0668 <f  !45//#'
4 &,,s"3Ab"9:J":.55d;< ' '4OO	89##8i&7&78 #	4A vv// $QVVH -Z [  vv##AFF+ 9$$QVV,;;/1hooIINN%%22IINN%%,,IINN%%++J 7
 "*q!1!6!6J!3(,ZL 9b c  (
3AFG#	4	'4R ! _ A s   P%P*+P*,P/P/r   c          
      t   t        | ||||      \  }}}t        | j                  | j                  ||| j                        }|j
                  j                  | j                  j
                         t        || j                        }	t        ||j                  |||	|| j                  | j                        }
|
S )Nr   r   r   r   )rootr   r   r   range_constraintsra  r   r   )rO  r  r   r   ra  r   r   _get_updated_range_constraintsr  rE   r   r   r   )r   r   r   r   r   r   r/  r   r|  new_range_constraintsexported_programs              rW   _decompose_exported_programr    s     	;
#/)$?		
 ;



 GGNN2??''(:


 'hh+//((,,	 rY   c                   	   e Zd ZU dZej
                  j                  ed<   	 eed<   	 e	e
ef   ed<   	 ded<   	 ee   ed<   	 eeedf   e	e
ef   f   d	z  ed
<   	 e	e
ef   ed<   	 eee      ed<   	 ee
   ed<   	 	 dMd	ddej$                  j&                  e	e
ef   z  dej
                  j(                  dede	e
ej*                  ej$                  j,                  z  f   dddee   deeedf   e	e
ef   f   d	z  de	e
ef   d	z  deee      d	z  fdZe ed      d               Zej6                   ed      d               Ze ed      d               Zej6                   ed      d               Ze ed      d                Zej6                   ed      d!               Ze ed      d"               Zej6                   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$j6                   ed      d*               Z$e ed      d+               Z%e%j6                   ed      d,               Z%e ed      d-               Z&e&j6                   ed      d.               Z&e ed      d/               Z'e'j6                   ed      d0               Z'e ed      d$efd1              Z(e(j6                   ed      d2               Z(e ed      d$e
fd3              Z)e)j6                   ed      d4               Z)e ed      d5               Z*e*j6                   ed      d6               Z*e ed      d7               Z+e+j6                   ed      d8               Z+e ed      d9               Z,e,j6                   ed      d:               Z,d; Z-d<ed=ed$efd>Z.d<ed=ed$efd?Z/d$e
fd@Z0dNd$ej
                  j                  fdAZ1dB Z2e3	 	 dOdCe	ejh                  jj                  e6f   d	z  dDe7d$d fdE       Z8dFe9d$d fdGZ:dH Z; ed      dI        Z<e=dJ        Z>d	d	d	dK	 dPdLZ?y	)QrE   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.
    _graph_module_graph_signature_state_dictzdict[sympy.Symbol, ValueRanges]_range_constraints_module_call_graph.N_example_inputs
_constants
_verifiers_guards_code)	verifiersr  r   r   r   r  dict[sympy.Symbol, Any]ra  r   r   r  c	                   t         j                  j                  j                         |_        t        ||      | _        t        |t         j                  j                        r/| j                  j                  j                  |j                         t        | j                  ||       || _        || _        || _        |t        d      || _        || _        |xs i | _        |	xs t&        g}	t)        d |	D              st        d|	       |	| _        | j-                          t/        | j                        | _        y )Nz"module_call_graph must not be Nonec              3   <   K   | ]  }t        |t                y wrP   )ru   r+   ).0rN  s     rW   	<genexpr>z+ExportedProgram.__init__.<locals>.<genexpr>c  s     >q:a*>s   z2all verifiers must be subclasses of Verifier, got )r   r   r   CodeGenr   _create_graph_module_for_exportr  r   r   r   r   rg  r  r  r  r   r  r  r  r+   allr  validate_convert_guards_to_coder  )
rR   r  r   r   r   r  ra  r   r   r  s
             rW   __init__zExportedProgram.__init__B  s    //1<T5IdEHH001##**4995(1B	
 7F+5CT$ !EFF9J-#/r+(	>I>> DYKP  $3D4F4FGrY   F)is_backward_compatiblec                     | j                   S rP   )r  rR   s    rW   r   zExportedProgram.graph_modulem  s     !!!rY   c                     t        d      )Nz7Unable to set ExportedProgram's graph_module attribute.r   rR   values     rW   r   zExportedProgram.graph_moduler  s     TUUrY   c                 .    | j                   j                  S rP   )r   r   r  s    rW   r   zExportedProgram.graphw  s       &&&rY   c                     t        d      )Nz0Unable to set ExportedProgram's graph attribute.r  r  s     rW   r   zExportedProgram.graph|  s     MNNrY   c                     | j                   S rP   )r  r  s    rW   r   zExportedProgram.graph_signature  s     $$$rY   c                     t        d      )Nz:Unable to set ExportedProgram's graph_signature attribute.r  r  s     rW   r   zExportedProgram.graph_signature  s     WXXrY   c                     | j                   S rP   )r  r  s    rW   r   zExportedProgram.state_dict  s     rY   c                     t        d      )Nz5Unable to set ExportedProgram's state_dict attribute.r  r  s     rW   r   zExportedProgram.state_dict  s     RSSrY   rs   c              #   D   K   | j                         D ]	  \  }}|  yw)zH
        Returns an iterator over original module's parameters.
        N)r   )rR   r4  params      rW   
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   r  r   )rR   
param_names     rW   r   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   )rR   r4  bufs      rW   bufferszExportedProgram.buffers  s)     
 ((* 	FAsI	r  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   r   r  r   r   )rR   r   buffer_names      rW   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 rP   )r  r  s    rW   r  z!ExportedProgram.range_constraints       &&&rY   c                     t        d      )Nz<Unable to set ExportedProgram's range_constraints attribute.r  r  s     rW   r  z!ExportedProgram.range_constraints       J
 	
rY   c                     | j                   S rP   )r  r  s    rW   ra  z!ExportedProgram.module_call_graph  r  rY   c                     t        d      )Nz<Unable to set ExportedProgram's module_call_graph attribute.r  r  s     rW   ra  z!ExportedProgram.module_call_graph  r  rY   c                     | j                   S rP   )r  r  s    rW   r   zExportedProgram.example_inputs  s     ###rY   c                    ||| _         y t        |t              r4t        |      dk(  r&t        |d   t              rt        |d   t              st        d      |\  }}ddlm}  |||| j                  j                         || _         y )N   r   r7   zqExample inputs should be a tuple containing example arguments (as a tuple), and example kwargs (as a dictionary).)_check_inputs_match)
r  r   r   r  rv   rw  _unliftr  	call_specrL   )rR   r  rh   ri   r  s        rW   r   zExportedProgram.example_inputs  s    
 =#(D  ue$E
a58U+58T*B 
 f0D&$..*@*@A$rY   c                 ~    G d dt               }t        | j                        dk(  r
 |d d       S | j                  d   j                  dk7  r%t	        d| j                  d   j                         || j                  d   j
                  j                  | j                  d   j
                  j                        S )Nc                   V    e Zd ZU ej                  dz  ed<   ej                  dz  ed<   y)+ExportedProgram.call_spec.<locals>.CallSpecNrL   rM   )rZ   r[   r\   r_   r`   r^   rb   rY   rW   CallSpecr    s     __t++oo,,rY   r  r   )rL   rM    z=expected first module_call_graph fqn to be empty string, got )r	   r  ra  rd   r   re   rL   rM   )rR   r  s     rW   r  zExportedProgram.call_spec  s    	-z 	- t%%&!+D488!!!$((B. OPTPfPfghPiPmPmOpq  **1-77??++A.88AA
 	
rY   c                     t        d      )Nz4Unable to set ExportedProgram's call_spec attribute.r  r  s     rW   r  zExportedProgram.call_spec       QRRrY   c                      | j                   d   S )Nr   r  r  s    rW   verifierzExportedProgram.verifier
  s     q!!rY   c                     t        d      )Nz3Unable to set ExportedProgram's verifier attribute.r  r  s     rW   r  zExportedProgram.verifier  s     PQQrY   c                 b    | j                   t        d      | j                   d   j                  S )Nz_verifiers must not be Noner   )r  r   dialectr  s    rW   r  zExportedProgram.dialect  s/     ??" !>??q!)))rY   c                     t        d      )Nz2Unable to set ExportedProgram's dialect attribute.r  r  s     rW   r  zExportedProgram.dialect  s     OPPrY   c                     | j                   S rP   r  r  s    rW   r  zExportedProgram.verifiers        rY   c                     t        d      )Nz4Unable to set ExportedProgram's verifiers attribute.r  r  s     rW   r  zExportedProgram.verifiers%  r  rY   c                     | j                   S rP   r  r  s    rW   tensor_constantsz ExportedProgram.tensor_constants*  r  rY   c                     t        d      )Nz;Unable to set ExportedProgram's tensor_constants attribute.r  r  s     rW   r  z ExportedProgram.tensor_constants/  s     I
 	
rY   c                     | j                   S rP   r  r  s    rW   r   zExportedProgram.constants6  r  rY   c                     t        d      )Nz4Unable to set ExportedProgram's constants attribute.r  r  s     rW   r   zExportedProgram.constants;  r  rY   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 flattened args / kwargs
            received_spec is the pytree spec produced while flattening the
            tuple (args, kwargs)
        c              3   &   K   | ]	  }|d      yw)r7   Nrb   )r  rn  s     rW   r  z<ExportedProgram._get_flat_args_with_check.<locals>.<genexpr>T  s     <1!A$<s   )r  rL   r1   r_   tree_flatten_with_path_check_input_constraintsr   )rR   rh   ri   rL   flat_args_with_pathreceived_spec	flat_argss          rW   _get_flat_args_with_checkz)ExportedProgram._get_flat_args_with_check@  so     ..((#FG4F-3-J-J6N.
*] 	%%&9:<(;<<	-''rY   rh   ri   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)r  rL   r  r0   rw   rw  r   r   r   r<   r  r  r  r  r   r   r   r   CONSTANT_TENSOR
CUSTOM_OBJr   )rR   rh   ri   rL   r  r  additional_inputsinput_s           rW   _graph_module_flat_inputsz)ExportedProgram._graph_module_flat_inputsW  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,,rY   c                     t        d      )Nz\Unable to call ExportedProgram directly. You should use `exported_program.module()` instead.r  )rR   rh   ri   s      rW   __call__zExportedProgram.__call__  s    B
 	
rY   c                     | j                   j                  dd      j                  dd      }t        | j                        j                  dd      }d| d| d| j
                   d}|S )NF)print_outputcolored
z
    zExportedProgram:
    z
Graph signature: z
Range constraints: )r   print_readablereplacera   r   r  )rR   r   r   strings       rW   __str__zExportedProgram.__str__  s    ((77 8 

'$
! 	 d223;;D(K. !  /0 1""&"8"8!9= 	 rY   c                     ddl m}  || |      }ddt        fd}ddt        fd}t        j                  ||      |_        t        j                  ||      |_        |S )a  
        Returns a self contained GraphModule with all the parameters/buffers inlined.

        - When `check_guards=True` (default), a `_guards_fn` submodule is generated
          and a call to a `_guards_fn` submodule is inserted right after placeholders
          in the graph. This module checks guards on inputs.
        - When `check_guards=False`, a subset of these checks are performed by a
          forward pre-hook on the graph module. No `_guards_fn` submodule is generated.

        r7   )&_unlift_exported_program_lifted_states)check_guardsmodec                     t        d      )Nz%Calling train() is not supported yet.NotImplementedErrorrR   r  s     rW   _trainz&ExportedProgram.module.<locals>._train  s    %&MNNrY   c                     t        d      )Nz$Calling eval() is not supported yet.r  r   s     rW   _evalz%ExportedProgram.module.<locals>._eval  s    %&LMMrY   T)r  r  booltypes
MethodTypetraineval)rR   r  r  r   r  r  s         rW   r   zExportedProgram.module  s_     	D7<X	Ot 	O	Nd 	N ''7&&uf5rY   c                     t        d t        | j                  j                        D        t	        | j                  j                              S )Nc              3   `   K   | ]&  \  }}|j                   t        j                  k(  r| ( y wrP   )r   r<   r  )r  rU   ss      rW   r  z=ExportedProgram._num_lifted_params_buffers.<locals>.<genexpr>  s.      Aq66Y111 s   ,.)nextr
  r  r   r  r  s    rW   _num_lifted_params_buffersz*ExportedProgram._num_lifted_params_buffers  sC    %d&;&;&G&GH
 %%112
 	
rY   r   r   c                     |
t               n
t        |      }t        |t              r|j	                         }t        |      \  }}t        | ||d|      S )a  
        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.

        Args:
            decomp_table:
             An optional argument that specifies decomp behaviour for Aten ops
             (1) If None, we decompose to core aten decompositions
             (2) If empty, we don't decompose any operator


        Some examples:

        If you don't want to decompose anything

        .. code-block:: python

            ep = torch.export.export(model, ...)
            ep = ep.run_decompositions(decomp_table={})

        If you want to get a core aten operator set except for certain operator, you can do following:

        .. code-block:: python

            ep = torch.export.export(model, ...)
            decomp_table = torch.export.default_decompositions()
            decomp_table[your_op] = your_custom_decomp
            ep = ep.run_decompositions(decomp_table=decomp_table)
        Nr  )rH   rv   r   r3   materializer   r  )rR   r   r   _decomp_tabler   r   s         rW   run_decompositionsz"ExportedProgram.run_decompositions  sk    P )5(<"$$|BT 	 m%67)557M" 9G	
 +' 3!(C
 	
rY   passesc                 f   t        t        |            }ddlm}  |       5   || j                        }d d d        |j                  n| j                  }|t        d      || 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.                  j1                  | j                  j.                         |j                  j.                  j1                  |j                  j.                         |S # 1 sw Y   sxY w)	Nr   )r   ztransformed_gm must not be Noneold_signaturenew_gmrs   c           	         g }t        |j                  j                        D ]  \  }}|j                  dk7  r n|t	        | j
                        k\  r%t        d| dt	        | 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k7  rt        d|j                        g }t        |j&                  d         D ]  \  }}|t	        | j(                        k\  r%t        d	| dt	        | 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   z9Number of inputs changed after transformation: got index z
 but only z input_specsrr  r@  z,expected last node to have op='output', got r   z:Number of outputs changed after transformation: got index z output_specsr   )r
  r   r   r}   r  r   r   r   r   r9   r:   r   rQ   r   r=   r   r   r  r]   rh   r   r?   r;   )r  r  new_input_specsrU   r$  old_input_specr   r   new_output_specsold_output_specnew_signatures              rW   _get_updated_graph_signaturezKExportedProgram._transform_do_not_use.<locals>._get_updated_graph_signature  sb    !O$V\\%7%78 477m+M5566(STUSV W$$'(A(A$B#C<Q  "/!:!:1!= "&**-=?P,Q #&& 2n001$))<   &&&++&--&11	#4 v||11226K~~)$B;>>BTU   "$[%5%5a%89 4M6677(TUVTW X$$'(B(B$C#DMS  #0"<"<Q"? "'++.>@Q-R $'' 3o112499=  !''33S/:P:PQ$ 1+:JM ! rY   	r  r   r   r   r  ra  r   r   r  )r6   r]   r   r   r   r   modifiedr;   r   r   r   rE   r   r   r   r  r  r   rs  r  r   r   r  r   r   )rR   r  pmr   restransformed_gmr  transformed_eps           rW   _transform_do_not_usez%ExportedProgram._transform_do_not_use  sw   f& 	@$& 	(T&&'C	(-0_))$BSBS! !BCC T...s||K>	!/>	!HH((>	! ">	!@ ) &&8$$n <&& #mmD,C,CD..nnnn
  	##((//0A0A0F0FG##((//0@0@0E0EF}	( 	(s   F&&F0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   r<   r  r  )rR   r  r&  r0  placeholdersr  input_placeholderss          rW   r  z(ExportedProgram._check_input_constraintsh  s    J#'::#3#3Maqtt}7LMM L$*>*>*J*JK
1vv--- 
 

 	+ 3T5K5K	
 N
s   BB)B#c                 $    | j                          y rP   )	_validater  s    rW   r  zExportedProgram.validateu  s    rY   c                     t        | j                        dk(  rt        d      | j                  D ]  } |       j                  |         y )Nr   z0ExportedProgram must have at least one verifier.)r  r  r   check)rR   rN  s     rW   r+  zExportedProgram._validatez  s?    t~~!# !STT 	ACIIdO	rY   )r   r   r  c                $   t        ||j                  |||n| j                  t        j                  | j
                        t        j                  | j                        | j                  ||n| j                  ||	      S | j                  	      S )Nr  )
rE   r   r   r   rs  r  r  r   r   r  )rR   r   r   r   r   r  s         rW   _updatezExportedProgram._update  s     $$+%/%;z"mmD,B,BC"mmD,C,CD..#,#8idnn#,#8i

 
	
 ?Cnn

 
	
rY   )NNr  )NF)rs   rE   )@rZ   r[   r\   __doc__r   r   r   r^   r;   rv   ra   r   r]   rF   r   r   r   r+   r   ModuleGraphr  r   r  propertyr4   r   setterr   r   r   r   r  r   r  r   r  ra  r   r  r  r  r  r  r   r  r  r  r  r   r  rn   _opsOperatorBaser   r  r  PassTyper$  r  r  r   r+  r/  rb   rY   rW   rE   rE     sE    88'''O**Mc3hU99E_--J5c?DcN:;dBBLS0011:T(^$$Is) IM>B)H 26)HhhooS#X.)H xx~~)H .	)H
 ellUXX-?-???@)H 5)H  0)H eCHotCH~=>E)H 334t;)H X'$.)HV %0" 1 " %0V 1 V %0' 1 ' \\%0O 1 O %0% 1 % %0Y 1 Y %0  1   %0T 1 T %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 %2 %0
 1 
  %0S 1 S %0"# " 1 " __%0R 1 R %0* * 1 *
 ^^%0Q 1 Q %0 1  %0S 1 S %0 1  %0
 1 

 %0 1  %0S 1 S(.,-c ,-3 ,-3 ,-\
c 
S 
S 
 588+?+? 2
 $ HL,1D
5::22H<=DD
 &*D
 
	D
 $D
LdX d:K dL
 %0 1   
 

rY   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   r   SymIntr$  )r   r$  valsr-   r  rN  s         rW   r	  r	    s     HHNN99==%1 			%D 
 / &I""" $a&66###$s   2B#old_range_constraintszdict[sympy.Symbol, Any] | Noner  c                 N   |t        d      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 )Nz&old_range_constraints must not be None)r   r	  r   r   replacementsvar_to_range)r   r<  r   r  r   rN  s         rW   r  r    s     $EFF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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.r  )
stacklevel)r   r   r   SyntaxErrorwarningswarnr2  _graph)r  r   r   s      rW   r  r    s}    XX!!$." I!   	G 	
 XX!!$(89	I!s    $ A#B
Bc                 (   t        |       }|g S |j                  j                         D ch c]  \  }}t        d |D              r| }}}t        j
                  j                  j                  j                  |j                  d |j                        }|j                  D cg c]B  }|j                  j                  j                  |      r|j                  |j                        D }}|D cg c]	  }d|vs| c}S c c}}w c c}w c c}w )Nc              3   z   K   | ]3  }t        |t        j                  j                  j                          5 y wrP   )r   r   r  rL  ConstantSource)r  rL  s     rW   r  z*_convert_guards_to_code.<locals>.<genexpr>  s2      
 65==#7#7#F#FGG
s   9;c                     | j                   S rP   r   )r  s    rW   rp  z)_convert_guards_to_code.<locals>.<lambda>  s
    AFF rY   z	L['self'])r	  var_to_sourcesr   r  r   r   experimentalsymbolic_shapesShapeGuardPythonPrinterguardsexprfree_symbolsissubsetdoprint)r   r   varsources
local_vars
py_printerguardrets           rW   r  r    s
   |,I	 &44::<C 
!
 
 	J  &&66NN  "2I4L4LJ
 %%::""++J7 	5::&C   #?ek&>E??% @s   D&AD
3	D=D)rs   r3   rP   )r   r   dataclassesrl   r_  r  rC  collectionsr   collections.abcr   r   r   typingr   r   r	   r
   r9  r   r   torch._higher_order_ops.utilsr   "torch._library.fake_class_registryr   torch._subclasses.fake_implsr   r   r   torch._subclasses.fake_tensorr   torch.fx._symbolic_tracer   torch.fx._utilsr   torch.fx.graphr   r   torch.fx.immutable_collectionsr   r   torch.fx.passes.runtime_assertr   sympytorch.utils._sympy.value_rangesr   r   torch.utils._pytreeutils_pytreer_   r'  r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   torch._export.verifierr+   r-   r/   torch.export._tree_utilsr0   r1   torch.export.decomp_utilsr3   torch.fx._compatibilityr4   torch.fx.passes.infra.pass_baser5   "torch.fx.passes.infra.pass_managerr6   r   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   __all__r   r   r7  	dataclassrG   rF   rn   r   Contextr  rw   r   r   AutogradCPUAutogradCUDAAutogradMetaAutogradXLAAutogradLazyAutogradIPUAutogradXPUAutogradMPSAutogradHPUAutogradPrivateUse1AutogradPrivateUse2AutogradPrivateUse3r   CPUCUDAMetaXLALazyIPUXPUMPSHPUr   r   rv   r5  r6  r   r   rH   r  rO  r   rg  r]   r  r  rE   r	  r  r  r  rb   rY   rW   <module>r     s          # . % 8 8 1 B ? 
 9 ; ? 6 I J 
 ;  $ $    $ , * @ B 7 1 6 :   " UXX))*J,==> ' ' '  1 1 1
GtG>>G tG >>	G
 
G: 
HH$$	HH%%	HH%%	HH$$	HH%%	HH$$	HH$$	HH$$	HH$$	HH,,	HH,,	HH,,, (, 
HH	HH	HH	HH	HH	HH	HH	HH	HH
  P$ P$f,-uzz..89,-
4

''12C78,-^b2b2 

//9:b2 ejj55x?@	b2
 Djb2J##3G#
588!556#2..:x!HH  x!-x! 	x! *	x!
  0x!v4 

//9:4 ejj55x?@	4
 Dj4 "&4n}	
 }	
@$$ ?C; 0,@rY   