Training (tune.Trainable, tune.track)

Training can be done with either a Class API (tune.Trainable) or function-based API (track.log).

You can use the function-based API for fast prototyping. On the other hand, the tune.Trainable interface supports checkpoint/restore functionality and provides more control for advanced algorithms.

For the sake of example, let’s maximize this objective function:

def objective(x, a, b):
    return a * (x ** 0.5) + b

Function-based API

def trainable(config):
    # config (dict): A dict of hyperparameters.

    for x in range(20):
        score = objective(x, config["a"], config["b"])

        tune.track.log(score=score)  # This sends the score to Tune.

analysis = tune.run(
    trainable,
    config={
        "a": 2,
        "b": 4
    })

print("best config: ", analysis.get_best_config(metric="score", mode="max"))

Tip

Do not use tune.track.log within a Trainable class.

Tune will run this function on a separate thread in a Ray actor process. Note that this API is not checkpointable, since the thread will never return control back to its caller.

Note

If you want to pass in a Python lambda, you will need to first register the function: tune.register_trainable("lambda_id", lambda x: ...). You can then use lambda_id in place of my_trainable.

Trainable Class API

Caution

Do not use tune.track.log within a Trainable class.

The Trainable class API will require users to subclass ray.tune.Trainable. Here’s a naive example of this API:

from ray import tune

class Trainable(tune.Trainable):
    def _setup(self, config):
        # config (dict): A dict of hyperparameters
        self.x = 0
        self.a = config["a"]
        self.b = config["b"]

    def _train(self):  # This is called iteratively.
        score = objective(self.x, self.a, self.b)
        self.x += 1
        return {"score": score}

analysis = tune.run(
    Trainable,
    stop={"training_iteration": 20},
    config={
        "a": 2,
        "b": 4
    })

print('best config: ', analysis.get_best_config(metric="score", mode="max"))

As a subclass of tune.Trainable, Tune will create a Trainable object on a separate process (using the Ray Actor API).

  1. _setup function is invoked once training starts.

  2. _train is invoked multiple times. Each time, the Trainable object executes one logical iteration of training in the tuning process, which may include one or more iterations of actual training.

  3. _stop is invoked when training is finished.

Tip

As a rule of thumb, the execution time of _train should be large enough to avoid overheads (i.e. more than a few seconds), but short enough to report progress periodically (i.e. at most a few minutes).

In this example, we only implemented the _setup and _train methods for simplification. Next, we’ll implement _save and _restore for checkpoint and fault tolerance.

Save and Restore

Many Tune features rely on _save, and _restore, including the usage of certain Trial Schedulers, fault tolerance, and checkpointing.

class MyTrainableClass(Trainable):
    def _save(self, tmp_checkpoint_dir):
        checkpoint_path = os.path.join(tmp_checkpoint_dir, "model.pth")
        torch.save(self.model.state_dict(), checkpoint_path)
        return tmp_checkpoint_dir

    def _restore(self, tmp_checkpoint_dir):
        checkpoint_path = os.path.join(tmp_checkpoint_dir, "model.pth")
        self.model.load_state_dict(torch.load(checkpoint_path))

Checkpoints will be saved by training iteration to local_dir/exp_name/trial_name/checkpoint_<iter>. You can restore a single trial checkpoint by using tune.run(restore=<checkpoint_dir>).

Tune also generates temporary checkpoints for pausing and switching between trials. For this purpose, it is important not to depend on absolute paths in the implementation of save.

Use validate_save_restore to catch _save/_restore errors before execution.

from ray.tune.utils import validate_save_restore

# both of these should return
validate_save_restore(MyTrainableClass)
validate_save_restore(MyTrainableClass, use_object_store=True)

Advanced Resource Allocation

Trainables can themselves be distributed. If your trainable function / class creates further Ray actors or tasks that also consume CPU / GPU resources, you will want to set extra_cpu or extra_gpu inside tune.run to reserve extra resource slots. For example, if a trainable class requires 1 GPU itself, but also launches 4 actors, each using another GPU, then you should set "gpu": 1, "extra_gpu": 4.

 tune.run(
     my_trainable,
     name="my_trainable",
     resources_per_trial={
         "cpu": 1,
         "gpu": 1,
         "extra_gpu": 4
     }
 )

