o
    !‘i%	  ã                   @   sj   U d dl mZmZ d dlmZ g Zee ed< dedefdd„Zded	ed
eee	e	e	e	f  fdd„Z
dS )é    )ÚListÚTuple)ÚChunkStorageMetadataÚ__all__Úshard1Úshard2c                 C   sd   t | jƒ}t|ƒD ]&}| 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   úv/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/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 ]2\}\}}}}t|| || ƒ}|t||ƒ }	||kr2d}
|| }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    ÿÿþ