
    sh                        S r SSKrSSKJr  SSKJr  SSKJrJr   S\R                  S\
4S jrS\R                  S\\\   \\R                     4   4S jrS\R                  S\\   4S	 jrS\R                  S\4S
 jrg)z
NOTE: This file must be imported like
``import torch.distributed.fsdp._traversal_utils`` and not like
``from torch.distirbuted.fsdp._traversal_utils import ...`` to avoid circular
imports. For brevity, we may import the file as ``traversal_utils``.
    N_get_registry)
_FSDPState_get_module_fsdp_statemodulereturnc                 *    [        U 5      nUc  gSU;  $ )z9
Returns if ``module`` can compose with ``fully_shard``.
T	replicater   )r   registrys     {/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/distributed/fsdp/_traversal_utils.py_composabler   "   s"    
 V$Hh&&    c                    / n/ n[        5       n[        5       n[        R                  " U /5      nU(       a  UR                  5       nUR	                  U5        [        U5      (       d  M:  [        [        UR                  5       5      5       H  nXt;  d  M
  UR                  U5        M     [        U5      nUb8  X;  a3  UR	                  U5        UR                  U5        UR                  U5        U(       a  M  X4$ )a  
Returns a tuple containing:
1. A list of the ``_FSDPState`` instances in the module tree rooted at
``module`` without any duplicates and following the ``module.modules()``
traversal order (which is assumed to be depth-first).
2. A corresponding list of the modules owning the states in the first list.

For the wrapper code path, both returned lists are the same, each
containing all ``FullyShardedDataParallel`` instances. For the composable
code path, this returns a list of all composable state instances and a list
of the corresponding fully sharded modules. See [Note: Fully Sharded
Module].

NOTE: The traversal does not proceed into any module annotated by an
incompatible API (e.g. ``replicate``).
)setcollectionsdequepopleftaddr   reversedlistchildren
appendleftr   append)	r   fsdp_statesfsdp_modulesvisited_fsdp_statesvisited_modulesr   	submodulechild_moduleoptional_states	            r   _get_fsdp_states_with_modulesr!   0   s    & %'K$&L ,/5 '*eO
 +6*;*;VH*EE
MMO	I&9%%$T)*<*<*>%?@L2  . A 0	:%.*S##N3~.	* % $$r   c                      [        U 5      u  pU$ )z*See :func:`_get_fsdp_states_with_modules`.)r!   )r   r   _s      r   _get_fsdp_statesr$   `   s    26:NKr   c                 x    [        U 5       Vs/ s H  nUR                  c  M  UR                  PM      nnU$ s  snf )z{
Returns all ``FlatParamHandle`` s in the module tree rooted at ``module``
following the rules in :func:`_get_fsdp_state`.
)r$   _handle)r   
fsdp_statehandless      r   _get_fsdp_handlesr)   f   sF     +622J 	
2  
 Ns   77)__doc__r   torch.nnnn&torch.distributed._composable.contractr   $torch.distributed.fsdp._common_utilsr   r   Moduleboolr   tupler   r!   r$   r)    r   r   <module>r3      s      @ S&'		 'd '-%II-%
4
T"))_,--%`RYY 4
+; 
bii 
D 
r   