§
    X�’jŽ$  ã                   ó6  — 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dS )é    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                   óv   ‡ — 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                 ó®  •— t           j                             d¦  «         t          ¦   «                              ¦   «          || _        t          |¦  «         || _        || _        |€|�t          d¦  «        ‚t          |t          t          f¦  «        r*t          |¦  «        dk    rt          d¦  «        ‚|d         }|| _        t          ||¦  «         d S )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        €úVC:\Projects\DSCons\.venv\Lib\site-packages\torch\utils\data\datapipes\iter\callable.pyr   zMapperIterDataPipe.__init__H   sË   ø€ õ 	Œ×$Ò$Ð%<Ñ=Ô=Ð=Ý‰Œ×ÒÑÔÐØ ˆŒå˜RÑ Ô Ð ØˆŒà"ˆŒØÐ Ð!7ÝÐRÑSÔSÐSÝ�j¥4­ -Ñ0Ô0ð 	'Ý�:‰Œ Ò"Ð"Ý Ð!VÑWÔWÐWØ# AœˆJØ$ˆŒÝ˜2˜yÑ)Ô)Ð)Ð)Ð)ó    c                 óú  ‡— | j         €| j        €|                      ‰¦  «        S | j         €|                      ‰¦  «        }nlt          | j         t          t
          f¦  «        r+t          ˆfd„| j         D ¦   «         ¦  «        } | j        |Ž }n |                      ‰| j                  ¦  «        }t          ‰t
          ¦  «        rd}t	          ‰¦  «        Šnd}| j        €at          | j         t          t
          f¦  «        r5|‰| j         d         <   t          | j         dd …         d¬¦  «        D ]}‰|= Œn6|‰| j         <   n+| j        dk    r‰                     |¦  «         n
|‰| j        <   |rt          ‰¦  «        n‰S )Nc              3   ó(   •K  — | ]}‰|         V — Œd S ©N© )Ú.0ÚcolÚdatas     €r(   ú	<genexpr>z/MapperIterDataPipe._apply_fn.<locals>.<genexpr>g   s'   øè è € Ð=Ð= s˜˜cœÐ=Ð=Ð=Ð=Ð=Ð=r)   TFr   r   )Úreverseéÿÿÿÿ)r   r%   r   r!   r"   r#   ÚsortedÚappend)r&   r0   ÚresÚargsÚt_flagÚidxs    `    r(   Ú	_apply_fnzMapperIterDataPipe._apply_fn`   s…  ø€ ØŒ>Ð! d¤oÐ&=Ø—7’7˜4‘=”=Ð àŒ>Ð!Ø—'’'˜$‘-”-ˆCˆCÝ˜œ­­u¨Ñ6Ô6ð 	0ÝÐ=Ð=Ð=Ð=¨d¬nÐ=Ñ=Ô=Ñ=Ô=ˆDØ�$”'˜4�.ˆCˆCà—'’'˜$˜tœ~Ô.Ñ/Ô/ˆCõ �d�EÑ"Ô"ð 	ØˆFÝ˜‘:”:ˆDˆDàˆFàŒ?Ð"Ý˜$œ.­4µ¨-Ñ8Ô8ð +Ø*-��T”^ AÔ&Ñ'Ý! $¤.°°°Ô"4¸dÐCÑCÔCð "ð "�CØ˜S˜	˜	ð"ð (+��T”^Ñ$Ð$àŒ "Ò$Ð$Ø—’˜CÑ Ô Ð Ð à(+��T”_Ñ%ð %Ð.�u�T‰{Œ{ˆ{¨$Ð.r)   c              #   óL   K  — | j         D ]}|                      |¦  «        V — Œd S r,   )r   r:   )r&   r0   s     r(   Ú__iter__zMapperIterDataPipe.__iter__ƒ   s<   è è € Ø”Mð 	'ð 	'ˆDØ—.’. Ñ&Ô&Ð&Ð&Ð&Ð&ð	'ð 	'r)   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__‡   sF   € å�d”m¥UÑ+Ô+ð 	&Ý�t”}Ñ%Ô%Ð%Ý�4 ™:œ:Ô.ÐSÐSÐSÑTÔTÐTr)   )NN)r@   Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   r:   r   r   r<   ÚintrA   Ú__classcell__©r'   s   @r(   r   r      sÝ   ø€ € € € € € ð&ð &ðP ÐÐÑØ€L€L�Lð Øð*ð *àð*ð ð*ð 
ð*ð *ð *ð *ð *ð *ð0!/ð !/ð !/ðF'˜( 5œ/ð 'ð 'ð 'ð 'ðU˜ð Uð Uð Uð Uð Uð Uð Uð Ur)   c                 óp  — t          |j        ¦  «        dk    rt          d¦  «        ‚|d         }t          j        |¦  «        }g }g }| D ]}||vrt          d¦  «        ‚Œ|D ]¾}|| v r-t          | |         ¦  «        st          d¦  «        ‚| |         }nC	 dd lm} |j         	                    ¦   «         }n"# t          $ r}	t          d¦  «        |	‚d }	~	ww xY w|                     t          |¦  «        ¦  «          |||         ¦  «        }
|                     |
¦  «         Œ¿t          d|¦  «        } ||Ž }|S )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   Ž   s’  € å
ˆ4Œ:�„˜ÒÐåÐBÑCÔCÐCØ	ˆaŒ€BÝÔ)¨"Ñ-Ô-€LØ€KØ€Làð ;ð ;ˆØ�|Ð#Ð#ÝÐ9Ñ:Ô:Ð:ð $ð ð #ð #ˆØ�:ÐÐÝ˜J tÔ,Ñ-Ô-ð Ý"ØKñô ð ð & dÔ+ˆLˆLðØ0Ð0Ð0Ð0Ð0Ð0à"œwŸšÑ0Ô0��øÝð ð ð Ý"ØUñô àðøøøøðøøøð
 	×Ò�3˜t™9œ9Ñ%Ô%Ð%Ø�˜R œXÑ&Ô&ˆØ×Ò˜EÑ"Ô"Ð"Ð"õ ˜¨+Ñ6Ô6€GØˆG�\Ð"€EØ€Ls   ÂB6Â6
