o
    i e                     @   s6   d Z ddlZddlZddlZddlZG dd dZdS )z0Steps per execution autotuning for Keras engine.    Nc                   @   sr   e Zd ZdZ			dddZdd Zed	d
 Zejdd
 Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )StepsPerExecutionTunera  Steps per execution tuner class.

    Args:
        optimizer: The optimizer used for training/evaluation/prediction. Used
            to measure iterations and global throughput
            (`optimizer.iterations`/second).
        spe_variable: A `tf.Variable` representing the `steps_per_execution`
            variable used during training/evaluation/prediction. Must be
            updatable with `spe_variable.assign`.
        interval: Optional int, the amount of seconds to wait between calls to
            measure throughput and tune `spe_variable`. Defaults to 5.
        change_spe_interval: Optional int, the number of throughput measurements
            before tuning. Defaults to 10.
        change_threshold: Optional float, the percent different in throughput to
            trigger a `steps_per_execution` change. For example, `0.1` triggers
            changes if throughput ()
       
   皙?c                 C   s2   || _ || _|| _|| _|| _t | _d | _d S N)		optimizer_steps_per_executionintervalchange_spe_intervalspe_change_threshold	threadingEventsteps_per_execution_stop_eventthread)selfr   spe_variabler	   r
   change_threshold r   Z/var/www/myenv/lib/python3.10/site-packages/keras/src/engine/steps_per_execution_tuning.py__init__+   s   

zStepsPerExecutionTuner.__init__c                 C   s@   | j r| j  r| j S |   tj| jdd| _ | j   | j S )zStarts steps per execution tuning thread.

        Returns a `threading.Thread` which will run every `self.interval`
            seconds to measure throughput and tune steps per execution.
        T)targetdaemon)r   is_alive_begin_tuningr   Thread"_steps_per_execution_interval_callstartr   r   r   r   r   ;   s   
zStepsPerExecutionTuner.startc                 C   s   | j S )z>Settable attribute representing`steps_per_execution` variable.)r   r   r   r   r   steps_per_executionJ   s   z*StepsPerExecutionTuner.steps_per_executionc                 C   s   | j | || _d S r   )r   assigninit_spe)r   valuer   r   r   r   O   s   
c                 C   s2   | j  s|   | j | j | j  rd S d S r   )r   is_set_measure_and_tunewaitr	   r   r   r   r   r   T   s   
z9StepsPerExecutionTuner._steps_per_execution_interval_callc                 C   sZ   t   | _| jj | _| j  | _| j| jd| _	g | _
d| _d| _d| _d| _d S )N)	iteration	time_secsr   T)time
start_timer   
iterationsnumpyinit_iterationsr   itemr    spe_last_loggedrgsps	avg_rgspsprev_avg_rgspsspe_tune_last_action_addspe_measurement_countr   r   r   r   r   Y   s   

z$StepsPerExecutionTuner._begin_tuningc                 C   s   | j  s| j   dS dS )z(Stops steps per execution tuning thread.N)r   r"   setr   r   r   r   stopg   s   
zStepsPerExecutionTuner.stopc                 C   s2   d}| j d dkrd}| j| j dko| j o| S )NFr   T)r.   r2   r
   )r   epoch_boundaryr   r   r   _should_tunel   s   z#StepsPerExecutionTuner._should_tunec           	      C   s   t | jt| j | _d| j | j }d| j | j }| jr#d}d}nd}d}| j}|jj	d }|
  }| j|kr@|| }n| j|k rO|| }| j | _n|}||krX|}n|dkr_| j}| jt| | j| _dS )a  Changes the steps per execution using the following algorithm.

        If there is more than a 10% increase in the throughput, then the last
        recorded action is repeated (i.e. if increasing the SPE caused an
        increase in throughput, it is increased again). If there is more than a
        10% decrease in the throughput, then the opposite of the last action is
        performed (i.e. if increasing the SPE decreased the throughput, then the
        SPE is decreased).
           g      ?g      ?r   N)sumr.   lenr/   r   r0   r1   r   dtypemaxr*   r,   r    r   npround)	r   fast_thresholdslow_thresholdrepeat_action_multopposite_action_multr   	spe_limitcurrent_spenew_sper   r   r   _tunew   s.   



zStepsPerExecutionTuner._tunec                 C   s   |  j d7  _ | jj }t }|| jd  || jd   }| j| t| j| j	kr3| j
d |dkr<|| _d S || jd< || jd< z|  rR|   W d S W d S  tyc   td Y d S w )Nr8   r%   r&   r   z,Steps per execution autotuner failed to run.)r2   r   r)   r*   r'   r-   r.   appendr:   r
   popr(   r7   rF   RuntimeErrorlogging	exception)r   cur_iterationcur_time_secsrecent_gspsr   r   r   r#      s,   


z(StepsPerExecutionTuner._measure_and_tuneN)r   r   r   )__name__
__module____qualname____doc__r   r   propertyr   setterr   r   r4   r7   rF   r#   r   r   r   r   r      s"    


*r   )rR   rJ   r   r'   r*   r=   r   r   r   r   r   <module>   s   