Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions lmdeploy/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
from collections import defaultdict
from typing import Any

from lmdeploy.utils import get_logger

logger = get_logger('lmdeploy')


class DefaultsAndTypesHelpFormatter(argparse.HelpFormatter):
"""Formatter to output default value and type in help information."""
Expand Down Expand Up @@ -68,12 +64,12 @@ def get_lora_adapters(adapters: list[str]):
return output


def get_chat_template(chat_template: str, model_path: str = None):
def get_chat_template(chat_template: str | None, model_path: str | None = None):
"""Get chat template config.

Args:
chat_template(str): it could be a builtin chat template name, or a chat template json file
model_path(str): the model path, used to check deprecated chat template names
chat_template(str | None): it could be a builtin chat template name, or a chat template json file
model_path(str | None): the model path, passed through to the chat template config
"""
import os

Expand All @@ -82,14 +78,7 @@ def get_chat_template(chat_template: str, model_path: str = None):
if os.path.isfile(chat_template):
return ChatTemplateConfig.from_json(chat_template)
else:
from lmdeploy.model import DEPRECATED_CHAT_TEMPLATE_NAMES, MODELS, REMOVED_CHAT_TEMPLATE_NAMES
if chat_template in REMOVED_CHAT_TEMPLATE_NAMES:
raise ValueError(f"The chat template '{chat_template}' has been removed. "
f'Please refer to the latest chat templates in '
f'https://lmdeploy.readthedocs.io/en/latest/advance/chat_template.html')
if chat_template in DEPRECATED_CHAT_TEMPLATE_NAMES:
logger.warning(f"The chat template '{chat_template}' is deprecated and fallback to hf chat template.")
chat_template = 'hf'
from lmdeploy.model import MODELS
assert chat_template in MODELS.module_dict.keys(), \
f"chat template '{chat_template}' is not " \
f'registered. The builtin chat templates are: ' \
Expand Down
28 changes: 28 additions & 0 deletions tests/test_lmdeploy/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import torch
from transformers import AutoConfig

Expand Down Expand Up @@ -87,3 +88,30 @@ def test_get_and_verify_max_len():
assert (_get_and_verify_max_len(config, None) == 32768)
assert (_get_and_verify_max_len(config, 1024) == 1024)
assert (_get_and_verify_max_len(config, 102400) == 102400)


def test_get_chat_template_builtin_name():
"""A builtin chat-template name must resolve without crashing.

Regression for #4533: get_chat_template imported DEPRECATED_CHAT_TEMPLATE_NAMES / REMOVED_CHAT_TEMPLATE_NAMES from
lmdeploy.model, which were removed in #4252, so any non-file --chat-template value raised ImportError before
reaching the registry check.
"""
from lmdeploy.cli.utils import get_chat_template
cfg = get_chat_template('llama2')
assert cfg is not None
assert cfg.model_name == 'llama2'


def test_get_chat_template_unregistered_name():
"""An unregistered name is rejected by the registry check, not by an
ImportError."""
from lmdeploy.cli.utils import get_chat_template
with pytest.raises(AssertionError):
get_chat_template('not-a-registered-template')


def test_get_chat_template_none():
"""Passing no chat template returns None."""
from lmdeploy.cli.utils import get_chat_template
assert get_chat_template(None) is None
Loading