
    kh                     X    d dl Z d dlZd dlmZ d dlmZ  G d de      Z G d de      Zy)    N)Samplerc                   $    e Zd ZdZddZd Zd Zy)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                    |3t        j                         st        d      t        j                         }|3t        j                         st        d      t        j                         }|| _        || _        || _        t        t        j                  t        | j
                        dz  | j                  z              | _        | j                  | j                  z  | _        y )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   s       Y/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/data/distributed_sampler.py__init__z"OrderedDistributedSampler.__init__   s    $$&"#QRR..0L<$$&"#QRR==?D(	tyyT\\):S)@4CTCT)TUV**T->->>    c                 V   t        t        t        | j                                    }||d | j                  t        |      z
   z  }t        |      | j                  k(  sJ || j
                  | j                  | j                     }t        |      | j                  k(  sJ t        |      S N)	listranger   r   r   r   r   r   iter)r   indicess     r   __iter__z"OrderedDistributedSampler.__iter__%   s    uS./0 	7<T__s7|;==7|t... $))DOOD4E4EEF7|t/////G}r   c                     | j                   S r   )r   r   s    r   __len__z!OrderedDistributedSampler.__len__2   s    r   )NN)__name__
__module____qualname____doc__r   r    r#    r   r   r   r      s    ? r   r   c                   6    e Zd ZdZ	 	 	 	 	 	 ddZd Zd Zd Zy)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.
    Nc                    |3t        j                         st        d      t        j                         }|3t        j                         st        d      t        j                         }|| _        || _        || _        || _        || _	        d| _
        t        t        j                  t        | j
                        |z  | j                  z              | _        | j                  | j                  z  | _        |xs |}|r@t        t        j"                  t        | j
                        |z  |z  |z              | _        y t        t        j                  t        | j
                        |z              | _        y )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-   selected_roundselected_ratios           r   r   zRepeatAugSampler.__init__A   s5    $$&"#QRR..0L<$$&"#QRR==?D(	&
tyyT\\):[)H4K\K\)\]^**T->->> (7<(+DJJT\\"n4~EV-X )YD% ),DIIc$,,6G.6X,Y(ZD%r   c           
      .   t        j                         }|j                  | j                         | j                  r+t        j
                  t        | j                        |      }n*t        j                  dt        | j                              }t        | j                  t              r| j                  j                         s{t        j                  | j                  t        | j                        z        }|t        j                  t!        |      D cg c]  }t#        || j                  z         c}         }n+t        j$                  |t#        | j                        d      }|j'                         }| j(                  t        |      z
  }|dkD  r||d | z  }t        |      | j(                  k(  sJ || j*                  | j(                  | j,                     }t        |      | j.                  k(  sJ t1        |d | j2                         S c c}w )N)	generatorr   )startend)repeatsdim)torch	Generatormanual_seedr.   r,   randpermr   r   arange
isinstancer-   float
is_integerr   r   tensorr   r   repeat_interleavetolistr   r   r   r   r   r0   )r   gr   repeat_sizeipadding_sizes         r   r    zRepeatAugSampler.__iter__e   s   OO	djj!<<nnS%6!DGllDLL0ABG d&&.t7G7G7R7R7T))D$4$4s4<<7H$HIKellPUVaPb+c1CT5E5E0E,F+cdeG--gs4CSCS?TZ[\G.."W5!w}--G7|t... $))DOOD4E4EEF7|t///// G6T66788 ,ds   H
c                     | j                   S r   )r0   r"   s    r   r#   zRepeatAugSampler.__len__   s    (((r   c                     || _         y r   )r.   )r   r.   s     r   	set_epochzRepeatAugSampler.set_epoch   s	    
r   )NNT      r   )r$   r%   r&   r'   r   r    r#   rJ   r(   r   r   r*   r*   6   s1     "[H9<)r   r*   )	r   r9   torch.utils.datar   torch.distributeddistributedr   r   r*   r(   r   r   <module>rP      s,      $  ,  , ^Qw Qr   