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      ?)distZis_availableRuntimeErrorZget_world_sizeZget_ranklen
ValueErrorr   r   r   r   epochintmathceilZnum_group_samplesnum_samples
total_sizer   )selfr   r   r   r   r   Zdataset_group_length r   p/home/www/facesmatcher.com/frenv_anti/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Zorig_indicesr   r   
<listcomp>_   s    z/DistributedSampler.__iter__.<locals>.<listcomp>)torch	GeneratorZmanual_seedr   r   randpermr   r   tolistlistranger   r   ZreshapeZ
LongTensorr   r   r   
isinstancer   iter)r   gindicesZ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 r0   )r   )r   r   r   r   r   	set_epochf   s   
zDistributedSampler.set_epoch)NNFr
   )__name__
__module____qualname____doc__r   r   r   boolr   r   r/   r2   r3   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 )r+   r   	TypeErrortyper;   r<   )r   r;   r<   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
   )Zsteps)r;   clipsr   r%   Zlinspacer<   floortoZint64appendr,   r   r)   r   catr(   )r   idxssclengthsampledr   r   r   r/   {   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   r<   r!   rI   r1   r   r   	<genexpr>   s     z-UniformClipSampler.__len__.<locals>.<genexpr>sumr;   rB   r1   r   r1   r   r2         zUniformClipSampler.__len__
r4   r5   r6   r7   r   r   r   r   r/   r2   r   r   r   r   r:   j   s
    
r:   c                   @   r9   )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
    r;   max_clips_per_videor   Nc                 C   r=   r>   )r+   r   r?   r@   r;   rT   )r   r;   rT   r   r   r   r      rA   zRandomClipSampler.__init__c           	      C   sz   g }d}| j jD ] }t|}t|| j}t|d | | }||7 }|| qt|}tt|}t	|| 
 S )Nr   )r;   rB   r   minrT   r%   r'   rE   rF   r,   r(   )	r   rG   rH   rI   rJ   sizerK   Zidxs_permr   r   r   r/      s   
zRandomClipSampler.__iter__c                    rL   )Nc                 3   s     | ]}t t| jV  qd S r0   )rU   r   rT   rM   r1   r   r   rN      s    z,RandomClipSampler.__len__.<locals>.<genexpr>rO   r1   r   r1   r   r2      rQ   zRandomClipSampler.__len__rR   r   r   r   r   rS      s
    rS   )r   collections.abcr   r   typingr   r   r   r%   Ztorch.distributeddistributedr   Ztorch.utils.datar   Z torchvision.datasets.video_utilsr   r	   r:   rS   r   r   r   r   <module>   s    _$