a
    d
                     @  s   d dl mZ d dl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 d d	lmZmZ d d
lmZ G dd deZdS )    )annotations)AnyN)Tensor)Module)Accelerator)CheckpointIO)	Precision)Strategy
TBroadcast)_DEVICEc                      s   e Zd ZdZd+ddddd fd	d
ZeddddZeddddZdddddZdddddddZ	d,dddddd d!Z
dddd"d#d$Zd-d&d'd&d(d)d*Z  ZS ).SingleDeviceStrategyz7Strategy that handles communication on a single device.cpuNr   zAccelerator | NonezCheckpointIO | NonezPrecision | None)deviceacceleratorcheckpoint_io	precisionc                   s4   t  j|||d t|| _d| _d| _d| _d S )N)r   r   r   r      )super__init__torchr   _root_deviceZglobal_rankZ
local_rankZ
world_size)selfr   r   r   r   	__class__ r/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_fabric/strategies/single_device.pyr       s
    zSingleDeviceStrategy.__init__ztorch.device)returnc                 C  s   | j S N)r   r   r   r   r   root_device-   s    z SingleDeviceStrategy.root_deviceboolc                 C  s   dS )NTr   r   r   r   r   is_global_zero1   s    z#SingleDeviceStrategy.is_global_zeror   None)moduler   c                 C  s   | | j d S r   )tor   )r   r#   r   r   r   module_to_device5   s    z%SingleDeviceStrategy.module_to_devicezAny | Tensorr   )tensorargskwargsr   c                 O  s   |S )a  Reduces a tensor from several distributed processes to one aggregated tensor. As this plugin only
        operates with a single device, the reduction is simply the identity.

        Args:
            tensor: the tensor to sync and reduce
            *args: ignored
            **kwargs: ignored

        Return:
            the unmodified input as reduction is not needed for single process operation
        r   )r   r&   r'   r(   r   r   r   
all_reduce8   s    zSingleDeviceStrategy.all_reduceFr   z
Any | None)r&   group
sync_gradsr   c                 C  s   |S )z&Perform a all_gather on all processes.r   )r   r&   r*   r+   r   r   r   
all_gatherF   s    zSingleDeviceStrategy.all_gather)r'   r(   r   c                 O  s   d S r   r   )r   r'   r(   r   r   r   barrierJ   s    zSingleDeviceStrategy.barrierr   r
   int)objsrcr   c                 C  s   |S r   r   )r   r/   r0   r   r   r   	broadcastM   s    zSingleDeviceStrategy.broadcast)r   NNN)NF)r   )__name__
__module____qualname____doc__r   propertyr   r!   r%   r)   r,   r-   r1   __classcell__r   r   r   r   r      s       r   )
__future__r   typingr   r   r   Ztorch.nnr   Zlightning_fabric.acceleratorsr   Z)lightning_fabric.plugins.io.checkpoint_ior   Z"lightning_fabric.plugins.precisionr   Z$lightning_fabric.strategies.strategyr	   r
   Z lightning_fabric.utilities.typesr   r   r   r   r   r   <module>   s   