§
    X�’j¬1  ã                   óf  — d dl mZ d dlmZmZmZ d dlmZmZm	Z	 d dl
mZ d dlmZmZ d dlmZ g d¢Z e	dd	¬
¦  «        Zdedefd„Z ed¦  «         G d„ dee         ¦  «        ¦   «         Z ed¦  «         G d„ de¦  «        ¦   «         Z ed¦  «         G d„ dee         ¦  «        ¦   «         ZdS )é    )Údefaultdict)ÚCallableÚIteratorÚSized)ÚAnyÚNoReturnÚTypeVar)Úfunctional_datapipe)Ú	DataChunkÚIterDataPipe)Ú_check_unpickable_fn)ÚBatcherIterDataPipeÚGrouperIterDataPipeÚUnBatcherIterDataPipeÚ_T_coT)Ú	covariantÚnameÚreturnc                 ó6   — t          dt          › d| › �¦  «        ‚)Nzmodule z has no attribute )ÚAttributeErrorÚ__name__)r   s    úVC:\Projects\DSCons\.venv\Lib\site-packages\torch\utils\data\datapipes\iter\grouping.pyÚ__getattr__r      s!   € Ý
ÐE¥8ÐEÐE¸tÐEÐEÑ
FÔ
FÐFó    Úbatchc                   óŽ   ‡ — e Zd ZU dZeed<   eed<   eed<   defdededede	e         ddf
ˆ fd	„Z
dee         fd
„Zdefd„Zˆ xZS )r   a2  
    Creates 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
        wrapper_class: wrapper to apply onto each batch (type ``List``) before yielding,
            defaults to ``DataChunk``

    Example:
        >>> # xdoctest: +SKIP
        >>> from torchdata.datapipes.iter import IterableWrapper
        >>> dp = IterableWrapper(range(10))
        >>> dp = dp.batch(batch_size=3, drop_last=True)
        >>> list(dp)
        [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
    ÚdatapipeÚ
batch_sizeÚ	drop_lastFÚwrapper_classr   Nc                 óª   •— |dk    rt          d¦  «        ‚t          ¦   «                              ¦   «          || _        || _        || _        || _        d S )Nr   z+Batch size is required to be larger than 0!)ÚAssertionErrorÚsuperÚ__init__r   r   r   r    )Úselfr   r   r   r    Ú	__class__s        €r   r$   zBatcherIterDataPipe.__init__5   sV   ø€ ð ˜Š?ˆ?Ý Ð!NÑOÔOÐOÝ‰Œ×ÒÑÔÐØ ˆŒØ$ˆŒØ"ˆŒØ*ˆÔÐÐr   c              #   ó  K  — g }| j         D ]H}|                     |¦  «         t          |¦  «        | j        k    r|                      |¦  «        V — g }ŒIt          |¦  «        dk    r | j        s|                      |¦  «        V — d S d S d S ©Nr   )r   ÚappendÚlenr   r    r   )r%   r   Úxs      r   Ú__iter__zBatcherIterDataPipe.__iter__D   s¨   è è € ØˆØ”ð 	ð 	ˆAØ�LŠL˜‰OŒOˆOÝ�5‰zŒz˜Tœ_Ò,Ð,Ø×(Ò(¨Ñ/Ô/Ð/Ð/Ð/Ø�øÝˆu‰:Œ:˜Š>ˆ>Ø”>ð 0Ø×(Ò(¨Ñ/Ô/Ð/Ð/Ð/Ð/Ð/ð ˆ>ð0ð 0r   c                 ó  — t          | j        t          ¦  «        rJ| j        rt	          | j        ¦  «        | j        z  S t	          | j        ¦  «        | j        z   dz
  | j        z  S t          t          | ¦  «        j        › d�¦  «        ‚)Né   z# instance doesn't have valid length)	Ú
