
    Yf                     D    d dl Z d dlZd dlZd dlmZ d dlZ G d d      Zy)    N)Tuplec                   ~    e Zd ZdZddZddZddZdefdZde	de
fd	Zdefd
ZddZde	de
fdZdee
e
f   fdZy)
ScaleneGPUz;A wrapper around the nvidia device driver library (pynvml).returnNc                 T   d| _         d| _        t        j                         | _        d| _        t        j                  t              5  t        j                          t        j                         | _         t        | j                         D cg c]  }t        j                  |       c}| _        | j                         | _        | j!                  | j                         | j#                  | j                         | j                   dkD  | _        d d d        y c c}w # 1 sw Y   y xY w)Nr   F)_ScaleneGPU__ngpus_ScaleneGPU__has_gpuosgetpid_ScaleneGPU__pid#_ScaleneGPU__has_per_pid_accounting
contextlibsuppress	ExceptionpynvmlnvmlInitnvmlDeviceGetCountrangenvmlDeviceGetHandleByIndex_ScaleneGPU__handle_set_accounting_modegpu_utilizationgpu_memory_usage)selfis     S/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/scalene/scalene_gpu.py__init__zScaleneGPU.__init__   s    YY[
(-%  + 	.OO "446DL t||, 11!4DM -1,E,E,GD)  ,!!$**- "\\A-DN	. 	.
	. 	.s    ADD)A'DDD'c                     d| _         y)zTurn off GPU accounting.FNr	   r   s    r   disablezScaleneGPU.disable!   s	        c                     | j                         rD| j                  s7t        dt        j                         t        dt        j                         y y y )NzoNOTE: The GPU is currently running in a mode that can reduce Scalene's accuracy when reporting GPU utilization.)filezdRun once as Administrator or root (i.e., prefixed with `sudo`) to enable per-process GPU accounting.)has_gpur   printsysstderrr    s    r   __del__zScaleneGPU.__del__%   sD    <<>$"?"? BZZ vZZ #@>r"   c                 n   | j                   }t        |      D ]  }| j                  |   }t        j                  |      t        j
                  k7  s9	 t        j                  |t        j
                         t        j                  |t        j
                          y# t        j                  $ r Y  yw xY w)zPReturns true iff the accounting mode was set already for all GPUs or is now set.FT)	r   r   r   r   nvmlDeviceGetAccountingModeNVML_FEATURE_ENABLEDnvmlDeviceSetPersistenceModenvmlDeviceSetAccountingMode	NVMLError)r   ngpusr   hs       r   r   zScaleneGPU._set_accounting_mode0   s    u 	!Aa A2215../
	!77666 66666	!( 	 '' ! !s   ABB43B4pidc                    | j                         sy| j                  }| j                  }d}t        |      D ]|  }| j                  |   }|rFt        j                  t              5  |t        j                  ||      j                  z  }ddd       Z	 |t        j                  |      j                  z  }~ ||z  dz  S # 1 sw Y   xY w# t        j                  j                  $ r Y w xY w)zReturn overall GPU utilization by pid if possible.
        Otherwise, returns aggregate utilization across all running processes.r   Ng      Y@)r%   r   r   r   r   r   r   r   r   nvmlDeviceGetAccountingStatsgpuUtilizationnvmlDeviceGetUtilizationRatesgpunvmlNVMLError_Unknown)r   r2   r0   accounting_onutilizationr   r1   s          r   r   zScaleneGPU.gpu_utilizationJ   s     ||~55u 	Aa A((3 %6#F#F3$$n%K% %
6#G#G#J#N#NNK	 e#u,,% % {{44 s   $$B>"C
>C	
C*)C*c                     | j                   S )z'True iff the system has a detected GPU.r   r    s    r   r%   zScaleneGPU.has_gpua   s    ~~r"   c                 T   | j                         syg | _        t        j                  t              5  t        j                          t        j                         | _        | j                  j                  d t        | j                        D               ddd       y# 1 sw Y   yxY w)z Reinitialize the nvidia wrapper.Nc              3   F   K   | ]  }t        j                  |        y w)N)r   r   ).0r   s     r   	<genexpr>z)ScaleneGPU.nvml_reinit.<locals>.<genexpr>m   s#      ! 11!4!s   !)r%   r   r   r   r   r   r   r   r   extendr   r    s    r   nvml_reinitzScaleneGPU.nvml_reinite   sz    ||~  + 	OO!446DLMM   !t||,! 	 	 	s   A#BB'c                 n   | j                         syd}t        | j                        D ]}  }| j                  |   }t	        j
                  t              5  t        j                  |      D ]1  }|j                  s|j                  |k(  s ||j                  dz  z  }3 	 ddd        |S # 1 sw Y   xY w)z2Returns GPU memory used by the process pid, in MB.r   i   N)r%   r   r   r   r   r   r   r   $nvmlDeviceGetComputeRunningProcessesusedGpuMemoryr2   )r   r2   total_used_GPU_memoryr   handleprocs         r   r   zScaleneGPU.gpu_memory_usager   s     ||~ !t||$ 
	NA]]1%F$$Y/ N"GG ND ))dhh#o .1C1Cg1MM-NN N
	N %$N Ns   %B+:B+
B++B4	c                     | j                         r:| j                  | j                        }| j                  | j                        }||fS y)z2Returns a tuple of (utilization %, memory in use).)        rJ   )r%   r   r   r   )r   
total_loadmem_useds      r   	get_statszScaleneGPU.get_stats   sA    <<>--djj9J,,TZZ8H))r"   )r   N)__name__
__module____qualname____doc__r   r!   r)   boolr   intfloatr   r%   rB   r   r   rM    r"   r   r   r   	   sl    E.*	d 4-3 -5 -. %C %E %&5. r"   r   )r   r
   r'   typingr   r   r   rU   r"   r   <module>rW      s      	 
  B Br"   