Files
vllm-omni/vllm_omni/model_extras/registry.py
T
2026-06-22 15:17:00 +08:00

168 lines
5.5 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from __future__ import annotations
from collections.abc import Callable
from typing import Any
from PIL import Image
from vllm_omni.model_extras.bagel import (
BAGEL_EXTRA_BODY_PARAMS,
BAGEL_EXTRA_OUTPUT_PARAMS,
BAGEL_INIT_EXTRA_ARGS_FOR_NON_DIFFUSION_STAGES,
)
from vllm_omni.model_extras.bagel import (
build_image_to_image_prompt as build_bagel_image_to_image_prompt,
)
from vllm_omni.model_extras.bagel import (
build_text_to_image_prompt as build_bagel_text_to_image_prompt,
)
from vllm_omni.model_extras.helios import (
HELIOS_EXTRA_BODY_PARAMS,
HELIOS_EXTRA_OUTPUT_PARAMS,
)
from vllm_omni.model_extras.magi_human import (
MAGI_HUMAN_EXTRA_BODY_PARAMS,
MAGI_HUMAN_EXTRA_OUTPUT_PARAMS,
)
from vllm_omni.model_extras.sensenova_u1 import (
SENSENOVA_U1_EXTRA_BODY_PARAMS,
SENSENOVA_U1_EXTRA_OUTPUT_PARAMS,
)
# TODO: extend with TextToVideoPromptBuilder, ImageToVideoPromptBuilder
# once a video model needs model-specific prompt construction (follow-up PR).
TextToImagePromptBuilder = Callable[
[str, str | None, int | None, int | None],
dict[str, Any],
]
ImageToImagePromptBuilder = Callable[
[str, str | None, "Image.Image | list[Image.Image]", int | None, int | None],
dict[str, Any],
]
def default_text_to_image_prompt(
prompt: str,
negative_prompt: str | None,
height: int | None = None,
width: int | None = None,
) -> dict[str, Any]:
result: dict[str, Any] = {"prompt": prompt}
if negative_prompt is not None:
result["negative_prompt"] = negative_prompt
return result
def default_image_to_image_prompt(
prompt: str,
negative_prompt: str | None,
input_image: Image.Image | list[Image.Image],
height: int | None = None,
width: int | None = None,
) -> dict[str, Any]:
result: dict[str, Any] = {
"prompt": prompt,
"multi_modal_data": {"image": input_image},
}
if negative_prompt is not None:
result["negative_prompt"] = negative_prompt
return result
_EXTRA_SPECS: dict[str, dict[str, Any]] = {
"BagelPipeline": {
"extra_body_params": BAGEL_EXTRA_BODY_PARAMS,
"extra_output_params": BAGEL_EXTRA_OUTPUT_PARAMS,
"init_extra_args_for_non_diffusion_stages": BAGEL_INIT_EXTRA_ARGS_FOR_NON_DIFFUSION_STAGES,
"text_to_image_prompt_builder": build_bagel_text_to_image_prompt,
"image_to_image_prompt_builder": build_bagel_image_to_image_prompt,
},
"SenseNovaU1Pipeline": {
"extra_body_params": SENSENOVA_U1_EXTRA_BODY_PARAMS,
"extra_output_params": SENSENOVA_U1_EXTRA_OUTPUT_PARAMS,
},
"MagiHumanPipeline": {
"extra_body_params": MAGI_HUMAN_EXTRA_BODY_PARAMS,
"extra_output_params": MAGI_HUMAN_EXTRA_OUTPUT_PARAMS,
},
"HeliosPipeline": {
"extra_body_params": HELIOS_EXTRA_BODY_PARAMS,
"extra_output_params": HELIOS_EXTRA_OUTPUT_PARAMS,
},
"HeliosPyramidPipeline": {
"extra_body_params": HELIOS_EXTRA_BODY_PARAMS,
"extra_output_params": HELIOS_EXTRA_OUTPUT_PARAMS,
},
}
def _get_spec(model_class_name: str | None) -> dict[str, Any] | None:
if not model_class_name:
return None
return _EXTRA_SPECS.get(model_class_name)
def get_model_class_name(omni: Any) -> str | None:
"""Extract model_class_name from an Omni/AsyncOmni instance.
This hides the internal ODConfig plumbing from example scripts.
"""
engine = getattr(omni, "engine", None)
if engine is None:
return None
od_config = getattr(engine, "od_config", None)
if od_config is None and hasattr(engine, "get_diffusion_od_config"):
od_config = engine.get_diffusion_od_config()
return getattr(od_config, "model_class_name", None) if od_config else None
def get_extra_body_params(model_class_name: str | None) -> frozenset[str]:
spec = _get_spec(model_class_name)
return spec.get("extra_body_params", frozenset()) if spec is not None else frozenset()
def get_extra_output_params(model_class_name: str | None) -> frozenset[str]:
spec = _get_spec(model_class_name)
return spec.get("extra_output_params", frozenset()) if spec is not None else frozenset()
def should_init_extra_args_for_non_diffusion_stages(model_class_name: str | None) -> bool:
spec = _get_spec(model_class_name)
return bool(spec and spec.get("init_extra_args_for_non_diffusion_stages", False))
def build_text_to_image_prompt(
model_class_name: str | None,
prompt: str,
negative_prompt: str | None,
height: int | None = None,
width: int | None = None,
) -> dict[str, Any]:
spec = _get_spec(model_class_name)
builder: TextToImagePromptBuilder = (
spec.get("text_to_image_prompt_builder", default_text_to_image_prompt)
if spec is not None
else default_text_to_image_prompt
)
return builder(prompt, negative_prompt, height, width)
def build_image_to_image_prompt(
model_class_name: str | None,
prompt: str,
negative_prompt: str | None,
input_image: Image.Image | list[Image.Image],
height: int | None = None,
width: int | None = None,
) -> dict[str, Any]:
spec = _get_spec(model_class_name)
builder: ImageToImagePromptBuilder = (
spec.get("image_to_image_prompt_builder", default_image_to_image_prompt)
if spec is not None
else default_image_to_image_prompt
)
return builder(prompt, negative_prompt, input_image, height, width)