ó
    "EñiÄ0  ã                   ó  • S SK Jr  S SKJrJrJr  S SKJrJrJ	r	  S SK
Jr  S SKJrJr  S SKJr  / SQr\	" SS	S
9rS\S\4S jr\" S5       " S S\\   5      5       r\" S5       " S S\5      5       r\" S5       " S S\\   5      5       rg)é    )Údefaultdict)ÚCallableÚIteratorÚSized)ÚAnyÚNoReturnÚTypeVar)Úfunctional_datapipe)Ú	DataChunkÚIterDataPipe)Ú_check_unpickable_fn)ÚBatcherIterDataPipeÚGrouperIterDataPipeÚUnBatcherIterDataPipeÚ_T_coT)Ú	covariantÚnameÚreturnc                 ó,   • [        S[         SU  35      e)Nzmodule z has no attribute )ÚAttributeErrorÚ__name__)r   s    Úe/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/utils/data/datapipes/iter/grouping.pyÚ__getattr__r      s   € Ü
˜7¤8 *Ð,>¸t¸fÐEÓ
FÐFó    Úbatchc                   ó”   ^ • \ rS rSr% Sr\\S'   \\S'   \\S'   S\	4S\S\S\S\
\	   SS	4
U 4S
 jjjrS\\	   4S jrS\4S jrSrU =r$ )r   é   aî  
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                 ót   >• US::  a  [        S5      e[        TU ]	  5         Xl        X l        X0l        X@l        g )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%   ÚBatcherIterDataPipe.__init__5   s8   ø€ ð ˜‹?Ü Ð!NÓOÐOÜ‰ÑÔØ ŒØ$ŒØ"ŒØ*Õr   c              #   ó"  #   • / nU R                    HD  nUR                  U5        [        U5      U R                  :X  d  M/  U R	                  U5      v •  / nMF     [        U5      S:”  a&  U R
                  (       d  U R	                  U5      v •  g g g 7f©Nr   )r   ÚappendÚlenr   r!   r    )r&   r   Úxs      r   Ú__iter__ÚBatcherIterDataPipe.__iter__D   sx   é € ØˆØ—”ˆAØ�L‰L˜ŒOÜ�5‹z˜TŸ_™_Õ,Ø×(Ñ(¨Ó/Ò/Ø’ñ	 ô
 ˆu‹:˜‹>Ø—>—>Ø×(Ñ(¨Ó/Ó/ð "ð ùs   ‚:BÁ ABc                 óL  • [        U R                  [        5      (       ae  U R                  (       a"  [	        U R                  5      U R
                  -  $ [	        U R                  5      U R
                  -   S-
  U R
                  -  $ [        [        U 5      R                   S35      e)Né   z# instance doesn't have valid length)	Ú
isinstancer   r   r    r,   r   Ú	TypeErrorÚtyper   ©r&   s    r   Ú__len__ÚBatcherIterDataPipe.__len__O   sv   € ä�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   r   r    r!   )r   Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Ú__annotations__ÚintÚboolr   r4   r%   r   r.   r6   Ú__static_attributes__Ú__classcell__)r'   s   @r   r   r      s…   ø‡ ñð, ÓØƒOØƒOð  Ø)2ñ+àð+ð ð+ð ð	+ð
 ˜I‘ð+ð 
÷+ð +ð	0˜( 9Ñ-ô 	0ðY˜÷ Yò Yr   r   Úunbatchc                   ó>   • \ rS rSrSrSS\S\SS4S jjrS rS	 r	S
r
g)r   éZ   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_levelr   Nc                 ó   • Xl         X l        g ©N©r   rD   )r&   r   rD   s      r   r%   ÚUnBatcherIterDataPipe.__init__r   s   € Ø ŒØ*Õr   c              #   óx   #   • U R                    H$  nU R                  XR                  S9 S h  v•N   M&     g  N	7f)N©rD   )r   Ú_diverD   )r&   Úelements     r   r.   ÚUnBatcherIterDataPipe.__iter__v   s0   é € Ø—}”}ˆGØ—z‘z '×9KÑ9K�zÐL×LÒLò %ÙLùs   ‚,:®8¯
:c              #   óŠ  #   • US:  a  [        S5      eUS:X  aB  [        U[        [        45      (       a"  U H  nU R	                  USS9 S h  v•N   M     g Uv •  g US:X  a  Uv •  g [        U[        [        45      (       a$  U H  nU R	                  X2S-
  S9 S h  v•N   M     g [        SU R                   S35      e Nq N$7f)Néÿÿÿÿz unbatch_level must be -1 or >= 0rJ   r   r1   zunbatch_level z" exceeds the depth of the DataPipe)Ú
ValueErrorr2   Úlistr   rK   Ú
IndexErrorrD   )r&   rL   rD   Úitems       r   rK   ÚUnBatcherIterDataPipe._divez   sË   é € Ø˜2ÓÜÐ?Ó@Ð@Ø˜BÓÜ˜'¤D¬)Ð#4×5Ñ5Û#�DØ#Ÿz™z¨$¸b˜zÐA×AÒAò $ð “Ø˜aÓØ‹Mä˜'¤D¬)Ð#4×5Ñ5Û#�DØ#Ÿz™z¨$ÈaÑ>O˜zÐP×PÒPò $ô !Ø$ T×%7Ñ%7Ð$8Ð8ZÐ[óð ñ Bñ Qùs%   ‚ACÁB?ÁACÂCÂ#CÃCrG   )r1   )r   r8   r9   r:   r;   r   r=   r%   r.   rK   r?   © r   r   r   r   Z   s,   † ññ,+ ð +¸cð +È$õ +òMõr   r   Úgroupbyc                   óœ   • \ rS rSrSrSSSSSS.S\\   S\\/\4   S	\	S
\
S\
S-  S\
S-  S\	SS4S jjrS rS rSS jrS rS rSS jrSrg)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_fnrY   rZ   r[   r\   r]   r   c                óT  • [        U5        Xl        X l        X0l        X@l        [        [        5      U l        SU l        XPl	        S U l
        Ub#  Ub   SUs=:  a  U::  d  O  [        S5      eXPl
        Ub#  Ub  SUs=:  a  U::  d  O  [        S5      eX`l
        Xpl        [        U l        g )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   r^   rY   Úmax_buffer_sizer   rQ   Úbuffer_elementsÚcurr_buffer_sizer[   r\   r#   r]   r   r!   )r&   r   r^   rY   rZ   r[   r\   r]   s           r   r%   ÚGrouperIterDataPipe.__init__Ä   s­   € ô 	˜\Ô*à Œà(Ôà ŒØ*ÔÜ7BÄ4Ó7HˆÔØ !ˆÔØ$ŒØ%)ˆÔ"ØÑ! kÑ&=Ø˜
Õ1 kÕ1Ü$Ð%PÓQÐQà)3Ô&Ø Ñ,ØÑ!¨!Ð.CÕ*QÀzÕ*QÜ$Ødóð ð *?Ô&Ø,ÔÜ&ˆÕr   c                 óà  • S nSnS nU R                    H;  n[        U R                   U   5      U:”  d  M!  [        U R                   U   5      nUnM=     U R                  bB  X R                  :  a3  U R                  (       d"  [	        S[        U R                   U   5      5      eU R                  b  X R                  :¼  a  U R                   U   nU =R                  U-  sl        U R                   U	 U$ )Nr   zFailed to group items)ra   r,   r\   r]   ÚRuntimeErrorÚstrrb   )r&   Úbiggest_keyÚbiggest_sizeÚresult_to_yieldÚfindkeys        r   Ú_remove_biggest_keyÚ'GrouperIterDataPipe._remove_biggest_keyê   sç   € ØˆØˆØˆØ×+Ô+ˆGÜ�4×'Ñ'¨Ñ0Ó1°LÕ@Ü" 4×#7Ñ#7¸Ñ#@ÓA�Ø%’ñ ,ð ×&Ñ&Ñ2Ø×9Ñ9Ó9Ø×'×'äØ'¬¨T×-AÑ-AÀ+Ñ-NÓ)Oóð ð
 ×&Ñ&Ñ.Ø×9Ñ9Ó9à"×2Ñ2°;Ñ?ˆOà×Ò Ñ-ÕØ× Ñ  Ð-àÐr   c              #   óÌ  #   • U R                    GHE  nU R                  U5      nU R                  U   R                  U5        U =R                  S-  sl        U R
                  b”  U R
                  [        U R                  U   5      :X  an  U R                  U R                  U   5      nU R                  (       a  X#4OUv •  U =R                  [        U R                  U   5      -  sl        U R                  U	 U R                  U R                  :X  d  GM  U R                  5       nUc  GM  U R                  U5      nU R                  (       a  X#4OUv •  GMH     [        U R                  R                  5       5       Hc  nU R                  U R                  R                  U5      5      nU =R                  [        U5      -  sl        U R                  (       a  X#4OUv •  Me     g 7f)Nr1   )r   r^   ra   r+   rb   r[   r,   r!   rY   r`   rk   ÚtupleÚkeysÚpop)r&   r-   ÚkeyÚresultri   s        r   r.   ÚGrouperIterDataPipe.__iter__  sƒ  é € Ø—•ˆAØ×#Ñ# AÓ&ˆCà× Ñ  Ñ%×,Ñ,¨QÔ/Ø×!Ò! QÑ&Õ!à�‰Ñ*¨t¯©Ä#Ø×$Ñ$ SÑ)óCó 0ð *.×);Ñ);¸D×<PÑ<PÐQTÑ<UÓ)V�Ø'+§}§}�s‘m¸&Ò@Ø×%Ò%¬¨T×-AÑ-AÀ#Ñ-FÓ)GÑGÕ%Ø×(Ñ(¨Ð-à×$Ñ$¨×(<Ñ(<Ö<Ø"&×":Ñ":Ó"<�Ø"Ô.Ø!×/Ñ/°Ó@�FØ+/¯=¯=˜3™-¸fÕDñ% ô( ˜×-Ñ-×2Ñ2Ó4Ö5ˆCØ×'Ñ'¨×(<Ñ(<×(@Ñ(@ÀÓ(EÓFˆFØ×!Ò!¤S¨£[Ñ0Õ!Ø#'§=§=�3‘-°fÔ<ò 6ùs   ‚DG$ÄG$Ä+B9G$c                 ó:   • SU l         [        [        5      U l        g r*   )rb   r   rQ   ra   r5   s    r   ÚresetÚGrouperIterDataPipe.reset!  s   € Ø !ˆÔÜ*¬4Ó0ˆÕr   c           
      ó4  • U R                   U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  4
n[        R                  b  [        R                  " U5      $ U$ rF   )r   r^   rY   r`   r[   r\   r]   r!   Ú_valid_iterator_idÚ_number_of_samples_yieldedr   Úgetstate_hook©r&   Ústates     r   Ú__getstate__Ú GrouperIterDataPipe.__getstate__%  s�   € à�M‰MØ×ÑØ�M‰MØ× Ñ Ø�O‰OØ×&Ñ&Ø×ÑØ×ÑØ×#Ñ#Ø×+Ñ+ð
ˆô ×%Ñ%Ñ1Ü×-Ò-¨eÓ4Ð4Øˆr   c                 ó¸   • Uu
  U l         U l        U l        U l        U l        U l        U l        U l        U l        U l	        SU l
        [        [        5      U l        g r*   )r   r^   rY   r`   r[   r\   r]   r!   rx   ry   rb   r   rQ   ra   r{   s     r   Ú__setstate__Ú GrouperIterDataPipe.__setstate__6  sZ   € ð ñ	
ØŒMØÔØŒMØÔ ØŒOØÔ&ØÔØÔØÔ#ØÔ+à !ˆÔÜ*¬4Ó0ˆÕr   c                 ó8   • U R                   R                  5         g rF   )ra   Úclearr5   s    r   Ú__del__ÚGrouperIterDataPipe.__del__F  s   € Ø×Ñ×"Ñ"Õ$r   )ry   rx   ra   rb   r   r]   r^   r[   r\   rY   r`   r!   )r   N)r   r8   r9   r:   r;   r   r   r   r   r>   r=   r%   rk   r.   ru   r}   r€   r„   r?   rU   r   r   r   r   �   s£   † ñ1ðp Ø Ø!%Ø,0Ø$ò$'à˜uÑ%ð$'ð  ˜w¨˜|Ñ,ð$'ð
 ð$'ð ð$'ð ˜$‘Jð$'ð  # T™zð$'ð ð$'ð 
õ$'òLò:=ô4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   rf   r   r   r   r   rU   r   r   Ú<module>r�      sº   ðå #ß 5Ñ 5ß )Ñ )å Eß GÝ Hò€ñ 	� 4Ñ(€ðG�cð G˜hô Gñ �WÓô=Y˜, yÑ1ó =Yó ð=Yñ@ �YÓô1˜Ló 1ó  ð1ñh �YÓôw%˜, yÑ1ó w%ó  ñw%r   