
    JThA                     r   S SK r S SKrS SKJrJr  S SKJrJrJr  S SK	J
r
  S SKJr  S SKJrJr  S SKJrJrJr  S SKJrJr  \
" 5       r\SLr/ S	Qr\" S
5      r\" SSS9r/ SQr " S S\\   5      r " S S\\   \S9r " S S\5      r  " S S\\   \S9r! " S S5      r" " S S\"\5      r# " S S\"\!5      r$g)    N)IterableIterator)CallableOptionalTypeVar)import_dill)_SnapshotState)_DataPipeMeta_IterDataPipeMeta)_deprecation_warning!_iter_deprecated_functional_names _map_deprecated_functional_names)DatasetIterableDataset)	DataChunkDFIterDataPipeIterDataPipeMapDataPipe_T_T_coT)	covariant)batchgroupby_dataframes_as_tuplestrace_as_dataframec                   ~   ^  \ rS rSrS\\   SS4U 4S jjrSS\S\4S jjrS\	\   4U 4S jjr
S\	\   4S	 jrS
rU =r$ )r   '   itemsreturnNc                 F   > [        U5      n[        TU ]	  U5        Xl        g N)listsuper__init__r   )selfr   	__class__s     [/var/www/auris/envauris/lib/python3.13/site-packages/torch/utils/data/datapipes/datapipe.pyr$   DataChunk.__init__(   s    U
    indentc                 V    US-   SR                  S [        U 5       5       5      -   S-   $ )N[z, c              3   8   #    U  H  n[        U5      v   M     g 7fr!   )str).0is     r'   	<genexpr>#DataChunk.as_str.<locals>.<genexpr>.   s     'C
1A
s   ])joiniter)r%   r*   s     r'   as_strDataChunk.as_str-   s)    |dii'CT
'CCCcIIr)   c              #   >   >#    [         TU ]  5        S h  vN   g  N7fr!   )r#   __iter__r%   r&   s    r'   r9   DataChunk.__iter__0   s     7#%%%s   c              #   8   #    U R                    S h  vN   g  N7fr!   r   r%   s    r'   raw_iteratorDataChunk.raw_iterator3   s     ::s   r=   ) )__name__
__module____qualname____firstlineno__r   r   r$   r.   r6   r   r9   r?   __static_attributes____classcell__r&   s   @r'   r   r   '   sU    hrl t 
JS J# J&(2, &hrl  r)   r   c                   v  ^  \ rS rSr% Sr0 r\\\4   \	S'   Sr
\\   \	S'   Sr\\   \	S'   Sr\\   \	S'   Sr\\   \	S'   Sr\\   \	S	'   S
r\\	S'   \R&                  r\\	S'   Sr\\   \	S'   S\\   4S jrS r\S 5       r\ SS j5       rS rU 4S jr\S 5       r\S 5       r S r!S r"U 4S jr#SS jr$Sr%U =r&$ )r   7   a  
Iterable-style DataPipe.

All DataPipes that represent an iterable of data samples should subclass this.
This style of DataPipes is particularly useful when data come from a stream, or
when the number of samples is too large to fit them all in memory. ``IterDataPipe`` is lazily initialized and its
elements are computed only when ``next()`` is called on the iterator of an ``IterDataPipe``.

All subclasses should overwrite :meth:`__iter__`, which would return an
iterator of samples in this DataPipe. Calling ``__iter__`` of an ``IterDataPipe`` automatically invokes its
method ``reset()``, which by default performs no operation. When writing a custom ``IterDataPipe``, users should
override ``reset()`` if necessary. The common usages include resetting buffers, pointers,
and various state variables within the custom ``IterDataPipe``.

Note:
    Only `one` iterator can be valid for each ``IterDataPipe`` at a time,
    and the creation a second iterator will invalidate the first one. This constraint is necessary because
    some ``IterDataPipe`` have internal buffers, whose states can become invalid if there are multiple iterators.
    The code example below presents details on how this constraint looks in practice.
    If you have any feedback related to this constraint, please see `GitHub IterDataPipe Single Iterator Issue`_.

