
    |h"                        d dl mZmZmZmZ d dlmZ d dlmZ  G d d      Z	e
dk(  r^dZdZdZ e	       Zej                          ej!                  eee	      Zer* ed
e        eD  cg c]  } d|  	 c} Z ede        yyyc c} w )    )AnyDictListOptional)LOGGER)check_requirementsc            
       j    e Zd ZdZd Zd Zd Zd Zdede	e
ef   fdZd	 Z	 dd
edededee   fdZy)GPUInfoat  
    Manages NVIDIA GPU information via pynvml with robust error handling.

    Provides methods to query detailed GPU statistics (utilization, memory, temp, power) and select the most idle
    GPUs based on configurable criteria. It safely handles the absence or initialization failure of the pynvml
    library by logging warnings and disabling related features, preventing application crashes.

    Includes fallback logic using `torch.cuda` for basic device counting if NVML is unavailable during GPU
    selection. Manages NVML initialization and shutdown internally.

    Attributes:
        pynvml (module | None): The `pynvml` module if successfully imported and initialized, otherwise `None`.
        nvml_available (bool): Indicates if `pynvml` is ready for use. True if import and `nvmlInit()` succeeded,
            False otherwise.
        gpu_stats (List[Dict[str, Any]]): A list of dictionaries, each holding stats for one GPU. Populated on
            initialization and by `refresh_stats()`. Keys include: 'index', 'name', 'utilization' (%),
            'memory_used' (MiB), 'memory_total' (MiB), 'memory_free' (MiB), 'temperature' (C), 'power_draw' (W),
            'power_limit' (W or 'N/A'). Empty if NVML is unavailable or queries fail.

    Methods:
        refresh_stats: Refresh the internal gpu_stats list by querying NVML.
        print_status: Print GPU status in a compact table format using current stats.
        select_idle_gpu: Select the most idle GPUs based on utilization and free memory.
        shutdown: Shut down NVML if it was initialized.

    Examples:
        Initialize GPUInfo and print status
        >>> gpu_info = GPUInfo()
        >>> gpu_info.print_status()

        Select idle GPUs with minimum memory requirements
        >>> selected = gpu_info.select_idle_gpu(count=2, min_memory_fraction=0.2)
        >>> print(f"Selected GPU indices: {selected}")
    c                 $   d| _         d| _        g | _        	 t        d       t	        d      | _         | j                   j                          d| _        | j                          y# t        $ r"}t        j                  d|        Y d}~yd}~ww xY w)z?Initialize GPUInfo, attempting to import and initialize pynvml.NFzpynvml>=12.0.0pynvmlTz1Failed to initialize pynvml, GPU stats disabled: )
r   nvml_available	gpu_statsr   
__import__nvmlInitrefresh_stats	Exceptionr   warning)selfes     [/var/www/html/test/engine/venv/lib/python3.12/site-packages/ultralytics/utils/autodevice.py__init__zGPUInfo.__init__-   s    %)$)/1	T/0$X.DKKK  ""&D  	TNNNqcRSS	Ts   AA$ $	B-B

Bc                 $    | j                          y)z>Ensure NVML is shut down when the object is garbage collected.N)shutdownr   s    r   __del__zGPUInfo.__del__<   s        c                     | j                   r0| j                  r#	 | j                  j                          d| _         yyy# t        $ r Y w xY w)z%Shut down NVML if it was initialized.FN)r   r   nvmlShutdownr   r   s    r   r   zGPUInfo.shutdown@   sL    4;;((* #(D $/  s   > 	A
	A
