Skip to main content

Hyperparameter Sweeps

Automatically search for the best hyperparameters.

Quick Start

Python API

Parameters

Search Spaces

Default Search Space

By default, sweeps search over:
  • Learning rate: 1e-5 to 1e-3 (log uniform)
  • Batch size: 2, 4, 8, 16 (categorical)
  • Warmup ratio: 0.0 to 0.2 (uniform)
LoRA rank is NOT included in the default sweep. Add it manually via sweep_params if needed.

Custom Search Space

The sweep_params parameter expects a JSON string. Both list and dict formats are supported:
Supported dict types:

Sweep Backends

Optuna

Efficient Bayesian optimization:
Exhaustive search over all combinations:
Random sampling from search space:

Metrics

Standard Metrics

Enhanced Evaluation Metrics

Enable use_enhanced_eval to access additional metrics:

Enhanced Evaluation Parameters

Standard Benchmarks

Use eval_benchmark to run standard LLM benchmarks:

Custom Metrics Example

Example: Find Best LR

Viewing Results

Optuna Dashboard

W&B Native Sweep Dashboard

By default, sweeps run locally and only log individual runs to W&B. Enable native W&B sweep integration to get aggregated views, parallel coordinates plots, and parameter importance analysis in a dedicated sweep dashboard.
Local vs W&B Sweeps: Without wandb_sweep=True, each trial logs as a separate W&B run. With wandb_sweep=True, all trials are grouped under a single sweep dashboard with unified visualizations.

Enabling W&B Sweeps

Or in Python:

W&B Sweep Parameters

Continuing an Existing Sweep

To add more trials to an existing sweep instead of creating a new one, pass the sweep ID:
If you don’t pass wandb_sweep_id, a new sweep is created every time. The sweep ID is printed in the logs when the sweep starts (look for “Created W&B sweep: ”).

Accessing the Sweep Dashboard

  1. Go to wandb.ai and open your project
  2. Click the Sweep icon (broom) in the left panel
  3. Select your sweep from the list

Built-in Visualizations

W&B automatically generates three visualizations: Each panel has an Edit button to customize axes and behavior.
The parallel coordinates plot is especially useful for identifying which hyperparameter combinations lead to the best results. You can drag on any axis to filter runs.

Using with External W&B Sweep Agents

If you’re running AITraining from an external W&B sweep agent (not AITraining’s built-in sweep), use --wandb-run-id to resume the agent’s run instead of creating a duplicate:
When --wandb-run-id is set, AITraining automatically sets WANDB_RESUME=allow so the trainer resumes the specified run instead of creating a new one.

Important Notes

  • Requires W&B login: Run wandb login before using W&B sweeps
  • Sweep ID is logged: Look for “Created W&B sweep: ” in the logs
  • Trials are grouped: Each trial appears as a run with group={sweep_id} for aggregation
  • Optuna still manages search: W&B is for visualization only; Optuna/grid/random handles the actual hyperparameter search

Post-Trial Actions

Execute custom actions after each trial completes, such as committing checkpoints to git, sending notifications, or syncing to remote storage.

CLI Usage

Environment Variables

The post-trial script receives these environment variables:

Example: Git Commit Best Models

Example: Slack Notification

Python API with Callback

For more control, use the Python API with a callback function:

TrialInfo Fields

Post-trial actions are non-blocking. If a callback or script fails, a warning is logged but the sweep continues. This ensures that sweep progress isn’t lost due to callback errors.

Best Practices

  1. Start small - 10-20 trials for initial exploration
  2. Use early stopping - Stop bad trials early
  3. Fix what you know - Only sweep uncertain params
  4. Use validation data - Always have eval split
  5. Use post-trial scripts - Automate checkpointing or notifications

Next Steps

Evaluation

Evaluate sweep results

LoRA/PEFT

Efficient fine-tuning