o
    TÃi‚  ã                   @   s6   ddd„Z ddd„Zddd„Zddd„Zdd	d
„Zd S )Nc                 C   ó8   | du rdS t | dƒr|  ¡ S t | dƒr|  ¡ S |  ¡ S )a>  Backwards-compatible way of querying the tensor model parallel rank from
    an ``mpu`` object.

    *Tensor* model parallelism means that tensors are physically split across
    processes. This contrasts with *pipeline* model parallelism, in which the
    layers are partitioned but tensors left intact.

    The API for tensor model parallelism has changed across versions and this
    helper provides a best-effort implementation across versions of ``mpu``
    objects.  The preferred mechanism is
    ``mpu.get_tensor_model_parallel_rank()``.

    This should "just work" with both Megatron-LM and DeepSpeed's pipeline
    parallelism.

    Args:
        mpu (model parallel unit, optional): The tensor model parallel rank.
            If ``mpu=None``, returns 0. Defaults to ``None``.

    Returns:
        int: the rank
    Né    Úget_tensor_model_parallel_rankÚget_slice_parallel_rank)Úhasattrr   r   Úget_model_parallel_rank©Úmpu© r	   úG/home/ubuntu/.local/lib/python3.10/site-packages/deepspeed/utils/bwc.pyÚbwc_tensor_model_parallel_rank   s   

r   c                 C   r   )zBackwards-compatible way of querying the tensor model parallel world size.
       Similar to bwc_tensor_model_parallel_rank.
    Né   Ú$get_tensor_model_parallel_world_sizeÚget_slice_parallel_world_size)r   r   r   Úget_model_parallel_world_sizer   r	   r	   r
   Ú$bwc_tensor_model_parallel_world_size-   ó   

r   c                 C   s8   | du rdS t | dƒr|  ¡ S t | dƒr|  ¡ S |  ¡ S )z|Backwards-compatible way of querying the tensor model parallel group.
       Similar to bwc_tensor_model_parallel_rank.
    NÚget_tensor_model_parallel_groupÚget_slice_parallel_group)r   r   r   Úget_model_parallel_groupr   r	   r	   r
   Úbwc_tensor_model_parallel_group?   r   r   c                 C   s8   d}| durt | dƒr|  ¡ }|S t | dƒr|  ¡ }|S )zFBackwards-compatible way of querying the pipeline parallel world size.r   NÚ&get_pipeline_model_parallel_world_sizeÚget_pipe_parallel_world_size)r   r   r   )r   Ú
world_sizer	   r	   r
   Ú bwc_pipeline_parallel_world_sizeQ   s   

ýr   c                 C   s8   | du rdS t | dƒr|  ¡ S t | dƒr|  ¡ S J dƒ‚)zABackwards-compatible way of querying the pipeline parallel group.NÚ!get_pipeline_model_parallel_groupÚget_pipe_parallel_groupFz,mpu does not support pipeline parallel group)r   r   r   r   r	   r	   r
   Úbwc_pipeline_parallel_group^   s   

r   )N)r   r   r   r   r   r	   r	   r	   r
   Ú<module>   s
   

&

