3
I4W^M	  ã               @   s:   d dl Z d dlZddlmZ d dljZG dd„ deƒZdS )é    Né   )ÚSamplerc               @   s2   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ ZdS )ÚDistributedSampleraÓ  Sampler that restricts data loading to a subset of the dataset.

    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.

    Arguments:
        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.
        shuffle (optional): If true (default), sampler will shuffle the indices
    NTc             C   s�   |d kr t jƒ stdƒ‚t jƒ }|d kr@t jƒ s8tdƒ‚t jƒ }|| _|| _|| _d| _t	t
jt| jƒd | j ƒƒ| _| j| j | _|| _d S )Nz,Requires distributed package to be availabler   g      ð?)ÚdistZis_availableÚRuntimeErrorZget_world_sizeZget_rankÚdatasetÚnum_replicasÚrankÚepochÚintÚmathÚceilÚlenÚnum_samplesÚ
total_sizeÚshuffle)Úselfr   r   r	   r   © r   ú=/tmp/pip-build-e66j97b2/torch/torch/utils/data/distributed.pyÚ__init__   s     zDistributedSampler.__init__c             C   s    t jƒ }|j| jƒ | jr4t jt| jƒ|d�jƒ }nt	t
t| jƒƒƒ}||d | jt|ƒ … 7 }t|ƒ| jksrt‚|| j| j| j… }t|ƒ| jks˜t‚t|ƒS )N)Ú	generator)ÚtorchÚ	GeneratorZmanual_seedr
   r   Zrandpermr   r   ÚtolistÚlistÚranger   ÚAssertionErrorr	   r   r   Úiter)r   ÚgÚindicesr   r   r   Ú__iter__+   s    zDistributedSampler.__iter__c             C   s   | j S )N)r   )r   r   r   r   Ú__len__?   s    zDistributedSampler.__len__c             C   s
   || _ d S )N)r
   )r   r
   r   r   r   Ú	set_epochB   s    zDistributedSampler.set_epoch)NNT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r!   r"   r   r   r   r   r      s
   
r   )r   r   Ú r   Ztorch.distributedZdistributedr   r   r   r   r   r   Ú<module>   s   
