
    ^j9                       d dl m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mZmZ d dlZd dlZd dlZd dlmc mZ d dlmZmZmZ d dl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% d dl&m'Z' d dl(m)Z)m*Z* d dl+m,Z,m-Z- d dl.m/Z/m0Z0m1Z1 d dl2m3Z3m4Z4 er$d dl5m6Z6m7Z7m8Z8 d dl9m:Z: d dl;m<Z< d dlm=Z= d dl>m?Z?m@Z@  ed      ZA ej                  eC      ZD	 	 	 	 	 	 	 	 	 	 d3dZEg dZFd4dZG	 d5	 	 	 	 	 d6dZHd7dZI G d dej                  j                        ZL edddg      ZM G d  d!ej                  j                        ZP	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d8d"ZQd9d#ZRd:d$ZS G d% d&      ZT G d' d(eU      ZV G d) d*      ZW G d+ d,      ZX	 	 	 	 	 	 	 	 	 	 d;d-ZY G d. d/ej                  j                  j                        Z\	 d<	 	 	 	 	 d=d0Z]ddd1	 	 	 	 	 	 	 d>d2Z^y)?    )annotationsN)
namedtuple)AnycastTYPE_CHECKINGTypeVar)CaptureOutputfullgraph_captureget_traced_fn)disable)argument_namescheck_user_input_output)UserErrorType)GetItemSource)dynamo_timedget_metrics_context)_compiling_state_context)detect_fake_modeTracingContext)_RelaxedConstraint
Constraint)ConstraintViolationError
DimDynamicStatelessSymbolicContext)_PyTreeCodeGen_PyTreeInfo)CallableIterableSequence)OutputReturnInfo)FakeTensorMode)Node)ArgumentTargetTc                    ddl m}m} t        j                  |      } |||f|      }| j
                  r || j
                  d   |      f| _        | S )z
    Because we trace a different callable, the sources are all messed up.
    Manually patch them so the error message looks correct.
    r   )_get_input_paths_replace_sources)torch.export._unliftr'   r(   inspect	signatureargs)constraint_violation_errorfuncr,   kwargsr'   r(   orig_sigflat_input_pathss           j/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_dynamo/functional_export.pypost_process_error_msgr3   -   s\     H  &H'vA!&&7<<Q?AQR+
"' &%    ))__export_root__)z_export_root. )z._export_rootr7   c                J    | }t         D ]  \  }}|j                  ||      } |S )z;Generic utility to clean export_root patterns from strings.)EXPORT_ROOT_REPLACEMENTSreplace)textresultpatternreplacements       r2   clean_export_root_stringr?   I   s0    F 8 656Mr4   c                   	 	 	 	 dfd}dd}| j                   j                  D ]  }d|j                  v r0 ||j                  d   j                               |j                  d<   |j                  j	                  dd      }|s` ||j                               |j                  d<    d| j                  v rMi }| j                  d   j                         D ]  \  }}t        |      |t        |      <    || j                  d<   | S )	a  
    Clean up nn_module_stack metadata by removing export_root references.

    Removes the _export_root module references from nn_module_stack metadata
    in graph nodes, which are artifacts from the export process. Fixes two patterns:

    1. Keys: Removes "__export_root_" and "__modules['_export_root']_" prefixes
       - Normal case: "L__self____export_root_child" -> "L__self__child"
       - inline_builtin case: Uses numeric ID strings like "140468831433840"

    2. Values: Removes "._export_root" and "._modules['_export_root']" from child names
       e.g., "L['self']._export_root.child" -> "L['self'].child"
       e.g., "L['self']._modules['_export_root'].child" -> "L['self'].child"

    Also removes the root export entry "L__self____export_root" entirely.

    Args:
        graph_module: The GraphModule to clean up
        is_inline_builtin: If True, keys are numeric ID strings and self references
                          (L['self']) are filtered out

    Returns:
        The cleaned GraphModule (modified in-place)
    nn_module_stackc                    d| v r| d= i }| j                         D ]-  \  }\  }}t        |      }t        |      }r|dk(  r'||f||<   / |S )NL__self____export_rootz	L['self'])itemsr?   )rA   cleaned_stackkey
child_namechild_class	clean_key
clean_nameis_inline_builtins          r2   _process_nn_module_stackzEclean_nn_module_stack_and_source_fn.<locals>._process_nn_module_stackm   s}     $6 89 .=.C.C.E 	A*C**k05I 2*=J !Z;%>(2K'@M)$	A r4   source_fn_stackc                   g }| D ]w  }t        |t              rTt        |      dk(  rF|\  }}t        |t              rt	        |      }|j                  ||f       U|j                  |       g|j                  |       y |S )N   )
isinstancetuplelenstrr?   append)rM   rE   itemnameclsrJ   s         r2   _process_source_fnz?clean_nn_module_stack_and_source_fn.<locals>._process_source_fn   s    # 
	+D$&3t9> 	cdC(!9$!?J!((*c):;!((. $$T*
	+ r4   N dynamo_flat_name_to_original_fqn)rA   dict[str, tuple[str, T]]returnrZ   )rM   Iterable[T]r[   r\   )graphnodesmetacopygetrD   r?   )	graph_modulerK   rL   rX   noderM   clean_name_to_original_fqn	flat_nameoriginal_fqns	    `       r2   #clean_nn_module_stack_and_source_fnrg   Q   s    81	!,  ""(( V		)+C		+,113,DII'( ))--(94@+=o>R>R>T+UDII'(V *\->->>%'"'3'8'8.(

%'	#I| )6 ''?	'JK	 ' 	<= r4   c                   i }| j                   j                  D ]  }|j                  dk(  r|j                  }t	        |      }||k7  r||_        t        | |      st        d|      t        j                  j                  j                  | |      }t        j                  j                  j                  || |       t        j                  j                  j                  | |       |j                  dk(  s|j                  }t        |t              st        dt        |             t	        |      }t        |t              st        dt        |             t	        |j                         }||k(  r`||v r||   |_        ||_        w| j#                  |      }| j%                  |       | j'                  ||       ||_        ||_        |||<    y)z3Remove export_root artifacts from FX graph in-placeget_attrzgraph_module missing attribute call_modulez#expected old_target to be str, got z#expected new_target to be str, got N)r]   r^   optargetr?   hasattrAssertionErrortorchfxrb   	_get_attr_assign_attr	_del_attrrP   rS   typerV   get_submoduledelete_submoduleadd_submodule)rb   clean_named_module_maprc   
old_target
new_targetparamnew_namerl   s           r2   clean_export_rootr}      s   
 .0 ""(( (<77j J1*=JZ'(|Z8(9*H  --77jQ%%225,
S%%//jI77m#Jj#.$9$z:J9KL  2*=Jj#.$9$z:J9KL  0		:HZ' 334Z@$	!//
;F))*5&&z6:$DK DI1;":.Q(<r4   c                  (     e Zd Zd fdZddZ xZS )ModuleToTracec                >    t         |           || _        || _        y N)super__init___export_rootin_spec)selffoor   	__class__s      r2   r   zModuleToTrace.__init__   s    r4   c                    t        j                  || j                        \  }} | j                  |i |}t        j                  |      \  }}t        ||      S r   )pytreetree_unflattenr   r   tree_flattenExportTracerOutput)r   	flat_argsr,   r/   resout_flatout_specs          r2   forwardzModuleToTrace.forward   sU    ,,YEfd00#005(!(H55r4   )r   r   r   r   r[   None)r   r   r[   r   )__name__
__module____qualname__r   r   __classcell__r   s   @r2   r   r      s    
6r4   r   r   r   r   c                       e Zd ZdZ	 	 d		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d
 fdZd fdZddZ	 	 	 	 	 	 	 	 d fdZ	 	 	 	 	 	 	 	 d fdZd fdZ	d fdZ
 xZS )DynamoGraphTransformerzZGraph transformer for dynamo export that flattens inputs/outputs without complex matching.c                $   t         	|   |       t        |      t        |      k7  r$t        dt        |       dt        |       d      || _        || _        || _        || _        || _        |xs i | _	        |j                  j                  D cg c]  }|j                  dk(  s| c}| _        t        d |j                  j                  D              | _        i | _        | j#                          i | _        | j'                          y c c}w )Nzflat_args_dynamic_dims length (z!) must match flat_inputs length ()placeholderc              3  @   K   | ]  }|j                   d k(  s|  yw)outputN)rk   ).0ns     r2   	<genexpr>z2DynamoGraphTransformer.__init__.<locals>.<genexpr>  s     RaAQRs   )r   r   rR   rn   flat_inputsflat_args_dynamic_dimsgraph_input_ordergraph_output_map	fake_modegraph_inputsr]   r^   rk   placeholdersnextoutput_nodenew_input_nodes_create_flattened_inputsold_to_new_mapping_create_placeholder_mapping)
