-
Notifications
You must be signed in to change notification settings - Fork 287
Support wInt4aFp8 for moe #2027
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
Wangzheee
wants to merge
2
commits into
vllm-project:main
Choose a base branch
from
Wangzheee:wInt4aFp8
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.
+122
−0
Open
Changes from all commits
Commits
Show all changes
2 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
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 |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import torch | ||
| from datasets import load_dataset | ||
| from transformers import AutoModelForCausalLM, AutoTokenizer | ||
|
|
||
| from llmcompressor import oneshot | ||
| from llmcompressor.modifiers.quantization import GPTQModifier, QuantizationModifier | ||
| from llmcompressor.utils import dispatch_for_generation | ||
|
|
||
| # select a Mixture of Experts model for quantization | ||
| MODEL_ID = "Qwen/Qwen1.5-MoE-A2.7B-Chat" | ||
|
|
||
| model = AutoModelForCausalLM.from_pretrained( | ||
| MODEL_ID, torch_dtype=torch.bfloat16, trust_remote_code=True | ||
| ) | ||
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | ||
|
|
||
| # Select calibration dataset. | ||
| DATASET_ID = "HuggingFaceH4/ultrachat_200k" | ||
| DATASET_SPLIT = "train_sft" | ||
| NUM_CALIBRATION_SAMPLES = 512 | ||
| MAX_SEQUENCE_LENGTH = 2048 | ||
|
|
||
|
|
||
| # Load dataset and preprocess. | ||
| ds = load_dataset(DATASET_ID, split=DATASET_SPLIT) | ||
| ds = ds.shuffle(seed=42).select(range(NUM_CALIBRATION_SAMPLES)) | ||
|
|
||
|
|
||
| def preprocess(example): | ||
| return { | ||
| "text": tokenizer.apply_chat_template( | ||
| example["messages"], | ||
| tokenize=False, | ||
| ) | ||
| } | ||
|
|
||
|
|
||
| ds = ds.map(preprocess) | ||
|
|
||
|
|
||
| # Tokenize inputs. | ||
| def tokenize(sample): | ||
| return tokenizer( | ||
| sample["text"], | ||
| padding=False, | ||
| max_length=MAX_SEQUENCE_LENGTH, | ||
| truncation=True, | ||
| add_special_tokens=False, | ||
| ) | ||
|
|
||
|
|
||
| ds = ds.map(tokenize, remove_columns=ds.column_names) | ||
|
|
||
| # define a llmcompressor recipe for W416 quantization with a group size of 128 | ||
| # since the MoE gate layers are sensitive to quantization, we add them to the ignore | ||
| # list so they remain at full precision | ||
|
|
||
| recipe = """ | ||
| quant_stage: | ||
| quant_modifiers: | ||
| QuantizationModifier: | ||
| ignore: ["lm_head", "re:.*mlp.gate$"] | ||
| config_groups: | ||
| group_0: | ||
| weights: | ||
| num_bits: 8 | ||
| type: float | ||
| strategy: block | ||
| dynamic: false | ||
| symmetric: true | ||
| block_structure: [128, 128] | ||
| input_activations: | ||
| num_bits: 8 | ||
| type: float | ||
| strategy: token | ||
| dynamic: true | ||
| symmetric: true | ||
| targets: ["re:.*self_attn.q_proj", "re:.*self_attn.k_proj", | ||
| "re:.*self_attn.v_proj", "re:.*self_attn.o_proj",] | ||
| group_1: | ||
| weights: | ||
| num_bits: 4 | ||
| type: int | ||
| strategy: tensor_group | ||
| dynamic: false | ||
| symmetric: true | ||
| group_size: 128 | ||
| input_activations: | ||
| num_bits: 8 | ||
| type: float | ||
| strategy: tensor | ||
| dynamic: true | ||
| symmetric: true | ||
| targets: [ "re:.*gate_proj", "re:.*up_proj", "re:.*down_proj"] | ||
| """ | ||
|
|
||
| oneshot( | ||
| model=model, | ||
| dataset=ds, | ||
| recipe=recipe, | ||
| max_seq_length=MAX_SEQUENCE_LENGTH, | ||
| num_calibration_samples=NUM_CALIBRATION_SAMPLES, | ||
| save_compressed=True, | ||
| trust_remote_code_model=True, | ||
| ) | ||
|
|
||
| # Confirm generations of the quantized model look sane. | ||
| print("========== SAMPLE GENERATION ==============") | ||
| dispatch_for_generation(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("==========================================") | ||
|
|
||
| # Save to disk in compressed-tensors format. | ||
| SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-wInt4aFp8" | ||
| model.save_pretrained(SAVE_DIR, save_compressed=True) | ||
| tokenizer.save_pretrained(SAVE_DIR) |
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
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.
For comparing string values, it's safer and more conventional to use the
==operator instead ofis. Theisoperator checks for object identity, which might work for interned strings but is not guaranteed across different Python implementations or versions. Using==ensures the comparison is always done by value.1Rules References
Footnotes
Use
==for value equality andisfor identity equality. For comparing string literals,==is preferred for robustness as string interning is an implementation detail. ↩