-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
[Bugfix] Fix Qwen2 audio chat template for old version transformers compatibility #20826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
from .registry import get_chat_template_fallback_path | ||
from .registry import (get_chat_template_fallback, | ||
get_chat_template_fallback_path) | ||
|
||
__all__ = ["get_chat_template_fallback_path"] | ||
__all__ = ["get_chat_template_fallback", "get_chat_template_fallback_path"] |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,6 @@ | ||||||||||||||||||||||||||||
# SPDX-License-Identifier: Apache-2.0 | ||||||||||||||||||||||||||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||||||||||||||||||||||||||||
from dataclasses import dataclass | ||||||||||||||||||||||||||||
from pathlib import Path | ||||||||||||||||||||||||||||
from typing import Callable, Optional, Union | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -20,41 +21,64 @@ def _get_qwen_chat_template_fallback( | |||||||||||||||||||||||||||
return CHAT_TEMPLATES_DIR / "template_basic.jinja" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
@dataclass(frozen=True) | ||||||||||||||||||||||||||||
class TemplateInfo: | ||||||||||||||||||||||||||||
path: ChatTemplatePath | ||||||||||||||||||||||||||||
override_exists: bool = False | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def get_path(self, tokenizer_name_or_path: str) -> Optional[Path]: | ||||||||||||||||||||||||||||
if callable(self.path): | ||||||||||||||||||||||||||||
return self.path(tokenizer_name_or_path) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return self.path | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# yapf: disable | ||||||||||||||||||||||||||||
_MODEL_TYPE_TO_CHAT_TEMPLATE_FALLBACK: dict[str, ChatTemplatePath] = { | ||||||||||||||||||||||||||||
"blip-2": CHAT_TEMPLATES_DIR / "template_blip2.jinja", | ||||||||||||||||||||||||||||
"chameleon": CHAT_TEMPLATES_DIR / "template_basic.jinja", | ||||||||||||||||||||||||||||
"deepseek_vl_v2": CHAT_TEMPLATES_DIR / "template_deepseek_vl2.jinja", | ||||||||||||||||||||||||||||
"florence2": CHAT_TEMPLATES_DIR / "template_basic.jinja", | ||||||||||||||||||||||||||||
"fuyu": CHAT_TEMPLATES_DIR / "template_fuyu.jinja", | ||||||||||||||||||||||||||||
"paligemma": CHAT_TEMPLATES_DIR / "template_basic.jinja", | ||||||||||||||||||||||||||||
"qwen": _get_qwen_chat_template_fallback, | ||||||||||||||||||||||||||||
_MODEL_TYPE_TO_CHAT_TEMPLATE_FALLBACK: dict[str, TemplateInfo] = { | ||||||||||||||||||||||||||||
"blip-2": TemplateInfo(CHAT_TEMPLATES_DIR / "template_blip2.jinja"), | ||||||||||||||||||||||||||||
"chameleon": TemplateInfo(CHAT_TEMPLATES_DIR / "template_basic.jinja"), | ||||||||||||||||||||||||||||
"deepseek_vl_v2": TemplateInfo(CHAT_TEMPLATES_DIR / "template_deepseek_vl2.jinja"), # noqa: E501 | ||||||||||||||||||||||||||||
"florence2": TemplateInfo(CHAT_TEMPLATES_DIR / "template_basic.jinja"), | ||||||||||||||||||||||||||||
"fuyu": TemplateInfo(CHAT_TEMPLATES_DIR / "template_fuyu.jinja"), | ||||||||||||||||||||||||||||
"paligemma": TemplateInfo(CHAT_TEMPLATES_DIR / "template_basic.jinja"), | ||||||||||||||||||||||||||||
"qwen": TemplateInfo(_get_qwen_chat_template_fallback), | ||||||||||||||||||||||||||||
"qwen2_audio": TemplateInfo(CHAT_TEMPLATES_DIR / "template_qwen2_audio.jinja", # noqa: E501 | ||||||||||||||||||||||||||||
override_exists=True), | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
# yapf: enable | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def register_chat_template_fallback_path( | ||||||||||||||||||||||||||||
model_type: str, | ||||||||||||||||||||||||||||
chat_template: ChatTemplatePath, | ||||||||||||||||||||||||||||
override_exists: bool = False, | ||||||||||||||||||||||||||||
) -> None: | ||||||||||||||||||||||||||||
if model_type in _MODEL_TYPE_TO_CHAT_TEMPLATE_FALLBACK: | ||||||||||||||||||||||||||||
logger.warning( | ||||||||||||||||||||||||||||
"Model type %s already has a chat template registered. " | ||||||||||||||||||||||||||||
"It will be overwritten by the new chat template %s.", model_type, | ||||||||||||||||||||||||||||
chat_template) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
_MODEL_TYPE_TO_CHAT_TEMPLATE_FALLBACK[model_type] = chat_template | ||||||||||||||||||||||||||||
_MODEL_TYPE_TO_CHAT_TEMPLATE_FALLBACK[model_type] = TemplateInfo( | ||||||||||||||||||||||||||||
chat_template, override_exists=override_exists) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def get_chat_template_fallback(model_type: str) -> Optional[TemplateInfo]: | ||||||||||||||||||||||||||||
chat_template_info = _MODEL_TYPE_TO_CHAT_TEMPLATE_FALLBACK.get(model_type) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if chat_template_info is None: | ||||||||||||||||||||||||||||
return None | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return chat_template_info | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def get_chat_template_fallback_path( | ||||||||||||||||||||||||||||
model_type: str, | ||||||||||||||||||||||||||||
tokenizer_name_or_path: str, | ||||||||||||||||||||||||||||
) -> Optional[Path]: | ||||||||||||||||||||||||||||
chat_template = _MODEL_TYPE_TO_CHAT_TEMPLATE_FALLBACK.get(model_type) | ||||||||||||||||||||||||||||
if callable(chat_template): | ||||||||||||||||||||||||||||
chat_template = chat_template(tokenizer_name_or_path) | ||||||||||||||||||||||||||||
chat_template_info = get_chat_template_fallback(model_type) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if chat_template is None: | ||||||||||||||||||||||||||||
if chat_template_info is None: | ||||||||||||||||||||||||||||
return None | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return chat_template | ||||||||||||||||||||||||||||
return chat_template_info.get_path(tokenizer_name_or_path) | ||||||||||||||||||||||||||||
Comment on lines
+79
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider simplifying this function by directly returning the result of
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{%- set audio_count = namespace(value=0) -%} | ||
{%- for message in messages -%} | ||
{%- if loop.first and message['role'] != 'system' -%} | ||
{{ '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n' }} | ||
{%- endif -%} | ||
{{ '<|im_start|>' + message['role'] + '\n' }} | ||
{%- if message['content'] is string -%} | ||
{{ message['content'] + '<|im_end|>\n' }} | ||
{%- else -%} | ||
{%- for content in message['content'] -%} | ||
{%- if 'audio' in content or 'audio_url' in content or message['type'] == 'audio' or content['type'] == 'audio' -%} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition
|
||
{%- set audio_count.value = audio_count.value + 1 -%} | ||
{{ 'Audio ' + audio_count.value|string + ': <|audio_bos|><|AUDIO|><|audio_eos|>\n' }} | ||
{%- elif 'text' in content -%} | ||
{{ content['text'] }} | ||
{%- endif -%} | ||
{%- endfor -%} | ||
{{ '<|im_end|>\n' }} | ||
{%- endif -%} | ||
{%- endfor -%} | ||
|
||
{%- if add_generation_prompt -%} | ||
{{ '<|im_start|>assistant\n' }} | ||
{%- endif -%} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent a potential
TypeError
, ensurepath
is notNone
before callingload_chat_template(path)
. Iffallback_info.get_path()
returnsNone
,load_chat_template(None)
would raise an error.