a
    d                     @   s0  U d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
 ddlZddlZddlmZ ddlmZ ddlmZ d	Zejeed
ddZeeejejdfeeejejdfeeejejdfejefgZee	eeeegef f  ed< G dd deZeeedddZeeedddZeedddZ dS )zUtilities used for collections.    )ABC)partial)AnyCallableListTupleUnionN)apply_to_collection)Tensor)_DEVICE)cpuZmps)valuedevicereturnc                 C   s   t | |S )N)torchZ
from_numpyto)r   r    r   n/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_fabric/utilities/apply_func.py_from_numpy   s    r   )dtypeCONVERSION_DTYPESc                   @   s,   e Zd ZdZeeeeef dddZdS )_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                 C   s    | t u rt|dd }t|S tS )Nr   )r   getattrcallableNotImplemented)clsr   r   r   r   r   __subclasshook__=   s    z&_TransferableDataType.__subclasshook__N)	__name__
__module____qualname____doc__classmethodr   r   boolr   r   r   r   r   r   *   s   r   )batchr   r   c                    s6   t  trt  ttd fdd}t| t|dS )aj  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                    sN   i }t | tr,t  tjr, jtvr,d|d< | j fi |}|d urJ|S | S )NTZnon_blocking)
isinstancer
   r   r   type_BLOCKING_DEVICE_TYPESr   )r&   kwargsZdata_outputr   r   r   batch_toY   s     z%move_data_to_device.<locals>.batch_to)r   function)r'   strr   r   r   r	   r   )r$   r   r,   r   r+   r   move_data_to_deviceE   s    

r/   )r&   r   r   c                 C   s(   t D ]\}}t| |||d} qt| |S )Nr+   )r   r	   r/   )r&   r   Z	src_dtypeZconversion_funcr   r   r   convert_to_tensorsh   s    r0   r%   c                 C   s&   t ttttf dd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                 S   s$   |   dkrtd|  d|  S )N   zThe metric `zM` does not contain a single element, thus it cannot be converted to a scalar.)Znumel
ValueErroritem)r   r   r   r   to_itemw   s
    
z+convert_tensors_to_scalars.<locals>.to_item)r
   r   intfloatr#   r	   )r&   r4   r   r   r   convert_tensors_to_scalarso   s    r7   )!r!   abcr   	functoolsr   typingr   r   r   r   r   numpynpr   Z#lightning_utilities.core.apply_funcr	   r
   Z lightning_fabric.utilities.typesr   r)   Zndarrayr   r#   ZtensorZuint8r5   r6   r   __annotations__r   r/   r0   r7   r   r   r   r   <module>   s&   $	#