[diffusion] feat: Add --model-id for config resolution; deprecate model_detectors (#19607)

This commit is contained in:
Mick
2026-03-02 16:39:53 +08:00
committed by GitHub
parent f2c5503542
commit 2e15c015c0
7 changed files with 115 additions and 15 deletions
+30 -11
View File
@@ -245,10 +245,29 @@ def get_model_short_name(model_id: str) -> str:
return model_id
def _get_config_info(model_path: str) -> Optional[ConfigInfo]:
@lru_cache(maxsize=1)
def _get_config_info(
model_path: str, model_id: Optional[str] = None
) -> Optional[ConfigInfo]:
"""
Gets the ConfigInfo for a given model path using mappings and detectors.
"""
all_model_hf_paths = sorted(_MODEL_HF_PATH_TO_NAME.keys(), key=len, reverse=True)
# 0. Explicit model_id override: match by short name
if model_id is not None:
model_id_lower = model_id.lower()
for registered_hf_id in all_model_hf_paths:
if get_model_short_name(registered_hf_id).lower() == model_id_lower:
logger.debug(
f"Resolved model via explicit --model-id '{model_id}''{registered_hf_id}'."
)
return _CONFIG_REGISTRY.get(_MODEL_HF_PATH_TO_NAME[registered_hf_id])
logger.warning(
f"--model-id '{model_id}' did not match any registered model; "
"falling back to automatic detection."
)
# 1. Exact match
if model_path in _MODEL_HF_PATH_TO_NAME:
model_id = _MODEL_HF_PATH_TO_NAME[model_path]
@@ -256,12 +275,11 @@ def _get_config_info(model_path: str) -> Optional[ConfigInfo]:
return _CONFIG_REGISTRY.get(model_id)
# 2. Partial match: find the best (longest) match against all registered model hf paths.
model_name = get_model_short_name(model_path.lower())
all_model_hf_paths = sorted(_MODEL_HF_PATH_TO_NAME.keys(), key=len, reverse=True)
model_short_name = get_model_short_name(model_path.lower())
for registered_model_hf_id in all_model_hf_paths:
registered_model_name = get_model_short_name(registered_model_hf_id.lower())
if registered_model_name == model_name:
if registered_model_name == model_short_name:
logger.debug(
f"Resolved model name '{registered_model_hf_id}' from partial path match."
)
@@ -310,7 +328,7 @@ class ModelInfo:
pipeline_config_cls: Type[PipelineConfig]
def _get_diffusers_model_info(model_path: str) -> ModelInfo:
def _get_diffusers_model_info() -> ModelInfo:
"""
Get model info for diffusers backend.
@@ -337,6 +355,7 @@ def _get_diffusers_model_info(model_path: str) -> ModelInfo:
def get_model_info(
model_path: str,
backend: Optional[Union[str, "Backend"]] = None,
model_id: Optional[str] = None,
) -> Optional[ModelInfo]:
"""
Resolves all necessary classes (pipeline, sampling, config) for a given model path.
@@ -365,7 +384,7 @@ def get_model_info(
logger.info(
"Using diffusers backend for model '%s' (explicitly requested)", model_path
)
return _get_diffusers_model_info(model_path)
return _get_diffusers_model_info()
# For AUTO or SGLANG backend, try native implementation first
# 1. Discover all available pipeline classes and cache them
@@ -389,7 +408,7 @@ def get_model_info(
logger.error(f"Could not read model config for '{model_path}': {e}")
if backend == Backend.AUTO:
logger.info("Falling back to diffusers backend")
return _get_diffusers_model_info(model_path)
return _get_diffusers_model_info()
return None
pipeline_class_name = config.get("_class_name")
@@ -399,7 +418,7 @@ def get_model_info(
)
if backend == Backend.AUTO:
logger.info("Falling back to diffusers backend")
return _get_diffusers_model_info(model_path)
return _get_diffusers_model_info()
return None
pipeline_cls = _PIPELINE_REGISTRY.get(pipeline_class_name)
@@ -409,7 +428,7 @@ def get_model_info(
f"Pipeline class '{pipeline_class_name}' specified in '{model_path}' has no native sglang support. "
f"Falling back to diffusers backend."
)
return _get_diffusers_model_info(model_path)
return _get_diffusers_model_info()
else:
logger.error(
f"Pipeline class '{pipeline_class_name}' specified in '{model_path}' is not a registered EntryClass in the framework. "
@@ -419,14 +438,14 @@ def get_model_info(
return None
# 3. Get configuration classes (sampling, pipeline config)
config_info = _get_config_info(model_path)
config_info = _get_config_info(model_path, model_id=model_id)
if not config_info:
if backend == Backend.AUTO:
logger.warning(
f"Could not resolve native configuration for model '{model_path}'. "
f"Falling back to diffusers backend."
)
return _get_diffusers_model_info(model_path)
return _get_diffusers_model_info()
else:
logger.error(
f"Could not resolve configuration for model '{model_path}'. "