o
    ©Ì³i-  ã                   @   s\   d dl mZmZmZmZmZ d dlmZmZ G dd„ deee  ƒZ	G dd„ dee ƒZ
dS )é    )ÚAnyÚDictÚListÚOptionalÚSequence)ÚBaseNodeÚTc                       s€   e Zd ZdZdZddee dedef‡ fdd„Z	dd	e
eeef  f‡ fd
d„Zdee fdd„Zdeeef fdd„Z‡  ZS )ÚBatcheraY  Batcher node batches the data from the source node into batches of size batch_size.
    If the source node is exhausted, it will return the batch or raise StopIteration.
    If drop_last is True, the last batch will be dropped if it is smaller than batch_size.
    If drop_last is False, the last batch will be returned even if it is smaller than batch_size.

    Args:
        source (BaseNode[T]): The source node to batch the data from.
        batch_size (int): The size of the batch.
        drop_last (bool): Whether to drop the last batch if it is smaller than batch_size. Default is True.
    ÚsourceTÚ
batch_sizeÚ	drop_lastc                    s    t ƒ  ¡  || _|| _|| _d S ©N)ÚsuperÚ__init__r
   r   r   )Úselfr
   r   r   ©Ú	__class__© úI/home/ubuntu/.local/lib/python3.10/site-packages/torchdata/nodes/batch.pyr      s   

zBatcher.__init__NÚinitial_statec                    s8   t ƒ  |¡ |d ur| j || j ¡ d S | j ¡  d S r   )r   Úresetr
   Ú
SOURCE_KEY©r   r   r   r   r   r       s   zBatcher.resetÚreturnc                 C   sˆ   g }t |ƒ| jk r/zt| jƒ}W n	 ty   Y nw | |¡ t |ƒ| jkr(|S t |ƒ| jk s	t |ƒ| jkr8|S t |ƒrA| jsA|S tƒ ‚r   )Úlenr   Únextr
   ÚStopIterationÚappendr   )r   ÚbatchÚitemr   r   r   r   '   s    ÿ
ù	zBatcher.nextc                 C   s   | j | j ¡ iS r   )r   r
   Ú
state_dict©r   r   r   r   Ú	get_state9   s   zBatcher.get_state)Tr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   ÚintÚboolr   r   r   Ústrr   r   r   r   r"   Ú__classcell__r   r   r   r   r	      s      r	   c                       sz   e Zd ZdZdZdZdeee  f‡ fdd„Z	dde
eeef  f‡ fdd	„Zd
efdd„Zd
eeef fdd„Z‡  ZS )Ú	UnbatcherzÏUnbatcher will flatten batches pulled from source, and
    yields elements in sequential order when next() is called on it.

    Args:
        source (BaseNode[T]): The source node to pull batches from.
    r
   Ú	batch_idxc                    s   t ƒ  | ¡ || _d S r   )r   r   r
   )r   r
   r   r   r   r   H   s   
zUnbatcher.__init__Nr   c                    s’   t ƒ  |¡ |d ur9| j || j ¡ || j | _zt| jƒ| _|| j | _W d S  t	y8   g | _d| _Y d S w | j ¡  g | _d | _d| _d S )Nr   )
r   r   r
   r   Ú_cached_state_dictr   Ú_batchÚBATCH_IDX_KEYÚ
_batch_idxr   r   r   r   r   r   L   s   ü

zUnbatcher.resetr   c                 C   s\   | j t| jƒkr| j ¡ | _t| jƒ| _d| _ | j t| jƒks|  j d7  _ | j| j d  S )Nr   é   )r0   r   r.   r
   r    r-   r   r!   r   r   r   r   _   s   ýzUnbatcher.nextc                 C   s*   | j d u r| j ¡ | _ | j| j | j| jiS r   )r-   r
   r    r   r/   r0   r!   r   r   r   r"   h   s
   
þzUnbatcher.get_stater   )r#   r$   r%   r&   r   r/   r   r   r   r   r   r   r)   r   r   r   r"   r*   r   r   r   r   r+   =   s     	r+   N)Útypingr   r   r   r   r   Útorchdata.nodes.base_noder   r   r	   r+   r   r   r   r   Ú<module>   s   1