Ë
    Z^(h¶j  ã                   óö  — d dl Zd dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZmZmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ g d
¢Z edd¬«      Z ed«       G d„ de«      «       Z ed«       G d„ de«      «       Z G d„ de«      Zd„ Z G d„ dee«      Z  G d„ de«      Z! ed«       G d„ de«      «       Z" G d„ dee«      Z# ed «       G d!„ d"e«      «       Z$ ed#«       G d$„ d%ee%e      «      «       Z&y)&é    N)ÚABCÚabstractmethod)Údeque)ÚIteratorÚSized)ÚAnyÚCallableÚLiteralÚOptionalÚTypeVar)Úfunctional_datapipe)Ú_SnapshotState)ÚIterDataPipe)Ú_check_unpickable_fnÚStreamWrapper)ÚConcaterIterDataPipeÚDemultiplexerIterDataPipeÚForkerIterDataPipeÚMultiplexerIterDataPipeÚZipperIterDataPipeÚ_T_coT)Ú	covariantÚconcatc                   óF   — e Zd ZU dZee   ed<   defd„Zdefd„Z	de
fd„Zy)r   aN  
    Concatenates multiple Iterable DataPipes (functional name: ``concat``).

    The resulting DataPipe will yield all the elements from the first input DataPipe, before yielding from the subsequent ones.

    Args:
        datapipes: Iterable DataPipes being concatenated

    Example:
        >>> # xdoctest: +REQUIRES(module:torchdata)
        >>> import random
        >>> from torchdata.datapipes.iter import IterableWrapper
        >>> dp1 = IterableWrapper(range(3))
        >>> dp2 = IterableWrapper(range(5))
        >>> list(dp1.concat(dp2))
        [0, 1, 2, 0, 1, 2, 3, 4]
    Ú	datapipesc                 ó~   — t        |«      dk(  rt        d«      ‚t        d„ |D «       «      st        d«      ‚|| _        y )Nr   z/Expected at least one DataPipe, but got nothingc              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­w©N©Ú
isinstancer   ©Ú.0Údps     úg/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/utils/data/datapipes/iter/combining.pyú	<genexpr>z0ConcaterIterDataPipe.__init__.<locals>.<genexpr>4   ó   è ø€ ÒD°B”:˜b¤,×/ÑDùó   ‚z(Expected all inputs to be `IterDataPipe`)ÚlenÚ
ValueErrorÚallÚ	TypeErrorr   ©Úselfr   s     r$   Ú__init__zConcaterIterDataPipe.__init__1   s;   € Üˆy‹>˜QÒÜÐNÓOÐOÜÑD¸)ÔDÔDÜÐFÓGÐGØ"ˆ�ó    Úreturnc              #   óF   K  — | j                   D ]  }|E d {  –—†  Œ y 7 Œ­wr   )r   )r-   r#   s     r$   Ú__iter__zConcaterIterDataPipe.__iter__8   s#   è ø€ Ø—.‘.ò 	ˆBØ�M‰Mñ	Øús   ‚!—˜!c                 ó´   — t        d„ | j                  D «       «      rt        d„ | j                  D «       «      S t        t	        | «      j
                  › d�«      ‚)Nc              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­wr   ©r    r   r!   s     r$   r%   z/ConcaterIterDataPipe.__len__.<locals>.<genexpr>=   ó   è ø€ Ò>¨Œz˜"œe×$Ñ>ùr'   c              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr   ©r(   r!   s     r$   r%   z/ConcaterIterDataPipe.__len__.<locals>.<genexpr>>   ó   è ø€ Ò8 2”s˜2—wÑ8ùó   ‚ú# instance doesn't have valid length)r*   r   Úsumr+   ÚtypeÚ__name__©r-   s    r$   Ú__len__zConcaterIterDataPipe.__len__<   óG   € ÜÑ>¨t¯~©~Ô>Ô>ÜÑ8¨¯©Ô8Ó8Ð8äœt D›z×2Ñ2Ð3Ð3VÐWÓXÐXr/   N)r>   Ú
