U
    Jºca  ã                   @   sB   d dl mZmZ d dlmZ eeeeeeeef  dœdd„ZdS )é    )ÚListÚTuple)ÚShardMetadata)Ú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ÚzipZshard_offsetsZshard_sizesÚminÚmaxÚappend)r   r   ZnarrowsZdimZsaved_shard_offsetZcurrent_shard_offsetZsaved_shard_sizeZcurrent_shard_sizeZmin_range_endÚlengthZoffset_for_saved_tensorZoffset_for_current_tensor© r   úR/tmp/pip-unpacked-wheel-gikjz4vx/torch/distributed/_shard/checkpoint/resharding.pyÚ+_shards_get_overlap_region_wrt_saved_tensor   s8    üÿû
	þ

ÿr   N)Útypingr   r   Z&torch.distributed._shard.sharding_specr   Úintr   r   r   r   r   Ú<module>   s
    þ