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