o
    niM                     @   s   d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	m
Z
 er8d dlmZ d dlmZ d dlmZ d dlmZ d dlZe	de j dZeeZG d	d
 d
eZe
G dd deZe
G dd deZG dd deZG dd dZdS )    N)TYPE_CHECKINGAnyListOptionalProtocolTypeVarruntime_checkable)Deque)TelemetryRecord)	FilesDict)SettingsStatic	TimeStamp)boundc                   @   sD   e Zd ZU dZeed< ded< dddZdd	d
ZdefddZ	dS )Metricz%Base protocol for individual metrics.namez
Deque[Any]samplesreturnNc                 C      dS )zSample the metric.N selfr   r   i/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/sdk/internal/system/assets/interfaces.pysample$      zMetric.samplec                 C   r   )zClear the samples.Nr   r   r   r   r   clear(   r   zMetric.clearc                 C   r   )zAggregate the samples.Nr   r   r   r   r   	aggregate,   r   zMetric.aggregater   N)
__name__
__module____qualname____doc__str__annotations__r   r   dictr   r   r   r   r   r      s   
 

r   c                   @   s$   e Zd ZdZdddZdddZdS )	SetupTeardownz5Protocol for classes that require setup and teardown.r   Nc                 C   r   )z4Extra setup required for the metric beyond __init__.Nr   r   r   r   r   setup5   r   zSetupTeardown.setupc                 C   r   )z'Extra teardown required for the metric.Nr   r   r   r   r   teardown9   r   zSetupTeardown.teardownr   )r   r   r   r    r%   r&   r   r   r   r   r$   1   s    
r$   c                   @   sx   e Zd ZU dZeed< ee ed< ded< dededd	fd
dZ	e
defddZdddZdddZdefddZd	S )AssetzwBase protocol encapsulate everything relating to an "Asset".

    An asset can be CPU, GPU, TPU, Network, I/O etc.
    r   metricsMetricsMonitormetrics_monitorargskwargsr   Nc                 O      d S Nr   )r   r+   r,   r   r   r   __init__I       zAsset.__init__c                 C   r   )z#Check if the resource is available.Nr   )clsr   r   r   is_availableK   s   zAsset.is_availablec                 C   r   )zStart monitoring the resource.Nr   r   r   r   r   startP   r   zAsset.startc                 C   r   )zFinish monitoring the resource.Nr   r   r   r   r   finishT   r   zAsset.finishc                 C   r   )z*Get static information about the resource.Nr   r   r   r   r   probeX   r   zAsset.prober   )r   r   r   r    r!   r"   r   r   r   r/   classmethodboolr2   r3   r4   r#   r5   r   r   r   r   r'   >   s   
 

r'   c                   @   s:   e Zd ZdeddfddZ				ddd	ZdddZdS )	Interfacestatsr   Nc                 C   r-   r.   r   )r   r9   r   r   r   publish_stats^   r0   zInterface.publish_stats	telemetryr
   c                 C   r-   r.   r   )r   r;   r   r   r   _publish_telemetry`   r   zInterface._publish_telemetry
files_dictr   c                 C   r-   r.   r   )r   r=   r   r   r   publish_filesd   r0   zInterface.publish_files)r;   r
   r   N)r=   r   r   N)r   r   r   r#   r:   r<   r>   r   r   r   r   r8   ]   s    
r8   c                   @   sn   e Zd ZdZdedee dedddej	dd	fd
dZ
dddZdefddZdddZdddZdddZd	S )r)   zQTakes care of collecting, sampling, serializing, and publishing a set of metrics.
asset_namer(   	interfacesettingsr   shutdown_eventr   Nc                 C   s:   || _ || _|| _d | _|| _ttd|j| _d| _	d S )Ng?   )
r(   r?   
_interface_process_shutdown_eventfloatmaxx_stats_sampling_intervalsampling_intervalsamples_to_aggregate)r   r?   r(   r@   rA   rB   r   r   r   r/   j   s   
zMetricsMonitor.__init__c                 C   s   | j  set| jD ]O}| jD ];}z|  W q tjy1   t	d|j
 d | j   Y  n tyJ } ztd|  W Y d}~qd}~ww | j | j | j  rY nq
|   | j  rdS dS )zPoll the Asset metrics.zProcess z has exited.zFailed to sample metric: N)rF   is_setrangerK   r(   r   psutilNoSuchProcessloggerinfor   set	ExceptionerrorwaitrJ   publish)r   _metricer   r   r   monitor   s&   



zMetricsMonitor.monitorc                 C   s^   i }| j D ]'}z| }|| W q ty, } ztd|  W Y d}~qd}~ww |S )zReturn a dict of metrics.zFailed to serialize metric: N)r(   r   updaterS   rP   rT   )r   aggregated_metricsrX   serialized_metricrY   r   r   r   r      s   
zMetricsMonitor.aggregatec              
   C   sj   z|   }|r| j| | jD ]}|  qW dS  ty4 } ztd|  W Y d}~dS d}~ww )zPublish the Asset metrics.zFailed to publish metrics: N)r   rD   r:   r(   r   rS   rP   rT   )r   r\   rX   rY   r   r   r   rV      s   

zMetricsMonitor.publishc              
   C   s   | j d us
| j rd S | jd d  }z*| jD ]}t|tr#|  qtj	| j
d|d| _ | j   td| d W d S  ty_ } ztd| d|  d | _ W Y d }~d S d }~ww )N   T)targetdaemonr   zStarted z monitoringzFailed to start  monitoring: )rE   rF   rL   r?   r(   
isinstancer$   r%   	threadingThreadrZ   r3   rP   rQ   rS   warningr   thread_namerX   rY   r   r   r   r3      s(   


zMetricsMonitor.startc              
   C   s   | j d u rd S | jd d  }zIz| j   td| d | jD ]}t|tr-|  q"W n t	yL } zt
d| d|  W Y d }~nd }~ww W d | _ d S W d | _ d S d | _ w )Nr^   zJoined z monitorzFailed to finish ra   )rE   r?   joinrP   rQ   r(   rb   r$   r&   rS   re   rf   r   r   r   r4      s&   



"
zMetricsMonitor.finishr   )r   r   r   r    r!   r   r   r8   rc   Eventr/   rZ   r#   r   rV   r3   r4   r   r   r   r   r)   g   s(    



r)   )datetimeloggingrc   typingr   r   r   r   r   r   r   r	   wandb.proto.wandb_telemetry_pb2r
   wandb.sdk.interface.interfacer   "wandb.sdk.internal.settings_staticr   rN   r   	getLoggerr   rP   r   r$   r'   r8   r)   r   r   r   r   <module>   s&    $


