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 (llm): add checks for group dimensions #751

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/brevitas_examples/llm/llm_quant/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def pack_int_weights(self, bit_width, int_weights):
if bit_width == 8:
return int_weights
elif bit_width == 4 or bit_width == 2:
assert int_weights.shape[1] * bit_width % 8 == 0, "Number of columns multiplied by the bit-width must be a multiple of 8"
packed_int_weights = torch.zeros(
(int_weights.shape[0], int_weights.shape[1] * bit_width // 8),
device=int_weights.device,
Expand Down
12 changes: 6 additions & 6 deletions src/brevitas_examples/llm/llm_quant/quantizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ class WeightSymmetricGroupQuantMixin(ExtendedInjector):
@value
def expanded_scaling_shape(module, block_size):
if isinstance(module, nn.Conv2d):
return module.weight.size(0), module.weight.size(1) // block_size, block_size, module.weight.size(2), module.weight.size(3)
return module.weight.size(0), (module.weight.size(1) + block_size - 1) // block_size, block_size, module.weight.size(2), module.weight.size(3)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's just put an assert to check that the block size divides size(1) of the weights.

elif isinstance(module, nn.Linear):
return module.weight.size(0), module.weight.size(1) // block_size, block_size
return module.weight.size(0), (module.weight.size(1) + block_size - 1) // block_size, block_size
elif isinstance(module, nn.Embedding):
return module.weight.size(0), module.weight.size(1) // block_size, block_size
return module.weight.size(0), (module.weight.size(1) + block_size - 1) // block_size, block_size
else:
raise RuntimeError("Module not supported.")

@value
def scaling_shape(module, block_size):
if isinstance(module, nn.Conv2d):
return module.weight.size(0), module.weight.size(1) // block_size, 1, module.weight.size(2), module.weight.size(3)
return module.weight.size(0), (module.weight.size(1) + block_size - 1) // block_size, 1, module.weight.size(2), module.weight.size(3)
elif isinstance(module, nn.Linear):
return module.weight.size(0), module.weight.size(1) // block_size, 1
return module.weight.size(0), (module.weight.size(1) + block_size - 1) // block_size, 1
elif isinstance(module, nn.Embedding):
return module.weight.size(0), module.weight.size(1) // block_size, 1
return module.weight.size(0), (module.weight.size(1) + block_size - 1) // block_size, 1
else:
raise RuntimeError("Module not supported.")

Expand Down
Loading