o
    Vhd                     @   s   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 deZG dd deZG d	d
 d
eZdS )    N)castIteratorListOptionalSizedUnion)Sampler)
VideoClipsc                   @   sv   e Zd ZdZ				ddedee dee ded	ed
dfddZd
e	e fddZ
d
efddZded
dfd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                 C   s   |d u rt  stdt  }|d u r t  stdt  }t|| dkr4tdt| d| || _|| _|| _	|| _
d| _t|| }tt|d | j	 | _| j| | _| j| j	 | _|| _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_length r"   ^/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/datasets/samplers/clip_sampler.py__init__'   s,   
zDistributedSampler.__init__c                    s  t  }|| j | jrt jt| j|d }n	t	t
t| j}||d | jt|  7 }t|| jks9J | j| j }t t ||| jf}|| j|| jd d f }t |d }t|| jksiJ t| jtrt	t| j  fdd|D }t|S )N)	generator)c                    s   g | ]} | qS r"   r"   ).0iorig_indicesr"   r#   
<listcomp>^   s    z/DistributedSampler.__iter__.<locals>.<listcomp>)torch	Generatormanual_seedr   r   randpermr   r   tolistlistranger   r   reshape
LongTensorr   r   r   
isinstancer   iter)r    gindicestotal_group_sizer"   r)   r#   __iter__F   s    zDistributedSampler.__iter__c                 C   s   | j S N)r   r    r"   r"   r#   __len__b   s   zDistributedSampler.__len__r   c                 C   s
   || _ d S r;   )r   )r    r   r"   r"   r#   	set_epoche   s   
zDistributedSampler.set_epoch)NNFr   )__name__
__module____qualname____doc__r   r   r   boolr$   r   r:   r=   r>   r"   r"   r"   r#   r
   
   s,    
r
   c                   @   F   e Zd ZdZdededdfddZdee fdd	Zdefd
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                 C   ,   t |tstdt| || _|| _d S Nz:Expected video_clips to be an instance of VideoClips, got )r5   r	   	TypeErrortyperF   rG   )r    rF   rG   r"   r"   r#   r$   t      

zUniformClipSampler.__init__c                 C   s|   g }d}| j jD ]'}t|}|dkrqtj||| d | jd tj}||7 }|	| qt
ttt t| S )Nr   r   )steps)rF   clipsr   r,   linspacerG   floortoint64appendr6   r   r   r   catr0   )r    idxssclengthsampledr"   r"   r#   r:   z   s   &zUniformClipSampler.__iter__c                       t  fdd jjD S )Nc                 3   s"    | ]}t |d kr jV  qdS )r   N)r   rG   r'   rW   r<   r"   r#   	<genexpr>   s     z-UniformClipSampler.__len__.<locals>.<genexpr>sumrF   rN   r<   r"   r<   r#   r=         zUniformClipSampler.__len__
r?   r@   rA   rB   r	   r   r$   r   r:   r=   r"   r"   r"   r#   rE   i   s
    
rE   c                   @   rD   )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
    rF   max_clips_per_videor   Nc                 C   rH   rI   )r5   r	   rJ   rK   rF   rb   )r    rF   rb   r"   r"   r#   r$      rL   zRandomClipSampler.__init__c           	      C   sz   g }d}| j jD ] }t|}t|| j}t|d | | }||7 }|| qt|}tt|}t	|| 
 S )Nr   )rF   rN   r   minrb   r,   r/   rS   rT   r6   r0   )	r    rU   rV   rW   rX   sizerY   idxs_permr"   r"   r#   r:      s   
zRandomClipSampler.__iter__c                    rZ   )Nc                 3   s     | ]}t t| jV  qd S r;   )rc   r   rb   r[   r<   r"   r#   r\      s    z,RandomClipSampler.__len__.<locals>.<genexpr>r]   r<   r"   r<   r#   r=      r_   zRandomClipSampler.__len__r`   r"   r"   r"   r#   ra      s
    ra   )r   typingr   r   r   r   r   r   r,   torch.distributeddistributedr   torch.utils.datar    torchvision.datasets.video_utilsr	   r
   rE   ra   r"   r"   r"   r#   <module>   s     _$