
    ڧgd                         d dl Z d dlmZmZmZmZmZmZ d dlZd dl	m
Z d dlmZ d dlmZ  G d de          Z G d de          Z G d	 d
e          ZdS )    N)castIteratorListOptionalSizedUnion)Sampler)
VideoClipsc                       e Zd ZdZ	 	 	 	 ddedee         dee         ded	ed
dfdZd
e	e         fdZ
d
efdZded
dfdZdS )DistributedSamplera  
    Extension of DistributedSampler, as discussed in
    https://github.com/pytorch/pytorch/issues/23430

    Example:
        dataset: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
        num_replicas: 4
        shuffle: False

    when group_size = 1
            RANK    |  shard_dataset
            =========================
            rank_0  |  [0, 4, 8, 12]
            rank_1  |  [1, 5, 9, 13]
            rank_2  |  [2, 6, 10, 0]
            rank_3  |  [3, 7, 11, 1]

    when group_size = 2

            RANK    |  shard_dataset
            =========================
            rank_0  |  [0, 1, 8, 9]
            rank_1  |  [2, 3, 10, 11]
            rank_2  |  [4, 5, 12, 13]
            rank_3  |  [6, 7, 0, 1]

    NF   datasetnum_replicasrankshuffle
group_sizereturnc                 r   |5t          j                    st          d          t          j                    }|5t          j                    st          d          t          j                    }t          |          |z  dk    r"t          dt          |           d|           || _        || _        || _	        || _
        d| _        t          |          |z  }t          t          j        |dz  | j	        z                      | _        | j        |z  | _        | j        | j	        z  | _        || _        d S )Nz,Requires distributed package to be availabler   zBdataset length must be a multiplier of group size dataset length: z, group size: g      ?)distis_availableRuntimeErrorget_world_sizeget_ranklen
ValueErrorr   r   r   r   epochintmathceilnum_group_samplesnum_samples
total_sizer   )selfr   r   r   r   r   dataset_group_lengths          f/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/datasets/samplers/clip_sampler.py__init__zDistributedSampler.__init__'   s<    $&& S"#QRRR.00L<$&& S"#QRRR=??Dw<<*$))}UXY`UaUa}}q{}}   $(	
"7||z9!$TY/Cc/IDL]/]%^%^!_!_1J>*T->>    c                    t          j                    }|                    | j                   | j        r;t          j        t          | j                  |                                          }n.t          t          t          | j                                      }||d | j        t          |          z
           z  }t          |          | j        k    sJ | j        | j        z  }t          j        t          j        |          || j        f          }|| j        || j        d d f         }t          j        |d                                          }t          |          | j        k    sJ t%          | j        t&                    r/t          t)          | j                            fd|D             }t)          |          S )N)	generator)c                      g | ]
}|         S  r,   ).0iorig_indicess     r%   
<listcomp>z/DistributedSampler.__iter__.<locals>.<listcomp>^   s    8881|A888r'   )torch	Generatormanual_seedr   r   randpermr   r   tolistlistranger"   r   reshape
LongTensorr   r   r!   
isinstancer	   iter)r#   gindicestotal_group_sizer/   s       @r%   __iter__zDistributedSampler.__iter__F   s   O	dj!!!< 	5nS%6%6!DDDKKMMGG5T\!2!23344G 	7=doG<=>>7||t....?do=- 0 9 9<Ldo;^__ $)&69JJAAAMN-//66887||t/////dlG,, 	9T\ 2 233L8888888GG}}r'   c                     | j         S N)r!   r#   s    r%   __len__zDistributedSampler.__len__b   s    r'   r   c                     || _         d S rA   )r   )r#   r   s     r%   	set_epochzDistributedSampler.set_epoche   s    


