From 2ae889052c6d0205ca677052ddb41db96a2a2620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AE=E0=AE=A9=E0=AF=8B=E0=AE=9C=E0=AF=8D=E0=AE=95?= =?UTF-8?q?=E0=AF=81=E0=AE=AE=E0=AE=BE=E0=AE=B0=E0=AF=8D=20=E0=AE=AA?= =?UTF-8?q?=E0=AE=B4=E0=AE=A9=E0=AE=BF=E0=AE=9A=E0=AF=8D=E0=AE=9A=E0=AE=BE?= =?UTF-8?q?=E0=AE=AE=E0=AE=BF?= Date: Mon, 10 Feb 2025 20:56:50 +0530 Subject: [PATCH] Fix seed parameter behavior in vLLM (#13007) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: மனோஜ்குமார் பழனிச்சாமி --- docs/seed_parameter_behavior.md | 51 +++++++++++++++++++++++++++++++++ tests/test_seed_behavior.py | 39 +++++++++++++++++++++++++ vllm/platforms/interface.py | 9 +++--- 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 docs/seed_parameter_behavior.md create mode 100644 tests/test_seed_behavior.py diff --git a/docs/seed_parameter_behavior.md b/docs/seed_parameter_behavior.md new file mode 100644 index 00000000..ff17525c --- /dev/null +++ b/docs/seed_parameter_behavior.md @@ -0,0 +1,51 @@ +# Seed Parameter Behavior in vLLM + +## Overview + +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. + +## Default Behavior + +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. + +## Specifying a Seed + +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. + +## Example Usage + +### Without Specifying a Seed + +```python +import random +from vllm import LLM + +# Initialize a vLLM model without specifying a seed +model = LLM(model="Qwen/Qwen2.5-0.5B-Instruct") + +# Try generating random numbers +print(random.randint(0, 100)) # Outputs different numbers across runs +``` + +### Specifying a Seed + +```python +import random +from vllm import LLM + +# Initialize a vLLM model with a specific seed +model = LLM(model="Qwen/Qwen2.5-0.5B-Instruct", seed=42) + +# Try generating random numbers +print(random.randint(0, 100)) # Outputs the same number across runs +``` + +## Important Notes + +- If the `seed` parameter is not specified, the behavior of global random states remains unaffected. +- 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. +- This behavior can be useful for reproducibility but may lead to non-intuitive behavior if the user is not explicitly aware of it. + +## Conclusion + +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. diff --git a/tests/test_seed_behavior.py b/tests/test_seed_behavior.py new file mode 100644 index 00000000..7e4e7156 --- /dev/null +++ b/tests/test_seed_behavior.py @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: Apache-2.0 +import random + +import numpy as np +import torch + +from vllm.platforms.interface import Platform + + +def test_seed_behavior(): + # Test with seed=None + Platform.seed_everything(None) + random_value_1 = random.randint(0, 100) + np_random_value_1 = np.random.randint(0, 100) + torch_random_value_1 = torch.randint(0, 100, (1, )).item() + + Platform.seed_everything(None) + random_value_2 = random.randint(0, 100) + np_random_value_2 = np.random.randint(0, 100) + torch_random_value_2 = torch.randint(0, 100, (1, )).item() + + assert random_value_1 != random_value_2 + assert np_random_value_1 != np_random_value_2 + assert torch_random_value_1 != torch_random_value_2 + + # Test with a specific seed + Platform.seed_everything(42) + random_value_3 = random.randint(0, 100) + np_random_value_3 = np.random.randint(0, 100) + torch_random_value_3 = torch.randint(0, 100, (1, )).item() + + Platform.seed_everything(42) + random_value_4 = random.randint(0, 100) + np_random_value_4 = np.random.randint(0, 100) + torch_random_value_4 = torch.randint(0, 100, (1, )).item() + + assert random_value_3 == random_value_4 + assert np_random_value_3 == np_random_value_4 + assert torch_random_value_3 == torch_random_value_4 diff --git a/vllm/platforms/interface.py b/vllm/platforms/interface.py index 211e288b..645d98a1 100644 --- a/vllm/platforms/interface.py +++ b/vllm/platforms/interface.py @@ -211,16 +211,17 @@ class Platform: return torch.inference_mode(mode=True) @classmethod - def seed_everything(cls, seed: int) -> None: + def seed_everything(cls, seed: Optional[int] = None) -> None: """ Set the seed of each random module. `torch.manual_seed` will set seed on all devices. Loosely based on: https://github.com/Lightning-AI/pytorch-lightning/blob/2.4.0/src/lightning/fabric/utilities/seed.py#L20 """ - random.seed(seed) - np.random.seed(seed) - torch.manual_seed(seed) + if seed is not None: + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) @classmethod def check_and_update_config(cls, vllm_config: VllmConfig) -> None: