o
    i                     @   sd   d dl Z d dlZd dlmZmZ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)MultiprocessIteratorSerialIteratorShuffleOrderSampler)	Extensionc                   @   s    e Zd ZdZdd Zdd ZdS )ShufflingEnablerz.An extension enabling shuffling on an Iteratorc                 C   s   d| _ || _dS )zrInits the ShufflingEnabler

        :param list[Iterator] iterators: The iterators to enable shuffling on
        FN)set	iterators)selfr    r
   S/home/ubuntu/.local/lib/python3.10/site-packages/espnet/utils/training/iterators.py__init__
   s   
zShufflingEnabler.__init__c                 C   s(   | j s| jD ]}|  qd| _ dS dS )zVCalls the enabler on the given iterator

        :param trainer: The iterator
        TN)r   r   start_shuffle)r	   traineriteratorr
   r
   r   __call__   s
   


zShufflingEnabler.__call__N)__name__
__module____qualname____doc__r   r   r
   r
   r
   r   r      s    r   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )!ToggleableShufflingSerialIteratorzHA SerialIterator having its shuffling property activated during trainingTc                    s   t t| |||| dS )a  Init the Iterator

        :param torch.nn.Tensor dataset: The dataset to take batches from
        :param int batch_size: The batch size
        :param bool repeat: Whether to repeat data (allow multiple epochs)
        :param bool shuffle: Whether to shuffle the batches
        N)superr   r   )r	   dataset
batch_sizerepeatshuffle	__class__r
   r   r       s   
z*ToggleableShufflingSerialIterator.__init__c                 C   sX   d| _ ttjjd dkrtjt| j	| _
dS t | _| tt| j	d| _
dS z,Starts shuffling (or reshuffles) the batchesTr      N)_shuffleintchainer_version__version__nprandompermutationlenr   _orderr   order_sampleraranger	   r
   r
   r   r   ,   s
   z/ToggleableShufflingSerialIterator.start_shuffle)TTr   r   r   r   r   r   __classcell__r
   r
   r   r   r      s    r   c                       s6   e Zd ZdZ						d
 fdd	Zdd	 Z  ZS )'ToggleableShufflingMultiprocessIteratorzNA MultiprocessIterator having its shuffling property activated during trainingTN      c	           	   
      s$   t t| j||||||||d dS )a:  Init the iterator

        :param torch.nn.Tensor dataset: The dataset to take batches from
        :param int batch_size: The batch size
        :param bool repeat: Whether to repeat batches or not (enables multiple epochs)
        :param bool shuffle: Whether to shuffle the order of the batches
        :param int n_processes: How many processes to use
        :param int n_prefetch: The number of prefetch to use
        :param int shared_mem: How many memory to share between processes
        :param int maxtasksperchild: Maximum number of tasks per child
        )r   r   r   r   n_processes
n_prefetch
shared_memmaxtasksperchildN)r   r.   r   )	r	   r   r   r   r   r1   r2   r3   r4   r   r
   r   r   9   s   

z0ToggleableShufflingMultiprocessIterator.__init__c                 C   s^   d| _ ttjjd dkrtjt| j	| _
nt | _| tt| j	d| _
|   dS r   )r   r    r!   r"   r#   r$   r%   r&   r'   r   r(   r   r)   r*   _set_prefetch_stater+   r
   r
   r   r   Z   s   z5ToggleableShufflingMultiprocessIterator.start_shuffle)TTNr/   Nr0   r,   r
   r
   r   r   r.   6   s    !r.   )r!   numpyr$   chainer.iteratorsr   r   r   chainer.training.extensionr   r   r   r.   r
   r
   r
   r   <module>   s    