
      i                         d dl Z d dlZd dlmZ d dlmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZ  G d de      Zd	ee   fd
Zy)    N)	lru_cache)OptionalUnion)override)Accelerator)_AcceleratorRegistryc                   |   e Zd ZdZedej                  ddfd       Zedd       Ze	ede
eeee   f   deee      fd              Ze	ede
eeee   f   deej                     fd	              Ze	edefd
              Ze	e ed      defd                     Ze	edefd              Zeededdfd              Zy)MPSAcceleratorzAccelerator for Metal Apple Silicon GPU devices.

    .. warning::  Use of this accelerator beyond import and instantiation is experimental.

    devicereturnNc                 @    |j                   dk7  rt        d| d      y)zd
        Raises:
            ValueError:
                If the selected device is not MPS.
        mpszDevice should be MPS, got z	 instead.N)type
ValueError)selfr   s     v/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning/fabric/accelerators/mps.pysetup_devicezMPSAccelerator.setup_device!   s*     ;;%9&KLL      c                      y )N )r   s    r   teardownzMPSAccelerator.teardown+   s    r   devicesc                 "    ddl m}  || d      S )z!Accelerator device parsing logic.r   )_parse_gpu_idsT)include_mps)(lightning.fabric.utilities.device_parserr   )r   r   s     r   parse_deviceszMPSAccelerator.parse_devices/   s     	Lg488r   c                     t         j                  |       }|J t        t        |            D cg c]  }t	        j
                  d|       c}S c c}w )z*Gets parallel devices for the Accelerator.r   )r
   r   rangelentorchr   )r   parsed_devicesis      r   get_parallel_devicesz#MPSAccelerator.get_parallel_devices7   sO     (55g>)))05c.6I0JK0J1UA&0JKKKs   Ac                       y)z!Get the devices when set to auto.   r   r   r   r   auto_device_countz MPSAccelerator.auto_device_count?   s     r   r&   c                      t        j                  dd      dk(  } |  xr@ t        j                  j                  j                         xr t        j                         dv S )zOMPS is only available on a machine with the ARM-based Apple Silicon processors.DISABLE_MPS01)armarm64)osgetenvr!   backendsr   is_availableplatform	processor)mps_disableds    r   r1   zMPSAccelerator.is_availableE   sN    
 yy4;rENN$6$6$C$C$Er(J\J\J^brJrrr   c                       y)Nr   r   r   r   r   namezMPSAccelerator.nameM   s     r   accelerator_registryc                 \    |j                  | j                         | | j                         y )N)description)registerr6   __name__)clsr7   s     r   register_acceleratorsz$MPSAccelerator.register_acceleratorsR   s*     	%%HHJ 	& 	
r   )r   N)r;   
__module____qualname____doc__r   r!   r   r   r   staticmethodr   intstrlistr   r   r$   r'   r   boolr1   r6   classmethodr   r=   r   r   r   r
   r
      ss    M5<< MD M M   9uS#tCy%89 9htCy>Q 9  9 LeCd3i,?&@ LT%,,EW L  L s    q\s$ s   s
 #    
9M 
RV 
  
r   r
   r   c                  4    t         j                         rdgS g S )z;
    Returns:
        A list of all available MPS GPUs
    r   )r
   r1   r   r   r   _get_all_available_mps_gpusrH   \   s    
 !--/A37R7r   )r.   r2   	functoolsr   typingr   r   r!   typing_extensionsr   )lightning.fabric.accelerators.acceleratorr   &lightning.fabric.accelerators.registryr   r
   rD   rB   rH   r   r   r   <module>rN      s<    
   "  & A G?
[ ?
D8T#Y 8r   