Skip to content
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

Add Llama2-Chinese model support #2218

Merged
merged 1 commit into from
Aug 15, 2023
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
1 change: 1 addition & 0 deletions docs/model_support.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
loading multiple peft models, you can have them share the base model weights by
setting the environment variable `PEFT_SHARE_BASE_WEIGHTS=true` in any model
worker.
- [FlagAlpha/Llama2-Chinese-13b-Chat](https://huggingface.co/FlagAlpha/Llama2-Chinese-13b-Chat)

## How to support a new model

Expand Down
15 changes: 15 additions & 0 deletions fastchat/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,21 @@ def get_conv_template(name: str) -> Conversation:
)
)

# Llama2-Chinese default template
# source: https://huggingface.co/FlagAlpha
register_conv_template(
Conversation(
name="llama2-chinese",
system_message="<s>{system_message}</s>",
roles=("Human", "Assistant", "System"),
messages=(),
offset=0,
sep_style=SeparatorStyle.ADD_COLON_TWO,
sep="\n",
sep2="\n</s><s>",
stop_str="</s>",
)
)

if __name__ == "__main__":
print("Vicuna template:")
Expand Down
26 changes: 26 additions & 0 deletions fastchat/model/model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,31 @@ def get_default_conv_template(self, model_path: str) -> Conversation:
return get_conv_template("aquila-chat")


class Lamma2ChineseAdapter(BaseModelAdapter):
"""The model adapter for FlagAlpha/LLama2-Chinese sft"""

def match(self, model_path: str):
return "llama2-chinese" in model_path.lower()

def load_model(self, model_path: str, from_pretrained_kwargs: dict):
revision = from_pretrained_kwargs.get("revision", "main")
tokenizer = AutoTokenizer.from_pretrained(
model_path,
trust_remote_code=True,
revision=revision,
)
model = AutoModelForCausalLM.from_pretrained(
model_path,
trust_remote_code=True,
low_cpu_mem_usage=True,
**from_pretrained_kwargs,
)
return model, tokenizer

def get_default_conv_template(self, model_path: str) -> Conversation:
return get_conv_template("llama2-chinese")


# Note: the registration order matters.
# The one registered earlier has a higher matching priority.
register_model_adapter(PeftModelAdapter)
Expand Down Expand Up @@ -1432,6 +1457,7 @@ def get_default_conv_template(self, model_path: str) -> Conversation:
register_model_adapter(QwenChatAdapter)
register_model_adapter(AquilaChatAdapter)
register_model_adapter(BGEAdapter)
register_model_adapter(Lamma2ChineseAdapter)

# After all adapters, try the default base adapter.
register_model_adapter(BaseModelAdapter)
6 changes: 6 additions & 0 deletions fastchat/model/model_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,9 @@ def get_model_info(name: str) -> ModelInfo:
"https://huggingface.co/Qwen/Qwen-7B-Chat",
"Qwen is a multi-language large-scale language model (LLM), developed by Damo Academy.",
)
register_model_info(
["Llama2-Chinese-13b-Chat", "LLama2-Chinese-13B"],
"Llama2-Chinese",
"https://huggingface.co/FlagAlpha/Llama2-Chinese-13b-Chat",
"Llama2-Chinese is a multi-language large-scale language model (LLM), developed by FlagAlpha.",
)