Upload folder using huggingface_hub
Browse files- README.md +0 -0
- run_compression.py +132 -0
README.md
ADDED
File without changes
|
run_compression.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Adapted from https://github.com/vllm-project/llm-compressor/blob/e7c6ef485c3ae764bfea0b2eb5c3c41fedac1353/examples/multimodal_vision/qwen_2_5_vl_example.py
|
2 |
+
|
3 |
+
import base64
|
4 |
+
from io import BytesIO
|
5 |
+
|
6 |
+
import torch
|
7 |
+
|
8 |
+
from datasets import load_dataset
|
9 |
+
from llmcompressor.modifiers.quantization import GPTQModifier
|
10 |
+
from llmcompressor.transformers import oneshot
|
11 |
+
from qwen_vl_utils import process_vision_info
|
12 |
+
from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration
|
13 |
+
|
14 |
+
# Load model.
|
15 |
+
model_id = "yujiepan/ui-tars-1.5-7B-bf16"
|
16 |
+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
17 |
+
model_id,
|
18 |
+
device_map="auto",
|
19 |
+
torch_dtype="auto",
|
20 |
+
)
|
21 |
+
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
22 |
+
|
23 |
+
# Oneshot arguments
|
24 |
+
DATASET_ID = "lmms-lab/flickr30k"
|
25 |
+
DATASET_SPLIT = {"calibration": "test[:512]"}
|
26 |
+
DATASET_SPLIT = "test[:512]" # changed to this
|
27 |
+
NUM_CALIBRATION_SAMPLES = 512
|
28 |
+
MAX_SEQUENCE_LENGTH = 2048
|
29 |
+
|
30 |
+
# Load dataset and preprocess.
|
31 |
+
ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
|
32 |
+
ds = ds.shuffle(seed=42)
|
33 |
+
|
34 |
+
|
35 |
+
# Apply chat template and tokenize inputs.
|
36 |
+
def preprocess_and_tokenize(example):
|
37 |
+
# preprocess
|
38 |
+
buffered = BytesIO()
|
39 |
+
example["image"].save(buffered, format="PNG")
|
40 |
+
encoded_image = base64.b64encode(buffered.getvalue())
|
41 |
+
encoded_image_text = encoded_image.decode("utf-8")
|
42 |
+
base64_qwen = f"data:image;base64,{encoded_image_text}"
|
43 |
+
messages = [
|
44 |
+
{
|
45 |
+
"role": "user",
|
46 |
+
"content": [
|
47 |
+
{"type": "image", "image": base64_qwen},
|
48 |
+
{"type": "text", "text": "What does the image show?"},
|
49 |
+
],
|
50 |
+
}
|
51 |
+
]
|
52 |
+
text = processor.apply_chat_template(
|
53 |
+
messages, tokenize=False, add_generation_prompt=True
|
54 |
+
)
|
55 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
56 |
+
|
57 |
+
# tokenize
|
58 |
+
return processor(
|
59 |
+
text=[text],
|
60 |
+
images=image_inputs,
|
61 |
+
videos=video_inputs,
|
62 |
+
padding=False,
|
63 |
+
max_length=MAX_SEQUENCE_LENGTH,
|
64 |
+
truncation=True,
|
65 |
+
)
|
66 |
+
|
67 |
+
|
68 |
+
ds = ds.map(preprocess_and_tokenize, remove_columns=ds.column_names)
|
69 |
+
|
70 |
+
|
71 |
+
# Define a oneshot data collator for multimodal inputs.
|
72 |
+
def data_collator(batch):
|
73 |
+
assert len(batch) == 1
|
74 |
+
return {key: torch.tensor(value) for key, value in batch[0].items()}
|
75 |
+
|
76 |
+
|
77 |
+
# Recipe
|
78 |
+
recipe = [
|
79 |
+
GPTQModifier(
|
80 |
+
targets="Linear",
|
81 |
+
scheme="W4A16",
|
82 |
+
sequential_targets=["Qwen2_5_VLDecoderLayer"],
|
83 |
+
ignore=["lm_head", "re:visual.*"],
|
84 |
+
),
|
85 |
+
]
|
86 |
+
|
87 |
+
# Perform oneshot
|
88 |
+
oneshot(
|
89 |
+
model=model,
|
90 |
+
tokenizer=model_id,
|
91 |
+
dataset=ds,
|
92 |
+
recipe=recipe,
|
93 |
+
max_seq_length=MAX_SEQUENCE_LENGTH,
|
94 |
+
num_calibration_samples=NUM_CALIBRATION_SAMPLES,
|
95 |
+
trust_remote_code_model=True,
|
96 |
+
data_collator=data_collator,
|
97 |
+
)
|
98 |
+
|
99 |
+
# Confirm generations of the quantized model look sane.
|
100 |
+
print("========== SAMPLE GENERATION ==============")
|
101 |
+
messages = [
|
102 |
+
{
|
103 |
+
"role": "user",
|
104 |
+
"content": [
|
105 |
+
{
|
106 |
+
"type": "image",
|
107 |
+
"image": "http://images.cocodataset.org/train2017/000000231895.jpg",
|
108 |
+
},
|
109 |
+
{"type": "text", "text": "Please describe the animal in this image\n"},
|
110 |
+
],
|
111 |
+
}
|
112 |
+
]
|
113 |
+
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
114 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
115 |
+
inputs = processor(
|
116 |
+
text=[prompt],
|
117 |
+
images=image_inputs,
|
118 |
+
videos=video_inputs,
|
119 |
+
padding=False,
|
120 |
+
max_length=MAX_SEQUENCE_LENGTH,
|
121 |
+
truncation=True,
|
122 |
+
return_tensors="pt",
|
123 |
+
).to("cuda")
|
124 |
+
output = model.generate(**inputs, max_new_tokens=100)
|
125 |
+
print(processor.decode(output[0], skip_special_tokens=True))
|
126 |
+
print("==========================================")
|
127 |
+
|
128 |
+
|
129 |
+
# Save to disk compressed.
|
130 |
+
SAVE_DIR = model_id.split("/")[1] + "-GPTQ-W4A16g128"
|
131 |
+
model.save_pretrained(SAVE_DIR, save_compressed=True)
|
132 |
+
processor.save_pretrained(SAVE_DIR)
|