Skip to content

Fix fp8 kv replicate #349

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion QEfficient/transformers/quantizers/quant_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class FP8DeQuantLinearToLinearTransform(ModuleMutatorTransform):
@classmethod
def mutate(cls, original_module, parent_module):
# -- de-quantizing the weights --
dequant_weights = original_module.weight.to(torch.float32) * original_module.weight_scale
dequant_weights = original_module.weight.to(torch.float32) # * original_module.weight_scale
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this removed?

dequant_linear_layer = nn.Linear(
original_module.in_features, original_module.out_features, bias=original_module.bias is not None
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def for_fp8_layer(cls, in_features, out_features, activation_quantization_strate
def forward(self, x):
# Only inference supported
with torch.no_grad():
dequantized_weights = self.weight.to(torch.float32) * self.weight_scale
dequantized_weights = self.weight.to(torch.float32) # * self.weight_scale
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed?

out = torch.matmul(x.float(), dequantized_weights.T)
out = out + self.bias if self.bias is not None else out

Expand Down
4 changes: 4 additions & 0 deletions scripts/replicate_kv_head/replicate_kv_heads.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def duplicate_weights_for_linear_layer(
layer.weight.data = torch.repeat_interleave(
layer.weight.data.view(orig_kv_heads, head_dim, hidden_size), repeat, 0
).view(new_kv_heads * head_dim, hidden_size)
if layer.bias is not None:
layer.bias.data = torch.repeat_interleave(layer.bias.data.view(orig_kv_heads, head_dim), repeat, 0).view(
new_kv_heads * head_dim
)
Comment on lines +56 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM



def main(args):
Expand Down
Loading