
    shi#                         S SK r S SKJr  S SKJrJr  S SKJrJrJ	r	J
r
Jr  S SKJr  S SKJr  S SKJr  S SKJr  S S	KJrJr  S
S/r\
" SSS9r\" S5       " S S\\   5      5       rS r\" S5       " S S
\5      5       rg)    N)
namedtuple)IteratorSized)AnyCallableOptionalTypeVarUnion)default_collate)functional_datapipe)dataframe_wrapper)IterDataPipe)_check_unpickable_fnvalidate_input_colCollatorIterDataPipeMapperIterDataPipe_T_coT)	covariantmapc                      ^  \ rS rSr% Sr\\S'   \\S'     SS\S\SS4U 4S jjjrS r	S\
\   4S	 jrS\4S
 jrSrU =r$ )r      ah  
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U ]  5         Xl        [        U5        X l        X0l        Uc  Ub  [        S5      e[        U[        [        45      (       a  [        U5      S:  a  [        S5      eUS   nX@l        [        X#5        g )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        |/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/utils/data/datapipes/iter/callable.pyr   MapperIterDataPipe.__init__F   s}     	 R "!7RSSj4-00:" !VWW#AJ$2)    c                 <  ^ U R                   c  U R                  c  U R                  T5      $ U R                   c  U R                  T5      nOr[        U R                   [        [
        45      (       a/  [        U4S jU R                    5       5      nU R                  " U6 nOU R                  TU R                      5      n[        T[
        5      (       a  Sn[	        T5      mOSnU R                  ci  [        U R                   [        [
        45      (       a4  UTU R                   S   '   [        U R                   SS  SS9 H  nTU	 M     OAUTU R                   '   O1U R                  S:X  a  TR                  U5        OUTU R                  '   U(       a  [        T5      $ T$ )Nc              3   .   >#    U  H
  nTU   v   M     g 7fN ).0coldatas     r(   	<genexpr>/MapperIterDataPipe._apply_fn.<locals>.<genexpr>d   s     =nscns   TFr   r   )reverse)r   r%   r   r!   r"   r#   sortedappend)r&   r1   resargst_flagidxs    `    r(   	_apply_fnMapperIterDataPipe._apply_fn]   sI   >>!doo&=774= >>!''$-Cu66=dnn==D''4.C''$t~~./C dE""F:DF??"$..4-88*-T^^A&'!$.."4dCCS	 D (+T^^$"$C (+T__% %uT{.$.r*   c              #   X   #    U R                    H  nU R                  U5      v   M     g 7fr-   )r   r<   )r&   r1   s     r(   __iter__MapperIterDataPipe.__iter__   s"     MMD..&& "s   (*c                     [        U R                  [        5      (       a  [        U R                  5      $ [	        [        U 5      R                   S35      e)Nz# instance doesn't have valid length)r!   r   r   r$   	TypeErrortype__name__)r&   s    r(   __len__MapperIterDataPipe.__len__   s@    dmmU++t}}%%4:..//RSTTr*   )r   r   r   r%   )NN)rD   
__module____qualname____firstlineno____doc__r   __annotations__r   r   r<   r   r   r?   intrE   __static_attributes____classcell__r'   s   @r(   r   r      sn    %N L ** * 
* *.!/F'(5/ 'U U Ur*   c                 D   [        UR                  5      S:  a  [        S5      eUS   n[        R                  " U5      n/ n/ nU R                  5        H  nXc;  d  M
  [        S5      e   U H  nX`;   a"  [        X   5      (       d  [        S5      eX   nO! SS KJn  UR                  R                  5       nUR                  [        U5      5        U" X&   5      n
UR                  U
5        M     [        SU5      nU" U6 nU$ ! [         a  n	[        S5      U	eS n	A	ff = f)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	Exceptionr7   strr   )
conversionitemdfcolumns_nametuple_namestuple_valuesnamecollation_fntapevaluetpl_clsr#   s                r(   _collate_helperrj      s&   
4::BCC	aB))"-LKL!#:;; " J,--"K  &+L0"ww0 	3t9%RX&E") 0 +6G\"EL  "Us    D
DDDcollatec                      ^  \ rS rSrSr\S4S\S\\S\	4   \
\\\	4   \\\	4   4   S4   S\\   SS4U 4S	 jjjrS
rU =r$ )r      a  
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   r^   .
collate_fnr   c                    > Ub  [         TU ]  XS9  g [        U5      (       a  [         TU ]  XS9  g [        R                  " [
        U5      n[         TU ]  XS9  g )N)r   )r   r   rW   	functoolspartialrj   )r&   r   r^   rn   r'   s       r(   r   CollatorIterDataPipe.__init__   sZ     !GX5
##  9 '..
K
  9r*   r.   )rD   rG   rH   rI   rJ   r   r   r
   r   r   dictr]   r   r   rM   rN   rO   s   @r(   r   r      sy    '\ )-:: S#XU38_eHcM6J%J KTQ
: X&: 
: :r*   )rp   collectionsr   collections.abcr   r   typingr   r   r   r	   r
   torch.utils.data._utils.collater   %torch.utils.data.datapipes._decoratorr   $torch.utils.data.datapipes.dataframer   rT   #torch.utils.data.datapipes.datapiper   'torch.utils.data.datapipes.utils.commonr   r   __all__r   r   rj   r   r.   r*   r(   <module>r}      s     " + : : ; E P <  	4( UlUe, lU lU^(V Y<:- <:  <:r*   