-
Notifications
You must be signed in to change notification settings - Fork 55
[WIP]support automatic mixed bits assignment #851
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
wenhuach21
wants to merge
59
commits into
main
Choose a base branch
from
auto_scheme
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+786
−385
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
@xin3he @n1ck-guo def compute_layer_bits(
layer: torch.nn.Module,
ignore_scale_zp_bits: bool = False,
) -> tuple[int, float]:
"""Compute total and average bitwidth for a single quantized layer.
Args:
layer: A PyTorch layer with quantization attributes.
ignore_scale_zp_bits: Whether to ignore scale/zero-point overhead.
Returns:
A tuple (total_bits, avg_bits) representing bit usage.
"""
weight = layer.weight
n_param = weight.numel()
weight_bits = getattr(layer, "bits", 16)
group_size = getattr(layer, "group_size", 128)
super_group_size = getattr(layer, "super_group_size", None)
super_weight_bits = getattr(layer, "super_bits", None)
# Unquantized layer or ignoring scale/zp overhead
if weight_bits >= 16 or ignore_scale_zp_bits:
if super_weight_bits is not None: # reset gguf 16 bits to 32 bits
return 32 * n_param, 32
return weight_bits * n_param, 16.0
in_features, out_features = get_layer_features(layer)
# Determine number of groups based on group size
if group_size > 0:
n_group = out_features * (in_features + group_size - 1) // group_size
elif group_size == 0:
n_group = 1
elif group_size == -1:
n_group = out_features
else:
raise ValueError(f"Invalid group_size {group_size}")
# Compute auxiliary bits (scales, zero-points, or double quantization)
aux_total_bits = 0
if not super_group_size:
scale_bits = 16
zp_bits = weight_bits
aux_total_bits = n_group * (scale_bits + zp_bits)
else:
aux_total_bits += n_group * super_weight_bits * 2
n_super_group = (n_group + super_group_size - 1) // super_group_size
aux_total_bits += n_super_group * 32 * 2 # 32-bit scale and min_v
total_bits = weight_bits * n_param + aux_total_bits
avg_bits = total_bits / n_param
return total_bits, avg_bits |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.