Skip to content

Update converter.py#956

Merged
gushiqiao merged 2 commits intomainfrom
gushiqiao-patch-1
Mar 20, 2026
Merged

Update converter.py#956
gushiqiao merged 2 commits intomainfrom
gushiqiao-patch-1

Conversation

@gushiqiao
Copy link
Contributor

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the model quantization utility by providing more fine-grained control over which parts of a model are quantized. It introduces a mechanism to explicitly prevent certain tensors from being quantized based on their names, which is crucial for maintaining performance or compatibility for specific model components. The changes also streamline the process of overriding default quantization settings through command-line arguments, making the tool more flexible for various model conversion scenarios.

Highlights

  • Selective Quantization Skipping: Introduced a new ignore_quant_keys parameter and corresponding logic within the quantize_model function. This allows specific tensors to be excluded from quantization based on their keys, while still retaining them in the model, potentially converting their data type if necessary.
  • Command-Line Argument for Quantization Exclusion: Added a --ignore-quant-keys command-line argument to the converter.py script. Users can now provide a comma-separated list of substrings; any tensor key containing these substrings will not be quantized.
  • Enhanced Quantization Parameter Overrides: Improved the argument parsing for quantization settings, allowing command-line arguments such as --key-idx, --target-keys, --ignore-keys, and the new --ignore-quant-keys to override default model-specific quantization parameters.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new feature to skip quantization for specified tensor keys, which is a useful addition. The implementation is generally sound, but I've identified a couple of areas where the code can be improved for better maintainability and clarity. Specifically, I've suggested refactoring a block of code in quantize_model to eliminate duplication and simplifying the argument parsing logic in the main function. Addressing these points will make the code cleaner and more robust.

Comment on lines +385 to 398
# ignore_quant_keys: keep tensor but skip quantization when key matches.
if ignore_quant_keys is not None and any(ig_q in key for ig_q in ignore_quant_keys):
original_tensor_size = tensor.numel() * tensor.element_size()
original_size += original_tensor_size
if tensor.dtype != non_linear_dtype:
weights[key] = tensor.to(non_linear_dtype)
non_quantized_size += weights[key].numel() * weights[key].element_size()
else:
non_quantized_size += tensor.numel() * tensor.element_size()
continue

# try:
original_tensor_size = tensor.numel() * tensor.element_size()
original_size += original_tensor_size
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This logic can be refactored to remove code duplication and improve clarity. The calculation of original_size is performed in two separate places. By calculating it once for all quantization candidates, the code becomes cleaner and easier to maintain. Additionally, the logic for updating non_quantized_size is also duplicated elsewhere in the function and could be a candidate for further refactoring into a helper method.

Suggested change
# ignore_quant_keys: keep tensor but skip quantization when key matches.
if ignore_quant_keys is not None and any(ig_q in key for ig_q in ignore_quant_keys):
original_tensor_size = tensor.numel() * tensor.element_size()
original_size += original_tensor_size
if tensor.dtype != non_linear_dtype:
weights[key] = tensor.to(non_linear_dtype)
non_quantized_size += weights[key].numel() * weights[key].element_size()
else:
non_quantized_size += tensor.numel() * tensor.element_size()
continue
# try:
original_tensor_size = tensor.numel() * tensor.element_size()
original_size += original_tensor_size
# All tensors reaching this point are considered for quantization.
# We calculate their original size before deciding to quantize or skip.
original_tensor_size = tensor.numel() * tensor.element_size()
original_size += original_tensor_size
# ignore_quant_keys: keep tensor but skip quantization when key matches.
if ignore_quant_keys is not None and any(ig_q in key for ig_q in ignore_quant_keys):
if tensor.dtype != non_linear_dtype:
weights[key] = tensor.to(non_linear_dtype)
non_quantized_size += weights[key].numel() * weights[key].element_size()
else:
non_quantized_size += tensor.numel() * tensor.element_size()
continue
# try:

Comment on lines +965 to +969
ignore_quant_keys_ov = _parse_csv_override(args.ignore_quant_keys_override)
if args.ignore_quant_keys_override is not None:
args.ignore_quant_keys = ignore_quant_keys_ov
else:
args.ignore_quant_keys = None
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This block of code can be simplified. The if/else structure is redundant because in both branches, args.ignore_quant_keys is effectively assigned the result of _parse_csv_override(args.ignore_quant_keys_override). You can replace these five lines with a single assignment.

Suggested change
ignore_quant_keys_ov = _parse_csv_override(args.ignore_quant_keys_override)
if args.ignore_quant_keys_override is not None:
args.ignore_quant_keys = ignore_quant_keys_ov
else:
args.ignore_quant_keys = None
args.ignore_quant_keys = _parse_csv_override(args.ignore_quant_keys_override)

@gushiqiao gushiqiao merged commit a75f590 into main Mar 20, 2026
2 checks passed
@gushiqiao gushiqiao deleted the gushiqiao-patch-1 branch March 20, 2026 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants