-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add int8 quantizing script (#132)
Co-authored-by: Avram Tudor <tudor.avram@8x8.com>
- Loading branch information
Showing
5 changed files
with
168 additions
and
5 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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/sh | ||
|
||
poetry run black skynet | ||
poetry run usort format skynet | ||
poetry run black skynet tools | ||
poetry run usort format skynet tools |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,60 @@ | ||
# Courtesy of https://docs.vllm.ai/en/stable/quantization/int8.html | ||
|
||
from datasets import load_dataset | ||
from llmcompressor.modifiers.quantization import GPTQModifier | ||
from llmcompressor.modifiers.smoothquant import SmoothQuantModifier | ||
from llmcompressor.transformers import oneshot, SparseAutoModelForCausalLM | ||
from transformers import AutoTokenizer | ||
|
||
from skynet.env import llama_path as MODEL_ID | ||
|
||
model = SparseAutoModelForCausalLM.from_pretrained( | ||
MODEL_ID, | ||
device_map="auto", | ||
torch_dtype="auto", | ||
) | ||
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | ||
|
||
NUM_CALIBRATION_SAMPLES = 512 | ||
MAX_SEQUENCE_LENGTH = 2048 | ||
|
||
# Load and preprocess the dataset | ||
ds = load_dataset("HuggingFaceH4/ultrachat_200k", split="train_sft") | ||
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) | ||
|
||
|
||
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) | ||
|
||
|
||
# Configure the quantization algorithms | ||
recipe = [ | ||
SmoothQuantModifier(smoothing_strength=0.8), | ||
GPTQModifier(targets="Linear", scheme="W8A8", ignore=["lm_head"]), | ||
] | ||
|
||
# Apply quantization | ||
oneshot( | ||
model=model, | ||
dataset=ds, | ||
recipe=recipe, | ||
max_seq_length=MAX_SEQUENCE_LENGTH, | ||
num_calibration_samples=NUM_CALIBRATION_SAMPLES, | ||
) | ||
|
||
# Save the compressed model | ||
SAVE_DIR = MODEL_ID + "-W8A8-Dynamic-Per-Token" | ||
model.save_pretrained(SAVE_DIR, save_compressed=True) | ||
tokenizer.save_pretrained(SAVE_DIR) |