__module__Ú__qualname__Ú__doc__Útupler   Ú__annotations__r.   r   r2   Úintr@   © r/   r$   r   r      s:   … ñð$ �\Ñ"Ó"ð# <ó #ð˜(ó ðY˜ô Yr/   r   Úforkc                   ó:   — e Zd ZdZ	 	 d	dedededeed      fd„Zy)
r   aÝ  
    Creates multiple instances of the same Iterable DataPipe (functional name: ``fork``).

    Args:
        datapipe: Iterable DataPipe being copied
        num_instances: number of instances of the datapipe to create
        buffer_size: this restricts how far ahead the leading child DataPipe
           can read relative to the slowest child DataPipe.
           Defaults to ``1000``. Use ``-1`` for the unlimited buffer.
        copy: copy strategy to use for items yielded by each branch. Supported
            options are ``None`` for no copying, ``"shallow"`` for shallow object
            copies, and ``"deep"`` for deep object copies. Defaults to ``None``.

    Note:
        All branches of the forked pipeline return the identical object unless
        the copy parameter is supplied. If the object is mutable or contains
        mutable objects, changing them in one branch will affect all others.

    Example:
        >>> # xdoctest: +REQUIRES(module:torchdata)
        >>> from torchdata.datapipes.iter import IterableWrapper
        >>> source_dp = IterableWrapper(range(5))
        >>> dp1, dp2 = source_dp.fork(num_instances=2)
        >>> list(dp1)
        [0, 1, 2, 3, 4]
        >>> list(dp2)
        [0, 1, 2, 3, 4]
    NÚdatapipeÚnum_instancesÚbuffer_sizeÚcopy©ÚshallowÚdeepc                 ó¤   — |dk  rt        d|› d�«      ‚|dk(  r|S t        ||||«      }t        |«      D �cg c]  }t        ||«      ‘Œ c}S c c}w ©Né   z,Expected `num_instances` larger than 0, but z	 is found)r)   Ú_ForkerIterDataPipeÚrangeÚ_ChildDataPipe)ÚclsrK   rL   rM   rN   Ú	containerÚis          r$   Ú__new__zForkerIterDataPipe.__new__b   sd   € ð ˜1ÒÜØ>¸}¸oÈYÐWóð ð ˜AÒØˆOÜ'¨°-ÀÈdÓSˆ	Ü6;¸MÓ6JÖK°”˜y¨!Õ,ÒKÐKùÒKs   ·A©éè  N)	r>   rB   rC   rD   r   rG   r   r
   r[   rH   r/   r$   r   r   C   sK   „ ñðB  Ø59ñLàðLð ðLð ð	Lð
 �wÐ0Ñ1Ñ2ôLr/   r   c                   ód   — e Zd ZdZedefd„«       Zedefd„«       Zed	d„«       Z	edefd„«       Z
y)
Ú_ContainerTemplatezVAbstract class for container ``DataPipes``. The followings are three required methods.Úinstance_idc                  ó   — y r   rH   ©r-   r`   s     r$   Úget_next_element_by_instancez/_ContainerTemplate.get_next_element_by_instancev   ó   € àr/   r0   c                  ó   — y r   rH   r?   s    r$   Úis_every_instance_exhaustedz._ContainerTemplate.is_every_instance_exhaustedz   rd   r/   Nc                  ó   — y r   rH   r?   s    r$   Úresetz_ContainerTemplate.reset~   rd   r/   c                  ó   — y)zSRaise TypeError if it's not supposed to be implemented to support `list(datapipe)`.NrH   rb   s     r$   Úget_length_by_instancez)_ContainerTemplate.get_length_by_instance‚   s   � r/   ©r0   N)r>   rB   rC   rD   r   rG   rc   Úboolrf   rh   rj   rH   r/   r$   r_   r_   s   sk   „ Ùaàð¸ò ó ðð ð¨Tò ó ðð òó ðð ðc°#ò có ñcr/   r_   c                 ó   — | S r   rH   )Úxs    r$   Ú_no_opro   ‡   s   € Ø€Hr/   c                   óˆ   — e Zd ZdZ	 	 ddedededeed      fd„Zd	„ Z	d
efd„Z
defd„Zd
edefd„Zdd„Zd„ Zd„ Zd„ Zd„ Zy)rU   zä
    Container to hold instance-specific information on behalf of ForkerIterDataPipe.

    It tracks the state of its child DataPipes, maintains the buffer, and yields the next value
    as requested by the child DataPipes.
    NrK   rL   rM   rN   rO   c                 óØ  — || _         d | _        || _        t        «       | _        || _        | j
                  dk  rt        j                  dt        «       |€t        | _
        nE|dk(  rt        j                  | _
        n*|dk(  rt        j                  | _
        nt        d|› d�«      ‚dg|z  | _        d| _        d| _        d | _        t'        |«      D �cg c]  }d‘Œ c}| _        y c c}w )Nr   zPUnlimited buffer size is set for `fork`, please be aware of OOM at random placesrP   rQ   zUnknown copy method `z5` requested, choose one of None, `shallow` or `deep`.T)Úmain_datapipeÚ_datapipe_iteratorrL   r   ÚbufferrM   ÚwarningsÚwarnÚUserWarningro   Úcopy_fnÚ
copymodulerN   Údeepcopyr)   Úchild_pointersÚslowest_ptrÚleading_ptrÚend_ptrrV   Ú_child_stop)r-   rK   rL   rM   rN   Ú_s         r$   r.   z_ForkerIterDataPipe.__init__“   sé   € ð &ˆÔØ;?ˆÔØ*ˆÔÜ"›WˆŒØ&ˆÔØ×Ñ˜aÒÜ�M‰Mð:äôð
 ˆ<Ü!ˆD�LØ�YÒÜ%Ÿ?™?ˆD�LØ�VŠ^Ü%×.Ñ.ˆD�LäØ'¨ vÐ-bÐcóð ð
 ð*
àñ*ˆÔð ˆÔØˆÔØ&*ˆŒÜ6;¸MÓ6JÖ'K°ªÒ'KˆÕùÒ'Ks   Ã	C'c                 ó,   — t        | j                  «      S r   ©r(   rr   r?   s    r$   r@   z_ForkerIterDataPipe.__len__¸   ó   € Ü�4×%Ñ%Ó&Ð&r/   r`   c              #   ó  K  — | j                   €g| j                  |   rXt        | j                  «      | _         t        j
                  | _        t        | j                  «      D ]  }d| j                  |<   Œ 	 | j                  |   �sÅ| j                  |xx   dz  cc<   | j                  �-| j                  |   | j                  k(  rd| j                  |<   �nu| j                  rK| j                  |   | j                  k  r/| j                  |   | j                  z
  dz
  }| j                  |   }nE| j                  |   | _        	 t        | j                   «      }| j                  j                  |«       | j                  |   | j                  dz   k(  rEt#        | j                  «      }| j                  |k  r!|| _        | j                  j%                  «        | j&                  dk\  rB| j                  | j&                  | j                  z   kD  rt)        dd| j&                  › d�z   «      ‚| j+                  |«      –— | j                  |   s�ŒÅd| j                  |<   t-        | j                  «      rd | _         | j/                  «        y y # t         $ r+ d| j                  |<   d | _         | j                  | _
        Y �ŒEw xY w# d| j                  |<   t-        | j                  «      rd | _         | j/                  «        w w xY w­w)NFrT   Tr   z#ForkerIterDataPipe buffer overflow,zbuffer size ú is insufficient.)rs   r   Úiterrr   r   Ú	IteratingÚ_snapshot_staterV   rL   r{   r~   rt   r}   r|   ÚnextÚappendÚStopIterationÚminÚpopleftrM   ÚBufferErrorrx   r*   Ú_cleanup)r-   r`   rZ   ÚidxÚ
return_valÚnew_mins         r$   rc   z0_ForkerIterDataPipe.get_next_element_by_instance»   sÉ  è ø€ Ø×"Ñ"Ð*¨t×/?Ñ/?ÀÒ/LÜ&*¨4×+=Ñ+=Ó&>ˆDÔ#Ü#1×#;Ñ#;ˆDÔ Ü˜4×-Ñ-Ó.ò ,�Ø&+�× Ñ  Ò#ð,ð-	 Ø×&Ñ& {Ó3Ø×#Ñ# KÓ0°AÑ5Ó0à—L‘LÐ,Ø×+Ñ+¨KÑ8¸D¿L¹LÒHà48�D×$Ñ$ [Ñ1Ùà—;’; 4×#6Ñ#6°{Ñ#CÀt×GWÑGWÒ#WØ×-Ñ-¨kÑ:¸T×=MÑ=MÑMÐPQÑQ�CØ!%§¡¨SÑ!1‘Jà'+×':Ñ':¸;Ñ'G�DÔ$ð!Ü%)¨$×*AÑ*AÓ%B˜
ØŸ™×*Ñ*¨:Ô6ð ×&Ñ& {Ñ3°t×7GÑ7GÈ!Ñ7KÒKÜ!Ø×+Ñ+ó�Gð ×'Ñ'¨'Ò1Ø+2˜Ô(ØŸ™×+Ñ+Ô-à×$Ñ$¨Ò)Ø×(Ñ(¨4×+;Ñ+;¸d×>NÑ>NÑ+NÒNä%Ø=Ø(¨×)9Ñ)9Ð(:Ð:KÐLñMóð ð
 —l‘l :Ó.Ò.ðM ×&Ñ& {Ô3ðP -1ˆD×Ñ˜[Ñ)ä�4×#Ñ#Ô$Ø*.�Ô'Ø—‘•ð %øô3 )ò !Ø8<˜×(Ñ(¨Ñ5Ø26˜Ô/Ø'+×'7Ñ'7˜œÚ ð	!ûð. -1ˆD×Ñ˜[Ñ)ä�4×#Ñ#Ô$Ø*.�Ô'Ø—‘•ð %üsD   ‚A4LÁ7CK  Å0J	 Å3CK  É=LÊ	0J=Ê9K  Ê<J=Ê=K  Ë >K>Ë>Lr0   c                 óL   — | j                   d uxr t        | j                  «      S r   )r~   r*   r   r?   s    r$   rf   z/_ForkerIterDataPipe.is_every_instance_exhaustedð   s!   € Ø�|‰| 4Ð'ÒA¬C°×0@Ñ0@Ó,AÐAr/   c                 ó,   — t        | j                  «      S r   r‚   rb   s     r$   rj   z*_ForkerIterDataPipe.get_length_by_instanceó   rƒ   r/   c                 óÞ   — d | _         t        «       | _        dg| j                  z  | _        d| _        d| _        d | _        t        | j                  «      D �cg c]  }d‘Œ c}| _	        y c c}w ©Nr   T)
rs   r   rt   rL   r{   r|   r}   r~   rV   r   ©r-   r€   s     r$   rh   z_ForkerIterDataPipe.resetö   s`   € Ø"&ˆÔÜ“gˆŒØ ˜c D×$6Ñ$6Ñ6ˆÔØˆÔØˆÔØˆŒÜ*/°×0BÑ0BÓ*CÖD QšDÒDˆÕùÒDs   Á	A*c                 óØ   — | j                   | j                  | j                  | j                  | j                  | j
                  f}t        j                  �t        j                  |«      S |S r   )rr   rL   rM   rx   Ú_valid_iterator_idÚ_number_of_samples_yieldedr   Úgetstate_hook©r-   Ústates     r$   Ú__getstate__z _ForkerIterDataPipe.__getstate__ÿ   sa   € à×ÑØ×ÑØ×ÑØ�L‰LØ×#Ñ#Ø×+Ñ+ð
ˆô ×%Ñ%Ð1Ü×-Ñ-¨eÓ4Ð4Øˆr/   c                 ó,  — |\  | _         | _        | _        | _        | _        | _        d | _        t        «       | _        dg| j                  z  | _	        d| _
        d| _        d | _        t        | j                  «      D �cg c]  }d‘Œ c}| _        y c c}w r–   )rr   rL   rM   rx   r™   rš   rs   r   rt   r{   r|   r}   r~   rV   r   ©r-   r�   r€   s      r$   Ú__setstate__z _ForkerIterDataPipe.__setstate__  s�   € ð ñ	
ØÔØÔØÔØŒLØÔ#ØÔ+à"&ˆÔÜ“gˆŒØ ˜c D×$6Ñ$6Ñ6ˆÔØˆÔØˆÔØˆŒÜ*/°×0BÑ0BÓ*CÖD QšDÒDˆÕùÒDs   Á?	Bc                 ó–   — | j                   r=| j                   j                  «       }t        j                  |«       | j                   rŒ<y y r   )rt   r�   r   Úclose_streams)r-   Úds     r$   r�   z_ForkerIterDataPipe._cleanup  s2   € Ø�kŠkØ—‘×#Ñ#Ó%ˆAÜ×'Ñ'¨Ô*ð �k�kr/   c                 ó$   — | j                  «        y r   ©r�   r?   s    r$   Ú__del__z_ForkerIterDataPipe.__del__"  ó   € Ø�‰�r/   r\   rk   )r>   rB   rC   rD   r   rG   r   r
   r.   r@   rc   rl   rf   rj   rh   rž   r¡   r�   r§   rH   r/   r$   rU   rU   ‹   s™   „ ñð  Ø59ñ#Làð#Lð ð#Lð ð	#Lð
 �wÐ0Ñ1Ñ2ó#LòJ'ð3 ¸ó 3 ðjB¨Tó Bð'°#ð '¸#ó 'óEòòEò"+ó
r/   rU   c                   óT   — e Zd ZU dZdZeed<   dedefd„Z	d„ Z
d„ Zd	efd
„Zd	efd„Zy)rW   a�  
    Iterable Datapipe that is a child of a main DataPipe.

    The instance of this class will pass its instance_id to get the next value from its main DataPipe.

    Note:
        ChildDataPipe, like all other IterDataPipe, follows the single iterator per IterDataPipe constraint.
        Since ChildDataPipes share a common buffer, when an iterator is created for one of the ChildDataPipes,
        the previous iterators  for all ChildDataPipes must be invalidated, with the exception when a ChildDataPipe
        hasn't had an iterator created from it since the last invalidation. See the example below.

    Example:
        >>> # xdoctest: +REQUIRES(module:torchdata)
        >>> # Singler Iterator per IteraDataPipe Invalidation
        >>> from torchdata.datapipes.iter import IterableWrapper
        >>> source_dp = IterableWrapper(range(10))
        >>> cdp1, cdp2 = source_dp.fork(num_instances=2)
        >>> it1, it2 = iter(cdp1), iter(cdp2)
        >>> it3 = iter(cdp1)
        >>> # The line above invalidates `it1` and `it2`, and resets `ForkerIterDataPipe`.
        >>> it4 = iter(cdp2)
        >>> # The line above doesn't invalidate `it3`, because an iterator for `cdp2` hasn't been created since
        >>> # the last invalidation.

    Args:
        main_datapipe: Main DataPipe with a method 'get_next_element_by_instance(instance_id)'
        instance_id: integer identifier of this instance
    TÚ_is_child_datapiperr   r`   c                 óD   — t        |t        «      sJ ‚|| _        || _        y r   )r    r_   rr   r`   )r-   rr   r`   s      r$   r.   z_ChildDataPipe.__init__F  s#   € Ü˜-Ô);Ô<Ð<Ð<à+8ˆÔØ&ˆÕr/   c                 óL   — | j                   j                  | j                  «      S r   )rr   rc   r`   r?   s    r$   r2   z_ChildDataPipe.__iter__L  s!   € ð ×!Ñ!×>Ñ>¸t×?OÑ?OÓPÐPr/   c                 óL   — | j                   j                  | j                  «      S r   )rr   rj   r`   r?   s    r$   r@   z_ChildDataPipe.__len__Q  s   € Ø×!Ñ!×8Ñ8¸×9IÑ9IÓJÐJr/   r0   c                 óÀ  — | j                   j                  €d| j                   _        n�| j                   j                  | j                  k(  rm| j                   xj                  dz  c_        | j                   j                  «       st        j                  dt
        «       | j                   j                  «        | j                   j                  | _        | j                  S )z¼
        Update the valid iterator ID for both this DataPipe object and `main_datapipe`.

        `main_datapipe.reset()` is called when the ID is incremented to a new generation.
        r   rT   z“Some child DataPipes are not exhausted when __iter__ is called. We are resetting the buffer and each child DataPipe will read from the start again.)rr   r™   rf   ru   rv   rw   rh   r?   s    r$   Ú$_set_main_datapipe_valid_iterator_idz3_ChildDataPipe._set_main_datapipe_valid_iterator_idU  s´   € ð ×Ñ×0Ñ0Ð8Ø45ˆD×ÑÕ1ð ×Ñ×2Ñ2°d×6MÑ6MÒMØ×Ñ×1Ò1°QÑ6Õ1à×%Ñ%×AÑAÔCÜ—‘ðYäôð
 ×Ñ×$Ñ$Ô&ð #'×"4Ñ"4×"GÑ"GˆÔØ×&Ñ&Ð&r/   c                 óV   — || j                   k(  xr || j                  j                   k(  S )zXCheck the valid iterator ID against that of DataPipe object and that of `main_datapipe`.)r™   rr   )r-   Úiterator_ids     r$   Ú_check_valid_iterator_idz'_ChildDataPipe._check_valid_iterator_idp  s1   € ð ˜4×2Ñ2Ñ2ò EØ˜t×1Ñ1×DÑDÑDð	
