
    sg	                         d dl mZmZmZmZ d dlmZ d dlmZm	Z	 dgZ
 ed      Z ed       G d de	e                Zy)	    )ListSizedTypeTypeVar)functional_datapipe)	DataChunkMapDataPipeBatcherMapDataPipe_Tbatchc                        e Zd ZU dZeed<   eed<   eed<   defdee	   dedede
e   ddf
 fd	Zdefd
ZdefdZ x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_lastFwrapper_classreturnNc                 r    |dkD  sJ d       t         |           || _        || _        || _        || _        y )Nr   z+Batch size is required to be larger than 0!)super__init__r   r   r   r   )selfr   r   r   r   	__class__s        Z/var/www/html/venv/lib/python3.12/site-packages/torch/utils/data/datapipes/map/grouping.pyr   zBatcherMapDataPipe.__init__(   s?     A~LLL~ $"*    c                 h   g }t        || j                  z  |dz   | j                  z        }	 |D ]   }|j                  | j                  |          " | j	                  |      S # t
        $ rE}| j                  s$t        |      dkD  r| j	                  |      cY d }~S t        d| d      |d }~ww xY w)N   r   zIndex z is out of bound.)ranger   appendr   r   
IndexErrorr   len)r   indexr   indicesies         r   __getitem__zBatcherMapDataPipe.__getitem__6   s    /%!)t1NO	K /T]]1-./%%e,, 	K>>c%j1n))%00 6%0A!BCJ		Ks#   5A# #	B1,*B,B1B,,B1c                 8   t        | j                  t              r`| j                  r"t	        | j                        | j
                  z  S t	        | j                        | j
                  z   dz
  | j
                  z  S t        t        |       j                   d      )Nr   z# instance doesn't have valid length)	
isinstancer   r   r   r   r   	TypeErrortype__name__)r   s    r   __len__zBatcherMapDataPipe.__len__C   ss    dmmU+~~4==)T__<<DMM*T__<q@T__TTtDz2233VWXXr   )r)   
__module____qualname____doc__r	   __annotations__intboolr   r   r   r   r$   r*   __classcell__)r   s   @r   r
   r
      s{    ( OO  )2+b/+ + 	+
 I+ 
+KI KY Yr   N)typingr   r   r   r   %torch.utils.data.datapipes._decoratorr   #torch.utils.data.datapipes.datapiper   r	   __all__r   r
    r   r   <module>r7      sN    - - E F  
  T] W;YY/ ;Y ;Yr   