|
2 | 2 | from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 | 3 |
|
4 | 4 | from llmcompressor import oneshot
|
5 |
| -from llmcompressor.modifiers.quantization import GPTQModifier, QuantizationModifier |
| 5 | +from llmcompressor.modifiers.quantization import QuantizationModifier |
6 | 6 | from llmcompressor.modifiers.transform import SpinQuantModifier
|
7 | 7 | from llmcompressor.utils import dispatch_for_generation
|
8 | 8 |
|
9 | 9 | # Select model and load it.
|
10 |
| -# MODEL_ID = "meta-llama/Llama-3.2-1B-Instruct" |
11 |
| -# MODEL_ID = "meta-llama/Llama-3.2-3B-Instruct" # TODO hidden size 3072 causes failure when creating hadamard |
12 | 10 | MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct"
|
13 | 11 |
|
14 | 12 | model = AutoModelForCausalLM.from_pretrained(
|
@@ -57,36 +55,32 @@ def tokenize(sample):
|
57 | 55 | ds = ds.map(tokenize, remove_columns=ds.column_names)
|
58 | 56 |
|
59 | 57 | # Configure the quantization algorithm to run.
|
| 58 | +# * apply spinquant transforms to model in order to make quantization easier |
60 | 59 | # * quantize the weights to 4 bit with GPTQ with a group size 128
|
61 | 60 | recipe = [
|
62 |
| - # TODO preset_config="QUIP_ONLINE" outputs gibberish |
63 |
| - # preset_config="QUIP" output sensible, but cannot load saved |
64 |
| - # checkpoint or run evals (~4hrs to run) |
65 |
| - SpinQuantModifier(rotations=["R1", "R2"]), |
66 |
| - # QuantizationModifier(targets="Linear", scheme="W4A16", ignore=["lm_head"]), |
| 61 | + SpinQuantModifier(rotations=["R1", "R2"], transform_type="random-hadamard"), |
| 62 | + QuantizationModifier(targets="Linear", scheme="W4A16", ignore=["lm_head"]), |
67 | 63 | ]
|
68 | 64 |
|
69 | 65 | # Apply algorithms.
|
70 | 66 | oneshot(
|
71 | 67 | model=model,
|
72 | 68 | recipe=recipe,
|
73 |
| - # dataset=ds, |
74 |
| - pipeline="datafree", |
75 |
| - # max_seq_length=MAX_SEQUENCE_LENGTH, |
76 |
| - # num_calibration_samples=NUM_CALIBRATION_SAMPLES, |
77 |
| - log_dir=None, |
| 69 | + dataset=ds, |
| 70 | + max_seq_length=MAX_SEQUENCE_LENGTH, |
| 71 | + num_calibration_samples=NUM_CALIBRATION_SAMPLES, |
78 | 72 | )
|
79 | 73 |
|
80 |
| -# # Confirm generations of the quantized model look sane. |
| 74 | +# Confirm generations of the quantized model look sane. |
81 | 75 | print("\n\n")
|
82 | 76 | print("========== SAMPLE GENERATION ==============")
|
83 | 77 | dispatch_for_generation(model)
|
84 | 78 | input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to("cuda")
|
85 | 79 | output = model.generate(input_ids, max_new_tokens=100)
|
86 | 80 | print(tokenizer.decode(output[0]))
|
87 |
| -# print("==========================================\n\n") |
| 81 | +print("==========================================\n\n") |
88 | 82 |
|
89 |
| -# # Save to disk compressed. |
90 |
| -# SAVE_DIR = MODEL_ID.split("/")[1] + "-transform-quant-w4a16" |
91 |
| -# model.save_pretrained(SAVE_DIR, save_compressed=True) |
92 |
| -# tokenizer.save_pretrained(SAVE_DIR) |
| 83 | +# Save to disk compressed. |
| 84 | +SAVE_DIR = MODEL_ID.split("/")[1] + "-transformed-w4a16" |
| 85 | +model.save_pretrained(SAVE_DIR, save_compressed=True) |
| 86 | +tokenizer.save_pretrained(SAVE_DIR) |
0 commit comments