r/   N)r>   rB   rC   rD   rª   rl   rF   r   rG   r.   r2   r@   r¯   r²   rH   r/   r$   rW   rW   &  sK   … ñð:  $Ð˜Ó#ð' lð 'Àó 'òQò
Kð'°có 'ð6
°tô 
r/   rW   Údemuxc                   óD   — e Zd ZdZ	 	 d	dededeegee   f   de	def
d„Z
y)
r   a+  
    Splits the input DataPipe into multiple child DataPipes, using the given classification function (functional name: ``demux``).

    A list of the child DataPipes is returned from this operation.

    Args:
        datapipe: Iterable DataPipe being filtered
        num_instances: number of instances of the DataPipe to create
        classifier_fn: a function that maps values to an integer within the range ``[0, num_instances - 1]`` or ``None``
        drop_none: defaults to ``False``, if ``True``, the function will skip over elements classified as ``None``
        buffer_size: this defines the maximum number of inputs that the buffer can hold across all child
            DataPipes while waiting for their values to be yielded.
            Defaults to ``1000``. Use ``-1`` for the unlimited buffer.

    Examples:
        >>> # xdoctest: +REQUIRES(module:torchdata)
        >>> from torchdata.datapipes.iter import IterableWrapper
        >>> def odd_or_even(n):
        ...     return n % 2
        >>> source_dp = IterableWrapper(range(5))
        >>> dp1, dp2 = source_dp.demux(num_instances=2, classifier_fn=odd_or_even)
        >>> list(dp1)
        [0, 2, 4]
        >>> list(dp2)
        [1, 3]
        >>> # It can also filter out any element that gets `None` from the `classifier_fn`
        >>> def odd_or_even_no_zero(n):
        ...     return n % 2 if n != 0 else None
        >>> dp1, dp2 = source_dp.demux(num_instances=2, classifier_fn=odd_or_even_no_zero, drop_none=True)
        >>> list(dp1)
        [2, 4]
        >>> list(dp2)
        [1, 3]
    rK   rL   Úclassifier_fnÚ	drop_nonerM   c                 ó®   — |dk  rt        d|› d�«      ‚t        |«       t        |||||«      }t        |«      D �cg c]  }t	        ||«      ‘Œ c}S c c}w rS   )r)   r   Ú_DemultiplexerIterDataPiperV   rW   )rX   rK   rL   rµ   r¶   rM   rY   rZ   s           r$   r[   z!DemultiplexerIterDataPipe.__new__�  se   € ð ˜1ÒÜØ>¸}¸oÈYÐWóð ô 	˜]Ô+ô
 /¨x¸ÈÐW`ÐbmÓnˆ	Ü6;¸MÓ6JÖK°”˜y¨!Õ,ÒKÐKùÒKs   ¼AN)Fr]   )r>   rB   rC   rD   r   rG   r	   r   r   rl   r[   rH   r/   r$   r   r   x  s[   „ ñ!ðP  ØñLàðLð ðLð    ¨°#©Ð 6Ñ7ð	Lð
 ðLð ôLr/   r   c            
       óª   — e Zd ZdZdee   dedeegee   f   de	def
d„Z
ded	efd
„Zdefd„Zd	e	fd„Zded	efd„Zdd„Zd„ Zd„ Zddee   fd„Zd„ Zy)r¸   zþ
    Container to hold instance-specific information on behalf of DemultiplexerIterDataPipe.

    It tracks the state of its child DataPipes, maintains the buffer, classifies and yields the next correct value
    as requested by the child DataPipes.
    rK   rL   rµ   r¶   rM   c                 ób  — || _         d | _        || _        || _        | j                  dk  rt	        j
                  dt        «       d| _        t        |«      D �cg c]  }t        «       ‘Œ c}| _
        || _        || _        d| _        t        |«      D �cg c]  }d‘Œ c}| _        y c c}w c c}w )Nr   zQUnlimited buffer size is set for `demux`, please be aware of OOM at random placesFT)rr   rs   rL   rM   ru   rv   rw   Úcurrent_buffer_usagerV   r   Úchild_buffersrµ   r¶   Úmain_datapipe_exhaustedr   )r-   rK   rL   rµ   r¶   rM   r€   s          r$   r.   z#_DemultiplexerIterDataPipe.__init__»  s¥   € ð &ˆÔØ;?ˆÔØ*ˆÔØ&ˆÔØ×Ñ˜aÒÜ�M‰Mð:äôð
 %&ˆÔ!ÜCHÈÓCWÖ1X¸a´%µ'Ò1XˆÔØ*ˆÔØ"ˆŒØ',ˆÔ$Ü6;¸MÓ6JÖ'K°ªÒ'KˆÕùò	 2Yùò (Ls   ÁB'Â	B,r`   r0   c                 ór  — 	 | j                   s| j                  |   rt        ‚| j                  €t	        d«      ‚t        | j                  «      }| j                  |«      }|€"| j                  rt        j                  |«       Œƒ|�|| j                  k\  s|dk  r"t	        d| j                  dz
  › d�|› d�z   «      ‚||k(  r|S | j                  |   j                  |«       | xj                  dz  c_        | j                  dk\  r2| j                  | j                  kD  rt        d| j                  › d�«      ‚�Œ7)	Nz‘_datapipe_iterator has not been set, likely because this private method is called directly without invoking get_next_element_by_instance() first.r   z8Output of the classification fn should be between 0 and rT   z. z is returned.z7DemultiplexerIterDataPipe buffer overflow, buffer size r…   )r½   r   r‹   rs   r)   r‰   rµ   r¶   r   r£   rL   r¼   rŠ   r»   rM   rŽ   )r-   r`   ÚvalueÚclassifications       r$   Ú
_find_nextz%_DemultiplexerIterDataPipe._find_nextÔ  sR  € ØØ×+Ò+¨t×/?Ñ/?ÀÒ/LÜ#Ð#Ø×&Ñ&Ð.Ü ðMóð ô ˜×0Ñ0Ó1ˆEØ!×/Ñ/°Ó6ˆNØÐ%¨$¯.ª.Ü×+Ñ+¨EÔ2ØàÐ&Ø! T×%7Ñ%7Ò7Ø! AÒ%ä ØNÈt×OaÑOaÐdeÑOeÐNfÐfhÐiØ'Ð(¨Ð6ñ7óð ð  Ò,Ø�Ø×Ñ˜~Ñ.×5Ñ5°eÔ<Ø×%Ò%¨Ñ*Õ%Ø×Ñ 1Ò$¨×)BÑ)BÀT×EUÑEUÒ)UÜ!ØMÈd×N^ÑN^ÐM_Ð_pÐqóð ñ7 r/   c              #   ó‚  K  — | j                   €n| j                  |   r_t        | j                  «      | _         t        j
                  | _        d| _        t        | j                  «      D ]  }d| j                  |<   Œ 	 | j                  |   sh| j                  |   r5| xj                  dz  c_        | j                  |   j                  «       –— n	 | j                  |«      –— | j                  |   sŒhd| j                  |<   t        | j                  «      rd | _         | j                  |   r| j!                  |«       y y # t        $ r  d| j                  |<   d| _        d | _         Y Œ…w xY w# d| j                  |<   t        | j                  «      rd | _         | j                  |   r| j!                  |«       w w xY w­w)NFrT   T)rs   r   r†   rr   r   r‡   rˆ   r½   rV   rL   r¼   r»   r�   rÁ   r‹   r*   r�   )r-   r`   rZ   s      r$   rc   z7_DemultiplexerIterDataPipe.get_next_element_by_instanceô  s¦  è ø€ Ø×"Ñ"Ð*¨t×/?Ñ/?ÀÒ/LÜ&*¨4×+=Ñ+=Ó&>ˆDÔ#ä×(Ñ(ð Ô ð ,1ˆDÔ(Ü˜4×-Ñ-Ó.ò ,�Ø&+�× Ñ  Ò#ð,ð	+Ø×&Ñ& {Ò3Ø×%Ñ% kÒ2Ø×-Ò-°Ñ2Õ-Ø×,Ñ,¨[Ñ9×AÑAÓCÓCð7Ø"Ÿo™o¨kÓ:Ò:ð ×&Ñ& {Ó3ð -1ˆD×Ñ˜[Ñ)ä�4×#Ñ#Ô$Ø*.�Ô'Ø×!Ñ! +Ò.Ø—‘˜kÕ*ð /øô )ò 7Ø8<˜×(Ñ(¨Ñ5Ø7;˜Ô4Ø26˜Ö/ð7ûð
 -1ˆD×Ñ˜[Ñ)ä�4×#Ñ#Ô$Ø*.�Ô'Ø×!Ñ! +Ò.Ø—‘˜kÕ*ð /üsE   ‚A;F?Á>AE. ÃE Ã%E. Ã5AF?Å&E+Å(E. Å*E+Å+E. Å.AF<Æ<F?c                 óH   — | j                   xr t        | j                  «      S r   )r½   r*   r   r?   s    r$   rf   z6_DemultiplexerIterDataPipe.is_every_instance_exhausted  s   € Ø×+Ñ+ÒE´°D×4DÑ4DÓ0EÐEr/   c                 ó   — t         ‚r   )r+   rb   s     r$   rj   z1_DemultiplexerIterDataPipe.get_length_by_instance  s   € Üˆr/   Nc                 óò   — d | _         d| _        t        | j                  «      D �cg c]  }t	        «       ‘Œ c}| _        t        | j                  «      D �cg c]  }d‘Œ c}| _        d| _        y c c}w c c}w ©Nr   TF)rs   r»   rV   rL   r   r¼   r   r½   r—   s     r$   rh   z _DemultiplexerIterDataPipe.reset  sa   € Ø"&ˆÔØ$%ˆÔ!Ü/4°T×5GÑ5GÓ/HÖI¨!œe�gÒIˆÔÜ*/°×0BÑ0BÓ*CÖD QšDÒDˆÔØ',ˆÕ$ùò JùÚDs   ¦A/Á	A4c                 óî   — | j                   | j                  | j                  | j                  | j                  | j
                  | j                  f}t        j                  �t        j                  |«      S |S r   )	rr   rL   rM   rµ   r¶   r™   rš   r   r›   rœ   s     r$   rž   z'_DemultiplexerIterDataPipe.__getstate__  sj   € à×ÑØ×ÑØ×ÑØ×ÑØ�N‰NØ×#Ñ#Ø×+Ñ+ð
