-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[Feature] Support float8 dtype storage and deepseek v3 with fp8 inference. #9906
Open
ZHUI
wants to merge
18
commits into
PaddlePaddle:develop
Choose a base branch
from
ZHUI:support_fp8_storage
base: develop
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.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3189083
suppport float8 dtype.
ZHUI c17aa5f
fix
ZHUI f97937b
fix for empty shape.
ZHUI b9dbed9
Support load fp8 model.
ZHUI 60dab5f
debug
ZHUI 0f94c50
[DeepSeek] Support for float8 loading and inference.
ZHUI 10bb3a6
Add paddle patch.
ZHUI 72f3013
refine safetensors.
ZHUI 91f43cc
refine code.
ZHUI c536cc3
Merge remote-tracking branch 'upstream/develop' into support_fp8_storage
ZHUI e248bf8
Fix moe dtype error.
ZHUI bda020c
fix paddle.Tensor.
ZHUI c16b2f3
fix kwargs with patch api.
ZHUI eb5f238
fix for fast safe open
ZHUI 93b3c41
fix merge kit.
ZHUI 534f134
fix inference.
ZHUI 92b0a94
remove set device.
ZHUI be36ba2
Merge branch 'develop' into support_fp8_storage
ZHUI 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
import paddle | ||
|
||
original_linear = paddle.nn.functional.linear | ||
|
||
from typing import Literal, Optional | ||
|
||
# from ..linear_utils import RowParallelLinear as PD_RowParallelLinear | ||
from ..linear_utils import ColumnParallelLinear as PD_ColumnParallelLinear | ||
from ..linear_utils import ( | ||
ColumnSequenceParallelLinear as PD_ColumnSequenceParallelLinear, | ||
) | ||
from ..linear_utils import Linear as PD_Linear | ||
from ..linear_utils import RowParallelLinear as PD_RowParallelLinear | ||
from ..linear_utils import RowSequenceParallelLinear as PD_RowSequenceParallelLinear | ||
|
||
try: | ||
from .kernel import act_quant, fp8_gemm, weight_dequant | ||
except: | ||
pass | ||
|
||
|
||
__all__ = [ | ||
"Linear", | ||
"ColumnParallelLinear", | ||
"RowParallelLinear", | ||
"ColumnSequenceParallelLinear", | ||
"RowSequenceParallelLinear", | ||
] | ||
|
||
gemm_impl: Literal["bf16", "fp8"] = "bf16" | ||
block_size = 128 | ||
|
||
|
||
def fp8_linear( | ||
x: paddle.Tensor, weight: paddle.Tensor, bias: Optional[paddle.Tensor] = None, name=None | ||
) -> paddle.Tensor: | ||
""" | ||
Applies a linear transformation to the incoming data: y = xA^T + b. | ||
This function supports specialized implementations based on quantization | ||
and tensor formats. | ||
|
||
Args: | ||
x (paddle.Tensor): The input tensor. | ||
weight (paddle.Tensor): The weight tensor. It may be quantized and | ||
requires dequantization for certain cases. | ||
bias (Optional[paddle.Tensor]): The bias tensor to be added. Default is None. | ||
|
||
Returns: | ||
paddle.Tensor: The result of the linear transformation, which may involve | ||
quantization-aware computations depending on the input parameters. | ||
|
||
Notes: | ||
- If `weight` is quantized (e.g., `element_size() == 1`), a dequantized version | ||
is used for computation. | ||
- If `gemm_impl == "bf16"`, dequantization and a `bf16` GEMM operation are applied. | ||
- For other cases, the function applies quantization to `x` and uses `fp8_gemm` for computation. | ||
""" | ||
|
||
if paddle.in_dynamic_mode(): | ||
if weight.element_size() > 1: | ||
return original_linear(x, weight, bias) | ||
elif gemm_impl == "bf16": | ||
weight = weight_dequant(weight, weight._scale) | ||
return original_linear(x, weight, bias) | ||
else: | ||
x, scale = act_quant(x, block_size) | ||
y = fp8_gemm(x, scale, weight, weight._scale) | ||
if bias is not None: | ||
y += bias | ||
return y | ||
else: | ||
return original_linear(x, weight, bias) | ||
|
||
|
||
paddle.nn.functional.linear = fp8_linear | ||
|
||
|
||
def register_scale(self): | ||
if self.weight.element_size() == 1: | ||
in_features, out_features = self.weight.shape | ||
scale_out_features = (out_features + self.block_size - 1) // self.block_size | ||
scale_in_features = (in_features + self.block_size - 1) // self.block_size | ||
self.weight_scale_inv = self.create_parameter( | ||
shape=[scale_in_features, scale_out_features], | ||
attr=self._weight_attr, | ||
dtype="float32", | ||
is_bias=False, | ||
) | ||
self.weight._scale = self.weight_scale_inv | ||
|
||
|
||
class Linear(PD_Linear): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.block_size = kwargs.get("block_size", 128) | ||
register_scale(self) | ||
|
||
|
||
class ColumnParallelLinear(PD_ColumnParallelLinear): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.block_size = kwargs.get("block_size", 128) | ||
register_scale(self) | ||
|
||
|
||
class RowParallelLinear(PD_RowParallelLinear): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.block_size = kwargs.get("block_size", 128) | ||
register_scale(self) | ||
|
||
|
||
class ColumnSequenceParallelLinear(PD_ColumnSequenceParallelLinear): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.block_size = kwargs.get("block_size", 128) | ||
register_scale(self) | ||
|
||
|
||
class RowSequenceParallelLinear(PD_RowSequenceParallelLinear): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.block_size = kwargs.get("block_size", 128) | ||
register_scale(self) | ||
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.
这里替换__call__函数的原因是什么?
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.
paddle.Tensor 不支持 初始化 FP8 tensor,临时切换接口支持。