U
    Jc	                     @   s0   d dl Z d dlZd dlmZ G dd deZdS )    N)DistributedSamplerc                       s2   e Zd ZdZd
 fdd	Zdd Zdd	 Z  ZS )ElasticDistributedSamplera  
    Sampler that restricts data loading to a subset of
    the dataset for elastic training.

    It is especially useful in conjunction with
    :class:`torch.nn.parallel.DistributedDataParallel`. In such case, each
    process can pass a DistributedSampler instance as a DataLoader sampler,
    and load a subset of the original dataset that is exclusive to it.

    .. note::
        Dataset is assumed to be of constant size.

    Args:
        dataset: Dataset used for sampling.
        num_replicas (optional): Number of processes participating in
            distributed training.
        rank (optional): Rank of the current process within num_replicas.
        start_index (optional):  Which index of the dataset to start sampling from
    Nr   c                    sp   t  j|||d |t|kr2td|t||| _ttt	t| j
| j | j | _| j| j | _d S )N)datasetnum_replicasrankz2Start index {} should be less than dataset size {})super__init__len
ValueErrorformatstart_indexintmathceilfloatr   r   num_samples
total_size)selfr   r   r   r   	__class__ d/tmp/pip-unpacked-wheel-gikjz4vx/torch/distributed/elastic/utils/data/elastic_distributed_sampler.pyr   $   s     z"ElasticDistributedSampler.__init__c                 C   s   t  }|| j t jt| j| j |d| j	 }||d | j
t|  7 }t|| j
ksft|| j| j
| j }t|| jkstt|S )N)	generator)torch	GeneratorZmanual_seedepochZrandpermr	   r   r   addtolistr   AssertionErrorr   r   r   iter)r   gindicesr   r   r   __iter__3   s    z"ElasticDistributedSampler.__iter__c                 C   s   | j S )N)r   )r   r   r   r   __len__G   s    z!ElasticDistributedSampler.__len__)NNr   )__name__
__module____qualname____doc__r   r"   r#   __classcell__r   r   r   r   r      s   r   )r   r   Ztorch.utils.data.distributedr   r   r   r   r   r   <module>	   s   