U
    Mh	                     @   s^   d dl mZ d dlmZmZ d dl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SizedTypeVarBatcherMapDataPipeTbatchc                       sl   e Zd ZU dZeed< eed< 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_lastFN)r   r   r   returnc                    s6   |dkst dt   || _|| _|| _|| _d S )Nr   z+Batch size is required to be larger than 0!)AssertionErrorsuper__init__r   r   r   wrapper_class)selfr   r   r   r   	__class__ Y/var/www/html/venv/lib/python3.8/site-packages/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 } z:| jszt|dkrz| | W Y S td| d|W 5 d }~X Y nX d S )N   r   zIndex z is out of bound.)ranger   appendr   r   
IndexErrorr   len)r   indexr
   indicesier   r   r   __getitem__2   s    zBatcherMapDataPipe.__getitem__c                 C   sV   t | jtr>| jr"t| j| j S t| j| j d | j S ntt| j dd S )Nr   z# instance doesn't have valid length)	
isinstancer   r   r   r   r   	TypeErrortype__name__)r   r   r   r   __len__?   s
    zBatcherMapDataPipe.__len__)r%   
__module____qualname____doc__r   __annotations__intboolr   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   __all__r	   r   r   r   r   r   <module>   s   