-
Notifications
You must be signed in to change notification settings - Fork 202
[Example] [VLM] Gemma3n #1696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kylesayrs
wants to merge
10
commits into
main
Choose a base branch
from
kylesayrs/gemma3n
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[Example] [VLM] Gemma3n #1696
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
22a4189
support gemma3n
kylesayrs 677614d
style
kylesayrs 17decae
cleanup, add test
kylesayrs d69da65
add example
kylesayrs 09e6cfe
add gemma3n vision example
kylesayrs 355ad0c
Merge remote-tracking branch 'origin' into kylesayrs/autowrap-support…
kylesayrs ab6f510
account for other environments
kylesayrs 8b8140a
do not untie
kylesayrs e5e8c60
add more to ignore list
kylesayrs 85d1634
remove breakpoint
kylesayrs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,90 @@ | ||
import requests | ||
import torch | ||
from PIL import Image | ||
from transformers import AutoProcessor, Gemma3nForConditionalGeneration | ||
|
||
from llmcompressor import oneshot | ||
from llmcompressor.modifiers.quantization import GPTQModifier | ||
from llmcompressor.utils import dispatch_for_generation | ||
|
||
# Load model. | ||
model_id = "google/gemma-3n-E2B-it" | ||
model = Gemma3nForConditionalGeneration.from_pretrained(model_id, 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", | ||
ignore=[ | ||
"re:.*embed_audio.*", | ||
"re:.*embed_vision.*", | ||
"re:.*audio_tower.*", | ||
"re:.*vision_tower.*", | ||
"re:.*altup.*", | ||
"re:.*lm_head.*", | ||
"re:.*laurel.*", | ||
"re:model\.language_model\.layers\.\d+\.per_layer_input_gate", | ||
"re:model\.language_model\.layers\.\d+\.per_layer_projection", | ||
"model.language_model.per_layer_model_projection", | ||
], | ||
), | ||
] | ||
|
||
# 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, | ||
# gemma3n has broken weight offloading which is required by the sequential pipeline | ||
pipeline="basic", | ||
# gemma3n does not support untying word embeddings | ||
tie_word_embeddings=True, | ||
) | ||
|
||
# Confirm generations of the quantized model look sane. | ||
print("========== SAMPLE GENERATION ==============") | ||
dispatch_for_generation(model) | ||
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) | ||
|
||
# Note: compile is disabled: https://github.com/huggingface/transformers/issues/38333 | ||
inputs = processor(images=raw_image, text=prompt, return_tensors="pt").to("cuda") | ||
output = model.generate(**inputs, max_new_tokens=100, disable_compile=True) | ||
print(processor.decode(output[0], skip_special_tokens=True)) | ||
print("==========================================") | ||
|
||
# # Save to disk compressed. | ||
# SAVE_DIR = model_id.rstrip("/").split("/")[-1] + "-W4A16-G128" | ||
# model.save_pretrained(SAVE_DIR, save_compressed=True) | ||
# processor.save_pretrained(SAVE_DIR) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.