diff --git a/python/sglang/srt/configs/model_config.py b/python/sglang/srt/configs/model_config.py index c85062666..26bb2b6fc 100644 --- a/python/sglang/srt/configs/model_config.py +++ b/python/sglang/srt/configs/model_config.py @@ -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]): diff --git a/python/sglang/srt/environ.py b/python/sglang/srt/environ.py index c20d8602b..db7f667dd 100644 --- a/python/sglang/srt/environ.py +++ b/python/sglang/srt/environ.py @@ -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() diff --git a/python/sglang/srt/layers/logits_processor.py b/python/sglang/srt/layers/logits_processor.py index f0d1aabbb..f258e4890 100644 --- a/python/sglang/srt/layers/logits_processor.py +++ b/python/sglang/srt/layers/logits_processor.py @@ -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, diff --git a/python/sglang/srt/layers/rotary_embedding.py b/python/sglang/srt/layers/rotary_embedding.py index 0385c4466..1f17cf3a8 100644 --- a/python/sglang/srt/layers/rotary_embedding.py +++ b/python/sglang/srt/layers/rotary_embedding.py @@ -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 diff --git a/python/sglang/srt/managers/tokenizer_manager.py b/python/sglang/srt/managers/tokenizer_manager.py index 32fb8a1f5..77e44106f 100644 --- a/python/sglang/srt/managers/tokenizer_manager.py +++ b/python/sglang/srt/managers/tokenizer_manager.py @@ -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) diff --git a/python/sglang/srt/mem_cache/storage/mooncake_store/mooncake_store.py b/python/sglang/srt/mem_cache/storage/mooncake_store/mooncake_store.py index f6101229e..acc472369 100644 --- a/python/sglang/srt/mem_cache/storage/mooncake_store/mooncake_store.py +++ b/python/sglang/srt/mem_cache/storage/mooncake_store/mooncake_store.py @@ -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 diff --git a/python/sglang/srt/model_executor/model_runner.py b/python/sglang/srt/model_executor/model_runner.py index a1614e06b..861509d9a 100644 --- a/python/sglang/srt/model_executor/model_runner.py +++ b/python/sglang/srt/model_executor/model_runner.py @@ -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()}" diff --git a/python/sglang/srt/model_loader/loader.py b/python/sglang/srt/model_loader/loader.py index a40e47ab4..697af0b55 100644 --- a/python/sglang/srt/model_loader/loader.py +++ b/python/sglang/srt/model_loader/loader.py @@ -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) diff --git a/python/sglang/srt/models/registry.py b/python/sglang/srt/models/registry.py index 989b01f7a..066be3dc4 100644 --- a/python/sglang/srt/models/registry.py +++ b/python/sglang/srt/models/registry.py @@ -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) diff --git a/python/sglang/srt/utils/common.py b/python/sglang/srt/utils/common.py index 47d542e8a..2e1986ba8 100644 --- a/python/sglang/srt/utils/common.py +++ b/python/sglang/srt/utils/common.py @@ -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 = ( diff --git a/test/srt/test_external_models.py b/test/srt/test_external_models.py index cab5b785a..49fd00391 100644 --- a/test/srt/test_external_models.py +++ b/test/srt/test_external_models.py @@ -1,14 +1,14 @@ -import os import unittest import sglang as sgl +from sglang.srt.environ import envs from sglang.test.test_utils import CustomTestCase class TestExternalModels(CustomTestCase): def test_external_model(self): - os.environ["SGLANG_EXTERNAL_MODEL_PACKAGE"] = "external_models" - os.environ["SGLANG_EXTERNAL_MM_PROCESSOR_PACKAGE"] = "external_models" + envs.SGLANG_EXTERNAL_MODEL_PACKAGE.set("external_models") + envs.SGLANG_EXTERNAL_MM_PROCESSOR_PACKAGE.set("external_models") prompt = "Today is a sunny day and I like" model_path = "Qwen/Qwen2-VL-2B-Instruct"