These DataPipes can be invoked in two ways, using the class constructor or applying their
functional form onto an existing ``IterDataPipe`` (recommended, available to most but not all DataPipes).
You can chain multiple `IterDataPipe` together to form a pipeline that will perform multiple
operations in succession.

.. _GitHub IterDataPipe Single Iterator Issue:
    https://github.com/pytorch/data/issues/45

Note:
    When a subclass is used with :class:`~torch.utils.data.DataLoader`, each
    item in the DataPipe will be yielded from the :class:`~torch.utils.data.DataLoader`
    iterator. When :attr:`num_workers > 0`, each worker process will have a
    different copy of the DataPipe object, so it is often desired to configure
    each copy independently to avoid having duplicate data returned from the
    workers. :func:`~torch.utils.data.get_worker_info`, when called in a worker
    process, returns information about the worker. It can be used in either the
    dataset's :meth:`__iter__` method or the :class:`~torch.utils.data.DataLoader` 's
    :attr:`worker_init_fn` option to modify each copy's behavior.

Examples:
    General Usage:
        >>> # xdoctest: +SKIP
        >>> from torchdata.datapipes.iter import IterableWrapper, Mapper
        >>> dp = IterableWrapper(range(10))
        >>> map_dp_1 = Mapper(dp, lambda x: x + 1)  # Using class constructor
        >>> map_dp_2 = dp.map(lambda x: x + 1)  # Using functional form (recommended)
        >>> list(map_dp_1)
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        >>> list(map_dp_2)
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        >>> filter_dp = map_dp_1.filter(lambda x: x % 2 == 0)
        >>> list(filter_dp)
        [2, 4, 6, 8, 10]
    Single Iterator Constraint Example:
        >>> from torchdata.datapipes.iter import IterableWrapper, Mapper
        >>> source_dp = IterableWrapper(range(10))
        >>> it1 = iter(source_dp)
        >>> list(it1)
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        >>> it1 = iter(source_dp)
        >>> it2 = iter(source_dp)  # The creation of a new iterator invalidates `it1`
        >>> next(it2)
        0
        >>> next(it1)  # Further usage of `it1` will raise a `RunTimeError`
	functionsNreduce_ex_hookgetstate_hookstr_hook	repr_hook_valid_iterator_idr   _number_of_samples_yielded_snapshot_state_fast_forward_iteratorr   c                     U $ r!    r>   s    r'   r9   IterDataPipe.__iter__   s    r)   c                 0   U[         R                  ;   a^  U[        ;   a  [        U   n[        S0 UD6  [         R                  U   n[        R
                  " X05      n[        R                  " XCSS9  U$ [        SU R                  R                   SU 35      eN__doc__wrapperwrappedassigned'z' object has no attribute 'rU   )
r   rK   r   r   	functoolspartialupdate_wrapperAttributeErrorr&   rB   r%   attribute_namekwargsffunctions        r'   __getattr__IterDataPipe.__getattr__   s    \333!BB:>J$.v.&&~6A ((1H$$X<XO DNN++,,GGWX r)   c                      X R                   U'   g r!   rK   clsfunction_namerh   s      r'   register_functionIterDataPipe.register_function       '/m$r)   c                    ^ TU R                   ;   a  [        ST S35      eU4S jn[        R                  " XBU5      n[        R                  " XRSS9  XPR                   T'   g )N%Unable to add DataPipe function name  as it is already takenc                    > U " U/UQ70 UD6n[        U[        5      (       a6  U(       d  [        U[        5      (       a  T[        ;  a  UR	                  5       nU$ r!   )
isinstancer   r   UNTRACABLE_DATAFRAME_PIPESr   )rn   enable_df_api_tracing	source_dpargsrf   result_pipero   s         r'   class_functionBIterDataPipe.register_datapipe_as_function.<locals>.class_function   sP    i9$9&9K+|44(Jy.,Q,Q$,FF&1&D&D&Fr)   rY   r[   rK   	Exceptionr`   ra   rb   )rn   ro   cls_to_registerry   r}   rh   s    `    r'   register_datapipe_as_function*IterDataPipe.register_datapipe_as_function   sk     CMM)7F]^ 	 $$-B
 	  	
 (0m$r)   c                 j    U R                   n[        R                  b  [        R                  U5      $ U$ z
Serialize `lambda` functions when `dill` is available.

If this doesn't cover your custom DataPipe's use case, consider writing custom methods for
`__getstate__` and `__setstate__`, or use `pickle.dumps` for serialization.
)__dict__r   rM   r%   states     r'   __getstate__IterDataPipe.__getstate__   s/     %%1--e44r)   c                    > [         R                  b   [         R                  U 5      $ [        TU ]  " U0 UD6$ ! [         a     Nf = fr!   )r   rL   NotImplementedErrorr#   __reduce_ex__r%   r{   rf   r&   s      r'   r   IterDataPipe.__reduce_ex__   sP    &&2#22488 w$d5f55 '    9 
AAc                 X    [         R                  b  Ub  [        S5      eU[         l        g Nz*Attempt to override existing getstate_hook)r   rM   RuntimeErrorrn   hook_fns     r'   set_getstate_hookIterDataPipe.set_getstate_hook   s'    %%1g6IKLL%,"r)   c                 X    [         R                  b  Ub  [        S5      eU[         l        g Nz+Attempt to override existing reduce_ex_hook)r   rL   r   r   s     r'   set_reduce_ex_hookIterDataPipe.set_reduce_ex_hook   s'    &&2w7JLMM&-#r)   c                 |    U R                   b  U R                  U 5      $ [        U R                  R                  5      $ r!   rO   r.   r&   rD   r>   s    r'   __repr__IterDataPipe.__repr__   0    >>%>>$''4>>..//r)   c                 |    U R                   b  U R                  U 5      $ [        U R                  R                  5      $ r!   rN   r.   r&   rD   r>   s    r'   __str__IterDataPipe.__str__   0    ==$==&&4>>..//r)   c                 z   > [        [        TU ]	  5       5      [        U R                  R	                  5       5      -   $ r!   r"   r#   __dir__rK   keysr:   s    r'   r   IterDataPipe.__dir__   ,    EGO%&dnn.A.A.C)DDDr)   c                     g)a^  
Reset the `IterDataPipe` to the initial state.

By default, no-op. For subclasses of `IterDataPipe`, depending on their functionalities,
they may want to override this method with implementations that
may clear the buffers and reset pointers of the DataPipe.
The `reset` method is always called when `__iter__` is called as part of `hook_iterator`.
NrU   r>   s    r'   resetIterDataPipe.reset   s    r)   rU   )F)r   N)'rB   rC   rD   rE   rZ   rK   dictr.   r   __annotations__rL   r   rM   rN   rO   rP   intrQ   r	   
NotStartedrR   rS   r   r   r9   ri   classmethodrp   r   r   r   r   r   r   r   r   r   rF   rG   rH   s   @r'   r   r   7   s!   AF &(ItCM"')-NHX&-(,M8H%,#'Hhx '$(Ix!((,,&''&4&?&?O^?15HX.5(5/  0 0 CH0 02
6 - -
 . .
00E r)   r   )	metaclassc                       \ rS rSrS rSrg)r      c                     g)NTrU   r>   s    r'   
_is_dfpipeDFIterDataPipe._is_dfpipe   s    r)   rU   N)rB   rC   rD   rE   r   rF   rU   r)   r'   r   r      s    r)   r   c                      ^  \ rS rSr% Sr0 r\\\4   \	S'   Sr
\\   \	S'   Sr\\   \	S'   Sr\\   \	S'   Sr\\   \	S'   S	 r\S
 5       r\S 5       rS rU 4S jr\S 5       r\S 5       rS rS rU 4S jrSrU =r$ )r      a  