r'   )NNFr   )__name__
__module____qualname____doc__r   r   r   boolr&   r   r?   rC   rE   r,   r'   r%   r   r   
   s         > '+"  sm sm	
   
   >(3-    8         s t      r'   r   c                   J    e Zd ZdZdededdfdZdee         fdZdefdZ	dS )	UniformClipSamplera  
    Sample `num_video_clips_per_video` clips for each video, equally spaced.
    When number of unique clips in the video is fewer than num_video_clips_per_video,
    repeat the clips until `num_video_clips_per_video` clips are collected

    Args:
        video_clips (VideoClips): video clips to sample from
        num_clips_per_video (int): number of clips to be sampled per video
    video_clipsnum_clips_per_videor   Nc                     t          |t                    st          dt          |                     || _        || _        d S Nz:Expected video_clips to be an instance of VideoClips, got )r:   r
   	TypeErrortyperM   rN   )r#   rM   rN   s      r%   r&   zUniformClipSampler.__init__t   L    +z22 	nlY]^iYjYjllmmm&#6   r'   c                    g }d}| j         j        D ]}t          |          }|dk    rt          j        |||z   dz
  | j                                                                      t          j                  }||z  }|	                    |           t          t          t          t                   t          j        |                                                              S )Nr   r   )steps)rM   clipsr   r1   linspacerN   floortoint64appendr;   r   r   r   catr5   )r#   idxssclengthsampleds         r%   r?   zUniformClipSampler.__iter__z   s    !' 	! 	!AVVF{{nQF
Qd>VWWW]]__bbchcnooGKAKK    DcEIdOO$:$:$<$<==>>>r'   c                 N     t           fd j        j        D                       S )Nc              3   L   K   | ]}t          |          d k    j        V  dS )r   N)r   rN   r-   r_   r#   s     r%   	<genexpr>z-UniformClipSampler.__len__.<locals>.<genexpr>   s2      ZZsSTvvXYzz4+zzzzZZr'   sumrM   rV   rB   s   `r%   rC   zUniformClipSampler.__len__   s,    ZZZZT5E5KZZZZZZr'   
rF   rG   rH   rI   r
   r   r&   r   r?   rC   r,   r'   r%   rL   rL   i   s         7J 7S 7T 7 7 7 7?(3- ? ? ? ?[ [ [ [ [ [ [r'   rL   c                   J    e Zd ZdZdededdfdZdee         fdZdefdZ	dS )	RandomClipSamplerz
    Samples at most `max_video_clips_per_video` clips for each video randomly

    Args:
        video_clips (VideoClips): video clips to sample from
        max_clips_per_video (int): maximum number of clips to be sampled per video
    rM   max_clips_per_videor   Nc                     t          |t                    st          dt          |                     || _        || _        d S rP   )r:   r
   rQ   rR   rM   rk   )r#   rM   rk   s      r%   r&   zRandomClipSampler.__init__   rS   r'   c                    g }d}| j         j        D ]_}t          |          }t          || j                  }t          j        |          d |         |z   }||z  }|                    |           `t          j        |          }t          j        t          |                    }t          ||         
                                          S )Nr   )rM   rV   r   minrk   r1   r4   r[   r\   r;   r5   )	r#   r]   r^   r_   r`   sizera   idxs_perms	            r%   r?   zRandomClipSampler.__iter__   s    !' 	! 	!AVVFvt788DnV,,UdU3a7GKAKK    	$~c%jj))E$K&&(()))r'   c                 N     t           fd j        j        D                       S )Nc              3   \   K   | ]&}t          t          |          j                  V  'd S rA   )rn   r   rk   rd   s     r%   re   z,RandomClipSampler.__len__.<locals>.<genexpr>   s6      YYQ3s1vvt788YYYYYYr'   rf   rB   s   `r%   rC   zRandomClipSampler.__len__   s,    YYYY$BRBXYYYYYYr'   rh   r,   r'   r%   rj   rj      s         7J 7S 7T 7 7 7 7*(3- * * * *Z Z Z Z Z Z Zr'   rj   )r   typingr   r   r   r   r   r   r1   torch.distributeddistributedr   torch.utils.datar	    torchvision.datasets.video_utilsr
   r   rL   rj   r,   r'   r%   <module>ry      s!    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?              $ $ $ $ $ $ 7 7 7 7 7 7\ \ \ \ \ \ \ \~![ ![ ![ ![ ![ ![ ![ ![HZ Z Z Z Z Z Z Z Z Zr'   