-
Notifications
You must be signed in to change notification settings - Fork 41
Centralized environment variables #1058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1c29d25
fix: correct environment variable type conversions and improve comments
xingliu14 4f0289d
Use envs.USE_MOE_EP_KERNEL instead of direct os.getenv
xingliu14 575d181
Fix profiler directory env var reference and update tests
xingliu14 26db621
Resolve conflicts
xingliu14 baf057f
Remove unused JAX_RANDOM_WEIGHTS environment variable
xingliu14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the tpu-inference project | ||
|
|
||
| import functools | ||
| import os | ||
| from collections.abc import Callable | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| if TYPE_CHECKING: | ||
| JAX_PLATFORMS: str = "" | ||
| TPU_ACCELERATOR_TYPE: str | None = None | ||
| TPU_NAME: str | None = None | ||
| TPU_WORKER_ID: str | None = None | ||
| TPU_MULTIHOST_BACKEND: str = "" | ||
| PREFILL_SLICES: str = "" | ||
| DECODE_SLICES: str = "" | ||
| SKIP_JAX_PRECOMPILE: bool = False | ||
| MODEL_IMPL_TYPE: str = "flax_nnx" | ||
| NEW_MODEL_DESIGN: bool = False | ||
| PHASED_PROFILING_DIR: str = "" | ||
| PYTHON_TRACER_LEVEL: int = 1 | ||
| USE_MOE_EP_KERNEL: bool = False | ||
| RAY_USAGE_STATS_ENABLED: str = "0" | ||
| VLLM_USE_RAY_COMPILED_DAG_CHANNEL_TYPE: str = "shm" | ||
|
|
||
| environment_variables: dict[str, Callable[[], Any]] = { | ||
| # JAX platform selection (e.g., "tpu", "cpu", "proxy") | ||
| "JAX_PLATFORMS": | ||
| lambda: os.getenv("JAX_PLATFORMS", ""), | ||
| # TPU accelerator type (e.g., "v5litepod-16", "v4-8") | ||
| "TPU_ACCELERATOR_TYPE": | ||
| lambda: os.getenv("TPU_ACCELERATOR_TYPE", None), | ||
| # Name of the TPU resource | ||
| "TPU_NAME": | ||
| lambda: os.getenv("TPU_NAME", None), | ||
| # Worker ID for multi-host TPU setups | ||
| "TPU_WORKER_ID": | ||
| lambda: os.getenv("TPU_WORKER_ID", None), | ||
| # Backend for multi-host communication on TPU | ||
| "TPU_MULTIHOST_BACKEND": | ||
| lambda: os.getenv("TPU_MULTIHOST_BACKEND", "").lower(), | ||
| # Slice configuration for disaggregated prefill workers | ||
| "PREFILL_SLICES": | ||
| lambda: os.getenv("PREFILL_SLICES", ""), | ||
| # Slice configuration for disaggregated decode workers | ||
| "DECODE_SLICES": | ||
| lambda: os.getenv("DECODE_SLICES", ""), | ||
| # Skip JAX precompilation step during initialization | ||
| "SKIP_JAX_PRECOMPILE": | ||
| lambda: bool(int(os.getenv("SKIP_JAX_PRECOMPILE", "0"))), | ||
| # Model implementation type (e.g., "flax_nnx") | ||
| "MODEL_IMPL_TYPE": | ||
| lambda: os.getenv("MODEL_IMPL_TYPE", "flax_nnx").lower(), | ||
| # Enable new experimental model design | ||
| "NEW_MODEL_DESIGN": | ||
| lambda: bool(int(os.getenv("NEW_MODEL_DESIGN", "0"))), | ||
| # Directory to store phased profiling output | ||
| "PHASED_PROFILING_DIR": | ||
| lambda: os.getenv("PHASED_PROFILING_DIR", ""), | ||
| # Python tracer level for profiling | ||
| "PYTHON_TRACER_LEVEL": | ||
| lambda: int(os.getenv("PYTHON_TRACER_LEVEL", "1")), | ||
| # Use custom expert-parallel kernel for MoE (Mixture of Experts) | ||
| "USE_MOE_EP_KERNEL": | ||
| lambda: bool(int(os.getenv("USE_MOE_EP_KERNEL", "0"))), | ||
| # Enable/disable Ray usage statistics collection | ||
| "RAY_USAGE_STATS_ENABLED": | ||
| lambda: os.getenv("RAY_USAGE_STATS_ENABLED", "0"), | ||
| # Ray compiled DAG channel type for TPU | ||
| "VLLM_USE_RAY_COMPILED_DAG_CHANNEL_TYPE": | ||
| lambda: os.getenv("VLLM_USE_RAY_COMPILED_DAG_CHANNEL_TYPE", "shm"), | ||
| } | ||
|
|
||
|
|
||
| def __getattr__(name: str) -> Any: | ||
| """ | ||
| Gets environment variables lazily. | ||
|
|
||
| NOTE: After enable_envs_cache() invocation (which triggered after service | ||
| initialization), all environment variables will be cached. | ||
| """ | ||
| if name in environment_variables: | ||
| return environment_variables[name]() | ||
| raise AttributeError(f"module {__name__!r} has no attribute {name!r}") | ||
|
|
||
|
|
||
| def enable_envs_cache() -> None: | ||
| """ | ||
| Enables caching of environment variables by wrapping the module's __getattr__ | ||
| function with functools.cache(). This improves performance by avoiding | ||
| repeated re-evaluation of environment variables. | ||
|
|
||
| NOTE: This should be called after service initialization. Once enabled, | ||
| environment variable values are cached and will not reflect changes to | ||
| os.environ until the process is restarted. | ||
| """ | ||
| # Tag __getattr__ with functools.cache | ||
| global __getattr__ | ||
| __getattr__ = functools.cache(__getattr__) | ||
|
|
||
| # Cache all environment variables | ||
| for key in environment_variables: | ||
| __getattr__(key) | ||
|
|
||
|
|
||
| def __dir__() -> list[str]: | ||
| return list(environment_variables.keys()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add test coverage like what vllm.main do?
https://github.com/vllm-project/vllm/blob/main/tests/test_envs.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea. I initially thought that existing unit test that utilizes these environment variables has passed as a sufficient coverage test. But it's probably a good idea to add a separate test as well.
@xingliu14, can you add a unit test like the one reference here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll add test on the follow up PR.