The Trainable also provides the default_resource_requests interface to automatically declare the resources_per_trial based on the given configuration.

Advanced: Reusing Actors

Your Trainable can often take a long time to start. To avoid this, you can do tune.run(reuse_actors=True) to reuse the same Trainable Python process and object for multiple hyperparameters.

This requires you to implement Trainable.reset_config, which provides a new set of hyperparameters. It is up to the user to correctly update the hyperparameters of your trainable.

class PytorchTrainble(tune.Trainable):
    """Train a Pytorch ConvNet."""

    def _setup(self, config):
        self.train_loader, self.test_loader = get_data_loaders()
        self.model = ConvNet()
        self.optimizer = optim.SGD(
            self.model.parameters(),
            lr=config.get("lr", 0.01),
            momentum=config.get("momentum", 0.9))

    def reset_config(self, new_config):
        for param_group in self.optimizer.param_groups:
            if "lr" in new_config:
                param_group["lr"] = new_config["lr"]
            if "momentum" in new_config:
                param_group["momentum"] = new_config["momentum"]

        self.model = ConvNet()
        self.config = new_config
        return True

tune.Trainable

class ray.tune.Trainable(config=None, logger_creator=None)[source]

Abstract class for trainable models, functions, etc.

A call to train() on a trainable will execute one logical iteration of training. As a rule of thumb, the execution time of one train call should be large enough to avoid overheads (i.e. more than a few seconds), but short enough to report progress periodically (i.e. at most a few minutes).

Calling save() should save the training state of a trainable to disk, and restore(path) should restore a trainable to the given state.

Generally you only need to implement _setup, _train, _save, and _restore when subclassing Trainable.

Other implementation methods that may be helpful to override are _log_result, reset_config, _stop, and _export_model.

When using Tune, Tune will convert this class into a Ray actor, which runs on a separate process. Tune will also change the current working directory of this process to self.logdir.

_export_model(export_formats, export_dir)[source]

Subclasses should override this to export model.

Parameters
  • export_formats (list) – List of formats that should be exported.

  • export_dir (str) – Directory to place exported models.

Returns

A dict that maps ExportFormats to successfully exported models.

_log_result(result)[source]

Subclasses can optionally override this to customize logging.

The logging here is done on the worker process rather than the driver. You may want to turn off driver logging via the loggers parameter in tune.run when overriding this function.

Parameters

result (dict) – Training result returned by _train().

_restore(checkpoint)[source]

Subclasses should override this to implement restore().

Warning

In this method, do not rely on absolute paths. The absolute path of the checkpoint_dir used in _save may be changed.

If _save returned a prefixed string, the prefix of the checkpoint string returned by _save may be changed. This is because trial pausing depends on temporary directories.

The directory structure under the checkpoint_dir provided to _save is preserved.

See the example below.

class Example(Trainable):
    def _save(self, checkpoint_path):
        print(checkpoint_path)
        return os.path.join(checkpoint_path, "my/check/point")

    def _restore(self, checkpoint):
        print(checkpoint)

>>> trainer = Example()
>>> obj = trainer.save_to_object()  # This is used when PAUSED.
<logdir>/tmpc8k_c_6hsave_to_object/checkpoint_0/my/check/point
>>> trainer.restore_from_object(obj)  # Note the different prefix.
<logdir>/tmpb87b5axfrestore_from_object/checkpoint_0/my/check/point
Parameters

checkpoint (str|dict) – If dict, the return value is as returned by _save. If a string, then it is a checkpoint path that may have a different prefix than that returned by _save. The directory structure underneath the checkpoint_dir _save is preserved.

_save(tmp_checkpoint_dir)[source]

Subclasses should override this to implement save().

Warning

Do not rely on absolute paths in the implementation of _save and _restore.

Use validate_save_restore to catch _save/_restore errors before execution.

