Skip to content

primitive scale fix #2210

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 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion torchao/quantization/quant_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,9 @@ def _choose_qparams_affine(
f"Only symmetric quantization is supported for FP8 types, got {mapping_type}"
)

scale_device = None
if input is not None:
scale_device = input.device
if scale_dtype is None:
scale_dtype = input.dtype
if eps is None:
Expand Down Expand Up @@ -902,6 +904,8 @@ def _choose_qparams_affine(
if eps is None:
eps = torch.finfo(min_val.dtype).eps

scale_device = min_val.device

if preserve_zero:
min_val_neg = torch.min(min_val, torch.zeros_like(min_val))
max_val_pos = torch.max(max_val, torch.zeros_like(max_val))
Expand Down Expand Up @@ -948,7 +952,9 @@ def _choose_qparams_affine(
scale = torch.clamp(scale, min=eps)
else:
assert mapping_type == MappingType.ASYMMETRIC.name
scale = (max_val_pos - min_val_neg) / float(quant_max - quant_min)
scale = (max_val_pos - min_val_neg) / torch.tensor(
Copy link
Contributor

Choose a reason for hiding this comment

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

what if you did:
(max_val_pos - min_val_neg) / (quant_max - quant_min).to(torch.float32)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Casting to float32 doesn't help the discrepancy on CPU vs GPU.

Copy link
Contributor

@drisspg drisspg May 15, 2025

Choose a reason for hiding this comment

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

Ahh sorry I see that quant_max and quant_mins are ints, why is this better than the existing? Maybe a test might be helpful

import torch
from transformer_nuggets.utils.tracing import LoggingMode
a = torch.randn(2, 3, device="cuda")
with LoggingMode():
    out = a / float(32 - 12)
    print(out)


with LoggingMode():
    out = a / torch.tensor(float(32 - 12), dtype=a.dtype, device=a.device)
    print(out)
    

Produces:

$1: f32[2, 3] = aten.div.Tensor($0, 20.0)
tensor([[-0.0313, -0.0028, -0.0344],
       [ 0.0299,  0.0099, -0.0426]], device='cuda:0')
$0: f32[] = aten.lift_fresh.default($0)
$2: f32[2, 3] = aten.div.Tensor($1, $0)
tensor([[-0.0313, -0.0028, -0.0344],
       [ 0.0299,  0.0099, -0.0426]], device='cuda:0')

Copy link
Contributor

Choose a reason for hiding this comment

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

Driss, the issue is "float" as in python float is actually float64

Copy link
Contributor

Choose a reason for hiding this comment

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

So this isn't about fixing a device but about fixing the dtype?

float(quant_max - quant_min), dtype=scale_dtype, device=scale_device
)
scale = torch.clamp(scale, min=eps)
if zero_point_domain == ZeroPointDomain.NONE.name:
zero_point = None
Expand Down
Loading