o
    `۷i                     @   s   d dl Zd dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ G dd deZd	d
 Zdd ZdddZdd Zdd ZdS )    N)minimize)GaussianProcessRegressor)HyperparameterKernel)pairwise_distances)euclidean_distancesc                   @   s   e Zd ZdZ						dddZedd	 Zed
d Zedd ZdddZ	dd Z
dd Zedd Zejdd Zedd ZdS )TV_SquaredExpzTime varying squared exponential kernel.
    For more info see the TV-GP-UCB paper:
    http://proceedings.mlr.press/v51/bogunovic16.pdf
          ?皙?h㈵>g     j@r         ?c                 C   s(   || _ || _|| _|| _|| _|| _d S N)variancelengthscaleepsilonvariance_boundslengthscale_boundsepsilon_bounds)selfr   r   r   r   r   r    r   S/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/tune/schedulers/pb2_utils.py__init__   s   	
zTV_SquaredExp.__init__c                 C      t dd| jS )Nr   numeric)r   r   r   r   r   r   hyperparameter_variance      z%TV_SquaredExp.hyperparameter_variancec                 C   r   )Nr   r   )r   r   r   r   r   r   hyperparameter_lengthscale#   r   z(TV_SquaredExp.hyperparameter_lengthscalec                 C   r   )Nr   r   )r   r   r   r   r   r   hyperparameter_epsilon'   r   z$TV_SquaredExp.hyperparameter_epsilonNFc                 C   s"  t |}|d u r|}t | jdd}|d d df dd}|d d df dd}t||d}d| d|  }|d d dd f }	|d d dd f }
| jt t t	|	|
 | j
  }|| }|r|}t t	|	|
}|| | j
 }|d }| | | d|  }|t |||gfS |S )Nr   r   r      	cityblock   )np
atleast_2dclipr   reshaper   r   expsquarer   r   dstack)r   XYeval_gradientr   T1T2dists
timekernel	X_spatial	Y_spatialrbfKK_gradient_variancedist2K_gradient_lengthscalenK_gradient_epsilonr   r   r   __call__+   s0   
zTV_SquaredExp.__call__c                 C   s   t j|jd | jt jdS )Nr   )dtype)r%   fullshaper   float64)r   r,   r   r   r   diagM   s   zTV_SquaredExp.diagc                 C   s   dS NFr   r   r   r   r   is_stationaryP   s   zTV_SquaredExp.is_stationaryc                 C   s   t | j| j| jgS r   )r%   logr   r   r   r   r   r   r   thetaS   s   zTV_SquaredExp.thetac                 C   s4   t |d | _t |d | _t |d | _d S )Nr   r"   r$   )r%   r)   r   r   r   )r   rE   r   r   r   rE   W   s   c                 C   s"   t t| jt| jt| jgS r   )r%   rD   listr   r   r   r   r   r   r   bounds]   s   zTV_SquaredExp.bounds)r	   r	   r
   r   r   r   rB   )__name__
__module____qualname____doc__r   propertyr   r   r    r<   rA   rC   rE   setterrG   r   r   r   r   r   	   s0    




"

r   c                 C   s2   | t j|dd t j|ddt j|dd d  S )zgNormalize data to be in range (0,1), with respect to (wrt) boundaries,
    which can be specified.
    r   axis:0yE>)r%   minmax)datawrtr   r   r   	normalizeh   s   rU   c                 C   s2   | t j| dd t j| ddd  } t | ddS )z5Standardize to be Gaussian N(0,1). Clip final values.r   rN   rP   r$   )r%   meanstdr'   )rS   r   r   r   standardizeq   s   $rY   c                 C   s   d}d}|t dt|| jjd   }|du rt|n|}t|ddt|ddfj	}z	| 
|d }	W n tyF   d}	Y nw z|j
|dd	\}
}|d d
 }W n tyc   d}Y nw |	||  S )aP  UCB acquisition function. Interesting points to note:
    1) We concat with the fixed points, because we are not optimizing wrt
       these. This is the Reward and Time, which we can't change. We want
       to find the best hyperparameters *given* the reward and time.
    2) We use m to get the mean and m1 to get the variance. If we already
       have trials running, then m1 contains this information. This reduces
       the variance at points currently running, even if we don't have
       their label.
       Ref: https://jmlr.org/papers/volume15/desautels14a/desautels14a.pdf

    g?g?r   Nr!   r"   iT)
return_stdr$   )rR   r%   rD   X_train_r?   sqrtconcatenater(   arrayTpredict
ValueError)mm1xfixedkappac1c2beta_txtestrW   _rX   varr   r   r   UCBw   s"    &rm   c                    s   dddd}d}d}j dddf }dd	 tj jd
 | D }	t|D ]1}
tjdd
j jd
 | }t fdd||	d|d}|j }||krW|}|j}q&t|dd
S )zOptimize acquisition function.   F)maxitermaxfundisp
   ir   Nc                 S   s   g | ]}d qS ))r   r"   r   ).0rk   r   r   r   
<listcomp>       z optimize_acq.<locals>.<listcomp>r"   c                    s   |   S r   r   )rd   re   funcrb   rc   r   r   <lambda>   ru   zoptimize_acq.<locals>.<lambda>zL-BFGS-B)rG   methodoptions)	r[   ranger?   r%   randomuniformr   rd   r'   )rw   rb   rc   re   num_foptsr_   
best_value
best_thetarG   iix0resvalr   rv   r   optimize_acq   s(   r   c                 C   sR  d}| j d |k r| j d S |d }g }|d | j d kr|d7 }tt| j}| | dddf }|| d }	|ddd|f }
ttj|
ddtj|
ddf	d|
j d }tj||fdd}t
||}t|		|	jd}tddd	d
}t|dd}||| ||j |d | j d kst|}|t|d  d }|S )z?Select the number of datapoints to keep, using cross validationrn   r   rr   NrN   r$   r"   r	   r
   )r   r   r   fmin_l_bfgs_b)kernel	optimizer)r?   r%   r^   rF   valuesr_   r]   rR   rQ   r(   rU   rY   sizer   r   fitappendlog_marginal_likelihood_value_argmaxint)XrawyrawrG   r~   min_lenlengthscores	base_valsX_leny_len	oldpointsold_limslimitsr,   yr   rb   idxr   r   r   select_length   s6   


r   r   )numpyr%   scipy.optimizer   sklearn.gaussian_processr    sklearn.gaussian_process.kernelsr   r   sklearn.metricsr   sklearn.metrics.pairwiser   r   rU   rY   rm   r   r   r   r   r   r   <module>   s    _	
 