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 group quantization shape infer #1273

Merged
merged 1 commit into from
Nov 16, 2023
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
25 changes: 9 additions & 16 deletions python/mlc_chat/compiler/quantization/group_quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,11 @@ def __init__( # pylint: disable=too-many-arguments
self.out_features = out_features
self.out_dtype = out_dtype
self.config = config
num_group = tir.ceildiv(in_features, config.group_size)
self.q_weight = nn.Parameter(
(out_features, tir.ceildiv(in_features, config.num_elem_per_storage)),
config.storage_dtype,
)
self.q_scale = nn.Parameter(
(out_features, tir.ceildiv(in_features, config.group_size)), config.model_dtype
(out_features, config.num_storage_per_group * num_group), config.storage_dtype
)
self.q_scale = nn.Parameter((out_features, num_group), config.model_dtype)
if bias:
self.bias = nn.Parameter((out_features,), config.model_dtype)
else:
Expand Down Expand Up @@ -370,14 +368,12 @@ def __init__( # pylint: disable=too-many-arguments
self.out_features = out_features
self.out_dtype = out_dtype
self.config = config
num_group = tir.ceildiv(in_features, config.group_size)
self.q_weight = nn.Parameter(
(self.total_out_features, tir.ceildiv(in_features, config.num_elem_per_storage)),
(self.total_out_features, config.num_storage_per_group * num_group),
config.storage_dtype,
)
self.q_scale = nn.Parameter(
(self.total_out_features, tir.ceildiv(in_features, config.group_size)),
config.model_dtype,
)
self.q_scale = nn.Parameter((self.total_out_features, num_group), config.model_dtype)
if bias:
self.bias = nn.Parameter((self.total_out_features,), config.model_dtype)
else:
Expand Down Expand Up @@ -456,14 +452,11 @@ def __init__(self, num: int, dim: int, config: GroupQuantize):
self.num = num
self.dim = dim
self.config = config
num_group = tir.ceildiv(dim, config.group_size)
self.q_weight = nn.Parameter(
(num, tir.ceildiv(dim, config.num_elem_per_storage)),
config.storage_dtype,
)
self.q_scale = nn.Parameter(
(num, tir.ceildiv(dim, config.group_size)),
config.model_dtype,
(num, config.num_storage_per_group * num_group), config.storage_dtype
)
self.q_scale = nn.Parameter((num, num_group), config.model_dtype)

@staticmethod
def from_embedding(embedding: nn.Embedding, config: GroupQuantize) -> "GroupQuantizeEmbedding":
Expand Down
7 changes: 3 additions & 4 deletions tests/python/quantization/test_group_quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,13 @@ def forward(self, x: nn.Tensor):

config = QUANTIZATION[quant_name]
assert isinstance(config, GroupQuantize)
num_group = -(shape[1] // -config.group_size)
weight_np = np.random.randint(
np.iinfo(config.storage_dtype).min,
np.iinfo(config.storage_dtype).max,
(shape[0], -(shape[1] // -config.num_elem_per_storage)),
(shape[0], config.num_storage_per_group * num_group),
).astype(config.storage_dtype)
scale_np = np.random.random((shape[0], -(shape[1] // -config.group_size))).astype(
config.model_dtype
)
scale_np = np.random.random((shape[0], num_group)).astype(config.model_dtype)
mod = config.quantize_model(Test(), QuantizeMapping({}, {}), "")
mod.linear.q_weight.data = weight_np
mod.linear.q_scale.data = scale_np
Expand Down