
      i                        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/Volumes/fast/ai/experiments/voice-extract-mac/.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    &  dCi0@  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]     dF#
65<<(HV[[`vMv%)F>"dggf//"r   )r   function)r0   strr   r   r   r	   r   )r+   r   r5   s    ` r   move_data_to_devicer8   N   sB    " &#f%
s 
s 
 u,AH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   s.    &7"	?"4OFS '8tV,,r   c                 h    dt         dt        t        t        t        f   fd}t        | t         |      S )zRecursively 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ug%rs  zz|r   )r
   r   intfloatr)   r	   )r-   rC   s     r   convert_tensors_to_scalarsrF   x   s2    v %UD(8"9  tVW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 . . 
75<<u{{34'%,,eii
01
GELL45	E 4c8S#J,>#??@A  bjj+67C 8 Vs  VG  V  VF-S -' -c -6S 6S 6r   