From b0dff4009fa44dc17d26ac82968691b662880aa1 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 7 Oct 2026 14:21:37 +0800 Subject: [PATCH] [diffusion] cli: restore gated Diffusers model detection (#34903) --- python/sglang/cli/utils.py | 10 ++- test/registered/unit/cli/test_utils.py | 86 ++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 test/registered/unit/cli/test_utils.py diff --git a/python/sglang/cli/utils.py b/python/sglang/cli/utils.py index eebaecea02..e51d5c8072 100644 --- a/python/sglang/cli/utils.py +++ b/python/sglang/cli/utils.py @@ -4,6 +4,8 @@ import os import subprocess from functools import lru_cache +from huggingface_hub.errors import GatedRepoError + from sglang.srt.environ import envs from sglang.utils import ( has_diffusion_overlay_registry_match, @@ -69,7 +71,7 @@ def get_is_diffusion_model(model_path: str) -> bool: For registered models, consults the diffusion registry first. For other local directories, checks the filesystem directly. For other HF/ModelScope model IDs, attempts to fetch only model_index.json. - For gated repos where file download fails, falls back to HF model card + For gated HF repos where file download fails, falls back to HF model card metadata (library_name == "diffusers"). Returns False on any failure (network error, 404, offline mode, etc.) so that the caller falls through to the standard LLM server path. @@ -86,8 +88,10 @@ def get_is_diffusion_model(model_path: str) -> bool: if os.path.isdir(model_path): return _is_diffusers_model_dir(model_path) + use_modelscope = envs.SGLANG_USE_MODELSCOPE.get() + try: - if envs.SGLANG_USE_MODELSCOPE.get(): + if use_modelscope: from modelscope import model_file_download file_path = model_file_download( @@ -101,6 +105,8 @@ def get_is_diffusion_model(model_path: str) -> bool: return _is_diffusers_model_dir(os.path.dirname(file_path)) except Exception as e: logger.debug("Failed to auto-detect diffusion model for %s: %s", model_path, e) + if not use_modelscope and isinstance(e, GatedRepoError): + return _is_gated_diffusion_repo(model_path) return False diff --git a/test/registered/unit/cli/test_utils.py b/test/registered/unit/cli/test_utils.py new file mode 100644 index 0000000000..25eae4d137 --- /dev/null +++ b/test/registered/unit/cli/test_utils.py @@ -0,0 +1,86 @@ +# SPDX-License-Identifier: Apache-2.0 + +import unittest +from types import SimpleNamespace +from unittest.mock import patch + +import httpx +from huggingface_hub.errors import ( + GatedRepoError, + RemoteEntryNotFoundError, + RepositoryNotFoundError, +) + +from sglang.cli.utils import get_is_diffusion_model +from sglang.test.ci.ci_register import register_cpu_ci +from sglang.test.test_utils import CustomTestCase + +register_cpu_ci(est_time=5, suite="base-a-test-cpu") + + +def _hub_error(error_type, status_code): + request = httpx.Request("GET", "https://huggingface.co/test/model") + response = httpx.Response(status_code, request=request) + return error_type("test error", response=response) + + +class TestDiffusionModelDetection(CustomTestCase): + def setUp(self): + super().setUp() + for target in ( + "sglang.cli.utils._is_diffusion_model_from_registry", + "sglang.cli.utils._is_overlay_diffusion_model", + "sglang.cli.utils.os.path.isdir", + ): + patcher = patch(target, return_value=False) + patcher.start() + self.addCleanup(patcher.stop) + + modelscope_patcher = patch( + "sglang.cli.utils.envs.SGLANG_USE_MODELSCOPE.get", return_value=False + ) + modelscope_patcher.start() + self.addCleanup(modelscope_patcher.stop) + + @patch("huggingface_hub.HfApi") + @patch("huggingface_hub.hf_hub_download") + def test_gated_diffusion_repo_uses_metadata_fallback( + self, mock_download, mock_hf_api + ): + mock_download.side_effect = _hub_error(GatedRepoError, 401) + mock_hf_api.return_value.model_info.return_value = SimpleNamespace( + library_name="diffusers" + ) + + self.assertTrue(get_is_diffusion_model("test/gated-diffusion-model")) + mock_hf_api.return_value.model_info.assert_called_once_with( + "test/gated-diffusion-model" + ) + + @patch("huggingface_hub.HfApi") + @patch("huggingface_hub.hf_hub_download") + def test_gated_non_diffusion_repo_is_not_detected(self, mock_download, mock_hf_api): + mock_download.side_effect = _hub_error(GatedRepoError, 401) + mock_hf_api.return_value.model_info.return_value = SimpleNamespace( + library_name="transformers" + ) + + self.assertFalse(get_is_diffusion_model("test/gated-llm-model")) + mock_hf_api.return_value.model_info.assert_called_once_with( + "test/gated-llm-model" + ) + + @patch("huggingface_hub.HfApi") + @patch("huggingface_hub.hf_hub_download") + def test_non_gated_hub_errors_do_not_use_metadata_fallback( + self, mock_download, mock_hf_api + ): + for error_type in (RemoteEntryNotFoundError, RepositoryNotFoundError): + with self.subTest(error_type=error_type.__name__): + mock_download.side_effect = _hub_error(error_type, 404) + self.assertFalse(get_is_diffusion_model("test/llm-model")) + mock_hf_api.assert_not_called() + + +if __name__ == "__main__": + unittest.main()