Skip to content

create staticmethod for quantizing weights of QATLinear and QATEmbedding #2079

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

Merged
merged 1 commit into from
Apr 21, 2025
Merged
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
47 changes: 30 additions & 17 deletions torchao/quantization/qat/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

from typing import Any, Optional
from typing import Any, Optional, Tuple

import torch
import torch.nn.functional as F
Expand Down Expand Up @@ -196,15 +196,40 @@ def convert(
"""
self._convert_helper(model)
return model

@staticmethod
def quantize_weights(
weight: torch.Tensor,
bit_width: int,
group_size: int,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""
Helper function to quantize weights
"""
(qmin, qmax) = _get_qmin_qmax(bit_width)
(s, zp) = get_group_qparams_symmetric(
weight, bit_width, group_size
)
from torchao._executorch_ops import (
_quantized_decomposed_quantize_per_channel_group_wrapper,
)
q_weight = _quantized_decomposed_quantize_per_channel_group_wrapper(
weight,
s,
zp,
qmin,
qmax,
torch.int8,
group_size,
)
return (q_weight, s, zp)


def _convert_helper(self, module: torch.nn.Module):
"""
Helper function to recursively swap `Int4WeightOnlyQATEmbedding`
modules with `Int4WeightOnlyEmbedding`
"""
from torchao._executorch_ops import (
_quantized_decomposed_quantize_per_channel_group_wrapper,
)

for name, child in module.named_children():
if isinstance(child, Int4WeightOnlyQATEmbedding):
Expand All @@ -230,20 +255,8 @@ def _convert_helper(self, module: torch.nn.Module):
)
setattr(module, name, quantized_embedding)

q_weight, s, zp = self.quantize_weights(child.weight, self.bit_width, group_size)
# Load weights and qparams into quantized embedding
(qmin, qmax) = _get_qmin_qmax(self.bit_width)
(s, zp) = get_group_qparams_symmetric(
child.weight, self.bit_width, group_size
)
q_weight = _quantized_decomposed_quantize_per_channel_group_wrapper(
child.weight,
s,
zp,
qmin,
qmax,
torch.int8,
group_size,
)
quantized_embedding.weight = q_weight
quantized_embedding.scale = s.to(scale_precision)
quantized_embedding.zero_point = zp.to(zero_point_precision)
Expand Down
56 changes: 34 additions & 22 deletions torchao/quantization/qat/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

from typing import Any, Optional
from typing import Any, Optional, Tuple

import torch
import torch.nn.functional as F
Expand Down Expand Up @@ -197,6 +197,36 @@ def convert(
) -> torch.nn.Module:
self._convert_qat_linear_8da4w(model)
return model

@staticmethod
def quantize_weights(
weight: torch.Tensor,
group_size: int,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""
Helper function to quantize weights
"""
# Load weights and qparams into quantized linear
n_bit = 4
(qmin, qmax) = _get_qmin_qmax(n_bit)
(s, zp) = get_group_qparams_symmetric(
weight, n_bit, group_size
)
from torchao._executorch_ops import (
_quantized_decomposed_quantize_per_channel_group_wrapper,
)

q_weight = _quantized_decomposed_quantize_per_channel_group_wrapper(
weight,
s,
zp,
qmin,
qmax,
torch.int8,
group_size,
)
return (q_weight, s, zp)


def _convert_qat_linear_8da4w(self, module: torch.nn.Module):
"""
Expand All @@ -215,28 +245,10 @@ def _convert_qat_linear_8da4w(self, module: torch.nn.Module):
)
setattr(module, name, quantized_linear)

# Load weights and qparams into quantized linear
n_bit = 4
(qmin, qmax) = _get_qmin_qmax(n_bit)
(s, zp) = get_group_qparams_symmetric(
child.weight, n_bit, config.group_size
)
from torchao._executorch_ops import (
_quantized_decomposed_quantize_per_channel_group_wrapper,
)

q_weight = _quantized_decomposed_quantize_per_channel_group_wrapper(
child.weight,
s,
zp,
qmin,
qmax,
torch.int8,
config.group_size,
)
q_weight, scales, zeros = self.quantize_weights(child.weight, config.group_size)
quantized_linear.weight = q_weight
quantized_linear.scales = s
quantized_linear.zeros = zp
quantized_linear.scales = scales
quantized_linear.zeros = zeros
if child.bias is not None:
quantized_linear.bias = child.bias
else:
Expand Down
Loading