
    Ph                         d dl mZmZmZmZ d dlZd dlmZ d dlm	Z	m
Z
mZmZ d dlmZ dededee
   d	eed
f   fdZdededee
   d	eeed
f   eed
f   f   fdZdej$                  dedee
   d	eee   ee   f   fdZy)    )castListSequenceTupleN)	ShapeType)_Partial	Placement	ReplicateShard)
DeviceMeshglobal_shapemesh
placementsreturn.c                 v   |j                         }|yt        |       }t        |       }t        |      D ]x  \  }}|j	                  |      }t        |t              s(|j                  }	|	|k  sJ d|	 d|        |j                  ||	   |||         \  }
}t        |
t              sJ |
||	<   z t        |      S )zl
    Compute the shape of a local shard of the given DTensor on its current
    coordinate of the mesh.
    )r   Sharding dim  greater than tensor ndim )get_coordinatelistlen	enumeratesize
isinstancer   dim_local_shard_size_on_diminttuple)r   r   r   my_coordinatelocal_shapendimidx	placementmesh_dim_size	shard_dimlocal_shard_size_s               kC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/_tensor/_utils.pycompute_local_shaper(      s     '')M<(< '
3NC IIcNM)U+%MM	$O"9+-GvNO$&/&H&H	*M=;M'# ! ""2C888)9I& 4 [!!    c                    |j                         }|yt        |       }dgt        |       z  }t        |      D ]  \  }}|j	                  |      }t        |t              s(|j                  }	dgt        |       z  }
|	t        |      k  sJ d|	 dt        |              |j                  ||	   |||   d      \  }}|||	<   ||
|	<   ||	   |
|	   k  r	|
|	   ||	<   ||	xx   |
|	   z  cc<    t        |      t        |      fS )a0  
    Compute the local tensor shape and the global offsets into the original tensor
    of a DTensor on its current global rank. This is useful for checkpointing purpose.

    Example (2 host with 4GPUs each):
    # Below is a DeviceMesh with mesh_shape of (2, 4)
    mesh = DeviceMesh(device_type="cuda",
                        mesh=[
                        [0, 1, 2, 3],
                        [4, 5, 6, 7]
                        ],
    )

    Let's say we distribute a global_tensor of shape (8,4) over the above DeviceMesh
    with a placements of [Shard(0), Shard(0)].
    The local shape and global offset will be as follows:
    rank0 -- local_shape:[1, 4], global_offset:[0, 0]
    rank1 -- local_shape:[1, 4], global_offset:[1, 0]
    rank2 -- local_shape:[1, 4], global_offset:[2, 0]
    rank5 -- local_shape:[1, 4], global_offset:[5, 0]
    rank3 -- local_shape:[1, 4], global_offset:[3, 0]
    rank4 -- local_shape:[1, 4], global_offset:[4, 0]
    rank6 -- local_shape:[1, 4], global_offset:[6, 0]
    rank7 -- local_shape:[1, 4], global_offset:[7, 0]
    ) r+   r   r   r   T)return_offset)
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   global_offsetr!   r"   r#   r$   local_offset
shard_sizeshard_offsets                r'   %compute_local_shape_and_global_offsetr1   .   sL   8 '')M<(c,//'
3NC IIcNM)U+%MM	 !sS%66 3$  ["9+-GKHXGYZ[  ,5+M+M	*!!#&"&	 ,N ,(
L *4I&*6Y' !+|I/FF/;I/FM),!),Y0GG,3 46 [!5#777r)   tensorc           	         t        | j                               }t        | j                               }t        |      D ]  \  }}|j                  |      }|j	                         rt        t        |      }|j                  dk  rt        d|       |j                  }	|	| j                  k  sJ d|	 d| j                   d| d       ||	   }
|
|z  ||	<   t        t        |            D ]  }||	k7  s	||   ||	   k\  s||   |z  ||<   ! t        |t        t        f      rt        dt!        |       d       ||fS )	aV  
    Compute the global size and stride of a DTensor from the given local tensor.
    The local size is multiplited by `world_size` per Sharding dim.
    The local stride is multiplited by `world_size` per Sharding dim, as long as the
    dimension is outside sharding dim.

    For example, if we have a local tensor with size (4, 8, 2) and stride (16, 1, 8).
    If the DTensor placements are [Shard(2)] and world_size is 2;
    then the global size is (4, 8, 4) and stride is (16 * 2, 1, 8).

    Args:
        tensor (:class:`torch.Tensor`):
            Local tensor which DTensor will be constructed from.
        mesh (:class:`DeviceMesh`):
            Object which describes the mesh topology
            of devices for the DTensor.
        placements (Sequence[:class:`Placement`]]):
            The attribute of the DTensor that describes its layout
            on the mesh topology.

    Return:
        tensor_shape: A List of int which specifies the size of DTensor which build
            on top of the local tensor.
        tensor_stride: A List of int which specifies the stride of DTensor.
    r   zOShard placements should have negative dims normalized in the user-facing APIs: r   r   z for placement number .zplacement type z not supported!)r   r   strider   is_shardr   r   r   AssertionErrorr    ranger   r   r
   r   RuntimeErrortype)r2   r   r   tensor_shapetensor_strider!   r"   r#   shard_placementr$   local_dim_sizeis               r'   compute_global_tensor_infor@   q   sf   8 &L)M#J/Y		#"5)4O""Q&$--<,=?  (++I FKK'nyk)CFKK=Pfgjfkklmn' *)4N&4}&DL# 3}-.	>mA&6-	:R&R'4Q'7-'GM!$ / I	8'<=i0AQRR3 04 &&r)   )typingr   r   r   r   torchtorch._prims_commonr   )torch.distributed._tensor.placement_typesr   r	   r
   r   torch.distributed.device_meshr   r   r(   r1   Tensorr@   r+   r)   r'   <module>rG      s    . .  )  5""#-";CI;N"
38_">@8@8#-@8;CI;N@8
5c?E#s(O+,@8F8'LL8' *8'8@8K8'
49d3i 8'r)   