Skip to content
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

Fix fp16 overflow #532

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions awq/quantize/quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def __init__(
)

def pseudo_quantize_tensor(self, w: torch.Tensor):
org_w_dtype = w.dtype
w = w.float()
org_w_shape = w.shape
if self.group_size > 0:
assert org_w_shape[-1] % self.group_size == 0
Expand Down Expand Up @@ -105,6 +107,9 @@ def pseudo_quantize_tensor(self, w: torch.Tensor):
scales = scales.view(org_w_shape[0], -1)
w = w.reshape(org_w_shape)

w, scales, zeros = w.to(org_w_dtype), scales.to(org_w_dtype), (
zeros.to(org_w_dtype) if zeros is not None else None
)
return w, scales, zeros

def pseudo_dequantize_tensor(
Expand Down Expand Up @@ -386,6 +391,8 @@ def _compute_best_scale(
# avoid scaling values that overflow
scales[torch.isinf(scales)] = 1
scales[torch.isnan(scales)] = 1
for fc in linears2scale:
scales_view[torch.isinf(fc.weight.mul(scales_view)).sum(dim=0).unsqueeze(0) > 0] = 1

# Q(W * s)
for fc in linears2scale:
Expand Down