o
    ‡ZŽh  ã                   @   s    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d„Zdej	deee eej	 f fd	d
„Zdej	dee fdd„Zdej	defdd„ZdS )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                 C   s   t | ƒ}|du r
dS d|vS )zA
    Returns if ``module`` can compose with ``fully_shard``.
    NTZ	replicater   )r   Úregistry© r	   úV/var/www/auris/lib/python3.10/site-packages/torch/distributed/fsdp/_traversal_utils.pyÚ_composable"   s   r   c           	      C   sª   g }g }t ƒ }t ƒ }t | g¡}|rQ| ¡ }| |¡ t|ƒs qtt| ¡ ƒƒD ]}||vr3| 	|¡ q(t
|ƒ}|durO||vrO| |¡ | |¡ | |¡ |s||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``).
    N)ÚsetÚcollectionsÚdequeÚpopleftÚaddr   ÚreversedÚlistÚchildrenÚ
appendleftr   Úappend)	r   Úfsdp_statesZfsdp_modulesZvisited_fsdp_statesZvisited_modulesr   Ú	submoduleZchild_moduleZoptional_stater	   r	   r
   Ú_get_fsdp_states_with_modules0   s*   

€


ôr   c                 C   s   t | ƒ\}}|S )z*See :func:`_get_fsdp_states_with_modules`.)r   )r   r   Ú_r	   r	   r
   Ú_get_fsdp_states`   s   r   c                 C   s   d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                 S   s   g | ]
}|j d ur|j ‘qS )N)Z_handle)Ú.0Z
fsdp_stater	   r	   r
   Ú
<listcomp>k   s
    
þz%_get_fsdp_handles.<locals>.<listcomp>)r   )r   Zhandlesr	   r	   r
   Ú_get_fsdp_handlesf   s   þr   )Ú__doc__r   Ztorch.nnÚnnZ&torch.distributed._composable.contractr   Z$torch.distributed.fsdp._common_utilsr   r   ÚModuleÚboolr   Útupler   r   r   r   r	   r	   r	   r
   Ú<module>   s    ÿ
þ0