Skip to content

Commit 6b8822e

Browse files
Sweeps: Add notes to Sweeps walkthrough and wandb.agent() docs about guarding main when using multiprocessing (#1555)
Description ----------- Add warnings next to `wandb.agent()` in both the tutorial and `wandb.agent()` doc about guarding your `wandb.sweep` and `wandb.agent` calls with `if __name__ == '__main__':` if you're using `multiprocessing`, along with links to official documentation about it. `guides/sweeps/walkthrough/`: <img width="1020" height="516" alt="Screenshot 2025-08-26 at 10 54 07" src="https://github.com/user-attachments/assets/6285357d-219b-46c7-814d-667445385f73" /> `guides/sweeps/start-sweep-agents/`: <img width="1029" height="1227" alt="Screenshot 2025-08-26 at 10 53 57" src="https://github.com/user-attachments/assets/e1afac2d-5fe3-4df7-b420-9a5e4ad2c14b" /> Related issues ----------- - Fixes https://wandb.atlassian.net/browse/WB-22363 <!-- preview-links-comment --> 📄 **[View preview links for changed content](#1555 (comment) --------- Co-authored-by: Noah Luna <15202580+ngrayluna@users.noreply.github.com>
1 parent 73d26b6 commit 6b8822e

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

content/en/guides/models/sweeps/start-sweep-agents.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ Use the W&B Python SDK library to start a sweep. Provide the sweep ID that was r
4141
```python
4242
wandb.agent(sweep_id=sweep_id, function=function_name)
4343
```
44+
45+
{{% alert color="secondary" title="Multiprocessing" %}}
46+
You must wrap your `wandb.agent()` and `wandb.sweep()` calls with `if __name__ == '__main__':` if you use Python standard library's `multiprocessing` or PyTorch's `pytorch.multiprocessing` package. For example:
47+
48+
```python
49+
if __name__ == '__main__':
50+
wandb.agent(sweep_id="<sweep_id>", function="<function>", count="<count>")
51+
```
52+
53+
Wrapping your code with this convention ensures that it is only executed when the script is run directly, and not when it is imported as a module in a worker process.
54+
55+
See [Python standard library `multiprocessing`](https://docs.python.org/3/library/multiprocessing.html#the-spawn-and-forkserver-start-methods) or [PyTorch `multiprocessing`](https://docs.pytorch.org/docs/stable/notes/multiprocessing.html#asynchronous-multiprocess-training-e-g-hogwild) for more information about multiprocessing. See https://realpython.com/if-name-main-python/ for information about the `if __name__ == '__main__':` convention.
56+
{{% /alert %}}
57+
4458
{{% /tab %}}
4559
{{< /tabpane >}}
4660

@@ -71,7 +85,7 @@ wandb.agent(sweep_id, count=count)
7185

7286
{{% alert color="secondary" %}}
7387
If you start a new run after the sweep agent has finished, within the same script or notebook, then you should call `wandb.teardown()` before starting the new run.
74-
{{% /alert %}}
88+
{{% /alert %}}
7589
{{% /tab %}}
7690
{{% tab header="CLI" %}}
7791
First, initialize your sweep with the [`wandb sweep`]({{< relref "/ref/cli/wandb-sweep.md" >}}) command. For more information, see [Initialize sweeps]({{< relref "./initialize-sweeps.md" >}}).
@@ -86,6 +100,6 @@ Pass an integer value to the count flag to set the maximum number of runs to try
86100
NUM=10
87101
SWEEPID="dtzl1o7u"
88102
wandb agent --count $NUM $SWEEPID
89-
```
103+
```
90104
{{% /tab %}}
91105
{{< /tabpane >}}

content/en/guides/models/sweeps/walkthrough.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This page shows how to define, initialize, and run a sweep. There are four main
1919

2020
Copy and paste the following code into a Jupyter Notebook or Python script:
2121

22-
```python
22+
```python
2323
# Import the W&B Python Library and log into W&B
2424
import wandb
2525

@@ -109,12 +109,26 @@ For more information about initializing sweeps, see [Initialize sweeps]({{< relr
109109

110110
## Start the Sweep
111111

112-
Use the [`wandb.agent`]({{< relref "/ref/python/sdk/functions/agent.md" >}}) API call to start a sweep.
112+
Use the [`wandb.agent()`]({{< relref "/ref/python/sdk/functions/agent.md" >}}) API call to start a sweep.
113113

114114
```python
115115
wandb.agent(sweep_id, function=main, count=10)
116116
```
117117

118+
{{% alert color="secondary" title="Multiprocessing" %}}
119+
You must wrap your `wandb.agent()` and `wandb.sweep()` calls with `if __name__ == '__main__':` if you use Python standard library's `multiprocessing` or PyTorch's `pytorch.multiprocessing` package. For example:
120+
121+
```python
122+
if __name__ == '__main__':
123+
wandb.agent(sweep_id="<sweep_id>", function="<function>", count="<count>")
124+
```
125+
126+
Wrapping your code with this convention ensures that it is only executed when the script is run directly, and not when it is imported as a module in a worker process.
127+
128+
See [Python standard library `multiprocessing`](https://docs.python.org/3/library/multiprocessing.html#the-spawn-and-forkserver-start-methods) or [PyTorch `multiprocessing`](https://docs.pytorch.org/docs/stable/notes/multiprocessing.html#asynchronous-multiprocess-training-e-g-hogwild) for more information about multiprocessing. See https://realpython.com/if-name-main-python/ for information about the `if __name__ == '__main__':` convention.
129+
{{% /alert %}}
130+
131+
118132
## Visualize results (optional)
119133

120134
Open your project to see your live results in the W&B App dashboard. With just a few clicks, construct rich, interactive charts like [parallel coordinates plots]({{< relref "/guides/models/app/features/panels/parallel-coordinates.md" >}}),[ parameter importance analyzes]({{< relref "/guides/models/app/features/panels/parameter-importance.md" >}}), and [additional chart types]({{< relref "/guides/models/app/features/panels/" >}}).
@@ -126,5 +140,3 @@ For more information about how to visualize results, see [Visualize sweep result
126140
## Stop the agent (optional)
127141

128142
In the terminal, press `Ctrl+C` to stop the current run. Press it again to terminate the agent.
129-
130-
```

0 commit comments

Comments
 (0)