Views
No views yet
1from compressed_tensors.offload import dispatch_model
2from datasets import load_dataset
3from transformers import (
4 AutoProcessor,
5 MuseGlimmerForConditionalGeneration,
6)
7
8from llmcompressor import oneshot
9from llmcompressor.modifiers.gptq import GPTQModifier
10from llmcompressor.utils import load_context
11
12MODEL_ID = "meta-models/Muse-Glimmer-30B"
13
14# Load model.
15with load_context(MuseGlimmerForConditionalGeneration):
16 model = MuseGlimmerForConditionalGeneration.from_pretrained(MODEL_ID)
17processor = AutoProcessor.from_pretrained(MODEL_ID)
18
19DATASET_ID = "mlabonne/open-perfectblend"
20DATASET_SPLIT = "train"
21
22# Select number of samples. 512 samples is recommended for GPTQ.
23# Increasing the number of samples can improve accuracy.
24NUM_CALIBRATION_SAMPLES = 512
25MAX_SEQUENCE_LENGTH = 2048
26
27# Load dataset and preprocess.
28ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}")
29ds = ds.shuffle(seed=42)
30
31ROLE_MAP = {"human": "user", "gpt": "assistant"}
32
33
34def preprocess(example):
35 messages = [
36 {"role": ROLE_MAP.get(msg["from"], msg["from"]), "content": msg["value"]}
37 for msg in example["conversations"]
38 ]
39 return {
40 "text": processor.apply_chat_template(
41 messages,
42 tokenize=False,
43 )
44 }
45
46
47ds = ds.map(preprocess)
48
49
50# Tokenize inputs.
51def tokenize(sample):
52 return processor.tokenizer(
53 sample["text"],
54 padding=False,
55 max_length=MAX_SEQUENCE_LENGTH,
56 truncation=True,
57 add_special_tokens=False,
58 )
59
60
61ds = ds.map(tokenize, remove_columns=ds.column_names)
62
63# Configure the quantization algorithm and scheme.
64recipe = GPTQModifier(
65 targets="Linear",
66 scheme="W4A16",
67 ignore=["re:.*vision.*", "lm_head", "re:.*embed_tokens.*"],
68)
69
70# Apply quantization.
71oneshot(
72 model=model,
73 dataset=ds,
74 recipe=recipe,
75 max_seq_length=MAX_SEQUENCE_LENGTH,
76 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
77)
78
79print("\n\n")
80print("========== SAMPLE GENERATION ==============")
81dispatch_model(model)
82input_ids = processor.tokenizer(
83 "Hello my name is", return_tensors="pt"
84).input_ids.to(model.device)
85output = model.generate(input_ids, max_new_tokens=100)
86print(processor.tokenizer.decode(output[0]))
87print("==========================================\n\n")
88
89# Save to disk in compressed-tensors format.
90SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-INT4"
91model.save_pretrained(SAVE_DIR)
92processor.save_pretrained(SAVE_DIR)
93docker run --gpus all \
--privileged --ipc=host -p 8000:8000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
vllm/vllm-openai:muse-glimmer RedHatAI/Muse-Glimmer-30B-INT4 \
--generation-config auto \
--tensor-parallel-size 1 \
--enable-auto-tool-choice \
--tool-call-parser muse_glimmer \
--reasoning-parser muse_glimmer