> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-update-regex-mention.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Search and stop algorithms locally instead of using the W&B cloud-hosted service.

# Manage algorithms locally

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 to inspect and instrument the code to debug issues or develop new algorithms that you can later incorporate into the cloud-hosted controller.

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.

<Warning>
  This feature supports faster development and debugging of new algorithms for the Sweeps tool. It isn't appropriate for hyperparameter optimization workloads.
</Warning>

## Prerequisites

Install the W\&B SDK (`wandb`) so that the local controller commands are available:

```bash theme={null}
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](/models/sweeps/define-sweep-configuration).

## Run a local controller from the command line

Initialize a sweep the same way as when you use hyperparameter controllers hosted by W\&B as a cloud service, but pass the `--controller` flag to use the local controller:

1. Initialize the sweep with the `--controller` flag:

   ```bash theme={null}
   wandb sweep --controller config.yaml
   ```

2. Note the sweep ID that `wandb sweep` generates, then start the controller. Replace `[SWEEP-ID]` with the sweep ID. You can pass the short sweep ID alone or include the entity and project as a path (`[ENTITY]/[PROJECT]/[SWEEP-ID]`):

   ```bash theme={null}
   wandb controller [SWEEP-ID]
   ```

3. Start one or more sweep agents to run the sweep. Replace `[SWEEP-ID]` with the sweep ID:

   ```bash theme={null}
   wandb agent [SWEEP-ID]
   ```

   For more information, see [Start sweep agents](/models/sweeps/start-sweep-agents).

Alternatively, add the controller to your configuration file so you don't need to pass `--controller` each time you initialize the sweep:

```yaml theme={null}
controller:
  type: local
```

Then initialize the sweep without the flag, and continue with steps 2 and 3:

```bash theme={null}
wandb sweep config.yaml
```

## 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, so you can choose the approach that matches how much you need to customize search and scheduling.

To use a controller with the Python SDK, pass the sweep ID to the [`wandb.controller()`](/models/ref/python/functions/controller) method, then call the returned object's `run` method to start the sweep job:

```python theme={null}
sweep = wandb.controller(sweep_id)
sweep.run()
```

For more control over the controller loop, step through it yourself:

```python theme={null}
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:

```python theme={null}
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:

```python theme={null}
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()
```
