-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Added DeciLM-7b and DeciLM-7b-instruct #2062
Merged
WoosukKwon
merged 6 commits into
vllm-project:main
from
avideci:feature/add_decilm_7b_models
Dec 19, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e2e8d6f
Added DeciLM-7b and DeciLM-7b-instruct support with uniform GQA inste…
avideci f28efb5
Reformatted decilm new file
avideci 1636e61
Merge branch 'main' into feature/add_decilm_7b_models
avideci 61e10be
Merge branch 'main' into feature/add_decilm_7b_models
WoosukKwon 7e26d65
Minor fixes
WoosukKwon e13541c
Add DeciLM to supported models
WoosukKwon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# coding=utf-8 | ||
# Adapted from | ||
# https://github.com/huggingface/transformers/blob/v4.28.0/src/transformers/models/llama/modeling_llama.py | ||
# Copyright 2023 DeciAI Research Team. All rights reserved. | ||
# Copyright 2023 The vLLM team. | ||
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. | ||
# | ||
# This code is based on MistralAI GPT-NeoX library and the GPT-NeoX | ||
# and OPT implementations in this library. It has been modified from its | ||
# original forms to accommodate minor architectural differences compared | ||
# to GPT-NeoX and OPT used by the Meta AI team that trained the model. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Inference-only DeciLM model compatible with HuggingFace weights.""" | ||
|
||
from typing import Optional | ||
|
||
import torch | ||
from transformers import PretrainedConfig | ||
|
||
from vllm.model_executor.layers.linear import LinearMethodBase | ||
from vllm.model_executor.models.llama import LlamaForCausalLM | ||
from vllm.model_executor.weight_utils import (default_weight_loader, | ||
hf_model_weights_iterator) | ||
|
||
|
||
class DeciLMForCausalLM(LlamaForCausalLM): | ||
""" | ||
Implementation for https://huggingface.co/Deci/DeciLM-7b-instruct. | ||
Based on the llama executor. | ||
|
||
The main difference is that DeciLM uses Variable Grouped Query Attention. | ||
The constant number of GQA heads in the decoder is overriden with a value | ||
per layer. | ||
|
||
Usually, in the HuggingFace implementation, instead of | ||
"config.num_key_value_heads", we use | ||
"config.num_key_value_heads_per_layer[i]" which varies. | ||
|
||
Currently, PagedAttention does not work well with variable GQA, so we | ||
normalize the weights upon loading, and use uniform GQA with the max value | ||
instead. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
config: Optional[PretrainedConfig] = None, | ||
linear_method: Optional[LinearMethodBase] = None, | ||
) -> None: | ||
config.num_key_value_heads = max(config.num_key_value_heads_per_layer) | ||
delattr(config, "num_key_value_heads_per_layer") | ||
super().__init__(config=config, linear_method=linear_method) | ||
|
||
def load_weights(self, | ||
model_name_or_path: str, | ||
cache_dir: Optional[str] = None, | ||
load_format: str = "auto", | ||
revision: Optional[str] = None): | ||
stacked_params_mapping = [ | ||
# (param_name, shard_name, shard_id) | ||
("qkv_proj", "q_proj", "q"), | ||
("qkv_proj", "k_proj", "k"), | ||
("qkv_proj", "v_proj", "v"), | ||
("gate_up_proj", "gate_proj", 0), | ||
("gate_up_proj", "up_proj", 1), | ||
] | ||
params_dict = dict(self.named_parameters()) | ||
for name, loaded_weight in hf_model_weights_iterator( | ||
model_name_or_path, cache_dir, load_format, revision): | ||
if "rotary_emb.inv_freq" in name: | ||
continue | ||
|
||
if "k_proj" in name or "v_proj" in name: | ||
loaded_weight = self._degroup_weight(loaded_weight) | ||
|
||
for (param_name, weight_name, shard_id) in stacked_params_mapping: | ||
if weight_name not in name: | ||
continue | ||
name = name.replace(weight_name, param_name) | ||
# Skip loading extra bias for GPTQ models. | ||
if name.endswith(".bias") and name not in params_dict: | ||
continue | ||
param = params_dict[name] | ||
weight_loader = param.weight_loader | ||
weight_loader(param, loaded_weight, shard_id) | ||
break | ||
else: | ||
# Skip loading extra bias for GPTQ models. | ||
if name.endswith(".bias") and name not in params_dict: | ||
continue | ||
param = params_dict[name] | ||
weight_loader = getattr(param, "weight_loader", | ||
default_weight_loader) | ||
weight_loader(param, loaded_weight) | ||
|
||
def _degroup_weight(self, loaded_weight: torch.Tensor) -> torch.Tensor: | ||
hidden_size = self.config.hidden_size | ||
head_size = self.config.hidden_size // self.config.num_attention_heads | ||
target_num_kv_heads = self.config.num_key_value_heads | ||
num_kv_heads = loaded_weight.shape[0] // head_size | ||
n_repeats = target_num_kv_heads / num_kv_heads | ||
assert n_repeats == int(n_repeats) | ||
|
||
n_repeats = int(n_repeats) | ||
loaded_weight = loaded_weight.view(num_kv_heads, head_size, | ||
hidden_size) | ||
loaded_weight = torch.repeat_interleave(loaded_weight, | ||
repeats=n_repeats, | ||
dim=0) | ||
loaded_weight = loaded_weight.reshape(target_num_kv_heads * head_size, | ||
hidden_size) | ||
|
||
return loaded_weight |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, we are convertin the model to uniform GQA instead of variable GQA.
That's becasue PagedAttention kernel did not work well with variable GQA (not idea why, still) and this is a workaround.
We choose the max. number spotted in this list: https://huggingface.co/Deci/DeciLM-7B/blob/main/config.json#L18
Then we convert weights of "k_proj" and "v_proj" a uniform GQA using "degroup_weight".