Repository navigation
[Example] Add Mixed W2A16 and W4A16 MoE Example #2940
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
94 changes: 94 additions & 0 deletions
94
examples/autoround/quantization_wNa16/qwen3_moe_example_mixed_w2a16_w4a16.py
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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| from auto_round.calib_dataset import get_dataset | ||
| from compressed_tensors.offload import dispatch_model | ||
| from compressed_tensors.quantization import QuantizationArgs, QuantizationScheme | ||
| from transformers import AutoModelForCausalLM, AutoTokenizer | ||
|
|
||
| from llmcompressor import oneshot | ||
| from llmcompressor.modifiers.autoround import AutoRoundModifier | ||
| from llmcompressor.utils import load_context | ||
|
|
||
| # Select model and load it. | ||
| model_id = "Qwen/Qwen3-30B-A3B-Instruct-2507" | ||
| with load_context(): | ||
| model = AutoModelForCausalLM.from_pretrained(model_id) | ||
| tokenizer = AutoTokenizer.from_pretrained(model_id) | ||
|
|
||
| # Select calibration dataset. | ||
| NUM_CALIBRATION_SAMPLES = 128 | ||
| MAX_SEQUENCE_LENGTH = 2048 | ||
| ITERS = 200 | ||
| # Get aligned calibration dataset. | ||
|
|
||
| ds = get_dataset( | ||
| tokenizer=tokenizer, | ||
| seqlen=MAX_SEQUENCE_LENGTH, | ||
| nsamples=NUM_CALIBRATION_SAMPLES, | ||
| ) | ||
|
|
||
|
|
||
| # Configure the quantization algorithm to run. | ||
| # * quantize the attention weights to 4 bit with a group size 64 | ||
| # * mlp weights to 2 bit with a group size 64 | ||
|
|
||
| recipe = AutoRoundModifier( | ||
| config_groups={ | ||
| "attention": QuantizationScheme( | ||
| targets=[ | ||
| r"re:.*self_attn\.(q|k|v|o)_proj$", | ||
| ], | ||
| weights=QuantizationArgs( | ||
| num_bits=4, | ||
| type="int", | ||
| strategy="group", | ||
| symmetric=True, | ||
| dynamic=False, | ||
| group_size=64, | ||
| ), | ||
| ), | ||
| "mlp": QuantizationScheme( | ||
| targets=[ | ||
| r"re:.*mlp\..*(gate|up|down)_proj$", | ||
| ], | ||
| weights=QuantizationArgs( | ||
| num_bits=2, | ||
| type="int", | ||
| strategy="group", | ||
| symmetric=True, | ||
| dynamic=False, | ||
| group_size=64, | ||
| ), | ||
| ), | ||
| }, | ||
| ignore=["lm_head"], | ||
| iters=ITERS, | ||
| enable_torch_compile=False, | ||
| ) | ||
|
|
||
|
|
||
| # Apply algorithms. | ||
| oneshot( | ||
| model=model, | ||
| dataset=ds, | ||
| recipe=recipe, | ||
| max_seq_length=MAX_SEQUENCE_LENGTH, | ||
| num_calibration_samples=NUM_CALIBRATION_SAMPLES, | ||
| shuffle_calibration_samples=False, | ||
| ) | ||
|
|
||
| # Confirm generations of the quantized model look sane. | ||
| print("\n\n") | ||
| print("========== SAMPLE GENERATION ==============") | ||
| dispatch_model(model) | ||
| sample = tokenizer("Hello my name is", return_tensors="pt") | ||
| sample = {key: value.to(model.device) for key, value in sample.items()} | ||
| output = model.generate(**sample, max_new_tokens=100) | ||
| print(tokenizer.decode(output[0])) | ||
| print("==========================================\n\n") | ||
|
|
||
| # Save to disk compressed. | ||
| SAVE_DIR = ( | ||
| model_id.rstrip("/").split("/")[-1] + f"-Attn4bits-Mlp2bits-AutoRound-{ITERS}iters" | ||
| ) | ||
| print(f"save to {SAVE_DIR}") | ||
| model.save_pretrained(SAVE_DIR, save_compressed=True) | ||
|
dsikka marked this conversation as resolved.
Resolved
|
||
| tokenizer.save_pretrained(SAVE_DIR) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.