Ë
    ÌÓ^jš#  ã                   ó  — d dl Z d dlmZ d dlmZmZmZ d dlmZm	Z	 d dl
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)ÚCallableÚIteratorÚSized)ÚAnyÚTypeVar)Ú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))
        >>> # Invocation via functional form is preferred
        ... map_dp_1 = dp.map(add_one)
        >>> 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                 óL  •— t         j                  j                  d«       t        ‰| �  «        || _        t        |«       || _        || _        |€|�t        d«      ‚t        |t        t        f«      rt        |«      dkD  rt        d«      ‚|d   }|| _        t        ||«       y )Nzpython.data_pipes.mapz3`output_col` must be None when `input_col` is None.é   z3`output_col` must be a single-element list or tupler   )ÚtorchÚ_CÚ_log_api_usage_onceÚsuperÚ__init__r   r   r   Ú	input_colÚ
ValueErrorÚ
isinstanceÚlistÚtupleÚlenÚ
output_colr   )Úselfr   r   r   r%   Ú	__class__s        €ús/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/utils/data/datapipes/iter/callable.pyr   zMapperIterDataPipe.__init__H   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>g   s   øè ø€ Ò= s˜˜c�Ñ=ùs   ƒTFr   r   )Úreverseéÿÿÿÿ)r   r%   r   r!   r"   r#   ÚsortedÚappend)r&   r0   Ú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&   r0   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)r@   Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   r:   r   r   r<   ÚintrA   Ú__classcell__©r'   s   @r(   r   r      sc   ø… ñ&ðP ÓØƒLð Øñ*àð*ð ð*ð 
õ*ò0!/ðF'˜( 5™/ó 'ðU˜÷ Ur)   c                 ó  — t        |j                  «      dkD  rt        d«      ‚|d   }t        j                  |«      }g }g }| 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 mismatchz5Collate (DF)DataPipe requires callable as dict valuesz?unable to import default collation function from the TorchArrowÚCollateResult)r$   ÚitemsÚRuntimeErrorÚ
df_wrapperÚget_columnsÚcallableÚtorcharrow.pytorchÚpytorchÚrecÚDefaultÚ	Exceptionr5   Ústrr   )Ú
conversionÚitemÚdfÚcolumns_nameÚtuple_namesÚtuple_valuesÚnameÚcollation_fnÚtapÚeÚvalueÚtpl_clsr#   s                r(   Ú_collate_helperrb   Ž   s0  € ä
ˆ4�:‰:ƒ˜ÒäÐBÓCÐCØ	ˆa‰€BÜ×)Ñ)¨"Ó-€LØ€KØ€Làò ;ˆØ�|Ò#ÜÐ9Ó:Ð:ð;ð ò #ˆØ�:ÑÜ˜J tÑ,Ô-Ü"ØKóð ð & dÑ+‰LðÝ0à"Ÿw™wŸ™Ó0�ð 	×Ñœ3˜t›9Ô%Ù˜R ™XÓ&ˆØ×Ñ˜EÕ"ð)#ô0 ˜¨+Ó6€GÙ�\Ð"€EØ€Løô ò Ü"ØUóàðûðús   Â C,Ã,	DÃ5DÄDÚcollatec            	       ól   ‡ — e Zd ZdZedfdededef   ee	ez  eez  f   z  dz  dedz  ddfˆ fd„Z
ˆ xZS )	r   aI  
    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 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   rV   .Ú
collate_fnr   c                 ó¾   •— |�t         ‰| �  ||¬«       y t        |«      rt         ‰| �  ||¬«       y t        j                  t
        |«      }t         ‰| �  ||¬«       y )N)r   )r   r   rO   Ú	functoolsÚpartialrb   )r&   r   rV   re   r'   s       €r(   r   zCollatorIterDataPipe.__init__â   s]   ø€ ð Ð!Ü‰GÑ˜X¨*ÐÕ5ä˜
Ô#Ü‘Ñ  ¨jÐ Õ9ô '×.Ñ.¬À
ÓK�
Ü‘Ñ  ¨jÐ Õ9r)   )r@   rB   rC   rD   r	   r   r   r   ÚdictrU   r   rG   rH   s   @r(   r   r   ¹   sp   ø„ ñ%ðX !Ø&*ñ:àð:ð ˜S #˜XÑ&Ø
ˆs�S‰y˜( S™.Ð(Ñ
)ñ*à
ñð:ð ˜t‘Oð:ð 
÷:ñ :r)   )rg   Úcollectionsr   Úcollections.abcr   r   r   Útypingr   r   r   Útorch.utils.data._utils.collater	   Ú%torch.utils.data.datapipes._decoratorr
   Ú$torch.utils.data.datapipes.dataframer   rM   Ú#torch.utils.data.datapipes.datapiper   Ú'torch.utils.data.datapipes.utils.commonr   r   Ú__all__r   r   rb   r   r-   r)   r(   ú<module>rs      s˜   ðã Ý "ß 5Ñ 5ß ã Ý ;Ý EÝ PÝ <÷ð Øð€ñ 	� 4Ô(€ñ �UÓôoU˜ eÑ,ó oUó ðoUòd(ñV �YÓô::Ð-ó ::ó  ñ::r)   