
    %`iy                         d dl Z d dlmZmZ d dl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)IteratorSized)castOptional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          ~/home/jaya/work/projects/VOICE-AGENT/VIET/agent-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__G   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__c   s    r&   r   c                     || _         d S r@   )r   )r"   r   s     r$   	set_epochzDistributedSampler.set_epochf   s    


r&   )NNFr   )__name__
__module____qualname____doc__r   r   r   boolr%   r   r>   rB   rD   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 )r9   r	   	TypeErrortyperL   rM   )r"   rL   rM   s      r$   r%   zUniformClipSampler.__init__u   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)rL   clipsr   r0   linspacerM   floortoint64appendr:   r   r5   r   catr4   )r"   idxssclengthsampleds         r$   r>   zUniformClipSampler.__iter__{   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   rM   r,   r^   r"   s     r$   	<genexpr>z-UniformClipSampler.__len__.<locals>.<genexpr>   s2      ZZsSTvvXYzz4+zzzzZZr&   sumrL   rU   rA   s   `r$   rB   zUniformClipSampler.__len__   s,    ZZZZT5E5KZZZZZZr&   
rE   rF   rG   rH   r	   r   r%   r   r>   rB   r+   r&   r$   rK   rK   j   s         7J 7S 7T 7 7 7 7?(3- ? ? ? ?[ [ [ [ [ [ [r&   rK   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
    rL   max_clips_per_videor   Nc                     t          |t                    st          dt          |                     || _        || _        d S rO   )r9   r	   rP   rQ   rL   rj   )r"   rL   rj   s      r$   r%   zRandomClipSampler.__init__   rR   r&   c                    g }d}| j         j        D ]_}t          |          }t          || j                  }t          j        |          d |         |z   }||z  }|                    |           `t          j        |          }t          j        t          |                    }t          ||         
                                          S )Nr   )rL   rU   r   minrj   r0   r3   rZ   r[   r:   r4   )	r"   r\   r]   r^   r_   sizer`   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 r@   )rm   r   rj   rc   s     r$   rd   z,RandomClipSampler.__len__.<locals>.<genexpr>   s6      YYQ3s1vvt788YYYYYYr&   re   rA   s   `r$   rB   zRandomClipSampler.__len__   s,    YYYY$BRBXYYYYYYr&   rg   r+   r&   r$   ri   ri      s         7J 7S 7T 7 7 7 7*(3- * * * *Z Z Z Z Z Z Zr&   ri   )r   collections.abcr   r   typingr   r   r   r0   torch.distributeddistributedr   torch.utils.datar    torchvision.datasets.video_utilsr	   r   rK   ri   r+   r&   r$   <module>ry      s'    + + + + + + + + ( ( ( ( ( ( ( ( ( (              $ $ $ $ $ $ 7 7 7 7 7 7\ \ \ \ \ \ \ \~![ ![ ![ ![ ![ ![ ![ ![HZ Z Z Z Z Z Z Z Z Zr&   