o
    $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_dirnum_workersr   zptl/val_losszptl/val_accuracy)lossaccr   validation_end)on)
max_epochsgpusprogress_bar_refresh_rate	callbacks)r   getr   r   mlflowpytorchautologplTrainerr   fit)configr   
num_epochsnum_gpusmodeldmmetricstrainer r$   Y/home/ubuntu/veenaModal/venv/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 )Nmnist_data_    )r   r   )r)   @      )r*   r+      g-C6?g?)layer_1layer_2lrr   r   r	   r   r   )r   r   r   r
   )cpugpu)	resourcesr   min)metricmodenum_samples
tune_mnist)name)tune_config
run_configparam_spacez!Best hyperparameters found were: )ospathjointempfile
gettempdirr   prepare_datar   set_tracking_uriset_experimentr   choice
loguniformget_tracking_uriwith_parametersr&   Tunerwith_resources
TuneConfig	RunConfigr   printget_best_resultr   )
r6   r   gpus_per_trialr	   r   r   r   	trainabletunerresultsr$   r$   r%   r7   &   sB   


r7   __main__z--smoke-test
store_truezFinish quickly for testing)actionhelpr
   mlruns)r6   r   rN   r	   )r6   r   rN   )Nr   r   )r   r   r   Nr'   )__doc__r<   r?   r   pytorch_lightningr   rayr   ray.air.integrations.mlflowr    ray.tune.examples.mnist_ptl_minir   r   &ray.tune.integration.pytorch_lightningr   r&   r7   __name__argparseArgumentParserparseradd_argumentparse_known_argsargs_
smoke_testr=   r>   r@   r$   r$   r$   r%   <module>   s@    

2
