Skip to content

Commit ef5e072

Browse files
committed
Fix seed parameter behavior in vLLM
Fixes #11953 Update the `seed_everything` method to handle `seed=None` appropriately. * Modify `vllm/platforms/interface.py` to update the `seed_everything` method to handle `seed=None` and skip setting random seeds if `seed=None`. * Add a new test file `tests/test_seed_behavior.py` to verify the behavior of the `seed` parameter, including cases where `seed=None` and `seed` is specified. * Add documentation in `docs/seed_parameter_behavior.md` explaining the behavior of the `seed` parameter, its effect on global random states, and provide examples of usage with and without specifying the `seed` parameter. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/vllm-project/vllm/issues/11953?shareId=XXXX-XXXX-XXXX-XXXX). Signed-off-by: மனோஜ்குமார் பழனிச்சாமி <smartmanoj42857@gmail.com>
1 parent b2496bb commit ef5e072

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

docs/seed_parameter_behavior.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Seed Parameter Behavior in vLLM
2+
3+
## Overview
4+
5+
The `seed` parameter in vLLM is used to control the random states for various random number generators. This parameter can affect the behavior of random operations in user code, especially when working with models in vLLM.
6+
7+
## Default Behavior
8+
9+
By default, the `seed` parameter is set to `None`. When the `seed` parameter is `None`, the global random states for `random`, `np.random`, and `torch.manual_seed` are not set. This means that the random operations will behave as expected, without any fixed random states.
10+
11+
## Specifying a Seed
12+
13+
If a specific seed value is provided, the global random states for `random`, `np.random`, and `torch.manual_seed` will be set accordingly. This can be useful for reproducibility, as it ensures that the random operations produce the same results across multiple runs.
14+
15+
## Example Usage
16+
17+
### Without Specifying a Seed
18+
19+
```python
20+
import random
21+
from vllm import LLM
22+
23+
# Initialize a vLLM model without specifying a seed
24+
model = LLM(model="Qwen/Qwen2.5-0.5B-Instruct")
25+
26+
# Try generating random numbers
27+
print(random.randint(0, 100)) # Outputs different numbers across runs
28+
```
29+
30+
### Specifying a Seed
31+
32+
```python
33+
import random
34+
from vllm import LLM
35+
36+
# Initialize a vLLM model with a specific seed
37+
model = LLM(model="Qwen/Qwen2.5-0.5B-Instruct", seed=42)
38+
39+
# Try generating random numbers
40+
print(random.randint(0, 100)) # Outputs the same number across runs
41+
```
42+
43+
## Important Notes
44+
45+
- If the `seed` parameter is not specified, the behavior of global random states remains unaffected.
46+
- If a specific seed value is provided, the global random states for `random`, `np.random`, and `torch.manual_seed` will be set to that value.
47+
- This behavior can be useful for reproducibility but may lead to non-intuitive behavior if the user is not explicitly aware of it.
48+
49+
## Conclusion
50+
51+
Understanding the behavior of the `seed` parameter in vLLM is crucial for ensuring the expected behavior of random operations in your code. By default, the `seed` parameter is set to `None`, which means that the global random states are not affected. However, specifying a seed value can help achieve reproducibility in your experiments.

tests/test_seed_behavior.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import random
2+
import numpy as np
3+
import torch
4+
from vllm.platforms.interface import Platform
5+
6+
def test_seed_behavior():
7+
# Test with seed=None
8+
Platform.seed_everything(None)
9+
random_value_1 = random.randint(0, 100)
10+
np_random_value_1 = np.random.randint(0, 100)
11+
torch_random_value_1 = torch.randint(0, 100, (1,)).item()
12+
13+
Platform.seed_everything(None)
14+
random_value_2 = random.randint(0, 100)
15+
np_random_value_2 = np.random.randint(0, 100)
16+
torch_random_value_2 = torch.randint(0, 100, (1,)).item()
17+
18+
assert random_value_1 != random_value_2
19+
assert np_random_value_1 != np_random_value_2
20+
assert torch_random_value_1 != torch_random_value_2
21+
22+
# Test with a specific seed
23+
Platform.seed_everything(42)
24+
random_value_3 = random.randint(0, 100)
25+
np_random_value_3 = np.random.randint(0, 100)
26+
torch_random_value_3 = torch.randint(0, 100, (1,)).item()
27+
28+
Platform.seed_everything(42)
29+
random_value_4 = random.randint(0, 100)
30+
np_random_value_4 = np.random.randint(0, 100)
31+
torch_random_value_4 = torch.randint(0, 100, (1,)).item()
32+
33+
assert random_value_3 == random_value_4
34+
assert np_random_value_3 == np_random_value_4
35+
assert torch_random_value_3 == torch_random_value_4

vllm/platforms/interface.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,17 @@ def inference_mode(cls):
211211
return torch.inference_mode(mode=True)
212212

213213
@classmethod
214-
def seed_everything(cls, seed: int) -> None:
214+
def seed_everything(cls, seed: Optional[int] = None) -> None:
215215
"""
216216
Set the seed of each random module.
217217
`torch.manual_seed` will set seed on all devices.
218218
219219
Loosely based on: https://github.com/Lightning-AI/pytorch-lightning/blob/2.4.0/src/lightning/fabric/utilities/seed.py#L20
220220
"""
221-
random.seed(seed)
222-
np.random.seed(seed)
223-
torch.manual_seed(seed)
221+
if seed is not None:
222+
random.seed(seed)
223+
np.random.seed(seed)
224+
torch.manual_seed(seed)
224225

225226
@classmethod
226227
def check_and_update_config(cls, vllm_config: VllmConfig) -> None:

0 commit comments

Comments
 (0)