U
    <c"
                     @   sb   d dl mZ d dlmZmZ d dlmZmZmZm	Z	 dgZ
e	dZedG dd dee ZdS )	    )functional_datapipe)MapDataPipe	DataChunk)ListOptionalSizedTypeVarBatcherMapDataPipeTbatchc                       sx   e Zd ZU dZeed< eed< eed< ee ed< de	fee
 eedd fd	d
Ze	dddZedddZ  ZS )r	   a  
    Create mini-batches of data (functional name: ``batch``). An outer dimension will be added as
    ``batch_size`` if ``drop_last`` is set to ``True``, or ``length % batch_size`` for the
    last batch if ``drop_last`` is set to ``False``.

    Args:
        datapipe: Iterable DataPipe being batched
        batch_size: The size of each batch
        drop_last: Option to drop the last batch if it's not full

    Example:
        >>> # xdoctest: +SKIP
        >>> from torchdata.datapipes.map import SequenceWrapper
        >>> dp = SequenceWrapper(range(10))
        >>> batch_dp = dp.batch(batch_size=2)
        >>> list(batch_dp)
        [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
    datapipe
batch_size	drop_lastlengthFN)r   r   r   returnc                    s<   |dkst dt   || _|| _|| _d | _|| _d S )Nr   z+Batch size is required to be larger than 0!)AssertionErrorsuper__init__r   r   r   r   wrapper_class)selfr   r   r   r   	__class__ K/tmp/pip-unpacked-wheel-gikjz4vx/torch/utils/data/datapipes/map/grouping.pyr   #   s    
zBatcherMapDataPipe.__init__)r   c                 C   s   g }t || j |d | j }z&|D ]}|| j|  q$| |W S  tk
r   | jstt|dkrt| | Y S td| dY nX d S )N   r   zIndex z is out of bound.)ranger   appendr   r   
IndexErrorr   len)r   indexr   indicesir   r   r   __getitem__1   s    zBatcherMapDataPipe.__getitem__c                 C   sp   | j d k	r| j S t| jtrX| jr6t| j| j | _ nt| j| j d | j | _ | j S tdt	| j
d S )Nr   z%{} instance doesn't have valid length)r   
isinstancer   r   r   r   r   	TypeErrorformattype__name__)r   r   r   r   __len__>   s    
zBatcherMapDataPipe.__len__)r'   
__module____qualname____doc__r   __annotations__intboolr   r   r
   r   r"   r(   __classcell__r   r   r   r   r	   
   s   
N)Z%torch.utils.data.datapipes._decoratorr   Z#torch.utils.data.datapipes.datapiper   r   typingr   r   r   r   __all__r
   r	   r   r   r   r   <module>   s   