a
    Þñh4  ã                   @   sn   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
 G dd„ deƒZG dd„ dƒZdS )é    N)ÚOrderedDict)ÚviewkeysÚ
viewvaluesc                   @   s4   e Zd Ze ej¡ ejdd„ ƒZejdd„ ƒZ	dS )ÚExternalLoggerc                 C   s   t dƒ‚dS )zp
            Set runtime arguments for the logger.
            runtime_args: dict of runtime arguments.
        z<Must define set_runtime_args function to use this base classN©ÚNotImplementedError)ÚselfÚruntime_args© r
   ú]/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/caffe2/python/experiment_util.pyÚset_runtime_args   s    ÿzExternalLogger.set_runtime_argsc                 C   s   t dƒ‚dS )zj
            log a dict of key/values to an external destination
            log_dict: input dict
        z/Must define log function to use this base classNr   )r   Zlog_dictr
   r
   r   Úlog)   s    ÿzExternalLogger.logN)
Ú__name__Ú
__module__Ú__qualname__ÚsixÚadd_metaclassÚabcÚABCMetaÚabstractmethodr   r   r
   r
   r
   r   r      s
   
	r   c                   @   s&   e Zd Zddd„Zdd„ Zdd„ ZdS )	ÚModelTrainerLogNc                 C   sÄ   t j  t ¡ ¡}d || d¡¡| _d | j¡| _|  dt|ƒ ¡ d | _	t ¡ | _
| j
| _d| _d | _|d urº|| _t|tƒsŒtt|ƒƒ}| j|d< t ¡ |d< | jD ]}| |¡ q¨ng | _d S )Nz{}_{}z%Y%m%d_%H%M%Sz{}.logz# %sr   Úexperiment_idÚhostname)ÚdatetimeÚfromtimestampÚtimeÚformatÚstrftimer   ÚfilenameÚlogstrÚstrÚheadersÚ
start_timeÚ	last_timeÚlast_input_countÚexternal_loggersÚ
isinstanceÚdictÚvarsÚsocketÚgethostnamer   )r   Zexpnamer	   r%   ÚnowÚloggerr
   r
   r   Ú__init__6   s&    ÿ



zModelTrainerLog.__init__c                 C   sV   t | jdƒ�&}| |d ¡ | ¡  W d   ƒ n1 s80    Y  t d¡ |¡ d S )NÚaÚ
Zexperiment_logger)Úopenr   ÚwriteÚcloseÚloggingÚ	getLoggerÚinfo)r   r    Úfr
   r
   r   r   M   s    &zModelTrainerLog.logstrc           
      C   s<  t ƒ }t ¡ | j }|| j }t ¡ | _|| _||d< t ¡ | j |d< ||d< ||d< ||d< |dkrv|| |d< nd|d< tt|ƒƒD ]}|| ||< qŠ| jd u rÆtt|ƒƒ| _|  	d	 
| j¡¡ |  	d	 
d
d„ t|ƒD ƒ¡¡ | jD ]L}z| |¡ W qê t�y4 }	 zt d |	¡|	¡ W Y d }	~	qêd }	~	0 0 qêd S )NZ
time_spentZcumulative_time_spentÚinput_countZcumulative_input_countZcumulative_batch_countr   Zinputs_per_secg        ú,c                 s   s   | ]}t |ƒV  qd S )N)r    )Ú.0Úvr
   r
   r   Ú	<genexpr>l   ó    z&ModelTrainerLog.log.<locals>.<genexpr>z!Failed to call ExternalLogger: {})r   r   r#   r$   r"   Úsortedr   r!   Úlistr   Újoinr   r%   r   Ú	Exceptionr3   Úwarnr   )
r   r7   Zbatch_countZadditional_valuesZlogdictZdelta_tZdelta_countÚkr,   Úer
   r
   r   r   S   s4    




ÿzModelTrainerLog.log)N)r   r   r   r-   r   r   r
   r
   r
   r   r   4   s   
r   )r   r   r3   r)   r   r   Úcollectionsr   Zfuture.utilsr   r   Úobjectr   r   r
   r
   r
   r   Ú<module>   s   