Files
xc-llm-kunlun/vllm_kunlun/platforms/envs.py

112 lines
3.9 KiB
Python
Raw Normal View History

2025-12-10 12:05:39 +08:00
# SPDX-License-Identifier: Apache-2.0
import os
from typing import TYPE_CHECKING, Any, Callable, Optional
if TYPE_CHECKING:
2025-12-10 17:51:24 +08:00
VLLM_MULTI_LOGPATH : str = "./log",
ENABLE_VLLM_MULTI_LOG : bool = False,
ENABLE_VLLM_INFER_HOOK : bool = False,
ENABLE_VLLM_OPS_HOOK : bool = False,
ENABLE_VLLM_MODULE_HOOK : bool = False
2025-12-10 12:05:39 +08:00
def maybe_convert_int(value: Optional[str]) -> Optional[int]:
"""
2025-12-10 17:51:24 +08:00
如果值是None则返回None否则将字符串转换为整数并返回
2025-12-10 12:05:39 +08:00
Args:
2025-12-10 17:51:24 +08:00
value (Optional[str], optional): 要转换的可选字符串. Defaults to None.
2025-12-10 12:05:39 +08:00
Returns:
2025-12-10 17:51:24 +08:00
Optional[int]: 如果值是None则返回None否则将字符串转换为整数并返回.
2025-12-10 12:05:39 +08:00
"""
if value is None:
return None
return int(value)
# The begin-* and end* here are used by the documentation generator
# to extract the used env vars.
# begin-env-vars-definition
xvllm_environment_variables: dict[str, Callable[[], Any]] = {
# path to the logs of redirect-output, abstrac of related are ok
2025-12-10 17:51:24 +08:00
"VLLM_MULTI_LOGPATH":
lambda: os.environ.get("VLLM_MULTI_LOGPATH", "./logs"),
# turn on / off multi-log of multi nodes & multi cards
"ENABLE_VLLM_MULTI_LOG":
lambda: (os.environ.get("ENABLE_VLLM_MULTI_LOG", "False").lower() in
("true", "1")),
# turn on / off XVLLM infer stage log ability
"ENABLE_VLLM_INFER_HOOK":
lambda: (os.environ.get("ENABLE_VLLM_INFER_HOOK", "False").lower() in
("true", "1")),
# turn on / off XVLLM infer_ops log ability
"ENABLE_VLLM_OPS_HOOK":
lambda: (os.environ.get("ENABLE_VLLM_OPS_HOOK", "False").lower() in
("true", "1")),
"ENABLE_VLLM_MODULE_HOOK":
lambda: (os.environ.get("ENABLE_VLLM_MODULE_HOOK", "False").lower() in
("true", "1")),
2025-12-10 12:05:39 +08:00
# fuse sorted op with fused_moe kernel
2025-12-10 17:51:24 +08:00
"ENABLE_VLLM_MOE_FC_SORTED":
lambda: (os.environ.get("ENABLE_VLLM_MOE_FC_SORTED", "False").lower() in
("true", "1")),
2025-12-10 12:05:39 +08:00
# enable custom dpsk scaling rope
2025-12-10 17:51:24 +08:00
"ENABLE_CUSTOM_DPSK_SCALING_ROPE":
lambda: (os.environ.get("ENABLE_CUSTOM_DPSK_SCALING_ROPE", "False").lower() in
("true", "1")),
2025-12-10 12:05:39 +08:00
# fuse qkv split & qk norm & qk rope
# only works for qwen3 dense and qwen3 moe models
2025-12-10 17:51:24 +08:00
"ENABLE_VLLM_FUSED_QKV_SPLIT_NORM_ROPE":
lambda: (os.environ.get("ENABLE_VLLM_FUSED_QKV_SPLIT_NORM_ROPE", "False").lower() in
("true", "1")),
2025-12-10 12:05:39 +08:00
}
# end-env-vars-definition
def __getattr__(name: str):
"""
2025-12-10 17:51:24 +08:00
当调用不存在的属性时该函数被调用如果属性是xvllm_environment_variables中的一个则返回相应的值否则引发AttributeError异常
2025-12-10 12:05:39 +08:00
Args:
2025-12-10 17:51:24 +08:00
name (str): 要获取的属性名称
2025-12-10 12:05:39 +08:00
Raises:
2025-12-10 17:51:24 +08:00
AttributeError (Exception): 如果属性不是xvllm_environment_variables中的一个则会引发此异常
2025-12-10 12:05:39 +08:00
Returns:
2025-12-10 17:51:24 +08:00
Any, optional: 如果属性是xvllm_environment_variables中的一个则返回相应的值否则返回None
2025-12-10 12:05:39 +08:00
"""
# lazy evaluation of environment variables
if name in xvllm_environment_variables:
return xvllm_environment_variables[name]()
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
def __dir__():
"""
2025-12-10 17:51:24 +08:00
返回一个包含所有可见的变量名称的列表
返回值list一个包含所有可见的变量名称的列表这些变量是通过`xvllm_environment_variables`字典定义的
2025-12-10 12:05:39 +08:00
Returns:
2025-12-10 17:51:24 +08:00
List[str]: 一个包含所有可见的变量名称的列表
这些变量是通过`xvllm_environment_variables`字典定义的
2025-12-10 12:05:39 +08:00
"""
return list(xvllm_environment_variables.keys())
def is_set(name: str):
"""Check if an environment variable is explicitly set."""
if name in xvllm_environment_variables:
return name in os.environ
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")