-
Notifications
You must be signed in to change notification settings - Fork 1.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
Add fp8 support moe models #2928
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 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,162 @@ | ||
from typing import Optional | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
from text_generation_server.utils.weights import Weights | ||
from text_generation_server.layers.fp8 import ( | ||
Fp8Weight, | ||
fp8_quantize, | ||
quant_dtype, | ||
normalize_e4m3fn_to_native_float8, | ||
) | ||
from moe_kernels.fused_moe import fused_moe | ||
|
||
|
||
class FP8SparseMoELayer(nn.Module): | ||
def __init__( | ||
self, | ||
*, | ||
n_expert_group: Optional[int], | ||
n_experts: int, | ||
prefix: str, | ||
renormalize: bool, | ||
topk: int, | ||
topk_group: Optional[int], | ||
weights: Weights, | ||
gate_proj_name: str = "gate_proj", | ||
up_proj_name: str = "up_proj", | ||
down_proj_name: str = "down_proj", | ||
): | ||
super().__init__() | ||
|
||
assert (n_expert_group is None) == ( | ||
topk_group is None | ||
), "n_expert_group and topk_group must both be None or have some value" | ||
|
||
self.n_expert_group = n_expert_group | ||
self.topk = topk | ||
self.topk_group = topk_group | ||
self.renormalize = renormalize | ||
|
||
( | ||
self.gate_up_proj, | ||
self.gate_up_proj_weight_scale, | ||
self.gate_up_proj_input_scale, | ||
) = _load_expert_multi_weights_col( | ||
prefix=prefix, | ||
n_experts=n_experts, | ||
gate_proj_name=gate_proj_name, | ||
up_proj_name=up_proj_name, | ||
weights=weights, | ||
) | ||
|
||
self.down_proj, self.down_proj_weight_scale, self.down_proj_input_scale = ( | ||
_load_expert_weights_row( | ||
prefix=prefix, | ||
n_experts=n_experts, | ||
name=down_proj_name, | ||
weights=weights, | ||
) | ||
) | ||
|
||
def forward(self, x: torch.Tensor, *, gating_output: torch.Tensor) -> torch.Tensor: | ||
return fused_moe( | ||
x, | ||
w1=self.gate_up_proj, | ||
w2=self.down_proj, | ||
gating_output=gating_output, | ||
topk=self.topk, | ||
renormalize=self.renormalize, | ||
inplace=True, | ||
use_grouped_topk=self.n_expert_group is not None, | ||
num_expert_group=self.n_expert_group, | ||
topk_group=self.topk_group, | ||
use_fp8_w8a8=True, | ||
w1_scale=self.gate_up_proj_weight_scale, | ||
w2_scale=self.down_proj_weight_scale, | ||
a1_scale=self.gate_up_proj_input_scale, | ||
a2_scale=self.down_proj_input_scale, | ||
) | ||
|
||
|
||
def _load_expert_weights( | ||
get_weight_fn, | ||
*, | ||
prefix: str, | ||
n_experts: int, | ||
name: str, | ||
weights: Weights, | ||
) -> torch.Tensor: | ||
all_weight = None | ||
all_weight_scales = None | ||
max_input_scale = None | ||
|
||
for i in range(n_experts): | ||
weight = get_weight_fn(prefix, i, name, weights) | ||
|
||
assert isinstance(weight, Fp8Weight) | ||
|
||
if all_weight is None: | ||
all_weight = torch.empty( | ||
(n_experts,) + weight.weight.shape, | ||
dtype=quant_dtype, | ||
device=weight.weight.device, | ||
) | ||
if all_weight_scales is None: | ||
all_weight_scales = torch.empty( | ||
(n_experts,), | ||
dtype=torch.float32, | ||
device=weight.weight.device, | ||
) | ||
|
||
if weight.weight.dtype in {torch.float8_e4m3fn, torch.float8_e4m3fnuz}: | ||
all_weight[i], all_weight_scales[i], current_input_scale = ( | ||
normalize_e4m3fn_to_native_float8( | ||
weight.weight, weight.weight_scale, weight.input_scale | ||
) | ||
) | ||
if current_input_scale is not None: | ||
if max_input_scale is None or current_input_scale > max_input_scale: | ||
max_input_scale = current_input_scale | ||
else: | ||
all_weight[i], all_weight_scales[i] = fp8_quantize( | ||
weight.weight, scalar=True | ||
) | ||
|
||
assert all_weight is not None | ||
|
||
return all_weight, all_weight_scales, max_input_scale | ||
|
||
|
||
def _load_expert_multi_weights_col( | ||
*, | ||
prefix: str, | ||
n_experts: int, | ||
gate_proj_name: str, | ||
up_proj_name: str, | ||
weights: Weights, | ||
) -> torch.Tensor: | ||
def get_weight_fn(prefix, i, name, weights): | ||
return weights.get_multi_weights_col( | ||
[f"{prefix}.{i}.{gate_proj_name}", f"{prefix}.{i}.{up_proj_name}"], 0 | ||
) | ||
|
||
return _load_expert_weights( | ||
get_weight_fn, prefix=prefix, n_experts=n_experts, name=None, weights=weights | ||
) | ||
|
||
|
||
def _load_expert_weights_row( | ||
*, | ||
prefix: str, | ||
n_experts: int, | ||
name: str, | ||
weights: Weights, | ||
) -> torch.Tensor: | ||
def get_weight_fn(prefix, i, name, weights): | ||
return weights.get_weights_row(f"{prefix}.{i}.{name}") | ||
|
||
return _load_expert_weights( | ||
get_weight_fn, prefix=prefix, n_experts=n_experts, name=name, weights=weights | ||
) |
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
Oops, something went wrong.
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.
The function would now not normalize on
SYSTEM != "rocm"
even if the data type isfloat8_e4m3fn
. I think either the function should be renamed tonormalize_e4m3fn_to_native_float8
or this condition should not be there (and do the conversion regardlessSYSTEM
).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.
Done renamed the function