U
    Å9%e%	  ã                   @   sd   U d dl mZmZ d dlmZ g Zee ed< eedœdd„Zeeeee	e	e	e	f  dœdd	„Z
d
S )é    )ÚListÚTuple)ÚChunkStorageMetadataÚ__all__)Úshard1Úshard2c                 C   sd   t | jƒ}t|ƒD ]L}| j| |j| |j|  kr: dS |j| | j| | j|  kr dS qdS )z'
    Checks if two shards overlap.
    FT)ÚlenÚoffsetsÚrangeÚsizes)r   r   ZndimsÚi© r   úf/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/distributed/checkpoint/resharding.pyÚ"_check_shard_metadata_pair_overlap	   s    	
r   )Úsaved_shardÚcurrent_shardÚreturnc                 C   sˆ   g }t t| j|j| j|jƒƒD ]d\}\}}}}t|| || ƒ}|t||ƒ }	||krdd}
|| }n|| }
d}| ||
||	f¡ q|S )aZ  
    Return the overlapping region between saved_shard and current_shard.
    There returned list has the same number of elements as the tensor's dimension.
    For each element, we produce a tuple with the following contents:
        (dimension, `saved_shard` offset, `current_shard` offset, length)

    Offsets are relative to each shard.
    r   )Ú	enumerateÚzipr	   r   ÚminÚmaxÚappend)r   r   ZnarrowsÚdimZsaved_shard_offsetZcurrent_shard_offsetZsaved_shard_sizeZcurrent_shard_sizeZmin_range_endÚlengthZoffset_for_saved_tensorZoffset_for_current_tensorr   r   r   Ú+_shards_get_overlap_region_wrt_saved_tensor   s:    üÿû
	þÿ
ÿr   N)Útypingr   r   Z%torch.distributed.checkpoint.metadatar   r   ÚstrÚ__annotations__r   Úintr   r   r   r   r   Ú<module>   s     þ