> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wandb.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# How do I fix `Cuda out of memory` during a sweep?

If you see `Cuda out of memory` during a sweep, refactor your code to use process-based execution. Rewrite your code as a Python script and call the sweep agent from the CLI instead of the Python SDK.

1. Add your training logic to a Python script (for example, `train.py`):

   ```python theme={null}
   if __name__ == "__main__":
       train()
   ```

2. Reference the script in your YAML sweep configuration:

   ```yaml theme={null}
   program: train.py
   method: bayes
   metric:
     name: validation_loss
     goal: maximize
   parameters:
     learning_rate:
       min: 0.0001
       max: 0.1
     optimizer:
       values: ["adam", "sgd"]
   ```

3. Initialize the sweep with the CLI:

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

4. Start the sweep agent with the CLI. Replace `[SWEEP-ID]` with the ID returned in the previous step:

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

The CLI-based agent (`wandb agent`) runs each run as a separate process with its own memory allocation, which prevents CUDA memory from accumulating across runs. The Python SDK (`wandb.agent`) doesn't provide this process isolation.

For more information, see [Sweeps troubleshooting](/models/sweeps/troubleshoot-sweeps/).

***

<Badge stroke shape="pill" color="orange" size="md">[Sweeps](/support/models/tags/sweeps)</Badge><Badge stroke shape="pill" color="orange" size="md">[Run Crashes](/support/models/tags/run-crashes)</Badge>
