Ë
    Z^(hB0  ã                   ó(  — d dl Z d dlmZ d dlmZmZ d dlmZmZm	Z	m
Z
 d dl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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y)é    N)Údefaultdict)ÚIteratorÚSized)ÚAnyÚCallableÚOptionalÚTypeVar)Úfunctional_datapipe)Ú	DataChunkÚIterDataPipe)Ú_check_unpickable_fn)ÚBatcherIterDataPipeÚGrouperIterDataPipeÚUnBatcherIterDataPipeÚ_T_coT)Ú	covariantÚnamec                 óþ   — | dv ret        j                  d| › d| › d�t        d¬«       t        t        j
                  j                  j                  j                  j                  | «      S t        dt        › d| › �«      ‚)	N)ÚSHARDING_PRIORITIESÚShardingFilterIterDataPipeú`zc` from `torch.utils.data.datapipes.iter.grouping` is going to be removed in PyTorch 2.1Please use `z5` from the `torch.utils.data.datapipes.iter.sharding`é   )ÚcategoryÚ
stacklevelzmodule z has no attribute )ÚwarningsÚwarnÚFutureWarningÚgetattrÚtorchÚutilsÚdataÚ	datapipesÚiterÚshardingÚAttributeErrorÚ__name__)r   s    úf/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/utils/data/datapipes/iter/grouping.pyÚ__getattr__r(      s{   € ØÐDÑDÜ�‰Ø�ˆvð Ø˜&Ð UðWä"Øõ		
ô ”u—{‘{×'Ñ'×1Ñ1×6Ñ6×?Ñ?ÀÓFÐFä
˜7¤8 *Ð,>¸t¸fÐEÓ
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_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        €r'   r3   zBatcherIterDataPipe.__init__A   s?   ø€ ð ˜AŠ~ÐLÐLÓLˆ~Ü‰ÑÔØ ˆŒØ$ˆŒØ"ˆŒØ*ˆÕr)   c              #   ó  K  — g }| j                   D ]A  }|j                  |«       t        |«      | j                  k(  sŒ-| j	                  |«      –— g }ŒC t        |«      dkD  r!| j
                  s| j	                  |«      –— y y y ­w©Nr   )r,   ÚappendÚlenr-   r/   r.   )r4   r*   Úxs      r'   Ú__iter__zBatcherIterDataPipe.__iter__O   s~   è ø€ ØˆØ—‘ò 	ˆAØ�L‰L˜ŒOÜ�5‹z˜TŸ_™_Ó,Ø×(Ñ(¨Ó/Ò/Ø‘ð		ô
 ˆu‹:˜Š>Ø—>’>Ø×(Ñ(¨Ó/Ó/ð "ð ùs   ‚;B¾AB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�«      ‚)Né   z# instance doesn't have valid length)	Ú
isinstancer,   r   r.   r9   r-   Ú	TypeErrorÚtyper&   ©r4   s    r'   Ú__len__zBatcherIterDataPipe.__len__Z   ss   € Ü�d—m‘m¤UÔ+Ø�~Š~Ü˜4Ÿ=™=Ó)¨T¯_©_Ñ<Ð<ä˜DŸM™MÓ*¨T¯_©_Ñ<¸qÑ@ÀTÇ_Á_ÑTÐTäœt D›z×2Ñ2Ð3Ð3VÐWÓXÐXr)   )r&   Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__ÚintÚboolr   r@   r3   r   r;   rB   Ú__classcell__)r5   s   @r'   r   r   %   sz   ø… ñð, ÓØƒOØƒOð  Ø)2ñ+àð+ð ð+ð ð	+ð
 ˜I‘ð+ð 
õ+ð	0˜( 9Ñ-ó 	0ðY˜÷ Yr)   r   Úunbatchc                   ó.   — e Zd ZdZddedefd„Zd„ Zd„ Zy)	r   a   
    Undos 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,   Úunbatch_levelc                 ó    — || _         || _        y ©N)r,   rL   )r4   r,   rL   s      r'   r3   zUnBatcherIterDataPipe.__init__|   s   € Ø ˆŒØ*ˆÕr)   c              #   ó|   K  — | j                   D ]'  }| j                  || j                  ¬«      E d {  –—†  Œ) y 7 Œ­w)N©rL   )r,   Ú_diverL   )r4   Úelements     r'   r;   zUnBatcherIterDataPipe.__iter__€   s:   è ø€ Ø—}‘}ò 	MˆGØ—z‘z '¸×9KÑ9K�zÓL×LÑLñ	MØLús   ‚0<²:³<c              #   óv  K  — |dk  rt        d«      ‚|dk(  r>t        |t        t        f«      r#|D ]  }| j	                  |d¬«      E d {  –—†  Œ y |–— y |dk(  r|–— y t        |t        t        f«      r&|D ]   }| j	                  ||dz
  ¬«      E d {  –—†  Œ" y t        d| j                  › d�«      ‚7 Œk7 Œ"­w)Néÿÿÿÿz unbatch_level must be -1 or >= 0rP   r   r=   zunbatch_level z" exceeds the depth of the DataPipe)Ú
