o
    1 iQ                     @   s   d 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mZ ddlmZ ddd	Z					
dddZedkrrddlZe Zejdddd e \ZZejriedddeje dd dS edddd dS dS )zlAn example showing how to use Pytorch Lightning training, Ray Tune
HPO, and MLflow autologging all together.    N)tune)setup_mlflow)LightningMNISTClassifierMNISTDataModule)TuneReportCallback
   c                 C   sz   t | | dd | dd d t| |}t|d| d d}ddd	}tj  tj||d
t	|ddgd}|
|| d S )Nexperiment_nametracking_uri)r   r	      
batch_size)data_dirZnum_workersr   zptl/val_losszptl/val_accuracy)lossaccr   Zvalidation_end)on)Z
max_epochsZgpusZprogress_bar_refresh_rate	callbacks)r   getr   r   mlflowZpytorchZautologplZTrainerr   fit)configr   
num_epochsnum_gpusmodeldmZmetricsZtrainer r   h/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/tune/examples/mlflow_ptl.pytrain_mnist_tune   s$   





r   ptl_autologging_examplec           
   
   C   s   t jt d}t|dd  t| t	| t
g dt
g dt
ddt
g d|t t jt d|d}t
jt|||d	}t
jt
j|d
|ddt
jdd| dt
jdd|d}| }	td|	 j d S )NZmnist_data_    )r   r   )r   @      )r   r       g-C6?g?)Zlayer_1Zlayer_2lrr   r   r	   r   r   )r   r   r   r
   )cpuZgpu)	resourcesr   min)Zmetricmodenum_samples
tune_mnist)name)Ztune_configZ
run_configZparam_spacez!Best hyperparameters found were: )ospathjointempfile
gettempdirr   Zprepare_datar   Zset_tracking_uriZset_experimentr   choiceZ
loguniformZget_tracking_uriZwith_parametersr   ZTunerZwith_resourcesZ
TuneConfigZ	RunConfigr   printZget_best_resultr   )
r'   r   gpus_per_trialr	   r   r   r   Z	trainableZtunerresultsr   r   r   r(   &   sB   


r(   __main__z--smoke-test
store_truezFinish quickly for testing)actionhelpr
   Zmlruns)r'   r   r1   r	   )r'   r   r1   )Nr   r   )r   r   r   Nr   )__doc__r*   r-   r   Zpytorch_lightningr   Zrayr   Zray.air.integrations.mlflowr   Z ray.tune.examples.mnist_ptl_minir   r   Z&ray.tune.integration.pytorch_lightningr   r   r(   __name__argparseArgumentParserparseradd_argumentparse_known_argsargs_Z
smoke_testr+   r,   r.   r   r   r   r   <module>   s@    

2
