U
    d|                     @   sL   d dl mZ d dlmZmZ d dlmZ d dlmZ eG dd de	Z
dS )    )	dataclass)ListUnion)reduce)_remote_devicec                   @   sT   e Zd ZU dZdddgZee ed< ee ed< ee	e
f ed< dd Zdd Zd	S )
ShardMetadataaH  
    Represents a shard of the overall Tensor including its
    offsets, lengths and device placement.

    Args:
        shard_offsets(List[int]): Offsets in the original tensor indicating
            the start offsets for this shard. Should have the same rank as
            the original tensor.
        shard_sizes(List[int]): Integers indicating the size of each
            dimension for this shard. Should have the same rank as the
            original tensor.
        placement(:class:`torch.distributed._remote_device`):
            Specifies the placement of this shard.
    shard_offsetsshard_sizes	placementc                 C   s4   dd }t || jd}t || j|}||| j}|S )Nc                 S   s   | d> t | S )N   )hash)ab r   E/tmp/pip-unpacked-wheel-ua33x9lu/torch/distributed/_shard/metadata.py_hash_reduce   s    z,ShardMetadata.__hash__.<locals>._hash_reduce%   )r   r   r	   r
   )selfr   resr   r   r   __hash__   s
    zShardMetadata.__hash__c                 C   s   t | jtrt| j| _t| jt| jkrJtdt| j d| j dtt| jD ]0}| j| dk rrtd| j| dk rXtdqXd S )NzMshard_offsets and shard_sizes should have the same number of elements, found z and z respectivelyr   zshard_offsets should be >=0zshard_sizes should be >= 0)	
isinstancer
   strr   lenr   r	   
ValueErrorrange)r   ir   r   r   __post_init__'   s    zShardMetadata.__post_init__N)__name__
__module____qualname____doc__	__slots__r   int__annotations__r   r   r   r   r   r   r   r   r   r      s   

	r   N)Zdataclassesr   typingr   r   	functoolsr   Ztorch.distributed.remote_devicer   objectr   r   r   r   r   <module>   s
   