Skip to main content
This page shows you how to run a sweep’s search and stopping algorithms locally instead of using the W&B cloud-hosted controller. Use the local controller when you want to inspect and instrument the code to debug issues or develop new algorithms that you can later incorporate into the cloud service. By default, W&B hosts the hyperparameter controller as a cloud service. W&B agents communicate with the controller to determine the next set of parameters to use for training. The controller also runs early stopping algorithms to determine which runs to stop. The local controller feature lets you run search and stop algorithms locally.
This feature supports faster development and debugging of new algorithms for the Sweeps tool. It isn’t intended for hyperparameter optimization workloads.

Prerequisites

Install the W&B SDK (wandb) so that the local controller commands are available:
pip install wandb sweeps
The following examples assume you already have a configuration file and a training loop defined in a Python script or Jupyter notebook. For more information, see Define sweep configuration.

Run the local controller from the command line

Initialize a sweep the same way you do when you use hyperparameter controllers hosted by W&B as a cloud service. Specify the --controller flag to indicate that you want to use the local controller for sweep jobs:
wandb sweep --controller config.yaml
Alternatively, you can initialize the sweep and specify the local controller in two steps. To separate the steps, first add the following key-value to your sweep’s YAML configuration file:
controller:
  type: local
Next, initialize the sweep:
wandb sweep config.yaml
wandb sweep generates a sweep ID. After you initialize the sweep, start a controller with wandb controller. Replace [SWEEP-ID] with the sweep ID that wandb sweep generated. You can pass the short sweep ID or include the entity and project as a path ([ENTITY]/[PROJECT]/[SWEEP-ID]):
wandb controller [SWEEP-ID]
After you specify that you want to use a local controller, start one or more sweep agents to run the sweep, the same way you usually do. For more information, see Start sweep agents. Replace [SWEEP-ID] with the sweep ID returned by wandb sweep:
wandb agent [SWEEP-ID]

Run a local controller with W&B Python SDK

The following code snippets demonstrate how to specify and use a local controller with the W&B Python SDK. Each example offers progressively more control over the controller loop, so you can choose the approach that matches how much you need to customize search and scheduling. The simplest way to use a controller with the Python SDK is to pass the sweep ID to the wandb.controller() method. Then, use the returned object’s run method to start the sweep job:
sweep = wandb.controller(sweep_id)
sweep.run()
For more control over the controller loop, step through it yourself:
import wandb

sweep = wandb.controller(sweep_id)
while not sweep.done():
    sweep.print_status()
    sweep.step()
    time.sleep(5)
For even more control over the parameters served, call search and schedule directly:
import wandb

sweep = wandb.controller(sweep_id)
while not sweep.done():
    params = sweep.search()
    sweep.schedule(params)
    sweep.print_status()
To specify your sweep entirely in code rather than a YAML configuration file, configure the search, program, controller, and parameters in Python:
import wandb

sweep = wandb.controller()
sweep.configure_search("grid")
sweep.configure_program("train-dummy.py")
sweep.configure_controller(type="local")
sweep.configure_parameter("param1", value=3)
sweep.create()
sweep.run()