
    khd                         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y)    N)castIteratorListOptionalSizedUnion)Sampler)
VideoClipsc                   t    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y)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]

    Ndatasetnum_replicasrankshuffle
group_sizereturnc                 h   |3t        j                         st        d      t        j                         }|3t        j                         st        d      t        j                         }t        |      |z  dk7  rt        dt        |       d|       || _        || _        || _	        || _
        d| _        t        |      |z  }t        t        j                  |dz  | j                  z              | _        | j                  |z  | _        | j                   | j                  z  | _        || _        y )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/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/datasets/samplers/clip_sampler.py__init__zDistributedSampler.__init__'   s    $$&"#QRR..0L<$$&"#QRR==?Dw<*$)TUXY`UaTbbpq{p|}  $(	
"7|z9!$TYY/Cc/IDL]L]/]%^!_11J>**T->->>    c                    t        j                         }|j                  | j                         | j                  r9t        j
                  t        | j                        |      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      j                         }t        |      | j"                  k(  sJ t%        | j                  t&              r0t        t)        | j                              }|D cg c]  }||   	 }}t)        |      S c c}w )N)	generator))torch	Generatormanual_seedr   r   randpermr   r   tolistlistranger!   r   reshape
LongTensorr   r   r    
isinstancer	   iter)r"   gindicestotal_group_sizeorig_indicesis         r$   __iter__zDistributedSampler.__iter__F   si   OO	djj!<<nnS%6!DKKMG5T\\!234G 	7=dooG<>>7|t...??doo=-- 0 0 9<Ldoo;^_ $))&69J9JJAMN--/6687|t/////dllG,T\\ 23L0781|A8G8G} 9s   <Gc                     | j                   S N)r    r"   s    r$   __len__zDistributedSampler.__len__b   s    r&   r   c                     || _         y r<   )r   )r"   r   s     r$   	set_epochzDistributedSampler.set_epoche   s	    
r&   )NNF   )__name__
__module____qualname____doc__r   r   r   boolr%   r   r:   r>   r@    r&   r$   r   r   
   s    > '+" sm sm	
   
>(3- 8   s t r&   r   c                   B    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	y)	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                 n    t        |t              st        dt        |             || _        || _        y Nz:Expected video_clips to be an instance of VideoClips, got )r3   r
   	TypeErrortyperJ   rK   )r"   rJ   rK   s      r$   r%   zUniformClipSampler.__init__t   7    +z2XY]^iYjXklmm&#6 r&   c                    g }d}| j                   j                  D ]|  }t        |      }|dk(  rt        j                  |||z   dz
  | j
                        j                         j                  t        j                        }||z  }|j                  |       ~ t        t        t        t           t        j                  |      j                                     S )Nr   rA   )steps)rJ   clipsr   r*   linspacerK   floortoint64appendr4   r   r   r   catr.   )r"   idxssclengthsampleds         r$   r:   zUniformClipSampler.__iter__z   s    !!'' 	!AVF{nnQF
Qd>V>VW]]_bbchcncnoGKAKK 	! DcEIIdO$:$:$<=>>r&   c                 T     t         fd j                  j                  D              S )Nc              3   T   K   | ]  }t        |      d kD  sj                   ! yw)r   N)r   rK   .0r\   r"   s     r$   	<genexpr>z-UniformClipSampler.__len__.<locals>.<genexpr>   s#     ZsSTvXYz4++Zs   ((sumrJ   rS   r=   s   `r$   r>   zUniformClipSampler.__len__   s     ZT5E5E5K5KZZZr&   
rB   rC   rD   rE   r
   r   r%   r   r:   r>   rG   r&   r$   rI   rI   i   s?    7J 7S 7T 7?(3- ?[ [r&   rI   c                   B    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	y)	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
    rJ   max_clips_per_videor   Nc                 n    t        |t              st        dt        |             || _        || _        y rM   )r3   r
   rN   rO   rJ   ri   )r"   rJ   ri   s      r$   r%   zRandomClipSampler.__init__   rP   r&   c                    g }d}| j                   j                  D ]T  }t        |      }t        || j                        }t        j                  |      d | |z   }||z  }|j                  |       V t        j                  |      }t        j                  t        |            }t        ||   j                               S )Nr   )rJ   rS   r   minri   r*   r-   rX   rY   r4   r.   )	r"   rZ   r[   r\   r]   sizer^   idxs_perms	            r$   r:   zRandomClipSampler.__iter__   s    !!'' 	!AVFvt778DnnV,Ud3a7GKAKK 	! 		$~~c%j)E$K&&())r&   c                 T     t         fd j                  j                  D              S )Nc              3   \   K   | ]#  }t        t        |      j                         % y wr<   )rl   r   ri   ra   s     r$   rc   z,RandomClipSampler.__len__.<locals>.<genexpr>   s"     YQ3s1vt778Ys   ),rd   r=   s   `r$   r>   zRandomClipSampler.__len__   s     Y$BRBRBXBXYYYr&   rf   rG   r&   r$   rh   rh      s?    7J 7S 7T 7*(3- *Z Zr&   rh   )r   typingr   r   r   r   r   r   r*   torch.distributeddistributedr   torch.utils.datar	    torchvision.datasets.video_utilsr
   r   rI   rh   rG   r&   r$   <module>rw      sF     ? ?    $ 7\ \~![ ![HZ Zr&   