Skip to content

[Tracing] Support tracing of Gemma3 [#1248] #1373

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

Merged
merged 22 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bccc54b
Add Gemma3 model tracing support
kelkelcheng Apr 23, 2025
2877679
Refactor Gemma3 tracing imports and update function signatures for cl…
kelkelcheng Apr 23, 2025
0e322b6
[Tracing] Better runtime error messages (#1307)
kylesayrs Apr 23, 2025
b5be87f
Remove debug print statement
kelkelcheng Apr 27, 2025
0683e1e
[Tests] Fix test case; update structure (#1375)
dsikka Apr 23, 2025
7c0771b
fix: Make Recipe.model_dump() output compatible with model_validate()…
ved1beta Apr 23, 2025
11c0e9c
Add: documentation for enhanced `save_pretrained` parameters (#1377)
rahul-tuli Apr 23, 2025
353174c
Revert "fix: Make Recipe.model_dump() output compatible .... (#1378)
rahul-tuli Apr 24, 2025
1aebb7a
AWQ resolved mappings -- ensure shapes align (#1372)
brian-dellabetta Apr 24, 2025
cc665ac
Update w4a16_actorder_weight.yaml lmeval config (#1380)
dbarbuzzi Apr 24, 2025
82db147
[WIP] Add AWQ Asym e2e test case (#1374)
dsikka Apr 24, 2025
d3c0f0a
Bump version; set ct version (#1381)
dsikka Apr 25, 2025
addef4e
bugfix AWQ with Llama models and python 3.9 (#1384)
brian-dellabetta Apr 25, 2025
773fe7f
awq -- hotfix to missing kwargs (#1395)
brian-dellabetta Apr 28, 2025
11138ba
Exclude images from package (#1397)
kylesayrs Apr 29, 2025
8b9f7c4
add gemma3 example
kylesayrs Apr 29, 2025
ea481be
Merge remote-tracking branch 'origin' into kylesayrs/gemma3-example
kylesayrs Apr 29, 2025
81c5799
add back labels check
kylesayrs Apr 29, 2025
0da3931
Merge remote-tracking branch 'origin' into kylesayrs/gemma3-example
kylesayrs Apr 29, 2025
c236f32
Merge branch 'kylesayrs/gemma3-example' into kc/gemma-3-tracing-support
kelkelcheng Apr 29, 2025
94e07cc
Merge branch 'main' into kc/gemma-3-tracing-support
kylesayrs May 2, 2025
61cc2f9
Merge branch 'main' into kc/gemma-3-tracing-support
kylesayrs May 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions examples/multimodal_vision/gemma3_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import requests
import torch
from PIL import Image
from transformers import AutoProcessor

from llmcompressor import oneshot
from llmcompressor.modifiers.quantization import GPTQModifier
from llmcompressor.transformers.tracing import TraceableGemma3ForConditionalGeneration

# Load model.
model_id = "google/gemma-3-4b-it"
model = TraceableGemma3ForConditionalGeneration.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",
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)
6 changes: 5 additions & 1 deletion src/llmcompressor/transformers/tracing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from .gemma3 import (
Gemma3ForConditionalGeneration as TraceableGemma3ForConditionalGeneration,
)
from .llava import (
LlavaForConditionalGeneration as TraceableLlavaForConditionalGeneration,
)
Expand All @@ -11,12 +14,13 @@
Idefics3ForConditionalGeneration as TraceableIdefics3ForConditionalGeneration,
)
from .qwen2_5_vl import (
Qwen2_5_VLForConditionalGeneration as TraceableQwen2_5_VLForConditionalGeneration
Qwen2_5_VLForConditionalGeneration as TraceableQwen2_5_VLForConditionalGeneration,
)
from .debug import get_model_class

__all__ = [
"get_model_class",
"TraceableGemma3ForConditionalGeneration",
"TraceableLlavaForConditionalGeneration",
"TraceableMllamaForConditionalGeneration",
"TraceableQwen2VLForConditionalGeneration",
Expand Down
Loading