
    &Vji                         d Z ddlZddlmZ ddlmZ ddlmZmZ 	 dej	        de
fdZdej	        deee         eej	                 f         fdZdej	        dee         fd	Zdej	        defd
ZdS )z
NOTE: This file must be imported like
``import torch.distributed.fsdp._traversal_utils`` and not like
``from torch.distributed.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                 0    t          |           }|dS d|vS )zA
    Returns if ``module`` can compose with ``fully_shard``.
    NT	replicater   )r   registrys     a/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/distributed/fsdp/_traversal_utils.py_composabler   "   s'    
 V$$Hth&&    c                 *   g }g }t                      }t                      }t          j        | g          }|r|                                }|                    |           t          |          s;t          t          |                                                    D ]}||vr|	                    |           t          |          }|C||vr?|                    |           |                    |           |                    |           |||fS )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   s1   & %'K$&L ,/55 '*eeO
 +6*;VH*E*EE
 +MMOO	I&&&9%% 	$T)*<*<*>*>%?%?@@ 	/ 	/L?22  .../	::%.@S*S*S##N333~...	***  + $$r   c                 *    t          |           \  }}|S )z*See :func:`_get_fsdp_states_with_modules`.)r!   )r   r   _s      r   _get_fsdp_statesr$   `   s    26::NKr   c                 8    d t          |           D             }|S )z
    Returns all ``FlatParamHandle`` s in the module tree rooted at ``module``
    following the rules in :func:`_get_fsdp_state`.
    c                 *    g | ]}|j         	|j         S )N)_handle).0
fsdp_states     r   
<listcomp>z%_get_fsdp_handles.<locals>.<listcomp>k   s.       ) 	)))r   )r$   )r   handless     r   _get_fsdp_handlesr,   f   s/    
 *622  G
 Nr   )__doc__r   torch.nnnn&torch.distributed._composable.contractr   $torch.distributed.fsdp._common_utilsr   r   Moduleboolr   tupler   r!   r$   r,    r   r   <module>r6      s              @ @ @ @ @ @ S S S S S S S S&'	 'd ' ' ' '-%I-%
4
T")_,--% -% -% -%`RY 4
+;    
bi 
D 
 
 
 
 
 
r   