>>> from ray.tune.utils import validate_save_restore
>>> validate_save_restore(MyTrainableClass)
>>> validate_save_restore(MyTrainableClass, use_object_store=True)
Parameters

tmp_checkpoint_dir (str) – The directory where the checkpoint file must be stored. In a Tune run, if the trial is paused, the provided path may be temporary and moved.

Returns

A dict or string. If string, the return value is expected to be prefixed by tmp_checkpoint_dir. If dict, the return value will be automatically serialized by Tune and passed to _restore().

Examples

>>> print(trainable1._save("/tmp/checkpoint_1"))
"/tmp/checkpoint_1/my_checkpoint_file"
>>> print(trainable2._save("/tmp/checkpoint_2"))
{"some": "data"}
>>> trainable._save("/tmp/bad_example")
"/tmp/NEW_CHECKPOINT_PATH/my_checkpoint_file" # This will error.
_setup(config)[source]

Subclasses should override this for custom initialization.

Parameters

config (dict) – Hyperparameters and other configs given. Copy of self.config.

_stop()[source]

Subclasses should override this for any cleanup on stop.

If any Ray actors are launched in the Trainable (i.e., with a RLlib trainer), be sure to kill the Ray actor process here.

You can kill a Ray actor by calling actor.__ray_terminate__.remote() on the actor.

_train()[source]

Subclasses should override this to implement train().

The return value will be automatically passed to the loggers. Users can also return tune.result.DONE or tune.result.SHOULD_CHECKPOINT as a key to manually trigger termination or checkpointing of this trial. Note that manual checkpointing only works when subclassing Trainables.

Returns

A dict that describes training progress.

classmethod default_resource_request(config)[source]

Provides a static resource requirement for the given configuration.

This can be overridden by sub-classes to set the correct trial resource allocation, so the user does not need to.

@classmethod
def default_resource_request(cls, config):
    return Resources(
        cpu=0,
        gpu=0,
        extra_cpu=config["workers"],
        extra_gpu=int(config["use_gpu"]) * config["workers"])
Returns

A Resources object consumed by Tune for queueing.

Return type

Resources

delete_checkpoint(checkpoint_path)[source]

Deletes local copy of checkpoint.

Parameters

checkpoint_path (str) – Path to checkpoint.

export_model(export_formats, export_dir=None)[source]

Exports model based on export_formats.

Subclasses should override _export_model() to actually export model to local directory.

Parameters
  • export_formats (Union[list,str]) – Format or list of (str) formats that should be exported.

  • export_dir (str) – Optional dir to place the exported model. Defaults to self.logdir.

Returns

A dict that maps ExportFormats to successfully exported models.

get_config()[source]

Returns configuration passed in by Tune.

reset_config(new_config)[source]

Resets configuration without restarting the trial.

This method is optional, but can be implemented to speed up algorithms such as PBT, and to allow performance optimizations such as running experiments with reuse_actors=True. Note that self.config need to be updated to reflect the latest parameter information in Ray logs.

Parameters

new_config (dir) – Updated hyperparameter configuration for the trainable.

Returns

True if reset was successful else False.

classmethod resource_help(config)[source]

Returns a help string for configuring this trainable’s resources.

Parameters

config (dict) – The Trainer’s config dict.

restore(checkpoint_path)[source]

Restores training state from a given model checkpoint.

These checkpoints are returned from calls to save().

Subclasses should override _restore() instead to restore state. This method restores additional metadata saved with the checkpoint.

restore_from_object(obj)[source]

Restores training state from a checkpoint object.

These checkpoints are returned from calls to save_to_object().

save(checkpoint_dir=None)[source]

Saves the current model state to a checkpoint.

Subclasses should override _save() instead to save state. This method dumps additional metadata alongside the saved path.

Parameters

checkpoint_dir (str) – Optional dir to place the checkpoint.

Returns

Checkpoint path or prefix that may be passed to restore().

Return type

str

save_to_object()[source]

Saves the current model state to a Python object.

It also saves to disk but does not return the checkpoint path.

Returns

Object holding checkpoint data.

