o
    Ti                     @   sl   d dl Z ddlmZmZ ddlmZ ddlmZ ddlT ddlT d dl	Z	ddlm
Z
 dZG d	d
 d
eZdS )    N   )
AUTOTUNINGAUTOTUNING_METRIC_PATH   )	BaseTuner)XGBoostCostModel)*)AUTOTUNING_METRIC_LATENCYc                       sF   e Zd ZdZdef fddZdd Zdd Zd	d
 Zdd Z	  Z
S )ModelBasedTunerz,Exploring the search space with a cost modelexpsc                    s   t  ||| || _d| _dd |D | _t| j| _t| j| _t	
d| j d| j  tg | _g | _d| _tt| j}t|D ](}tj| j}d}|| jv r`tj| j}|| jv sT| j| | j| qDtd| _g | _g | _d| _d| _d S )Nr   c                 S   s   g | ]}|d  qS )	ds_config ).0er   r   `/home/ubuntu/.local/lib/python3.10/site-packages/deepspeed/autotuning/tuner/model_based_tuner.py
<listcomp>   s    z,ModelBasedTuner.__init__.<locals>.<listcomp>zCreate config dim: z, all configs: rankg?)super__init__tuning_space	best_iterall_configslennum_all_configsdict_to_dimsdimsloggerinfosetvisitedtrialstrial_ptminINIT_NUMrangenprandomrandintappendaddr   
cost_modelevaluated_configsevaluated_perftrain_ctrandom_exploration_ratio)selfr   resource_managermetricr   init_num_exp_feature	__class__r   r   r      s0   




zModelBasedTuner.__init__c                 C   s   g }| j D ]!}t|}g }| D ]\}}t|tjr || q|| qtj|tj	d}| j
|}t|}t|}	| jtkrG|	}
|
S |	ddd d| }
|
S )zUse the cost model to predict the estimated performance of configurations and find the top ones for the next round of evaluation)dtypeN)r   flattenitems
isinstancenumbersNumberr(   r%   arrayfloat32r*   predictr   argsortr1   r	   )r/   configscflattened_ds_configfeature_valkv	estimatesntop_idxtop_idx_retr   r   r   find_estimated_top_configs:   s"   


z*ModelBasedTuner.find_estimated_top_configsc                 C   s   g }d}||k rwt | j| jkr	 |S | jt | jk r?td| j  | j| j }|| jvr0n|  jd7  _| jt | jk stj	 }|| j
k ratj| j}|| jv ratj| j}|| jv sU|| j|  | j| |d7 }||k s|S )Nr   ztrials: r   )r   r   r   r!   r    r   debugr%   r&   randr.   choicer'   r(   all_expsr)   )r/   sample_sizesampled_batchcounterindexrN   featurer   r   r   
next_batchS   s0   

	


zModelBasedTuner.next_batchc                 C   s   t | j| jk S )N)r   r   r   )r/   r   r   r   has_nextt   s   zModelBasedTuner.has_nextc                 C   s  | j j D ]\}\}}g }|rNtd| d|d  d| d|d   |d }t|}| D ]\}}t|tjr@|	| q1| j
	| | j	d q|d t t }	t|	d	E}
t|
}|| j }td
| d|  |d }t|}| D ]\}}t|tjr|	| q}| j
	| | j	| W d    n1 sw   Y  qtdt| j
 d| j  | j| j
| j |  }|| _d| _|  jd7  _d S )NzSkipping exp_id = z, exp_name = namez7, the experiment did not run successfully with error = zK, thus a metrics.txt does not exist for it. Please check the stderr.log in 
result_dirr   g        rzparsing the results for u   ， Result is z**Evaluated configs: z, evaluated perf: r   r   )rmfinished_experimentsr:   r   r   r9   r;   r<   r=   r(   r+   r,   r   r   openhjsonloadr1   rM   r   r*   fitrL   r    r!   r-   )r/   exp_idexperrrE   r   rD   rF   rG   pfresults	curr_iterestimated_top_configsr   r   r   updatew   sH   "



zModelBasedTuner.update)__name__
__module____qualname____doc__listr   rL   rV   rW   ri   __classcell__r   r   r5   r   r
      s    $!r
   )r^   	constantsr   r   
base_tunerr   r*   r   utilsr<   r	   r#   r
   r   r   r   r   <module>   s   