o
    oi                     @   s   d dl mZmZmZmZ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 d d	lmZ d d
lmZ G dd deZdZdZdZdeeef fddZdS )    )AnyDictListOptionalUnionN)override)_AcceleratorRegistry)MPSAccelerator_parse_gpu_ids)_DEVICE)Accelerator)_PSUTIL_AVAILABLE)MisconfigurationExceptionc                   @   s   e Zd ZdZedejddfddZedede	e
ef fddZedd	d
Zeedeee
ee f deee  fddZeedeee
ee f deej fddZeedefddZeedefddZeededdfddZdS )r	   zAccelerator for Metal Apple Silicon GPU devices.

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

    devicereturnNc                 C   s   |j dkrtd| ddS )zs
        Raises:
            MisconfigurationException:
                If the selected device is not MPS.
        mpszDevice should be MPS, got z	 instead.N)typer   selfr    r   `/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/pytorch_lightning/accelerators/mps.pysetup_device#   s   
zMPSAccelerator.setup_devicec                 C   s   t  S )z1Get M1 (cpu + gpu) stats from ``psutil`` package.)get_device_statsr   r   r   r   r   -   s   zMPSAccelerator.get_device_statsc                 C   s   d S )Nr   )r   r   r   r   teardown2   s   zMPSAccelerator.teardowndevicesc                 C   s   t | ddS )z!Accelerator device parsing logic.T)include_mpsr
   )r   r   r   r   parse_devices6   s   zMPSAccelerator.parse_devicesc                 C   s,   t | }|dusJ dd tt|D S )z*Gets parallel devices for the Accelerator.Nc                 S   s   g | ]}t d |qS )r   )torchr   ).0ir   r   r   
<listcomp>C   s    z7MPSAccelerator.get_parallel_devices.<locals>.<listcomp>)r	   r   rangelen)r   parsed_devicesr   r   r   get_parallel_devices<   s   
z#MPSAccelerator.get_parallel_devicesc                   C   s   dS )z!Get the devices when set to auto.   r   r   r   r   r   auto_device_countE   s   z MPSAccelerator.auto_device_countc                   C   s   t  S )zOMPS is only available on a machine with the ARM-based Apple Silicon processors.)_MPSAcceleratoris_availabler   r   r   r   r)   K   s   zMPSAccelerator.is_availableaccelerator_registryc                 C   s   |j d| | jd d S )Nr   )description)register__name__)clsr*   r   r   r   register_acceleratorsQ   s
   
z$MPSAccelerator.register_accelerators)r   N)r-   
__module____qualname____doc__r   r   r   r   r   r   strr   r   r   staticmethodr   intr   r   r   r%   r'   boolr)   classmethodr   r/   r   r   r   r   r	      s.    	,*r	   M1_vm_percent
M1_percentM1_swap_percentr   c                  C   s>   t stdtt  dd l} t|  jt|  t	| 
 jiS )Nz=Fetching MPS device stats requires `psutil` to be installed. r   )r   ModuleNotFoundErrorr3   psutil_VM_PERCENTvirtual_memorypercent_PERCENTcpu_percent_SWAP_PERCENTswap_memory)r<   r   r   r   r   a   s   

r   )typingr   r   r   r   r   r   typing_extensionsr   lightning_fabric.acceleratorsr   !lightning_fabric.accelerators.mpsr	   r(   (lightning_fabric.utilities.device_parserr    lightning_fabric.utilities.typesr   *pytorch_lightning.accelerators.acceleratorr   "pytorch_lightning.accelerators.cpur   &pytorch_lightning.utilities.exceptionsr   r=   r@   rB   r3   floatr   r   r   r   r   <module>   s   @