tiny unify environ usage (#15335)

This commit is contained in:
Liangsheng Yin
2025-12-17 23:31:43 +08:00
committed by GitHub
parent eeb2b9b259
commit 0c00220795
11 changed files with 35 additions and 41 deletions

View File

@@ -1040,8 +1040,8 @@ multimodal_model_archs = [
"PaddleOCRVLForConditionalGeneration",
]
if envs.SGLANG_EXTERNAL_MM_MODEL_ARCH.value:
multimodal_model_archs.append(envs.SGLANG_EXTERNAL_MM_MODEL_ARCH.value)
if external_mm_model_arch := envs.SGLANG_EXTERNAL_MM_MODEL_ARCH.get():
multimodal_model_archs.append(external_mm_model_arch)
def is_multimodal_model(model_architectures: List[str]):

View File

@@ -70,10 +70,6 @@ class EnvField:
os.environ.pop(self.name, None)
self._set_to_none = False
@property
def value(self):
return self.get()
def __bool__(self):
raise RuntimeError(
"Please use `envs.YOUR_FLAG.get()` instead of `envs.YOUR_FLAG`"
@@ -425,9 +421,9 @@ def example_with_exit_stack():
# Use this style of context manager in unit test
exit_stack = ExitStack()
exit_stack.enter_context(envs.SGLANG_TEST_RETRACT.override(False))
assert envs.SGLANG_TEST_RETRACT.value is False
assert envs.SGLANG_TEST_RETRACT.get() is False
exit_stack.close()
assert envs.SGLANG_TEST_RETRACT.value is None
assert envs.SGLANG_TEST_RETRACT.get() is None
def example_with_subprocess():
@@ -472,29 +468,29 @@ def example_with_implicit_bool_avoidance():
def examples():
# Example usage for envs
envs.SGLANG_TEST_RETRACT.clear()
assert envs.SGLANG_TEST_RETRACT.value is False
assert envs.SGLANG_TEST_RETRACT.get() is False
envs.SGLANG_TEST_RETRACT.set(None)
assert envs.SGLANG_TEST_RETRACT.is_set() and envs.SGLANG_TEST_RETRACT.value is None
assert envs.SGLANG_TEST_RETRACT.is_set() and envs.SGLANG_TEST_RETRACT.get() is None
envs.SGLANG_TEST_RETRACT.clear()
assert not envs.SGLANG_TEST_RETRACT.is_set()
envs.SGLANG_TEST_RETRACT.set(True)
assert envs.SGLANG_TEST_RETRACT.value is True
assert envs.SGLANG_TEST_RETRACT.get() is True
with envs.SGLANG_TEST_RETRACT.override(None):
assert (
envs.SGLANG_TEST_RETRACT.is_set() and envs.SGLANG_TEST_RETRACT.value is None
envs.SGLANG_TEST_RETRACT.is_set() and envs.SGLANG_TEST_RETRACT.get() is None
)
assert envs.SGLANG_TEST_RETRACT.value is True
assert envs.SGLANG_TEST_RETRACT.get() is True
envs.SGLANG_TEST_RETRACT.set(None)
with envs.SGLANG_TEST_RETRACT.override(True):
assert envs.SGLANG_TEST_RETRACT.value is True
assert envs.SGLANG_TEST_RETRACT.get() is True
assert envs.SGLANG_TEST_RETRACT.is_set() and envs.SGLANG_TEST_RETRACT.value is None
assert envs.SGLANG_TEST_RETRACT.is_set() and envs.SGLANG_TEST_RETRACT.get() is None
example_with_exit_stack()
example_with_subprocess()

View File

@@ -268,9 +268,9 @@ class LogitsProcessor(nn.Module):
self.return_full_logits = return_full_logits
# enable chunked logprobs processing
self.enable_logprobs_chunk = envs.SGLANG_ENABLE_LOGITS_PROCESSER_CHUNK.value
self.enable_logprobs_chunk = envs.SGLANG_ENABLE_LOGITS_PROCESSER_CHUNK.get()
# chunk size for logprobs processing
self.logprobs_chunk_size = envs.SGLANG_LOGITS_PROCESSER_CHUNK_SIZE.value
self.logprobs_chunk_size = envs.SGLANG_LOGITS_PROCESSER_CHUNK_SIZE.get()
def compute_logprobs_for_multi_item_scoring(
self,

View File

@@ -183,7 +183,7 @@ class RotaryEmbedding(CustomOp):
return
# Align to reduce realloc frequency
align = envs.SGLANG_ROPE_CACHE_ALIGN.value
align = envs.SGLANG_ROPE_CACHE_ALIGN.get()
new_len = ((needed_max_pos + align) // align) * align
device = self.cos_sin_cache.device
dtype = self.cos_sin_cache.dtype

View File

@@ -215,10 +215,8 @@ class TokenizerManager(TokenizerCommunicatorMixin, TokenizerManagerMultiItemMixi
# Initialize tokenizer and processor
if self.model_config.is_multimodal:
import_processors("sglang.srt.multimodal.processors")
if envs.SGLANG_EXTERNAL_MM_PROCESSOR_PACKAGE.value:
import_processors(
envs.SGLANG_EXTERNAL_MM_PROCESSOR_PACKAGE.value, overwrite=True
)
if mm_process_pkg := envs.SGLANG_EXTERNAL_MM_PROCESSOR_PACKAGE.get():
import_processors(mm_process_pkg, overwrite=True)
_processor = _get_processor_wrapper(server_args)
transport_mode = _determine_tensor_transport_mode(self.server_args)

View File

@@ -57,7 +57,7 @@ class MooncakeStoreConfig:
raise RuntimeError(
f"Config file path not set. Please set {envs.SGLANG_HICACHE_MOONCAKE_CONFIG_PATH.name}"
)
file_path = envs.SGLANG_HICACHE_MOONCAKE_CONFIG_PATH.value
file_path = envs.SGLANG_HICACHE_MOONCAKE_CONFIG_PATH.get()
try:
with open(file_path) as fin:
config = json.load(fin)
@@ -104,7 +104,7 @@ class MooncakeStoreConfig:
# then fall back to LOCAL_HOSTNAME if not set.
# This is for forward compatibility with the legacy LOCAL_HOSTNAME environment variable.
if envs.MOONCAKE_LOCAL_HOSTNAME.is_set():
local_hostname = envs.MOONCAKE_LOCAL_HOSTNAME.value
local_hostname = envs.MOONCAKE_LOCAL_HOSTNAME.get()
else:
local_hostname = os.getenv(
"LOCAL_HOSTNAME", envs.MOONCAKE_LOCAL_HOSTNAME.default
@@ -112,15 +112,15 @@ class MooncakeStoreConfig:
return MooncakeStoreConfig(
local_hostname=local_hostname,
metadata_server=envs.MOONCAKE_TE_META_DATA_SERVER.value,
metadata_server=envs.MOONCAKE_TE_META_DATA_SERVER.get(),
global_segment_size=_parse_global_segment_size(
envs.MOONCAKE_GLOBAL_SEGMENT_SIZE.value
envs.MOONCAKE_GLOBAL_SEGMENT_SIZE.get()
),
protocol=envs.MOONCAKE_PROTOCOL.value,
device_name=envs.MOONCAKE_DEVICE.value,
master_server_address=envs.MOONCAKE_MASTER.value,
master_metrics_port=envs.MOONCAKE_MASTER_METRICS_PORT.value,
check_server=envs.MOONCAKE_CHECK_SERVER.value,
protocol=envs.MOONCAKE_PROTOCOL.get(),
device_name=envs.MOONCAKE_DEVICE.get(),
master_server_address=envs.MOONCAKE_MASTER.get(),
master_metrics_port=envs.MOONCAKE_MASTER_METRICS_PORT.get(),
check_server=envs.MOONCAKE_CHECK_SERVER.get(),
)
@staticmethod

View File

@@ -576,7 +576,7 @@ class ModelRunner:
self.remote_instance_transfer_engine = TransferEngine()
local_ip = get_local_ip_auto()
self.remote_instance_transfer_engine.initialize(
local_ip, "P2PHANDSHAKE", "rdma", envs.MOONCAKE_DEVICE.value
local_ip, "P2PHANDSHAKE", "rdma", envs.MOONCAKE_DEVICE.get()
)
self.remote_instance_transfer_engine_session_id = (
f"{local_ip}:{self.remote_instance_transfer_engine.get_rpc_port()}"

View File

@@ -270,7 +270,7 @@ def _initialize_model(
# Only add sparse head kwargs if envs.SGLANG_EMBEDDINGS_SPARSE_HEAD.is_set()
if envs.SGLANG_EMBEDDINGS_SPARSE_HEAD.is_set():
kwargs["sparse_head"] = envs.SGLANG_EMBEDDINGS_SPARSE_HEAD.value
kwargs["sparse_head"] = envs.SGLANG_EMBEDDINGS_SPARSE_HEAD.get()
kwargs["model_path"] = model_config.model_path
return model_class(**kwargs)

View File

@@ -128,5 +128,5 @@ def import_model_classes(package_name: str, strict: bool = False):
ModelRegistry = _ModelRegistry()
ModelRegistry.register("sglang.srt.models")
if envs.SGLANG_EXTERNAL_MODEL_PACKAGE.value:
ModelRegistry.register(envs.SGLANG_EXTERNAL_MODEL_PACKAGE.value, overwrite=True)
if external_pkg := envs.SGLANG_EXTERNAL_MODEL_PACKAGE.get():
ModelRegistry.register(external_pkg, overwrite=True)

View File

@@ -3718,9 +3718,9 @@ def reserve_rope_cache_for_long_sequences(
"""Pre-expand RoPE cache for long sequences and speculative decoding."""
from sglang.srt.environ import envs
SAFETY_FACTOR = envs.SGLANG_SPEC_EXPANSION_SAFETY_FACTOR.value
MARGIN = envs.SGLANG_ROPE_CACHE_SAFETY_MARGIN.value
ALIGN = envs.SGLANG_ROPE_CACHE_ALIGN.value
SAFETY_FACTOR = envs.SGLANG_SPEC_EXPANSION_SAFETY_FACTOR.get()
MARGIN = envs.SGLANG_ROPE_CACHE_SAFETY_MARGIN.get()
ALIGN = envs.SGLANG_ROPE_CACHE_ALIGN.get()
# 1) Estimate base context upper bound
base_ctx = (