[WIP][Examples] model_free_ptq of nvidia/DeepSeek-R1-NVFP4#2228
[WIP][Examples] model_free_ptq of nvidia/DeepSeek-R1-NVFP4#2228brian-dellabetta wants to merge 17 commits intomainfrom
Conversation
Signed-off-by: Brian Dellabetta <bdellabe@redhat.com>
|
👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review. Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed. |
Summary of ChangesHello @brian-dellabetta, 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 introduces a new example script for applying model-free post-training quantization to the Highlights
🧠 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 AssistThe 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
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 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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This PR adds a new example script for model-free PTQ on the nvidia/DeepSeek-R1-NVFP4 model. The script correctly sets up the quantization scheme to apply FP8-Block quantization to specific self-attention layers. My main feedback is focused on improving the clarity and readability of the layer selection logic. The current implementation uses a complex regex in the ignore list, which is not intuitive. I've suggested either using the more idiomatic targets list or, if that's not possible, significantly improving the comments to make the current approach easier to understand. As this is an example script, clarity is paramount.
| model_free_ptq( | ||
| model_stub=MODEL_ID, | ||
| save_directory=SAVE_DIR, | ||
| scheme=QuantizationScheme( |
There was a problem hiding this comment.
Just use the pre-set scheme
There was a problem hiding this comment.
we need to set targets
There was a problem hiding this comment.
you can set targets outside of the scheme?
There was a problem hiding this comment.
This is the model_free_ptq API. I don't think so
There was a problem hiding this comment.
Cleaned this up a bit to use **FP8_BLOCK
|
Documentation update |
|
The quality checks have failed. Please run |
Signed-off-by: Brian Dellabetta <bdellabe@redhat.com>
|
The quality checks have failed. Please run |
Signed-off-by: Brian Dellabetta <bdellabe@redhat.com>
|
The quality checks have failed. Please run |
Signed-off-by: Brian Dellabetta <bdellabe@redhat.com>
| weights=QuantizationArgs( | ||
| num_bits=8, | ||
| type=QuantizationType.FLOAT, | ||
| strategy=QuantizationStrategy.BLOCK, | ||
| symmetric=True, | ||
| dynamic=False, | ||
| block_structure=[128, 128], | ||
| ), | ||
| input_activations=QuantizationArgs( | ||
| num_bits=8, | ||
| type=QuantizationType.FLOAT, | ||
| strategy=QuantizationStrategy.GROUP, | ||
| symmetric=True, | ||
| dynamic=True, | ||
| observer=None, | ||
| group_size=128, |
There was a problem hiding this comment.
Is this not just "FP8_BLOCK"?
There was a problem hiding this comment.
yes but i need to set targets, I can do this more cleanly with **FP8_BLOCK
| ) | ||
|
|
||
|
|
||
| def merge_configs(): |
There was a problem hiding this comment.
It might be simpler to just build the config from scratch. Since this flow is very specialized to deepseek anyways.
There was a problem hiding this comment.
yeah, this is more to just show what i'm doing, i don't expect this pr to ever land like this. we can cherry-pick the changes in another PR
| # validate arguments | ||
| model_files = get_checkpoint_files(model_stub) | ||
| scheme_name, scheme = validate_scheme(scheme) | ||
| ignore = ignore or [] |
There was a problem hiding this comment.
We can just make this (or an empty tuple) the default value, since the ignore list is never mutated.
There was a problem hiding this comment.
Maybe should also consider lm_head
| # - model.layers.3.mlp.experts.0.down_proj.weight | ||
| # - model.layers.3.mlp.experts.0.gate_proj.weight | ||
| # - model.layers.3.mlp.experts.0.up_proj.weight | ||
| if _match_name(module_name, "re:.*mlp.*\.(gate|up|down)_proj$"): |
There was a problem hiding this comment.
Hopefully we don't plan to leave this in the source code.
There was a problem hiding this comment.
when i'm done i plan to ask team how frequently we expect requests like this to see if we should look into abstractions to help with this. i don't plan to land this PR as is
There was a problem hiding this comment.
I agree with Kyle. We can create a dedicated model_opt conversion tool in ct
| for name in list(tensors.keys()): | ||
| module_name, param_name = name.rsplit(".", 1) | ||
| is_linear_weight = param_name == "weight" and not module_name.endswith("norm") | ||
| is_targeted = (is_linear_weight and "Linear" in scheme.targets) or any( |
There was a problem hiding this comment.
Isn't it more robust and explicit to just make ["Linear"] the default target list?
There was a problem hiding this comment.
it's just hard to do with the API, user might use preset schemes which don't set targets
| # ct moe layer has a hard coded check for "Linear" | ||
| scheme.targets = ["Linear"] | ||
| if len(scheme.targets) == 0: | ||
| scheme.targets.append("Linear") |
There was a problem hiding this comment.
I think we should force users to be explicit about targeting linear layers.
Signed-off-by: Brian Dellabetta <bdellabe@redhat.com>
|
The quality checks have failed. Please run |
Signed-off-by: Brian Dellabetta <bdellabe@redhat.com>
Signed-off-by: Brian Dellabetta <bdellabe@redhat.com>
Signed-off-by: Brian Dellabetta <bdellabe@redhat.com>
|
The quality checks have failed. Please run |
Signed-off-by: Brian Dellabetta <bdellabe@redhat.com>
Signed-off-by: Brian Dellabetta <bdellabe@redhat.com>
Signed-off-by: Brian Dellabetta <bdellabe@redhat.com>
SUMMARY:
This PR extends the
nvidia/DeepSeek-R1-NVFP4checkpoint tohf_quant_config.jsonmodelopt config into the compressed-tensors config inconfig.json"quantization_config"Changes to src:
Results in new
config.json:{ "architectures": [ "DeepseekV3ForCausalLM" ], "attention_bias": false, "attention_dropout": 0.0, "auto_map": { "AutoConfig": "configuration_deepseek.DeepseekV3Config", "AutoModel": "modeling_deepseek.DeepseekV3Model", "AutoModelForCausalLM": "modeling_deepseek.DeepseekV3ForCausalLM" }, "aux_loss_alpha": 0.001, "bos_token_id": 0, "eos_token_id": 1, "ep_size": 1, "first_k_dense_replace": 3, "hidden_act": "silu", "hidden_size": 7168, "initializer_range": 0.02, "intermediate_size": 18432, "kv_lora_rank": 512, "max_position_embeddings": 163840, "model_type": "deepseek_v3", "moe_intermediate_size": 2048, "moe_layer_freq": 1, "n_group": 8, "n_routed_experts": 256, "n_shared_experts": 1, "norm_topk_prob": true, "num_attention_heads": 128, "num_experts_per_tok": 8, "num_hidden_layers": 61, "num_key_value_heads": 128, "num_nextn_predict_layers": 1, "pretraining_tp": 1, "q_lora_rank": 1536, "qk_nope_head_dim": 128, "qk_rope_head_dim": 64, "quantization_config": { "config_groups": { "config_group_0": { "targets": [ "re:.*self_attn.(o_proj|q_a_proj|q_b_proj).*" ], "weights": { "num_bits": 8, "type": "float", "symmetric": true, "group_size": null, "strategy": "block", "block_structure": [ 128, 128 ], "dynamic": false, "actorder": null, "scale_dtype": null, "zp_dtype": null, "observer": "static_minmax", "observer_kwargs": {} }, "input_activations": { "num_bits": 8, "type": "float", "symmetric": true, "group_size": 128, "strategy": "group", "block_structure": null, "dynamic": true, "actorder": null, "scale_dtype": null, "zp_dtype": null, "observer": null, "observer_kwargs": {} }, "output_activations": null, "format": "float-quantized" }, "config_group_1": { "targets": [ "re:.*mlp.*\\.(gate|up|down)_proj$" ], "weights": { "num_bits": 4, "type": "float", "symmetric": true, "group_size": 16, "strategy": "tensor_group", "block_structure": null, "dynamic": false, "actorder": null, "scale_dtype": "torch.float8_e4m3fn", "zp_dtype": null, "observer": "static_minmax", "observer_kwargs": {} }, "input_activations": { "num_bits": 4, "type": "float", "symmetric": true, "group_size": 16, "strategy": "tensor_group", "block_structure": null, "dynamic": "local", "actorder": null, "scale_dtype": "torch.float8_e4m3fn", "zp_dtype": null, "observer": "static_minmax", "observer_kwargs": {} }, "output_activations": null, "format": null } }, "quant_method": "compressed-tensors", "kv_cache_scheme": null, "format": "mixed-precision", "quantization_status": "compressed", "global_compression_ratio": null, "ignore": [] }, "rms_norm_eps": 1e-06, "rope_scaling": { "beta_fast": 32, "beta_slow": 1, "factor": 40, "mscale": 1.0, "mscale_all_dim": 1.0, "original_max_position_embeddings": 4096, "type": "yarn" }, "rope_theta": 10000, "routed_scaling_factor": 2.5, "scoring_func": "sigmoid", "seq_aux": true, "tie_word_embeddings": false, "topk_group": 4, "topk_method": "noaux_tc", "torch_dtype": "bfloat16", "transformers_version": "4.46.3", "use_cache": true, "v_head_dim": 128, "vocab_size": 129280 }TEST PLAN:
"please outline how the changes were tested"