Views
No views yet
1from vllm import LLM, SamplingParams
2from vllm.assets.image import ImageAsset
3from transformers import AutoProcessor
4
5# Define model name once
6model_name = "RedHatAI/gemma-3-12b-it-quantized.w8a8"
7
8# Load image and processor
9image = ImageAsset("cherry_blossom").pil_image.convert("RGB")
10processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
11
12# Build multimodal prompt
13chat = [
14 {"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "What is the content of this image?"}]},
15 {"role": "assistant", "content": []}
16]
17prompt = processor.apply_chat_template(chat, add_generation_prompt=True)
18
19# Initialize model
20llm = LLM(model=model_name, trust_remote_code=True)
21
22# Run inference
23inputs = {"prompt": prompt, "multi_modal_data": {"image": [image]}}
24outputs = llm.generate(inputs, SamplingParams(temperature=0.2, max_tokens=64))
25
26# Display result
27print("RESPONSE:", outputs[0].outputs[0].text)1import base64
2from io import BytesIO
3import torch
4from datasets import load_dataset
5from transformers import AutoProcessor, Gemma3ForConditionalGeneration
6from llmcompressor.modifiers.quantization import GPTQModifier
7from llmcompressor.transformers import oneshot
8
9
10# Load model.
11model_id = "google/gemma-3-12b-it"
12model = Gemma3ForConditionalGeneration.from_pretrained(
13 model_id,
14 device_map="auto",
15 torch_dtype="auto",
16)
17processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
18
19# Oneshot arguments
20DATASET_ID = "neuralmagic/calibration"
21DATASET_SPLIT = {"LLM": "train[:1024]"}
22NUM_CALIBRATION_SAMPLES = 1024
23MAX_SEQUENCE_LENGTH = 2048
24
25# Load dataset and preprocess.
26ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
27ds = ds.shuffle(seed=42)
28
29dampening_frac=0.01
30
31def data_collator(batch):
32 assert len(batch) == 1, "Only batch size of 1 is supported for calibration"
33 item = batch[0]
34 collated = {}
35 import torch
36
37
38 for key, value in item.items():
39 if isinstance(value, torch.Tensor):
40 collated[key] = value.unsqueeze(0)
41 elif isinstance(value, list) and isinstance(value[0][0], int):
42 # Handle tokenized inputs like input_ids, attention_mask
43 collated[key] = torch.tensor(value)
44 elif isinstance(value, list) and isinstance(value[0][0], float):
45 # Handle possible float sequences
46 collated[key] = torch.tensor(value)
47 elif isinstance(value, list) and isinstance(value[0][0], torch.Tensor):
48 # Handle batched image data (e.g., pixel_values as [C, H, W])
49 collated[key] = torch.stack(value) # -> [1, C, H, W]
50 elif isinstance(value, torch.Tensor):
51 collated[key] = value
52 else:
53 print(f"[WARN] Unrecognized type in collator for key={key}, type={type(value)}")
54
55 return collated
56
57
58
59# Recipe
60recipe = [
61 GPTQModifier(
62 targets="Linear",
63 ignore=["re:.*lm_head.*", "re:.*embed_tokens.*", "re:vision_tower.*", "re:multi_modal_projector.*"],
64 sequential_update=True,
65 sequential_targets=["Gemma3DecoderLayer"],
66 dampening_frac=dampening_frac,
67 )
68]
69
70SAVE_DIR=f"{model_id.split('/')[1]}-quantized.w8a8"
71
72# Perform oneshot
73oneshot(
74 model=model,
75 tokenizer=model_id,
76 dataset=ds,
77 recipe=recipe,
78 max_seq_length=MAX_SEQUENCE_LENGTH,
79 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
80 trust_remote_code_model=True,
81 data_collator=data_collator,
82 output_dir=SAVE_DIR
83)lm_eval \
--model vllm \
--model_args pretrained="<model_name>",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=<n>,gpu_memory_utilization=0.8,enable_chunked_prefill=True,trust_remote_code=True,enforce_eager=True \
--tasks openllm \
--batch_size auto| Category | Metric | google/gemma-3-12b-it | RedHatAI/gemma-3-12b-it-quantized.w8a8 | Recovery (%) |
|---|---|---|---|---|
| OpenLLM V1 | ARC Challenge | 68.43% | 68.43% | 100.00% |
| GSM8K | 88.10% | 87.72% | 99.57% | |
| Hellaswag | 83.76% | 83.53% | 99.73% | |
| MMLU | 72.15% | 71.65% | 99.30% | |
| Truthfulqa (mc2) | 58.13% | 58.44% | 100.54% | |
| Winogrande | 79.40% | 78.77% | 99.20% | |
| Average Score | 74.99% | 74.76% | 99.68% | |
| Vision Evals | MMMU (val) | 48.78% | 47.44% | 97.25% |
| ChartQA | 68.08% | 67.04% | 98.47% | |
| Average Score | 58.43% | 57.24% | 97.86% |