[Example] Migrate HunyuanImage-3.0 to model_extras + shared task examples (#5559)

Signed-off-by: suyanli220 <suyanli220@gmail.com>
Signed-off-by: suyan.li <suyan.li@bytedance.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: suyan.li <suyan.li@bytedance.com>
Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com>
This commit is contained in:
authored and GitHub committed 2026-09-11 16:47:25 +08:00
1 parent 96360ccb00
commit a7ab91fb85
17 files changed
+1627 -544

No files matched your search

@@ -24,6 +24,8 @@ from vllm_omni.model_extras import (
build_text_to_image_prompt as build_model_text_to_image_prompt,
)
from vllm_omni.model_extras import (
get_ar_input_builder,
get_ar_tokenizer_validator,
get_extra_body_params,
get_model_class_name,
should_init_extra_args_for_non_diffusion_stages,
@@ -377,10 +379,111 @@ def parse_args() -> argparse.Namespace:
default=None,
help="Supplementary auxiliary text encoder parameters model name or path (especially for Hidream-l1-full).",
)
parser.add_argument(
"--trust-remote-code",
action="store_true",
help="Trust and execute custom modeling code from the model repo (required by e.g. HunyuanImage-3.0).",
)
parser.add_argument(
"--allow-tokenizer-fallback",
action="store_true",
help=(
"For models with a declared ar_input_builder (e.g. HunyuanImage-3.0): if loading "
"the AR tokenizer fails, degrade to the string-prompt form (no BPE parity) instead "
"of failing the run. Off by default -- a tokenizer load failure usually means a "
"missing --trust-remote-code or a network/cache issue that's worth surfacing, not "
"silently masking with a possibly-wrong prompt."
),
)
current_omni_platform.pre_register_and_update(parser)
return parser.parse_args()
def _apply_ar_stage_inputs(
ar_input_builder: Any,
*,
model: str,
prompt_text: str,
extra_body: dict[str, Any],
num_images: int,
height: int | None,
width: int | None,
prompt_dict: dict[str, Any],
sampling_params_list: list[Any],
text_output: bool = False,
trust_remote_code: bool = False,
validate_tokenizer: Any | None = None,
allow_tokenizer_fallback: bool = False,
) -> None:
"""Apply a model's declared AR-stage inputs to the request in place.
Loads the model tokenizer (for byte-for-byte HF-parity segment
tokenization) and asks the model's ``ar_input_builder`` for the AR
prefill + stop tokens, then writes them onto ``prompt_dict`` and the
non-diffusion (AR) stage sampling params. Kept model-agnostic: the
example only knows the declared-hook contract.
``trust_remote_code`` must be threaded in from the caller's own resolved
``--trust-remote-code`` flag -- this is a separate tokenizer load outside
the main engine, so it needs the same explicit user opt-in rather than
defaulting to trusting whatever ``--model`` was passed.
``validate_tokenizer``, when the model declares one via
``get_ar_tokenizer_validator``, is called on a successfully-loaded real
tokenizer -- outside the load's try/except, so a validation failure (a
model/tokenizer revision drifting from hardcoded special-token ids)
raises instead of being swallowed into the string-prompt fallback.
``allow_tokenizer_fallback`` (default ``False``, i.e. fail fast): a
tokenizer load failure normally raises, since silently degrading to the
string-prompt form can produce a request that completes with subtly
wrong stop tokens instead of surfacing the real problem (missing
``--trust-remote-code``, network/cache issue, etc.). Set ``True`` only
for explicit unit tests or a deliberate offline-compat run where the
caller has already decided a degraded prompt is acceptable.
"""
try:
from transformers import AutoTokenizer
ar_tokenizer: Any | None = AutoTokenizer.from_pretrained(model, trust_remote_code=trust_remote_code)
except Exception as exc: # noqa: BLE001 - re-raised unless the caller opts into the fallback
if not allow_tokenizer_fallback:
raise
logger.warning(f"AR tokenizer load failed ({exc}); falling back to string prompt (no BPE parity).")
ar_tokenizer = None
if ar_tokenizer is not None and validate_tokenizer is not None:
validate_tokenizer(ar_tokenizer)
ar_inputs = ar_input_builder(
prompt=prompt_text,
tokenizer=ar_tokenizer,
extra_body=extra_body,
num_images=num_images,
height=height,
width=width,
text_output=text_output,
)
if ar_inputs.prompt_token_ids is not None:
prompt_dict["prompt_token_ids"] = ar_inputs.prompt_token_ids
elif ar_inputs.prompt is not None:
prompt_dict["prompt"] = ar_inputs.prompt
if ar_inputs.use_system_prompt:
prompt_dict["use_system_prompt"] = ar_inputs.use_system_prompt
prompt_dict["modalities"] = ar_inputs.modalities
# Apply stop_token_ids to exactly the stage(s) the builder names -- not
# "whichever stage isn't a diffusion stage," which would misfire on any
# future topology with more than one non-diffusion stage.
for stage_index in ar_inputs.stage_indices:
if stage_index >= len(sampling_params_list):
continue
stage_params = sampling_params_list[stage_index]
if not isinstance(stage_params, OmniDiffusionSamplingParams) and hasattr(stage_params, "stop_token_ids"):
stage_params.stop_token_ids = ar_inputs.stop_token_ids
def main():
args = parse_args()
generator = torch.Generator(device=current_omni_platform.device_type).manual_seed(args.seed)
@@ -469,6 +572,8 @@ def main():
omni_kwargs["tensor_parallel_size"] = args.tensor_parallel_size
if args.enforce_eager is not None:
omni_kwargs["enforce_eager"] = args.enforce_eager
if args.trust_remote_code:
omni_kwargs["trust_remote_code"] = True
if args.deploy_config:
omni_kwargs["deploy_config"] = args.deploy_config
if use_nextstep:
@@ -613,6 +718,27 @@ def main():
if not diffusion_replaced and len(sampling_params_list) == 1:
sampling_params_list = [diffusion_params]
# Models with an AR text stage (e.g. HunyuanImage3) declare an
# ar_input_builder. When present, build the AR prefill token-ids and AR
# stop-token-ids declaratively from the plain prompt + extra_body, so this
# example stays model-agnostic. Models without one are untouched.
ar_input_builder = get_ar_input_builder(model_class_name)
if ar_input_builder is not None:
_apply_ar_stage_inputs(
ar_input_builder,
model=args.model,
prompt_text=args.prompt,
extra_body=user_extra,
num_images=0,
height=args.height,
width=args.width,
prompt_dict=prompt_dict,
sampling_params_list=sampling_params_list,
trust_remote_code=args.trust_remote_code,
validate_tokenizer=get_ar_tokenizer_validator(model_class_name),
allow_tokenizer_fallback=args.allow_tokenizer_fallback,
)
outputs = omni.generate(prompt_dict, sampling_params_list=sampling_params_list)
generation_end = time.perf_counter()