o
    `۷iL                     @   s   d dl mZmZmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ e \ZZZdd ZeG d	d
 d
e
ZdS )    )CallableListOptionalTuple)override)try_import_tf)Schedule)
TensorType)DeveloperAPIc                 C   s   | |||    S )N )leftrightalphar   r   b/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/rllib/utils/schedules/piecewise_schedule.py_linear_interpolation   s   r   c                
       s   e Zd ZdZdedfdeeeef  de	e
 deeeegef de	e f fddZeed	ed
efddZeed	ed
efddZ  ZS )PiecewiseSchedulez!Implements a Piecewise Scheduler.N	endpoints	frameworkinterpolationoutside_valuec                    sL   t  j|d dd |D }|t|ksJ || _|| _dd |D | _dS )an  Initializes a PiecewiseSchedule instance.

        Args:
            endpoints: A list of tuples
                `(t, value)` such that the output
                is an interpolation (given by the `interpolation` callable)
                between two values.
                E.g.
                t=400 and endpoints=[(0, 20.0),(500, 30.0)]
                output=20.0 + 0.8 * (30.0 - 20.0) = 28.0
                NOTE: All the values for time must be sorted in an increasing
                order.
            framework: The framework descriptor string, e.g. "tf",
                "torch", or None.
            interpolation: A function that takes the left-value,
                the right-value and an alpha interpolation parameter
                (0.0=only left value, 1.0=only right value), which is the
                fraction of distance from left endpoint to right endpoint.
            outside_value: If t in call to `value` is
                outside of all the intervals in `endpoints` this value is
                returned. If None then an AssertionError is raised when outside
                value is requested.
        )r   c                 S      g | ]}|d  qS r   r   .0er   r   r   
<listcomp>6       z.PiecewiseSchedule.__init__.<locals>.<listcomp>c                 S   s$   g | ]}t |d  t|d fqS )r      )intfloatr   r   r   r   r   :   s   $ N)super__init__sortedr   r   r   )selfr   r   r   r   idxes	__class__r   r   r!      s    zPiecewiseSchedule.__init__treturnc                 C   s   t | jd d | jdd  D ]'\\}}\}}||  kr!|k r6n qt|| ||  }| |||  S q| jd us>J | jS )Nr   )zipr   r   r   r   )r#   r'   l_tlr_trr   r   r   r   _value<   s   .zPiecewiseSchedule._valuec                    s   | j d us	J dttdd | jD dg tj g }t| jd d | jdd  D ]$\\}}\}}t| tjt|| tj }|| 	||| q-|| j  t|} fdd}fdd	}	t
||	tjd
tjdg}
||
d
  S )NzHtf-version of PiecewiseSchedule requires `outside_value` to be provided!c                 S   r   r   r   r   r   r   r   r   P   r   z2PiecewiseSchedule._tf_value_op.<locals>.<listcomp>r)   r   c                    sL   t |t j}t t t  | d  dt  |  |k| | d  k S )Nr   r)   )tfcastint64logical_not
logical_orequallogical_andix)r   r   r   _cond\   s   z-PiecewiseSchedule._tf_value_op.<locals>._condc                    s   | d  fS )Nr   r   r7   )r'   r   r   _bodye   s   z-PiecewiseSchedule._tf_value_op.<locals>._bodyr   )dtype)r   r0   r1   stackr   r2   r*   float32appendr   
while_loopconstant)r#   r'   results_listr+   r,   r-   r.   r   r:   r;   	idx_and_tr   )r   r'   r   _tf_value_opI   s   &.$
	zPiecewiseSchedule._tf_value_op)__name__
__module____qualname____doc__r   r   r   r   r   r   strr   r	   r!   r   r   r/   rD   __classcell__r   r   r%   r   r      s(    (r   N)typingr   r   r   r   ray.rllib.utils.annotationsr   ray.rllib.utils.frameworkr   "ray.rllib.utils.schedules.scheduler   ray.rllib.utils.typingr	   ray.util.annotationsr
   tf1r0   tfvr   r   r   r   r   r   <module>   s    