Skip to content

[BugFix]Fix add_bias and with_bias #3462

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions fastdeploy/model_executor/layers/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def process_loaded_weights(self, layer, weights) -> None:
def apply(self, layer: nn.Layer, x: paddle.Tensor) -> paddle.Tensor:

linear_out = paddle.matmul(x, layer.weight)
if layer.with_bias:
if layer.with_bias and layer.add_bias:
linear_out = paddle.add(linear_out, layer.bias)
return linear_out

Expand Down Expand Up @@ -650,7 +650,7 @@ def __init__(
input_size: int = None,
output_size: int = None,
with_bias: bool = False,
add_bias: bool = False,
add_bias: bool = True,
reduce_results: bool = True,
skip_quant: bool = False,
):
Expand Down Expand Up @@ -717,6 +717,8 @@ def forward_cuda(self, x: paddle.Tensor) -> paddle.Tensor:
out = self.quant_method.apply(self, x)
else:
out = paddle.matmul(x, self.weight)
if self.with_bias and self.add_bias:
out = paddle.add(out, self.bias)

if self.reduce_results and self.nranks > 1:
tensor_model_parallel_all_reduce(out)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,6 @@ def apply(self, layer, x):
(layer.weight, layer.weight_scale),
linear_out,
)
if layer.with_bias:
if layer.with_bias and layer.add_bias:
linear_out = paddle.add(linear_out, layer.bias)
return linear_out
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def apply(self, layer, x):
layer.weight,
transpose_x=False,
transpose_y=True,
bias=None,
bias=layer.bias if layer.add_bias and layer.with_bias else None,
scale=self.total_scale,
output_dtype="bfloat16",
activation_type="identity",
Expand Down
2 changes: 1 addition & 1 deletion fastdeploy/model_executor/layers/quantization/w4afp8.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def apply(self, layer, x):
layer.weight,
layer.weight_scale,
zero_points=None,
bias=layer.bias if layer.add_bias else None,
bias=layer.bias if layer.add_bias and layer.with_bias else None,
out_scale=self.quant_config.weight_scale_dict.get(layer.prefix + ".weight_scale")
/ (
self.quant_config.act_scale_dict.get(layer.prefix + ".activation_scale")
Expand Down
4 changes: 2 additions & 2 deletions fastdeploy/model_executor/layers/quantization/weight_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def apply(self, layer, x):
linear_out = weight_only_linear(
x,
weight=layer.weight,
bias=layer.bias if layer.add_bias else None,
bias=layer.bias if layer.add_bias and layer.with_bias else None,
weight_scale=layer.weight_scale,
weight_dtype=("int8" if self.quant_config.name() == "wint8" else "int4"),
arch=80,
Expand All @@ -219,7 +219,7 @@ def apply(self, layer, x):
linear_out = weight_only_linear(
x,
weight=layer.weight,
bias=layer.bias if layer.add_bias else None,
bias=layer.bias if layer.add_bias and layer.with_bias else None,
weight_scale=layer.weight_scale,
weight_dtype=("int8" if self.quant_config.name() == "wint8" else "int4"),
arch=self.quant_config.weight_only_linear_arch,
Expand Down
2 changes: 1 addition & 1 deletion fastdeploy/model_executor/layers/quantization/wfp8afp8.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def apply(self, layer, x):
a_scales,
layer.weight_scale,
out_type,
layer.bias,
layer.bias if layer.add_bias and layer.with_bias else None,
)
else:
raise NotImplementedError
Expand Down
Loading