-
Notifications
You must be signed in to change notification settings - Fork 717
[Draft] Add PyTorch engine support for Hy3 BF16 and static FP8 (NO MTP) #4763
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
Draft
yidingcheng0206
wants to merge
2
commits into
InternLM:main
Choose a base branch
from
yidingcheng0206:feat/hy3-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| # Copyright (c) OpenMMLab. All rights reserved. | ||
| from .blocked_fp8 import TritonFusedMoEBlockedF8Builder # noqa: F401 | ||
| from .default import TritonFusedMoEBuilder # noqa: F401 | ||
| from .static_fp8 import TritonFusedMoEStaticF8Builder # noqa: F401 | ||
| from .w8a8 import TritonFusedMoEW8A8Builder # noqa: F401 |
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 |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Copyright (c) OpenMMLab. All rights reserved. | ||
|
|
||
| import torch | ||
|
|
||
| from lmdeploy.pytorch.backends.moe import ( | ||
| FusedMoEStaticF8Builder, | ||
| FusedMoEStaticF8Impl, | ||
| ) | ||
| from lmdeploy.pytorch.kernels.cuda.w8a8_fused_moe import ( | ||
| fused_moe_static_fp8, | ||
| ) | ||
|
|
||
|
|
||
| class TritonFusedMoEStaticF8Impl(FusedMoEStaticF8Impl): | ||
| """Triton static FP8 fused MoE implementation.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| top_k: int, | ||
| num_experts: int, | ||
| renormalize: bool = False, | ||
| out_dtype: torch.dtype = torch.float16, | ||
| quant_dtype: torch.dtype = torch.float8_e4m3fn, | ||
| ): | ||
| self.top_k = top_k | ||
| self.num_experts = num_experts | ||
| self.renormalize = renormalize | ||
| self.out_dtype = out_dtype | ||
| self.quant_dtype = quant_dtype | ||
|
|
||
| def forward( | ||
| self, | ||
| hidden_states: torch.Tensor, | ||
| topk_weights: torch.Tensor, | ||
| topk_ids: torch.LongTensor, | ||
| gate_up_weights: torch.Tensor, | ||
| gate_up_weight_scale: torch.Tensor, | ||
| gate_up_input_scale: torch.Tensor, | ||
| down_weights: torch.Tensor, | ||
| down_weight_scale: torch.Tensor, | ||
| down_input_scale: torch.Tensor, | ||
| expert_list: list[int] = None, | ||
| ): | ||
| """Forward.""" | ||
| hidden_states = hidden_states.contiguous() | ||
|
|
||
| expert_offset = 0 | ||
| num_experts = None | ||
|
|
||
| if ( | ||
| expert_list is not None | ||
| and len(expert_list) != self.num_experts | ||
| ): | ||
| expert_offset = expert_list[0] | ||
| num_experts = self.num_experts | ||
|
|
||
| return fused_moe_static_fp8( | ||
| hidden_states, | ||
| gate_up_input_scale, | ||
| gate_up_weights, | ||
| gate_up_weight_scale, | ||
| down_input_scale, | ||
| down_weights, | ||
| down_weight_scale, | ||
| topk_weights, | ||
| topk_ids, | ||
| topk=self.top_k, | ||
| out_dtype=self.out_dtype, | ||
| quant_dtype=self.quant_dtype, | ||
| expert_offset=expert_offset, | ||
| num_experts=num_experts, | ||
| renormalize=self.renormalize, | ||
| ) | ||
|
|
||
|
|
||
| class TritonFusedMoEStaticF8Builder( | ||
| FusedMoEStaticF8Builder, | ||
| ): | ||
| """Triton static FP8 fused MoE builder.""" | ||
|
|
||
| @staticmethod | ||
| def build( | ||
| top_k: int, | ||
| num_experts: int, | ||
| renormalize: bool = False, | ||
| out_dtype: torch.dtype = torch.float16, | ||
| quant_dtype: torch.dtype = torch.float8_e4m3fn, | ||
| ): | ||
| """Build static FP8 fused MoE.""" | ||
| return TritonFusedMoEStaticF8Impl( | ||
| top_k=top_k, | ||
| num_experts=num_experts, | ||
| renormalize=renormalize, | ||
| out_dtype=out_dtype, | ||
| quant_dtype=quant_dtype, | ||
| ) |
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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Copyright (c) OpenMMLab. All rights reserved. | ||
|
|
||
| import torch | ||
|
|
||
| import lmdeploy.pytorch.distributed as dist | ||
| from lmdeploy.pytorch.kernels.cuda.w8a8_triton_kernels import ( | ||
| matmul_kernel_static_quant, | ||
| ) | ||
|
|
||
| from ..static_fp8_modules import ( | ||
| LinearStaticF8Builder, | ||
| LinearStaticF8Impl, | ||
| ) | ||
|
|
||
|
|
||
| class TritonLinearStaticF8Impl(LinearStaticF8Impl): | ||
| """Triton static per-tensor FP8 linear implementation.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| in_features: int, | ||
| out_features: int, | ||
| out_dtype: torch.dtype = torch.float16, | ||
| ): | ||
| self.in_features = in_features | ||
| self.out_features = out_features | ||
| self.out_dtype = out_dtype | ||
|
|
||
| def forward( | ||
| self, | ||
| x: torch.Tensor, | ||
| weight: torch.Tensor, | ||
| input_scale: torch.Tensor, | ||
| weight_scale: torch.Tensor, | ||
| bias: torch.Tensor | None = None, | ||
| all_reduce: bool = False, | ||
| group: dist.ProcessGroup | None = None, | ||
| rank: int = 0, | ||
| scatter_size: list[int] | None = None, | ||
| ): | ||
| """Run static FP8 linear.""" | ||
| output_dtype = self.out_dtype or x.dtype | ||
|
|
||
| output = matmul_kernel_static_quant( | ||
| x, | ||
| weight, | ||
| input_scale, | ||
| weight_scale, | ||
| bias=bias, | ||
| output_dtype=output_dtype, | ||
| ) | ||
|
|
||
| if all_reduce: | ||
| if scatter_size is not None: | ||
| output = dist.reduce_scatter_by_tp_sizes( | ||
| output, | ||
| rank, | ||
| scatter_size, | ||
| group=group, | ||
| ) | ||
| else: | ||
| dist.all_reduce(output, group=group) | ||
|
|
||
| return output | ||
|
|
||
|
|
||
| class TritonLinearStaticF8Builder(LinearStaticF8Builder): | ||
| """Triton static per-tensor FP8 linear builder.""" | ||
|
|
||
| @staticmethod | ||
| def build( | ||
| in_features: int, | ||
| out_features: int, | ||
| bias: bool = True, | ||
| dtype: torch.dtype | None = None, | ||
| ): | ||
| """Build static FP8 linear implementation.""" | ||
| return TritonLinearStaticF8Impl( | ||
| in_features, | ||
| out_features, | ||
| out_dtype=dtype, | ||
| ) |
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Copyright (c) OpenMMLab. All rights reserved. | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| import torch | ||
| import torch.distributed as dist | ||
|
|
||
|
|
||
| class LinearStaticF8Impl(ABC): | ||
| """Static per-tensor FP8 linear implementation API.""" | ||
|
|
||
| def update_weights( | ||
| self, | ||
| weight: torch.Tensor, | ||
| input_scale: torch.Tensor, | ||
| weight_scale: torch.Tensor, | ||
| bias: torch.Tensor | None = None, | ||
| ): | ||
| """Update weights.""" | ||
| return weight, input_scale, weight_scale, bias | ||
|
|
||
| @abstractmethod | ||
| def forward( | ||
| self, | ||
| x: torch.Tensor, | ||
| weight: torch.Tensor, | ||
| input_scale: torch.Tensor, | ||
| weight_scale: torch.Tensor, | ||
| bias: torch.Tensor | None = None, | ||
| all_reduce: bool = False, | ||
| group: dist.ProcessGroup | None = None, | ||
| rank: int = 0, | ||
| scatter_size: list[int] | None = None, | ||
| ): | ||
| """Run static FP8 linear.""" | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| class LinearStaticF8Builder(ABC): | ||
| """Static per-tensor FP8 linear builder.""" | ||
|
|
||
| @staticmethod | ||
| @abstractmethod | ||
| def build( | ||
| in_features: int, | ||
| out_features: int, | ||
| bias: bool = True, | ||
| dtype: torch.dtype | None = None, | ||
| ): | ||
| """Build static FP8 linear implementation.""" | ||
| raise NotImplementedError |
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Copyright (c) OpenMMLab. All rights reserved. | ||
|
|
||
| from .default import DefaultModelConfigBuilder | ||
|
|
||
|
|
||
| class Hy3ModelConfigBuilder(DefaultModelConfigBuilder): | ||
| """Model config builder for Hy3.""" | ||
|
|
||
| @classmethod | ||
| def condition(cls, hf_config): | ||
| """Match Hy3 configurations.""" | ||
| return hf_config.model_type == 'hy_v3' |
Oops, something went wrong.
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.
if hy3 shares the same configuration behaviour as default, the config builder is not necessary.