
    sg!                        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     Z/var/www/html/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 '*eO
 *//9E
MMO	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    26:NK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 '-%II-%
4
T"))_,--%`RYY 4
+; 
bii 
D 
r   