U
    Jc+	                     @   sP   d dl mZ d dlmZ d dlZd dlmZ d dlmZ eG dd de	Z
dS )    )	dataclass)ListN)ShardMetadata)_remote_devicec                   @   sP   e Zd ZU dZddgZejed< eed< dd Z	e
ejee edddZd	S )
Shardae  
    Container which holds the data for a shard as a Tensor and also
    the associated metadata for that shard.

    Args:
        tensor(torch.Tensor): Local tensor for the shard.
        metadata(:class `torch.distributed._shard.sharded_tensor.ShardMetadata`):
            The metadata for the shard, including offsets, lengths and device placement.
    tensormetadatac                 C   sz   t | j | jjkr:tdt | j  d| jj d| jj}|d k	rv| | jjkrvtd| jj d|  d S )NzWShard tensor size does not match with metadata.shard_lengths! Found shard tensor size: z, metadata.shard_lengths: z, zhLocal shard tensor device does not match with local Shard's placement! Found local shard tensor device: z), local shard metadata placement device: )listr   sizer   shard_sizes
ValueError	placementdevice)selfZplacement_device r   Q/tmp/pip-unpacked-wheel-gikjz4vx/torch/distributed/_shard/sharded_tensor/shard.py__post_init__   s    zShard.__post_init__)r   shard_offsetsrankc                 C   s>   t | }td| dt|j }t|||d}t||S )ae  
        Creates a Shard of a ShardedTensor from a local torch.Tensor, shard_offsets and rank.

        Args:
            tensor(torch.Tensor): Local tensor for the shard.
            shard_offsets(List[int]): List of integers specify the offset
                of the shard on each dimension.
            rank(int): Specify the rank for the shard.
        zrank:/)r   r   r   )r	   r
   r   strr   r   r   )clsr   r   r   r   r   Z
shard_metar   r   r   from_tensor_and_offsets(   s    zShard.from_tensor_and_offsetsN)__name__
__module____qualname____doc__	__slots__torchZTensor__annotations__r   r   classmethodr   intr   r   r   r   r   r   	   s   
	
r   )Zdataclassesr   typingr   r   Z!torch.distributed._shard.metadatar   Ztorch.distributed.remote_devicer   objectr   r   r   r   r   <module>   s   