isinstancer   r   r   r*   r   Ú	TypeErrorÚtyper   ©r%   s    r   Ú__len__zBatcherIterDataPipe.__len__O   s|   € å�d”m¥UÑ+Ô+ð 	YØŒ~ð UÝ˜4œ=Ñ)Ô)¨T¬_Ñ<Ð<å˜DœMÑ*Ô*¨T¬_Ñ<¸qÑ@ÀTÄ_ÑTÐTå�t D™zœzÔ2ÐWÐWÐWÑXÔXÐXr   )r   Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__ÚintÚboolr   r1   r$   r   r,   r3   Ú__classcell__)r&   s   @r   r   r      sî   ø€ € € € € € ðð ð, ÐÐÑØ€O€O�OØ€O€O�Oð  Ø)2ð+ð +àð+ð ð+ð ð	+ð
 ˜I”ð+ð 
ð+ð +ð +ð +ð +ð +ð	0˜( 9Ô-ð 	0ð 	0ð 	0ð 	0ðY˜ð Yð Yð Yð Yð Yð Yð Yð Yr   r   Úunbatchc                   ó4   — e Zd ZdZd
dededdfd„Zd„ Zd	„ ZdS )r   a!  
    Undoes batching of data (functional name: ``unbatch``).

    In other words, it flattens the data up to the specified level within a batched DataPipe.

    Args:
        datapipe: Iterable DataPipe being un-batched
        unbatch_level: Defaults to ``1`` (only flattening the top level). If set to ``2``,
            it will flatten the top two levels, and ``-1`` will flatten the entire DataPipe.

    Example:
        >>> # xdoctest: +SKIP
        >>> from torchdata.datapipes.iter import IterableWrapper
        >>> source_dp = IterableWrapper([[[0, 1], [2]], [[3, 4], [5]], [[6]]])
        >>> dp1 = source_dp.unbatch()
        >>> list(dp1)
        [[0, 1], [2], [3, 4], [5], [6]]
        >>> dp2 = source_dp.unbatch(unbatch_level=2)
        >>> list(dp2)
        [0, 1, 2, 3, 4, 5, 6]
    r.   r   Úunbatch_levelr   Nc                 ó"   — || _         || _        d S ©N)r   r=   )r%   r   r=   s      r   r$   zUnBatcherIterDataPipe.__init__r   s   € Ø ˆŒØ*ˆÔÐÐr   c              #   ób   K  — | j         D ]$}|                      || j        ¬¦  «        E d {V —† Œ%d S )N©r=   )r   Ú_diver=   )r%   Úelements     r   r,   zUnBatcherIterDataPipe.__iter__v   sS   è è € Ø”}ð 	Mð 	MˆGØ—z’z '¸Ô9K�zÑLÔLÐLÐLÐLÐLÐLÐLÐLÐLð	Mð 	Mr   c              #   ó–  K  — |dk     rt          d¦  «        ‚|dk    rFt          |t          t          f¦  «        r$|D ]}|                      |d¬¦  «        E d {V —† Œ d S |V — d S |dk    r|V — d S t          |t          t          f¦  «        r'|D ]"}|                      ||dz
  ¬¦  «        E d {V —† Œ#d S t          d| j        › d�¦  «        ‚)Néÿÿÿÿz unbatch_level must be -1 or >= 0rA   r   r.   zunbatch_level z" exceeds the depth of the DataPipe)Ú
ValueErrorr/   Úlistr   rB   Ú
IndexErrorr=   )r%   rC   r=   Úitems       r   rB   zUnBatcherIterDataPipe._divez   s:  è è € Ø˜2ÒÐÝÐ?Ñ@Ô@Ð@Ø˜BÒÐÝ˜'¥D­)Ð#4Ñ5Ô5ð Ø#ð Bð B�DØ#Ÿzšz¨$¸b˜zÑAÔAÐAÐAÐAÐAÐAÐAÐAÐAðBð Bð �����Ø˜aÒÐØˆMˆMˆMˆMˆMå˜'¥D­)Ð#4Ñ5Ô5ð Ø#ð Qð Q�DØ#Ÿzšz¨$¸mÈaÑ>O˜zÑPÔPÐPÐPÐPÐPÐPÐPÐPÐPðQð Qõ !Ø[ TÔ%7Ð[Ð[Ð[ñô ð r   )r.   )	r   r4   r5   r6   r   r8   r$   r,   rB   © r   r   r   r   Z   sm   € € € € € ðð ð,+ð + ð +¸cð +È$ð +ð +ð +ð +ðMð Mð Mðð ð ð ð r   r   Úgroupbyc                   óš   — e Zd ZdZddddddœdee         deegef         ded	e	d
e	dz  de	dz  deddfd„Z
d„ Zd„ Zdd„Zd„ Zd„ Zdd„ZdS )r   aŽ
  
    Groups data from IterDataPipe by keys from ``group_key_fn``, yielding a ``DataChunk`` with batch size up to ``group_size``.

    (functional name: ``groupby``).

    The samples are read sequentially from the source ``datapipe``, and a batch of samples belonging to the same group
    will be yielded as soon as the size of the batch reaches ``group_size``. When the buffer is full,
    the DataPipe will yield the largest batch with the same key, provided that its size is larger
    than ``guaranteed_group_size``. If its size is smaller, it will be dropped if ``drop_remaining=True``.

    After iterating through the entirety of source ``datapipe``, everything not dropped due to the buffer capacity
    will be yielded from the buffer, even if the group sizes are smaller than ``guaranteed_group_size``.

    Args:
        datapipe: Iterable datapipe to be grouped
        group_key_fn: Function used to generate group key from the data of the source datapipe
        keep_key: Option to yield the matching key along with the items in a tuple,
            resulting in `(key, [items])` otherwise returning [items]
        buffer_size: The size of buffer for ungrouped data
        group_size: The max size of each group, a batch is yielded as soon as it reaches this size
        guaranteed_group_size: The guaranteed minimum group size to be yielded in case the buffer is full
        drop_remaining: Specifies if the group smaller than ``guaranteed_group_size`` will be dropped from buffer
            when the buffer is full

    Example:
        >>> import os
        >>> # xdoctest: +SKIP
        >>> from torchdata.datapipes.iter import IterableWrapper
        >>> def group_fn(file):
        ...     return os.path.basename(file).split(".")[0]
        >>> source_dp = IterableWrapper(
        ...     ["a.png", "b.png", "a.json", "b.json", "a.jpg", "c.json"]
        ... )
        >>> dp0 = source_dp.groupby(group_key_fn=group_fn)
        >>> list(dp0)
        [['a.png', 'a.json', 'a.jpg'], ['b.png', 'b.json'], ['c.json']]
        >>> # A group is yielded as soon as its size equals to `group_size`
        >>> dp1 = source_dp.groupby(group_key_fn=group_fn, group_size=2)
        >>> list(dp1)
        [['a.png', 'a.json'], ['b.png', 'b.json'], ['a.jpg'], ['c.json']]
        >>> # Scenario where `buffer` is full, and group 'a' needs to be yielded since its size > `guaranteed_group_size`
        >>> dp2 = source_dp.groupby(
        ...     group_key_fn=group_fn,
        ...     buffer_size=3,
        ...     group_size=3,
        ...     guaranteed_group_size=2,
        ... )
        >>> list(dp2)
        [['a.png', 'a.json'], ['b.png', 'b.json'], ['a.jpg'], ['c.json']]
    Fi'  N)Úkeep_keyÚbuffer_sizeÚ