stop()[source]

Releases all resources used by this trainable.

train()[source]

Runs one logical iteration of training.

Subclasses should override _train() instead to return results. This class automatically fills the following fields in the result:

done (bool): training is terminated. Filled only if not provided.

time_this_iter_s (float): Time in seconds this iteration took to run. This may be overriden in order to override the system-computed time difference.

time_total_s (float): Accumulated time in seconds for this entire experiment.

experiment_id (str): Unique string identifier for this experiment. This id is preserved across checkpoint / restore calls.

training_iteration (int): The index of this training iteration, e.g. call to train(). This is incremented after _train() is called.

pid (str): The pid of the training process.

date (str): A formatted date of when the result was processed.

timestamp (str): A UNIX timestamp of when the result was processed.

hostname (str): Hostname of the machine hosting the training process.

node_ip (str): Node ip of the machine hosting the training process.

Returns

A dict that describes training progress.

property iteration

Current training iteration.

This value is automatically incremented every time train() is called and is automatically inserted into the training result dict.

property logdir

Directory of the results and checkpoints for this Trainable.

Tune will automatically sync this folder with the driver if execution is distributed.

Note that the current working directory will also be changed to this.

property training_iteration

Current training iteration (same as self.iteration).

This value is automatically incremented every time train() is called and is automatically inserted into the training result dict.

property trial_id

Trial ID for the corresponding trial of this Trainable.

This is not set if not using Tune.

trial_id = self.trial_id
property trial_name

Trial name for the corresponding trial of this Trainable.

This is not set if not using Tune.

name = self.trial_name

tune.DurableTrainable

class ray.tune.DurableTrainable(remote_checkpoint_dir, *args, **kwargs)[source]

Abstract class for a remote-storage backed fault-tolerant Trainable.

Supports checkpointing to and restoring from remote storage. To use this class, implement the same private methods as ray.tune.Trainable (_save, _train, _restore, reset_config, _setup, _stop).

Warning

This class is currently experimental and may be subject to change.

Run this with Tune as follows. Setting sync_to_driver=False disables syncing to the driver to avoid keeping redundant checkpoints around, as well as preventing the driver from syncing up the same checkpoint.

See tune/trainable.py.

remote_checkpoint_dir

Upload directory (S3 or GS path).

Type

str

storage_client

Tune-internal interface for interacting with external storage.

>>> tune.run(MyDurableTrainable, sync_to_driver=False)

tune.track

ray.tune.track.shutdown()[source]

Cleans up the trial and removes it from the global context.

ray.tune.track.log(**kwargs)[source]

Logs all keyword arguments.

import time
from ray import tune
from ray.tune import track

def run_me(config):
    for iter in range(100):
        time.sleep(1)
        track.log(hello="world", ray="tune")

analysis = tune.run(run_me)
Parameters

**kwargs – Any key value pair to be logged by Tune. Any of these metrics can be used for early stopping or optimization.

ray.tune.track.trial_dir()[source]

Returns the directory where trial results are saved.

This includes json data containing the session’s parameters and metrics.

ray.tune.track.trial_name()[source]

Trial name for the corresponding trial of this Trainable.

This is not set if not using Tune.

ray.tune.track.trial_id()[source]

Trial id for the corresponding trial of this Trainable.

This is not set if not using Tune.

KerasCallback

StatusReporter

class ray.tune.function_runner.StatusReporter(result_queue, continue_semaphore, trial_name=None, trial_id=None, logdir=None)[source]

Object passed into your function that you can report status through.

Example

>>> def trainable_function(config, reporter):
>>>     assert isinstance(reporter, StatusReporter)
>>>     reporter(timesteps_this_iter=1)
__call__(**kwargs)[source]

Report updated training status.

Pass in done=True when the training job is completed.

Parameters

kwargs – Latest training result status.

Example

>>> reporter(mean_accuracy=1, training_iteration=4)
>>> reporter(mean_accuracy=1, training_iteration=4, done=True)
Raises

StopIteration – A StopIteration exception is raised if the trial has been signaled to stop.