@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / 1209261e93
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 16, 05:45 UTC

[Model] Support telechat2 (#10311)

Signed-off-by: Isotr0py <2037008807@qq.com> Co-authored-by: xiangw2 <xiangw2@chinatelecom.cn> Co-authored-by: Isotr0py <2037008807@qq.com>

shunxing12345 committed Nov 27, 2024 at 19:32 UTC 1209261e937f7cc5a933da48d625d17e6ee8eea9
8 files changed +210 -3
docs/source/models/supported_models.rst
+5
@@ -309,6 +309,11 @@ Text Generation
309 - :code:`upstage/solar-pro-preview-instruct`, etc.
310 - ✅︎
311 - ✅︎
312 + * - :code:`TeleChat2ForCausalLM`
313 + - TeleChat2
314 + - :code:`TeleAI/TeleChat2-3B`, :code:`TeleAI/TeleChat2-7B`, :code:`TeleAI/TeleChat2-35B`, etc.
315 + - ✅︎
316 + - ✅︎
317 * - :code:`XverseForCausalLM`
318 - XVERSE
319 - :code:`xverse/XVERSE-7B-Chat`, :code:`xverse/XVERSE-13B-Chat`, :code:`xverse/XVERSE-65B-Chat`, etc.
tests/models/registry.py
+2
@@ -115,6 +115,8 @@ _TEXT_GENERATION_EXAMPLE_MODELS = {
115 "StableLmForCausalLM": _HfExamplesInfo("stabilityai/stablelm-3b-4e1t"),
116 "Starcoder2ForCausalLM": _HfExamplesInfo("bigcode/starcoder2-3b"),
117 "SolarForCausalLM": _HfExamplesInfo("upstage/solar-pro-preview-instruct"),
118 + "TeleChat2ForCausalLM": _HfExamplesInfo("Tele-AI/TeleChat2-3B",
119 + trust_remote_code=True),
120 "XverseForCausalLM": _HfExamplesInfo("xverse/XVERSE-7B-Chat",
121 is_available_online=False,
122 trust_remote_code=True),
vllm/model_executor/models/llama.py
+4 -2
@@ -501,8 +501,7 @@ class LlamaForCausalLM(nn.Module, SupportsLoRA, SupportsPP):
501 self.config = config
502 self.lora_config = lora_config
503
504 - self.model = LlamaModel(vllm_config=vllm_config,
505 - prefix=maybe_prefix(prefix, "model"))
504 + self.model = self._init_model(vllm_config=vllm_config, prefix=prefix)
505 if get_pp_group().is_last_rank:
506 self.unpadded_vocab_size = config.vocab_size
507 if lora_config:
@@ -539,6 +538,9 @@ class LlamaForCausalLM(nn.Module, SupportsLoRA, SupportsPP):
538 normalize=False,
539 softmax=False)
540
541 + def _init_model(self, vllm_config: VllmConfig, prefix: str = ""):
542 + return LlamaModel(vllm_config=vllm_config, prefix=prefix)
543 +
544 def get_input_embeddings(self, input_ids: torch.Tensor) -> torch.Tensor:
545 return self.model.get_input_embeddings(input_ids)
546
vllm/model_executor/models/registry.py
+2
@@ -91,6 +91,7 @@ _TEXT_GENERATION_MODELS = {
91 "StableLmForCausalLM": ("stablelm", "StablelmForCausalLM"),
92 "Starcoder2ForCausalLM": ("starcoder2", "Starcoder2ForCausalLM"),
93 "SolarForCausalLM": ("solar", "SolarForCausalLM"),
94 + "TeleChat2ForCausalLM": ("telechat2", "TeleChat2ForCausalLM"),
95 "XverseForCausalLM": ("xverse", "XverseForCausalLM"),
96 # [Encoder-decoder]
97 "BartModel": ("bart", "BartForConditionalGeneration"),
@@ -118,6 +119,7 @@ _EMBEDDING_MODELS = {
119 "Qwen2ForCausalLM": ("qwen2", "Qwen2ForCausalLM"),
120 "Qwen2ForRewardModel": ("qwen2_rm", "Qwen2ForRewardModel"),
121 "Qwen2ForSequenceClassification": ("qwen2_cls", "Qwen2ForSequenceClassification"), # noqa: E501
122 + "TeleChat2ForCausalLM": ("telechat2", "TeleChat2ForCausalLM"),
123 # [Multimodal]
124 "LlavaNextForConditionalGeneration": ("llava_next", "LlavaNextForConditionalGeneration"), # noqa: E501
125 "Phi3VForCausalLM": ("phi3v", "Phi3VForCausalLM"),
vllm/model_executor/models/telechat2.py new
+131
@@ -0,0 +1,131 @@
1 +# Copyright 2023 The vLLM team.
2 +# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
3 +#
4 +# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
5 +# and OPT implementations in this library. It has been modified from its
6 +# original forms to accommodate minor architectural differences compared
7 +# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
8 +#
9 +# Licensed under the Apache License, Version 2.0 (the "License");
10 +# you may not use this file except in compliance with the License.
11 +# You may obtain a copy of the License at
12 +#
13 +# http://www.apache.org/licenses/LICENSE-2.0
14 +#
15 +# Unless required by applicable law or agreed to in writing, software
16 +# distributed under the License is distributed on an "AS IS" BASIS,
17 +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 +# See the License for the specific language governing permissions and
19 +# limitations under the License.
20 +from typing import Iterable, Set, Tuple
21 +
22 +import torch
23 +
24 +from vllm.config import VllmConfig
25 +from vllm.model_executor.model_loader.weight_utils import default_weight_loader
26 +from vllm.model_executor.models.llama import LlamaForCausalLM, LlamaModel
27 +
28 +from .utils import (AutoWeightsLoader, PPMissingLayer, WeightsMapper,
29 + is_pp_missing_parameter)
30 +
31 +
32 +class TeleChat2Model(LlamaModel):
33 +
34 + def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
35 + # 1. Initialize the LlamaModel with bias
36 + vllm_config.model_config.hf_config.bias = True
37 + vllm_config.model_config.hf_config.mlp_bias = True
38 + super().__init__(vllm_config=vllm_config, prefix=prefix)
39 + # 2. Remove the bias from the qkv_proj and gate_up_proj based on config
40 + # Telechat2's gate_up_proj and qkv_proj don't have bias
41 + # see: https://github.com/vllm-project/vllm/pull/10311#issuecomment-2490297566
42 + for layer in self.layers:
43 + if not isinstance(layer, PPMissingLayer):
44 + layer.self_attn.qkv_proj.bias = None
45 + layer.self_attn.qkv_proj.skip_bias_add = True
46 + layer.mlp.gate_up_proj.bias = None
47 + layer.mlp.gate_up_proj.skip_bias_add = True
48 +
49 + def load_weights(self, weights: Iterable[Tuple[str,
50 + torch.Tensor]]) -> Set[str]:
51 + stacked_params_mapping = [
52 + ('gate_up_proj', 'gate_proj', 0),
53 + ('gate_up_proj', 'up_proj', 1),
54 + ]
55 + params_dict = dict(self.named_parameters())
56 + loaded_params: Set[str] = set()
57 + total_num_heads = self.config.n_head
58 + head_dim = self.config.hidden_size // total_num_heads
59 + for name, loaded_weight in weights:
60 + if "self_attn.key_value" in name:
61 + k_weight = []
62 + v_weight = []
63 + for i in range(total_num_heads):
64 + start = i * head_dim * 2
65 + k_weight.append(loaded_weight[start:start + head_dim, :])
66 + v_weight.append(loaded_weight[start + head_dim:start +
67 + 2 * head_dim:])
68 + k_weight = torch.cat(k_weight, dim=0)
69 + v_weight = torch.cat(v_weight, dim=0)
70 + name = name.replace("key_value", "qkv_proj")
71 + if is_pp_missing_parameter(name, self):
72 + continue
73 + param = params_dict[name]
74 + weight_loader = param.weight_loader
75 + weight_loader(param, k_weight, "k")
76 + weight_loader(param, v_weight, "v")
77 + elif "query" in name:
78 + name = name.replace("query", "qkv_proj")
79 + if is_pp_missing_parameter(name, self):
80 + continue
81 + param = params_dict[name]
82 + weight_loader = param.weight_loader
83 + weight_loader(param, loaded_weight, "q")
84 + else:
85 + for param_name, weight_name, shard_id in stacked_params_mapping:
86 + if weight_name not in name:
87 + continue
88 + name = name.replace(weight_name, param_name)
89 + if is_pp_missing_parameter(name, self):
90 + continue
91 + param = params_dict[name]
92 + weight_loader = param.weight_loader
93 + weight_loader(param, loaded_weight, shard_id)
94 + break
95 + else:
96 + if is_pp_missing_parameter(name, self):
97 + continue
98 + param = params_dict[name]
99 + weight_loader = getattr(param, "weight_loader",
100 + default_weight_loader)
101 + weight_loader(param, loaded_weight)
102 + loaded_params.add(name)
103 + return loaded_params
104 +
105 +
106 +class TeleChat2ForCausalLM(LlamaForCausalLM):
107 +
108 + def _init_model(self, vllm_config: VllmConfig, prefix: str = ""):
109 + return TeleChat2Model(vllm_config=vllm_config, prefix=prefix)
110 +
111 + def load_weights(self, weights: Iterable[Tuple[str,
112 + torch.Tensor]]) -> Set[str]:
113 +
114 + hf_to_vllm_mapper = WeightsMapper(
115 + orig_to_new_prefix={
116 + "transformer.": "model.",
117 + },
118 + orig_to_new_substr={
119 + ".h.": ".layers.",
120 + ".self_attention.": ".self_attn.",
121 + ".word_embeddings.": ".embed_tokens.",
122 + ".dense.": ".o_proj.",
123 + ".ln_f.": ".norm.",
124 + },
125 + )
126 + loader = AutoWeightsLoader(
127 + self,
128 + skip_prefixes=(["lm_head."]
129 + if self.config.tie_word_embeddings else None),
130 + )
131 + return loader.load_weights(weights, mapper=hf_to_vllm_mapper)
vllm/transformers_utils/config.py
+3 -1
@@ -29,7 +29,8 @@ from vllm.transformers_utils.configs import (ChatGLMConfig, DbrxConfig,
29 MLPSpeculatorConfig, MPTConfig,
30 NemotronConfig, NVLM_D_Config,
31 Olmo2Config, RWConfig,
32 - SolarConfig, UltravoxConfig)
32 + SolarConfig, Telechat2Config,
33 + UltravoxConfig)
34 # yapf: enable
35 from vllm.transformers_utils.utils import check_gguf_file
36 from vllm.utils import resolve_obj_by_qualname
@@ -64,6 +65,7 @@ _CONFIG_REGISTRY: Dict[str, Type[PretrainedConfig]] = {
65 "NVLM_D": NVLM_D_Config,
66 "olmo2": Olmo2Config,
67 "solar": SolarConfig,
68 + "telechat": Telechat2Config,
69 "ultravox": UltravoxConfig,
70 **_CONFIG_REGISTRY_OVERRIDE_HF
71 }
vllm/transformers_utils/configs/__init__.py
+2
@@ -17,6 +17,7 @@ from vllm.transformers_utils.configs.nemotron import NemotronConfig
17 from vllm.transformers_utils.configs.nvlm_d import NVLM_D_Config
18 from vllm.transformers_utils.configs.olmo2 import Olmo2Config
19 from vllm.transformers_utils.configs.solar import SolarConfig
20 +from vllm.transformers_utils.configs.telechat2 import Telechat2Config
21 from vllm.transformers_utils.configs.ultravox import UltravoxConfig
22
23 __all__ = [
@@ -36,5 +37,6 @@ __all__ = [
37 "NVLM_D_Config",
38 "Olmo2Config",
39 "SolarConfig",
40 + "Telechat2Config",
41 "UltravoxConfig",
42 ]
\ No newline at end of file
vllm/transformers_utils/configs/telechat2.py new
+61
@@ -0,0 +1,61 @@
1 +# adapted from https://www.modelscope.cn/models/TeleAI/TeleChat2-3B/resolve/master/configuration_telechat2.py
2 +""" Telechat configuration compatible with LlamaConfig. """
3 +
4 +from transformers.configuration_utils import PretrainedConfig
5 +
6 +
7 +class Telechat2Config(PretrainedConfig):
8 +
9 + model_type = "telechat"
10 + keys_to_ignore_at_inference = ["past_key_values"]
11 + attribute_map = {
12 + "num_hidden_layers": "n_layer",
13 + "num_attention_heads": "n_head",
14 + "intermediate_size": "ffn_hidden_size",
15 + "rms_norm_eps": "layer_norm_epsilon"
16 + }
17 +
18 + def __init__(
19 + self,
20 + vocab_size=160256,
21 + hidden_size=4096,
22 + n_layer=30,
23 + n_head=32,
24 + layer_norm_epsilon=1e-5,
25 + initializer_range=0.02,
26 + use_cache=True,
27 + bos_token_id=1,
28 + eos_token_id=2,
29 + apply_residual_connection_post_layernorm=False,
30 + hidden_dropout=0.0,
31 + attention_dropout=0.0,
32 + ffn_hidden_size=12288,
33 + training_seqlen=8192,
34 + logn=True,
35 + embed_layernorm=False,
36 + hidden_act="silu",
37 + **kwargs,
38 + ):
39 + self.vocab_size = vocab_size
40 + n_embed = kwargs.pop("n_embed", None)
41 + self.hidden_size = hidden_size if n_embed is None else n_embed
42 + self.n_layer = n_layer
43 + self.n_head = n_head
44 + self.layer_norm_epsilon = layer_norm_epsilon
45 + self.initializer_range = initializer_range
46 + self.use_cache = use_cache
47 + self.apply_residual_connection_post_layernorm = (
48 + apply_residual_connection_post_layernorm)
49 + self.hidden_dropout = hidden_dropout
50 + self.attention_dropout = attention_dropout
51 + self.bos_token_id = bos_token_id
52 + self.eos_token_id = eos_token_id
53 + self.logn = logn
54 + self.training_seqlen = training_seqlen
55 + self.embed_layernorm = embed_layernorm
56 + self.num_key_value_heads = kwargs.pop("num_key_value_heads", None)
57 + self.ffn_hidden_size = ffn_hidden_size
58 + self.hidden_act = hidden_act
59 + super().__init__(bos_token_id=bos_token_id,
60 + eos_token_id=eos_token_id,
61 + **kwargs)