a
    dg                     @   s0   d dl Z d dlZd dlm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 )EnlargedSampleraX  Sampler that restricts data loading to a subset of the dataset.

    Modified from torch.utils.data.distributed.DistributedSampler
    Support enlarging the dataset for iteration-based training, for saving
    time when restart the dataloader after each epoch

    Args:
        dataset (torch.utils.data.Dataset): Dataset used for sampling.
        num_replicas (int | None): Number of processes participating in
            the training. It is usually the world_size.
        rank (int | None): Rank of the current process within num_replicas.
        ratio (int): Enlarging ratio. Default: 1.
       c                 C   sF   || _ || _|| _d| _tt| j | | j | _| j| j | _d S )Nr   )	datasetnum_replicasrankepochmathceillennum_samples
total_size)selfr   r   r   Zratio r   b/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/basicsr/data/data_sampler.py__init__   s    zEnlargedSampler.__init__c                    sr   t  }|| j t j| j|d }t| j  fdd|D }|| j	| j| j
 }t|| jksjJ t|S )N)	generatorc                    s   g | ]}|  qS r   r   ).0vZdataset_sizer   r   
<listcomp>$       z,EnlargedSampler.__iter__.<locals>.<listcomp>)torch	GeneratorZmanual_seedr   Zrandpermr   tolistr   r   r   r   r   iter)r   gindicesr   r   r   __iter__   s    
zEnlargedSampler.__iter__c                 C   s   | j S N)r   )r   r   r   r   __len__,   s    zEnlargedSampler.__len__c                 C   s
   || _ d S r   )r   )r   r   r   r   r   	set_epoch/   s    zEnlargedSampler.set_epochN)r   )__name__
__module____qualname____doc__r   r   r    r!   r   r   r   r   r      s
   
r   )r	   r   Ztorch.utils.data.samplerr   r   r   r   r   r   <module>   s   