U
    ­Ê¹jd  ã                   @   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                   @   sb   e Zd ZdZdeee ee eeddœdd„Ze	e dœd	d
„Z
edœdd„Zeddœ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 kr t  ¡ stdƒ‚t  ¡ }|d kr@t  ¡ s8tdƒ‚t  ¡ }t|ƒ| dkrht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   úi/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/datasets/samplers/clip_sampler.pyÚ__init__'   s,    ÿzDistributedSampler.__init__©r   c                    s  t  ¡ }| | j¡ | jr4t jt| jƒ|d� ¡ }nt	t
t| jƒƒƒ}||d | jt|ƒ … 7 }t|ƒ| jksrt‚| j| j }t  t  |¡|| jf¡}|| j|| j…d d …f }t  |d¡ ¡ }t|ƒ| jksÒt‚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   ÚAssertionErrorr   ZreshapeZ
LongTensorr   r   r   Ú
isinstancer   Úiter)r   ÚgÚindicesZ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   r   c                 C   s
   || _ d S r3   )r   )r   r   r   r   r   Ú	set_epoche   s    zDistributedSampler.set_epoch)NNFr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úboolr   r   r2   r5   r6   r   r   r   r   r
   
   s        úùr
   c                   @   sB   e Zd ZdZeeddœdd„Zee dœdd„Zedœ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
    N)Úvideo_clipsÚnum_clips_per_videor   c                 C   s,   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   t   s    
zUniformClipSampler.__init__r    c                 C   s|   g }d}| j jD ]N}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   r2   z   s    &zUniformClipSampler.__iter__c                    s   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   r4   r   r   Ú	<genexpr>Š   s      z-UniformClipSampler.__len__.<locals>.<genexpr>©Úsumr=   rB   r4   r   r4   r   r5   ‰   s    zUniformClipSampler.__len__©
r7   r8   r9   r:   r	   r   r   r   r2   r5   r   r   r   r   r<   i   s   
r<   c                   @   sB   e Zd ZdZeeddœdd„Zee dœdd„Zedœd	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
    N)r=   Úmax_clips_per_videor   c                 C   s,   t |tƒstdt|ƒ› �ƒ‚|| _|| _d S r?   )r.   r	   r@   rA   r=   rR   )r   r=   rR   r   r   r   r   –   s    
zRandomClipSampler.__init__r    c           	      C   sz   g }d}| j jD ]@}t|ƒ}t|| jƒ}t |¡d |… | }||7 }| |¡ qt |¡}t t|ƒ¡}t	||  
¡ ƒS )Nr   )r=   rB   r   ÚminrR   r'   r)   rE   rF   r/   r*   )	r   rG   rH   rI   rJ   ÚsizerK   Zidxs_Úpermr   r   r   r2   œ   s    
zRandomClipSampler.__iter__c                    s   t ‡ fdd„ˆ jjD ƒƒS )Nc                 3   s   | ]}t t|ƒˆ jƒV  qd S r3   )rS   r   rR   rL   r4   r   r   rM   ¬   s     z,RandomClipSampler.__len__.<locals>.<genexpr>rN   r4   r   r4   r   r5   «   s    zRandomClipSampler.__len__rP   r   r   r   r   rQ   �   s   rQ   )r   Útypingr   r   r   r   r   r   r'   Ztorch.distributedÚdistributedr   Ztorch.utils.datar   Z torchvision.datasets.video_utilsr	   r
   r<   rQ   r   r   r   r   Ú<module>   s    _$