r   moduler   r   r   r   r   r   r   r   s
            r2   r   zDynamoGraphTransformer.__init__   s    	 %&#k*:: 1#6L2M1N O225k2B1C1F 
 '&<#!2 0"(.B )/(:(:T1addm>SQTR6<<+=+=RR :<%%' #%((* Us   D'Dc                   t        t        | j                              D ]%  }t        |   d| di       }|| j
                  v rw| j
                  |   }|t        | j                        k  rP| j                  |   }|j                  j                         D ]$  \  }}|dk7  s||j                  j                  |<   & | j                  t        | j                  |   t        j                        r| j                  j                  | j                  |   t        t        t        | j                  |   j                               D cg c]3  }|| j"                  |   v rt$        j&                  nt$        j(                  5 c}dgt        | j                  |   j                         z              |j                  j                  d<   npt+        | j                  |   d      r1| j                  |   j,                  |j                  j                  d<   n&| j                  |   |j                  j                  d<   || j.                  |<   ( yc c}w )zKCreate new placeholder nodes for flattened inputs with proper fake tensors.arg_ valN)dynamic_sizesconstraint_sizes)symbolic_context)rangerR   r   r   r   r   r   r_   rD   rc   r   rP   ro   Tensorfrom_tensorr   shaper   r   DYNAMICSTATICrm   r   r   )	r   ir   graph_placeholder_idxorig_placeholderrF   valuedr   s	           r2   r   z/DynamoGraphTransformer._create_flattened_inputs  s!   s4++,- &	2A'-QCj"bAK D***(,(>(>q(A%(3t/@/@+AA'+'8'89N'O$&6&;&;&A&A&C ?
U%<9>K,,11#6?
 ~~)j  #U\\/ 04~~/I/I$$Q'%= &+3t/?/?/B/H/H+I%J' !" $%(C(CA(F#F !+ 2 2%/%6%6!7' +/#d6F6Fq6I6O6O2P)P
& 0J 0  %%e, ))!,e4/3/?/?/B/F/F  %%e,/3/?/?/B  %%e, '2D  #M&	2*'s   8I
c                    | j                   j                         D ]K  \  }}|t        | j                        k  s| j                  |   }| j                  |   }|| j
                  |<   M y)z1Create mapping from old placeholders to new ones.N)r   rD   rR   r   r   r   )r   user_input_idxr   old_placeholdernew_placeholders        r2   r   z2DynamoGraphTransformer._create_placeholder_mappingC  sn     6:5K5K5Q5Q5S 	K1N1$s4+<+<'=="&"3"34I"J"&"6"6~"F;J''8		Kr4   c                   | j                   | j                  v r| j                  | j                      }dD ]K  }|| j                   j                  v s| j                   j                  |   |j                  j                  |<   M d| j                   j                  v rHd|j                  j                  vr0| j                   j                  d   |j                  j                  d<   |S | j                  j                  | j                         }|| j                  v r-| j                  |   }t        |t              s| j                   j                  j                  d      xs% | j                   j                  j                  d      }|d| }	t        |t        j                        r| j                  j                  |	|       nt        | j                  |	|       | j                  j!                  d|	di       }
