ó
    Eñi„  ã                   óˆ  • S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
r
S SKrS SKJs  Jr  S SKJrJrJr  S SKJr  S SKJr  S SKJrJr  S S	KJr  S S
KJrJr  S SK J!r!J"r"  S SK#J$r$  S SK%J&r&  SSK'J(r(  SSK)J*r*  SSK+J,r,J-r-J.r.J/r/  S\R`                  S\R`                  S\14S jr2S\R`                  S\34S jr4S\3\5   4S jr6S\3\5   4S jr7S\3\5   S\3\Rp                     4S jr9\Rt                  Rv                  S 5       r<S r=S \R|                  R~                  S!\\5S-     S\@\A\5\R|                  R„                  4   \A\5\R|                  R„                  4   4   4S" jrCS \R|                  R~                  S#\\5S-     S$\A\5\R|                  R„                  4   S%\A\5\R|                  R„                  4   SS4
S& jrD SDS\R`                  S'\R`                  S-  S(\3\5   S-  S\4S) jjrE SDS \R|                  R~                  S!\\5S-     S#\\5S-     S\R`                  S'\R`                  S-  S(\3\5   S-  4S* jjrFS+\R|                  R~                  S,\-S-\A\5\	4   S.\A\5\	4   SS4
S/ jrG " S0 S1\H5      rI " S2 S3\R|                  R~                  \IS49rJS5\R|                  R~                  S6\,S\J4S7 jrKS8 rLS9\5S:\3\	   4S; jrMS<\3\R|                  R„                     S=\A\
Rœ                  \&4   S>\3\Rp                     4S? jrOS@ rP SES6\,S\R|                  R~                  4SA jjrQ " SB SC\R¤                  R¦                  5      rTg)Fé    N)ÚSequence)Úchain)ÚAny)Ú,_enter_enable_graph_inputs_of_type_nn_moduleÚ+_exit_enable_graph_inputs_of_type_nn_moduleÚ#_get_graph_inputs_of_type_nn_module)Ú_convert_range_to_int)Ú"_check_input_constraints_for_graph)Ú_assign_attrÚ	_AttrKind)Ú!_pytree_subclasses_that_lose_info)Ú_PyTreeCodeGenÚ_PyTreeInfo)Ú
NodeSourceÚNodeSourceAction)Ú	try_solve)ÚValueRangesé   )Ú_remove_effect_tokens)Úreorder_kwargs)ÚExportedProgramÚExportGraphSignatureÚ	InputKindÚ
OutputKindÚselfÚotherÚreturnc                 ó*   ^^• S mUU4S jmT" X5      $ )z›
Refinement of TreeSpec.__eq__ where, e.g., torch.Size(...) matches tuple(...).
See _pytree_subclasses_that_lose_info in proxy_tensor.py for more details.
c                 ó@   • [        [        R                  " X 5      5      $ ©N)Ústrr   Úget)Úts    ÚQ/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/export/_unlift.pyÚ_normalize_typeÚ eq_spec.<locals>._normalize_type.   s   € ÜÔ4×8Ò8¸Ó>Ó?Ð?ó    c                 óæ  >• XL a  gT" U R                   5      T" UR                   5      :w  a  gU R                   [        L aA  UR                   [        L a.  [        U R                  5      [        UR                  5      :w  a  gOU R                  UR                  :w  a  gU R                  UR                  :w  a  g[        U4S j[        U R                  5       UR                  5       5       5       5      $ )NTFc              3   ó8   >#   • U  H  u  pT" X5      v •  M     g 7fr    © )Ú.0ÚaÚbÚ_match_normalized_structures      €r$   Ú	<genexpr>Ú?eq_spec.<locals>._match_normalized_structure.<locals>.<genexpr>>   s"   øé € ð 
â7‘�ñ (¨×-Ð-Ú7ùs   ƒ)ÚtypeÚdictÚsetÚcontextÚnum_childrenÚallÚzipÚchildren)r,   r-   r.   r%   s     €€r$   r.   Ú,eq_spec.<locals>._match_normalized_structure1   s°   ø€ ØŠ6ØÙ˜1Ÿ6™6Ó"¡o°a·f±fÓ&=Ó=ØØ�6‰6”TŠ>˜aŸf™f¬šnä�1—9‘9‹~¤ Q§Y¡Y£Ó/Øð 0à�Y‰Y˜!Ÿ)™)Ó#ØØ�>‰>˜QŸ^™^Ó+ØÜô 
ä˜AŸJ™J›L¨!¯*©*«,Ô7ó
ó 
ð 	
r'   r*   )r   r   r.   r%   s     @@r$   Úeq_specr:   (   s   ù€ ò@ö
ñ$ ' tÓ3Ð3r'   Úin_specc                 ó’   • [        X5      n[        R                  " X45      u  pE[        XR5      (       d  [	        SU SU S35      eU$ )Nz>Trying to flatten user inputs with exported input tree spec: 
z-
but actually got inputs with tree spec of: 
zr.
Please check that the inputs have the same number and type of args and kwargs as the ones you used when tracing.)r   ÚpytreeÚtree_flatten_with_pathr:   Ú
ValueError)ÚargsÚkwargsr;   Úreordered_kwargsÚflat_args_with_pathÚreceived_specs         r$   Ú_check_inputs_matchrE   F   sg   € Ü% fÓ6ÐÜ)/×)FÒ)FØ	Ð ó*Ñ&Ðô �=×*Ñ*ÜØMØˆið <àˆoð AðAó
ð 	
ð Ðr'   Úep_guards_codec                 óþ   • 0 n[        U5       H%  u  p4S[        R                  " U5       3USU S3'   M'     / nU  H>  nUR                  5        H  u  pxUR	                  Xx5      nM     UR                  U5        M@     U$ )NÚLúL['flat_args'][Ú])Ú	enumerater=   ÚkeystrÚitemsÚreplaceÚappend)	rF   Úinput_pathsÚname_mappingÚidxÚpathÚnew_guards_codeÚguardÚold_nameÚnew_names	            r$   Ú_force_ep_signature_matchrX   Y   s‰   € ð €LÜ˜{Ö+‰	ˆØ34´V·]²]À4Ó5HÐ4IÐ1Jˆ� s e¨1Ð-Ó.ñ ,ð €OÛˆØ".×"4Ñ"4Ö"6ÑˆHØ—M‘M (Ó5ŠEñ #7à×Ñ˜uÖ%ñ  ð
 Ðr'   c                 óˆ  • SSK n[        5       nU  H$  nUR                  UR                  SU5      5        M&     [        5       nUR                   H$  nUR                  UR                  SU5      5        M&     U nU H>  nX…;   d  M
  Uu  pšU Vs/ s H#  nUR                  SU	 SU
 S3SU	 SU
 S	35      PM%     nnM@     U$ s  snf )
