§
    O�’jÙ  ã                   ó´   — d dl Z d dlmZ d dlZd dlmZ d dlmZmZ d dlm	Z	 dgZ
	 	 dd dœdej        dedz  d	e	eee	f         z  dz  d
edz  dej        f
d„ZdS )é    N©Úfnmatch)Ú_mesh_resourcesÚ
DeviceMesh)ÚParallelStyleÚparallelize_module©Úsrc_data_rankÚmoduleÚdevice_meshÚparallelize_planr
   Úreturnc          
      óv  ‡— t           j                             d¦  «         |pt          j        ¦   «         }|€t          j        dd¬¦  «         | S t          |t          ¦  «        r||_	        | 
                    | |¦  «        S t          |t          ¦  «        �r|                     ¦   «         D ]ì\  }}|dk    rt          | ||¦  «         Œ|                     d¦  «        }|d         Št          t!          ˆfd	„|                      ¦   «         ¦  «        ¦  «        }|s!t          j        d
|› d‰› d| › d�d¬¦  «         Œ�|                     d¦  «         |D ]E\  }}	|r+d                     |¦  «        }
t          |	||
|i|¬¦  «         Œ2t          |	|||¬¦  «         ŒFŒí| S t)          dt+          |¦  «        › 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`, optional):
            Object which describes the mesh topology of devices for the DTensor.
            If not specified, the call must be under a DeviceMesh context.
        parallelize_plan (Union[:class:`ParallelStyle`, Dict[str, :class:`ParallelStyle`]], optional):
            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. If not
            specified, the call will do nothing at the moment.
    Keyword args:
        src_data_rank (int, optional): the rank of the source data for the logical/global tensor, it is used by
            :meth:`distribute_tensor` to scatter/broadcast the shards/replicas to other ranks. By default,
            we use ``group_rank=0`` on each DeviceMesh dimension as the source data to preserve the single-device
            semantic. If passing ``None`` explicitly, :meth:`parallelize_module` simply uses its local data instead
            of trying to preserve the single-device semantic via scatter/broadcast. Default: 0
    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 achieve the desired sharding computation.
    z4torch.distributed.tensor.parallel.parallelize_moduleNz‚No parallelize_plan is provided and auto-parallel is not supported at the moment, so this parallelize_module call will do nothing.é   )Ú
stacklevelÚ ú.r   c                 ó0   •— t          | d         ‰¦  «        S )Nr   r   )ÚtÚtokens    €úSC:\Projects\DSCons\.venv\Lib\site-packages\torch\distributed\tensor\parallel\api.pyú<lambda>z$parallelize_module.<locals>.<lambda>f   s   ø€ �g a¨¤d¨EÑ2Ô2€ ó    zParallelize plan key 'z6' could not be resolved: no submodule matching token 'z' in module z, skipping this plan entry.r	   zLExpect Union[ParallelStyle, Dict[str, ParallelStyle]] for parallelize_plan, z found!)ÚtorchÚ_CÚ_log_api_usage_oncer   Úget_current_meshÚwarningsÚwarnÚ
isinstancer   r
   Ú_applyÚdictÚitemsr   ÚsplitÚlistÚfilterÚnamed_childrenÚpopÚjoinÚ	TypeErrorÚtype)r   r   r   r
   Úmodule_pathÚparallelize_styleÚpath_splitsÚmatched_childrenÚ_Ú	submoduleÚ	leaf_pathr   s              @r   r   r      su  ø€ õn 
„H× Ò Ð!WÑXÔXÐXàÐC¥Ô!AÑ!CÔ!C€KàÐÝŒðNàð	
ñ 	
ô 	
ð 	
ð
 ˆõ
 Ð"¥MÑ2Ô2ð :
Ø)6ÐÔ&Ø×&Ò& v¨{Ñ;Ô;Ð;Ý	Ð$¥dÑ	+Ô	+ñ 7
Ø.>×.DÒ.DÑ.FÔ.Fð 0	ð 0	Ñ*ˆKÐ*Ø˜bÒ Ð å" 6¨;Ð8IÑJÔJÐJØà%×+Ò+¨CÑ0Ô0ˆKð   ”NˆEå#Ýà2Ð2Ð2Ð2Ø×)Ò)Ñ+Ô+ñô ñ ô  Ðð $ð å”ð1¨[ð 1ð 1Ø49ð1ð 1ØGMð1ð 1ð 1ð  !ð	ñ ô ð ð ð �OŠO˜AÑÔÐà 0ð ð ‘��9Øð à #§¢¨Ñ 5Ô 5�IÝ&Ø!Ø#Ø"Ð$5Ð6Ø&3ð	ñ ô ð ð õ 'Ø!Ø#Ø)Ø&3ð	ñ ô ð ð ðð$ ˆåðBÝ"&Ð'7Ñ"8Ô"8ðBð Bð Bñ
ô 
ð 	
r   )NN)r   r   r   Útorch.nnÚnnÚtorch.distributed.device_meshr   r   Ú'torch.distributed.tensor.parallel.styler   Ú__all__ÚModuler"   ÚstrÚintr   © r   r   ú<module>r<      s  ðà €€€Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø EÐ EÐ EÐ EÐ EÐ EÐ EÐ EØ AÐ AÐ AÐ AÐ AÐ Að  Ð
 €ð
 &*ØHLð@
ð
 !"ð@
ð @
ð @
ØŒIð@
à˜dÑ"ð@
ð $ d¨3°Ð+=Ô&>Ñ>ÀÑEð@
ð
 ˜‘:ð@
ð „Yð@
ð @
ð @
ð @
ð @
ð @
r   