o
    1 i!                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZ dd Zddd	Zed
kr_e  Zejddddd e \ZZejrWejdd eejd dS dS )    N)tune)
Checkpoint)PopulationBasedTrainingc              	   C   s  | d }|  dd}d}d}t }|rS| '}ttj|dd}t	|}W d   n1 s3w   Y  W d   n1 sBw   Y  |d }|d	 }	|	d }d
}
d}d}	 ||
k red| |
 }n
dd||
  |
  }t
dtd|}t||t
|| }||k r|d| t  7 }n||kr||| t  8 }||tj  7 }td|}||||||
d kd}|| dkrt 7}ttj|dd}||d}t|| W d   n1 sw   Y  tj|t|d W d   n1 sw   Y  nt| |d7 }qZ)a  Toy PBT problem for benchmarking adaptive learning rate.

    The goal is to optimize this trainable's accuracy. The accuracy increases
    fastest at the optimal lr, which is a function of the current accuracy.

    The optimal lr schedule for this problem is the triangle wave as follows.
    Note that many lr schedules for real models also follow this shape:

     best lr
      ^
      |    /      |   /        |  /          | /            ------------> accuracy

    In this problem, using PBT with a population of 2-4 is sufficient to
    roughly approximate this lr schedule. Higher population sizes will yield
    faster convergence. Training will not converge without PBT.
    lrcheckpoint_interval   g        zcheckpoint.jsonrNaccstepd         Tg{Gz?gMbP?g      ?r   )mean_accuracyZcur_lr
optimal_lrq_errdonew)r	   r
   )
checkpoint)getr   Zget_checkpointZas_directoryopenospathjoinjsonloadminmaxrandomnpnormaltempfileTemporaryDirectorydumpreportr   from_directory)configr   r   Zaccuracyr
   r   Zcheckpoint_dirfZcheckpoint_dictZ	last_stepZmidpointZq_toleranceZnoise_levelr   r   Zmetricstempdir r(   j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/tune/examples/pbt_function.pypbt_function   s`   





r*   Fc                 C   s   d}t d|tddddgdd}tjttjd	d
d| rdnddtjddtjddddtj|ddddddd|dd}|	 }t
d| j d S )N   training_iterationg-C6?g{Gz?r   r   )r   some_other_factor)Z	time_attrperturbation_intervalZhyperparam_mutationsZpbt_function_api_exampleFT
   i  )r   r,   )Z	fail_fastr   )Zcheckpoint_score_attributeZnum_to_keep)nameverbosestopZfailure_configZcheckpoint_configr      )Z	schedulerZmetricmodeZnum_samplesZreuse_actors)r   r-   r   )Z
run_configZtune_configZparam_spacez!Best hyperparameters found were: )r   r   uniformZTunerr*   Z	RunConfigZFailureConfigZCheckpointConfigZ
TuneConfigfitprintZget_best_resultr%   )
smoke_testr.   ZpbtZtunerresultsr(   r(   r)   run_tune_pbtj   sL   

-r:   __main__z--smoke-test
store_truezFinish quickly for testing)actiondefaulthelpr   )Znum_cpus)r8   )F)argparser   r   r   r    numpyr   Zrayr   Zray.tuner   Zray.tune.schedulersr   r*   r:   __name__ArgumentParserparseradd_argumentparse_known_argsargs_r8   initr(   r(   r(   r)   <module>   s2   
Y?