Skip to content

compare prepared vs. converted outputs for Embedding #2087

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
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
11 changes: 8 additions & 3 deletions test/quantization/test_qat.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,8 @@ def test_qat_4w_embedding(self):
_quantized_decomposed_quantize_per_channel_group_wrapper,
)
from torchao.quantization.qat import Int4WeightOnlyEmbeddingQATQuantizer
from torchao.quantization.utils import compute_error


group_size = 256
model = M2()
Expand All @@ -816,9 +818,9 @@ def test_qat_4w_embedding(self):
quantizer = Int4WeightOnlyEmbeddingQATQuantizer(group_size)
prepared = quantizer.prepare(model)
prepared_embedding_weight = copy.deepcopy(prepared.embedding.weight)
prepared(*x)
converted = quantizer.convert(model)
converted(*x)
prepared_output = prepared(*x)
converted = quantizer.convert(copy.deepcopy(prepared))
converted_output = converted(*x)

# Assert the scales, zero points, and weights are correct after convert
qmin, qmax = -8, 7
Expand All @@ -837,9 +839,12 @@ def test_qat_4w_embedding(self):
torch.int8,
group_size,
)
sqnr = compute_error(prepared_output, converted_output).detach().item()
torch.testing.assert_close(converted.embedding.weight, q_weight)
torch.testing.assert_close(converted.embedding.scale, s)
torch.testing.assert_close(converted.embedding.zero_point, zp)
torch.testing.assert_close(sqnr, float('inf'))


def test_fake_quantize_config_granularity(self):
"""
Expand Down
5 changes: 4 additions & 1 deletion torchao/quantization/qat/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ def _convert_helper(self, module: torch.nn.Module):
group_size=group_size,
scale_precision=scale_precision,
zero_point_precision=zero_point_precision,
weight_original_precision=child.weight.dtype,
device=child.weight.device,
)
setattr(module, name, quantized_embedding)
Expand Down Expand Up @@ -336,6 +337,7 @@ def __init__(
group_size: int = 32,
scale_precision: torch.dtype = torch.float32,
zero_point_precision: torch.dtype = torch.int32,
weight_original_precision: torch.dtype = torch.float32,
device: torch.device = None,
):
super().__init__()
Expand All @@ -354,6 +356,7 @@ def __init__(
self.group_size = group_size
self.scale_precision = scale_precision
self.zero_point_precision = zero_point_precision
self.weight_original_precision = weight_original_precision

# currently storing unpacked int8 weights
self.register_buffer(
Expand Down Expand Up @@ -393,7 +396,7 @@ def forward(self, x):
qmax,
torch.int8,
self.group_size,
x.dtype,
self.weight_original_precision,
)
return F.embedding(
x,
Expand Down
Loading