o
    wO i`  ã                   @   s   G d d„ dƒZ dS )c                   @   s0   e Zd ZdZddef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   Úgenerator_fnÚstart_epoch© r   ú\/home/ubuntu/.local/lib/python3.10/site-packages/torchelastic/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   sj   zt | jƒW S  ty4 } z"| j| jd k r.|  jd7  _|  | j¡| _|  ¡ W  Y d }~S |‚d }~ww )Né   )Únextr   ÚStopIterationr   r   r   Ú__next__)r	   Ú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>   s   	