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 edZerd d	lmZ e Znd
Zerd dlm  mZ G dd deZeeeeee f  ee dddZdS )    )AnyDictListOptionalUnionN)package_available)_DEVICE)Accelerator)MisconfigurationException)rank_zero_debugZhabana_frameworks)is_habana_availableFc                   @   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 dddZeeeej dddZee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 )HPUAcceleratorzAccelerator for HPU devices.N)devicereturnc                 C   s   |j dkrtd| ddS )zs
        Raises:
            MisconfigurationException:
                If the selected device is not HPU.
        hpuzDevice should be HPU, got z	 instead.N)typer
   selfr    r   k/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/accelerators/hpu.pysetup_device)   s    
zHPUAccelerator.setup_devicec              	   C   s6   zt j|W S  ttfy0   td i  Y S 0 dS )an  Returns a map of the following metrics with their values:

        - Limit: amount of total memory on HPU device.
        - InUse: amount of allocated memory at any instance.
        - MaxInUse: amount of total active memory allocated.
        - NumAllocs: number of allocations.
        - NumFrees: number of freed chunks.
        - ActiveAllocs: number of active allocations.
        - MaxAllocSize: maximum allocated size.
        - TotalSystemAllocs: total number of system allocations.
        - TotalSystemFrees: total number of system frees.
        - TotalActiveAllocs: total number of active allocations.
        zHPU `get_device_stats` failedN)	torch_hpur   Zmemory_statsAttributeError	NameErrorr   r   r   r   r   get_device_stats2   s
    zHPUAccelerator.get_device_stats)r   c                 C   s   d S )Nr   )r   r   r   r   teardownF   s    zHPUAccelerator.teardowndevicesr   c                 C   s   t | S )z!Accelerator device parsing logic.)_parse_hpusr   r   r   r   parse_devicesI   s    zHPUAccelerator.parse_devicesc                 C   s   t dg|  S )z*Gets parallel devices for the Accelerator.r   )torchr   r   r   r   r   get_parallel_devicesN   s    z#HPUAccelerator.get_parallel_devicesc                	   C   s0   z
t  W S  ttfy*   td Y dS 0 dS )zBReturns the number of HPU devices when the devices is set to auto.z=HPU `auto_device_count` failed, returning default count of 8.   N)r   Zdevice_countr   r   r   r   r   r   r   auto_device_countS   s
    
z HPUAccelerator.auto_device_countc                	   C   s(   z
t  W S  ttfy"   Y dS 0 dS )z8Returns a bool indicating if HPU is currently available.FN)r   is_availabler   r   r   r   r   r   r%   \   s    
zHPUAccelerator.is_availablec                	   C   s(   z
t  W S  ttfy"   Y dS 0 dS )z#Returns the name of the HPU device. N)r   get_device_namer   r   r   r   r   r   r'   d   s    
zHPUAccelerator.get_device_name)accelerator_registryr   c                 C   s   |j d| | jjd d S )Nr   )description)register	__class____name__)clsr(   r   r   r   register_acceleratorsl   s
    z$HPUAccelerator.register_accelerators)r,   
__module____qualname____doc__r!   r   r   r   r   strr   r   r   staticmethodr   intr   r   r    r"   r$   boolr%   r'   classmethodr.   r   r   r   r   r   &   s    	$r   r   c                 C   s4   | durt | ttfstdt | tr0t| S | S )a  
    Parses the hpus given in the format as accepted by the
    :class:`~pytorch_lightning.trainer.Trainer` for the `devices` flag.

    Args:
        devices: An integer that indicates the number of Gaudi devices to be used

    Returns:
        Either an integer or ``None`` if no devices were requested

    Raises:
        MisconfigurationException:
            If devices aren't of type `int` or `str`
    Nz;`devices` for `HPUAccelerator` must be int, string or None.)
isinstancer4   r2   r
   r   r   r   r   r   u   s    r   )typingr   r   r   r   r   r!   Z lightning_utilities.core.importsr   Z lightning_fabric.utilities.typesr   Z*pytorch_lightning.accelerators.acceleratorr	   Z&pytorch_lightning.utilities.exceptionsr
   Z%pytorch_lightning.utilities.rank_zeror   Z_HABANA_FRAMEWORK_AVAILABLEZ,habana_frameworks.torch.utils.library_loaderr   Z_HPU_AVAILABLEZhabana_frameworks.torch.hpur   r   r   r4   r2   r   r   r   r   r   <module>   s   O