| j                   j                  j#                         |
j                  _        ||
j                  j                  d<   |
S t$        | M  |||      S )z1Replace old placeholders with new flattened ones.)tensor_dictexample_valueunbacked_bindingsr   r   
_captured_ri   r   )current_noder   r_   rc   r   indexr   rP   r   ra   ro   r   r   register_buffersetattrtracercreate_proxyr`   r   r   )r   rl   r,   r/   new_argrF   placeholder_idxsourceexample_val	attr_namer<   r   s              r2   r   z"DynamoGraphTransformer.placeholderM  s   
  7 77--d.?.?@G M I$++000-1->->-C-CC-HGLL%%c*I
 ))...5@Q@Q3Q+/+<+<+A+A%+H!!%(N #//55d6G6GHO$"3"33**?;!&-8"&"3"3"8"8"<"<# #E**//33OD   #.&00A$B	%k5<<@ KK77	;O#DKKKH!%!9!9*iQSUW!X+/+<+<+A+A+F+F+H(2=((/%7&vtV<<r4   c                   |d   }g }t        | j                  j                               D ]  }| j                  |   \  }}|dk(  r#|j                  |t	        t
        |                =|dk(  r9t	        t        |      j                  }	|j                  | j                  |	          {|dk(  s|j                  |        t        
| )  |t        |      fi       S )z/Transform output according to graph_output_map.r   	graph_outinputconstant)sortedr   keysrT   r   intr   r   r   r   r   rQ   )r   rl   r,   r/   original_outputsnew_outputsr   output_typer   	input_idxr   s             r2   r   zDynamoGraphTransformer.outputu  s      7 --2245 		(A#44Q7Kk)""#3DcN#CD' 4::	""4#7#7	#BC
*""3'		( w~fu['9&;R@@r4   c                `   || _         t        | 	  |      }t        |d      r|j                  |ur|dD ]7  }||j
                  v s|j
                  |   |j                  j
                  |<   9 |j                  dk7  r1t        |d      r%|j                  j                  |j                         |S )z.Run node transformation and preserve metadata.rc   )r   r   r   r   rV   )	r   r   run_noderm   rc   r_   rk   _renamerV   )r   r   r<   rF   r   s       r2   r   zDynamoGraphTransformer.run_node  s    !!$ 66"v{{!';D 8!&&=,-FF3KFKK$$S)8
 ttxGAv$6##AFF+r4   c                H   t         |          }t        | j                  d      r|d| j                  j                  v r&| j                  j                  d   |j                  d<   d| j                  j                  v r&| j                  j                  d   |j                  d<   |S )z:Perform the graph transformation and copy module metadata.r_   rY   dynamo_compile_id)r   	transformrm   r   r_   )r   	result_gmr   s     r2   r   z DynamoGraphTransformer.transform  s    G%'	 4;;'1T[[5E5EEEI[[EUEU6F	AB
 #dkk&6&666:kk6F6F'7	23
 r4   )NN)r   torch.fx.GraphModuler   z	list[Any]r   zlist[set[int]]r   zdict[int, int]r   zdict[int, OutputReturnInfo]r   
