+
    &j7                         ^ RI t ^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RIHt ^ RI	H
t
Ht ^ RIt^ RIHt ^ RIHt ^ RIHt  ! R R	] P&                  4      t ! R
 R]4      tR R ltR# )    N)CallableFunctionType)Anycast)normalize_source_lines)Tracer)Graphc                   t   a  ] tR t^t o Rt]P                  P                  V 3R lR l4       tR t	R t
RtV tR# )AST_Rewritera\  
Take a FunctionType object representing a `forward` method, then
perform an AST rewrite to swap out nodes that are not symbolically
traceable with a callsite to the FX alternative.

To support swapping out an AST node, define a new `visit` method on
that node. For more details, see:
https://docs.python.org/3/library/ast.html#ast.NodeTransformer
c                    < V ^8  d   QhRS[ /# )   fnr   )format__classdict__s   "v/Users/jameslopez/projects/CWCArchive/cwc-podcast/.venv/lib/python3.14/site-packages/torch/fx/experimental/rewriter.py__annotate__AST_Rewriter.__annotate__"   s     &H &H, &H    c                   \         P                  ! V4      w  r#\        V4      pR P                  V4      p\        P
                  ! V4      p\        P                  ! V4      p\        P                  ! V P                  V4      4      p\        VR R4      p\        P                  ! VP                  4      p	\        V	P                  4       4      p
\        W4       \!        \        V	P                  4       4      V
,
          4      p\#        V4      ^8w  d   \%        R\#        V4       24      hW^ ,          ,          pR pV! WP                  R7      # ) execzExpected 1 new key, got c                    \        V P                  VV P                  V P                  V P                  R7      p\
        P                  ! W 4      p\        P                  ! V P                  4      Vn        V# )z?Based on https://stackoverflow.com/a/13503277/2988730 (@unutbu))nameargdefsclosure)	r   __code____name____defaults____closure__	functoolsupdate_wrappercopy__kwdefaults__)fglobalsgs   && r   change_func_globals1AST_Rewriter.rewrite.<locals>.change_func_globals8   s[     

ZZA ((.A#yy)9)9:AHr   )r&   )inspectgetsourcelinesr   jointextwrapdedentastparsefix_missing_locationsvisitcompiler#   __globals__setkeysr   listlenAssertionError)selfr   sourcelines_sourcenormalized_str
source_astdest_astcodeglobals_dictkeys_beforenew_keysfn_compiledr(   s   &&            r   rewriteAST_Rewriter.rewrite!   s    !//3,[9%!0 YY~.
,,TZZ
-CD xV,yy0,++-.T L--/0;>?x=A #;CM?!KLL"A;/	  #;GGr   c                   \         P                  ! RRR7      p\        V\         P                  4      '       g   \	        R\        V4       24      hVP                  p\        V\         P                  4      '       g   \	        R\        V4       24      hVP                  '       d   VP                  M\         P                  ! RRR7      pVP                  V.Vn        \         P                  ! VR	7      p\         P                  ! WQ4      # )
zs
Swap out the Assert node (Python's `assert`) with a callsite to the
symbolically-traceable torch._assert function
ztorch._assert()eval)modezExpected ast.Expression, got zExpected ast.Call, got r   N)valuekind)rK   )r/   r0   
isinstance
Expressionr9   typebodyCallmsgConstanttestargsExprcopy_location)r:   noden	call_noderR   expr_wrappers   &&    r   visit_AssertAST_Rewriter.visit_AssertJ   s     II'f5!S^^,, #@a	!JKKFF	)SXX.. #:4	?:K!LMM(((dhh2D(I))S)	 xxi0   44r   c           
         \         P                  ! VP                  .\         P                  ! \         P                  ! R\         P
                  ! 4       R7      VP                  VP                  .. R7      R7      # )z
Swap out Python's AnnAssign with an Assign node where the annotation function is called.
Example:
     Original:
     y: Tensor_Type(1,2,3, Dyn) = f2(x)
    Output:
     y = annotate(f2(x),Tensor_Type((1,2,3,Dyn)))
annotate)idctx)funcrU   keywords)targetsrK   )r/   AssigntargetrQ   NameLoadrK   
annotation)r:   rX   s   &&r   visit_AnnAssignAST_Rewriter.visit_AnnAssign`   sQ     zz[[M((XX<jj$//2
 	
r    N)r   
__module____qualname____firstlineno____doc__torch_dynamodisablerF   r\   rj   __static_attributes____classdictcell__)r   s   @r   r   r      s:      ]]&H &HP5,
 
r   r   c                   B   a a ] tR t^st oRV3R lV 3R llltRtVtV ;t# )RewritingTracerc                   < V ^8  d   QhRS[ P                  P                  S[,          RS[S[S[3,          R,          RS[/# )r   rootconcrete_argsNreturn)rq   nnModuler   dictstrr   r
   )r   r   s   "r   r   RewritingTracer.__annotate__t   sC     < <hhoo(< CH~,< 
	<r   c                6   < \         SV `  \        V4      V4      # N)supertrace_rewrite)r:   ry   rz   	__class__s   &&&r   r   RewritingTracer.tracet   s    
 w}Xd^];;r   rl   r   )r   rm   rn   ro   r   rt   ru   __classcell__)r   r   s   @@r   rw   rw   s   s     < < <r   rw   c                    V ^8  d   QhR\         P                  P                  \        ,          R\         P                  P                  \        ,          /# )r   r   r{   )rq   r|   r}   r   )r   s   "r   r   r   |   s4     > >8+ >(0J >r   c                    a \        V \        P                  P                  4      '       d   R  V3R lloS! V 4      # \	        4       P                  \        \        V 4      4      # )c                L    V ^8  d   QhR\         P                  P                  /# )r   m)rq   r|   r}   )r   s   "r   r   _rewrite.<locals>.__annotate__   s     	& 	&ehhoo 	&r   c                    <  ! V3R  lR\         P                  P                  4      p\        4       P	                  \        \        V P                  4      4      Vn        V! V 4      # )c                   6   <a a ] tR t^t oV V3R ltRtVtV ;t# )9_rewrite.<locals>.rewrite_module.<locals>.RewrittenModulec                Z  < \         SV `  4        VP                  P                  4        F}  w  r#\	        V\
        P                  P                  4      '       d,   \        P                  ! S! V4      4      V P                  V&   KZ  \        P                  ! V4      V P                  V&   K  	  R # r   )	r   __init____dict__itemsrM   rq   r|   r}   r#   )r:   origkvr   rewrite_modules   &&  r   r   B_rewrite.<locals>.rewrite_module.<locals>.RewrittenModule.__init__   sn    G$& $ 3 3 5%a99/3yy9J/KDMM!,/3yy|DMM!,	 !6r   rl   )r   rm   rn   ro   r   rt   ru   r   )r   r   r   s   @@r   RewrittenModuler      s     < <r   r   )rq   r|   r}   r   rF   r   r   forward)r   r   r   s   & r   r    _rewrite.<locals>.rewrite_module   sI    <%((// < '3n&<&<\199-'O# #1%%r   )rM   rq   r|   r}   r   rF   r   r   )r   r   s   &@r   r   r   |   sK    "ehhoo&&	& 	& b!! ~%%d<&<==r   )r/   r#   r!   r*   r-   collections.abcr   typesr   typingr   r   rq   torch._sourcesr   torch.fx._symbolic_tracer	   torch.fx.graphr
   NodeTransformerr   rw   r   rl   r   r   <module>r      sQ         $    1 +  ^
3&& ^
B<f <>r   