
    RЦi                     X    S SK r S SKrS SKJr  S SKJr   " S S\5      r " S S\5      rg)    N)Samplerc                   .    \ rS rSrSrSS jrS rS rSrg)	OrderedDistributedSampler   aP  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                    Uc:  [         R                  " 5       (       d  [        S5      e[         R                  " 5       nUc:  [         R                  " 5       (       d  [        S5      e[         R                  " 5       nXl        X l        X0l        [        [        R                  " [        U R
                  5      S-  U R                  -  5      5      U l        U R                  U R                  -  U l        g )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       \/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/data/distributed_sampler.py__init__"OrderedDistributedSampler.__init__   s    $$&&"#QRR..0L<$$&&"#QRR==?D(	tyyT\\):S)@4CTCT)TUV**T->->>    c                 V   [        [        [        U R                  5      5      5      nXS U R                  [        U5      -
   -  n[        U5      U R                  :X  d   eXR
                  U R                  U R                  2   n[        U5      U R                  :X  d   e[        U5      $ N)	listranger   r   r   r   r   r   iter)r   indicess     r   __iter__"OrderedDistributedSampler.__iter__%   s    uS./0 	<T__s7|;==7|t... ))DOOD4E4EEF7|t/////G}r   c                     U R                   $ r   )r   r   s    r   __len__!OrderedDistributedSampler.__len__2   s    r   )r   r   r   r   r   )NN)	__name__
__module____qualname____firstlineno____doc__r   r"   r&   __static_attributes__ r   r   r   r      s    ? r   r   c                   @    \ rS rSrSr      S	S jrS rS rS rSr	g)
RepeatAugSampler6   a  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                 &   Uc:  [         R                  " 5       (       d  [        S5      e[         R                  " 5       nUc:  [         R                  " 5       (       d  [        S5      e[         R                  " 5       nXl        X l        X0l        X@l        XPl	        SU l
        [        [        R                  " [        U R
                  5      U-  U R                  -  5      5      U l        U R                  U R                  -  U l        U=(       d    UnU(       aA  [        [        R"                  " [        U R
                  5      U-  U-  U-  5      5      U l        g [        [        R                  " [        U R
                  5      U-  5      5      U l        g )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   r3   r4   selected_roundselected_ratios           r   r   RepeatAugSampler.__init__A   s1    $$&&"#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           
      L   [         R                  " 5       nUR                  U R                  5        U R                  (       a)  [         R
                  " [        U R                  5      US9nO([         R                  " S[        U R                  5      S9n[        U R                  [        5      (       a  U R                  R                  5       (       d  [        R                  " U R                  [        U R                  5      -  5      nU[         R                  " [!        U5       Vs/ s H  n[#        X@R                  -  5      PM     sn5         nO)[         R$                  " U[#        U R                  5      SS9nUR'                  5       nU R(                  [        U5      -
  nUS:  a  X"S U -  n[        U5      U R(                  :X  d   eX R*                  U R(                  U R,                  2   n[        U5      U R.                  :X  d   e[1        US U R2                   5      $ s  snf )N)	generatorr   )startend)repeatsdim)torch	Generatormanual_seedr5   r3   randpermr   r   arange
isinstancer4   float
is_integerr   r   tensorr   r   repeat_interleavetolistr   r   r   r   r    r7   )r   gr!   repeat_sizeipadding_sizes         r   r"   RepeatAugSampler.__iter__e   s   OO	djj!<<nnS%6!DGllDLL0ABG d&&..t7G7G7R7R7T7T))D$4$4s4<<7H$HIKellPUVaPb+cPb1C5E5E0E,FPb+cdeG--gs4CSCS?TZ[\G.."W5!}--G7|t... ))DOOD4E4EEF7|t///// G6T66788 ,ds   %!H!
c                     U R                   $ r   )r7   r%   s    r   r&   RepeatAugSampler.__len__   s    (((r   c                     Xl         g r   )r5   )r   r5   s     r   	set_epochRepeatAugSampler.set_epoch   s    
r   )	r   r5   r4   r   r   r7   r   r3   r   )NNT      r   )
r(   r)   r*   r+   r,   r   r"   r&   rT   r-   r.   r   r   r0   r0   6   s1     "[H9<)r   r0   )	r   rA   torch.utils.datar   torch.distributeddistributedr	   r   r0   r.   r   r   <module>r[      s,      $  ,  , ^Qw Qr   