Any | Noner   zdict[int, Any] | Noner[   r   )r[   r   )rl   r$   r,   ztuple[Argument, ...]r/   dict[str, Any]r[   r   )rl   r$   r,   zSequence[Any]r/   r   r[   r   )r   r"   r[   r   )r[   r   )r   r   r   __doc__r   r   r   r   r   r   r   r   r   s   @r2   r   r      s    d !%.2#+$#+ #+ !/	#+
 *#+ 6#+ #+ ,#+ 
#+J(2TK&=&=$8&=BP&=	&=PAA$1A;IA	A*" r4   r   c           	        d }	 t        |       \  }}	|j                  j                  |j                         t        |dd       x}`|j                  x}Qt        | j                  t        j                  j                  t        j                  j                  z        s|j                          |j                         }|j                  t!        j"                  |      |||      }|r.|j$                  r|j$                  d   |z   f|_        n-|f|_        n$|rt	        |      }nt&        j)                  d|       |j*                  D ]]  }t        |t,        j.                        st	        dj1                  t3        j4                  |j6                  |                d| d      }_ |rt9        ||||      }|y # t        $ r}
|
}Y d }
~
d }
~
ww xY w)N	shape_envr   z#Summary of dimension constraints:%sr7   zk
It appears that you're trying to set a constraint on a value which we evaluated to have a static value of z0. Set TORCH_LOGS="+export" for more information.)r   graph_capture_outputbuild_guards__code__r   getattrdim_constraintsrP   r   ro   _opsOpOverloadPacket
OpOverloadsolveforced_specializationsprettify_resultsr*   r+   r,   loginfovar_to_rangesympyIntegerjoin	tracebackformat_listvar_to_stackr3   )module_to_traceorig_callabler   r   r,   r/   dynamic_shapesr-   fnr6   er   r   r   msgks                   r2   &_suggest_or_raise_constraint_violationr
    s    "&'o.A11>>r{{K
 id;	;H ) 9 99_F##JJ''%***?*??

 	!0!G!G!I..m,&"	
 &)...33A6<3*/ 47&*/%-Ec-J*9 '' 	A!U]]+-Ewwy44Y5K5KA5NOPQ RJJK MEE.*	 "%;&tV&
" )(	 "_ $ '%&"'s   3F: :	GGGc                    | j                   j                          | j                          t        | j	                               D ]  \  }}t        | |       t        | ||         y r   )r]   eliminate_dead_code	recompilelistnamed_buffersdelattrr   )
shuffle_gmrV   buffers      r2   _normalize_shuffle_graphr    sW    ((*Z5578 *f
D!
D&)*r4   c                    | j                   j                  D ].  }|j                  dk(  s|j                  d   |j                  d<   0 y )Nr   r   r   )r]   r^   rk   r_   )gmrc   s     r2   normalize_graph_moduler    s<     :77m##yy9DIIe:r4   c                  4    e Zd Z	 	 	 	 	 	 	 	 ddZ	 	 	 	 ddZy)InputProcessorc                .    || _         || _        || _        y r   )rootnum_argskwarg_names)r   r  r  r  s       r2   r   zInputProcessor.__init__  s     	 &r4   c                   |}i }t        |      | j                  kD  r;t        t        | j                  || j                  d              }|d | j                   }| j
                  !t        | j
                  t        j                  j                        rt        | j
                  j                  j                  t              s5t        dt        | j
                  j                  j                               t        | j
                  j                  j                  j                   d      st        d      | j
                  j                  j                  j                   j"                  | urt        d      | j
                  g|}||fS )Nz%expected _DynamoBytecodeCodeGen, got input_processorz9dynamo_bytecode_flatten missing input_processor attributez3dynamo_bytecode_flatten.input_processor is not self)rR   r  dictzipr  r  rP   ro   rp   GraphModuler]   _codegen_DynamoBytecodeCodeGenrn   rt   rm   dynamo_bytecode_flattenr  )r   inputsr,   r/   s       r2   __call__zInputProcessor.__call__  sB    t9t}}$#d..T]]_0EFGF$--(D99 $))UXX%9%9:!$))//":":<RS(?TYY__E]E]@^?_`  IIOO,,DDFW )S  IIOO,,DDTT  )M  II%%DV|r4   N)r  objectr  r   r  	list[str]r[   r   )r%  tuple[object, ...]r[   z,tuple[tuple[object, ...], dict[str, object]])r   r   r   r   r&  r   r4   r2   r  r    sB    '' ' 	'
 