group_sizeÚguaranteed_group_sizeÚdrop_remainingr   Úgroup_key_fnrM   rN   rO   rP   rQ   r   c                ó‚  — t          |¦  «         || _        || _        || _        || _        t          t          ¦  «        | _        d| _        || _	        d | _
        |�'|�%d|cxk     r|k    sn t          d¦  «        ‚|| _
        |�'|�d|cxk     r|k    sn t          d¦  «        ‚|| _
        || _        t          | _        d S )Nr   z)group_size must be > 0 and <= buffer_sizezNguaranteed_group_size must be > 0 and <= group_size and group_size must be set)r   r   rR   rM   Úmax_buffer_sizer   rG   Úbuffer_elementsÚcurr_buffer_sizerO   rP   r"   rQ   r   r    )r%   r   rR   rM   rN   rO   rP   rQ   s           r   r$   zGrouperIterDataPipe.__init__Ä   sø   € õ 	˜\Ñ*Ô*Ð*à ˆŒà(ˆÔà ˆŒØ*ˆÔÝ7BÅ4Ñ7HÔ7HˆÔØ !ˆÔØ$ˆŒØ%)ˆÔ"ØÐ! kÐ&=Ø˜
Ð1Ð1Ò1Ð1 kÒ1Ð1Ð1Ð1Ý$Ð%PÑQÔQÐQØ)3ˆDÔ&Ø Ð,ØÐ!¨!Ð.CÐ*QÐ*QÒ*QÐ*QÀzÒ*QÐ*QÐ*QÐ*QÝ$Ødñô ð ð *?ˆDÔ&Ø,ˆÔÝ&ˆÔÐÐr   c                 óŠ  — d }d}d }| j         D ]<}t          | j         |         ¦  «        |k    rt          | j         |         ¦  «        }|}Œ=| j        �:|| j        k     r/| j        s(t	          dt          | j         |         ¦  «        ¦  «        ‚| j        �|| j        k    r| j         |         }| xj        |z  c_        | j         |= |S )Nr   zFailed to group items)rU   r*   rP   rQ   ÚRuntimeErrorÚstrrV   )r%   Úbiggest_keyÚbiggest_sizeÚresult_to_yieldÚfindkeys        r   Ú_remove_biggest_keyz'GrouperIterDataPipe._remove_biggest_keyè   sñ   € ØˆØˆØˆØÔ+ð 	&ð 	&ˆGÝ�4Ô'¨Ô0Ñ1Ô1°LÒ@Ð@Ý" 4Ô#7¸Ô#@ÑAÔA�Ø%�øð Ô&Ð2Ø˜tÔ9Ò9Ð9ØÔ'ð :õ Ø'­¨TÔ-AÀ+Ô-NÑ)OÔ)Oñô ð ð
 Ô&Ð.Ø˜tÔ9Ò9Ð9à"Ô2°;Ô?ˆOàÐÔ Ñ-ÐÔØÔ  Ð-àÐr   c              #   óV  K  — | j         D �]}|                      |¦  «        }| j        |                              |¦  «         | xj        dz  c_        | j        �‚| j        t          | j        |         ¦  «        k    r_|                      | j        |         ¦  «        }| j        r||fn|V — | xj        t          | j        |         ¦  «        z  c_        | j        |= | j        | j	        k    r:|  
                    ¦   «         }|�$|                      |¦  «        }| j        r||fn|V — �Œt          | j                             ¦   «         ¦  «        D ][}|                      | j                             |¦  «        ¦  «        }| xj        t          |¦  «        z  c_        | j        r||fn|V — Œ\d S )Nr.   )r   rR   rU   r)   rV   rO   r*   r    rM   rT   r^   ÚtupleÚkeysÚpop)r%   r+   ÚkeyÚresultr\   s        r   r,   zGrouperIterDataPipe.__iter__  sÖ  è è € Ø”ð 	Eñ 	EˆAØ×#Ò# AÑ&Ô&ˆCàÔ  Ô%×,Ò,¨QÑ/Ô/Ð/ØÐ!Ô! QÑ&Ð!Ô!àŒÐ*¨t¬Å#ØÔ$ SÔ)ñCô Cò 0ð 0ð *.×);Ò);¸DÔ<PÐQTÔ<UÑ)VÔ)V�Ø'+¤}Ð@�s˜F�m�m¸&Ð@Ð@Ð@ØÐ%Ô%­¨TÔ-AÀ#Ô-FÑ)GÔ)GÑGÐ%Ô%ØÔ(¨Ð-àÔ$¨Ô(<Ò<Ð<Ø"&×":Ò":Ñ"<Ô"<�Ø"Ð.Ø!×/Ò/°Ñ@Ô@�FØ+/¬=ÐD˜3 ˜-˜-¸fÐDÐDÐDùå˜Ô-×2Ò2Ñ4Ô4Ñ5Ô5ð 	=ð 	=ˆCØ×'Ò'¨Ô(<×(@Ò(@ÀÑ(EÔ(EÑFÔFˆFØÐ!Ô!¥S¨¡[¤[Ñ0Ð!Ô!Ø#'¤=Ð<�3˜�-�-°fÐ<Ð<Ð<Ð<ð	=ð 	=r   c                 óF   — d| _         t          t          ¦  «        | _        d S r(   )rV   r   rG   rU   r2   s    r   ÚresetzGrouperIterDataPipe.reset  s    € Ø !ˆÔÝ*­4Ñ0Ô0ˆÔÐÐr   c           
      óÂ   — | j         | j        | j        | j        | j        | j        | j        | j        | j        | j	        f
}t          j        �t          j        |¦  «        S |S r?   )r   rR   rM   rT   rO   rP   rQ   r    Ú_valid_iterator_idÚ_number_of_samples_yieldedr   Úgetstate_hook©r%   Ústates     r   Ú__getstate__z GrouperIterDataPipe.__getstate__#  sc   € àŒMØÔØŒMØÔ ØŒOØÔ&ØÔØÔØÔ#ØÔ+ð
ˆõ Ô%Ð1ÝÔ-¨eÑ4Ô4Ð4Øˆr   c                 óÄ   — |\
  | _         | _        | _        | _        | _        | _        | _        | _        | _        | _	        d| _
        t          t          ¦  «        | _        d S r(   )r   rR   rM   rT   rO   rP   rQ   r    rh   ri   rV   r   rG   rU   rk   s     r   Ú__setstate__z GrouperIterDataPipe.__setstate__4  sc   € ð ñ	
