-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gibberish is produced, even when the model is exactly copied
Signed-off-by: Kyle Sayers <kylesayrs@gmail.com>
- Loading branch information
Showing
6 changed files
with
358 additions
and
88 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import requests | ||
import torch | ||
from PIL import Image | ||
from transformers import AutoModelForCausalLM, AutoProcessor | ||
|
||
from llmcompressor.modifiers.quantization import GPTQModifier | ||
from llmcompressor.transformers import oneshot | ||
from llmcompressor.transformers.tracing import TraceableLlavaForConditionalGeneration | ||
|
||
# Load model. | ||
model_id = "deepseek-ai/Janus-Pro-7B" | ||
model = AutoModelForCausalLM.from_pretrained( | ||
model_id, device_map="auto", torch_dtype="auto" | ||
) | ||
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True) | ||
|
||
# Oneshot arguments | ||
DATASET_ID = "flickr30k" | ||
DATASET_SPLIT = {"calibration": "test[:512]"} | ||
NUM_CALIBRATION_SAMPLES = 512 | ||
MAX_SEQUENCE_LENGTH = 2048 | ||
|
||
|
||
# Define a oneshot data collator for multimodal inputs. | ||
def data_collator(batch): | ||
assert len(batch) == 1 | ||
return {key: torch.tensor(value) for key, value in batch[0].items()} | ||
|
||
|
||
# Recipe | ||
recipe = [ | ||
GPTQModifier( | ||
targets="Linear", | ||
scheme="W4A16", | ||
sequential_targets=["LlamaDecoderLayer"], | ||
ignore=["re:.*lm_head", "re:vision_tower.*", "re:multi_modal_projector.*"], | ||
), | ||
] | ||
|
||
# Perform oneshot | ||
oneshot( | ||
model=model, | ||
tokenizer=model_id, | ||
dataset=DATASET_ID, | ||
splits=DATASET_SPLIT, | ||
recipe=recipe, | ||
max_seq_length=MAX_SEQUENCE_LENGTH, | ||
num_calibration_samples=NUM_CALIBRATION_SAMPLES, | ||
trust_remote_code_model=True, | ||
data_collator=data_collator, | ||
) | ||
|
||
# Confirm generations of the quantized model look sane. | ||
print("========== SAMPLE GENERATION ==============") | ||
messages = [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{"type": "text", "text": "Please describe the animal in this image\n"}, | ||
{"type": "image"}, | ||
], | ||
}, | ||
] | ||
prompt = processor.apply_chat_template(messages, add_generation_prompt=True) | ||
image_url = "http://images.cocodataset.org/train2017/000000231895.jpg" | ||
raw_image = Image.open(requests.get(image_url, stream=True).raw) | ||
|
||
inputs = processor(images=raw_image, text=prompt, return_tensors="pt").to("cuda") | ||
output = model.generate(**inputs, max_new_tokens=100) | ||
print(processor.decode(output[0], skip_special_tokens=True)) | ||
print("==========================================") | ||
|
||
# Save to disk compressed. | ||
SAVE_DIR = model_id.split("/")[1] + "-W4A16-G128" | ||
model.save_pretrained(SAVE_DIR, save_compressed=True) | ||
processor.save_pretrained(SAVE_DIR) |
Oops, something went wrong.