Skip to content

Commit 374b6ad

Browse files
authored
feat(model): Support gemma-2 model (#1675)
1 parent cd2163e commit 374b6ad

File tree

8 files changed

+108
-8
lines changed

8 files changed

+108
-8
lines changed

README.ja.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ DB-GPTのアーキテクチャは以下の図に示されています:
154154
私たちは、LLaMA/LLaMA2、Baichuan、ChatGLM、Wenxin、Tongyi、Zhipuなど、オープンソースおよびAPIエージェントからの数十の大規模言語モデル(LLM)を含む幅広いモデルをサポートしています。
155155

156156
- ニュース
157+
- 🔥🔥🔥 [gemma-2-27b-it](https://huggingface.co/google/gemma-2-27b-it)
158+
- 🔥🔥🔥 [gemma-2-9b-it](https://huggingface.co/google/gemma-2-9b-it)
159+
- 🔥🔥🔥 [DeepSeek-Coder-V2-Instruct](https://huggingface.co/deepseek-ai/DeepSeek-Coder-V2-Instruct)
160+
- 🔥🔥🔥 [DeepSeek-Coder-V2-Lite-Instruct](https://huggingface.co/deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct)
161+
- 🔥🔥🔥 [Qwen2-57B-A14B-Instruct](https://huggingface.co/Qwen/Qwen2-57B-A14B-Instruct)
157162
- 🔥🔥🔥 [Qwen2-57B-A14B-Instruct](https://huggingface.co/Qwen/Qwen2-57B-A14B-Instruct)
158163
- 🔥🔥🔥 [Qwen2-72B-Instruct](https://huggingface.co/Qwen/Qwen2-72B-Instruct)
159164
- 🔥🔥🔥 [Qwen2-7B-Instruct](https://huggingface.co/Qwen/Qwen2-7B-Instruct)

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ At present, we have introduced several key features to showcase our current capa
158158
We offer extensive model support, including dozens of large language models (LLMs) from both open-source and API agents, such as LLaMA/LLaMA2, Baichuan, ChatGLM, Wenxin, Tongyi, Zhipu, and many more.
159159

160160
- News
161+
- 🔥🔥🔥 [gemma-2-27b-it](https://huggingface.co/google/gemma-2-27b-it)
162+
- 🔥🔥🔥 [gemma-2-9b-it](https://huggingface.co/google/gemma-2-9b-it)
163+
- 🔥🔥🔥 [DeepSeek-Coder-V2-Instruct](https://huggingface.co/deepseek-ai/DeepSeek-Coder-V2-Instruct)
164+
- 🔥🔥🔥 [DeepSeek-Coder-V2-Lite-Instruct](https://huggingface.co/deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct)
161165
- 🔥🔥🔥 [Qwen2-57B-A14B-Instruct](https://huggingface.co/Qwen/Qwen2-57B-A14B-Instruct)
162166
- 🔥🔥🔥 [Qwen2-72B-Instruct](https://huggingface.co/Qwen/Qwen2-72B-Instruct)
163167
- 🔥🔥🔥 [Qwen2-7B-Instruct](https://huggingface.co/Qwen/Qwen2-7B-Instruct)

README.zh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@
152152
海量模型支持,包括开源、API代理等几十种大语言模型。如LLaMA/LLaMA2、Baichuan、ChatGLM、文心、通义、智谱等。当前已支持如下模型:
153153

154154
- 新增支持模型
155+
- 🔥🔥🔥 [gemma-2-27b-it](https://huggingface.co/google/gemma-2-27b-it)
156+
- 🔥🔥🔥 [gemma-2-9b-it](https://huggingface.co/google/gemma-2-9b-it)
157+
- 🔥🔥🔥 [DeepSeek-Coder-V2-Instruct](https://huggingface.co/deepseek-ai/DeepSeek-Coder-V2-Instruct)
158+
- 🔥🔥🔥 [DeepSeek-Coder-V2-Lite-Instruct](https://huggingface.co/deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct)
159+
- 🔥🔥🔥 [Qwen2-57B-A14B-Instruct](https://huggingface.co/Qwen/Qwen2-57B-A14B-Instruct)
155160
- 🔥🔥🔥 [Qwen2-57B-A14B-Instruct](https://huggingface.co/Qwen/Qwen2-57B-A14B-Instruct)
156161
- 🔥🔥🔥 [Qwen2-72B-Instruct](https://huggingface.co/Qwen/Qwen2-72B-Instruct)
157162
- 🔥🔥🔥 [Qwen2-7B-Instruct](https://huggingface.co/Qwen/Qwen2-7B-Instruct)

dbgpt/configs/model_config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,16 @@ def get_device() -> str:
225225
"gemma-7b-it": os.path.join(MODEL_PATH, "gemma-7b-it"),
226226
# https://huggingface.co/google/gemma-2b-it
227227
"gemma-2b-it": os.path.join(MODEL_PATH, "gemma-2b-it"),
228+
"gemma-2-9b-it": os.path.join(MODEL_PATH, "gemma-2-9b-it"),
229+
"gemma-2-27b-it": os.path.join(MODEL_PATH, "gemma-2-27b-it"),
228230
"starling-lm-7b-beta": os.path.join(MODEL_PATH, "Starling-LM-7B-beta"),
229231
"deepseek-v2-lite-chat": os.path.join(MODEL_PATH, "DeepSeek-V2-Lite-Chat"),
232+
"deepseek-coder-v2-instruct": os.path.join(
233+
MODEL_PATH, "DeepSeek-Coder-V2-Instruct"
234+
),
235+
"deepseek-coder-v2-lite-instruct": os.path.join(
236+
MODEL_PATH, "DeepSeek-Coder-V2-Lite-Instruct"
237+
),
230238
"sailor-14b-chat": os.path.join(MODEL_PATH, "Sailor-14B-Chat"),
231239
# https://huggingface.co/microsoft/Phi-3-medium-128k-instruct
232240
"phi-3-medium-128k-instruct": os.path.join(

dbgpt/model/adapter/base.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,31 @@ def load(self, model_path: str, from_pretrained_kwargs: dict):
112112
"""Load model and tokenizer"""
113113
raise NotImplementedError
114114

115+
def parse_max_length(self, model, tokenizer) -> Optional[int]:
116+
"""Parse the max_length of the model.
117+
118+
Returns:
119+
Optional[int]: The max_length of the model
120+
"""
121+
if not (tokenizer or model):
122+
return None
123+
try:
124+
model_max_length = None
125+
if tokenizer and hasattr(tokenizer, "model_max_length"):
126+
model_max_length = tokenizer.model_max_length
127+
if model_max_length and model_max_length < 100000000:
128+
# Can't be too large
129+
return model_max_length
130+
if model and hasattr(model, "config"):
131+
model_config = model.config
132+
if hasattr(model_config, "max_sequence_length"):
133+
return model_config.max_sequence_length
134+
if hasattr(model_config, "max_position_embeddings"):
135+
return model_config.max_position_embeddings
136+
return None
137+
except Exception:
138+
return None
139+
115140
def load_from_params(self, params):
116141
"""Load the model and tokenizer according to the given parameters"""
117142
raise NotImplementedError

dbgpt/model/adapter/hf_adapter.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def load(self, model_path: str, from_pretrained_kwargs: dict):
7373
) from exc
7474
self.check_dependencies()
7575

76+
logger.info(
77+
f"Load model from {model_path}, from_pretrained_kwargs: {from_pretrained_kwargs}"
78+
)
79+
7680
revision = from_pretrained_kwargs.get("revision", "main")
7781
try:
7882
tokenizer = AutoTokenizer.from_pretrained(
@@ -235,6 +239,43 @@ def do_match(self, lower_model_name_or_path: Optional[str] = None):
235239
)
236240

237241

242+
class Gemma2Adapter(NewHFChatModelAdapter):
243+
"""
244+
https://huggingface.co/google/gemma-2-27b-it
245+
https://huggingface.co/google/gemma-2-9b-it
246+
"""
247+
248+
support_4bit: bool = True
249+
support_8bit: bool = True
250+
support_system_message: bool = False
251+
252+
def use_fast_tokenizer(self) -> bool:
253+
return True
254+
255+
def check_transformer_version(self, current_version: str) -> None:
256+
if not current_version >= "4.42.1":
257+
raise ValueError(
258+
"Gemma2 require transformers.__version__>=4.42.1, please upgrade your transformers package."
259+
)
260+
261+
def do_match(self, lower_model_name_or_path: Optional[str] = None):
262+
return (
263+
lower_model_name_or_path
264+
and "gemma-2-" in lower_model_name_or_path
265+
and "it" in lower_model_name_or_path
266+
)
267+
268+
def load(self, model_path: str, from_pretrained_kwargs: dict):
269+
import torch
270+
271+
if not from_pretrained_kwargs:
272+
from_pretrained_kwargs = {}
273+
from_pretrained_kwargs["torch_dtype"] = torch.bfloat16
274+
# from_pretrained_kwargs["revision"] = "float16"
275+
model, tokenizer = super().load(model_path, from_pretrained_kwargs)
276+
return model, tokenizer
277+
278+
238279
class StarlingLMAdapter(NewHFChatModelAdapter):
239280
"""
240281
https://huggingface.co/Nexusflow/Starling-LM-7B-beta
@@ -416,6 +457,17 @@ def load(self, model_path: str, from_pretrained_kwargs: dict):
416457
return model, tokenizer
417458

418459

460+
class DeepseekCoderV2Adapter(DeepseekV2Adapter):
461+
def do_match(self, lower_model_name_or_path: Optional[str] = None):
462+
return (
463+
lower_model_name_or_path
464+
and "deepseek" in lower_model_name_or_path
465+
and "coder" in lower_model_name_or_path
466+
and "v2" in lower_model_name_or_path
467+
and "instruct" in lower_model_name_or_path
468+
)
469+
470+
419471
class SailorAdapter(QwenAdapter):
420472
"""
421473
https://huggingface.co/sail/Sailor-14B-Chat
@@ -520,11 +572,13 @@ def do_match(self, lower_model_name_or_path: Optional[str] = None):
520572
register_model_adapter(Mixtral8x7BAdapter)
521573
register_model_adapter(SOLARAdapter)
522574
register_model_adapter(GemmaAdapter)
575+
register_model_adapter(Gemma2Adapter)
523576
register_model_adapter(StarlingLMAdapter)
524577
register_model_adapter(QwenAdapter)
525578
register_model_adapter(QwenMoeAdapter)
526579
register_model_adapter(Llama3Adapter)
527580
register_model_adapter(DeepseekV2Adapter)
581+
register_model_adapter(DeepseekCoderV2Adapter)
528582
register_model_adapter(SailorAdapter)
529583
register_model_adapter(PhiAdapter)
530584
register_model_adapter(SQLCoderAdapter)

dbgpt/model/cluster/worker/default_worker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ def start(
116116
self.model, self.tokenizer = self.ml.loader_with_params(
117117
model_params, self.llm_adapter
118118
)
119-
model_max_length = _parse_model_max_length(self.model, self.tokenizer)
119+
model_max_length = self.llm_adapter.parse_max_length(
120+
self.model, self.tokenizer
121+
)
120122
if model_max_length:
121123
logger.info(
122124
f"Parse model max length {model_max_length} from model {self.model_name}."

dbgpt/model/llm_out/hf_chat_llm.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def huggingface_chat_generate_stream(
2121
echo = params.get("echo", False)
2222
max_new_tokens = int(params.get("max_new_tokens", 2048))
2323
stop_token_ids = params.get("stop_token_ids", [])
24-
do_sample = params.get("do_sample", None)
24+
do_sample = params.get("do_sample", True)
2525
custom_stop_words = params.get("custom_stop_words", [])
2626

2727
input_ids = tokenizer(prompt).input_ids
@@ -34,11 +34,6 @@ def huggingface_chat_generate_stream(
3434
input_echo_len = len(input_ids)
3535
input_ids = torch.as_tensor([input_ids], device=device)
3636

37-
# messages = params["messages"]
38-
# messages = ModelMessage.to_openai_messages(messages)
39-
# input_ids = tokenizer.apply_chat_template(conversation=messages, tokenize=True, add_generation_prompt=True, return_tensors='pt')
40-
# input_ids = input_ids.to(device)
41-
4237
streamer = TextIteratorStreamer(
4338
tokenizer, skip_prompt=not echo, skip_special_tokens=True
4439
)
@@ -55,7 +50,9 @@ def huggingface_chat_generate_stream(
5550
if do_sample is not None:
5651
base_kwargs["do_sample"] = do_sample
5752

58-
logger.info(f"Predict with parameters: {base_kwargs}")
53+
logger.info(
54+
f"Predict with parameters: {base_kwargs}\ncustom_stop_words: {custom_stop_words}"
55+
)
5956

6057
generate_kwargs = {"input_ids": input_ids, **base_kwargs}
6158
thread = Thread(target=model.generate, kwargs=generate_kwargs)

0 commit comments

Comments
 (0)