o
    TÃiÂ
  ã                   @   s:   d dl Z d dlT d dlmZ d dlmZ G dd„ dƒZdS )é    N)Ú*)Úwrite_experiments)Úloggerc                   @   s6   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zddd„ZdS )Ú	BaseTunerc                 C   sF   || _ || _d| _d | _d | _|r|nt| _t dt	| j ƒ› ¡ d S )Nr   ztotal number of exps =  )
Úall_expsÚrmÚ	best_iterÚbest_expÚbest_metric_valÚAUTOTUNING_METRIC_DEFAULTÚmetricr   ÚinfoÚlen)ÚselfÚexpsÚresource_managerr   © r   úY/home/ubuntu/.local/lib/python3.10/site-packages/deepspeed/autotuning/tuner/base_tuner.pyÚ__init__   s   zBaseTuner.__init__c                 C   s   t | jƒdkr	dS dS )z7Whether there exists more configurations for evaluationr   TF)r   r   ©r   r   r   r   Úhas_next   s   zBaseTuner.has_nextc                 C   s   t ‚)z6Select the next batch of configurations for evaluation)ÚNotImplementedError)r   Úsample_sizer   r   r   Ú
next_batch   s   zBaseTuner.next_batchc                 C   s   dS )z\"Update the tuner with what configurations have been evaluated and their performance resultsNr   r   r   r   r   Úupdate#   s    zBaseTuner.updateé   éè  Nc           	      C   s  d}zx||k rx|   ¡ rx|  |¡}t|| jjƒ}| j |¡ | j ¡  | j | j¡\}}| j	d u s<| j
d u s<|rE|| j
krE|| _	|| _
|| _|t|ƒ7 }|  ¡  | j ¡  |rp|| j| krpt d|› d| j› d|› ¡ 	 |W S ||k rx|   ¡ s|W S    t dt ¡ d ¡ | Y S )Nr   z!Tuner early stopped at iteration z. Best iteration is z. Early stopping threshold is zTuner Error:)r   r   r   r   Úexps_dirÚschedule_experimentsÚrunÚparse_resultsr   r	   r
   r   r   r   Úclearr   r   ÚsysÚexc_info)	r   r   Ún_trialsÚearly_stoppingÚiÚsampled_expsÚ	exp_pathsÚexpÚ
metric_valr   r   r   Útune&   s6   



ÿäzBaseTuner.tune)r   r   N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r+   r   r   r   r   r      s    	r   )r"   Údeepspeed.autotuning.constantsÚdeepspeed.autotuning.utilsr   Údeepspeed.utilsr   r   r   r   r   r   Ú<module>   s
   