c                 X   g | _         | j                  r| j                  sy	 | j                  j                         }t	        |      D ],  }| j                   j                  | j                  |             . y# t        $ r)}t        j                  d|        g | _         Y d}~yd}~ww xY w)z5Refresh the internal gpu_stats list by querying NVML.NzError during device query: )
r   r   r   nvmlDeviceGetCountrangeappend_get_device_statsr   r   r   )r   device_countir   s       r   r   zGPUInfo.refresh_statsI   s    ""$++	 ;;99;L<( A%%d&<&<Q&?@A 	 NN8<=DNN	 s   AA7 7	B) B$$B)indexreturnc                 X   | j                   j                  |      }| j                   j                  |      }| j                   j                  |      }dddd}t	        | j                   dd      }|| j                   j                  |      |r|j                  nd|r|j                  dz	  nd|r|j                  dz	  nd|r|j                  dz	  nd || j                   j                  ||       || j                   j                  |d       || j                   j                  |d      d		S )
z"Get stats for a single GPU device.   )defaultdivisorc                t    	  | | }|dk7  rt        |t        t        f      r||z  S |S # t        $ r |cY S w xY w)Nr*   )
isinstanceintfloatr   )funcr+   r,   argsvals        r   safe_getz+GPUInfo._get_device_stats.<locals>.safe_get]   sH    Dk)0A*S3PU,:Wsg~`]`` s   $) ) 77NVML_TEMPERATURE_GPU   i  )r,   )	r&   nameutilizationmemory_usedmemory_totalmemory_freetemperature
power_drawpower_limit)r   nvmlDeviceGetHandleByIndexnvmlDeviceGetMemoryInfonvmlDeviceGetUtilizationRatesgetattrnvmlDeviceGetNamegpuusedtotalfreenvmlDeviceGetTemperaturenvmlDeviceGetPowerUsagenvmlDeviceGetEnforcedPowerLimit)r   r&   handlememoryutilr4   	temp_types          r   r#   zGPUInfo._get_device_statsW   s    77>44V<{{88@*,a 	 DKK)?D	 KK11&9'+488066;;",B28FLLB.b066;;",B#DKK$H$H&R[\"4;;#F#FX\]#DKK$O$OQWaef

 
	
r   c                    | j                          | j                  st        j                  d       y| j                  }t	        d |D              }ddddd| d	dd
dddddddddd}t        j
                  d| ddt        |      z          |D ]  }|d   dk\  r	|d   ddnd}|d   dk\  r|d   dd|d   dnd}|d   dk\  r|d    dnd}|d    dk\  r|d    d!d|d"   dnd}t        j
                  |j                  d#      d$d|j                  d%d&      d| d	d|dd|dd|dd|d        t        j
                  dt        |      z   d       y)'z?Print GPU status in a compact table format using current stats.zNo GPU stats available.Nc              3   R   K   | ]  }t        |j                  d d             ! yw)r7   N/AN)lenget).0rD   s     r   	<genexpr>z'GPUInfo.print_status.<locals>.<genexpr>z   s      Dss3776512Ds   %'Idxz<3 Name< Utilz>6z	Mem (MiB)z>15Tempz>5zPwr (W)z>10z
--- GPU Status ---

-r8   r   %z N/A r9   /r:   z<6z N/A / N/A r<   Cr=   z>3r>   r&   z<3dr7   rQ   )r   r   r   r   maxinforR   rS   )	r   statsname_lenhdrrD   umtps	            r   print_statuszGPUInfo.print_statusr   s   ~~NN45DeDDr
!F1XJ`,/q1[<MQvVXkYZ[dehZij,SEC#c(N3CDE 	vC/2=/AQ/F3}%b)+GAGJ=GY]^G^3}%b)3~+>r*BCdqA,/,>!,C3}%&a(AEHEVZ[E[3|$R(#m*<R)@AahAKK3777+C0#''&%2H8*TT1UUVWXY[V\\]^_`c]ddefghjekklmnorlstu	v 	sSX~&b)*r   countmin_memory_fractionmin_util_fractionc           	         |dk  s
J d|        |dk  s
J d|        t        j                  d| d|dz  dd|dz  dd	       |d
k  rg S | j                          | j                  st        j                  d       g S | j                  D cg c]G  }|j                  dd
      |j                  dd      z  |k\  rd|j                  dd      z
  |dz  k\  r|I }}|j                  d        |d| D cg c]  }|d   	 }}|rt        j                  d|        |S t        j                  d|dz  dd|dz  dd       |S c c}w c c}w )a  
        Select the most idle GPUs based on utilization and free memory.

        Args:
            count (int): The number of idle GPUs to select.
            min_memory_fraction (float): Minimum free memory required as a fraction of total memory.
            min_util_fraction (float): Minimum free utilization rate required from 0.0 - 1.0.

        Returns:
            (List[int]): Indices of the selected GPUs, sorted by idleness (lowest utilization first).

        Notes:
             Returns fewer than 'count' if not enough qualify or exist.
             Returns basic CUDA indices if NVML fails. Empty list if no GPUs found.
        g      ?z(min_memory_fraction must be <= 1.0, got z&min_util_fraction must be <= 1.0, got zSearching for z idle GPUs with free memory >= d   z.1fz% and free utilization >= z%...r   zNVML stats unavailable.r;   r:   r*   r8   c                 L    | j                  dd      | j                  dd       fS )Nr8   e   r;   r   )rS   )xs    r   <lambda>z)GPUInfo.select_idle_gpu.<locals>.<lambda>   s%    !%%s*CaeeM[\F]E])^ r   )keyNr&   zSelected idle CUDA devices z"No GPUs met criteria (Free Mem >= z% and Free Util >= z%).)r   rc   r   r   r   rS   sort)r   rl   rm   rn   rD   eligible_gpusselecteds          r   select_idle_gpuzGPUInfo.select_idle_gpu   s   $ #c)k-UViUj+kk) C'e+QRcQd)ee'UG#BCVY\C\]`Baa{  }N  QT  }T  UX  |Y  Y]  ^	
 A:I~~NN45I
 ~~
ww}a(377>1+EEI\\sww}c227H37NN 
 
 	^_ -:&5,ABSCLBBKK5hZ@A 	 NN45H35Ns4SSfgx{~g~  @C  gD  DG  H %
 Cs   AE6EN)r*   r   r   )__name__
__module____qualname____doc__r   r   r   r   r/   r   strr   r#   rk   r0   r   ry    r   r   r
   r
   	   sm    !FT( 
s 
tCH~ 
6+. Z[33383QV3	c3r   r
   __main__g?r*   )rl   rm   rn   z!
==> Using selected GPU indices: zcuda:z    Target devices: N)typingr   r   r   r   ultralytics.utilsr   ultralytics.utils.checksr   r
   rz   required_free_mem_fractionrequired_free_util_fractionnum_gpus_to_selectgpu_infork   ry   rx   printdevices)idxs   0r   <module>r      s    - , $ 7r rj z!$"%yH'' 65 ( H
 28*=>,45SU3%=5$WI./   6s   *B