ó
    EñiÂ*  ã                   ó‚  • S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJrJr  S SKJrJrJrJrJrJrJr  S\
R,                  S\\-  S	\\
R,                     S
\\
R0                     S\\\4   S\4S jrS\
R,                  4S jrS\\\
R,                        4S jrS\
R,                  S\\
R,                     S
\\
R0                     S\4S jr S\
R,                  S\!\"\\
R,                  4      4S jr#S\
R,                  S
\\
R0                     S\\
R,                     S\S\\
R0                  \4   4
S jr$g)é    N)ÚCallable)Úpartial)ÚAny)Ú_get_module_fsdp_stateÚ _override_module_mixed_precision)Ú_construct_wrap_fnÚ
_or_policyÚ_PolicyÚ_post_order_applyÚ_recursive_wrapÚ$_run_mixed_precision_override_policyÚ_wrap_module_cls_individuallyÚroot_moduleÚpolicyÚignored_modulesÚignored_paramsÚroot_kwargsÚfsdp_fnc           	      óª  • US   n[         R                  " U5      n[        U 5        [        U[        5      (       aª  SXG(       a  SOS'   UR                  XU5      nUb9  [        U UR                  UUU5      n[        XR                  5      n	[        U	5        UR                  SS5      n
[        U [        UR                  5       5      UU
5        [        XU5      n[        X5        gU UUUUSS.nUbU  [        XR                  5      n	[         R"                  " [$        U[#        [&        UR                  S	9/S
9nXS'   [        U	5        [)        S0 UDUD6  g)a'  
Auto wraps modules in ``root_module`` 's tree according to ``policy``
following a post-order traversal.

Precondition: ``root_kwargs`` should contain all arguments except
``module``. This function accepts the kwargs dict directly since it gets
forwarded into the post-order traversal function.
Úmixed_precisionNÚauto_wrap_policyr   Úuse_orig_paramsFT)Úmoduler   Úwrapper_clsr   r   Úonly_wrap_children)Úmodule_classes)Úpolicies© )ÚinspectÚisclassÚ_check_nested_wrappingÚ
isinstancer
   Ú_run_policyr   Ú_module_classes_to_ignorer   Ú#_warn_on_overridden_mixed_precisionÚgetÚ_validate_frozen_paramsÚsetÚkeysr   r   Ú	functoolsr   r	   r   r   )r   r   r   r   r   r   r   Ú
is_wrapperÚtarget_module_to_kwargsÚoverridden_module_classesr   Úwrap_fnÚrecursive_wrap_kwargss                Ú_/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/distributed/fsdp/_wrap_utils.pyÚ
_auto_wrapr1      s�  € ð  "Ð"3Ñ4€OÜ—’ Ó)€Jô ˜;Ô'ä�&œ'×"Ñ"ØFJˆ­*Ñ&¸(ÑCØ"(×"4Ñ"4Ø¨+ó#
Ðð Ñ&Ü&JØØ×9Ñ9ØØØ'ó'Ð#ô )IØ×FÑFó)Ð%ô 0Ð0IÔJØ%Ÿ/™/Ð*;¸UÓCˆÜØÜÐ'×,Ñ,Ó.Ó/ØØô		
ô % [È7ÓSˆÜ˜+Ô/Øð Ø"ØØ*Ø(Ø"ñÐð Ñ"ô %EØ×BÑBó%
Ð!ô ×"Ò"ÜàÜÜ1Ø#2×#LÑ#Lñðñ	
ˆð 5;Ð0Ñ1Ü+Ð,EÔFÜÑ;Ð+Ð;¨{Ó;ó    c                 óp   • U R                  5        H"  u  p[        U5      c  M  [        SU SU  35      e   g )NzOFSDP auto wrapping requires modules to not already have FSDP applied but found z in
)Únamed_modulesr   Ú
ValueError)r   Úmodule_namer   s      r0   r!   r!   i   sF   € Ø*×8Ñ8Ö:ÑˆÜ! &Ó)Ó5Üð*Ø*5¨°e¸K¸=ðJóð ò  ;r2   r-   c                 óV   • [        U 5      S:X  a  g [        R                  " SU  S3SS9  g )Nr   zvBoth mixed precision and an auto_wrap_policy were specified to FSDP, where the wrapped module has submodules of type:
zX
These modules will be wrapped as separate FSDP instacnes with mixed precision disabled.é   ©Ú
stacklevel)ÚlenÚwarningsÚwarn)r-   s    r0   r%   r%   r   s<   € ô Ð$Ó%¨Ó*ØÜ‡M‚Mð	=Ø=VÐ<Wð Xð	ð ór2   Úmodules_to_wrapr   c                 óX  • [        U 5      n[        5       nU GH  u  pgXq;   d  M  [        XrXV5      n/ n	Sn
/ nSnUR                  5        H^  u  pÞUR                  (       a%  UR                  U5        XÍR                  5       -  nM;  U	R                  U5        X­R                  5       -  n
M`     [        U	5      S:”  d  M¤  [        U5      S:”  d  Mµ  U S3nU(       a  X¬-   nUSU SU S3-  nOUS-  nUS-  nUSU S	U	 3-  nU(       a  [        R                  " US
S9  GM  [        U5      e   g)a  
This checks that, given ``modules_to_wrap``, each module would manage
parameters that are uniformly frozen or non-frozen. This uniformity
requirement is strict for ``use_orig_params=False`` (hard error) and highly
recommended for ``use_orig_params=True`` (user warning).
r   z7 has both parameters with requires_grad=True and False.zi We do not recommend wrapping such modules since the gradient memory usage will be higher than expected (z numel instead of z, numel before sharding via reduce-scatter). zI FSDP does not support wrapping such modules when use_orig_params=False. z>If possible, wrap the frozen parameters with FSDP separately.
z2The following parameters have requires_grad=True:
z4
The following parameters have requires_grad=False:
r8   r9   N)Ú_get_post_order_named_modulesr(   Ú_get_managed_param_to_fqnÚitemsÚrequires_gradÚappendÚnumelr;   r<   r=   r5   )r   r>   r   r   Úpost_order_named_modulesÚvisited_modulesr6   r   Úparam_to_fqnÚfrozen_param_fqnsÚfrozen_param_numelÚnonfrozen_param_fqnsÚnonfrozen_param_numelÚparamÚfqnÚmsgÚtotal_param_numels                    r0   r'   r'   €   sg  € ô  =¸[ÓIÐÜ&)£e€OÜ7ÑˆØÕ$Ü4Ø¨óˆLð ,.ÐØ!"ÐØ.0Ð Ø$%Ð!Ø*×0Ñ0Ö2‘
�Ø×&×&Ø(×/Ñ/°Ô4Ø)¯[©[«]Ñ:Ò)à%×,Ñ,¨SÔ1Ø&¯+©+«-Ñ7Ò&ñ 3ô Ð$Ó%¨Õ)¬cÐ2FÓ.GÈ!Õ.KØ$˜Ð%\Ð]�Þ"Ø(:Ñ(RÐ%Øðà-Ð.Ð.@ÐAVÐ@Wð X@ð@ñ‘Cð ÐfÑf�CØÐXÑX�ØØIÐJ^ÐI_ð `KØK\ÐJ]ð_ñ�ö #Ü—M’M #°!Õ4ä$ S›/Ð)òI  8r2   Úreturnc                 óV  • U 1nSU 4/n/ nU(       aƒ  UR                  5       u  pEUR                  XE45        UR                  5        HB  u  pgUc  M
  Xq;  d  M  UR                  U5        US:w  a  US-   U-   nUR                  Xg45        MD     U(       a  Mƒ  [	        [        U5      5      nU$ )a»  
This returns the named modules following a post-order traversal, which is a
valid reverse topological sort. We achieve this using the reverse of a
stack-based DFS order instead of reversing ``root_module.named_modules()``
since the former gives the modules in registration order at each level in
the module tree (as opposed to the reverse), which allows us to error/warn
on the first registered module that violates the condition.

For example, consider the following module structure:
    M(
      S1(),
      S2(
        SS1(),
        SS2(),
      ),
      S3(),
    )
The reverse DFS order is [S1, SS1, SS2, S2, S3, M], while the reverse
``named_modules()`` order is [S3, SS2, SS1, S2, S1, M].
Ú Ú.)ÚpoprD   Únamed_childrenÚaddÚlistÚreversed)	r   rG   ÚstackÚ reverse_post_order_named_modulesr6   r   Úchild_module_nameÚchild_modulerF   s	            r0   r@   r@   µ   s¾   € ð. #�m€OØ�+ÐÐ€EàDFÐ$Þ
Ø#Ÿi™i›kÑˆØ(×/Ñ/°Ð0EÔFØ/5×/DÑ/DÖ/FÑ+ÐØÑ#ÙØÕ2Ø×#Ñ# LÔ1Ø "Ó$Ø(3°cÑ(9Ð<MÑ(MÐ%Ø—‘Ð/Ð>Ö?ñ 0G÷ ˆ%ô  $¤HÐ-MÓ$NÓOÐØ#Ð#r2   Úmodule_to_wraprG   Úroot_prefixc                 ó¶  • 0 n[         R                  " X4/5      nUR                  U 5        U(       a¦  UR                  5       u  pgUR	                  SS9 H   u  p‰X‘;  d  M  US:X  a  UOUS-   U-   n
X¤U	'   M"     UR                  5        HD  u  p¼Uc  M
  XÂ;  d  M  UR                  U5        US:X  a  UOUS-   U-   nUR                  XÍ45        MF     U(       a  M¦  U$ )aö  
This returns a dict that maps managed parameter to its FQN for the given
``module_to_wrap``. The dict's keys are exactly the parameters that would
be managed by the module, where this is achieved by calling this function
on the modules to wrap in reverse topological order, destructively updating
``visited_modules``, and not traversing into those modules. The FQNs are
prefixed from the root (via ``root_prefix``) to be more informative.

NOTE: This function is meant to be called pre-wrapping and iteratively in
reverse topological order to cover the full module tree. This differs from
the ``_get_param_to_fqn()`` function meant to be called post-wrapping and
on the full module tree in one shot. Given those differences, we do not try
to unify the two.
F)ÚrecurserS   rT   )ÚcollectionsÚdequerW   ÚpopleftÚnamed_parametersrV   rD   )r^   r   rG   r_   rH   Úqueuer   ÚprefixÚ
param_namerM   rN   r\   r]   Úchild_prefixs                 r0   rA   rA   ß   sô   € ð( -/€Lä×Ò Ð<Ð=Ó>€EØ×Ñ˜Ô'Þ
ØŸ™›‰ˆØ!'×!8Ñ!8ÀÐ!8Ó!GÑˆJØÕ*Ø$*¨b£L‘j°f¸s±lÀZÑ6O�Ø&)˜UÓ#ñ "Hð 06×/DÑ/DÖ/FÑ+ÐØÑ#ÙØÕ2Ø×#Ñ# LÔ1ð  “|ñ &à #™Ð(9Ñ9ð ð
 —‘˜lÐ9Ö:ñ 0G÷ ˆ%ð" Ðr2   )%rb   r*   r   r<   Úcollections.abcr   r   Útypingr   Útorch.nnÚnnÚ$torch.distributed.fsdp._common_utilsr   r   Útorch.distributed.fsdp.wrapr   r	   r
   r   r   r   r   ÚModuler(   Ú	ParameterÚdictÚstrr1   r!   Útyper%   Úboolr'   rX   Útupler@   rA   r   r2   r0   Ú<module>rw      sƒ  ðã Û Û Û Ý $Ý Ý å ÷÷÷ ñ ðL<Ø—‘ðL<à�wÑðL<ð ˜Ÿ™‘^ðL<ð ˜Ÿ™Ñ%ð	L<ð
 �c˜3�h‘ðL<ð ôL<ð^¨¯	©	ô ðØ" 4¨¯	©	¡?Ñ3ôð2*Ø—‘ð2*à˜Ÿ™‘^ð2*ð ˜Ÿ™Ñ%ð2*ð ô	2*ðj'$Ø—‘ð'$à	ˆ%��R—Y‘Y�Ñ
Ñ ô'$ðT)Ø—I‘Ið)à˜Ÿ™Ñ%ð)ð ˜Ÿ™‘^ð)ð ð	)ð
 
ˆ"�,‰,˜Ð
Ñõ)r2   