U
    ¯Ê¹j¤  ã                   @   sL   d dl Z d dlZd dlmZ d dlmZ G dd„ deƒZG dd„ deƒZdS )é    N)ÚSamplerc                   @   s*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
ÚOrderedDistributedSamplera€  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.
    Nc                 C   s„   |d kr t  ¡ stdƒ‚t  ¡ }|d kr@t  ¡ s8tdƒ‚t  ¡ }|| _|| _|| _tt	 
t| jƒd | j ¡ƒ| _| j| j | _d S )Nú,Requires distributed package to be availableg      ð?)ÚdistÚis_availableÚRuntimeErrorÚget_world_sizeÚget_rankÚdatasetÚnum_replicasÚrankÚintÚmathÚceilÚlenÚnum_samplesÚ
total_size)Úselfr
   r   r   © r   ú\/var/www/html/bwave/python/venv/lib/python3.8/site-packages/timm/data/distributed_sampler.pyÚ__init__   s     z"OrderedDistributedSampler.__init__c                 C   sl   t tt| jƒƒƒ}||d | jt|ƒ … 7 }t|ƒ| jks>t‚|| j| j| j… }t|ƒ| jksdt‚t	|ƒS ©N)
ÚlistÚranger   r
   r   ÚAssertionErrorr   r   r   Úiter)r   Úindicesr   r   r   Ú__iter__%   s    z"OrderedDistributedSampler.__iter__c                 C   s   | j S r   )r   ©r   r   r   r   Ú__len__2   s    z!OrderedDistributedSampler.__len__)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r      s   
r   c                   @   s2   e Zd ZdZddd„Zd	d
„ Zdd„ Zdd„ ZdS )ÚRepeatAugSamplera»  Sampler that restricts data loading to a subset of the dataset for distributed,
    with repeated augmentation.
    It ensures that different each augmented version of a sample will be visible to a
    different process (GPU). Heavily based on torch.utils.data.DistributedSampler

    This sampler was taken from https://github.com/facebookresearch/deit/blob/0c4b8f60/samplers.py
    Used in
    Copyright (c) 2015-present, Facebook, Inc.
    NTé   é   r   c                 C   sà   |d kr t  ¡ stdƒ‚t  ¡ }|d kr@t  ¡ s8tdƒ‚t  ¡ }|| _|| _|| _|| _|| _	d| _
tt t| jƒ| | j ¡ƒ| _| j| j | _|p˜|}|rÂtt t| jƒ| | | ¡ƒ| _ntt t| jƒ| ¡ƒ| _d S )Nr   r   )r   r   r   r   r	   r
   r   r   ÚshuffleÚnum_repeatsÚepochr   r   r   r   r   r   ÚfloorÚnum_selected_samples)r   r
   r   r   r'   r(   Zselected_roundZselected_ratior   r   r   r   A   s,    
 ÿ
zRepeatAugSampler.__init__c                    s"  t  ¡ }| ˆ j¡ ˆ jr0t jtˆ jƒ|d�}nt jdtˆ jƒd�}t	ˆ j
tƒr’ˆ j
 ¡ s’t ˆ j
tˆ jƒ ¡}|t  ‡ fdd„t|ƒD ƒ¡ }nt j|tˆ j
ƒdd�}| ¡ }ˆ jt|ƒ }|dkrÖ||d |… 7 }t|ƒˆ jksèt‚|ˆ jˆ jˆ j… }t|ƒˆ jk�st‚t|d ˆ j… ƒS )N)Ú	generatorr   )ÚstartÚendc                    s   g | ]}t |ˆ j ƒ‘qS r   )r   r(   )Ú.0Úir   r   r   Ú
<listcomp>r   s     z-RepeatAugSampler.__iter__.<locals>.<listcomp>)ZrepeatsÚdim)ÚtorchÚ	GeneratorZmanual_seedr)   r'   Zrandpermr   r
   ZarangeÚ
isinstancer(   ÚfloatÚ
is_integerr   r   Ztensorr   Zrepeat_interleaver   Útolistr   r   r   r   r   r   r+   )r   Úgr   Zrepeat_sizeZpadding_sizer   r   r   r   e   s"    "zRepeatAugSampler.__iter__c                 C   s   | j S r   )r+   r   r   r   r   r   ƒ   s    zRepeatAugSampler.__len__c                 C   s
   || _ d S r   )r)   )r   r)   r   r   r   Ú	set_epoch†   s    zRepeatAugSampler.set_epoch)NNTr%   r&   r   )r    r!   r"   r#   r   r   r   r:   r   r   r   r   r$   6   s         ø
$r$   )	r   r3   Ztorch.utils.datar   Ztorch.distributedÚdistributedr   r   r$   r   r   r   r   Ú<module>   s
   /