|
| 1 | +from datasets import load_dataset |
| 2 | +from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer |
| 3 | + |
| 4 | +from llmcompressor.modeling import prepare_for_calibration |
| 5 | +from llmcompressor.modifiers.quantization import GPTQModifier |
| 6 | +from llmcompressor.transformers import oneshot |
| 7 | + |
| 8 | +# Select model and load it. |
| 9 | + |
| 10 | +# This script takes about 48 hours on 1xA100 to complete. |
| 11 | +# Future improvements will reduce this runtime (#1561, #1558). |
| 12 | + |
| 13 | +# For DeepSeek-R1, we require a full precision model in order to properly calibrate |
| 14 | +# `DeepSeek-R1-0528-BF16` is a DeepSeek-V3 FP8 model which has been converted to BF16 |
| 15 | + |
| 16 | +model_id = "unsloth/DeepSeek-R1-0528-BF16" |
| 17 | +config = AutoConfig.from_pretrained(model_id) |
| 18 | +del config.quantization_config # fp8 qconfig no longer appplies to bf16 model |
| 19 | +model = AutoModelForCausalLM.from_pretrained( |
| 20 | + model_id, torch_dtype="auto", config=config |
| 21 | +) |
| 22 | +tokenizer = AutoTokenizer.from_pretrained(model_id) |
| 23 | +model = prepare_for_calibration(model) |
| 24 | + |
| 25 | +# Select calibration dataset. |
| 26 | +DATASET_ID = "HuggingFaceH4/ultrachat_200k" |
| 27 | +DATASET_SPLIT = "train_sft" |
| 28 | + |
| 29 | +# Select number of samples. 512 samples is a good place to start. |
| 30 | +# Increasing the number of samples can improve accuracy. |
| 31 | +NUM_CALIBRATION_SAMPLES = 512 |
| 32 | +MAX_SEQUENCE_LENGTH = 2048 |
| 33 | + |
| 34 | +# Load dataset and preprocess. |
| 35 | +ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]") |
| 36 | +ds = ds.shuffle(seed=42) |
| 37 | + |
| 38 | + |
| 39 | +def preprocess(example): |
| 40 | + return { |
| 41 | + "text": tokenizer.apply_chat_template( |
| 42 | + example["messages"], |
| 43 | + tokenize=False, |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | +ds = ds.map(preprocess) |
| 49 | + |
| 50 | + |
| 51 | +# Tokenize inputs. |
| 52 | +def tokenize(sample): |
| 53 | + return tokenizer( |
| 54 | + sample["text"], |
| 55 | + padding=False, |
| 56 | + max_length=MAX_SEQUENCE_LENGTH, |
| 57 | + truncation=True, |
| 58 | + add_special_tokens=False, |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +ds = ds.map(tokenize, remove_columns=ds.column_names) |
| 63 | + |
| 64 | +# Configure the quantization algorithm to run. |
| 65 | +# since the MoE gate layers are sensitive to quantization, we add them to the ignore |
| 66 | +# list so they remain at full precision |
| 67 | +recipe = GPTQModifier( |
| 68 | + targets="Linear", scheme="W4A16", ignore=["lm_head", "re:.*mlp.gate$"] |
| 69 | +) |
| 70 | + |
| 71 | +# Apply algorithms. |
| 72 | +# due to the large size of DeepSeekV3, we specify sequential targets such that |
| 73 | +# only one MLP is loaded into GPU memory at a time |
| 74 | +oneshot( |
| 75 | + model=model, |
| 76 | + dataset=ds, |
| 77 | + recipe=recipe, |
| 78 | + max_seq_length=MAX_SEQUENCE_LENGTH, |
| 79 | + num_calibration_samples=NUM_CALIBRATION_SAMPLES, |
| 80 | + sequential_targets=["DeepseekV3Attention", "DeepseekV3MLP"], |
| 81 | +) |
| 82 | + |
| 83 | +# Save to disk compressed. |
| 84 | +SAVE_DIR = model_id.rstrip("/").split("/")[-1] + "-W4A16-G128" |
| 85 | +model.save_pretrained(SAVE_DIR, save_compressed=True) |
| 86 | +tokenizer.save_pretrained(SAVE_DIR) |
0 commit comments