/usr/local/lib64/python3.6/site-packages/torch/utils/data/__pycache__
Edit: /usr/local/lib64/python3.6/site-packages/torch/utils/data/__pycache__/distributed.cpython-36.pyc (4851B)
3
Eg9 @ sb d dl Z d dlmZmZmZ d dlZddlmZmZ d dl j
ZedddZG dd d ee Z
dS )
N)TypeVarOptionalIterator )SamplerDatasetT_coT) covariantc @ sd e Zd ZdZdeee ee eeeddddZe e
d d
dZed dd
ZeddddZ
dS )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 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.
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)datasetnum_replicasrankshuffleseed drop_lastreturnc C s |d kr t j stdt j }|d kr@t j s8tdt j }||ksP|dk rdtdj||d || _|| _|| _ d| _
|| _| jrt| j| j dkrt
jt| j| j | j | _nt
jt| j| j | _| j| j | _|| _|| _d S )Nz,Requires distributed package to be availabler z7Invalid rank {}, rank should be in the interval [0, {}]r )distZis_availableRuntimeErrorZget_world_sizeZget_rank
ValueErrorformatr r r
epochr lenmathceilnum_samples
total_sizer r )selfr r r
r r r r H/usr/local/lib64/python3.6/site-packages/torch/utils/data/distributed.py__init__; s0 zDistributedSampler.__init__)r c C s | j r:tj }|j| j| j tjt| j|dj }nt
tt| j}| js| j
t| }|t|kr~||d | 7 }q||tj|t| d | 7 }n|d | j
}t|| j
kst|| j| j
| j }t|| jkstt|S )N) generator)r torch GeneratorZmanual_seedr r Zrandpermr r tolistlistranger r r r AssertionErrorr
r r iter)r gindicesZpadding_sizer r r __iter__^ s $zDistributedSampler.__iter__c C s | j S )N)r )r r r r __len__y s zDistributedSampler.__len__)r r c C s
|| _ dS )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 zDistributedSampler.set_epoch)NNTr F)__name__
__module____qualname____doc__r r intboolr r r r* r+ r, r r r r r
s - $!r
)r typingr r r r! r r Ztorch.distributedZdistributedr r r
r r r r
s