Ë
    P/æj!  ã                   ó  — d Z ddlZddlmZmZmZm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y)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)ÚDequeÚListÚSetÚTuple©Ú_get_registry)Ú
_FSDPStateÚ_get_module_fsdp_stateÚmoduleÚreturnc                 ó&   — t        | «      }|€yd|vS )zA
    Returns if ``module`` can compose with ``fully_shard``.
    TÚ	replicater   )r   Úregistrys     új/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torch/distributed/fsdp/_traversal_utils.pyÚ_composabler   #   s"   € ô
 ˜VÓ$€HØÐØØ˜hÐ&Ð&ó    c                 óÌ  — g }g }t        «       }t        «       }t        j                  | g«      }|r±|j                  «       }|j	                  |«       t        |«      sŒ/t        t        |j                  «       «      «      D ]  }||vsŒ|j                  |«       Œ t        |«      }|�7||vr3|j	                  |«       |j                  |«       |j                  |«       |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%   1   sê   € ð& %'€KØ$&€Lô ,/«5Ðô '*£e€Oô
 *×/Ñ/°°Ó9€EÙ
Ø—M‘M“Oˆ	Ø×Ñ˜IÔ&Ü˜9Ô%ØÜ$¤T¨)×*<Ñ*<Ó*>Ó%?Ó@ò 	/ˆLØ ?Ò2Ø× Ñ  Õ.ð	/ô 0°	Ó:ˆØÐ%¨.Ð@SÑ*SØ×#Ñ# NÔ3Ø×Ñ˜~Ô.Ø×Ñ 	Ô*ò ð ˜Ð$Ð$r   c                 ó"   — t        | «      \  }}|S )z*See :func:`_get_fsdp_states_with_modules`.)r%   )r   r   Ú_s      r   Ú_get_fsdp_statesr(   a   s   € ä2°6Ó:�N€K�ØÐr   c                 ól   — t        | «      D �cg c]  }|j                  �|j                  ‘Œ }}|S c c}w )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-   g   sD   € ô +¨6Ó2öàØ×ÑÐ)ð 	×Óð€Gð ð
 €Nùòs   Ž1)Ú__doc__r   Útypingr   r   r   r   Útorch.nnÚnnÚ&torch.distributed._composable.contractr   Ú$torch.distributed.fsdp._common_utilsr	   r
   ÚModuleÚboolr   r%   r(   r-   © r   r   ú<module>r7      sž   ðñó ß *Ó *å Ý @ß Sðð&'˜Ÿ	™	ð ' dó 'ð-%Ø�I‰Ið-%à
ˆ4�
Ñ˜T "§)¡)™_Ð,Ñ-ó-%ð`˜RŸY™Yð ¨4°
Ñ+;ó ð
˜bŸi™ið 
¨Dô 
r   