U
    Jºc`  ã                   @   s   G d d„ dƒZ dS )c                   @   s0   e Zd ZdZdedœdd„Zdd„ Zdd	„ Zd
S )ÚCyclingIteratoraü  
    An iterator decorator that cycles through the
    underlying iterator "n" times. Useful to "unroll"
    the dataset across multiple training epochs.

    The generator function is called as ``generator_fn(epoch)``
    to obtain the underlying iterator, where ``epoch`` is a
    number less than or equal to ``n`` representing the ``k``th cycle

    For example if ``generator_fn`` always returns ``[1,2,3]``
    then ``CyclingIterator(n=2, generator_fn)`` will iterate through
    ``[1,2,3,1,2,3]``
    é    )Únc                 C   s"   || _ || _|| _|| jƒ| _d S ©N)Ú_nÚ_epochÚ_generator_fnÚ_iter)Úselfr   Zgenerator_fnZstart_epoch© r
   úY/tmp/pip-unpacked-wheel-gikjz4vx/torch/distributed/elastic/utils/data/cycling_iterator.pyÚ__init__   s    zCyclingIterator.__init__c                 C   s   | S r   r
   )r	   r
   r
   r   Ú__iter__   s    zCyclingIterator.__iter__c              
   C   st   zt | jƒW S  tk
rn } zD| j| jd k rZ|  jd7  _|  | j¡| _|  ¡  W Y ¢
S |‚W 5 d }~X Y nX d S )Né   )Únextr   ÚStopIterationr   r   r   Ú__next__)r	   Zeodr
   r
   r   r   "   s    zCyclingIterator.__next__N)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úintr   r   r   r
   r
   r
   r   r   
   s   r   N)r   r
   r
   r
   r   Ú<module>
   ó    