o
    ci"                  
   @   s  d dl mZ d dlZd dlZd dlZd dlmZ d dlZd dlmZ d dl	m
Z
mZmZmZ d dlmZ dZdZejdd	Zejd
dddd ejdedd ejdddd G dd dejZedkre Zejejejrqdndd e Zejej ede!ej"ddej#dejrdnddej$d dd!d"ej%d#d$eejrd%ndd&ee&d'd(e&d(d)d*d+Z'e'( Z)e*d,e)+ j, dS dS )-    )print_functionN)tune)ConvNetget_data_loaders	test_func
train_func)ASHAScheduleri      zPyTorch MNIST Example)descriptionz	--use-gpu
store_trueFzenables CUDA training)actiondefaulthelpz--ray-addressz!The Redis address of the cluster.)typer   z--smoke-testzFinish quickly for testing)r   r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )

TrainMNISTc                 C   sr   | do	tj }t|rdnd| _t \| _| _t 	| j| _
tj| j
 | dd| ddd| _d S )	Nuse_gpucudacpulrg{Gz?momentum?)r   r   )gettorchr   is_availabledevicer   train_loadertest_loaderr   tomodeloptimSGD
parameters	optimizer)selfconfiguse_cuda r&   ]/home/ubuntu/.local/lib/python3.10/site-packages/ray/tune/examples/mnist_pytorch_trainable.pysetup(   s   

zTrainMNIST.setupc                 C   s2   t | j| j| j| jd t| j| j| j}d|iS )N)r   mean_accuracy)r   r   r"   r   r   r   r   )r#   accr&   r&   r'   step2   s
   zTrainMNIST.stepc                 C   s$   t j|d}t| j | d S Nz	model.pth)ospathjoinr   saver   
state_dictr#   checkpoint_dircheckpoint_pathr&   r&   r'   save_checkpoint8      zTrainMNIST.save_checkpointc                 C   s$   t j|d}| jt| d S r,   )r-   r.   r/   r   load_state_dictr   loadr2   r&   r&   r'   load_checkpoint<   r6   zTrainMNIST.load_checkpointN)__name__
__module____qualname__r(   r+   r5   r9   r&   r&   r&   r'   r   '   s
    
r   __main__   )addressnum_cpus   )r   gpu)	resourcesgffffff?   )r)   training_iterationT)checkpoint_at_endcheckpoint_frequency)stopcheckpoint_configr)   max   )metricmode	schedulernum_samplesgMbP?g?r   )argsr   r   )
run_configtune_configparam_spacezBest config is:)-
__future__r   argparser-   r   torch.optimr   rayr   ray.tune.examples.mnist_pytorchr   r   r   r   ray.tune.schedulersr   
EPOCH_SIZE	TEST_SIZEArgumentParserparseradd_argumentstr	Trainabler   r:   
parse_argsrP   initray_address
smoke_testschedTunerwith_resourcesintr   	RunConfigCheckpointConfig
TuneConfiguniformtunerfitresultsprintget_best_resultr$   r&   r&   r&   r'   <module>   s`   	

