|
| 1 | +# Usage: python train_sweep.py --config configs/ray_tune_configs/ppo_config.yml --example-name ppo_sentiments |
| 2 | +import wandb |
| 3 | +import argparse |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import ray |
| 7 | +from ray.air import session |
| 8 | +from ray import tune |
| 9 | + |
| 10 | +import trlx |
| 11 | +from trlx.ray_tune import load_ray_yaml |
| 12 | +from trlx.ray_tune import get_param_space |
| 13 | +from trlx.ray_tune import get_tune_config |
| 14 | +from trlx.ray_tune import get_train_function |
| 15 | +from trlx.ray_tune.wandb import log_trials, create_report |
| 16 | + |
| 17 | +from ray.tune.logger import JsonLoggerCallback |
| 18 | +from ray.tune.logger import CSVLoggerCallback |
| 19 | + |
| 20 | + |
| 21 | +def tune_function(train_function, param_space: dict, tune_config: dict, resources: dict): |
| 22 | + tuner = tune.Tuner( |
| 23 | + tune.with_resources(train_function, resources=resources), |
| 24 | + param_space=param_space, |
| 25 | + tune_config=tune.TuneConfig(**tune_config), |
| 26 | + run_config = ray.air.RunConfig( |
| 27 | + local_dir="ray_results", |
| 28 | + callbacks=[CSVLoggerCallback()] |
| 29 | + ), |
| 30 | + ) |
| 31 | + |
| 32 | + results = tuner.fit() |
| 33 | + |
| 34 | + log_trials( |
| 35 | + tuner._local_tuner.get_experiment_checkpoint_dir(), |
| 36 | + param_space["train"]["project_name"] |
| 37 | + ) |
| 38 | + |
| 39 | + create_report( |
| 40 | + param_space, |
| 41 | + tune_config, |
| 42 | + Path(tuner._local_tuner.get_experiment_checkpoint_dir()).stem, |
| 43 | + results.get_best_result().config |
| 44 | + ) |
| 45 | + |
| 46 | + print("Best hyperparameters found were: ", results.get_best_result().config) |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + parser = argparse.ArgumentParser() |
| 51 | + parser.add_argument( |
| 52 | + "--example-name", type=str, default="ppo_sentiments", help="Name of the example" |
| 53 | + ) |
| 54 | + parser.add_argument( |
| 55 | + "--config", type=str, default=None, required=True, help="The config file defining the param_space." |
| 56 | + ) |
| 57 | + parser.add_argument( |
| 58 | + "--num-cpus", type=int, default=4, help="Number of CPUs to use per exp." |
| 59 | + ) |
| 60 | + parser.add_argument( |
| 61 | + "--num-gpus", type=int, default=1, help="Number of GPUs to use per exp." |
| 62 | + ) |
| 63 | + parser.add_argument( |
| 64 | + "--server-address", |
| 65 | + type=str, |
| 66 | + default=None, |
| 67 | + required=False, |
| 68 | + help="The address of server to connect to if using Ray Client.", |
| 69 | + ) |
| 70 | + |
| 71 | + args, _ = parser.parse_known_args() |
| 72 | + |
| 73 | + # Read config and parse it |
| 74 | + config = load_ray_yaml(args.config) |
| 75 | + tune_config = get_tune_config(config) |
| 76 | + param_space = get_param_space(config) |
| 77 | + |
| 78 | + # Initialize Ray. |
| 79 | + if args.server_address: |
| 80 | + ray.init(address=f"ray://{args.server_address}") |
| 81 | + else: |
| 82 | + ray.init() |
| 83 | + |
| 84 | + resources = { |
| 85 | + "cpu": args.num_cpus, |
| 86 | + "gpu": args.num_gpus, |
| 87 | + } |
| 88 | + |
| 89 | + # Register the training function that will be used for training the model. |
| 90 | + train_function = get_train_function(args.example_name) |
| 91 | + tune.register_trainable("train_function", train_function) |
| 92 | + |
| 93 | + tune_function(train_function, param_space, tune_config, resources) |
| 94 | + |
| 95 | + # Shut down Ray. |
| 96 | + ray.shutdown() |
0 commit comments