a
    7Sic5                     @   sj   d dl Z d dlmZmZmZ d dlZddlmZmZ d dl	m
Z dgZedddZG d	d dee ZdS )
    N)TypeVarOptionalIterator   )SamplerDatasetDistributedSamplerT_coT)	covariantc                	   @   sd   e Zd ZdZdeee ee eeeddddZe	e
 d	d
dZed	ddZeddddZdS )r   a		  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 a case, each
    process can pass a :class:`~torch.utils.data.DistributedSampler` instance as a
    :class:`~torch.utils.data.DataLoader` sampler, and load a subset of the
    original dataset that is exclusive to it.

    .. note::
        Dataset is assumed to be of constant size and that any instance of it always
        returns the same elements in the same order.

    Args:
        dataset: Dataset used for sampling.
        num_replicas (int, optional): Number of processes participating in
            distributed training. By default, :attr:`world_size` is retrieved from the
            current distributed group.
        rank (int, optional): Rank of the current process within :attr:`num_replicas`.
            By default, :attr:`rank` is retrieved from the current distributed
            group.
        shuffle (bool, optional): If ``True`` (default), sampler will shuffle the
            indices.
        seed (int, optional): random seed used to shuffle the sampler if
            :attr:`shuffle=True`. This number should be identical across all
            processes in the distributed group. Default: ``0``.
        drop_last (bool, optional): if ``True``, then the sampler will drop the
            tail of the data to make it evenly divisible across the number of
            replicas. If ``False``, the sampler will add extra indices to make
            the data evenly divisible across the replicas. Default: ``False``.

    .. warning::
        In distributed mode, calling the :meth:`set_epoch` method at
        the beginning of each epoch **before** creating the :class:`DataLoader` iterator
        is necessary to make shuffling work properly across multiple epochs. Otherwise,
        the same ordering will be always used.

    Example::

        >>> sampler = DistributedSampler(dataset) if is_distributed else None
        >>> loader = DataLoader(dataset, shuffle=(sampler is None),
        ...                     sampler=sampler)
        >>> for epoch in range(start_epoch, n_epochs):
        ...     if is_distributed:
        ...         sampler.set_epoch(epoch)
        ...     train(loader)
    NTr   F)datasetnum_replicasrankshuffleseed	drop_lastreturnc                 C   s   |d u r t  stdt  }|d u r@t  s8tdt  }||ksP|dk rdtd||d || _|| _|| _	d| _
|| _| jrt| j| j dkrtt| j| j | j | _ntt| j| j | _| j| j | _|| _|| _d S )Nz,Requires distributed package to be availabler   z7Invalid rank {}, rank should be in the interval [0, {}]r   )distis_availableRuntimeErrorget_world_sizeget_rank
ValueErrorformatr   r   r   epochr   lenmathceilnum_samples
total_sizer   r   )selfr   r   r   r   r   r    r    X/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/utils/data/distributed.py__init__=   s6    zDistributedSampler.__init__)r   c                 C   s   | j r:t }|| j| j  tjt| j|d	 }nt
tt| j}| js| jt| }|t|kr~||d | 7 }q||t|t|  d | 7 }n|d | j }t|| jksJ || j| j| j }t|| jksJ t|S )N)	generator)r   torch	Generatormanual_seedr   r   randpermr   r   tolistlistranger   r   r   r   r   r   r   iter)r   gindicesZpadding_sizer    r    r!   __iter__`   s    $zDistributedSampler.__iter__c                 C   s   | j S )N)r   )r   r    r    r!   __len__{   s    zDistributedSampler.__len__)r   r   c                 C   s
   || _ dS )a)  
        Sets the epoch for this sampler. When :attr:`shuffle=True`, this ensures all replicas
        use a different random ordering for each epoch. Otherwise, the next iteration of this
        sampler will yield the same ordering.

        Args:
            epoch (int): Epoch number.
        N)r   )r   r   r    r    r!   	set_epoch~   s    	zDistributedSampler.set_epoch)NNTr   F)__name__
__module____qualname____doc__r   r   intboolr"   r   r	   r.   r/   r0   r    r    r    r!   r      s   /   
#)r   typingr   r   r   r$    r   r   Ztorch.distributeddistributedr   __all__r	   r   r    r    r    r!   <module>   s   