|
1 | 1 | # Reproducibility
|
2 | 2 |
|
3 |
| -## Overview |
| 3 | +vLLM does not guarantee the reproducibility of the results by default, for the sake of performance. You need to do the following to achieve |
| 4 | +reproducible results: |
4 | 5 |
|
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 | +- For V1: Turn off multiprocessing to make the scheduling deterministic by setting `VLLM_ENABLE_V1_MULTIPROCESSING=0`. |
| 7 | +- For V0: Set the global seed (see below). |
6 | 8 |
|
7 |
| -## Default Behavior |
| 9 | +Example: <gh-file:examples/offline_inference/reproducibility.py> |
8 | 10 |
|
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. |
| 11 | +!!! warning |
10 | 12 |
|
11 |
| -## Specifying a Seed |
| 13 | + Applying the above settings [changes the random state in user code](#locality-of-random-state). |
12 | 14 |
|
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. |
| 15 | +!!! note |
14 | 16 |
|
15 |
| -## Example Usage |
| 17 | + Even with the above settings, vLLM only provides reproducibility |
| 18 | + when it runs on the same hardware and the same vLLM version. |
| 19 | + Also, the online serving API (`vllm serve`) does not support reproducibility |
| 20 | + because it is almost impossible to make the scheduling deterministic in the |
| 21 | + online setting. |
16 | 22 |
|
17 |
| -### Without Specifying a Seed |
| 23 | +## Setting the global seed |
18 | 24 |
|
19 |
| -```python |
20 |
| -import random |
21 |
| -from vllm import LLM |
| 25 | +The `seed` parameter in vLLM is used to control the random states for various random number generators. |
22 | 26 |
|
23 |
| -# Initialize a vLLM model without specifying a seed |
24 |
| -model = LLM(model="Qwen/Qwen2.5-0.5B-Instruct") |
| 27 | +If a specific seed value is provided, the random states for `random`, `np.random`, and `torch.manual_seed` will be set accordingly. |
25 | 28 |
|
26 |
| -# Try generating random numbers |
27 |
| -print(random.randint(0, 100)) # Outputs different numbers across runs |
28 |
| -``` |
| 29 | +However, in some cases, setting the seed will also [change the random state in user code](#locality-of-random-state). |
29 | 30 |
|
30 |
| -### Specifying a Seed |
| 31 | +### Default Behavior |
31 | 32 |
|
32 |
| -```python |
33 |
| -import random |
34 |
| -from vllm import LLM |
| 33 | +In V0, the `seed` parameter defaults to `None`. When the `seed` parameter is `None`, the random states for `random`, `np.random`, and `torch.manual_seed` are not set. This means that each run of vLLM will produce different results if `temperature > 0`, as expected. |
35 | 34 |
|
36 |
| -# Initialize a vLLM model with a specific seed |
37 |
| -model = LLM(model="Qwen/Qwen2.5-0.5B-Instruct", seed=42) |
| 35 | +In V1, the `seed` parameter defaults to `0` which sets the random state for each worker, so the results will remain consistent for each vLLM run even if `temperature > 0`. |
38 | 36 |
|
39 |
| -# Try generating random numbers |
40 |
| -print(random.randint(0, 100)) # Outputs the same number across runs |
41 |
| -``` |
| 37 | +!!! note |
42 | 38 |
|
43 |
| -## Important Notes |
| 39 | + It is impossible to un-specify a seed for V1 because different workers need to sample the same outputs |
| 40 | + for workflows such as speculative decoding. |
| 41 | + |
| 42 | + For more information, see: <gh-pr:17929> |
44 | 43 |
|
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. |
| 44 | +### Locality of random state |
48 | 45 |
|
49 |
| -## Conclusion |
| 46 | +The random state in user code (i.e. the code that constructs [LLM][vllm.LLM] class) is updated by vLLM under the following conditions: |
50 | 47 |
|
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. |
| 48 | +- For V0: The seed is specified. |
| 49 | +- For V1: The workers are run in the same process as user code, i.e.: `VLLM_ENABLE_V1_MULTIPROCESSING=0`. |
| 50 | + |
| 51 | +By default, these conditions are not active so you can use vLLM without having to worry about |
| 52 | +accidentally making deterministic subsequent operations that rely on random state. |
0 commit comments