| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import json |
| 4 | import os |
| 5 | |
| 6 | import vllm.envs as envs |
| 7 | from vllm.logger import init_logger |
| 8 | |
| 9 | logger = init_logger(__name__) |
| 10 | |
| 11 | CONFIG_HOME = envs.VLLM_CONFIG_ROOT |
| 12 | |
| 13 | # Env vars that should NOT be copied from the driver to Ray workers. |
| 14 | RAY_NON_CARRY_OVER_ENV_VARS_FILE = os.path.join( |
| 15 | CONFIG_HOME, "ray_non_carry_over_env_vars.json" |
| 16 | ) |
| 17 | |
| 18 | try: |
| 19 | if os.path.exists(RAY_NON_CARRY_OVER_ENV_VARS_FILE): |
| 20 | with open(RAY_NON_CARRY_OVER_ENV_VARS_FILE) as f: |
| 21 | RAY_NON_CARRY_OVER_ENV_VARS = set(json.load(f)) |
| 22 | else: |
| 23 | RAY_NON_CARRY_OVER_ENV_VARS = set() |
| 24 | except json.JSONDecodeError: |
| 25 | logger.warning( |
| 26 | "Failed to parse %s. Using an empty set for non-carry-over env vars.", |
| 27 | RAY_NON_CARRY_OVER_ENV_VARS_FILE, |
| 28 | ) |
| 29 | RAY_NON_CARRY_OVER_ENV_VARS = set() |
| 30 | |
| 31 | # --------------------------------------------------------------------------- |
| 32 | # Built-in defaults for env var propagation. |
| 33 | # Users can add more via VLLM_RAY_EXTRA_ENV_VAR_PREFIXES_TO_COPY and |
| 34 | # VLLM_RAY_EXTRA_ENV_VARS_TO_COPY (additive, not replacing). |
| 35 | # --------------------------------------------------------------------------- |
| 36 | DEFAULT_ENV_VAR_PREFIXES: set[str] = { |
| 37 | "VLLM_", |
| 38 | "FLASH_ATTENTION_", |
| 39 | "LMCACHE_", |
| 40 | "NCCL_", |
| 41 | "UCX_", |
| 42 | "HF_", |
| 43 | "HUGGING_FACE_", |
| 44 | } |
| 45 | |
| 46 | DEFAULT_EXTRA_ENV_VARS: set[str] = { |
| 47 | "PYTHONHASHSEED", |
| 48 | } |
| 49 | |
| 50 | |
| 51 | def _parse_csv(value: str) -> set[str]: |
| 52 | """Split a comma-separated string into a set of stripped, non-empty tokens.""" |
| 53 | return {tok.strip() for tok in value.split(",") if tok.strip()} |
| 54 | |
| 55 | |
| 56 | def get_env_vars_to_copy( |
| 57 | exclude_vars: set[str] | None = None, |
| 58 | additional_vars: set[str] | None = None, |
| 59 | destination: str | None = None, |
| 60 | ) -> set[str]: |
| 61 | """Return the env var names to copy from the driver to Ray actors. |
| 62 | |
| 63 | The result is the union of: |
| 64 | |
| 65 | 1. Env vars registered in ``vllm.envs.environment_variables``. |
| 66 | 2. Env vars in ``os.environ`` matching a prefix in |
| 67 | ``DEFAULT_ENV_VAR_PREFIXES`` + ``VLLM_RAY_EXTRA_ENV_VAR_PREFIXES_TO_COPY``. |
| 68 | 3. Individual names in ``DEFAULT_EXTRA_ENV_VARS`` + |
| 69 | ``VLLM_RAY_EXTRA_ENV_VARS_TO_COPY``. |
| 70 | 4. Caller-supplied *additional_vars* (e.g. platform-specific). |
| 71 | |
| 72 | Minus any names in *exclude_vars* or ``RAY_NON_CARRY_OVER_ENV_VARS``. |
| 73 | |
| 74 | Args: |
| 75 | exclude_vars: Env vars to exclude (e.g. worker-specific ones). |
| 76 | additional_vars: Extra individual env var names to copy. Useful |
| 77 | for caller-specific vars (e.g. platform env vars). |
| 78 | destination: Label used in log messages only. |
| 79 | |
| 80 | """ |
| 81 | exclude = (exclude_vars or set()) | RAY_NON_CARRY_OVER_ENV_VARS |
| 82 | |
| 83 | # -- prefixes (built-in + user-supplied, additive) ---------------------- |
| 84 | prefixes = DEFAULT_ENV_VAR_PREFIXES | _parse_csv( |
| 85 | envs.VLLM_RAY_EXTRA_ENV_VAR_PREFIXES_TO_COPY |
| 86 | ) |
| 87 | |
| 88 | # -- collect env var names ---------------------------------------------- |
| 89 | # 1. vLLM's registered env vars |
| 90 | result = set(envs.environment_variables) |
| 91 | # 2. Prefix-matched vars present in the current environment |
| 92 | result |= {name for name in os.environ if any(name.startswith(p) for p in prefixes)} |
| 93 | # 3. Individual extra vars (built-in + user-supplied, additive) |
| 94 | result |= DEFAULT_EXTRA_ENV_VARS | _parse_csv(envs.VLLM_RAY_EXTRA_ENV_VARS_TO_COPY) |
| 95 | # 4. Caller-supplied extra vars (e.g. platform-specific) |
| 96 | result |= additional_vars or set() |
| 97 | # 5. Exclude worker-specific and user-blacklisted vars |
| 98 | result -= exclude |
| 99 | |
| 100 | # -- logging ------------------------------------------------------------ |
| 101 | dest = f" to {destination}" if destination else "" |
| 102 | logger.info("Env var prefixes to copy: %s", sorted(prefixes)) |
| 103 | logger.info( |
| 104 | "Copying the following environment variables%s: %s", |
| 105 | dest, |
| 106 | sorted(v for v in result if v in os.environ), |
| 107 | ) |
| 108 | if RAY_NON_CARRY_OVER_ENV_VARS: |
| 109 | logger.info( |
| 110 | "RAY_NON_CARRY_OVER_ENV_VARS from config: %s", |
| 111 | RAY_NON_CARRY_OVER_ENV_VARS, |
| 112 | ) |
| 113 | logger.info( |
| 114 | "To exclude env vars from copying, add them to %s", |
| 115 | RAY_NON_CARRY_OVER_ENV_VARS_FILE, |
| 116 | ) |
| 117 | |
| 118 | return result |