o
    ,hy                     @   s   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 deZG dd	 d	eZG d
d deZdS )    N)IteratorSized)castOptional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!   e/var/www/html/scripts/venv/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__G   s    zDistributedSampler.__iter__c                 C   s   | j S N)r   r   r!   r!   r"   __len__c   s   zDistributedSampler.__len__r   c                 C   s
   || _ d S r:   )r   )r   r   r!   r!   r"   	set_epochf   s   
zDistributedSampler.set_epoch)NNFr
   )__name__
__module____qualname____doc__r   r   r   boolr#   r   r9   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 )r4   r   	TypeErrortyperE   rF   )r   rE   rF   r!   r!   r"   r#   u      

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)rE   clipsr   r+   linspacerF   floortoint64appendr5   r   r0   r   catr/   )r   idxssclengthsampledr!   r!   r"   r9   {   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   rF   r&   rV   r;   r!   r"   	<genexpr>   s     z-UniformClipSampler.__len__.<locals>.<genexpr>sumrE   rM   r;   r!   r;   r"   r<         zUniformClipSampler.__len__
r>   r?   r@   rA   r   r   r#   r   r9   r<   r!   r!   r!   r"   rD   j   s
    
rD   c                   @   rC   )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
    rE   max_clips_per_videor   Nc                 C   rG   rH   )r4   r   rI   rJ   rE   ra   )r   rE   ra   r!   r!   r"   r#      rK   zRandomClipSampler.__init__c           	      C   sz   g }d}| j jD ] }t|}t|| j}t|d | | }||7 }|| qt|}tt|}t	|| 
 S )Nr   )rE   rM   r   minra   r+   r.   rR   rS   r5   r/   )	r   rT   rU   rV   rW   sizerX   idxs_permr!   r!   r"   r9      s   
zRandomClipSampler.__iter__c                    rY   )Nc                 3   s     | ]}t t| jV  qd S r:   )rb   r   ra   rZ   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"   r`      s
    r`   )r   collections.abcr   r   typingr   r   r   r+   torch.distributeddistributedr   torch.utils.datar    torchvision.datasets.video_utilsr   r	   rD   r`   r!   r!   r!   r"   <module>   s    _$