Skip to content

Commit c063ff9

Browse files
SmartManojlulmer
authored andcommitted
Fix seed parameter behavior in vLLM (vllm-project#13007)
Signed-off-by: மனோஜ்குமார் பழனிச்சாமி <[email protected]> Signed-off-by: Louis Ulmer <[email protected]>
1 parent 74c9b38 commit c063ff9

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

docs/seed_parameter_behavior.md

+51
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

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

vllm/platforms/interface.py

+5-4
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)