|
| 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.modifiers.transform import QuIPModifier |
| 7 | +from llmcompressor.utils import dispatch_for_generation |
| 8 | + |
| 9 | +# Select model and load it. |
| 10 | +MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct" |
| 11 | + |
| 12 | +model = AutoModelForCausalLM.from_pretrained( |
| 13 | + MODEL_ID, |
| 14 | + torch_dtype="auto", |
| 15 | +) |
| 16 | +tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
| 17 | + |
| 18 | +# Select calibration dataset. |
| 19 | +DATASET_ID = "HuggingFaceH4/ultrachat_200k" |
| 20 | +DATASET_SPLIT = "train_sft" |
| 21 | + |
| 22 | +# Select number of samples. 512 samples is a good place to start. |
| 23 | +# Increasing the number of samples can improve accuracy. |
| 24 | +NUM_CALIBRATION_SAMPLES = 512 |
| 25 | +MAX_SEQUENCE_LENGTH = 2048 |
| 26 | + |
| 27 | +# Load dataset and preprocess. |
| 28 | +ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]") |
| 29 | +ds = ds.shuffle(seed=42) |
| 30 | + |
| 31 | + |
| 32 | +def preprocess(example): |
| 33 | + return { |
| 34 | + "text": tokenizer.apply_chat_template( |
| 35 | + example["messages"], |
| 36 | + tokenize=False, |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +ds = ds.map(preprocess) |
| 42 | + |
| 43 | + |
| 44 | +# Tokenize inputs. |
| 45 | +def tokenize(sample): |
| 46 | + return tokenizer( |
| 47 | + sample["text"], |
| 48 | + padding=False, |
| 49 | + max_length=MAX_SEQUENCE_LENGTH, |
| 50 | + truncation=True, |
| 51 | + add_special_tokens=False, |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +ds = ds.map(tokenize, remove_columns=ds.column_names) |
| 56 | + |
| 57 | +# Configure the quantization algorithm to run. |
| 58 | +# * apply spinquant transforms to model in order to make quantization easier |
| 59 | +# * quantize the weights to 4 bit with GPTQ with a group size 128 |
| 60 | +recipe = [ |
| 61 | + QuIPModifier(transform_type="random-hadamard"), |
| 62 | + QuantizationModifier(targets="Linear", scheme="W4A16", ignore=["lm_head"]), |
| 63 | +] |
| 64 | + |
| 65 | +# Apply algorithms. |
| 66 | +oneshot( |
| 67 | + model=model, |
| 68 | + recipe=recipe, |
| 69 | + dataset=ds, |
| 70 | + max_seq_length=MAX_SEQUENCE_LENGTH, |
| 71 | + num_calibration_samples=NUM_CALIBRATION_SAMPLES, |
| 72 | + pipeline="basic", |
| 73 | +) |
| 74 | + |
| 75 | +# Confirm generations of the quantized model look sane. |
| 76 | +print("\n\n") |
| 77 | +print("========== SAMPLE GENERATION ==============") |
| 78 | +dispatch_for_generation(model) |
| 79 | +input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to("cuda") |
| 80 | +output = model.generate(input_ids, max_new_tokens=100) |
| 81 | +print(tokenizer.decode(output[0])) |
| 82 | +print("==========================================\n\n") |
| 83 | + |
| 84 | +# Save to disk compressed. |
| 85 | +SAVE_DIR = MODEL_ID.split("/")[1] + "-transformed-w4a16" |
| 86 | +model.save_pretrained(SAVE_DIR, save_compressed=True) |
| 87 | +tokenizer.save_pretrained(SAVE_DIR) |
0 commit comments