ØŒMØÔØŒMØÔ ØŒOØÔ&ØÔØÔØÔ#ØÔ+à !ˆÔÝ*­4Ñ0Ô0ˆÔÐÐr   c                 ó8   — | j                              ¦   «          d S r?   )rU   Úclearr2   s    r   Ú__del__zGrouperIterDataPipe.__del__D  s   € ØÔ×"Ò"Ñ$Ô$Ð$Ð$Ð$r   )r   N)r   r4   r5   r6   r   r   r   r   r9   r8   r$   r^   r,   rf   rm   ro   rr   rJ   r   r   r   r   �   s  € € € € € ð1ð 1ðp Ø Ø!%Ø,0Ø$ð"'ð "'ð "'à˜uÔ%ð"'ð  ˜w¨˜|Ô,ð"'ð
 ð"'ð ð"'ð ˜$‘Jð"'ð  # T™zð"'ð ð"'ð 
ð"'ð "'ð "'ð "'ðHð ð ð:=ð =ð =ð41ð 1ð 1ð 1ðð ð ð"1ð 1ð 1ð %ð %ð %ð %ð %ð %r   r   N)Úcollectionsr   Úcollections.abcr   r   r   Útypingr   r   r	   Ú%torch.utils.data.datapipes._decoratorr
   Ú#torch.utils.data.datapipes.datapiper   r   Ú'torch.utils.data.datapipes.utils.commonr   Ú__all__r   rY   r   r   r   r   rJ   r   r   ú<module>rz      sÌ  ðà #Ð #Ð #Ð #Ð #Ð #Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )à EÐ EÐ EÐ EÐ EÐ EØ GÐ GÐ GÐ GÐ GÐ GÐ GÐ GØ HÐ HÐ HÐ HÐ HÐ Hðð ð €ð 	ˆ� 4Ð(Ñ(Ô(€ðG�cð G˜hð Gð Gð Gð Gð Ð�WÑÔð=Yð =Yð =Yð =Yð =Y˜, yÔ1ñ =Yô =Yñ Ôð=Yð@ Ð�YÑÔð1ð 1ð 1ð 1ð 1˜Lñ 1ô 1ñ  Ôð1ðh Ð�YÑÔðu%ð u%ð u%ð u%ð u%˜, yÔ1ñ u%ô u%ñ  Ôðu%ð u%ð u%r   