'(	5r4   r  c                      e Zd Zy)YieldN)r   r   r   r   r4   r2   r+  r+  0  s    r4   r+  c                  D    e Zd Z	 	 	 	 	 	 	 	 ddZ ed      dd       Zy)DynamoBytecodeFlattenc                <    || _         || _        || _        d | _        y r   )r  out	f_globals	gm_inputsr   r  r/  r0  s       r2   r   zDynamoBytecodeFlatten.__init__5  s!      /"15r4   *do not trace internal dynamo graph capturereasonc                    d fd} j                  |      \  }}	   j                  j                  | j                        |i | t        # t        $ r'  j
                  t        d      d  j
                  cY S w xY w)Nc                     | _         t        r   )r1  r+  )example_inputsr   s    r2   backend_dummyz5DynamoBytecodeFlatten.__call__.<locals>.backend_dummyB  s    +DNKr4   compiled_fnextra_globalsz&gm_inputs must not be None after Yield)r8  r'  r[   r   )r  r/  forward_callabler0  r+  r1  rn   RuntimeError)r   r%  r9  r,   r/   s   `    r2   r&  zDynamoBytecodeFlatten.__call__@  s    	 ++F3f	"DHH%%) &  	  	"~~%$%MNTXX>>!	"s   -A -B ?B Nr  r  r/  r	   r0  zdict[str, object]r[   r   )r%  r'  r[   r'  r   r   r   r   dynamo_disabler&  r   r4   r2   r-  r-  4  sH    	6'	6 	6 %		6
 
	6 GH Ir4   r-  c                  P    e Zd Z	 	 	 	 	 	 	 	 ddZ ed      	 	 	 	 	 	 dd       Zy)DynamoBytecodeUnflattenc                .    || _         || _        || _        y r   )r  r/  r0  r2  s       r2   r   z DynamoBytecodeUnflatten.__init__S  s      /"r4   r3  r4  c                    dfd}| j                  |      \  }}t        j                  j                         5   | j                  j                  || j                        |i |}d d d        |S # 1 sw Y   S xY w)Nc                     S r   r   )r8  	flat_outss    r2   r9  z7DynamoBytecodeUnflatten.__call__.<locals>.backend_dummya  s	    r4   r:  )r8  r'  r[   Sequence[object])r  ro   _C_DisableTorchDispatchr/  r=  r0  )r   rG  r%  r9  r,   r/   resultss    `     r2   r&  z DynamoBytecodeUnflatten.__call__]  s    	 ++F3fXX++- 	dhh//) 0 G	 		 s   .A22A<Nr?  )rG  rH  r%  r)  r[   r'  r@  r   r4   r2   rC  rC  R  s`    #'# # %	#
 
# GH)3E	 Ir4   rC  c                .
   | j                   t        d      | j                   }t        j                  j                  j                  |      \  }}t        j                  ||f      }t        j                  j                  j                  |t        j                         | j                  j                  }|j                  t        |t        j                   j"                        r[|j$                  _        |j&                  _        t)        fd|j*                  D              st        d      j*                  j-                  |j*                         |j.                  _        t0        j2                  dk\  r.dd l}	|	j7                  t        j                   j"                        }
n%t9        t        j                   j"                  dd       }
|j:                  j=                         D ]  \  }}|
s	||
vs|j:                  |<    |j>                  jA                         _        |jB                  jA                         _!        |jD                  jA                         _"        |jF                  jA                         _#        j>                  sjB                  rt9        dd       t        d      t        jH                  t        jJ                  j                  jL                        s!t        d	tO        jH                               jH                  jP                  t        d
      dd}|jH                  _(        t        |t        j                   j"                        rn|}tS        |tU        |      tW        |jY                                     }t[        || |      }t]        || |      }t_        ta        tc        jd                  |      ||      ||      jf                  _4        tk               tm        d      rt        d      tm        d      rt        d      |_7        |_8        ts        d       ju                          | j                  jv                  jx                  jz                  j|                  d<   | j                   t        d      | j                   j~                  j|                  d<   dj|                  d   _@        t        j|                  d         }| j                   j                  |_B        |j|                  d<   S )Nz"out.backend_input must not be Nonec              3  8   K   | ]  }t        |         y wr   )rm   )r   mrb   s     r2   r   z7create_fx_graph_from_captured_output.<locals>.<genexpr>  s     GAw|Q//Gs   zBgraph_module already has attributes conflicting with root._modules)      r   __annotations___wrapped_callz+graph_module._wrapped_call must not be Nonezexpected _WrappedCall, got z0graph_module._wrapped_call.cls_call must be Nonec                   d| j                   v rt        d      | j                  | j                  t	        |       j
                  dfd}	 t        j                  ||       | _        i | _        i | _        t        t	        |       | &  |i || j                   j                  d       | _        | _        S # | j                   j                  d       | _        | _        w xY w)Nr   z('forward' should not be in self.__dict__c                8    | _         | _         | g|i |S r   )_forward_hooks_forward_pre_hooks)r   r,   r/   	fwd_hooksfwd_pre_hooksoriginal_forwards      r2   patched_forwardzZcreate_fx_graph_from_captured_output.<locals>.dynamo_wrapped_call.<locals>.patched_forward  s(    *3D'.;D++DB4B6BBr4   r,   r'  r/   r'  r[   r'  )__dict__rn   rU  rV  rt   r   types
