a
    d                     @   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 G d	d
 d
eZdZdZdZeeef dddZdS )    )AnyDictListOptionalUnionN)MPSAccelerator_parse_gpu_ids)_DEVICE)Accelerator)MisconfigurationException)_PSUTIL_AVAILABLEc                   @   s   e Zd ZdZejddddZeee	e
f dddZddd	d
Zeeee	ee f eee  dddZeeee	ee f eej dddZeedddZeedddZeeddddZdS )r   z0Accelerator for Metal Apple Silicon GPU devices.N)devicereturnc                 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   k/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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_statsr   c                 C   s   d S )Nr   )r   r   r   r   teardown*   s    zMPSAccelerator.teardown)devicesr   c                 C   s   t | dd}|S )z!Accelerator device parsing logic.T)Zinclude_mpsr   r   Zparsed_devicesr   r   r   parse_devices-   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>9       z7MPSAccelerator.get_parallel_devices.<locals>.<listcomp>)r   r   rangelenr   r   r   r   get_parallel_devices3   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_count;   s    z MPSAccelerator.auto_device_countc                   C   s   t  S )zGMPS is only available for certain torch builds starting at torch>=1.12.)_MPSAcceleratoris_availabler   r   r   r   r(   @   s    zMPSAccelerator.is_available)accelerator_registryr   c                 C   s   |j d| | jjd d S )Nr   )description)register	__class____name__)clsr)   r   r   r   register_acceleratorsE   s
    z$MPSAccelerator.register_accelerators)r-   
__module____qualname____doc__r   r   r   r
   r   strr   r   r   staticmethodr   intr   r   r   r$   r&   boolr(   classmethodr/   r   r   r   r   r      s   	(&r   ZM1_vm_percentZ
M1_percentZM1_swap_percentr   c                  C   s4   t stddd l} t|  jt|  t| 	 jiS )NzjFetching M1 device stats requires `psutil` to be installed. Install it by running `pip install -U psutil`.r   )
r   ModuleNotFoundErrorpsutil_VM_PERCENTZvirtual_memorypercent_PERCENTZcpu_percent_SWAP_PERCENTZswap_memory)r9   r   r   r   r   T   s    

r   )typingr   r   r   r   r   r   Z!lightning_fabric.accelerators.mpsr   r'   Z(lightning_fabric.utilities.device_parserr	   Z lightning_fabric.utilities.typesr
   Z*pytorch_lightning.accelerators.acceleratorr   Z&pytorch_lightning.utilities.exceptionsr   Z#pytorch_lightning.utilities.importsr   r:   r<   r=   r3   floatr   r   r   r   r   <module>   s   5