a8  
The signature of the originally exported module may not match
the signature of the unlifted graph module extracted from the
exported program. The guards code extracted from the exported
program is based on the former, but the generated guards fn is
based on the latter; thus we need to reconcile any such diff.
r   NzL\[\'([^\']+)\'\]\[([0-9]+)\]z(.+)_([0-9]+)zL['z'][rJ   Ú_z'])Úrer3   ÚupdateÚfindallÚ
parametersrN   )rF   Ú	signaturer[   Úorig_arg_namesÚgÚsig_arg_namesÚnrT   ÚmatchÚbaserR   s              r$   Ú_force_gm_signature_matchrf   p   sß   € ó ô “U€NÛˆà×Ñ˜bŸj™jÐ)IÈ1ÓMÖNñ ô “E€MØ×!Ô!ˆà×Ñ˜RŸZ™ZÐ(8¸!Ó<Ö=ñ "ð
 %€OÛˆØÕ!Ø‰IˆDñ )óâ(�Að —	‘	˜C ˜v S¨¨¨QÐ/°3°t°f¸A¸c¸UÀ"Ð1EÖFÙ(ð ð ‰Oñ  ð Ðùòs   Â*B?Úguards_codeÚpaths_of_placeholdersc                 óØ  • SSK nSSKJn  / n/ nU  HÈ  nXfp‡[        U5       H‚  u  pšUR	                  S[
        R                  " U
5      -   SU	 S35      nUR	                  S[
        R                  " U
5      -   U
S   R                  [
        R                  " U
SS 5      -   5      nM„     UR                  U5        UR                  UR	                  SS	5      5        MÊ     S
n[        XE5       H0  u  pÍUR                  UR                  USS95      nUSU SU S3-  nM2     US-  n0 UEn[        X¿5        [        5       n[        R                  R!                  US   5      Ul        SUl        U$ )a  
Generates Python code given guards code and paths of placeholders.
We assume that, based on source information,
- the tracer generates the guards code
- the input spec generates the paths of placeholders.

Example:

Suppose we are given the guards code "L['z']['k'].size()[1] == 3"
and we are given that ['z']['k'] is the path of placeholder #2.
Then we will generate:
```
torch._assert(
    args[2].size()[0] == 3,
    "Guard failed: z['k'].size()[0] == 3",
)
```

FAQ: Why do we generate code based on (flattened) args instead of
the original (unflattened) inputs? Because this would require
inserting an additional pytree.unflatten call in our graph.

FAQ: Why do we not emit RuntimeError on guard failure as we used to?
Because it is inconvenient :/, get used to AssertionError instead.
r   N)ÚSYMPY_INTERPrH   zargs[rJ   r   Ú
Ú z
def _(*args):
Úeval)Úmodez  torch._assert(z, "Guard failed: z")
z	  return
rZ   T)ÚastÚ%torch.fx.experimental.symbolic_shapesrj   rK   rN   r=   rL   ÚkeyrO   r7   ÚunparseÚparseÚexecÚGuardsFnÚtorchÚ_dynamoÚdont_skip_tracingÚforwardÚ
_is_impure)rg   rh   ro   rj   Úactual_guards_codeÚshadow_guards_codeÚcr,   ÚsrR   rS   Úcode_strÚactualÚshadowÚ_shadowÚ	namespaceÚ	guards_fns                    r$   Ú_convert_guards_code_to_fnr…   “   sh  € ó< åBàÐØÐÛˆØˆ1Ü"Ð#8Ö9‰IˆCà—	‘	˜#¤§¢¨dÓ 3Ñ3°u¸S¸EÀ°^ÓDˆAà—	‘	Ø”f—m’m DÓ)Ñ)Ø�Q‘—‘œfŸmšm¨D°°¨HÓ5Ñ5óŠAñ	 :ð 	×!Ñ! !Ô$Ø×!Ñ! !§)¡)¨D°"Ó"5Ö6ñ ð #€HÜÐ0ÖE‰ˆð —+‘+˜cŸi™i¨°V˜iÐ<Ó=ˆàÐ& v hÐ.?À¸yÈÐMÑMŠñ Fð �Ñ€Hð !�<Ð €IÜˆÔô “
€IÜŸ™×7Ñ7¸	À#¹ÓG€IÔØ€IÔØÐr'   c                 óŒ   • [        XU R                  5      n[        U R                  R	                  SS9UU R
                  5        g )NÚplaceholder©Úop)rE   Ú_in_specr
   ÚgraphÚ
find_nodesÚrange_constraints)r   r@   rA   rC   s       r$   Ú#_check_input_constraints_for_modulerŽ   Þ   s<   € ä-¨d¸D¿M¹MÓJÐÜ&Ø�
‰
×Ñ ÐÐ/ØØ×Ñõr'   c                 óØ   • U R                   (       d  g [        U S5      (       a  [        XU R                  5        g [        R
                  R                  5       (       d  [        XU5        g g )NÚ
_guards_fn)Úvalidate_inputsÚhasattrrE   rŠ   rv   ÚcompilerÚis_dynamo_compilingrŽ   )r   r@   rA   s      r$   Ú!_check_input_constraints_pre_hookr•   è   sR   € à××Øô
 ˆt�\×"Ñ"Ü˜D¨$¯-©-Ô8Øô
 �>‰>×-Ñ-×/Ñ/Ü+¨D¸Õ?ð 0r'   ÚgmÚlifted_inputsc                 óv  • 0 n0 nU R                   R                   Vs/ s H  oDR                  S:X  d  M  UPM     nn[        U5      [        U5      :w  a$  [	        S[        U5       S[        U5       S35      e[        XQ5       GH  u  pgUc  XcUR                  '   M  U R                   R                  U5         [        R                  " 5          [        R                  " S5        U R                   R                  U5      nSSS5        UR                  W5        UR                  n	U R                   R                  U5        X˜l        [        US[         R"                  [         R$                  /5      /UR                  S'   X‚U'   SSS5        GM
     X#4$ s  snf ! , (       d  f       Nž= f! , (       d  f       GM5  = f)	zS
Unlift inputs referring to params/buffers/constants as getattr nodes in the
graph
r‡   zNumber of lifted inputs (z$) does not match placeholder nodes (Ú)NÚignoreú!ExportedProgram.module().unlift()Ú	from_node)r‹   Únodesr‰   ÚlenÚAssertionErrorr7   ÚnameÚinserting_afterÚwarningsÚcatch_warningsÚsimplefilterÚget_attrÚreplace_all_uses_withÚmetaÚ
erase_noder   r   ÚCREATEÚREPLACE)
r–   r—   Úunlifted_name_to_nodeÚinput_name_to_nodeÚnodeÚplaceholder_nodesÚ
input_nodeÚlifted_nodeÚgetattr_nodeÚmetadatas
             r$   Ú_unlift_inputs_as_getattrr³   û   sˆ  € ð ÐØÐà*,¯(©(¯.ª.ÓUª. $¿G¹GÀ}Ñ<TŸ©.ÐÐUÜ
ˆ=ÓœSÐ!2Ó3Ó3ÜØ'¬¨MÓ(:Ð';ð <"Ü"%Ð&7Ó"8Ð!9¸ð<ó
ð 	
ô $'Ð'8×#HÑˆ
ØÑØ2<˜zŸ™Ó/ð —‘×)Ñ)¨*Õ5ô ×,Ò,Õ.Ü×)Ò)¨(Ô3Ø#%§8¡8×#4Ñ#4°[Ó#A�L÷ /ð ×0Ñ0°Ô>Ø%Ÿ?™?�Ø—‘×#Ñ# JÔ/Ø$,Ô!äØ"Ø;Ü)×0Ñ0Ô2B×2JÑ2JÐKóð2�×!Ñ! +Ñ.ð 6B kÑ2÷% 6Ò5ñ $Ið2 !Ð4Ð4ùò? V÷ /Õ.ú÷	 6×5ús/   �F´FÂ7F(Ã2FÃ?BF(Æ
F%Æ!F(Æ(
F8	Úmutated_outputsr«   r¬   c                 ó¤  ^• U R                   R                  5       n[        R                  " UR                  5      S   n[        U5      [        U5      :w  a$  [        S[        U5       S[        U5       S35      e/ n0 m[        XQ5       H°  u  pxUc  UR                  U5        M  X‚;   a  X(   n	OXƒ;   a  X8   n	O[        SU S35      eU R                   R                  U5         U R                   R                  [        R                  R                  R                  R                   X—45      n
U
TU'   SSS5        M²     [#        U4S jU 5       5      nU R                   R                  U5         U R                   R%                  U5      nUR'                  U5        U R                   R)                  U5        UR*                  Ul        UR,                  R/                  UR,                  5        [1        US	[2        R4                  [2        R6                  /5      /UR,                  S
'   SSS5        g! , (       d  f       GM´  = f! , (       d  f       g= f)zh
Find the all the buffers and inputs that were mutated and insert copy_
operators to reflect mutations.
r   zNumber of outputs (z") does not match mutated outputs (r™   NzCould not find z  in either buffer or input nodesc              3   óF   >#   • U  H  nTR                  X5      v •  M     g 7fr    )r"   )r+   r­   Úreturn_nodes_to_copys     €r$   r/   Ú-_insert_copy_for_mutations.<locals>.<genexpr>P  s$   øé € ð Ú9J°Ð× Ñ  ×,Ð,Ò9Jùs   ƒ!r›   rœ   )r‹   Úoutput_noder=   Útree_flattenr@   rž   rŸ   r7   rO   ÚRuntimeErrorÚinserting_beforeÚcall_functionrv   ÚopsÚatenÚcopy_ÚdefaultÚtupleÚoutputr¦   r¨   r    r§   r\   r   r   r©   rª   )r–   r´   r«   r¬   r¹   ÚoutputsÚuser_output_nodesÚreturn_nodeÚmutated_node_nameÚmutated_nodeÚ	copy_nodeÚoutput_argsÚ
new_outputr·   s                @r$   Ú_insert_copy_for_mutationsrÌ   (  s  ø€ ð —(‘(×&Ñ&Ó(€KÜ×!Ò! +×"2Ñ"2Ó3°AÑ6€GÜ
ˆ7ƒ|”s˜?Ó+Ó+ÜØ!¤# g£, ð 0 Ü # OÓ 4Ð5°Qð8ó
ð 	
ð
 ÐØÐÜ*-¨gÖ*GÑ&ˆØÑ$Ø×$Ñ$ [Ô1ÙàÓ5Ø0ÑC‰LØÓ4Ø-Ñ@‰LäØ!Ð"3Ð!4Ð4TÐUóð ð �X‰X×&Ñ& {Õ3ØŸ™×.Ñ.Ü—	‘	—‘×$Ñ$×,Ñ,¨|Ð.IóˆIð 1:Ð  Ñ-÷	 4Ñ3ñ +Hô( ô Ù9Jóó €Kð 
�‰×	"Ñ	" ;Õ	/à—X‘X—_‘_ [Ó1ˆ
Ø×)Ñ)¨*Ô5Ø
�‰×Ñ˜KÔ(Ø%×*Ñ*ˆ
ŒØ�‰×Ñ˜{×/Ñ/Ô0äØØ3Ü!×(Ñ(Ô*:×*BÑ*BÐCóð(
ˆ
�‰˜Ñ$÷ 
0Ð	/÷ 4×3ú÷ 
0Õ	/ús   Ã#AH.Å.B7IÈ.
H>	É
IÚout_specÚforward_arg_namesc                 ó8  • U(       a  UnOñU R                   [        L a¶  U R                  S:X  a¦  U R                  S5      R                   [        L a„  U R                  S5      R                   [        L ab  [        U R                  S5      R                  5       Vs/ s H  nSU 3PM
     nnUR                  U R                  S5      R                  5        O([        U R                  5       Vs/ s H  nSU 3PM
     nn[        [        UU U5      5      $ s  snf s  snf )zC
Create the codegen for the graph module based on the in/out specs
é   r   r   Úarg_)
r1   rÂ   r5   Úchildr2   ÚrangeÚextendr4   r   r   )r;   rÍ   rÎ   ÚnamesÚis        r$   Ú_get_codegenr×   c  sò   € ö Ø!‰à�‰œÒØ× Ñ  AÓ%Ø�M‰M˜!Ó×!Ñ!¤UÒ*Ø�M‰M˜!Ó×!Ñ!¤TÒ)ô &+¨7¯=©=¸Ó+;×+HÑ+HÔ%IÓJÒ%I �4˜�s“Ñ%IˆÐJà�‰�W—]‘] 1Ó%×-Ñ-Õ.ä%*¨7×+?Ñ+?Ô%@ÓAÒ%@ �4˜�s“Ñ%@ˆÐAäÜØØØó	
óð ùò Kùò Bs   ÂDÃ+Dc                 óÂ   • [        X5      u  pg[        XXg5        [        X4U5      U R                  l        U R                  R                  5         U R                  5         U $ )aÙ  
Args:
    lifted_inputs: A list matching the graph module's input nodes. For
    an input node that is referring to a lifted parameter/buffer, this
    list will contain the fqn the corresponding attribute. Otherwise, this
    list will contain None. This is used to unlift the lifted parameters as
    get_attr nodes.

    mutated_outputs: A list matching the graph module's output nodes. For
    an output node that is referring to a mutated buffer or user input, this
    list will contain the name of the corresponding buffer or user input
    that needs to be mutated. Otherwise, this list will contain None. This
    is used to re-insert an inplace copy_ operator to copy the mutated
    values back to the original node.
)r³   rÌ   r×   r‹   Ú_codegenÚlintÚ	recompile)r–   r—   r´   r;   rÍ   rÎ   r«   r¬   s           r$   Ú_unliftrÜ   ƒ  sW   € ô. 1JØ
ó1Ñ-Ðô Ø
Ð2ôô % WÐ8IÓJ€B‡H�HÔØ‡H�H‡M�M„OØ‡L�L„NØ€Ir'   Únew_gmÚgraph_signatureÚ
state_dictÚ	constantsc           	      ó˜  • [        UR                  5      nUR                   H/  nXT;   a  SnX5   nOSnX%   n[        XpU[        R
                  US9  M1     UR                   H!  nX%   n[        UU U[        R                  S9  M#     [        UR                  UR                  5       H!  nX5   n[        UU U[        R                  S9  M#     g )NFT©Ú	attr_kindÚ
persistent©rã   )r3   Únon_persistent_buffersÚbuffersr   r   ÚBUFFERr^   Ú	PARAMETERr   Úlifted_custom_objsÚlifted_tensor_constantsÚCONSTANT)rÝ   rÞ   rß   rà   ræ   r    rä   Úvalues           r$   Ú_register_attrs_to_new_gmrî   ¦  sÔ   € ô ! ×!GÑ!GÓHÐØ×'Ô'ˆØÓ)ØˆJØ‘O‰EàˆJØÑ$ˆEÜØ˜4¬9×+;Ñ+;È
ô	
ñ (ð  ×*Ô*ˆØÑ ˆÜØØØÜ×)Ñ)ô		
ñ +ô Ø×*Ñ*¨O×,SÑ,Söˆð ‘ˆÜØØØÜ×(Ñ(ô		
ò	r'   c                   ó6   ^ • \ rS rSrSrS rSU 4S jjrSrU =r$ )Ú_StatefulGraphModuleFactoryiÏ  zG
Metaclass that ensures a private constructor for _StatefulGraphModule
c                 óL   • [        U R                   SU R                   S35      e)NÚ.z has no public constructor. )Ú	TypeErrorÚ
__module__Ú__qualname__)Úclsr@   rA   s      r$   Ú__call__Ú$_StatefulGraphModuleFactory.__call__Ô  s,   € ÜØ�~‰~Ð˜a × 0Ñ 0Ð1Ð1MÐNó
ð 	
r'   c                 ó"   >• [         TU ]  UUUS9$ )N©r�   )Úsuperr÷   )rö   Úrootr‹   r�   Ú	__class__s       €r$   Ú_createÚ#_StatefulGraphModuleFactory._createÙ  s#   ø€ Ü‰wÑØØØ/ð  ð 
ð 	
r'   r*   r    )	Ú__name__rô   rõ   Ú__firstlineno__Ú__doc__r÷   rþ   Ú__static_attributes__Ú__classcell__©rý   s   @r$   rð   rð   Ï  s   ø† ñò
÷

õ 
r'   rð   c                   ó,   ^ • \ rS rSrSU 4S jjrSrU =r$ )Ú_StatefulGraphModuleiá  c                 óR   >• [         TU ]  X5        U=(       d    / U l        SU l        g )NT)rû   Ú__init__r�   r‘   )r   rü   r‹   r�   rý   s       €r$   r	  Ú_StatefulGraphModule.__init__â  s%   ø€ Ü‰Ñ˜Ô%à!2×!8°bˆÔØ#ˆÕr'   )r�   r‘   r    )r   rô   rõ   r  r	  r  r  r  s   @r$   r  r  á  s   ø† ÷$õ $r'   r  )Ú	metaclassÚplain_graph_moduleÚepc           	      óÄ  ^• [         R                  U U R                  US9n[        UR                  5      mUR                  U4S j5        UR                  [        SS9  UR                  U4S jSS9  0 nUR                  R                   H´  nUR                  U5      nUR                  (       a/  [        R                  " SU S3S	S
9  UR                  5       nXtU'   UnUR                  S5      Gt p‰[         R"                  R$                  R'                  X85      n
[)        X©5        [+        XcU[,        R.                  S9  M¶     UR0                  R3                  5        H¸  u  p¼[         R"                  R$                  R5                  X;5      (       a  M5  [7        U[         R8                  5      (       aJ  UR                  (       a9  [        R                  " SU S3S	S
9  XÄ;   a  XL   nOUR                  5       nXÔU'   Un[+        UUU[,        R.                  S9  Mº     UR                  R:                   H-  n[+        U R                  U5      UU[,        R<                  SS9  M/     U$ )Nrú   c                  ó   >• [        T5      $ r    )r   ©r@   rA   Úmodule_typess     €r$   Ú<lambda>Ú/_create_stateful_graph_module.<locals>.<lambda>ö  s   ø€ Ô LØô!
r'   T)Úwith_kwargsc                  ó   >• [        T5      $ r    )r   r  s     €r$   r  r  ÿ  s   ø€ Ô KØô!
r'   )Úalways_callzA model attribute `z°` requires gradient. but it's not properly registered as a parameter. torch.export will detach it and treat it as a constant tensor but please register it as parameter instead.rÐ   )Ú
stacklevelrò   rå   z¯` requires gradient but it's not properly registered as a parameter. torch.export will detach it and treat it as a constant tensor but please register it as parameter instead.Frâ   )r  rþ   r‹   r   Úexample_inputsÚregister_forward_pre_hookr•   Úregister_forward_hookrÞ   rë   Ú
get_bufferÚrequires_gradr¢   ÚwarnÚdetachÚrsplitrv   ÚfxÚgraph_moduleÚ_get_attr_via_attr_listÚdelattrr   r   rì   rà   rM   Ú	_has_attrÚ
isinstanceÚTensorræ   rè   )r  r�   r  Ústateful_gmÚ"original_tensor_to_detached_tensorÚconstant_fqnÚbufferÚdetached_bufferÚprefixÚfieldÚsubmodÚ
const_namerí   Údetached_valuer  s                 @r$   Ú_create_stateful_graph_moduler1  é  sE  ø€ ô
 '×.Ñ.ØØ× Ñ Ø+ð /ð €Kô 7°r×7HÑ7HÓI€LØ×)Ñ)ô	
ôð
 ×)Ñ)Ü)°tð *ñ ð ×%Ñ%ô	
ð ð	 &ñ ð *,Ð&ð ×*Ñ*×BÔBˆð ×'Ñ'¨Ó5ˆØ××Ü�MŠMØ% l ^ð 4?ð @ð òð %Ÿm™m›oˆOØ9H¨vÑ6Ø$ˆFØ%×,Ñ,¨SÓ1‰ˆÜ—‘×&Ñ&×>Ñ>¸{ÓSˆÜ�ÔÜ�V¨,Ä)×BTÑBTÔUñ+ Cð0  Ÿ\™\×/Ñ/Ö1Ñˆ
Ü�x‰x×$Ñ$×.Ñ.¨{×GÓGÜ˜%¤§¡×.Ñ.Ø×&×&Ü—M’MØ-¨j¨\ð :Gð Hð $%òð ÓBØ BÑ I™à).¯©«˜ØDR¸5ÑAØ .˜ÜØØØÜ#×,Ñ,ô	ñ# 2ð6 ×$Ñ$×;Ô;ˆÜØ×)Ñ)¨&Ó1ØØÜ×&Ñ&Øô	
ñ <ð Ðr'   c                 óÌ   • U u  p#UR                   " U0 UD6nUR                  5         UR                  n[        R                  " U5      nU VVs/ s H  u  pxUPM	     snn$ s  snnf )zÛ
Generate paths of placeholders, needed for generating the guards function.

NOTE: Here we make use of the example inputs used for export as well as
the signature of the unlifted graph module (not preserved by export).
)ÚbindÚapply_defaultsÚ	argumentsr=   Útree_leaves_with_path)	r  r_   r@   rA   ÚbindedÚctxÚflat_example_inputs_with_pathsrS   rZ   s	            r$   Ú_get_input_pathsr:  P  sb   € ð "�L€DØ�^Š^˜TÐ, VÑ,€FØ
×ÑÔØ
×
Ñ
€CÜ%+×%AÒ%AÀ#Ó%FÐ"Ù >Ô?Ò >‘W�T‹DÑ >Ò?Ð?ùÓ?s   ÁA Ú
result_strÚflat_input_pathsc                 óÊ   • 0 n[        U5       H%  u  p4S[        R                  " U5       3USU S3'   M'     U nUR                  5        H  u  pgUR	                  Xg5      nM     U$ )zn
Given user specified input paths, maybe fix up the guard string
to reflect user path instead of tracer path.
rH   rI   rJ   )rK   r=   rL   rM   rN   )r;  r<  rQ   rR   rS   rN   rq   Úvals           r$   Ú_replace_sourcesr?  `  sn   € ð
 €LÜÐ/Ö0‰	ˆØ34´V·]²]À4Ó5HÐ4IÐ1Jˆ� s e¨1Ð-Ó.ñ 1ð €GØ ×&Ñ&Ö(‰ˆØ—/‘/ #Ó+Šñ )à€Nr'   Úplaceholdersr�   Úpaths_for_placeholdersc                 óò  ^^^^• / m/ m0 mUUUU4S jn[        X5       GHJ  u  pES[        R                  " U5      -   nUR                  S   n[	        U[
        5      (       a  TR                  U SU 35        [	        U[        5      (       am  U[        R                  :X  a  TR                  U S35        M˜  U[        R                  * :X  a  TR                  U S35        MÃ  TR                  U SU 35        MÛ  [	        U[        5      (       a  TR                  U SU S35        GM
  [	        U[        R                  5      (       a:  UR                  R                  T;   a   U" UR                  R                  U5        GMc  [	        U[        R                  5      (       d  GM…  [!        UR"                  5       H¬  u  p‰S[        R                  " U5      -   S	U S
3-   n[	        U	[
        5      (       a  TR                  U SU	 35        MR  [	        U	[        R                  5      (       d  Ms  U	R                  R                  T;   d  M�  U" U	R                  R                  U5        M®     GMM     0 n
[        R$                  R&                  R(                  R+                  5       nT HÏ  u  pl[-        [/        UR0                  5      5      nUT;   a  M+  XÚ;   a\  UR3                  U
5      nTR                  UR5                  [6        R8                  " U[6        R:                  " U5      5      5      5        MŒ  [=        [6        R8                  " U[6        R:                  " U5      5      U5      nUc  MÆ  US   nUX­'   MÑ     T$ )a   
Guards generated by the tracer include conditions observed in code, but
but do not include some additional checks we typically do in export.
For example, when dynamic shapes get specialized, are specified to be
within a range, or are specified to be in some equational relation,
corresponding input invalidation is done within a pre_hook, specifically,
`_check_input_constraints_for_graph`.

Here we generate guards corresponding to the checks that happen in
`_check_input_constraints_for_graph`, and add them to the guards already
generated by the tracer. In the future, it may be worthwhile to separate
them so that we can allow clients to turn off one but not the other.
(Looking at you, AOTI.)

NOTE: We should eventually reconcile this logic with `build_guards` that
is used by AOT Precompile.
c                 óX  >• [        U R                  5      S:X  a  TR                  X45        U T;   a  TU    nTR                  U SU 35        g UTU '   [        TU    5      u  p4US:”  a  TR                  U SU 35        U[        R
                  :  a  TR                  U SU 35        g g )Nr   ú == rÐ   z >= z <= )rž   Úfree_symbolsrO   r	   ÚmathÚinf)	ÚexprÚsrcÚorig_srcÚmin_valÚmax_valÚdeferred_expressionsrT   r�   Úsourcess	        €€€€r$   Úhandle_symintÚ2_get_input_guards_for_graph.<locals>.handle_symintŠ  s¹   ø€ Üˆt× Ñ Ó! QÓ&ð !×'Ñ'¨¨Ô4Ø�7‹?ð ˜t‘}ˆHØ×"Ñ" c U¨$¨x¨jÐ#9Õ:àˆG�D‰Mä4Ð5FÀtÑ5LÓMÑˆGØ˜‹{Ø×&Ñ&¨#¨¨d°7°)Ð'<Ô=ØœŸ™Ó!Ø×&Ñ&¨#¨¨d°7°)Ð'<Õ=ð "r'   rH   r>  rD  z == math.infz == -math.infz == 'Ú'z.size()[rJ   r   )r7   r=   rL   r§   r%  ÚintrO   ÚfloatrF  rG  r!   rv   ÚSymIntr­   rH  r&  rK   ÚshapeÚutilsÚ_sympyÚprintersÚPythonPrinterÚnextÚiterrE  ÚsubsÚdoprintÚsympyÚEqÚSymbolr   )r@  r�   rA  rO  r‡   rS   rI  r§   rÖ   ÚdimÚunification_mapÚ
py_printerrH  ÚsymbolÚsubstitutionÚsolutionÚ
definitionrM  rT   rN  s    `               @@@r$   Ú_get_input_guards_for_graphrh  o  sÁ  û€ ð. ÐØ€OØ%'€G÷>ð >ô* ! ×FÑˆØ”F—M’M $Ó'Ñ'ˆØ×Ñ Ñ&ˆä�dœC× Ñ Ø×"Ñ" c U¨$¨t¨fÐ#5Ô6Ü�dœE×"Ñ"Ø”t—x‘xÓØ×&Ñ&¨#¨¨lÐ';Ö<Øœ$Ÿ(™(˜Ó"Ø×&Ñ&¨#¨¨mÐ'<Ö=à×&Ñ&¨#¨¨d°4°&Ð'9Ö:Ü˜œc×"Ñ"Ø×"Ñ" c U¨%°¨v°QÐ#7×8ä˜œeŸl™l×+Ñ+°·	±	·±ÐBSÓ0SÙ˜$Ÿ)™)Ÿ.™.¨#×.Ü˜œeŸl™l×+Ô+Ü# D§J¡JÖ/‘�ØœFŸMšM¨$Ó/Ñ/°H¸Q¸C¸q°/ÑA�Ü˜c¤3×'Ñ'à#×*Ñ*¨c¨U°$°s°eÐ+<Ö=ä˜s¤E§L¡L×1Ó1°c·h±h·m±mÐGXÕ6Xñ " #§(¡(§-¡-°Ö5ô 0ñ' Gð< 79€OÜ—‘×#Ñ#×,Ñ,×:Ñ:Ó<€Jó *‰	ˆä”d˜4×,Ñ,Ó-Ó.ˆØ�WÓñ ð Ó$ð  Ÿ9™9 _Ó5ˆLØ×"Ñ"Ø×"Ñ"¤5§8¢8¨L¼%¿,º,ÀsÓ:KÓ#LÓMöô !¤§¢¨$´·²¸SÓ0AÓ!BÀFÓKˆHØÓ#Ø% a™[�
Ø*4�Ó'ñ1 *ð4 Ðr'   c                  ó–   ^• / SQn [         R                  " 5       mTb,  [        U4S jU  5       5      (       a  gTR                  mTb  M,  g)N)Ú
executorchÚmodaiÚon_device_aiÚtorchaoc              3   óT   >#   • U  H  oTR                   R                  ;   v •  M     g 7fr    )Úf_codeÚco_filename)r+   rS   Úframes     €r$   r/   Ú,_ok_to_generate_guards_fn.<locals>.<genexpr>ë  s   øé € ÐEºH°D�u—|‘|×/Ñ/Ö/ºHùs   ƒ%(FT)ÚinspectÚcurrentframeÚanyÚf_back)Úpatternsrq  s    @r$   Ú_ok_to_generate_guards_fnrx  Þ  sJ   ø€ ò€Hô × Ò Ó"€EØ
Ñ
ÜÔE¹HÓE×EÑEØØ—‘ˆð Ó
ð
 r'   c           	      ód  • U=(       a
    [        5       nU R                  R                   Vs0 s H!  o"R                  S:w  d  M  UR                  U_M#     nnU R                  R                   Vs0 s H!  o"R                  S:X  d  M  UR
                  U_M#     nn[        R                  R                  U R                  [        R                  " U R                  5      5      nUR                  R                  U R                  R                  5        [        R                  " U 5      n [        U R                  R                   U R                  R"                  5      U l        XPl        U R&                  S   R(                  S:w  a  [+        U 5      n [-        XPR.                  U R0                  U R2                  5        U R4                  S   R6                  =n(       a  UR8                  OS nU R.                  R                    Vs/ s H]  nUR:                  [<        R>                  [<        R@                  [<        RB                  [<        RD                  4;   a  UR
                  OS PM_     n	nU R.                  R"                   V
s/ s HN  n
U
R:                  [F        RH                  [F        RJ                  [F        RL                  4;   a  U
R
                  OS PMP     nn
UR                  R                   H…  nS nUR                  S:X  a  URO                  UR
                  5      nO+UR                  U;   a  URO                  UR                  5      n[Q        US[R        RT                  5      /UR                  S'   M‡     U RV                  RX                  c  [[        S5      e[]        UU	UU RV                  RX                  U RV                  R^                  US9n[a        XPRb                  U 5      nUR                  R                  U R                  R                  5        UR                  nURe                  SS9nU(       GaZ  U(       GaR  U Rf                  (       Ga@  [h        R6                  " URj                  5      n[m        U Rf                  U5      n/ nU Rn                   H  nURq                  [s        UU5      5        M      [u        XðRb                  U5      n[w        U Rn                  U5      n[y        UU5      nUR{                  U5        [}        UU5      Ul?        [        R                  R€                  Rƒ                  U5      nUR…                  US	   5         UR‡                  S
[‰        U5      5      nUUR                  S'   S S S 5        UR‹                  5         U$ s  snf s  snf s  snf s  sn
f ! , (       d  f       N4= f)Nr‡   r   ÚTRAININGzExportedProgram.module()rœ   z#ep.call_spec.in_spec cannot be None)rÎ   rˆ   éÿÿÿÿr�   Únn_module_stack)Frx  r‹   r�   r‰   r    Útargetrv   r   ÚGraphModuler!  ÚcopyÚdeepcopyr§   r\   r   Ú_graph_signatureÚinput_specsÚoutput_specsÚ_graph_moduleÚ	verifiersÚdialectr   rî   rÞ   rß   rà   Úmodule_call_graphr_   rÎ   Úkindr   rè   ÚCONSTANT_TENSORré   Ú
CUSTOM_OBJr   ÚBUFFER_MUTATIONÚUSER_INPUT_MUTATIONÚPARAMETER_MUTATIONr"   r   r   r©   Ú	call_specr;   rŸ   rÜ   rÍ   r1  r�   rŒ   r  rs  ry   r:  Ú_guards_coderO   r?  rh  rX   rf   rÔ   r…   r�   Ú_utilsÚ#first_call_function_nn_module_stackr¡   Úcall_modulerÂ   rÛ   )r  Úcheck_guardsr­   Úsource_node_dictÚplaceholder_source_node_dictrÝ   ÚsigrÎ   r;   r—   rÍ   r´   Úsource_nodeÚ	unlift_gmr‹   r@  rP   Ú	ep_guardsrU   rg   rF   Úroot_nn_module_stacks                         r$   Ú&_unlift_exported_program_lifted_statesr›  ò  s¾  € ð  ×?Ô$=Ó$?€Lð %'§H¡H§N¢NóÚ$2˜D·g±gÀÑ6N‹ˆ�	‰	�4Š¡Nð ð ð
 ')§h¡h§n¢nó$Ú&4˜d¿¹À=Ñ8PÓˆ�‰�TÒ¡nð !ð $ô �X‰X×!Ñ! "§/¡/´4·=²=ÀÇÁÓ3JÓK€FØ
‡K�K×Ñ�r—‘×+Ñ+Ô,Ü	�Š�2‹€BÜ.Ø
×Ñ×'Ñ'¨×)<Ñ)<×)IÑ)Ió€BÔð Ôð 
‡|�|�A�×Ñ *Ó,Ü" 2Ó&ˆä˜f×&8Ñ&8¸"¿-¹-ÈÏÉÔVà)+×)=Ñ)=¸aÑ)@×)JÑ)JÐ"J #Õ"Jˆ×ÒÐQUð ð ×)Ñ)×5Ò5ó'ò 6ˆGð �|‰|ä× Ñ Ü×)Ñ)Ü×#Ñ#Ü×$Ñ$ð	óð �NŠNð òñ 6ð ð 'ð4 ×*Ñ*×7Ò7ó)ò 8ˆHð �}‰}ä×*Ñ*Ü×.Ñ.Ü×-Ñ-ðóð �OŠOð òñ 8ð ð )ð —‘×"Ô"ˆØˆØ�7‰7�mÓ#Ø6×:Ñ:¸4¿;¹;ÓG‰Kà�y‰yÐ,Ó,Ø.×2Ñ2°4·9±9Ó=�äØØ*Ü ×'Ñ'óð"
ˆ�	‰	�+Óñ #ð 
‡|�|×ÑÑ#ÜÐBÓCÐCÜØØØØ
�‰×ÑØ
�‰×ÑØ+ñ€Fô .¨f×6JÑ6JÈBÓO€IØ‡N�N×Ñ˜"Ÿ/™/×.Ñ.Ô/ð �O‰O€EØ×#Ñ# }Ð#Ð5€LßŸ¨×):×):Ð):Ü×Ò 	× 1Ñ 1Ó2ˆÜ&Ø×ÑØó
ˆð ˆ	Ø—_”_ˆEØ×ÑÔ-¨e°[ÓAÖBñ %ô 2Ø×.Ñ.°ó
ˆô 3°2·?±?ÀKÓPˆÜ2°>À3ÓGˆØ×Ñ˜>Ô*Ü9¸+À{ÓSˆ	Ôä$Ÿx™xŸ™×RÑRØó 
Ðð ×"Ñ" <°Ñ#3Õ4Ø×$Ñ$ \´5¸Ó3FÓGˆDØ+?ˆD�I‰IÐ'Ñ(÷ 5ð 	×ÑÔàÐùòuùò$ùò('ùò)÷Z 5Õ4ús1   ¬VÁVÁ.VÂVÇ=A$VÉ;AVÕ+V!Ö!
V/c                   ó   • \ rS rSrSrS rSrg)ru   it  z#
Module class for guard functions.
c                 ó   • g r    r*   )r   r@   s     r$   ry   ÚGuardsFn.forwardy  s   € Ør'   r*   N)r   rô   rõ   r  r  ry   r  r*   r'   r$   ru   ru   t  s   † ñõr'   ru   r    )T)Ur  rs  rF  r¢   Úcollections.abcr   Ú	itertoolsr   Útypingr   r^  rv   Útorch.utils._pytreerV  Ú_pytreer=   Útorch._export.non_strict_utilsr   r   r   Ú@torch._export.passes.add_runtime_assertions_for_constraints_passr	   Útorch._export.utilsr
   Útorch.export.unflattenr   r   Ú"torch.fx.experimental.proxy_tensorr   Útorch.fx.graphr   r   Útorch.fx.tracebackr   r   Útorch.utils._sympy.solver   Útorch.utils._sympy.value_rangesr   Ú_remove_effect_tokens_passr   Ú_tree_utilsr   Úexported_programr   r   r   r   ÚTreeSpecÚboolr:   ÚlistrE   r!   rX   rf   ÚKeyPathr…   rw   ÚdisablerŽ   r•   r   r~  rÂ   r2   ÚNoder³   rÌ   r×   rÜ   rî   r1   rð   r  r1  r:  r?  r`  rh  rx  r›  ÚnnÚModuleru   r*   r'   r$   Ú<module>r¸     sÐ  ðã Û Û Û Ý $Ý Ý ã ã ß $Ð $÷ñ õ
õ Cß :Ý Pß 6ß ;Ý .Ý 7å =Ý '÷ó ð4�&—/‘/ð 4¨&¯/©/ð 4¸dô 4ð<¨v¯©ð À4ô ð&¨d°3©iô ð. ¨d°3©iô  ðFHØ�c‘ðHà §¡Ñ/ôHðV ‡�×Ññó ðò@ð&*5Ø�‰×Ñð*5à˜C $™JÑ'ð*5ð ˆ4��U—X‘X—]‘]Ð"Ñ# T¨#¨u¯x©x¯}©}Ð*<Ñ%=Ð=Ñ>ô*5ðZ8
Ø�‰×Ñð8
à˜c D™jÑ)ð8
ð    U§X¡X§]¡]Ð 2Ñ3ð8
ð ˜S %§(¡(§-¡-Ð/Ñ0ð	8
ð
 
ô8
ð| +/ñØ�_‰_ðà�o‰o Ñ$ðð ˜C‘y 4Ñ'ðð õ	ðL +/ñ Ø�‰×Ñð à˜C $™JÑ'ð ð ˜c D™jÑ)ð ð �_‰_ð	 ð
 �o‰o Ñ$ð ð ˜C‘y 4Ñ'õ ðF&
Ø�H‰H× Ñ ð&
à)ð&
ð �S˜#�X‘ð&
ð �C˜�H‰~ð	&
ð
 
ô&
ôR
 $ô 
ô$$˜5Ÿ8™8×/Ñ/Ð;Vò $ðdØŸ™×,Ñ,ðdð 	ðdð ô	dòN@ð  ð ¸¸S¹	ô ðlØ�u—x‘x—}‘}Ñ%ðlà˜EŸL™L¨+Ð5Ñ6ðlð ! §¡Ñ0ôlò^ð* '+ñØðà
‡X�X×ÑõôDˆu�x‰x�‰õ r'   