MethodTyper   r&  pop)r   r,   r/   rZ  rW  rX  rY  s       @@@r2   dynamo_wrapped_callzAcreate_fx_graph_from_captured_output.<locals>.dynamo_wrapped_call  s    -()STT //	 $ 7 7#':#5#5 C
<#(#3#3OT#JDL*,D'.0D+ dT;TLVLMM%%i0*3D'.;D+ MM%%i0*3D'.;D+s   A B= =+C(_dynamo_bytecode_flattenz;graph_module already has _dynamo_bytecode_flatten attribute_dynamo_bytecode_unflattenz=graph_module already has _dynamo_bytecode_unflatten attribute_param_name_to_sourcemodule_call_specsz0out.backend_input must not be None for fake_moder   Ttracing_contextr[  )Cbackend_inputrn   ro   _dynamoconvert_framer   r   tree_leaves
eval_framer   r   INVALID_INPUTr   r0  rb   rP   nnModule_parameters_buffersall_modulesupdate_non_persistent_buffers_setsysversion_infoannotationlibget_annotationsr   r\  rD   rU  r`   rV  _backward_hooks_backward_pre_hooksrR  rp   _WrappedCallrt   cls_callr  rR   r  r   r-  rC  r#  r   r*   r+   r]   r"  r  rm   ra  rb  r  r  output_graphexport_metadatamodule_call_specr_   r   allow_non_fake_inputsr   tensor_to_context)r/  modr,   r/   rf  r6   r  flat_real_argsr0  rv  r   rV   r   r`  r  r$  dynamo_bytecode_unflattenre  rb   s                     @r2   $create_fx_graph_from_captured_outputr  l  s9     ABB%%Mmm))77<GAt''v7N	MM4433 ((22I --L$(#'#3#3  $GGG T  	$$T]]3373S3S0w& '77HK!%((//3DdKK==..0 	4KD%t;6.3%%d+	4 '+&9&9&>&>&@#*.*A*A*F*F*H''+';';'@'@'B$+/+C+C+H+H+J(&&,*I*I
 |_d;C$%RSS**EHH,A,A,N,N %1$|7Q7Q2R1ST  ))22>$%WXX<6 3FL&&/%dEHHOO<<$D$T3t9d6;;=6IJO3OS)T 7i X"8w((-tV<!#L
 <(|78I
 	
 |9:K
 	
 -DL).GL+L12  --==NN )*  OPP%(%6%6%@%@Lk";?Lk"8$\%6%6{%CDO(+(9(9(K(KO%+:L'(r4   c                       e Zd Z	 	 	 	 	 	 	 	 d fdZddZddZdd	 	 	 	 	 	 	 d fdZ	 	 	 	 	 	 	 	 ddZddd		 	 	 	 	 	 	 dd
Z xZ	S )r#  c                h    t         |           || _        || _        || _        d| _        d | _        y )NF)r   r   orig_arg_namesr$  r  
wrap_tuple_inputs)r   r  r$  r  r   s       r2   r   z_DynamoBytecodeCodeGen.__init__  s6     	,'>$)B&/3r4   c                    || _          | j                  | }t                t        j                  fd|      rd_        |S )Nc                `    t        | t        j                        xr j                  |        S r   )rP   ro   r   is_our_fake)xr   s    r2   <lambda>z7_DynamoBytecodeCodeGen.process_inputs.<locals>.<lambda>  s&    jELL1R):O:OPQ:R6R r4   T)r  r$  r   r   tree_anyr  )r   r%  rK  r   s      @r2   process_inputsz%_DynamoBytecodeCodeGen.process_inputs  sJ    .$..7$&	 V__R&
 /3I+r4   c                j    | j                  || j                        }| j                  r|f}d | _        |S r   )r  r  r  )r   outputsrK  s      r2   process_outputsz&_DynamoBytecodeCodeGen.process_outputs  s2    00$,,G??jGr4   Fexpanded_defc               |   | j                   }t        |      dkD  r|d   dk(  nd}|r|j                  dd       t        |      }|rdnd}t	        |t        |            D ]   }||   dk(  sd}||v r
|dz  }||v r
|||<   " t
        
|   |d d  ||      }	t        |      dkD  r|	| j                  |||      z  }	|	S )Nr   r   F   self_r6   r  )r  rR   insertr  r   r   
gen_fn_defgen_var_bindings)r   	free_varsmaybe_return_annotationr  fn_argshas_orig_self	first_posr   r|   fn_definitionr   s             r2   r  z!_DynamoBytecodeCodeGen.gen_fn_def  s     %%25g,2Bv-Q' w-&AA	y#g,/ 	&AqzV#"')OH ')%
	& *AJ/l + 
 y>AT227I|TTMr4   c                   |D cg c](  }|j                  d      d   j                  d      d   * }}t        |      dk(  rd}n)t        |      dk(  r	|d    d}ndj                  |       }d| ddj                  |       d	S c c}w )