ˆô ×%Ñ%Ð1Ü×-Ñ-¨eÓ4Ð4Øˆr/   c                 óL  — |\  | _         | _        | _        | _        | _        | _        | _        d | _        d| _        t        | j                  «      D �cg c]  }t        «       ‘Œ c}| _        t        | j                  «      D �cg c]  }d‘Œ c}| _        d| _        y c c}w c c}w rÆ   )rr   rL   rM   rµ   r¶   r™   rš   rs   r»   rV   r   r¼   r   r½   r    s      r$   r¡   z'_DemultiplexerIterDataPipe.__setstate__-  s”   € ð ñ	
ØÔØÔØÔØÔØŒNØÔ#ØÔ+à"&ˆÔØ$%ˆÔ!Ü/4°T×5GÑ5GÓ/HÖI¨!œe�gÒIˆÔÜ*/°×0BÑ0BÓ*CÖD QšDÒDˆÔØ',ˆÕ$ùò JùÚDs   ÁBÂ	B!c                 óº   — |€t        | j                  «      n|g}|D ]<  }| j                  |   }|sŒ|j                  «       }t	        j
                  |«       |rŒ(Œ> y r   )rV   rL   r¼   r�   r   r£   )r-   r`   ÚidsrZ   Úqr¤   s         r$   r�   z#_DemultiplexerIterDataPipe._cleanup=  sk   € ð Ð"ô �$×$Ñ$Ô%ð ðð 	ð ò 	/ˆAØ×"Ñ" 1Ñ%ˆAÚØ—I‘I“K�Ü×+Ñ+¨AÔ.ó ñ	/r/   c                 ó$   — | j                  «        y r   r¦   r?   s    r$   r§   z"_DemultiplexerIterDataPipe.__del__K  r¨   r/   rk   r   )r>   rB   rC   rD   r   r   rG   r	   r   rl   r.   rÁ   rc   rf   rj   rh   rž   r¡   r�   r§   rH   r/   r$   r¸   r¸   ³  s¼   „ ñðLà˜uÑ%ðLð ðLð    ¨°#©Ð 6Ñ7ð	Lð
 ðLð óLð2 cð ¨eó ð@+¸ó +ð<F¨Tó Fð°#ð ¸#ó ó-òò-ñ / H¨S¡Mó /ór/   r¸   Úmuxc                   ó<   — e Zd ZdZd„ Zd„ Zd„ Zd
d„Zd„ Zd„ Z	d	„ Z
y)r   aó  
    Yields one element at a time from each of the input Iterable DataPipes (functional name: ``mux``).

    As in, one element from the 1st input DataPipe, then one element from the 2nd DataPipe in the next iteration,
    and so on. It ends when the shortest input DataPipe is exhausted.

    Args:
        datapipes: Iterable DataPipes that will take turn to yield their elements, until the shortest DataPipe is exhausted

    Example:
        >>> # xdoctest: +REQUIRES(module:torchdata)
        >>> from torchdata.datapipes.iter import IterableWrapper
        >>> dp1, dp2, dp3 = IterableWrapper(range(3)), IterableWrapper(range(10, 15)), IterableWrapper(range(20, 25))
        >>> list(dp1.mux(dp2, dp3))
        [0, 10, 20, 1, 11, 21, 2, 12, 22]
    c                 ó    — || _         g | _        y r   )r   rt   r,   s     r$   r.   z MultiplexerIterDataPipe.__init__b  s   € Ø"ˆŒàð 	�r/   c              #   óš  K  — | j                   D �cg c]  }t        |«      ‘Œ }}t        |«      ri|D ])  }	 t        |«      }| j                  j                  |«       Œ+ | j                  E d {  –—†  | j                  j                  «        t        |«      rŒhy y c c}w # t        $ r | j                  j                  «        Y  y w xY w7 Œ[­wr   )r   r†   r(   r‰   rt   rŠ   r‹   Úclear)r-   rn   Ú	iteratorsÚitr¿   s        r$   r2   z MultiplexerIterDataPipe.__iter__h  s­   è ø€ Ø&*§n¡nÖ5 ”T˜!•WÐ5ˆ	Ð5Ü�)ŒnØò �ðÜ  ›H�EØ—K‘K×&Ñ& uÕ-ðð —{‘{×"Ð"Ø�K‰K×ÑÔô �)�nùò 6øô %ò Ø—K‘K×%Ñ%Ô'Úðúð #úsD   ‚C‘B£C¶&BÁCÁ-C	Á.)CÂCÂ#CÃCÃCÃCc                 óà   — t        d„ | j                  D «       «      r2t        d„ | j                  D «       «      t        | j                  «      z  S t	        t        | «      j                  › d�«      ‚)Nc              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­wr   r5   r!   s     r$   r%   z2MultiplexerIterDataPipe.__len__.<locals>.<genexpr>v  r6   r'   c              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr   r8   r!   s     r$   r%   z2MultiplexerIterDataPipe.__len__.<locals>.<genexpr>w  r9   r:   r;   )r*   r   rŒ   r(   r+   r=   r>   r?   s    r$   r@   zMultiplexerIterDataPipe.__len__u  sU   € ÜÑ>¨t¯~©~Ô>Ô>ÜÑ8¨¯©Ô8Ó8¼3¸t¿~¹~Ó;NÑNÐNäœt D›z×2Ñ2Ð3Ð3VÐWÓXÐXr/   Nc                 ó   — g | _         y r   )rt   r?   s    r$   rh   zMultiplexerIterDataPipe.reset{  s	   € Øˆ�r/   c                 ó–   — | j                   | j                  | j                  f}t        j                  �t        j                  |«      S |S r   )r   r™   rš   r   r›   rœ   s     r$   rž   z$MultiplexerIterDataPipe.__getstate__~  sF   € à�N‰NØ×#Ñ#Ø×+Ñ+ð
ˆô
 ×%Ñ%Ð1Ü×-Ñ-¨eÓ4Ð4Øˆr/   c                 ó<   — |\  | _         | _        | _        g | _        y r   )r   r™   rš   rt   rœ   s     r$   r¡   z$MultiplexerIterDataPipe.__setstate__ˆ  s$   € ð
 ñ		
ØŒNØÔ#ØÔ+àˆ�r/   c                 ó8   — | j                   j                  «        y r   )rt   rÑ   r?   s    r$   r§   zMultiplexerIterDataPipe.__del__�  s   € Ø�‰×ÑÕr/   rk   )r>   rB   rC   rD   r.   r2   r@   rh   rž   r¡   r§   rH   r/   r$   r   r   O  s+   „ ñò"
ò òYóòòór/   r   Úzipc                   ó^   ‡ — e Zd ZU dZee   ed<   defˆ fd„Zdeee	      fd„Z
defd„Zˆ xZS )r   aa  
    Aggregates elements into a tuple from each of the input DataPipes (functional name: ``zip``).

    The output is stopped as soon as the shortest input DataPipe is exhausted.

    Args:
        *datapipes: Iterable DataPipes being aggregated

    Example:
        >>> # xdoctest: +REQUIRES(module:torchdata)
        >>> from torchdata.datapipes.iter import IterableWrapper
        >>> dp1, dp2, dp3 = IterableWrapper(range(5)), IterableWrapper(range(10, 15)), IterableWrapper(range(20, 25))
        >>> list(dp1.zip(dp2, dp3))
        [(0, 10, 20), (1, 11, 21), (2, 12, 22), (3, 13, 23), (4, 14, 24)]
    r   c                 ój   •— t        d„ |D «       «      st        d«      ‚t        ‰| �  «        || _        y )Nc              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­wr   r   r!   s     r$   r%   z.ZipperIterDataPipe.__init__.<locals>.<genexpr>©  r&   r'   zCAll inputs are required to be `IterDataPipe` for `ZipIterDataPipe`.)r*   r+   Úsuperr.   r   )r-   r   Ú	__class__s     €r$   r.   zZipperIterDataPipe.__init__¨  s5   ø€ ÜÑD¸)ÔDÔDÜØUóð ô 	‰ÑÔØ"ˆ�r/   r0   c              #   ó~   K  — | j                   D �cg c]  }t        |«      ‘Œ }}t        |Ž E d {  –—†  y c c}w 7 Œ
­wr   )r   r†   rÛ   )r-   rK   rÒ   s      r$   r2   zZipperIterDataPipe.__iter__°  s7   è ø€ Ø48·N±NÖC¨”T˜(•^ÐCˆ	ÐCÜ˜	�?×"Ñ"ùò DØ"ús   ‚=‘6£=°;±=c                 ó´   — t        d„ | j                  D «       «      rt        d„ | j                  D «       «      S t        t	        | «      j
                  › d�«      ‚)Nc              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­wr   r5   r!   s     r$   r%   z-ZipperIterDataPipe.__len__.<locals>.<genexpr>µ  r6   r'   c              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr   r8   r!   s     r$   r%   z-ZipperIterDataPipe.__len__.<locals>.<genexpr>¶  r9   r:   r;   )r*   r   rŒ   r+   r=   r>   r?   s    r$   r@   zZipperIterDataPipe.__len__´  rA   r/   )r>   rB   rC   rD   rE   r   rF   r.   r   r   r2   rG   r@   Ú__classcell__)rà   s   @r$   r   r   ”  sD   ø… ñð  �\Ñ"Ó"ð# <õ #ð#˜( 5¨¡<Ñ0ó #ðY˜÷ Yr/   r   )'rN   ry   ru   Úabcr   r   Úcollectionsr   Úcollections.abcr   r   Útypingr   r	   r
   r   r   Ú%torch.utils.data.datapipes._decoratorr   Ú)torch.utils.data.datapipes._hook_iteratorr   Ú#torch.utils.data.datapipes.datapiper   Ú'torch.utils.data.datapipes.utils.commonr   r   Ú__all__r   r   r   r_   ro   rU   rW   r   r¸   r   rE   r   rH   r/   r$   ú<module>rï      s=  ðã Û ß #Ý ß +ß <Õ <å EÝ DÝ <ß Wò€ñ 	� 4Ô(€ñ �XÓô$Y˜<ó $Yó ð$YñN �VÓô,L˜ó ,Ló ð,Lô^c˜ô cò(ôX˜,Ð(:ô XôvO
�\ô O
ñd �WÓô7L ó 7Ló ð7LôtY Ð/Aô Yñx �UÓôA˜ló Aó ðAñH �UÓô#Y˜ e¨E¡lÑ3ó #Yó ñ#Yr/   