ValueErrorr>   Úlistr   rQ   Ú
IndexErrorrL   )r4   rR   rL   Úitems       r'   rQ   zUnBatcherIterDataPipe._dive„   s×   è ø€ Ø˜2ÒÜÐ?Ó@Ð@Ø˜BÒÜ˜'¤D¬)Ð#4Ô5Ø#ò B�DØ#Ÿz™z¨$¸b˜zÓA×AÑAñBð “Ø˜aÒØ‹Mä˜'¤D¬)Ð#4Ô5Ø#ò Q�DØ#Ÿz™z¨$¸mÈaÑ>O˜zÓP×PÑPñQô !Ø$ T×%7Ñ%7Ð$8Ð8ZÐ[óð ð Bøð Qús%   ‚AB9Á	B5Á
A
B9ÂB7Â!B9Â7B9N)r=   )	r&   rC   rD   rE   r   rG   r3   r;   rQ   © r)   r'   r   r   d   s%   „ ññ,+ ð +¸có +ò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
e	   de
e	   defd„Zd„ Zd„ Zdd„Zd„ Zd„ Zd„ Zy)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_fnr\   r]   r^   r_   r`   c                ó4  — t        |«       || _        || _        || _        || _        t        t        «      | _        d| _        || _	        d | _
        |�|�d|cxk  r|k  sJ ‚ J ‚|| _
        |�|�d|cxk  r|k  sJ ‚ J ‚|| _
        || _        t        | _        y r7   )r   r,   ra   r\   Úmax_buffer_sizer   rV   Úbuffer_elementsÚcurr_buffer_sizer^   r_   r`   r   r/   )r4   r,   ra   r\   r]   r^   r_   r`   s           r'   r3   zGrouperIterDataPipe.__init__Ç   sº   € ô 	˜\Ô*Ø ˆŒØ(ˆÔà ˆŒØ*ˆÔÜ7BÄ4Ó7HˆÔØ !ˆÔØ$ˆŒØ%)ˆÔ"ØÐ! kÐ&=Ø�zÔ0 [Ò0Ð0Ñ0Ð0Ð0Ø)3ˆDÔ&Ø Ð,ØÐ)¨aÐ2GÔ.UÈ:Ò.UÐUÐUÐ.UÐUÐUØ)>ˆDÔ&Ø,ˆÔÜ&ˆÕr)   c                 óæ  — d }d}d }| j                   j                  «       D ]8  }t        | j                   |   «      |kD  sŒ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)rd   Úkeysr9   r_   r`   ÚRuntimeErrorÚstrre   )r4   Úbiggest_keyÚbiggest_sizeÚresult_to_yieldÚfindkeys        r'   Ú_remove_biggest_keyz'GrouperIterDataPipe._remove_biggest_keyå   sù   € ØˆØˆØˆØ×+Ñ+×0Ñ0Ó2ò 	&ˆGÜ�4×'Ñ'¨Ñ0Ó1°LÓ@Ü" 4×#7Ñ#7¸Ñ#@ÓA�Ø%‘ð	&ð ×&Ñ&Ð2Ø˜t×9Ñ9Ò9Ø×'Ò'äØ'¬¨T×-AÑ-AÀ+Ñ-NÓ)Oóð ð
 ×&Ñ&Ð.Ø˜t×9Ñ9Ò9à"×2Ñ2°;Ñ?ˆOà×Ò Ñ-ÕØ× Ñ  Ð-àÐr)   c              #   óž  K  — | j                   D �]5  }| j                  |«      }| j                  |   j                  |«       | xj                  dz  c_        | j
                  ��| j
                  t        | j                  |   «      k(  rj| j                  | j                  |   «      }| j                  r||fn|–— | xj                  t        | j                  |   «      z  c_        | j                  |= | j                  | j                  k(  sŒý| j                  «       }|€�Œ| j                  |«      }| j                  r||fn|–— �Œ8 t        | j                  j                  «       «      D ]^  }| j                  | j                  j                  |«      «      }| xj                  t        |«      z  c_        | j                  r||fn|–— Œ` y ­w)Nr=   )r,   ra   rd   r8   re   r^   r9   r/   r\   rc   rn   Útuplerg   Úpop)r4   r:   ÚkeyÚresultrl   s        r'   r;   zGrouperIterDataPipe.__iter__  s–  è ø€ Ø—‘ó 	EˆAØ×#Ñ# AÓ&ˆCà× Ñ  Ñ%×,Ñ,¨QÔ/Ø×!Ò! QÑ&Õ!à�‰Ð*¨t¯©Ä#Ø×$Ñ$ SÑ)óCò 0ð *.×);Ñ);¸D×<PÑ<PÐQTÑ<UÓ)V�Ø'+§}¢}�s˜F‘m¸&Ò@Ø×%Ò%¬¨T×-AÑ-AÀ#Ñ-FÓ)GÑGÕ%Ø×(Ñ(¨Ð-à×$Ñ$¨×(<Ñ(<Ó<Ø"&×":Ñ":Ó"<�Ø"Ò.Ø!×/Ñ/°Ó@�FØ+/¯=ª=˜3 ™-¸fÔDð%	Eô( ˜×-Ñ-×2Ñ2Ó4Ó5ò 	=ˆCØ×'Ñ'¨×(<Ñ(<×(@Ñ(@ÀÓ(EÓFˆFØ×!Ò!¤S¨£[Ñ0Õ!Ø#'§=¢=�3˜‘-°fÓ<ñ	=ùs   ‚D	GÄGÄ B-Gc                 ó:   — d| _         t        t        «      | _        y r7   )re   r   rV   rd   rA   s    r'   ÚresetzGrouperIterDataPipe.reset  s   € Ø !ˆÔÜ*¬4Ó0ˆÕr)   c           
      ó0  — | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  f
}t        j                  �t        j                  |«      S |S rN   )r,   ra   r\   rc   r^   r_   r`   r/   Ú_valid_iterator_idÚ_number_of_samples_yieldedr   Úgetstate_hook©r4   Ústates     r'   Ú__getstate__z GrouperIterDataPipe.__getstate__   s�   € à�M‰MØ×ÑØ�M‰MØ× Ñ Ø�O‰OØ×&Ñ&Ø×ÑØ×ÑØ×#Ñ#Ø×+Ñ+ð
ˆô ×%Ñ%Ð1Ü×-Ñ-¨eÓ4Ð4Øˆr)   c                 ó¸   — |\
  | _         | _        | _        | _        | _        | _        | _        | _        | _        | _	        d| _
        t        t        «      | _        y r7   )r,   ra   r\   rc   r^   r_   r`   r/   rw   rx   re   r   rV   rd   rz   s     r'   Ú__setstate__z GrouperIterDataPipe.__setstate__1  sZ   € ð ñ	
ØŒMØÔØŒMØÔ ØŒOØÔ&ØÔØÔØÔ#ØÔ+à !ˆÔÜ*¬4Ó0ˆÕr)   c                 ó8   — | j                   j                  «        y rN   )rd   ÚclearrA   s    r'   Ú__del__zGrouperIterDataPipe.__del__A  s   € Ø×Ñ×"Ñ"Õ$r)   )r0   N)r&   rC   rD   rE   r   r   r   r   rH   rG   r   r3   rn   r;   ru   r|   r~   r�   rY   r)   r'   r   r   ™   s˜   „ ñ*ðb Ø Ø$(Ø/3Ø$ò'à˜uÑ%ð'ð  ˜w¨˜|Ñ,ð'ð
 ð'ð ð'ð ˜S‘Mð'ð  (¨™}ð'ð ó'ò<ò:=ó41òò"1ó %r)   r   )r   Úcollectionsr   Úcollections.abcr   r   Útypingr   r   r   r	   Ú(torch.utils.data.datapipes.iter.shardingr   Ú%torch.utils.data.datapipes._decoratorr
   Ú#torch.utils.data.datapipes.datapiper   r   Ú'torch.utils.data.datapipes.utils.commonr   Ú__all__r   ri   r(   r   r   r   rY   r)   r'   ú<module>rŠ      sµ   ðã Ý #ß +ß 3Ó 3ã /Ý Eß GÝ Hò€ñ 	� 4Ô(€ðG�có Gñ �WÓô;Y˜, yÑ1ó ;Yó ð;Yñ| �YÓô1˜Ló 1ó  ð1ñh �YÓôh%˜, yÑ1ó h%ó  ñh%r)   