N:r   #r7   r  , z
    _fn_args = (z)
    z,, = self._dynamo_bytecode_flatten(*_fn_args))splitrR   r   )r   r  r  r  r  without_annotationfn_signatures          r2   r  z'_DynamoBytecodeCodeGen.gen_var_bindings1  s     FOOaggcl1o33C8;OOw<1L\Q%aj\,L"ii01L 	YY!"##OS 	S Ps   -BN)descsrepr_fnc          	         |t         }ddj                  |D cg c]
  } ||       c}       d}| j                  rd| d}d| S c c}w )Nz!self._dynamo_bytecode_unflatten((r  z,), _fn_args)(z,)zreturn )reprr   r  )r   output_argsr  r  areturneds         r2   generate_outputz&_DynamoBytecodeCodeGen.generate_output?  sa     ?G6tyyVaAbQR'!*Ab7c6ddqr??8*B'H
## Bcs   A	
)r  r(  r$  r   r  r   r[   r   )r%  r   r[   r   )r  r   r[   r   )r  r(  r  rS   r  boolr[   rS   )r  r(  r  r(  r  r  r[   rS   )r  ztorch.fx.node.Argumentr  zobject | Noner  r   r[   rS   )
r   r   r   r   r  r  r  r  r  r   r   s   @r2   r#  r#    s    4!4 "*	4 $,4 
4" #"" "%"
 " 
"HS S-6SFJS	S$  $"$+$ 	$
 $ 
$r4   r#  c                *    t         t        j                  j                        rdd}d}g } j                  j
                  D ]G  }|j                  r|sd}|j                  d       |j                  |j                   ||      z          I ddj                  |       ddj                   j                  j
                  D cg c]  }|j                   d|j                    c}       d	}i }t        |d
 i|       |d    d fd}|S c c}w )Nc                B    | j                         rd| j                  S y)N=r7   )has_default_valuedefault_value)args    r2   default_annotationz;dynamo_graph_capture_for_export.<locals>.default_annotationU  s%    $$&3,,/00r4   FT*z
def op_overload_wrapper(r  z):
    return op(r  z)
rk   op_overload_wrapperc            
        t         j                  j                  j                  rt	        d      t               5  t         j                  j                  j                  dd      5  t               5  t        d      5  t        | |      }d d d        d d d        d d d        d d d        t        | |      }|S # 1 sw Y   1xY w# 1 sw Y   5xY w# 1 sw Y   9xY w# 1 sw Y   =xY w)Nz"install_free_tensors must be FalseFwarn)replay_side_effectsside_effect_replay_policyr
   )constraints)ro   rg  configinstall_free_tensorsrn   r   patchr   r   r
   r  )r,   r/   r/  rb   r  r  s       r2   innerz.dynamo_graph_capture_for_export.<locals>.inneri  s    ==44 !EFF$&	MM  &&$)V ' 	
  !	 ,-	 $'	C	 	 	 	 <CT6R	 	 	 	 	 	 	 	sS   ,C"'C2C
>B>	C
CC">CC

CCC	C""C+)r  ztorch.Argumentr[   rS   )r,   r   r/   r   r[   r   )rP   ro   r   r   _schema	arguments
kwarg_onlyrT   rV   r   exec)	r  r  r  has_kwarg_onlyarg_listr  func_strr/  r  s	   ``       r2   dynamo_graph_capture_for_exportr  O  s    "ejj++,	
 ::'' 	@C~~n!%$OOCHH'9#'>>?		@
8,- .yybjj>R>RSsSXXJaz2STU V Xbz3'&'( L5 Ts   "D)r  r  c               $     ||d fd}|S )a  
    Improved dynamo graph capture using transformer approach with proper fake tensor handling.

    This function creates a capture instance that handles:
    1. PyTree flattening/unflattening with proper input ordering
    2. Dynamo graph capture with export-specific context
    3. FX graph transformation for export compatibility
    4. Proper fake tensor metadata preservation
    5. Dynamic dimension constraint handling

    Notable improvements over manual approach:
    - Uses FX Transformer for cleaner graph manipulation
    - Properly handles fake tensor metadata and dynamic dimensions
    - Preserves all necessary metadata for export
    - More robust error handling and edge case management

    TODO:
    1. Are we actually gonna run the bytecode?
    2. Need to attach guards
    c                 	   t               5  t        j                  | |f      \  }}t        |t        j
                         t        |      }t        t        j                  j                        rj                  n}}}ddlm}  |        t        j                  j                  j!                  ddddddddt        j                  j                  j"                  	      }	t%               5  t'        d      5  |	5  t)        |t+        |      d      }
