|
| 1 | +import torch |
| 2 | +from datasets import load_dataset |
| 3 | +from transformers import AutoTokenizer |
| 4 | + |
| 5 | +from llmcompressor.modifiers.quantization import GPTQModifier |
| 6 | +from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot |
| 7 | +from llmcompressor.transformers.compression.helpers import calculate_offload_device_map |
| 8 | + |
| 9 | +# select a Mixture of Experts model for quantization |
| 10 | +MODEL_ID = "deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct" |
| 11 | + |
| 12 | +# adjust based off number of desired GPUs |
| 13 | +# if not enough memory is available, some layers will automatically be offlaoded to cpu |
| 14 | +device_map = calculate_offload_device_map( |
| 15 | + MODEL_ID, |
| 16 | + reserve_for_hessians=True, |
| 17 | + num_gpus=2, |
| 18 | + torch_dtype=torch.bfloat16, |
| 19 | + trust_remote_code=True, |
| 20 | +) |
| 21 | + |
| 22 | +model = SparseAutoModelForCausalLM.from_pretrained( |
| 23 | + MODEL_ID, device_map=device_map, torch_dtype=torch.bfloat16, trust_remote_code=True |
| 24 | +) |
| 25 | +tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
| 26 | + |
| 27 | +# Select calibration dataset. |
| 28 | +# its recommended to use more calibration samples for MoE models so each expert is hit |
| 29 | +DATASET_ID = "HuggingFaceH4/ultrachat_200k" |
| 30 | +DATASET_SPLIT = "train_sft" |
| 31 | +NUM_CALIBRATION_SAMPLES = 2048 |
| 32 | +MAX_SEQUENCE_LENGTH = 2048 |
| 33 | + |
| 34 | + |
| 35 | +# Load dataset and preprocess. |
| 36 | +ds = load_dataset(DATASET_ID, split=DATASET_SPLIT) |
| 37 | +ds = ds.shuffle(seed=42).select(range(NUM_CALIBRATION_SAMPLES)) |
| 38 | + |
| 39 | + |
| 40 | +def preprocess(example): |
| 41 | + return { |
| 42 | + "text": tokenizer.apply_chat_template( |
| 43 | + example["messages"], |
| 44 | + tokenize=False, |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +ds = ds.map(preprocess) |
| 50 | + |
| 51 | + |
| 52 | +# Tokenize inputs. |
| 53 | +def tokenize(sample): |
| 54 | + return tokenizer( |
| 55 | + sample["text"], |
| 56 | + padding=False, |
| 57 | + max_length=MAX_SEQUENCE_LENGTH, |
| 58 | + truncation=True, |
| 59 | + add_special_tokens=False, |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +ds = ds.map(tokenize, remove_columns=ds.column_names) |
| 64 | + |
| 65 | +# define a llmcompressor recipe for W416 quantization |
| 66 | +# since the MoE gate layers are sensitive to quantization, we add them to the ignore |
| 67 | +# list so they remain at full precision |
| 68 | +recipe = [ |
| 69 | + GPTQModifier( |
| 70 | + targets="Linear", |
| 71 | + scheme="W8A8", |
| 72 | + ignore=["lm_head", "re:.*mlp.gate$"], |
| 73 | + sequential_update=True, |
| 74 | + ), |
| 75 | +] |
| 76 | + |
| 77 | +SAVE_DIR = MODEL_ID.split("/")[1] + "-W8A8" |
| 78 | + |
| 79 | +oneshot( |
| 80 | + model=model, |
| 81 | + dataset=ds, |
| 82 | + recipe=recipe, |
| 83 | + max_seq_length=MAX_SEQUENCE_LENGTH, |
| 84 | + num_calibration_samples=NUM_CALIBRATION_SAMPLES, |
| 85 | + save_compressed=True, |
| 86 | + output_dir=SAVE_DIR, |
| 87 | +) |
| 88 | + |
| 89 | + |
| 90 | +print("========== SAMPLE GENERATION ==============") |
| 91 | +SAMPLE_INPUT = ["I love quantization because"] |
| 92 | +tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
| 93 | +inputs = tokenizer(SAMPLE_INPUT, return_tensors="pt", padding=True).to(model.device) |
| 94 | +output = model.generate(**inputs, max_length=50) |
| 95 | +text_output = tokenizer.batch_decode(output) |
| 96 | +print(text_output) |
0 commit comments