|
| 1 | +from datasets import load_dataset |
| 2 | +from transformers import AutoModelForCausalLM, AutoTokenizer |
| 3 | + |
| 4 | +from llmcompressor import oneshot |
| 5 | +from llmcompressor.modifiers.quantization import QuantizationModifier |
| 6 | +from llmcompressor.utils import dispatch_for_generation |
| 7 | + |
| 8 | +# NOTE: Qwen3-Next-80B-A3B-Instruct support is not in transformers<=4.56.2 |
| 9 | +# you may need to install transformers from source |
| 10 | + |
| 11 | +MODEL_ID = "Qwen/Qwen3-Next-80B-A3B-Instruct" |
| 12 | + |
| 13 | +# Load model. |
| 14 | +model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype="auto") |
| 15 | +tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
| 16 | + |
| 17 | + |
| 18 | +DATASET_ID = "HuggingFaceH4/ultrachat_200k" |
| 19 | +DATASET_SPLIT = "train_sft" |
| 20 | + |
| 21 | +# Select number of samples |
| 22 | +NUM_CALIBRATION_SAMPLES = 20 |
| 23 | +MAX_SEQUENCE_LENGTH = 2048 |
| 24 | + |
| 25 | +# Load dataset and preprocess. |
| 26 | +ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]") |
| 27 | +ds = ds.shuffle(seed=42) |
| 28 | + |
| 29 | + |
| 30 | +def preprocess(example): |
| 31 | + return { |
| 32 | + "text": tokenizer.apply_chat_template( |
| 33 | + example["messages"], |
| 34 | + tokenize=False, |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | +ds = ds.map(preprocess) |
| 40 | + |
| 41 | + |
| 42 | +# Tokenize inputs. |
| 43 | +def tokenize(sample): |
| 44 | + return tokenizer( |
| 45 | + sample["text"], |
| 46 | + padding=False, |
| 47 | + max_length=MAX_SEQUENCE_LENGTH, |
| 48 | + truncation=True, |
| 49 | + add_special_tokens=False, |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +ds = ds.map(tokenize, remove_columns=ds.column_names) |
| 54 | + |
| 55 | +# Configure the quantization algorithm and scheme. |
| 56 | +# In this case, we: |
| 57 | +# * quantize the weights to fp4 with per group 16 via ptq |
| 58 | +# * calibrate a global_scale for activations, which will be used to |
| 59 | +# quantize activations to fp4 on the fly |
| 60 | +recipe = QuantizationModifier( |
| 61 | + targets="Linear", |
| 62 | + scheme="NVFP4", |
| 63 | + ignore=[ |
| 64 | + "lm_head", |
| 65 | + "re:.*mlp.gate$", |
| 66 | + "re:.*mlp.shared_expert_gate$", |
| 67 | + "re:.*linear_attn.*", |
| 68 | + ], |
| 69 | +) |
| 70 | + |
| 71 | +# Apply quantization. |
| 72 | +# We see `calibrate_moe_context` to True to update all `Qwen3MoeSparseMoeBlock` |
| 73 | +# during calibration. |
| 74 | +# Feel free to update the definition under |
| 75 | +# llm-compressor/src/llmcompressor/modeling/qwen3_moe.py` to play around with |
| 76 | +# this behaviour and evaluate its impact on quantization performance |
| 77 | +oneshot( |
| 78 | + model=model, |
| 79 | + dataset=ds, |
| 80 | + recipe=recipe, |
| 81 | + max_seq_length=MAX_SEQUENCE_LENGTH, |
| 82 | + num_calibration_samples=NUM_CALIBRATION_SAMPLES, |
| 83 | + calibrate_moe_context=True, |
| 84 | +) |
| 85 | + |
| 86 | + |
| 87 | +print("\n\n") |
| 88 | +print("========== SAMPLE GENERATION ==============") |
| 89 | +dispatch_for_generation(model) |
| 90 | +input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to( |
| 91 | + model.device |
| 92 | +) |
| 93 | +output = model.generate(input_ids, max_new_tokens=100) |
| 94 | +print(tokenizer.decode(output[0])) |
| 95 | +print("==========================================\n\n") |
| 96 | + |
| 97 | + |
| 98 | +# Save to disk in compressed-tensors format. |
| 99 | +SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-NVFP4" |
| 100 | +model.save_pretrained(SAVE_DIR, save_compressed=True) |
| 101 | +tokenizer.save_pretrained(SAVE_DIR) |
0 commit comments