|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the tpu-inference project |
| 3 | + |
| 4 | +import functools |
| 5 | +import os |
| 6 | +from collections.abc import Callable |
| 7 | +from typing import TYPE_CHECKING, Any |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + JAX_PLATFORMS: str = "" |
| 11 | + TPU_ACCELERATOR_TYPE: str | None = None |
| 12 | + TPU_NAME: str | None = None |
| 13 | + TPU_WORKER_ID: str | None = None |
| 14 | + TPU_MULTIHOST_BACKEND: str = "" |
| 15 | + PREFILL_SLICES: str = "" |
| 16 | + DECODE_SLICES: str = "" |
| 17 | + SKIP_JAX_PRECOMPILE: bool = False |
| 18 | + MODEL_IMPL_TYPE: str = "flax_nnx" |
| 19 | + NEW_MODEL_DESIGN: bool = False |
| 20 | + PHASED_PROFILING_DIR: str = "" |
| 21 | + PYTHON_TRACER_LEVEL: int = 1 |
| 22 | + USE_MOE_EP_KERNEL: bool = False |
| 23 | + RAY_USAGE_STATS_ENABLED: str = "0" |
| 24 | + VLLM_USE_RAY_COMPILED_DAG_CHANNEL_TYPE: str = "shm" |
| 25 | + |
| 26 | +environment_variables: dict[str, Callable[[], Any]] = { |
| 27 | + # JAX platform selection (e.g., "tpu", "cpu", "proxy") |
| 28 | + "JAX_PLATFORMS": |
| 29 | + lambda: os.getenv("JAX_PLATFORMS", ""), |
| 30 | + # TPU accelerator type (e.g., "v5litepod-16", "v4-8") |
| 31 | + "TPU_ACCELERATOR_TYPE": |
| 32 | + lambda: os.getenv("TPU_ACCELERATOR_TYPE", None), |
| 33 | + # Name of the TPU resource |
| 34 | + "TPU_NAME": |
| 35 | + lambda: os.getenv("TPU_NAME", None), |
| 36 | + # Worker ID for multi-host TPU setups |
| 37 | + "TPU_WORKER_ID": |
| 38 | + lambda: os.getenv("TPU_WORKER_ID", None), |
| 39 | + # Backend for multi-host communication on TPU |
| 40 | + "TPU_MULTIHOST_BACKEND": |
| 41 | + lambda: os.getenv("TPU_MULTIHOST_BACKEND", "").lower(), |
| 42 | + # Slice configuration for disaggregated prefill workers |
| 43 | + "PREFILL_SLICES": |
| 44 | + lambda: os.getenv("PREFILL_SLICES", ""), |
| 45 | + # Slice configuration for disaggregated decode workers |
| 46 | + "DECODE_SLICES": |
| 47 | + lambda: os.getenv("DECODE_SLICES", ""), |
| 48 | + # Skip JAX precompilation step during initialization |
| 49 | + "SKIP_JAX_PRECOMPILE": |
| 50 | + lambda: bool(int(os.getenv("SKIP_JAX_PRECOMPILE", "0"))), |
| 51 | + # Model implementation type (e.g., "flax_nnx") |
| 52 | + "MODEL_IMPL_TYPE": |
| 53 | + lambda: os.getenv("MODEL_IMPL_TYPE", "flax_nnx").lower(), |
| 54 | + # Enable new experimental model design |
| 55 | + "NEW_MODEL_DESIGN": |
| 56 | + lambda: bool(int(os.getenv("NEW_MODEL_DESIGN", "0"))), |
| 57 | + # Directory to store phased profiling output |
| 58 | + "PHASED_PROFILING_DIR": |
| 59 | + lambda: os.getenv("PHASED_PROFILING_DIR", ""), |
| 60 | + # Python tracer level for profiling |
| 61 | + "PYTHON_TRACER_LEVEL": |
| 62 | + lambda: int(os.getenv("PYTHON_TRACER_LEVEL", "1")), |
| 63 | + # Use custom expert-parallel kernel for MoE (Mixture of Experts) |
| 64 | + "USE_MOE_EP_KERNEL": |
| 65 | + lambda: bool(int(os.getenv("USE_MOE_EP_KERNEL", "0"))), |
| 66 | + # Enable/disable Ray usage statistics collection |
| 67 | + "RAY_USAGE_STATS_ENABLED": |
| 68 | + lambda: os.getenv("RAY_USAGE_STATS_ENABLED", "0"), |
| 69 | + # Ray compiled DAG channel type for TPU |
| 70 | + "VLLM_USE_RAY_COMPILED_DAG_CHANNEL_TYPE": |
| 71 | + lambda: os.getenv("VLLM_USE_RAY_COMPILED_DAG_CHANNEL_TYPE", "shm"), |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +def __getattr__(name: str) -> Any: |
| 76 | + """ |
| 77 | + Gets environment variables lazily. |
| 78 | +
|
| 79 | + NOTE: After enable_envs_cache() invocation (which triggered after service |
| 80 | + initialization), all environment variables will be cached. |
| 81 | + """ |
| 82 | + if name in environment_variables: |
| 83 | + return environment_variables[name]() |
| 84 | + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") |
| 85 | + |
| 86 | + |
| 87 | +def enable_envs_cache() -> None: |
| 88 | + """ |
| 89 | + Enables caching of environment variables by wrapping the module's __getattr__ |
| 90 | + function with functools.cache(). This improves performance by avoiding |
| 91 | + repeated re-evaluation of environment variables. |
| 92 | +
|
| 93 | + NOTE: This should be called after service initialization. Once enabled, |
| 94 | + environment variable values are cached and will not reflect changes to |
| 95 | + os.environ until the process is restarted. |
| 96 | + """ |
| 97 | + # Tag __getattr__ with functools.cache |
| 98 | + global __getattr__ |
| 99 | + __getattr__ = functools.cache(__getattr__) |
| 100 | + |
| 101 | + # Cache all environment variables |
| 102 | + for key in environment_variables: |
| 103 | + __getattr__(key) |
| 104 | + |
| 105 | + |
| 106 | +def __dir__() -> list[str]: |
| 107 | + return list(environment_variables.keys()) |
0 commit comments