
    sh                        S r SSKrSSKrSSKrSSKrSSKJrJr  SSKJ	r	  SSK
rSSKJrJrJr  SSKJrJrJrJrJr  SSKJrJr  SS	KJr  SS
KJrJrJrJrJ r J!r!J"r"J#r#  SSK$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/J0r0J1r1  SSK2J3r3J4r4J5r5  SSK6J7r7  SSK8J9r9  SSK:J;r;  SSK<J=r=  \	(       a  SSK>J?r?  S S jr@\S\AS\R                  R                  4S j5       rDS rE " S S\55      rF " S S\=5      rG " S S\G5      rH " S S\G5      rIg)!aQ  
This module implements variable tracking for PyTorch nn.Module instances during Dynamo tracing.

It provides specialized handling for different types of nn.Module instances through several key classes:

- NNModuleVariable: Handles instance-specific module tracing, specializing on module id() and placing
  parameters directly on the torch.fx.GraphModule. This creates one graph per module instance.

- UnspecializedNNModuleVariable: Provides class-level module tracing, treating nn.Modules like other
  user-defined objects and passing parameters as inputs to the FX graph. This creates one graph per
  module class.

- UnspecializedBuiltinNNModuleVariable: Specifically handles built-in PyTorch modules (e.g. nn.Linear)
  with appropriate optimizations.

- FSDPManagedNNModuleVariable: Special handling for FSDP-wrapped modules with modified guarding behavior
  and parameter handling.

The module integrates with Dynamo's broader tracing functionality to handle module method calls,
parameter access, hooks, and other nn.Module behaviors while maintaining proper scoping and guarding
of module state.
    N)contextmanagernullcontext)TYPE_CHECKING   )graph_break_hintstrace_rules	variables)raise_observed_exceptionunimplementedunimplemented_v2UnspecializeRestartAnalysisUnsupported)GuardBuilderinstall_guard)GenerationTracker)
AttrSourceConstDictKeySourceDictGetItemSourceFSDPNNModuleSourceGetItemSourceNNModuleSource"UnspecializedBuiltinNNModuleSourceUnspecializedNNModuleSource)get_custom_getattrget_fake_valueis_lazy_moduleis_namedtupleis_safe_constantistensoristypennmodule_has_hooksobject_has_getattributeproxy_args_kwargsset_example_valueunpatched_nn_module_callunpatched_nn_module_call_impl   )typestrValueMutationNewVariableTracker)invoke_and_store_as_constant)LazyVariableTracker)SliceVariable)UserDefinedObjectVariable)InstructionTranslatorc                   ^ ^ [        US5      (       aj  UU 4S jm[        X#5      u  pEU Vs/ s H  nT" U5      PM     nnUR                  5        VV	s0 s H  u  pUT" U	5      _M     n
nn	UR                  XU
5        ggs  snf s  sn	nf )aj  
Fairly coupled helper used by NNModuleVariable and UnspecializedNNModuleVariable.

Used to cause lazy module to be initialized (and delete its init hook) before tracing. Especially
useful now that 'allowed' modules graph-break on hooks, calling this first ensures there is no hook
by the time we trace __call__ and thus no graph-break for lazy allowed modules.
_initialize_hookc                   > [        U 5      (       a  [        U 5      " U4S jU  5       6 $ [        U [        5      (       a,  U R	                  5        VVs0 s H  u  pUT" U5      _M     snn$ [        U [
        [        [        45      (       a  [        U 5      " U4S jU  5       5      $ [        U [        R                  R                  5      (       a  [        U R                  T5      $ U $ s  snnf )Nc              3   4   >#    U  H  nT" U5      v   M     g 7fN .0elemconvert_to_fakes     u/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/_dynamo/variables/nn_module.py	<genexpr>Binitialize_lazy_module.<locals>.convert_to_fake.<locals>.<genexpr>]   s      E14!6!61   c              3   4   >#    U  H  nT" U5      v   M     g 7fr4   r5   r6   s     r:   r;   r<   a   s     Ct44r=   )r   type
isinstancedictitemslisttuplesettorchfxProxyr   node)xkvr9   txs      r:   r9   /initialize_lazy_module.<locals>.convert_to_fake[   s    QAw E1 EFFAt$$:;'')D)$!?1--)DDAeS122AwCCCCAuxx~~..%affb11 Es   C'N)hasattrr#   rB   _infer_parameters)rM   modargskwargs
proxy_argsproxy_kwargsarg	fake_argsrK   rL   fake_kwargsr9   s   `          @r:   initialize_lazy_modulerY   Q   s     s&''
	 $5T#B 
5?@Zc_S)Z	@9E9K9K9MN9Mq/!,,9MNck:# ( ANs   A?B
module_keyrQ   c              #   "  #    UR                  5       nUR                  R                  US5      nUS:  a  U  SU 3OU n  XCR                  4UR                  U '   US-   UR                  U'   S v   UR                  U 	 g ! UR                  U 	 f = f7f)Nr   @r'   )name	num_callsget	__class__nn_module_stack)rZ   sourcerM   rQ   fully_qualified_namer^   s         r:   record_nn_module_stackrd   m   s     !;;=  !5q9I09AJ<q,:J+*>)N:&-6])*z*Bz*s   <B0A= /B=BBc           	         U (       a  SUR                   ;   a  [        UR                   S   5      (       al  UR                   S   n[        U S5      n[        U5      [        R
                  L a  [        US5      n[        UR                  [        R                  5      5        g [        U R                  [        R                  " [        R                  SS95      5        g g )Nforward__func__)attr)__dict__callabler   r?   types
MethodTyper   
make_guardr   CLOSURE_MATCH	functoolspartialNOT_PRESENT_IN_GENERIC_DICT)rb   rQ   fwdforward_sources       r:   &guard_to_detect_forward_monkeypatchingrt   z   s     $#,,y2I)J)J,,y)C'	:NCyE,,,!+NJ!G.33L4N4NOP !!%%$@@y     c                      ^  \ rS rSrSSSS1\R
                  krS\S\S\R                  R                  SS4U 4S jjrS	 rS
 rS r  SS jrS rSSS\SS4S jrS rS rSS jrS rSS jr      SS jr S     SU 4S jjjrSrU =r$ )NNModuleVariable   module_typerZ   valuenn_module_stack_sourcereturnNc                    > [         TU ]  " S0 UD6  Xl        X l        X0l        U R
                  (       d   eU R
                  U l        g )Nr5   )super__init__ry   rZ   rz   rb   r{   )selfry   rZ   rz   rS   r`   s        r:   r   NNModuleVariable.__init__   s=     	"6"&$
{{{&*kk#ru   c                 @    U R                   =(       d    U R                  $ r4   r{   rb   r   s    r:   get_nn_module_stack_source+NNModuleVariable.get_nn_module_stack_source       **9dkk9ru   c                     Xl         g r4   r{   r   rb   s     r:   set_nn_module_stack_source+NNModuleVariable.set_nn_module_stack_source       &,#ru   c                     U R                   $ r4   )ry   r   s    r:   python_typeNNModuleVariable.python_type   s    ru   rM   r/   c                     g r4   r5   )r   rM   rb   submod	key_extraoptionss         r:   _wrap_submodule NNModuleVariable._wrap_submodule   s     	ru   c                    UR                   R                  U R                  5      n[        U[        R
                  R                  5      (       a  / nUR                  5        Hx  u  pE[        R                  R                  U5      nUR                   R                  UU R                  U[        [        U R                  U5      5      S9  UR                  U5        Mz     U$ [        U[        R
                  R                   [        R
                  R"                  [        R
                  R$                  45      (       d   ['        U5      5       eU R                  (       d   e/ n[)        U5       HW  u  puUR                  UR                   R                  UU R                  U[        [        U R                  U5      5      S95        MY     U$ Nrb   )outputget_submodulerZ   r@   rF   nn
ModuleDictrB   r	   ConstantVariablecreateregister_attr_or_moduler   r   rb   append
ModuleListParameterList
Sequentialr(   	enumerate)r   rM   baseresultr]   r   name_varidxs           r:   unpack_var_sequence$NNModuleVariable.unpack_var_sequence   sd   yy&&t7dEHH//00F $

$55<<TB		11OO)-T*JK	 2  h' !- M588&&(>(>@S@ST
 
 	4=	 
 {{{$T?KCMM		11OO)-S*IJ	 2  + ru   r]   r*   c                 *   UR                   R                  U R                  5      n[        X25      n[	        [        [        U R                  U5      5      R                  [        R                  5      5        [        R                  R                  U5      $ r4   )r   r   rZ   rO   r   r   r   rb   rm   r   HASATTRr	   r   r   )r   rM   r]   rQ   r   s        r:   call_obj_hasattr!NNModuleVariable.call_obj_hasattr   sn     ii%%doo6#:dkk489DD$$	

 ))0088ru   c                 f    UR                   R                  U R                  5      n[        USS5      $ )NtrainingF)r   r   rZ   getattrr   rM   rQ   s      r:   is_trainingNNModuleVariable.is_training   s)    ii%%doo6sJ..ru   c                     UR                   R                  U R                  5      n[        R                  " U5        UR
                  R                  S:w  a  [        R                  " [        U5      5        [        e)zIRestart analysis treating this module as an UnspecializedNNModuleVariabler   )
r   r   rZ   r   tagf_codeco_namemark_class_dynamicr?   r   r   s      r:   convert_to_unspecialized)NNModuleVariable.convert_to_unspecialized   sT    ii%%doo6c" 99
*00c;))ru   c                    UR                   R                  U R                  5      n[        U5      (       a  [	        S5        UR                   R
                  R                  X5      (       aC  UR                   R
                  R                  XSS9n[        U[        R                  5      (       + $ [        R                  US5      nX%;   $ )Nz-NNModuleVariable with custom __getattribute__T)
deleted_okri   )r   r   rZ   r"   r   side_effectshas_pending_mutation_of_attr	load_attrr@   r	   DeletedVariableobject__getattribute__)r   rM   keyr   mutated_attr	base_dicts         r:   has_key_in_generic_dict(NNModuleVariable.has_key_in_generic_dict   s    yy&&t7"4((IJ99!!>>tII9911;;DRV;WL!,	0I0IJJJ++D*=	ru   c                 P   [        U5      (       a  [        S5        [        USS9nUc  g[        U[        R
                  5      (       d  [        S5        S[        US5      0n[        R                  " XP40 UD6R                  U[        R                  R                  U5      /0 5      $ )zDCheck for a __getattr__ and handle it specially if it is implementedz6torch.nn.Module with a custom __getattribute__ definedT)ignore_nn_module_getattrNz6torch.nn.Module with a non-function custom __getattr__rb   __getattr__)r"   r   r   r@   rk   FunctionTyper   r	   UserMethodVariablecall_functionr   r   )r   r   rM   r]   
obj_source
getattr_fnr   s          r:   _custom_getattr_fallback)NNModuleVariable._custom_getattr_fallback  s    "4((RS'tL
*e&8&899RSZ
MBC++JHHVV++224892
 	
ru   c                    U R                   =(       a    [        U R                   U5      nUR                  R                  U R                  5      n[
        R                  US5      nSn[        5       n[        R                  " UR                  5       H,  nUR                  UR                  R                  5       5        M.     U R                   (       d  [        S5        US:X  a  [        R                   " XUS9$ X%;   a  XR   n	OcSU;   a  X%S   ;   a  X';  a	  US   U   n	OGSU;   a  X%S   ;   a	  US   U   n	O0SU;   a  X%S   ;   a	  US   U   n	O [        R"                  " XB5      n	SnUS
:X  a  [+        U R                   U5        US:X  a&  U(       d  [        R,                  " UR                  US9$ U(       ag  [.        R0                  " UW	[3        U5      5      n[5        U[6        [8        45      (       a)  UR;                  [        U R=                  5       U5      5        U$ [?        W	[@        5      (       am  U R                   (       a,  [        [        U R                   S5      U5      n[        US5      n[        RB                  " U	RD                  US9RG                  X/0 5      $ [?        U	[H        5      (       a=  [        RJ                  " U	RL                  [        RN                  " [Q        U5      5      US9$ [?        U	[R        5      (       a$  [        RB                  " U	RU                  U5      US9$ [?        U	[V        RX                  5      (       a  [        RJ                  " XUS9$ [[        U	5      (       d  []        U	5      (       a   [.        R0                  " X[3        U5      5      $ [_        SS[a        U5       SU S[a        U	5       3S[a        U	5       S3SU S[a        U	5       S[a        U5       S3S/[b        Rd                  QS9  [        R                   " XUS9$ ! [$         a5    U R'                  XAX R                   S	9n
U
b  U
s $ [)        [$        U5         GNf = f)Nri   TzGETATTR with no sourcer   _modules_parameters_buffersF)r   rM   r]   r   rf   r`   fgetz$Unsupported nn.Module attribute typeznn.Module subclass: z, name: z, attribute type: z>Dynamo does not support tracing nn.Module attributes of type ``zRefactor your code so that `z	` (type `z`) is not an attribute of `zqCurrently supported attribute types are methods, classmethods, staticmethods, properties, constants, and tensors.)gb_typecontextexplanationhints)3rb   r   r   r   rZ   r   r   rE   inspectgetmror`   updateri   keysr   r	   GetAttrVariablegetattr_staticAttributeErrorr   r
   rt   UserDefinedClassVariabler*   buildr   r@   rw   UnspecializedNNModuleVariabler   r   r    propertyUserFunctionVariabler   r   classmethodr   rg   r.   r?   staticmethod__get__rk   r   r   r   r   r(   r   SUPPORTABLE)r   rM   r]   rb   r   r   object_memberall_class_attribute_namesrJ   subobjr   outs               r:   var_getattrNNModuleVariable.var_getattr  s   >DKK!>yy&&t7++D*=	$'E!/A%,,QZZ__->? 0 {{23:,,TGG_F)#*--5z*40Fi'Dm4L,L}-d3F9$:1F)Fz*40F //; % 924;;E;}55dnnVTT!''FN64JKC# 02OPQQ ..t>>@$G J fh'';;'
4;;(LdSF'7F 55KK!  -Hb12 ,, 33OO77T
C! 
 -- 55NN4(   2 233 33FPP!&))Xf-=-=&,,R9OPP B274=/$Oabijpbqars"`ahioap`qqr s6tfIgfoEVVqryz~r  rA  AB  C> +66		
 ((FCCM " 664KK 7  %!M("s   P )Q>QQc                 *   UR                   R                  U R                  5      n[        U R                  U R	                  5       X5         [        U5      n[        U[        R                  R                  5      (       Ga
  UR                  R                  [        R                  R                  R                  L a  [        U5      (       a  U R                  U5        U(       a   S5       eU(       a   eUu  nUR                  R                  5        Hj  u  pxUR!                  UR                   R#                  UU R                  U[%        ['        U R(                  U5      5      S9U/0 5        UR+                  5       nMl     UsS S S 5        $ U(       a*  UR,                  b  UR,                  U l        [1        XX#5        UR                   R3                  5       (       a  UR4                  R7                  S5      (       av  UR4                  S:w  af  [        USSS9(       a  U R                  U5        SSKJn	  U	" UUR                   R<                  " S	U R                  /[?        X#5      Q76 S
9sS S S 5        $ U R(                  (       d   S5       e[        U[        R@                  RB                  5      (       a#  UR                  n
['        U R(                  S5      nO"URD                  n
['        U R(                  S5      n[G        U
[H        RJ                  5      (       a  U
RL                  n
['        US5      nU /U-   nO![G        U
[H        RN                  5      (       d   eURQ                  [R        RT                  " XS9UU5      sS S S 5        $ ! , (       d  f       g = f)Nz3Expected lazy sequential isn't a valid combination?r   )z	torch.nn.z	torch.ao.ztorch.nn.utils.parametrizeT)check_forward_hookscheck_backward_hooksr'   wrap_fx_proxycall_modulerM   proxyzsMust provide a valid source in order to inline, since inlined function may have default args which must be guarded.rf   
_call_implrg   )+r   r   rZ   rd   r   r   r@   rF   r   r   r`   rf   r!   r   r   rB   r   r   r   r   rb   popcls_to_becomery   rY   is_root_tracer
__module__
startswithbuilderr   create_proxyr#   rG   GraphModuler   r    rk   rl   rg   r   inline_user_function_returnr	   r   )r   rM   rR   rS   rQ   is_lazyrV   
child_namer   r   fn	fn_sources               r:   r   NNModuleVariable.call_function|  s    ii%%doo6#OOT<<>
 %S)G3 3 344MM))UXX-@-@-H-HH%c** 11"5 # I{ "!z*-,,*<*<*>&J$$		99" OO&#1*T[[*2U#V	 :  	 &&(C +? C
 
F $$0'*'8'8D$
 'r= 		((**NN--.HIINN&BB%T 11"52$))00% +48C
 
T {{ Z{ c588#7#788 B *4;;	 BIB *4;; EI"e..//B *9j AI 6D=D!"e&8&8999955222H{
 
 
s   D6NCN1D	N
Nc                   >^ ^^^^^^^^^^^ SSK JmJmJm  T R                  mTR
                  R                  T5      mUUUU U4S jnTS;   a  T R                  TTT5      $ TS:X  a8  [        T R                  T R                  5       TT5         U" T5      sS S S 5        $ TS:X  aT  [        R                  " [        R                  " TR                  R                  5      5      (       a  TR!                  S5      $ TS:X  a  TS   R#                  5       (       d   e[%        TS	   T5      (       d   eTS	   R&                  TS   R(                     n[%        U[*        5      (       a  U$ UR                  mTR
                  R                  T5      nTR
                  R-                  UTT[/        [1        T R2                  T5      5      S
9$ U(       a7  [5        TT5      n	TR                  R6                   ST S3m[9        TU	TTT5      $ UUU4S jmUUUUU4S jn
UUUU U4S jnUUUUU U4S jnS mTS:X  a  TR
                  R:                  R=                  [?        T R2                  S5      RA                  5       5        T(       d  T(       a   e/ nTRC                  5        H  u  mnURE                  U" TU5      5        M      T" U[G        5       S9$ TS:X  a  TR
                  R:                  R=                  [?        T R2                  S5      RA                  5       5        / nTRH                  " S00 U
" SS5      D6 H  u  mnURE                  U" TU5      5        M      T" U[G        5       S9$ TS:X  a  TR
                  R:                  R=                  [?        T R2                  S5      RA                  5       5        / nTRJ                  " S00 U
" SSS5      D6 H  u  mnURE                  U" TU5      5        M      T" U[G        5       S9$ TS:X  a  TR
                  R:                  R=                  [?        T R2                  S5      RA                  5       5        / nTRL                  " S00 U
" SSS5      D6 H  u  mnURE                  U" TU5      5        M      T" U[G        5       S9$ TS:X  am  TR
                  R:                  R=                  [?        T R2                  S5      RA                  5       5        T(       d  T(       a   eU" TRC                  5       5      $ TS:X  a]  TR
                  R:                  R=                  [?        T R2                  S5      RA                  5       5        U" TRM                  5       5      $ TS :X  ae  TR
                  R:                  R=                  [?        T R2                  S5      RA                  5       5        U" TRH                  " S00 U
" S5      D65      $ TS!:X  ae  TR
                  R:                  R=                  [?        T R2                  S5      RA                  5       5        U" TRJ                  " S00 U
" S5      D65      $ TS":X  aX  T(       d  T(       a   e/ nTRO                  5        H#  mURE                  TR!                  T5      5        M%     T" U[G        5       S9$ TS#:X  a&  T(       d  T(       a   eU" TR'                  5       5      $ TS$:X  aS  T(       d  T(       a   e/ nTR'                  5        H  u  mnURE                  U" TU5      5        M      T" U[G        5       S9$ TS%:X  a*  T(       d  T(       a   eTR!                  [Q        T5      5      $ TS&:X  a  [%        T[R        RT                  RV                  [R        RT                  RX                  45      (       aN  T(       aG  TS	   R#                  5       (       a/  TR!                  TS	   R[                  5       TR\                  ;   5      $ TS':X  Ga  T(       d  [Q        T5      S:X  d   e[R        RT                  RV                  R^                  [R        RT                  R`                  R^                  [R        RT                  RX                  R^                  [R        RT                  Rb                  R^                  [R        RT                  Rd                  R^                  4n[g        T5      R^                  U;  a  [%        TS	   [h        R                  5      (       d   [k        TS	   5      5       eTS	   R[                  5       m[%        T[l        [n        45      (       d   e[5        TT5      Rp                  n	[%        U	[r        Rt                  5      (       d   e[?        [?        T R2                  T5      S(5      nTRw                  [h        Rx                  " U	US
9T /[{        T5      -   T5      $ T R2                  (       d   e[%        TS	   [|        5      (       Ga;  TR
                  R~                  (       Ga  / n[{        [        [Q        T5      5      5      TS	   R[                  5          n[        TTS	   R[                  5          5       HT  u  nnUU   m[/        [1        T R2                  T5      5      nURE                  TR
                  R-                  UTUS
95        MV     TTS	   R[                  5          nTR
                  R-                  UT  S)3[/        [1        T R2                  TS	   R[                  5       5      5      S
9nU$ T R                  T5        SS*KCJDn  [%        TS	   U5      (       a  TS	   R                  TR
                  5      mO=TS	   R#                  5       (       a  TS	   R[                  5       mO[        S+TS	    35        TT   nTR
                  R-                  UT R                  T[/        [1        T R2                  T5      5      S
9$ TS,:X  d  [%        T[R        RT                  R                  R                  R                  5      (       a  TS-:X  dC  [%        T[R        RT                  R                  R                  R                  5      (       af  TS.:X  a`  [5        TT5      Rp                  n	[?        [?        T R2                  T5      S(5      nTRw                  [h        Rx                  " U	US
9T /T-   T5      $ TTR                  R                  ;   ai  [        TR                  R                  T   5      (       aB  [        S/ [        R                  " TTR                  5       5       5       5      (       a  U" T5      $ [        TT G]I  TTTT5      $ ! , (       d  f       GN= f)1Nr'   )r   ListIteratorVariableTupleVariablec           
         > T	R                   R                  STR                  S0 5      n[        UR                  T5        [        TT5      u  p#SSKJn  U" T	T	R                   R                  SU U/UQ7US9S9$ )Nget_attrr5   r'   r   call_method)rR   rS   r   )r   r  rZ   r$   rI   r#   r  r   )
r]   	mod_proxyrT   rU   r   rR   rS   moduler   rM   s
        r:   generic_call_method_helper@NNModuleVariable.call_method.<locals>.generic_call_method_helper  s     		..	I innf5'8v'F$J. ii,,!#1j1'	 -  ru   r   _wrapped_call_implrf   _check_input_dimT_get_item_by_idxr   r   __resultc                     > [        S [        R                  " T TR                  5       5       5       5      (       d  [	        ST 35        g g )Nc              3   @   #    U  H  oR                  5       v   M     g 7fr4   )is_python_constantr7   rJ   s     r:   r;   UNNModuleVariable.call_method.<locals>.assert_all_args_kwargs_const.<locals>.<genexpr>5  s      0V1$$&&0Vs   znon-const NNModule method )all	itertoolschainvaluesr   )rR   rS   r]   s   r:   assert_all_args_kwargs_constBNNModuleVariable.call_method.<locals>.assert_all_args_kwargs_const4  sE     09fmmo0V    :4&ABru   c                    > T" 5         [        T	T
5      n[        R                  " U5      R                  " T Vs/ s H  o"R	                  5       PM     sn0 TR                  5        VVs0 s H  u  p4X4R	                  5       _M     snnD6nUR                  5         UR                  nU  Vs0 s H  o3XS   _M	     sn$ s  snf s  snnf s  snf r4   )r   r   	signaturebindas_python_constantrB   apply_defaults	arguments)namesr  rJ   rK   rL   
bound_argsrR   r$  rS   r  r]   s         r:   
get_kwargs0NNModuleVariable.call_method.<locals>.get_kwargs:  s    (*&B **2.333784a'')489?H1**,,HJ %%'#--J.34ez}$e44 9H 5s   B;(C *Cc                    > / nU  HJ  u  p#UR                  TR                  R                  UTU[        T" TR                  U5      5      S95        ML     T" U[        5       S9$ )Nr   mutation_type)r   r   r   r   rb   r)   )	rB   r   r]   r   r  
gen_sourcer   r   rM   s	       r:   wrap_values1NNModuleVariable.call_method.<locals>.wrap_valuesE  sf    F %II55-jd.KL	 6  !& (>N>PQQru   c                    > T" TR                  U 5      TR                  R                  UTU [        T" TR                  U 5      5      S9/5      $ r   )r   r   r   r   rb   )r]   objr   r  r3  r   r   rM   s     r:   named_embed1NNModuleVariable.call_method.<locals>.named_embedR  sV     $++D1II55-jd.KL	 6 
 
ru   c                     UR                  S5      nUS   S:X  a  U $ [        U5      S:  a-  UR                  S5      n[        X5      n [        U5      S:  a  M-  U $ )N.r    )splitlenr   r   )rb   r]   
name_splitrJ   s       r:   r3  0NNModuleVariable.call_method.<locals>.gen_source_  sY    CJ!}"j/A%NN1%#F. j/A% Mru   named_childrenr   r1  named_parametersr   prefixrecursenamed_buffersr   remove_duplicatenamed_modulesmemochildrenmodules
parametersbuffersr   r#  rB   __len____contains____getitem__rg   z.__getitem__(slice))SymNodeVariablez%getitem on NNModuleVariable with key _get_abs_string_index_conv_forward_output_paddingc              3   V   #    U  H  n[        U[        R                  5      v   M!     g 7fr4   )r@   r	   TensorVariabler  s     r:   r;   /NNModuleVariable.call_method.<locals>.<genexpr>  s'      ?A 1i6677?s   ')r5   )Sr<  r   r  r  rZ   r   r   r   rd   r   r   is_torch_inline_allowedr   getfiler`   r  r   r  r@   rB   rz   r   r   r   r   rb   r   __name__r+   guard_on_key_orderaddr   r]   rA  r   r)   rB  rE  rG  r   r>  rF   r   r   ParameterDictr)  r   rO  r   r   r   r?   r	   r(   strintrg   rk   r   r  r   rC   r-   exportranger   r   tensorrP  evaluate_exprr   rJ  conv_ConvNd_ConvTransposeNdri   rj   r   r!  r"  r#  r~   r  ) r   rM   r]   rR   rS   constantr  mod_varr   r  r.  r4  r8  r   parambufferbuiltin_supportedsrcr   r   
new_modulenew_module_variablerP  r	  r   r  r  r$  r3  r   r  r`   s    `````                   @@@@@@@r:   r  NNModuleVariable.call_method  sq    	LKoo((-	 	2 77 %%b$77Y (!@!@!BB 2$7 
 %%+*M*MOOF,,==>+
 +
 $**400%%7--////d1g}55551gmmDGMM2G'#@AA$$CYY,,S1F9944%mDKK&EF	 5   &B&&//0$w?D/BdFKK	C		5 		5	R 	R	 		 ##II((,,ZZ-P-U-U-WX''F & 5 5 7fk$78 !8'>N>PQQ''II((,,4;;6;;= F%66  Xy1 e k$67  (>N>PQQ_$II((,,ZZ-P-U-U-WXF & 4 4 !Xy2DE!f k$78! (>N>PQQ_$II((,,ZZ-P-U-U-WXF & 4 4 !VX/AB!f k$78! (>N>PQQZII((,,ZZ-P-U-U-WX''v44677YII((,,ZZ-P-U-U-WXv33566\!II((,,4;;6;;= v66OI9NOPPYII((,,ZZ-P-U-U-WXv33Lj6KLMMV^''F.55d;< &'>N>PQQX''v||~..W_''F &fk$78 !/'>N>PQQY''#**3v;77N"6EHH$7$79O9O#PQQQ**,,#**Q**,?  ]"#d)q.00##//##//&&22&&22##//! F|''/@@!$q'9+E+EFFXPTUVPWHXXF1g002!#Sz2222VT*33!"e&8&89999 DKK!>
K55222cBFT$Z'  ;;;$q'=11 99###F  c&k 23DG4N4N4PQD'0Q8R8R8T1U'VV"3i,]4;;-LMII== & #'* >  (W "(Q(B(B(D!EJ*,))*K*K"& 34-)$++tAw7Q7Q7ST  +L +' /. 11"5/$q'?331g++BII6a++--1g002 Ed1gYOPC[F9944%mDKK&EF	 5   ++6588#3#3#8#8#@#@AAO+ 6588#3#3#8#8#I#IJJ-- &//B":dkk4#@*MI11..r)D  F$$---))224899 "v}}?  
 .d337&r4v>>S s   u00
u?)rZ   ry   r{   rz   rM   r/   rR   list[VariableTracker]rS   dict[str, VariableTracker]r|   r*   )F)rY  r   __qualname____firstlineno__r*   _nonvar_fieldsr?   r]  rF   r   Moduler   r   r   r   r   r   r   r   r   r   r   r   r   r  __static_attributes____classcell__r`   s   @r:   rw   rw      s    	
 
	'	'N22-029>2	2:- )
@
9)
914
9	
9/* 
"fDPi &i -	i
 
ib x? &	x?
 -x? 
x? x?ru   rw   c                     ^  \ rS rSrSSS1\R
                  kr SU 4S jjrS rS rS	 r	\
\R                  " S5      S
 5       5       rU 4S jr        SS jr      SU 4S jjrSS jrSU 4S jjrSS jrSrU =r$ )r   i"  
value_typeis_state_mutatedr{   Nc                 4  > [        U5      [        R                  R                  R                  L a  [        S5      eSU;   a,  [        US   SS 5      n[        U5      UL a  [        U5      US'   [        TU ]   " SSU0UD6  SU l	        U R                  U l        g )NzScriptModules aren't supported in UnspecializedNNModuleVariable becuase their .forward function isn't a static member of their typer{  r   rz   Fr5   )r?   rF   jit_scriptRecursiveScriptModuler   r   r~   r   r|  rb   r{   )r   rz   rS   lazy_value_to_becomer`   s       r:   r   &UnspecializedNNModuleVariable.__init__2  s    ;%))++AAAW  6!#*6,+?RV#W E{22
 (,E{|$/u// % '+kk#ru   c                 F    [        U[        5      (       d  [        U5      $ U$ r4   )r@   r   r   attr_sources     r:   _wrap_source*UnspecializedNNModuleVariable._wrap_sourceK  s!    +'BCC.{;;ru   c                 @    U R                   =(       d    U R                  $ r4   r   r   s    r:   r   8UnspecializedNNModuleVariable.get_nn_module_stack_sourceP  r   ru   c                     Xl         g r4   r   r   s     r:   r   8UnspecializedNNModuleVariable.set_nn_module_stack_sourceS  r   ru   c                  |   [         R                  R                  R                  [         R                  R                  R                  1n [         R                  R                  R
                  R                  5        Vs1 s H2  n[        US5      (       d  M  X;  d  M  [        UR                  5      iM4     sn$ s  snf )N__code__)
rF   r   rv  __setattr__r   ri   r#  rO   idr  )	supportedrJ   s     r:   _nn_module_method_ids3UnspecializedNNModuleVariable._nn_module_method_idsV  s     XX__00%((//2J2JK	 XX__--446
6q*% *+*< BqzzN6
 	
 
s   =B9B9B9c                   >  [         R                  " U R                  S5      nU[
        R                  R                  R                  [
        R                  R                  R                  [
        R                  R                  R                  4;   a7  UR                  [        R                  " U5      U /0 5      R                  U5      $ [        TU ]9  U5      $ ! [         a  n[        UeS nAff = f)N__iter__)r   r   r{  r   NotImplementedErrorrF   r   r   r  r   r   r  r	   r   r   r~   )r   rM   r  er`   s       r:   r   1UnspecializedNNModuleVariable.unpack_var_sequencea  s    	-''DB HH((HH""++HH((
 
 11..r2  "!"%& w*2..'  	-%1,	-s   !C 
C/#C**C/c                    U R                   n[        U5      (       a*  UR                  b  UR                  U l        [	        XX#5        [        U[        R                  R                  5      (       d6  UR                  R                  [        La  Sn[        U R                  U5      nOSn[        U R                  U5      n[        UR                  [        R                  5      (       Ga4  [        UR                   [        R                  5      (       Ga
  UR                  R                  [        L Ga  UR                   R                  ["        L Ga  SUR$                  ;  Ga  [&        R(                  " US5      n[        U[        R*                  5      (       Ga  UR,                  nU R/                  US5      R1                  5       R3                  5       (       GdF  U R/                  US5      R1                  5       R3                  5       (       Gd  U R/                  US5      R1                  5       R3                  5       (       d  U R/                  US5      R1                  5       R3                  5       (       d  UR/                  US5      R3                  5       (       d  UR/                  US	5      R3                  5       (       db  UR/                  US
5      R3                  5       (       d=  UR/                  US5      R3                  5       (       d  SnU R                  R4                  nU R6                  (       a!  [9        [9        U R6                  S5      U5      n	OS n	[;        U R6                  U5        U R6                  (       a-  [=        [?        [A        U5      5      U RC                  5       X5      O	[E        5       n
U
   [F        RH                  " XiS9RK                  X/[M        U5      -   U5      sS S S 5        $ ! , (       d  f       g = f)N__call__r   rf   _backward_hooks_backward_pre_hooks_forward_hooks_forward_pre_hooks_global_backward_pre_hooks_global_backward_hooks_global_forward_hooks_global_forward_pre_hooksr`   r   )'rz   r   r   r{  rY   r@   rF   rG   r  r  rg   r%   r   r    rk   rl   r   r&   ri   r   r   r   nn_modules_globals_vtr   realizer>  rf   rb   r   rt   rd   r]  r  r   r   r	   r   r   rC   )r   rM   rR   rS   rQ   r]   r  forward_method
globals_vtrb   ctxs              r:   r   +UnspecializedNNModuleVariable.call_functiony  s    jj#  ,"%"3"3"2D9 3 4 455%%-EED$/BD$/B
 3<<!1!122s~~u'7'788%%)AA''+HH-$33CCN.%*<*<==55
$$R):;CCEIIKK'',ABJJLPPRR'',<=EEGKKMM'',@AIIKOOQQ!--b2NOSSUU!--b2JKOOQQ!--b2IJNNPP!--b2MNRRTT$D00B;;
4;; DdKFF.t{{C@ {{ #BsGd==?  	 11"DRRFT$Z' SSs   01O++
O9c                   > US;   a{  [        U R                  U5      nU R                  (       a!  [        [        U R                  S5      U5      nOS n[        R
                  " XVS9R                  X/[        U5      -   U5      $ U[        U R                  S0 5      ;  Ga   [        R                  " [        U R                  5      U5      n[        U[        5      (       aZ  [        [        [        U R                  S5      U5      S5      nUR                  [        R
                  " UR                   US9UU5      $ [#        US5      (       a5  [%        UR&                  5      U R)                  5       ;   a  [+        SU 35        US:X  Ga  S	U R                  R,                  ;   a  US
   R/                  5       nUS   n	[        U	[        R0                  5      (       a+  U	R3                  5       [4        R6                  R8                  L d  XR                  R,                  S	   ;   a  SU l        OvXR                  R,                  S   ;   a  SU l        OR[        U	[        R<                  [        R>                  45      (       d  XR                  R,                  S   ;   a  SU l        U[4        R6                  R@                  RB                  L ao  [        US   [        RD                  5      (       aM  UR                  [        R
                  " [4        R6                  R@                  RF                  5      XS
   /U5      $ [H        T
U ]  XX45      $ ! [         a    S n GNf = f)Nr  r`   r   ri   rg   r  z&UnspecializedNNModuleVariable missing r  r   r   r'   Tr   r   )&r   r{  rb   r   r	   r   r   rC   rz   r   r   r?   r   r@   r   r  rg   rO   r  r  r  r   ri   r)  rU  r   rF   r   	Parameterr|  rw   r   rv  r  r   __delattr__r~   r  )r   rM   r]   rR   rS   r  rb   method	attr_namerz   r`   s             r:   r  )UnspecializedNNModuleVariable.call_method  s    77$/B{{#Jt{{K$H$O11"DRRFT$Z'  wtzz:r:: //TZZ0@$G &,//#z$++{CTJJ 55226??6R  
++v'4+E+E+GG FtfMN }$$**:M:M)M !G668	Q
 ui&>&>??))+uxx/A/AA**"5"5m"DD,0D)**"5"5j"AA,0D)%66%CC  !JJ$7$7
$CC -1D)444Q22: :
 5522588??3N3NO7O  w"2T::I " s    *M MMc                     U R                  X5      n[        U[        R                  5      (       a  UR	                  U5      $ g r4   )r   r@   r	   ConstDictVariablemaybe_getitem_const)r   rM   fieldname_vtdict_vts        r:   getattr_helper,UnspecializedNNModuleVariable.getattr_helper  s9    ""2-gy::;;..w77ru   c                   >^^ US;   a  UR                   R                  R                  X5      (       d  [        U R                  U5      n[        U[        5      (       at  [        U5      S:X  ae  U R                  (       a>  [        U R                  U5      n[        UR                  [        R                  5      5        [        R                  " 0 5      $ U R                  (       Ga	  US;   Ga  UR                   R                  R                  X5      (       d  [        U R                  U5      n[        U R                  U5      m[        TR                  [        R                   5      5        UR                   R"                  R%                  TR'                  5       5        U4S jm[        U4S j[)        UR+                  5       5       5       5      n[        R,                  " U[/        U5      TS9$ [0        TU ]e  X5      $ )N)r  r  r  r  r   )r  r  c                    > [         R                  R                  U5      n[        TU 5      n[	        TU5      n[
        R                  " X%5      nX64$ r4   )r	   r   r   r   r   r,   )irK   rL   r   
source_keysource_valuerz   hooks_dict_sources          r:   build_key_valueBUnspecializedNNModuleVariable.var_getattr.<locals>.build_key_valueF  sM    0077: 00A1E
01BJO+221Cz!ru   c              3   @   >#    U  H  u  nu  p#T" XU5      v   M     g 7fr4   r5   )r7   r  rK   rL   r  s       r:   r;   <UnspecializedNNModuleVariable.var_getattr.<locals>.<genexpr>Q  s%      :WYQa((:Ws   r   )r   r   r   r   rz   r@   rA   r>  rb   r   r   rm   r   EMPTY_NN_MODULE_HOOKS_DICTr	   r  SEQUENCE_LENGTHrZ  r[  r]   r   rB   NNModuleHooksDictVariabler?   r~   r   )	r   rM   r]   
hooks_dicthooks_sourcer   r  r  r`   s	         @@r:   r   )UnspecializedNNModuleVariable.var_getattr  s    
 
 99))FFtRR$TZZ6
j$//C
Oq4H{{'1$++t'D%(33 , G G
 %66r:: KKK
 II**GGSS T2J *4;; =+66|7S7STUII((,,->-C-C-EF	"  :CJDTDTDV:W F 66Z(1B  w"2,,ru   c                     [         R                  " U5      nU R                  USU5      nUc  U R                  USU5      nUc  U R                  USU5      nUc  [        [        U5        U$ )z
Dynamo tracing of nn.Module __getattr__ can be expensive if the model
has deep submodule hierarchy. Since the __getattr__ is stable, we can
directly look into the underlying datastructures. This saves a lot of
compilation time.
r   r   r   )r	   r   r  r
   r   )r   rM   r]   r  r   s        r:    manually_trace_nn_module_getattr>UnspecializedNNModuleVariable.manually_trace_nn_module_getattrZ  sn     ,,T2!!"mW=;%%b*g>C;%%b*g>C;$^R8
ru   )r|  r{   r{  r|   N)rM   r/   rR   rq  rS   rr  r|   r*   rp  ro  )rY  r   rs  rt  r.   ru  r   r  r   r   r   ro   	lru_cacher  r   r   r  r  r   r  rw  rx  ry  s   @r:   r   r   "  s      
#	1	1	N22
:- 
  
/0A#A &A -	A
 
AFY; &	Y;
 -Y; 
Y;v;-z ru   r   c                       \ rS rSrSrS rSrg)$UnspecializedBuiltinNNModuleVariableil  z_
Differentiates between builtin nn modules (e.g. torch.nn.Linear) and user defined nn modules.
c                 F    [        U[        5      (       d  [        U5      $ U$ r4   )r@   r   r  s     r:   r  1UnspecializedBuiltinNNModuleVariable._wrap_sourceq  s!    +'IJJ5kBBru   r5   N)rY  r   rs  rt  __doc__r  rw  r5   ru   r:   r  r  l  s    ru   r  c                   6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )FSDPManagedNNModuleVariableiw  a  
Tracing behavior: trace into submodules and treat them as Unspecialized, do not
register parameters to the top-level, treat them as function inputs.

Guards behavior: if 'skip_fsdp_guards', many guards that would be installed
by a vanilla UnspecializedNNModuleVariable are simply dropped, on the basis
that a user wrapping their model in FSDP(model) is already opting into a
requirement to not modify internal model state, which would already break FSDP without
compilation.
c                 n   > UR                  SS 5      nUc   S5       e[        TU ]  " SSU0UD6  X0l        g )Nrb   zMFSDPManagedNNModule depends on having an accurate source to control guarding.rz   r5   )r_   r~   r   rb   )r   rz   rS   rb   r`   s       r:   r   $FSDPManagedNNModuleVariable.__init__  sE    Hd+! 	
[	
! 	/u//ru   c                     [        U[        [        45      (       d?  [        R                  R
                  R                  (       a  [        U5      $ [        U5      $ U$ r4   )r@   r   r   rF   _dynamoconfigskip_fsdp_guardsr  s     r:   r  (FSDPManagedNNModuleVariable._wrap_source  sL    ,.IJ
 
 }}##44)+662;??ru   r   r  )	rY  r   rs  rt  r  r   r  rw  rx  ry  s   @r:   r  r  w  s    	 ru   r  ro  )Jr  ro   r   r!  rk   
contextlibr   r   typingr   torch.nnrF   r<  r   r   r	   excr
   r   r   r   r   guardsr   r   mutation_guardr   rb   r   r   r   r   r   r   r   r   utilsr   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r   r(   r)   r*   	functionsr+   lazyr,   listsr-   user_definedr.   torch._dynamo.symbolic_convertr/   rY   r]  r   rv  rd   rt   rw   r   r  r  r5   ru   r:   <module>r     s   .     2    8 8  1 .	 	 	    = < 3 %   3 D;8 	+s 	+UXX__ 	+ 	+BD
? D
?NG$= GT
+H "? ru   