|
j,                  j.                  t1        d      g }|
j2                  C|
j2                  j4                  }|
j2                  j6                  }|
j2                  j8                  }nt        j:                  j=                  t        j                  j                         t        j:                  j?                               }|j@                  jC                  d        |jE                          d }tG        ||||
| ||       |
j,                  j.                  jH                  }|jJ                  }|jL                  }|jN                  }|jP                  }d d d        d d d        d d d        |D cg c]  }|xs d	D ch c]w  }|jR                  tU        |      k(  r]t        |tV              sM|jX                  jZ                  j\                  |jX                  jZ                  j^                  k7  r|j`                  y c} }}}i }D ]N  }||   }t        |t        j                  jb                  jd                        s7tg        |      ||jh                  <   P |jk                         D ]  \  }}|   ||<    tm        ||||      jo                         }tq        ts        tu        tw        jx                  |      | |      |            |j@                  _=        |jE                          t}        |d       t        |       |j                  d
<   ||j                  d<   |cd d d        S # 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 # 1 sw Y   y xY w)Nr  )resetTF)	specialize_intspecialize_floatassume_static_by_defaultautomatic_dynamic_shapes capture_dynamic_output_shape_opscapture_scalar_outputs'constant_fold_autograd_profiler_enabledlog_graph_in_out_metadatar  r
   )r   _is_export_deprecated_do_not_usez5output_graph must not be None after fullgraph_capturer   rd  r   )Ar   r   r   r   r   rk  r   rP   ro   rl  rm  r   r7   r  rg  r  r  install_free_tensors_for_exportr   r   r
   rQ   r   r|  rn   rf  rb   r   r8  rp   r!  Graphr]   r   r  r
  r}  graph_input_idx_to_local_sourceoutput_return_typer   r~  t_ididr   constraint_rangevrlowerupperdimr   r   rR   r   rD   r   r   r   r   r   r*   r+   r"  rg   r}   r_   )r,   r/   r   r   r  r  r  r  r  dynamo_config_ctxr/  r8  r]   r   r}  r   r   r   r~  r  cr   r   inpr   real_idx	graph_idxtransformed_graph_constraints_dynamic_shapesr  s                               r2   r  z/_dynamo_graph_capture_for_export.<locals>.inner  s<   %' C	%#)#6#6f~#F K#K1L1LM+C9O+5c588??+KCKKQTM3?K   G % 4 4 : :#!%)-).15'+8<*. &+]]%9%9%Y%Y !; !$ $%+D01+D "+D
 (#+& ,59	 ++88@(O  -/$$0--::E # 1 1 ; ;I%(%6%6%E%EN!HH001BEHHNNDTUEKK&&t,OO% $I6#!" #&":":"G"G"W"W.NN#2#E#E *33#2#C#C W+D +D +Dp %&  */R"Q% *1.@ A..11771;M;M;P;P;V;VV EE&" &" 13# M%c*femm&:&:&H&HI69:K6L%fll3M
 (9'>'>'@ B#)(6y(AH%B !7&! ! ik  0>"7#4#4]#CT6R0##, '')/0A4H/0:J""#672;"";/$GC	% C	%@+D +D +D +D +D +D^&]C	% C	%s   C"Q5.Q:Q=E1Q	.Q6Q>Q5Q/A<Q*Q/=Q5C"Q5QQQQQ'	"Q5*Q//Q55Q>)r,   r   r/   r   r[   r   r   )r  r  r  r  r  r  s   `   @@r2    _dynamo_graph_capture_for_exportr    s    6 %OLE%N Lr4   )
r-   r   r.   Callable[..., Any]r,   r   r/   r   r[   r   )r;   rS   r[   rS   )F)rb   r   rK   r  r[   r   )rb   r   r[   r   )r  ztorch.nn.Moduler  r  r   zFakeTensorMode | Noner   r	   r,   r   r/   r   r  .dict[str, Any] | tuple[Any] | list[Any] | Noner[   r   )r  r   r[   r   )r  r   r[   r   )
r/  r	   r  r   r,   ztuple[Any, ...]r/   r   r[   r   r   )r  r  r  list[Constraint] | Noner[   r  )r  r  r  r  r  r  r[   z#Callable[..., torch.fx.GraphModule])_
__future__r   r*   loggingrt  r   r]  collectionsr   typingr   r   r   r   r   ro   torch.fxtorch.utils._pytreeutils_pytreer   torch._dynamo.convert_framer	   r
   r   torch._dynamo.decoratorsr   rA  torch._dynamo.eval_framer   r   torch._dynamo.excr   torch._dynamo.sourcer   torch._dynamo.utilsr   r   torch._export.utilsr   torch._guardsr   r   torch.export.dynamic_shapesr   r   %torch.fx.experimental.symbolic_shapesr   r   r   torch.fx.graphr   r   collections.abcr   r   r   torch._dynamo.output_graphr    torch._subclasses.fake_tensorr!   r"   torch.fx.noder#   r$   r%   	getLoggerr   r   r3   r9   r?   rg   r}   rl  rm  r   r   rp   Transformerr   r
  r  r  r  	Exceptionr+  r-  rC  r  r]   CodeGenr#  r  r  r   r4   r2   <module>r     s   "   
   " 4 4    $ $ W W > L + . A 8 : F 
 7 <<;<.CLg!& 8&
& & 	&
 &*  CHY&Y;?YYx0<f
6EHHOO 
6   4{J6OP UXX11 DA)$A)%A) %A) (	A)
 A) A) CA) 
A)H*:( (V	I 	 < 4v	v v(7vAOvvrg$UXX^^33 g$X ,0..(. .h ,0EI	e	e )e C	e
 )er4   