Map-style DataPipe.

All datasets that represent a map from keys to data samples should subclass this.
Subclasses should overwrite :meth:`__getitem__`, supporting fetching a
data sample for a given, unique key. Subclasses can also optionally overwrite
:meth:`__len__`, which is expected to return the size of the dataset by many
:class:`~torch.utils.data.Sampler` implementations and the default options
of :class:`~torch.utils.data.DataLoader`.

These DataPipes can be invoked in two ways, using the class constructor or applying their
functional form onto an existing `MapDataPipe` (recommend, available to most but not all DataPipes).

Note:
    :class:`~torch.utils.data.DataLoader` by default constructs an index
    sampler that yields integral indices. To make it work with a map-style
    DataPipe with non-integral indices/keys, a custom sampler must be provided.

Example:
    >>> # xdoctest: +SKIP
    >>> from torchdata.datapipes.map import SequenceWrapper, Mapper
    >>> dp = SequenceWrapper(range(10))
    >>> map_dp_1 = dp.map(lambda x: x + 1)  # Using functional form (recommended)
    >>> list(map_dp_1)
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> map_dp_2 = Mapper(dp, lambda x: x + 1)  # Using class constructor
    >>> list(map_dp_2)
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> batch_dp = map_dp_1.batch(batch_size=2)
    >>> list(batch_dp)
    [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
rK   NrL   rM   rN   rO   c                 0   U[         R                  ;   a^  U[        ;   a  [        U   n[        S0 UD6  [         R                  U   n[        R
                  " X05      n[        R                  " XCSS9  U$ [        SU R                  R                   SU 35      erX   )
r   rK   r   r   r`   ra   rb   rc   r&   rB   rd   s        r'   ri   MapDataPipe.__getattr__  s    [222!AA9.I$.v.%%n5A ((1H$$X<XO DNN++,,GGWX r)   c                      X R                   U'   g r!   rl   rm   s      r'   rp   MapDataPipe.register_function*  rr   r)   c                     XR                   ;   a  [        SU S35      eS n[        R                  " X25      n[        R                  " XBSS9  X@R                   U'   g )Nrt   ru   c                     U " U/UQ70 UD6nU$ r!   rU   )rn   rz   r{   rf   r|   s        r'   r}   AMapDataPipe.register_datapipe_as_function.<locals>.class_function5  s    i9$9&9Kr)   rY   r[   r   )rn   ro   r   r}   rh   s        r'   r   )MapDataPipe.register_datapipe_as_function.  s^    MM)7F]^ 	 $$^E  	
 (0m$r)   c                 j    U R                   n[        R                  b  [        R                  U5      $ U$ r   )r   r   rM   r   s     r'   r   MapDataPipe.__getstate__?  s/     $$0,,U33r)   c                    > [         R                  b   [         R                  U 5      $ [        TU ]  " U0 UD6$ ! [         a     Nf = fr!   )r   rL   r   r#   r   r   s      r'   r   MapDataPipe.__reduce_ex__K  sP    %%1"11$77 w$d5f55 ' r   c                 X    [         R                  b  Ub  [        S5      eU[         l        g r   )r   rM   r   r   s     r'   r   MapDataPipe.set_getstate_hookS  s'    $$0W5HKLL$+!r)   c                 X    [         R                  b  Ub  [        S5      eU[         l        g r   )r   rL   r   r   s     r'   r   MapDataPipe.set_reduce_ex_hookY  s'    %%1g6ILMM%,"r)   c                 |    U R                   b  U R                  U 5      $ [        U R                  R                  5      $ r!   r   r>   s    r'   r   MapDataPipe.__repr___  r   r)   c                 |    U R                   b  U R                  U 5      $ [        U R                  R                  5      $ r!   r   r>   s    r'   r   MapDataPipe.__str__e  r   r)   c                 z   > [        [        TU ]	  5       5      [        U R                  R	                  5       5      -   $ r!   r   r:   s    r'   r   MapDataPipe.__dir__k  r   r)   rU   )rB   rC   rD   rE   rZ   rK   r   r.   r   r   rL   r   rM   rN   rO   ri   r   rp   r   r   r   r   r   r   r   r   rF   rG   rH   s   @r'   r   r      s    B &(ItCM"')-NHX&-(,M8H%,#'Hhx '$(Ix!( 0 0 0 0 
6 , ,
 - -
00E Er)   r   c                   ,    \ rS rSrS rS rS rS rSrg)_DataPipeSerializationWrapperip  c                     Xl         g r!   	_datapipe)r%   datapipes     r'   r$   &_DataPipeSerializationWrapper.__init__q  s    !r)   c                     Sn [         R                  " U R                  5      nX!4$ ! [         a2    [        (       a%  [
        R                  U R                  5      nSn X!4$ e f = f)NFT)pickledumpsr   r   HAS_DILLdill)r%   use_dillvalues      r'   r   *_DataPipeSerializationWrapper.__getstate__t  sf    	LL0E     	x

4>>2    	s    ' 6A#!A#c                     Uu  p#U(       a  [         R                  U5      U l        g [        R                  " U5      U l        g r!   )r   loadsr   r   )r%   r   r   r   s       r'   __setstate__*_DataPipeSerializationWrapper.__setstate__  s,    !ZZ.DN#\\%0DNr)   c                      [        U R                  5      $ ! [         a'  n[        [	        U 5      R
                   S35      UeS nAff = f)Nz# instance doesn't have valid length)lenr   r   	TypeErrortyperB   )r%   es     r'   __len__%_DataPipeSerializationWrapper.__len__  sL    	t~~&& 	:&&''JK	s    
A"AAr   N)	rB   rC   rD   rE   r$   r   r   r   rF   rU   r)   r'   r   r   p  s    "
!1r)   r   c                   N   ^  \ rS rSrS\\   4U 4S jjrSS jrS\4S jrSr	U =r
$ )	!_IterDataPipeSerializationWrapperi  r   c                 2   > [         TU ]  U5        S U l        g r!   )r#   r$   _datapipe_iter)r%   r   r&   s     r'   r$   *_IterDataPipeSerializationWrapper.__init__  s    "9=r)   r   c                 :    [        U R                  5      U l        U $ r!   )r5   r   r   r>   s    r'   r9   *_IterDataPipeSerializationWrapper.__iter__  s    "4>>2r)   c                 J    U R                   c   e[        U R                   5      $ r!   )r   nextr>   s    r'   __next__*_IterDataPipeSerializationWrapper.__next__  s%    ""...D''((r)   )r   )r   r   )rB   rC   rD   rE   r   r   r$   r9   r   rF   rG   rH   s   @r'   r   r     s*    >e!4 >)% ) )r)   r   c                       \ rS rSrS rSrg) _MapDataPipeSerializationWrapperi  c                      U R                   U   $ r!   r   )r%   idxs     r'   __getitem__,_MapDataPipeSerializationWrapper.__getitem__  s    ~~c""r)   rU   N)rB   rC   rD   rE   r   rF   rU   r)   r'   r   r     s    #r)   r   )%r`   r   collections.abcr   r   typingr   r   r   torch.utils._import_utilsr   )torch.utils.data.datapipes._hook_iteratorr	   "torch.utils.data.datapipes._typingr
   r   'torch.utils.data.datapipes.utils.commonr   r   r   torch.utils.data.datasetr   r   r   r   __all__r   r   rx   r"   r   r   r   r   r   r   r   rU   r)   r'   <module>r     s      . . . 1 D O 
 > }t T]4( R  u?5)5F up\ 
yE'%.M yEx @)(E| )#'Dk #r)   