Ë
    ÿÍ:j§  ã                   ó  — U d Z ddlmZ ddlmZ ddlmZmZ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 erddlZd
Zdddedefd„Ze ee
j.                  e
j0                  ¬«      fe ee
j.                  e
j2                  ¬«      fe ee
j.                  e
j4                  ¬«      fgZeeeeeegef   f      ed<   er!ddlZej?                  ej@                  ef«        G d„ de«      Z!dededefd„Z"dededefd„Z#dedefd„Z$y)zUtilities used for collections.é    )ÚABC)Úpartial)ÚTYPE_CHECKINGÚAnyÚCallableÚUnionN)Úapply_to_collection)ÚTensor)Ú_NUMPY_AVAILABLE)Ú_DEVICE)ÚcpuÚmpsÚvaluez
np.ndarrayÚdeviceÚreturnc                 óJ   — t        j                  | «      j                  |«      S )N)ÚtorchÚ
from_numpyÚto)r   r   s     úz/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/lightning_fabric/utilities/apply_func.pyÚ_from_numpyr   !   s   € Ü×Ñ˜EÓ"×%Ñ% fÓ-Ð-ó    )ÚdtypeÚCONVERSION_DTYPESc                   ó4   — e Zd ZdZededeeef   fd„«       Zy)Ú_TransferableDataTypea"  A custom type for data that can be moved to a torch device via ``.to(...)``.

    Example:

        >>> isinstance(dict, _TransferableDataType)
        False
        >>> isinstance(torch.rand(2, 3), _TransferableDataType)
        True
        >>> class CustomObject:
        ...     def __init__(self):
        ...         self.x = torch.rand(2, 2)
        ...     def to(self, device):
        ...         self.x = self.x.to(device)
        ...         return self
        >>> isinstance(CustomObject(), _TransferableDataType)
        True

    Úsubclassr   c                 óN   — | t         u rt        |dd «      }t        |«      S t        S )Nr   )r   ÚgetattrÚcallableÚNotImplemented)Úclsr   r   s      r   Ú__subclasshook__z&_TransferableDataType.__subclasshook__F   s)   € àÔ'Ñ'Ü˜ 4¨Ó.ˆBÜ˜B“<ÐÜÐr   N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   r   Úboolr#   © r   r   r   r   2   s2   „ ñð& ð¨ð °°d¸C°iÑ0@ò ó ñr   r   Úbatchc                 ó–   ‡— t        ‰t        «      rt        j                  ‰«      Šdt        dt        fˆfd„}t        | t        |¬«      S )ak  Transfers a collection of data to the given device. Any object that defines a method ``to(device)`` will be
    moved and all other objects in the collection will be left untouched.

    Args:
        batch: A tensor or collection of tensors or anything that has a method ``.to(...)``.
            See :func:`apply_to_collection` for a list of supported collection types.
        device: The device to which the data should be moved

    Return:
        the same collection but with all contained tensors residing on the new device.

    See Also:
        - :meth:`torch.Tensor.to`
        - :class:`torch.device`

    Údatar   c                 ó¼   •— i }t        | t        «      r1t        ‰t        j                  «      r‰j                  t
        vrd|d<    | j                  ‰fi |¤Ž}|�|S | S )NTÚnon_blocking)Ú
isinstancer
   r   r   ÚtypeÚ_BLOCKING_DEVICE_TYPESr   )r-   ÚkwargsÚdata_outputr   s      €r   Úbatch_toz%move_data_to_device.<locals>.batch_tob   s]   ø€ Øˆô �dœFÔ#¬
°6¼5¿<¹<Ô(HÈVÏ[É[Ô`vÑMvØ%)ˆF�>Ñ"Ø�d—g‘g˜fÑ/¨Ñ/ˆØÐ"ØÐàˆr   )r   Úfunction)r0   Ústrr   r   r   r	   r   )r+   r   r5   s    ` r   Úmove_data_to_devicer8   N   sB   ø€ ô" �&œ#ÔÜ—‘˜fÓ%ˆð
”sð 
œsõ 
ô ˜uÔ,AÈHÔUÐUr   r-   c                 óT   — t         D ]  \  }}t        | |||¬«      } Œ t        | |«      S )N)r   )r   r	   r8   )r-   r   Ú	src_dtypeÚconversion_funcs       r   Úconvert_to_tensorsr<   q   s5   € ä&7ò TÑ"ˆ	�?Ü" 4¨°OÈFÔS‰ðTä˜t VÓ,Ð,r   c                 óh   — dt         dt        t        t        t        f   fd„}t        | t         |«      S )zíRecursively walk through a collection and convert single-item tensors to scalar values.

    Raises:
        ValueError:
            If tensors inside ``metrics`` contains multiple elements, hence preventing conversion to a scalar.

    r   r   c                 óf   — | j                  «       dk7  rt        d| › d�«      ‚| j                  «       S )Né   zThe metric `zM` does not contain a single element, thus it cannot be converted to a scalar.)ÚnumelÚ
ValueErrorÚitem)r   s    r   Úto_itemz+convert_tensors_to_scalars.<locals>.to_item�   s8   € Ø�;‰;‹=˜AÒÜØ˜u˜gÐ%rÐsóð ð �z‰z‹|Ðr   )r
   r   ÚintÚfloatr)   r	   )r-   rC   s     r   Úconvert_tensors_to_scalarsrF   x   s2   € ð”vð ¤%¬¬U´DÐ(8Ñ"9ó ô ˜t¤V¨WÓ5Ð5r   )%r'   Úabcr   Ú	functoolsr   Útypingr   r   r   r   r   Ú#lightning_utilities.core.apply_funcr	   r
   Ú"lightning_fabric.utilities.importsr   Ú lightning_fabric.utilities.typesr   ÚnumpyÚnpr2   r   r)   ÚtensorÚuint8rD   rE   r   ÚlistÚtupleÚ__annotations__ÚappendÚndarrayr   r8   r<   rF   r*   r   r   ú<module>rV      s6  ðò &å Ý ß 6Ó 6ã Ý CÝ å ?Ý 4áÛà'Ð ð.�|ð .¨Wð .¸ó .ð 
‰7�5—<‘< u§{¡{Ô3Ð4Ø‰'�%—,‘, e§i¡iÔ
0Ð1Ø
‰G�E—L‘L¨¯©Ô4Ð5ð	EÐ �4˜˜c 8¨S°#¨J¸Ð,>Ñ#?Ð?Ñ@ÑAó ñ Ûà×Ñ˜bŸj™j¨+Ð6Ô7ô˜Cô ð8 V˜sð  V¨Gð  V¸ó  VðF-˜Sð -¨'ð -°có -ð6 Sð 6¨Sô 6r   