Ë
    P/æj�  ã            
       óÚ   — d dl m Z  d dlmZmZ d dlZd dlmc mc mZ	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d dlmZ dgZd	ej*                  d
edeeeeef   f   dej*                  fd„Zy)é    ©Úfnmatch)ÚDictÚUnionN)Ú
DeviceMesh)Úis_rng_supported_meshÚTensorParallelRNGTracker)Ú_validate_tp_mesh_dim)ÚParallelStyleÚparallelize_moduleÚmoduleÚdevice_meshÚparallelize_planÚreturnc                 óJ  ‡
— t         j                  j                  d«       t        |«       t	        |«      rrt        t        j                  t        «      sTt        |j                  «      t        _        t        j                  j                  |d¬«       dt        j                  _        t        |t        «      r|j                  | |«      S t        |t        «      r²|j                  «       D ]�  \  }}|j!                  d«      }t#        |«      dk(  rt%        d«      ‚|sŒ3|j'                  d«      Š
t)        ˆ
fd„| j+                  «       «      }|D ]5  \  }}|r!dj-                  |«      }	t/        |||	|i«       Œ)t/        |||«       Œ7 |rŒkŒŸ | S t1        d	t3        |«      › d
�«      ‚)a  
    Apply Tensor Parallelism in PyTorch by parallelizing modules or sub-modules based on a user-specified plan.

    We parallelize module or sub_modules based on a parallelize_plan. The parallelize_plan contains
    :class:`ParallelStyle`, which indicates how user wants the module or sub_module
    to be parallelized.

    User can also specify different parallel style per module fully qualified name (FQN).

    Note that ``parallelize_module`` only accepts a 1-D :class:`DeviceMesh`, if you have a 2-D or N-D :class:`DeviceMesh`,
    slice the DeviceMesh to a 1-D sub DeviceMesh first then pass to this API(i.e. ``device_mesh["tp"]``)

    Args:
        module (:class:`nn.Module`):
            Module to be parallelized.
        device_mesh (:class:`DeviceMesh`):
            Object which describes the mesh topology
            of devices for the DTensor.
        parallelize_plan (Union[:class:`ParallelStyle`, Dict[str, :class:`ParallelStyle`]]):
            The plan used to parallelize the module. It can be either a
            :class:`ParallelStyle` object which contains how
            we prepare input/output for Tensor Parallelism or it can be a
            dict of module FQN and its corresponding :class:`ParallelStyle` object.
    Return:
        A :class:`nn.Module` object parallelized.

    Example::
        >>> # xdoctest: +SKIP("distributed")
        >>> from torch.distributed.tensor.parallel import parallelize_module, ColwiseParallel
        >>> from torch.distributed.device_mesh import init_device_mesh
        >>>
        >>> # Define the module.
        >>> m = Model(...)
        >>> tp_mesh = init_device_mesh("cuda", (8,))
        >>> m = parallelize_module(m, tp_mesh, {"w1": ColwiseParallel(), "w2": RowwiseParallel()})
        >>>

    .. note:: For complex module architecture like Attention, MLP layers, we recommend composing
        different ParallelStyles together (i.e. ``ColwiseParallel`` and ``RowwiseParallel``) and pass
        as a parallelize_plan, to achieves the desired sharding computation.
    z4torch.distributed.tensor.parallel.parallelize_moduleiÒ  )Ú	base_seedFú.r   z9Expect module path to be non-empty, but got empty string!c                 ó"   •— t        | d   ‰«      S )Nr   r   )ÚtÚatoms    €úh/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torch/distributed/tensor/parallel/api.pyú<lambda>z$parallelize_module.<locals>.<lambda>a   s   ø€ œg a¨¡d¨DÓ1€ ó    zLExpect Union[ParallelStyle, Dict[str, ParallelStyle]] for parallelize_plan, z found!)ÚtorchÚ_CÚ_log_api_usage_oncer
   r   Ú
isinstanceÚrandomÚ_rng_trackerr	   Údevice_typeÚ_manual_seedÚdistribute_region_enabledr   Ú_applyÚdictÚitemsÚsplitÚlenÚ
ValueErrorÚpopÚfilterÚnamed_childrenÚjoinr   Ú	TypeErrorÚtype)r   r   r   Úmodule_pathÚparallelize_styleÚpath_splitsÚmatched_childrenÚ_Ú	submoduleÚ	leaf_pathr   s             @r   r   r      s¢  ø€ ô\ 
‡H�H× Ñ Ð!WÔXä˜+Ô&ô ˜[Ô)´*Ü×ÑÔ5ô3ô 7°{×7NÑ7NÓOŒÔä×Ñ×(Ñ(¨ÀÐ(ÔEð 9>Œ×ÑÔ5äÐ"¤MÔ2Ø×&Ñ& v¨{Ó;Ð;Ü	Ð$¤dÔ	+Ø.>×.DÑ.DÓ.Fò 	VÑ*ˆKÐ*Ø%×+Ñ+¨CÓ0ˆKÜ�;Ó 1Ò$Ü ØOóð ò Ø"—‘ qÓ)�Ü#)ã1Ø×)Ñ)Ó+ó$Ð ð %5ò V‘L�A�yÙ"à$'§H¡HØ'ó%˜	ô +Ø% {°YÐ@QÐ4Rõô
 +¨9°kÐCTÕUðVó ð	Vð4 ˆäð"Ü"&Ð'7Ó"8Ð!9¸ðBó
ð 	
r   )r   Útypingr   r   r   Ú torch.distributed.tensor._randomÚdistributedÚtensorÚ_randomr   Útorch.nnÚnnÚtorch.distributed.tensorr   r   r	   Ú(torch.distributed.tensor.parallel._utilsr
   Ú'torch.distributed.tensor.parallel.styler   Ú__all__ÚModuleÚstrr   © r   r   ú<module>rD      s~   ðå ß ã ß 1Ó 1Ý Ý /÷õ KÝ Að ð€ð
`
Ø�I‰Ið`
àð`
ð ˜M¨4°°]Ð0BÑ+CÐCÑDð`
ð ‡Y�Yô	`
r   