-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
[Quantization] add BNB for MixtralForCausalLM #20893
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -45,12 +45,14 @@ | |
from vllm.model_executor.layers.rotary_embedding import get_rope | ||
from vllm.model_executor.layers.vocab_parallel_embedding import ( | ||
DEFAULT_VOCAB_PADDING_SIZE, ParallelLMHead, VocabParallelEmbedding) | ||
from vllm.model_executor.model_loader.weight_utils import ( | ||
default_weight_loader, maybe_remap_kv_scale_name) | ||
from vllm.model_executor.sampling_metadata import SamplingMetadata | ||
from vllm.sequence import IntermediateTensors | ||
|
||
from . import mixtral | ||
from .interfaces import SupportsLoRA, SupportsPP | ||
from .utils import AutoWeightsLoader, make_layers, maybe_prefix | ||
from .utils import (AutoWeightsLoader, is_pp_missing_parameter, make_layers, | ||
maybe_prefix) | ||
|
||
|
||
class GraniteMoeMoE(nn.Module): | ||
|
@@ -307,6 +309,103 @@ def forward( | |
hidden_states = self.norm(hidden_states) | ||
return hidden_states | ||
|
||
def _load_weights(self, | ||
weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: | ||
""" | ||
This function is copied from `MixtralModel.load_weights`, mainly to | ||
decouple from mixtral, avoiding impact on support like BNB | ||
quantization. | ||
""" | ||
Comment on lines
+312
to
+318
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
stacked_params_mapping = [ | ||
# (param_name, shard_name, shard_id) | ||
("qkv_proj", "q_proj", "q"), | ||
("qkv_proj", "k_proj", "k"), | ||
("qkv_proj", "v_proj", "v"), | ||
] | ||
|
||
# Params for weights, fp8 weight scales, fp8 activation scales | ||
# (param_name, weight_name, expert_id, shard_id) | ||
expert_params_mapping = FusedMoE.make_expert_params_mapping( | ||
ckpt_gate_proj_name="w1", | ||
ckpt_down_proj_name="w2", | ||
ckpt_up_proj_name="w3", | ||
num_experts=self.config.num_local_experts) | ||
|
||
params_dict = dict(self.named_parameters()) | ||
loaded_params: set[str] = set() | ||
for name, loaded_weight in weights: | ||
if (self.quant_config is not None and | ||
(scale_name := self.quant_config.get_cache_scale(name))): | ||
# Loading kv cache quantization scales | ||
param = params_dict[scale_name] | ||
weight_loader = getattr(param, "weight_loader", | ||
default_weight_loader) | ||
loaded_weight = (loaded_weight if loaded_weight.dim() == 0 else | ||
loaded_weight[0]) | ||
weight_loader(param, loaded_weight) | ||
loaded_params.add(scale_name) | ||
continue | ||
|
||
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") or name.endswith("_bias")) | ||
and name not in params_dict): | ||
continue | ||
# Skip layers on other devices. | ||
if is_pp_missing_parameter(name, self): | ||
continue | ||
if name.endswith("scale"): | ||
# Remapping the name of FP8 kv-scale. | ||
name = maybe_remap_kv_scale_name(name, params_dict) | ||
if name is None: | ||
continue | ||
param = params_dict[name] | ||
weight_loader = param.weight_loader | ||
weight_loader(param, loaded_weight, shard_id) | ||
break | ||
else: | ||
for mapping in expert_params_mapping: | ||
param_name, weight_name, expert_id, shard_id = mapping | ||
if weight_name not in name: | ||
continue | ||
name = name.replace(weight_name, param_name) | ||
# Skip layers on other devices. | ||
if is_pp_missing_parameter(name, self): | ||
continue | ||
if ((name.endswith(".bias") or name.endswith("_bias")) | ||
and name not in params_dict): | ||
continue | ||
param = params_dict[name] | ||
weight_loader = param.weight_loader | ||
weight_loader(param, | ||
loaded_weight, | ||
name, | ||
shard_id=shard_id, | ||
expert_id=expert_id) | ||
break | ||
else: | ||
# Skip loading extra bias for GPTQ models. | ||
if ((name.endswith(".bias") or name.endswith("_bias")) | ||
and name not in params_dict): | ||
continue | ||
# Skip layers on other devices. | ||
if is_pp_missing_parameter(name, self): | ||
continue | ||
# Remapping the name of FP8 kv-scale. | ||
name = maybe_remap_kv_scale_name(name, params_dict) | ||
if name is None: | ||
continue | ||
|
||
param = params_dict[name] | ||
weight_loader = getattr(param, "weight_loader", | ||
default_weight_loader) | ||
weight_loader(param, loaded_weight) | ||
loaded_params.add(name) | ||
return loaded_params | ||
|
||
def load_weights(self, weights: Iterable[tuple[str, | ||
torch.Tensor]]) -> set[str]: | ||
new_weights = {} | ||
|
@@ -339,7 +438,7 @@ def load_weights(self, weights: Iterable[tuple[str, | |
new_weights[gate_name] = p | ||
else: | ||
new_weights[n] = p | ||
return mixtral.MixtralModel.load_weights(self, new_weights.items()) | ||
return self._load_weights(new_weights.items()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
|
||
class GraniteMoeForCausalLM(nn.Module, SupportsLoRA, SupportsPP): | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -27,8 +27,7 @@ | |
from vllm.model_executor.sampling_metadata import SamplingMetadata | ||
from vllm.sequence import IntermediateTensors | ||
|
||
from . import mixtral | ||
from .granitemoe import GraniteMoeAttention, GraniteMoeMoE | ||
from .granitemoe import GraniteMoeAttention, GraniteMoeModel, GraniteMoeMoE | ||
from .interfaces import SupportsLoRA, SupportsPP | ||
from .utils import AutoWeightsLoader, make_layers, maybe_prefix | ||
|
||
|
@@ -242,7 +241,7 @@ def load_weights(self, weights: Iterable[tuple[str, | |
new_weights[gate_name] = p | ||
else: | ||
new_weights[n] = p | ||
return mixtral.MixtralModel.load_weights(self, new_weights.items()) | ||
return GraniteMoeModel._load_weights(self, new_weights.items()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
|
||
class GraniteMoeSharedForCausalLM(nn.Module, SupportsLoRA, SupportsPP): | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Consider adding
bnb
tomixtral_supported
for consistency and clarity.