
    
3jM+                     `    S SK r S SKrSSKJr  S SKrS SKrS\4S jr\" 5       r	 " S S5      r
g)    N   )loggerreturnc                     Sn  [        SS5       nUR                  5       R                  5       n [        R                  " SU  35        SU ;   sSSS5        $ ! , (       d  f       g= f! [
         a}  n[        R                  " 5       n[        R                  " SU 35        SUR                  5       ;   a  [        R                  " S	5         SnAg
[        R                  " S5         SnAgSnAff = f)z
Determines if the Python environment is running on a Jetson device by checking the device model
information or the platform release.
 z/proc/device-tree/modelrzDevice model: NVIDIANzPlatform release: tegraz=Detected 'tegra' in platform release. Assuming Jetson device.TzJETSON: Not detected.F)	openreadstripr   info	Exceptionplatformreleaselower)PROC_DEVICE_MODELfeplatform_releases       J/home/wildlama/comfy/ComfyUI/custom_nodes/ComfyUI-Crystools/general/gpu.py	is_jetsonr      s    
 +S1Q ! 0KK.):(;<=00 211  
 $++-()9(:;<&,,..KKWXKK/0
s;   A' <A	A' 
A$ A' $A' '
C.1AC)C))C.c                       \ rS rSrSrSrSrSrSrSr	Sr
SrSrSrSr/ r/ r/ r/ rS rS rS	 rS
 rS rS rS rS rS rS rS rSrg)CGPUInfo    zD
This class is responsible for getting information from GPU (ONLY).
Fcpur   Tc                    [         (       aK   SSKJn  U" 5       U l        U R                  R                  5         SU l        [
        R                  " S5        OB SS KnX0l        U R                  R                  5         SU l        [
        R                  " S5        U R                  =(       d    U R                  U l         [        R                   R#                  [        R                   R%                  5       5      U l        U R                  (       aQ  U R                  (       d@  U R)                  5       (       d+  [
        R*                  " S5        SU l        SU l        SU l        U R                  (       GaK  U R)                  5       S:  Ga  U R)                  5       U l        [
        R                  " S5        [/        U R,                  5       H  nU R1                  U5      nU R3                  XT5      n[
        R                  " U SU 35        U R4                  R7                  UUS.5        U R8                  R7                  S5        U R:                  R7                  S5        U R<                  R7                  S5        M     SU l        [
        R                  " U RA                  5       5        O-[
        R*                  " S5        O[
        R*                  " S5        U R&                  S:X  a  SOSU l!        [D        R>                  RG                  5       U l$        U R>                  (       a:  U RH                  (       a(  U R&                  S:X  a  [
        R*                  " S5        g g g g ! [         a-  n[
        R                  " S[        U5      -   5         S nAGNS nAf[         a-  n[
        R                  " S[        U5      -   5         S nAGN)S nAff = f! [         a-  n[
        R                  " S[        U5      -   5         S nAGNcS nAf[         a-  n[
        R                  " S	[        U5      -   5         S nAGNS nAff = f! [         a-  n[
        R                  " S
[        U5      -   5         S nAGNmS nAff = f)Nr   )jtopTz"jtop initialized on Jetson device.zjtop is not installed. zCould not initialize jtop. zpynvml (NVIDIA) initialized.zpynvml is not installed. z Could not init pynvml (NVIDIA). zCould not pick default device. z*No GPU detected, disabling GPU monitoring.FzGPU/s:z) )indexnamezNo GPU with CUDA detected.z&No GPU monitoring libraries available.r   cudaz*CUDA is available, but torch is using CPU.)%	IS_JETSONr   jtopInstancestart
jtopLoadedr   r   ImportErrorerrorstrr   pynvmlnvmlInitpynvmlLoadedanygpuLoadedcomfymodel_managementget_torch_device_nameget_torch_devicetorchDevicedeviceGetCountwarningcudaDevicesFoundrangedeviceGetHandleByIndexdeviceGetNamegpusappendgpusUtilizationgpusVRAMgpusTemperaturer!   systemGetDriverVersion
cudaDevicetorchis_availablecudaAvailable)selfr   r   r)   deviceIndexdeviceHandlegpuNames          r   __init__CGPUInfo.__init__3   s@   9	E%$(F!!!'')"&@A	J$$$&$(!:; !--@	E$55KKELbLbLsLsLuvD T__T=P=P=R=RNNGH %D %D#DO""$q((,(;(;(=%f&#()>)>#?K#'#>#>{#KL"00KGKK;-r' ;<II$$!, '&  ((//5MM((.((//5 $@" !	D779:;<NNCD#'#3#3u#<%&"ZZ44699++0@0@E0INNGH 1J+9  A6Q?@@ E:SVCDDE  C83q6ABB J?#a&HIIJ  	ELL:SVCDD	Esb   A	M AO
 >A P9 
O%"NO"OO

P6"O<<P6	"P11P69
Q0"Q++Q0c                 F    [         R                  " S5        U R                  $ )NzGetting GPUs info...)r   debugr8   rB   s    r   getInfoCGPUInfo.getInfo~   s    +,yy    c           	         SnSnSnSnSnSn/ nU R                   S:X  a  SnUR                  SSSSSS.5        GOSU R                   nU R                  (       Ga5  U R                  (       Ga#  U R                  (       Ga  [        U R                  5       H  nU R                  U5      n	SnSnSnSnSnU R                  (       a&  U R                  U   (       a   U R                  U	5      nU R                  (       aD  U R                   U   (       a0   U R#                  U	5      nUS   nUS	   nU(       a  US
:w  a  X4-  S-  nU R$                  (       a&  U R&                  U   (       a   U R)                  U	5      nUR                  UUUUUS.5        M     UUS.$ ! [         aI  n
[        R                  " S[        U
5      -   5        [        R                  " S5        SU l         S n
A
NS n
A
ff = f! [         a3  n
[        R                  " S[        U
5      -   5        SU l         S n
A
NS n
A
ff = f! [         a3  n
[        R                  " S[        U
5      -   5        SU l         S n
A
NS n
A
ff = f)Nr   r   )gpu_utilizationgpu_temperature
vram_total	vram_usedvram_used_percentCould not get GPU utilization. zMonitor of GPU is turning off.Fusedtotalr   d   zCould not get GPU memory info. z9Could not get GPU temperature. Turning off this feature. )device_typer8   )r>   r9   r,   r!   rA   r5   r4   r6   	switchGPUr:   deviceGetUtilizationRatesr   r   r'   r(   
switchVRAMr;   deviceGetMemoryInfoswitchTemperaturer<   deviceGetTemperature)rB   gpuUtilizationgpuTemperaturevramUsed	vramTotalvramPercentgpuTyper8   rC   rD   r   memorys               r   	getStatusCGPUInfo.getStatus   sF   	??e#GKK#%#% %'  ooG   TYYY43E3E3E#()>)>#?K#'#>#>{#KL%'N"$K!H "I%'N ~~$*>*>{*K3-1-K-KL-YN 4==+E
4%)%=%=l%KF'-f~H(.wI  )Y!^.6.BS.H --$2F2F{2S;-1-F-F|-TN
 KK+9+9&/%--8! O $@` #
 	
