Skip to content

Chapter 11: Hyperparameter Optimization with Ray Tune & ASHA

  • Topic Focus • Ray Tune, Search Spaces, ASHA Early-Stopping Schedulers, and Bayesian Optimization
  • Interactive Challenges • 3 Hands-on Exercises
  • Launch Playground in Wasm →

1. Architectural Overview & Control Plane Mechanics

Ray Tune executes scalable, distributed hyperparameter search across hundreds of concurrent trials. It couples state-of-the-art search algorithms (Bayesian, Optuna, HyperOpt) with early-stopping trial schedulers like ASHA (Asynchronous Successive Halving Algorithm).

flowchart LR
    Tuner["Tuner Coordinator<br/>(TrialRunner + SearchAlg)"] -->|"1. Dispatch Trial Configs"| Rung0["Rung 0: All Trials<br/>(1 Epoch)"]
    Rung0 -->|"2. Bottom 50% Pruned"| Killed["Terminated Early ❌<br/>(Reclaim GPUs)"]
    Rung0 -->|"3. Top 50% Promoted"| Rung1["Rung 1: Promoted<br/>(4 Epochs)"]
    Rung1 -->|"4. Optimal Result"| Winner["Best Trial Winner 🎉<br/>(Full Epochs)"]

    style Tuner fill:#1e293b,stroke:#38bdf8,stroke-width:2px,color:#f8fafc
    style Rung0 fill:#0f172a,stroke:#818cf8,stroke-width:1px,color:#f8fafc
    style Killed fill:#1e1e2e,stroke:#ef4444,stroke-width:1px,stroke-dasharray: 5 5,color:#f8fafc
    style Rung1 fill:#1e1e38,stroke:#f59e0b,stroke-width:1px,color:#f8fafc
    style Winner fill:#0f172a,stroke:#34d399,stroke-width:2px,color:#f8fafc

Diagram Walkthrough & Core Concepts: - Search Algorithm Generation: Algorithms like Optuna, HyperOpt, or Bayesian search suggest candidate hyperparameter configurations to the TrialRunner. - ASHA Successive Halving: Trials are evaluated on low resource budgets (Rung 0). Poorly performing configurations are aggressively pruned early in training. - Dynamic Resource Reallocation: Terminating low-performing trials frees up cluster GPUs and CPUs immediately to explore new candidate configurations, drastically reducing total compute budget.


2. Annotated Python Code Anatomy & API Reference

import ray
from ray import tune
from ray.tune.schedulers import ASHAScheduler

# 1. Define objective function
def evaluation_objective(config: dict):
    lr = config["lr"]
    momentum = config["momentum"]

    for epoch in range(10):
        # Simulated validation loss curve
        val_loss = (1.0 - (lr * 10)) ** 2 + (0.5 - momentum) ** 2 + (1.0 / (epoch + 1))
        # Report metric to Tune scheduler
        tune.report({"val_loss": val_loss, "epoch": epoch})

# 2. Define search space and ASHA early-stopping scheduler
search_space = {
    "lr": tune.loguniform(1e-4, 1e-1),
    "momentum": tune.uniform(0.1, 0.9),
}

scheduler = ASHAScheduler(
    metric="val_loss",
    mode="min",
    max_t=10,
    grace_period=2,
    reduction_factor=3
)

# 3. Launch tuner
tuner = tune.Tuner(
    evaluation_objective,
    param_space=search_space,
    tune_config=tune.TuneConfig(num_samples=20, scheduler=scheduler)
)
results = tuner.fit()
best_result = results.get_best_result(metric="val_loss", mode="min")
print(f"Best config: {best_result.config} | Loss: {best_result.metrics['val_loss']}")

3. Production Best Practices & Hardening Guidelines

  1. Use ASHA for Iterative Deep Learning: ASHA outperforms random and grid search by 5–10x by aggressively terminating unpromising trials.
  2. Define Log-Uniform Spaces for Learning Rates: Use tune.loguniform(min, max) rather than linear uniform for parameters spanning multiple orders of magnitude.
  3. Limit Max Concurrent Trials: Set tune.TuneConfig(max_concurrent_trials=N) to prevent overloading available cluster GPU slots.
  4. Use Checkpoints for Early Stopping & Resuming: Save trial state with train.report(..., checkpoint=...) to allow paused trials to resume.
  5. Track Experiments with MLflow / Wandb: Attach WandbLoggerCallback or MLflowLoggerCallback to tune.Tuner(run_config=...).

4. Troubleshooting & Diagnostic Workflows

  1. Trials Terminating Immediately with Error:
  2. Symptom: All trials fail on step 0.
  3. Fix: Check trial error logs in results.errors; verify input data path is accessible to all worker nodes.
  4. Scheduler Metric Direction Inverted:
  5. Symptom: Best trial selected has highest loss instead of lowest.
  6. Fix: Verify mode="min" for loss minimization or mode="max" for accuracy metrics.
  7. Resource Starvation:
  8. Symptom: Only 1 trial runs at a time.
  9. Fix: Check trial resource requests in tune.with_resources(objective, {"cpu": 1, "gpu": 0.5}).

5. Hands-on Practice Exercises

Exercise ID Goal / Topic Playground Link
tune01 Tune Search Spaces & Distributed Trials Open Exercise tune01 →
tune02 ASHA / HyperBand Schedulers Open Exercise tune02 →
tune03 Population-Based Training (PBT) Open Exercise tune03 →