-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* gemma2 * remove spurious change * revert * revert llama-3 changes
- Loading branch information
1 parent
919a0a1
commit 2ab6ae5
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from transformers import AutoTokenizer | ||
|
||
from llmcompressor.modifiers.quantization import QuantizationModifier | ||
from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot | ||
|
||
MODEL_ID = "google/gemma-2-27b-it" | ||
|
||
# 1) Load model. | ||
model = SparseAutoModelForCausalLM.from_pretrained( | ||
MODEL_ID, device_map="auto", torch_dtype="auto") | ||
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | ||
|
||
# 2) Configure the quantization algorithm and scheme. | ||
# In this case, we: | ||
# * quantize the weights to fp8 with per channel via ptq | ||
# * quantize the activations to fp8 with dynamic per token | ||
recipe = QuantizationModifier( | ||
targets="Linear", scheme="FP8_DYNAMIC", ignore=["lm_head"]) | ||
|
||
# 3) Apply quantization and save in compressed-tensors format. | ||
OUTPUT_DIR = MODEL_ID.split("/")[1] + "-FP8-Dynamic" | ||
oneshot(model=model, | ||
recipe=recipe, | ||
output_dir=OUTPUT_DIR, | ||
tokenizer=tokenizer) | ||
|
||
# Confirm generations of the quantized model look sane. | ||
print("========== SAMPLE GENERATION ==============") | ||
input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to("cuda") | ||
output = model.generate(input_ids, max_new_tokens=20) | ||
print(tokenizer.decode(output[0])) | ||
print("==========================================") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from datasets import load_dataset | ||
from transformers import AutoTokenizer | ||
|
||
from llmcompressor.modifiers.quantization import GPTQModifier | ||
from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot | ||
|
||
# 1) Select model and load it. | ||
MODEL_ID = "google/gemma-2-2b-it" | ||
model = SparseAutoModelForCausalLM.from_pretrained( | ||
MODEL_ID, device_map="auto", torch_dtype="auto",) | ||
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | ||
|
||
# 2) Prepare calibration dataset. | ||
DATASET_ID = "HuggingFaceH4/ultrachat_200k" | ||
DATASET_SPLIT = "train_sft" | ||
|
||
# Select number of samples. 512 samples is a good place to start. | ||
# Increasing the number of samples can improve accuracy. | ||
NUM_CALIBRATION_SAMPLES = 512 | ||
MAX_SEQUENCE_LENGTH = 2048 | ||
|
||
# Load dataset and preprocess. | ||
ds = load_dataset(DATASET_ID, split=DATASET_SPLIT) | ||
ds = ds.shuffle(seed=42).select(range(NUM_CALIBRATION_SAMPLES)) | ||
|
||
|
||
def preprocess(example): | ||
return { | ||
"text": tokenizer.apply_chat_template( | ||
example["messages"], | ||
tokenize=False, | ||
) | ||
} | ||
|
||
|
||
ds = ds.map(preprocess) | ||
|
||
|
||
# Tokenize inputs. | ||
def tokenize(sample): | ||
return tokenizer( | ||
sample["text"], | ||
padding=False, | ||
max_length=MAX_SEQUENCE_LENGTH, | ||
truncation=True, | ||
add_special_tokens=False, | ||
) | ||
|
||
|
||
ds = ds.map(tokenize, remove_columns=ds.column_names) | ||
|
||
# 3) Select quantization algorithms. In this case, we: | ||
# * quantize the weights to int8 with GPTQ (static per channel) | ||
# * quantize the activations to int8 (dynamic per token) | ||
# Note: set sequential_update: true in the recipe to reduce memory | ||
recipe = GPTQModifier(targets="Linear", scheme="W8A8", ignore=["lm_head"]) | ||
|
||
# 4) Apply quantization and save to disk compressed. | ||
oneshot( | ||
model=model, | ||
dataset=ds, | ||
recipe=recipe, | ||
max_seq_length=MAX_SEQUENCE_LENGTH, | ||
num_calibration_samples=NUM_CALIBRATION_SAMPLES, | ||
output_dir=MODEL_ID.split("/")[1] + "-INT8" | ||
) | ||
|
||
# Confirm generations of the quantized model look sane. | ||
print("========== SAMPLE GENERATION ==============") | ||
input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to("cuda") | ||
output = model.generate(input_ids, max_new_tokens=20) | ||
print(tokenizer.decode(output[0])) | ||
print("==========================================") |