Skip to content

Commit

Permalink
support llama3 (#3259)
Browse files Browse the repository at this point in the history
  • Loading branch information
gasvn authored Apr 23, 2024
1 parent 24c4d4f commit 27a05b0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
29 changes: 29 additions & 0 deletions fastchat/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SeparatorStyle(IntEnum):
NO_COLON_TWO = auto()
ADD_NEW_LINE_SINGLE = auto()
LLAMA2 = auto()
LLAMA3 = auto()
CHATGLM = auto()
CHATML = auto()
CHATINTERN = auto()
Expand Down Expand Up @@ -153,6 +154,19 @@ def get_prompt(self) -> str:
else:
ret += tag
return ret
elif self.sep_style == SeparatorStyle.LLAMA3:
ret = "<|begin_of_text|>"
if self.system_message:
ret += system_prompt
else:
ret += ""
for i, (role, message) in enumerate(self.messages):
if message:
ret += f"<|start_header_id|>{role}<|end_header_id|>\n\n"
ret += f"{message.strip()}<|eot_id|>"
else:
ret += f"<|start_header_id|>{role}<|end_header_id|>\n\n"
return ret
elif self.sep_style == SeparatorStyle.CHATGLM:
# source: https://huggingface.co/THUDM/chatglm-6b/blob/1d240ba371910e9282298d4592532d7f0f3e9f3e/modeling_chatglm.py#L1302-L1308
# source2: https://huggingface.co/THUDM/chatglm2-6b/blob/e186c891cf64310ac66ef10a87e6635fa6c2a579/modeling_chatglm.py#L926
Expand Down Expand Up @@ -1245,6 +1259,21 @@ def get_conv_template(name: str) -> Conversation:
)
)

# llama3 template
# reference: https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct/blob/main/tokenizer_config.json
# reference: https://github.com/meta-llama/llama3/blob/0cee08ec68f4cfc0c89fe4a9366d82679aaa2a66/llama/tokenizer.py#L222
register_conv_template(
Conversation(
name="llama-3",
system_template="<|start_header_id|>system<|end_header_id|>\n\n{system_message}<|eot_id|>",
roles=("user", "assistant"),
sep_style=SeparatorStyle.LLAMA3,
sep="",
stop_str="<|eot_id|>",
stop_token_ids=[128001, 128009],
)
)

register_conv_template(
Conversation(
name="chinese-alpaca2",
Expand Down
17 changes: 17 additions & 0 deletions fastchat/model/model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,22 @@ def get_default_conv_template(self, model_path: str) -> Conversation:
return get_conv_template("llama-2")


class Llama3Adapter(BaseModelAdapter):
"""The model adapter for Llama-3 (e.g., meta-llama/Meta-Llama-3-8B-Instruct)"""

def match(self, model_path: str):
return "llama-3" in model_path.lower()

def load_model(self, model_path: str, from_pretrained_kwargs: dict):
model, tokenizer = super().load_model(model_path, from_pretrained_kwargs)
model.config.eos_token_id = tokenizer.eos_token_id
model.config.pad_token_id = tokenizer.pad_token_id
return model, tokenizer

def get_default_conv_template(self, model_path: str) -> Conversation:
return get_conv_template("llama-3")


class CuteGPTAdapter(BaseModelAdapter):
"""The model adapter for CuteGPT"""

Expand Down Expand Up @@ -2440,6 +2456,7 @@ def get_default_conv_template(self, model_path: str) -> Conversation:
register_model_adapter(InternLMChatAdapter)
register_model_adapter(StarChatAdapter)
register_model_adapter(Llama2Adapter)
register_model_adapter(Llama3Adapter)
register_model_adapter(CuteGPTAdapter)
register_model_adapter(OpenOrcaAdapter)
register_model_adapter(DolphinAdapter)
Expand Down
7 changes: 7 additions & 0 deletions fastchat/model/model_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,13 @@ def get_model_info(name: str) -> ModelInfo:
"A multi-language large-scale language model (LLM), developed by FlagAlpha.",
)

register_model_info(
["Meta-Llama-3-8B-Instruct", "Meta-Llama-3-70B-Instruct"],
"llama-3",
"https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct",
"Meta developed and released the Meta Llama 3 family of large language models (LLMs), a collection of pretrained and instruction tuned generative text models in 8 and 70B sizes.",
)

register_model_info(
["Chinese-Alpaca-2-7B", "Chinese-Alpaca-2-13B"],
"Chinese-Alpaca",
Expand Down

0 comments on commit 27a05b0

Please sign in to comment.