CÃ CÃCÚcollatec            	       óx   ‡ — 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                 ó8  •— |�%t          ¦   «                              ||¬¦  «         d S t          |¦  «        r%t          ¦   «                              ||¬¦  «         d S t          j        t
          |¦  «        }t          ¦   «                              ||¬¦  «         d S )N)r   )r   r   rO   Ú	functoolsÚpartialrb   )r&   r   rV   re   r'   s       €r(   r   zCollatorIterDataPipe.__init__â   s˜   ø€ ð Ð!Ý‰GŒG×Ò˜X¨*ÐÑ5Ô5Ð5Ð5Ð5å˜
Ñ#Ô#ð :Ý‘”× Ò  ¨jÐ Ñ9Ô9Ð9Ð9Ð9õ 'Ô.­À
ÑKÔK�
Ý‘”× Ò  ¨jÐ Ñ9Ô9Ð9Ð9Ð9r)   )r@   rB   rC   rD   r	   r   r   r   ÚdictrU   r   rG   rH   s   @r(   r   r   ¹   sª   ø€ € € € € ð%ð %ð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Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø Ð Ð Ð Ð Ð Ð Ð à €€€Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø EÐ EÐ EÐ EÐ EÐ EØ PÐ PÐ PÐ PÐ PÐ PØ <Ð <Ð <Ð <Ð <Ð <ðð ð ð ð ð ð ð ð Øð€ð 	ˆ� 4Ð(Ñ(Ô(€ð Ð�UÑÔðoUð oUð oUð oUð oU˜ eÔ,ñ oUô oUñ ÔðoUðd(ð (ð (ðV Ð�YÑÔð::ð ::ð ::ð ::ð ::Ð-ñ ::ô ::ñ  Ôð::ð ::ð ::r)   