Ë
    Q/æjZ#  ã                   ó  — d dl Z d dlmZ d dlmZmZmZmZ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 d dlmZmZ d	d
gZ edd¬«      Z ed«       G d„ d
ee   «      «       Zd„ Z ed«       G d„ d	e«      «       Zy)é    N)Ú
namedtuple)	ÚAnyÚCallableÚDictÚIteratorÚListÚOptionalÚSizedÚTypeVarÚUnion)Údefault_collate)Úfunctional_datapipe)Údataframe_wrapper)ÚIterDataPipe)Ú_check_unpickable_fnÚvalidate_input_colÚCollatorIterDataPipeÚMapperIterDataPipeÚ_T_coT)Ú	covariantÚmapc                   óp   ‡ — e Zd ZU dZeed<   eed<   	 	 d
dededdfˆ fd„Zd„ Zde	e
   fd„Zdefd	„Zˆ xZS )r   aä  
    Applies a function over each item from the source DataPipe (functional name: ``map``).

    The function can be any regular Python function or partial object. Lambda
    function is not recommended as it is not supported by pickle.

    Args:
        datapipe: Source Iterable DataPipe
        fn: Function being applied over each item
        input_col: Index or indices of data which ``fn`` is applied, such as:

            - ``None`` as default to apply ``fn`` to the data directly.
            - Integer(s) is used for list/tuple.
            - Key(s) is used for dict.

        output_col: Index of data where result of ``fn`` is placed. ``output_col`` can be specified
            only when ``input_col`` is not ``None``

            - ``None`` as default to replace the index that ``input_col`` specified; For ``input_col`` with
              multiple indices, the left-most one is used, and other indices will be removed.
            - Integer is used for list/tuple. ``-1`` represents to append result at the end.
            - Key is used for dict. New key is acceptable.

    Example:
        >>> # xdoctest: +SKIP
        >>> from torchdata.datapipes.iter import IterableWrapper, Mapper
        >>> def add_one(x):
        ...     return x + 1
        >>> dp = IterableWrapper(range(10))
        >>> map_dp_1 = dp.map(add_one)  # Invocation via functional form is preferred
        >>> list(map_dp_1)
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        >>> # We discourage the usage of `lambda` functions as they are not serializable with `pickle`
        >>> # Use `functools.partial` or explicitly define the function instead
        >>> map_dp_2 = Mapper(dp, lambda x: x + 1)
        >>> list(map_dp_2)
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    ÚdatapipeÚfnNÚreturnc                 ó  •— t         ‰| �  «        || _        t        |«       || _        || _        |€|�t        d«      ‚t        |t        t        f«      rt        |«      dkD  rt        d«      ‚|d   }|| _        t        ||«       y )Nz3`output_col` must be None when `input_col` is None.é   z3`output_col` must be a single-element list or tupler   )ÚsuperÚ__init__r   r   r   Ú	input_colÚ
ValueErrorÚ
isinstanceÚlistÚtupleÚlenÚ
output_colr   )Úselfr   r   r    r&   Ú	__class__s        €úk/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torch/utils/data/datapipes/iter/callable.pyr   zMapperIterDataPipe.__init__E   s„   ø€ ô 	‰ÑÔØ ˆŒä˜RÔ ØˆŒà"ˆŒØÐ Ð!7ÜÐRÓSÐSÜ�j¤4¬ -Ô0Ü�:‹ Ò"Ü Ð!VÓWÐWØ# A™ˆJØ$ˆŒÜ˜2˜yÕ)ó    c                 ó
  ‡— | j                   €| j                  €| j                  ‰«      S | j                   €| j                  ‰«      }nlt        | j                   t        t
        f«      r.t        ˆfd„| j                   D «       «      } | j                  |Ž }n| j                  ‰| j                      «      }t        ‰t
        «      rd}t	        ‰«      Šnd}| j                  €et        | j                   t        t
        f«      r5|‰| j                   d   <   t        | j                   dd  d¬«      D ]  }‰|= Œ n@|‰| j                   <   n0| j                  dk(  r‰j                  |«       n|‰| j                  <   |rt        ‰«      S ‰S )Nc              3   ó(   •K  — | ]	  }‰|   –— Œ y ­w©N© )Ú.0ÚcolÚdatas     €r)   ú	<genexpr>z/MapperIterDataPipe._apply_fn.<locals>.<genexpr>c   s   øè ø€ Ò= s˜˜c�Ñ=ùs   ƒTFr   r   )Úreverseéÿÿÿÿ)r    r&   r   r"   r#   r$   ÚsortedÚappend)r'   r1   ÚresÚargsÚt_flagÚidxs    `    r)   Ú	_apply_fnzMapperIterDataPipe._apply_fn\   sF  ø€ Ø�>‰>Ð! d§o¡oÐ&=Ø—7‘7˜4“=Ð à�>‰>Ð!Ø—'‘'˜$“-‰CÜ˜Ÿ™¬¬u¨Ô6ÜÓ=¨d¯n©nÔ=Ó=ˆDØ�$—'‘'˜4�.‰Cà—'‘'˜$˜tŸ~™~Ñ.Ó/ˆCô �dœEÔ"ØˆFÜ˜“:‰DàˆFà�?‰?Ð"Ü˜$Ÿ.™.¬4´¨-Ô8Ø*-��T—^‘^ AÑ&Ñ'Ü! $§.¡.°°Ð"4¸dÔCò "�CØ˜S™	ñ"ð (+��T—^‘^Ò$à�‰ "Ò$Ø—‘˜CÕ à(+��T—_‘_Ñ%ñ %Œu�T‹{Ð.¨$Ð.r*   c              #   óT   K  — | j                   D ]  }| j                  |«      –— Œ y ­wr-   )r   r;   )r'   r1   s     r)   Ú__iter__zMapperIterDataPipe.__iter__   s(   è ø€ Ø—M‘Mò 	'ˆDØ—.‘. Ó&Ó&ñ	'ùs   ‚&(c                 ó¢   — t        | j                  t        «      rt        | j                  «      S t	        t        | «      j                  › d�«      ‚)Nz# instance doesn't have valid length)r"   r   r
   r%   Ú	TypeErrorÚtypeÚ__name__)r'   s    r)   Ú__len__zMapperIterDataPipe.__len__ƒ   s=   € Ü�d—m‘m¤UÔ+Ü�t—}‘}Ó%Ð%Üœ4 ›:×.Ñ.Ð/Ð/RÐSÓTÐTr*   )NN)rA   Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   r;   r   r   r=   ÚintrB   Ú__classcell__©r(   s   @r)   r   r      sc   ø… ñ%ðN ÓØƒLð Øñ*àð*ð ð*ð 
