[Refactor]: Intrduce x_to_text.py in examples (#5384)
Signed-off-by: princepride <wangzhipeng628@gmail.com> Signed-off-by: 汪志鹏 <wangzhipeng628@gmail.com>
This commit is contained in:
12 files changed
+457
-9
No files matched your search
@@ -5,9 +5,11 @@ from vllm_omni.model_extras.registry import (
|
||||
build_image_to_image_prompt,
|
||||
build_image_to_video_prompt,
|
||||
build_text_to_image_prompt,
|
||||
build_x_to_text_prompt,
|
||||
get_extra_body_params,
|
||||
get_extra_output_params,
|
||||
get_model_class_name,
|
||||
get_x_to_text_model_family,
|
||||
should_init_extra_args_for_non_diffusion_stages,
|
||||
)
|
||||
|
||||
@@ -15,8 +17,10 @@ __all__ = [
|
||||
"build_image_to_image_prompt",
|
||||
"build_image_to_video_prompt",
|
||||
"build_text_to_image_prompt",
|
||||
"build_x_to_text_prompt",
|
||||
"get_extra_body_params",
|
||||
"get_extra_output_params",
|
||||
"get_model_class_name",
|
||||
"get_x_to_text_model_family",
|
||||
"should_init_extra_args_for_non_diffusion_stages",
|
||||
]
|
||||
@@ -50,6 +50,23 @@ def build_image_to_image_prompt(
|
||||
return img_prompt
|
||||
|
||||
|
||||
def build_x_to_text_prompt(
|
||||
model: str,
|
||||
prompt: str,
|
||||
has_image: bool,
|
||||
) -> tuple[dict[str, Any], list[int] | None]:
|
||||
"""Build BAGEL's chat prompt for text-output tasks."""
|
||||
del model
|
||||
image_token = "<|image_pad|>\n" if has_image else ""
|
||||
return (
|
||||
{
|
||||
"prompt": f"<|im_start|>user\n{image_token}{prompt}<|im_end|>\n<|im_start|>assistant\n",
|
||||
"modalities": ["text"],
|
||||
},
|
||||
None,
|
||||
)
|
||||
|
||||
|
||||
BAGEL_EXTRA_BODY_PARAMS = frozenset(
|
||||
{
|
||||
"cfg_text_scale",
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
def build_x_to_text_prompt(
|
||||
model: str,
|
||||
prompt: str,
|
||||
has_image: bool,
|
||||
) -> tuple[dict[str, Any], list[int] | None]:
|
||||
"""Build HunyuanImage-3 prompt tokens and stopping rules for T2T/I2T."""
|
||||
from transformers import AutoTokenizer
|
||||
|
||||
from vllm_omni.diffusion.models.hunyuan_image3.prompt_utils import (
|
||||
build_prompt_tokens,
|
||||
resolve_stop_token_ids,
|
||||
resolve_sys_type,
|
||||
)
|
||||
|
||||
task = "i2t" if has_image else "t2t"
|
||||
tokenizer = AutoTokenizer.from_pretrained(model, trust_remote_code=True)
|
||||
build_kwargs: dict[str, Any] = {"task": task, "bot_task": None}
|
||||
if has_image:
|
||||
build_kwargs["num_images"] = 1
|
||||
built = build_prompt_tokens(prompt, tokenizer, **build_kwargs)
|
||||
return (
|
||||
{
|
||||
"prompt": prompt,
|
||||
"prompt_token_ids": built.token_ids,
|
||||
"modalities": ["text"],
|
||||
"use_system_prompt": resolve_sys_type(None),
|
||||
},
|
||||
resolve_stop_token_ids(task=task, bot_task=None, tokenizer=tokenizer, image_size="auto"),
|
||||
)
|
||||
@@ -66,5 +66,29 @@ MAMMOTHMODA2_PREVIEW_EXTRA_BODY_PARAMS = frozenset(
|
||||
"num_inference_steps",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def build_x_to_text_prompt(
|
||||
model: str,
|
||||
prompt: str,
|
||||
has_image: bool,
|
||||
) -> tuple[dict[str, Any], list[int] | None]:
|
||||
"""Build MammothModa2's chat prompt for text-output tasks."""
|
||||
del model
|
||||
vision = "<|vision_start|><|image_pad|><|vision_end|>" if has_image else ""
|
||||
return (
|
||||
{
|
||||
"prompt": (
|
||||
"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n"
|
||||
f"<|im_start|>user\n{vision}{prompt}<|im_end|>\n"
|
||||
"<|im_start|>assistant\n"
|
||||
),
|
||||
"modalities": ["text"],
|
||||
"additional_information": {"omni_task": ["chat"]},
|
||||
},
|
||||
None,
|
||||
)
|
||||
|
||||
|
||||
MAMMOTHMODA2_PREVIEW_EXTRA_OUTPUT_PARAMS = frozenset()
|
||||
MAMMOTHMODA2_PREVIEW_INIT_EXTRA_ARGS_FOR_NON_DIFFUSION_STAGES = True
|
||||
@@ -23,6 +23,7 @@ from vllm_omni.model_extras.bagel import (
|
||||
from vllm_omni.model_extras.bagel import (
|
||||
build_text_to_image_prompt as build_bagel_text_to_image_prompt,
|
||||
)
|
||||
from vllm_omni.model_extras.bagel import build_x_to_text_prompt as build_bagel_x_to_text_prompt
|
||||
from vllm_omni.model_extras.cosmos3 import (
|
||||
COSMOS3_EXTRA_BODY_PARAMS,
|
||||
COSMOS3_EXTRA_OUTPUT_PARAMS,
|
||||
@@ -34,6 +35,7 @@ from vllm_omni.model_extras.helios import (
|
||||
HELIOS_EXTRA_BODY_PARAMS,
|
||||
HELIOS_EXTRA_OUTPUT_PARAMS,
|
||||
)
|
||||
from vllm_omni.model_extras.hunyuan_image3 import build_x_to_text_prompt as build_hunyuan_x_to_text_prompt
|
||||
from vllm_omni.model_extras.lingbot_video import LINGBOT_VIDEO_EXTRA_BODY_PARAMS
|
||||
from vllm_omni.model_extras.magi_human import (
|
||||
MAGI_HUMAN_EXTRA_BODY_PARAMS,
|
||||
@@ -47,6 +49,9 @@ from vllm_omni.model_extras.mammothmodal2_preview import (
|
||||
from vllm_omni.model_extras.mammothmodal2_preview import (
|
||||
build_text_to_image_prompt as build_mammothmoda2_text_to_image_prompt,
|
||||
)
|
||||
from vllm_omni.model_extras.mammothmodal2_preview import (
|
||||
build_x_to_text_prompt as build_mammothmoda2_x_to_text_prompt,
|
||||
)
|
||||
from vllm_omni.model_extras.ming_flash_omni import (
|
||||
MING_FLASH_OMNI_EXTRA_BODY_PARAMS,
|
||||
MING_FLASH_OMNI_EXTRA_OUTPUT_PARAMS,
|
||||
@@ -89,6 +94,50 @@ ImageToVideoPromptBuilder = Callable[
|
||||
],
|
||||
dict[str, Any],
|
||||
]
|
||||
XToTextPromptBuilder = Callable[[str, str, bool], tuple[dict[str, Any], list[int] | None]]
|
||||
|
||||
|
||||
def default_x_to_text_prompt(
|
||||
model: str,
|
||||
prompt: str,
|
||||
has_image: bool,
|
||||
) -> tuple[dict[str, Any], list[int] | None]:
|
||||
del model, has_image
|
||||
return {"prompt": prompt, "modalities": ["text"]}, None
|
||||
|
||||
|
||||
_X_TO_TEXT_SPECS: dict[str, XToTextPromptBuilder] = {
|
||||
"bagel": build_bagel_x_to_text_prompt,
|
||||
"hunyuan_image3": build_hunyuan_x_to_text_prompt,
|
||||
"mammoth_moda2": build_mammothmoda2_x_to_text_prompt,
|
||||
}
|
||||
|
||||
|
||||
def get_x_to_text_model_family(model: str) -> str:
|
||||
"""Resolve a text-output prompt family from a checkpoint's config.json."""
|
||||
from vllm.transformers_utils.config import get_hf_file_to_dict
|
||||
|
||||
config = get_hf_file_to_dict("config.json", model) or {}
|
||||
model_type = str(config.get("model_type", "")).lower()
|
||||
architectures = {str(value).lower() for value in (config.get("architectures") or [])}
|
||||
if model_type == "bagel" or "bagelforconditionalgeneration" in architectures:
|
||||
return "bagel"
|
||||
if model_type == "hunyuan_image_3_moe" or any("hunyuanimage3" in value for value in architectures):
|
||||
return "hunyuan_image3"
|
||||
if "mammoth" in model_type or any("mammothmoda2" in value for value in architectures):
|
||||
return "mammoth_moda2"
|
||||
return "generic"
|
||||
|
||||
|
||||
def build_x_to_text_prompt(
|
||||
model_family: str,
|
||||
model: str,
|
||||
prompt: str,
|
||||
has_image: bool,
|
||||
) -> tuple[dict[str, Any], list[int] | None]:
|
||||
"""Build a model-aware T2T/I2T prompt and optional stop-token ids."""
|
||||
builder = _X_TO_TEXT_SPECS.get(model_family, default_x_to_text_prompt)
|
||||
return builder(model, prompt, has_image)
|
||||
|
||||
|
||||
def default_text_to_image_prompt(
|
||||
|
||||
Reference in new issue
Block a user