E  ) 3"LL)JSQRV)ST"LL)IJ-2DNN3  ) 4"LL)JSQRV)ST.3DOO4  ) ;"LL)dgjklgm)mn5:D22;sH   F/G(!H(
G%?G  G%(
H%2)H  H%(
I%2)I  I%c                 ~    U R                   (       a  U R                  R                  5       $ U R                  (       a  gg)N   r   )r+   r)   nvmlDeviceGetCountr%   rJ   s    r   r2   CGPUInfo.deviceGetCount   s+    ;;1133__rM   c                     U R                   (       a  U R                  R                  U5      $ U R                  (       a  U$ g)Nr   )r+   r)   nvmlDeviceGetHandleByIndexr%   )rB   r   s     r   r6   CGPUInfo.deviceGetHandleByIndex   s/    ;;99%@@__LrM   c                    U R                   (       a1  Sn U R                  R                  U5      n UR                  SSS9nU$ U R                  (       a;   U R                  R                  n[        [        UR                  5       5      5      nU$ g! [         a     U$ f = f! [
         a&  nSn[        R                  " SU 35         S nAU$ S nAff = f! [         a,  n[        R                  " S[        U5      -   5         S nAgS nAff = f)	NzUnknown GPUzutf-8ignore)errorszUnknown GPU (decoding error)zUnicodeDecodeError: zCould not get GPU name. r   )r+   r)   nvmlDeviceGetNamedecodeAttributeErrorUnicodeDecodeErrorr   r'   r%   r#   gpunextiterkeysr   r(   )rB   rD   rC   rE   r   gpu_infogpu_names          r   r7   CGPUInfo.deviceGetName   s    #G	9++77E%nnWXnFG N__%,,00X]]_ 56
 % &  N & 983A3788N	9  %7#a&@A$%sL   B! B 9C 
BB! BB! !
C+CC
D
"DD
c                     U R                   (       a  SU R                  R                  5        3$ U R                  (       a  gg)NzNVIDIA Driver: zNVIDIA Driver: unknownzDriver unknown)r+   r)   nvmlSystemGetDriverVersionr%   rJ   s    r   r=   CGPUInfo.systemGetDriverVersion   s3    $T[[%K%K%M$NOO__+#rM   c                 V   U R                   (       a%  U R                  R                  U5      R                  $ U R                  (       a)   U R
                  R                  R                  SS5      nU$ g! [         a,  n[        R                  " S[        U5      -   5         S nAgS nAff = f)NGPUrO   rU   r   )r+   r)   nvmlDeviceGetUtilizationRatesrw   r%   r#   statsgetr   r   r'   r(   )rB   rD   gpu_utilr   s       r   r[   "CGPUInfo.deviceGetUtilizationRates  s    ;;<<\JNNN__,,2266ubA
 	  >QGHs   	'A2 2
B(<"B##B(c                    U R                   (       a4  U R                  R                  U5      nUR                  UR                  S.$ U R
                  (       a'  U R                  R                  S   nUS   nUS   nXES.$ SSS.$ )N)rW   rV   RAMtotrV   rj   )r+   r)   nvmlDeviceGetMemoryInforW   rV   r%   r#   rf   )rB   rD   memmem_datarW   rV   s         r   r]   CGPUInfo.deviceGetMemoryInfo  sv    ++55lCC YY99__((//6HUOEF#D"11**rM   c                 j   U R                   (       a/  U R                  R                  XR                  R                  5      $ U R                  (       a)   U R
                  R                  R                  SS5      nU$ g! [         a,  n[        R                  " S[        U5      -   5         S nAgS nAff = f)NzTemp gpurO   zCould not get GPU temperature. r   )r+   r)   nvmlDeviceGetTemperatureNVML_TEMPERATURE_GPUr%   r#   r   r   r   r   r'   r(   )rB   rD   temperaturer   s       r   r_   CGPUInfo.deviceGetTemperature  s    ;;77kkFfFfgg__"//5599*bI""
 	  >QGHs   'A< <
B2"B--B2c                 x    U R                   (       a)  U R                  b  U R                  R                  5         g g g )N)r%   r#   closerJ   s    r   r   CGPUInfo.close*  s-    ??t00<##%  =?rM   )r,   r!   rA   r>   r4   r#   r%   r)   r+   rZ   r^   r\   r1   N)__name__
__module____qualname____firstlineno____doc__r!   r+   r%   rA   r1   r>   r4   rZ   r\   r^   r8   r:   r;   r<   rF   rK   rg   r2   r6   r7   r=   r[   r]   r_   r   __static_attributes__ rM   r   r   r       s     DLJMKJIJDOHOIIVI
V8$
+&rM   r   )r?   comfy.model_managementr-   corer   osr   boolr   r"   r   r   rM   r   <module>r      s5       	 4 . K	L& L&rM   