-
Notifications
You must be signed in to change notification settings - Fork 729
Add the IQ2_XXS weight-only quantization format #2511
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
+1,558
−129
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a3052f9
Add the IQ2_XXS weight-only quantization format
cjluo-nv f6f658c
Carry IQ block metadata through HF config conversion for every format
cjluo-nv 37ae39c
Cover the mixed IQ export path and correct the changelog
cjluo-nv cecb5d6
Run the Megatron IQ export tests for every format
cjluo-nv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,6 @@ | |
| from safetensors.torch import save_file | ||
|
|
||
| from modelopt import __version__ | ||
| from modelopt.torch.quantization.ggml import quantize_iq1_s, quantize_iq2_xs | ||
| from modelopt.torch.quantization.nn.modules.tensor_quantizer import GroupedQuantizer | ||
| from modelopt.torch.utils import import_plugin, warn_rank_0 | ||
| from modelopt.torch.utils.plugins.hf_checkpoint_utils import ( | ||
|
|
@@ -57,13 +56,13 @@ | |
| ) | ||
| from .plugins.megatron_importer import GPTModelImporter, _get_mamba_conv1d | ||
| from .quant_format import ( | ||
| IQ_FORMATS, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have unit tests for megatron as well? |
||
| IQ_PACKERS, | ||
| KV_CACHE_FP8, | ||
| KV_CACHE_NVFP4, | ||
| QUANTIZATION_FP8, | ||
| QUANTIZATION_FP8_PB_REAL, | ||
| QUANTIZATION_FP8_PB_WO, | ||
| QUANTIZATION_IQ1_S, | ||
| QUANTIZATION_IQ2_XS, | ||
| QUANTIZATION_NONE, | ||
| QUANTIZATION_NVFP4, | ||
| QUANTIZATION_W4A16_NVFP4, | ||
|
|
@@ -85,6 +84,7 @@ | |
| import transformers | ||
| from transformers import AutoProcessor | ||
|
|
||
|
|
||
| has_mcore = False | ||
| with import_plugin("megatron"): | ||
| from megatron.core.models.gpt import GPTModel | ||
|
|
@@ -351,7 +351,7 @@ def save_pretrained( | |
| quantization = "NVFP4" | ||
| elif quantization_format == QUANTIZATION_W4A16_NVFP4: | ||
| quantization = "W4A16_NVFP4" | ||
| elif quantization_format in (QUANTIZATION_IQ1_S, QUANTIZATION_IQ2_XS): | ||
| elif quantization_format in IQ_FORMATS: | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| quantization = quantization_format.upper() | ||
|
|
||
| if is_last_stage_main_rank: | ||
|
|
@@ -1115,7 +1115,7 @@ def _get_quantized_state( | |
| self._record_excluded_module(prefix) | ||
| block_size = get_weight_block_size(module) | ||
|
|
||
| is_iq = qformat in (QUANTIZATION_IQ1_S, QUANTIZATION_IQ2_XS) | ||
| is_iq = qformat in IQ_FORMATS | ||
| name_to_value = self._get_weight_bias( | ||
| module, dtype, name_to_value, keep_weight_device=is_iq | ||
| ) | ||
|
|
@@ -1185,7 +1185,7 @@ def _get_weight_scales(self, quantized_state: dict[str, Any], qformat: str): | |
| @staticmethod | ||
| def _pack_iq_weight(weight: torch.Tensor, qformat: str) -> torch.Tensor: | ||
| """Pack one ``[out, in]`` weight and return its CPU payload.""" | ||
| quantize_iq = quantize_iq1_s if qformat == QUANTIZATION_IQ1_S else quantize_iq2_xs | ||
| quantize_iq = IQ_PACKERS[qformat] | ||
| packed_weight, _ = quantize_iq(weight) | ||
| return packed_weight.detach().cpu() | ||
|
|
||
|
|
@@ -1210,7 +1210,7 @@ def _reject_unsupported_fused_iq_export(qformat: str) -> None: | |
| The one gap left is a rank holding no local expert at all, which needs expert-parallel | ||
| size to exceed the expert count. Worth revisiting if that becomes a supported topology. | ||
| """ | ||
| if qformat in (QUANTIZATION_IQ1_S, QUANTIZATION_IQ2_XS): | ||
| if qformat in IQ_FORMATS: | ||
| raise NotImplementedError( | ||
| "Fused-MoE IQ export requires a deployment loader that supports " | ||
| "[num_experts, out_features, in_features // 256, payload_bytes]" | ||
|
|
@@ -1280,7 +1280,7 @@ def _name_remapping( | |
| weight = weight + 1.0 | ||
| weight_scale, weight_scale_2 = self._get_weight_scales(name_to_value, qformat) | ||
|
|
||
| if qformat in (QUANTIZATION_IQ1_S, QUANTIZATION_IQ2_XS): | ||
| if qformat in IQ_FORMATS: | ||
| self._state_dict.update(self._get_iq_weight_state(prefix + "weight", weight, qformat)) | ||
| elif weight_scale is None: | ||
| self._state_dict[prefix + "weight"] = weight | ||
|
|
@@ -1327,7 +1327,7 @@ def _gated_mlp_slicing( | |
| gate_proj_weight = weight[:ffn_hidden_size, :] | ||
| up_proj_weight = weight[ffn_hidden_size:, :] | ||
|
|
||
| if qformat in (QUANTIZATION_IQ1_S, QUANTIZATION_IQ2_XS): | ||
| if qformat in IQ_FORMATS: | ||
| self._state_dict.update( | ||
| self._get_iq_weight_state(gate_proj_prefix + "weight", gate_proj_weight, qformat) | ||
| ) | ||
|
|
@@ -1501,7 +1501,7 @@ def _grouped_mlp_slicing( | |
| seen_qformat, seen_block_size = qformat, block_size | ||
|
|
||
| weight = state_dict[weight_key].to(self.dtype) | ||
| if qformat not in (QUANTIZATION_IQ1_S, QUANTIZATION_IQ2_XS): | ||
| if qformat not in IQ_FORMATS: | ||
| weight = weight.cpu() | ||
| weight_scale_cpu = ( | ||
| weight_scale.detach().cpu().clone() if weight_scale is not None else None | ||
|
|
@@ -1533,7 +1533,7 @@ def _grouped_mlp_slicing( | |
| ] | ||
|
|
||
| for shard_prefix, shard_weight, shard_scale in shards: | ||
| if qformat in (QUANTIZATION_IQ1_S, QUANTIZATION_IQ2_XS): | ||
| if qformat in IQ_FORMATS: | ||
| local_expert_state.update( | ||
| self._get_iq_weight_state( | ||
| shard_prefix + "weight", shard_weight, qformat | ||
|
|
@@ -1702,7 +1702,7 @@ def _take(tensor, index, last_dim, with_gate=False): | |
| proj_weights = [_take(weight, s, hidden_size, g) for s, g in zip(slices, gated)] | ||
| proj_keys = [p + "weight" for p in prefixes] | ||
|
|
||
| if qformat in (QUANTIZATION_IQ1_S, QUANTIZATION_IQ2_XS): | ||
| if qformat in IQ_FORMATS: | ||
| for key, weight in zip(proj_keys, proj_weights): | ||
| self._state_dict.update(self._get_iq_weight_state(key, weight, qformat)) | ||
| elif weight_scale is None: | ||
|
|
@@ -1820,7 +1820,7 @@ def _gated_delta_net_slicing(self, module, prefix, is_mtp=False): | |
| proj_keys = [p + "weight" for p in proj_prefixes] | ||
| weight_scale, weight_scale_2 = self._get_weight_scales(name_to_value, qformat) | ||
|
|
||
| if qformat in (QUANTIZATION_IQ1_S, QUANTIZATION_IQ2_XS): | ||
| if qformat in IQ_FORMATS: | ||
| for proj_prefix, proj_weight in zip(proj_prefixes, proj_weights): | ||
| if proj_prefix in keep_bf16: | ||
| self._state_dict[proj_prefix + "weight"] = proj_weight.cpu() | ||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new metadata reaches
get_quant_config, butconvert_hf_config.pystill recognizes only("IQ1_S", "IQ2_XS")in bothconvert_hf_quant_config_formatand_quant_algo_to_group_config. Consequently a uniform IQ2_XXS export dropspacking,block_payload_bytes,effective_bits, andgroup_size; mixed exports warn and omit those fields from the IQ2_XXS config group. Extend both conversion paths, preferably using shared IQ metadata rather than another format list. Add IQ2_XXS totest_iq_quantization_configand cover mixed conversion and rejection of a non-256 group size.