õ*ò.!/ðF'˜( 5™/ó 'ðU˜÷ Ur*   c                 ó.  — t        |j                  «      dkD  rt        d«      ‚|d   }t        j                  |«      }g }g }| j                  «       D ]  }||vsŒt        d«      ‚ |D ]|  }|| v rt        | |   «      st        d«      ‚| |   }n!	 dd lm} |j                  j                  «       }|j                  t        |«      «        |||   «      }
|j                  |
«       Œ~ t        d|«      } ||Ž }|S # t        $ r}	t        d«      |	‚d }	~	ww xY w)Nr   z%Only supports one DataFrame per batchr   zConversion keys missmatchz5Collate (DF)DataPipe requires callable as dict valuesz?unable to import default collation function from the TorchArrowÚCollateResult)r%   ÚitemsÚRuntimeErrorÚ
df_wrapperÚget_columnsÚkeysÚcallableÚtorcharrow.pytorchÚpytorchÚrecÚDefaultÚ	Exceptionr6   Ústrr   )Ú
conversionÚitemÚdfÚcolumns_nameÚtuple_namesÚtuple_valuesÚnameÚcollation_fnÚtapÚeÚvalueÚtpl_clsr$   s                r)   Ú_collate_helperrd   ‰   s7  € ä
ˆ4�:‰:ƒ˜ÒäÐBÓCÐCØ	ˆa‰€BÜ×)Ñ)¨"Ó-€LØ€KØ€Là—‘Ó!ò <ˆØ�|Ò#ÜÐ:Ó;Ð;ð<ð ò #ˆØ�:ÑÜ˜J tÑ,Ô-Ü"ØKóð ð & dÑ+‰LðÝ0à"Ÿw™wŸ™Ó0�ð 	×Ñœ3˜t›9Ô%Ù˜R ™XÓ&ˆØ×Ñ˜EÕ"ð)#ô0 ˜¨+Ó6€GÙ�\Ð"€EØ€Løô ò Ü"ØUóàðûðús   Â C:Ã:	DÄDÄDÚcollatec                   ót   ‡ — e Zd ZdZedfdedeedef   e	ee
ef   eeef   f   df   dee   ddfˆ fd„Zˆ xZS )	r   af  
    Collates samples from DataPipe to Tensor(s) by a custom collate function (functional name: ``collate``).

    By default, it uses :func:`torch.utils.data.default_collate`.

    .. note::
        While writing a custom collate function, you can import :func:`torch.utils.data.default_collate` for the
        default behavior and `functools.partial` to specify any additional arguments.

    Args:
        datapipe: Iterable DataPipe being collated
        collate_fn: Customized collate function to collect and combine data or a batch of data.
            Default function collates to Tensor(s) based on data type.

    Example:
        >>> # xdoctest: +SKIP
        >>> # Convert integer data to float Tensor
        >>> class MyIterDataPipe(torch.utils.data.IterDataPipe):
        ...     def __init__(self, start, end):
        ...         super(MyIterDataPipe).__init__()
        ...         assert end > start, "this example code only works with end >= start"
        ...         self.start = start
        ...         self.end = end
        ...
        ...     def __iter__(self):
        ...         return iter(range(self.start, self.end))
        ...
        ...     def __len__(self):
        ...         return self.end - self.start
        ...
        >>> ds = MyIterDataPipe(start=3, end=7)
        >>> print(list(ds))
        [3, 4, 5, 6]
        >>> def collate_fn(batch):
        ...     return torch.tensor(batch, dtype=torch.float)
        ...
        >>> collated_ds = CollateIterDataPipe(ds, collate_fn=collate_fn)
        >>> print(list(collated_ds))
        [tensor(3.), tensor(4.), tensor(5.), tensor(6.)]
    Nr   rX   .Ú
collate_fnr   c                 ó¾   •— |�t         ‰| �  ||¬«       y t        |«      rt         ‰| �  ||¬«       y t        j                  t
        |«      }t         ‰| �  ||¬«       y )N)r   )r   r   rQ   Ú	functoolsÚpartialrd   )r'   r   rX   rg   r(   s       €r)   r   zCollatorIterDataPipe.__init__ß   s]   ø€ ð Ð!Ü‰GÑ˜X¨*ÐÕ5ä˜
Ô#Ü‘Ñ  ¨jÐ Õ9ô '×.Ñ.¬À
ÓK�
Ü‘Ñ  ¨jÐ Õ9r*   )rA   rC   rD   rE   r   r   r   r   r   r   rW   r	   r   rH   rI   s   @r)   r   r   ´   sy   ø„ ñ'ð\ Ø)-ñ:àð:ð Ø�S˜#�XÑ  U¨3°¨8¡_°e¸HÀc¸MÑ6JÐ%JÑ KÈTÐQñ
ð:ð ˜XÑ&ð:ð 
÷:ñ :r*   )ri   Úcollectionsr   Útypingr   r   r   r   r   r	   r
   r   r   Útorch.utils.data._utils.collater   Ú%torch.utils.data.datapipes._decoratorr   Ú$torch.utils.data.datapipes.dataframer   rN   Ú#torch.utils.data.datapipes.datapiper   Ú'torch.utils.data.datapipes.utils.commonr   r   Ú__all__r   r   rd   r   r.   r*   r)   ú<module>rs      s•   ðã Ý "ß W× WÕ Wå ;Ý EÝ PÝ <÷ð Øð€ñ 	� 4Ô(€ñ �UÓôlU˜ eÑ,ó lUó ðlUò^(ñV �YÓô<:Ð-ó <:ó  ñ<:r*   