
      ij                        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e      Zy)    )annotations)AnyN)Module)override)Accelerator)CheckpointIO)	Precision)Strategy
TBroadcast)_DEVICEc                       e Zd ZdZ	 	 	 	 d
	 	 	 	 	 	 	 d fdZeedd              Zeedd              Zedd       Z	edd       Z
eddd       Zedd       Zeddd	       Z xZS )SingleDeviceStrategyz7Strategy that handles communication on a single device.c                    t         |   |||       t        |t        j                        st        j                  |      }|| _        d| _        d| _        d| _        y )N)acceleratorcheckpoint_io	precisionr      )	super__init__
isinstancetorchdevice_root_deviceglobal_rank
local_rank
world_size)selfr   r   r   r   	__class__s        ~/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning/fabric/strategies/single_device.pyr   zSingleDeviceStrategy.__init__    sT     	[Ybc&%,,/\\&)F"    c                    | j                   S N)r   r   s    r   root_devicez SingleDeviceStrategy.root_device/   s        r    c                     y)NT r#   s    r   is_global_zeroz#SingleDeviceStrategy.is_global_zero4   s     r    c                :    |j                  | j                         y r"   )tor$   )r   modules     r   module_to_devicez%SingleDeviceStrategy.module_to_device9   s    		$""#r    c                    |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   tensorargskwargss       r   
all_reducezSingleDeviceStrategy.all_reduce=   s	     r    c                    |S )z*Perform a ``all_gather`` on all processes.r&   )r   r-   group
sync_gradss       r   
all_gatherzSingleDeviceStrategy.all_gatherM   s	     r    c                     y r"   r&   )r   r.   r/   s      r   barrierzSingleDeviceStrategy.barrierR   s    r    c                    |S r"   r&   )r   objsrcs      r   	broadcastzSingleDeviceStrategy.broadcastV   s    
r    )cpuNNN)r   r   r   zAccelerator | Noner   zCheckpointIO | Noner   zPrecision | None)returnztorch.device)r<   bool)r*   r   r<   None)r-   Any | torch.Tensorr.   r   r/   r   r<   r?   )NF)r-   torch.Tensorr2   z
Any | Noner3   r=   r<   r@   )r.   r   r/   r   r<   r>   )r   )r8   r   r9   intr<   r   )__name__
__module____qualname____doc__r   propertyr   r$   r'   r+   r0   r4   r6   r:   __classcell__)r   s   @r   r   r      s    A  *.-1&* ( +	
 $ !  !    $ $        r    r   )
__future__r   typingr   r   torch.nnr   typing_extensionsr   lightning.fabric.acceleratorsr   )lightning.fabric.plugins.io.checkpoint_ior   "lightning.fabric.plugins.precisionr	   $lightning.fabric.strategies.strategyr
   r    lightning.fabric.utilities.typesr   r   r&   r    r   <module>rQ      s1    #    & 5 B 8 E 4;8 ;r    