a
    dO                     @  s|   d dl mZ d dlmZ d dl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mZ G d	d
 d
eZdS )    )annotations)AnyN)Tensor)CheckpointIO)_DEVICE)PrecisionPlugin)Strategy
TBroadcastc                      s   e Zd ZdZdZd2ddddd	 fd
dZdddddddZd3dddddddZeddddZ	ddddZ
ddd  fd!d"Zeddd#d$Zdddd%d&d'Zd4d)d*d)d+d,d-Zed.dd/d0d1Z  ZS )5SingleDeviceStrategyz7Strategy that handles communication on a single device.Zsingle_devicecpuNr   z.pl.accelerators.accelerator.Accelerator | NonezCheckpointIO | NonezPrecisionPlugin | None)deviceacceleratorcheckpoint_ioprecision_pluginc                   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__ s/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/strategies/single_device.pyr   !   s
    zSingleDeviceStrategy.__init__zAny | Tensorr   )tensorargskwargsreturnc                 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   reduce.   s    zSingleDeviceStrategy.reduceFr   z
Any | Nonebool)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_gather<   s    zSingleDeviceStrategy.all_gatherztorch.device)r   c                 C  s   | j S N)r   r   r   r   r   root_device@   s    z SingleDeviceStrategy.root_deviceNonec                 C  s$   | j d usJ d| j | j d S )Nz-self.model must be set before self.model.to())modeltor%   r$   r   r   r   model_to_deviceD   s    z$SingleDeviceStrategy.model_to_devicez
pl.Trainer)trainerr   c                   s   |    t | d S r#   )r)   r   setup)r   r*   r   r   r   r+   H   s    zSingleDeviceStrategy.setupc                 C  s   dS )NTr   r$   r   r   r   is_global_zeroL   s    z#SingleDeviceStrategy.is_global_zero)r   r   r   c                 O  s   d S r#   r   )r   r   r   r   r   r   barrierP   s    zSingleDeviceStrategy.barrierr   r	   int)objsrcr   c                 C  s   |S r#   r   )r   r/   r0   r   r   r   	broadcastS   s    zSingleDeviceStrategy.broadcastdict)strategy_registryr   c                 C  s   |j | j| | jj d d S )N)description)registerstrategy_namer   __name__)clsr3   r   r   r   register_strategiesV   s
    z(SingleDeviceStrategy.register_strategies)r   NNN)NF)r   )r7   
__module____qualname____doc__r6   r   r   r"   propertyr%   r)   r+   r,   r-   r1   classmethodr9   __classcell__r   r   r   r   r
      s&       r
   )
__future__r   typingr   r   r   Zpytorch_lightningplZlightning_fabric.pluginsr   Z lightning_fabric.utilities.typesr   Z#pytorch_lightning.plugins.precisionr   Z%pytorch_lightning.strategies.strategyr   r	   r
   r   r   r   r   <module>   s   