U
    d)                     @   sf   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                   @   s.   e Zd ZejZ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
   A/tmp/pip-unpacked-wheel-ua33x9lu/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__abcABCMeta__metaclass__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 k	r|| _t|tstt|}| j|d< t |d< | jD ]}|| qn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__5   s&    



zModelTrainerLog.__init__c              	   C   sB   t | jd}||d  |  W 5 Q R X td| d S )Na
Zexperiment_logger)openr   writecloselogging	getLoggerinfo)r   r   fr
   r
   r   r   L   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 krtt|| _| 	d	
| j | 	d	
d
d t|D  | jD ]J}z|| W q tk
r2 }	 ztd|	|	 W 5 d }	~	X Y qX qd 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>k   s     z&ModelTrainerLog.log.<locals>.<genexpr>z!Failed to call ExternalLogger: {})r   r   r"   r#   r!   sortedr   r    listr   joinr   r$   r   	Exceptionr2   warningr   )
r   r6   Zbatch_countZadditional_valuesZlogdictZdelta_tZdelta_countkr+   er
   r
   r   r   R   s6    



 zModelTrainerLog.log)N)r   r   r   r,   r   r   r
   r
   r
   r   r   3   s   
r   )r   r   r2   r(   r   collectionsr   Zfuture.utilsr   r   objectr   r   r
   r
   r
   r   <module>   s   