Gemma 3 4b It Quantized.w4a16
基於google/gemma-3-4b-it的量化版本,採用INT4權重量化和FP16激活量化,優化推理效率
Downloads 195
Release Time : 6/4/2025
Model Overview
Gemma 3 4B指令調優模型的量化版本,支持視覺-文本輸入和文本輸出,適用於多模態推理任務
Model Features
高效量化
採用INT4權重量化和FP16激活量化,顯著降低計算資源需求
多模態支持
支持圖像和文本的聯合輸入,實現視覺-語言理解與生成
高性能推理
通過vLLM後端優化,實現高效的推理速度
高精度保持
量化後平均性能恢復率達97.42%,視覺任務恢復率達98.86%
Model Capabilities
圖像內容理解
多模態對話
視覺問答
文本生成
Use Cases
視覺內容分析
圖像描述生成
分析輸入圖像並生成自然語言描述
在MMMU驗證集達到40.11%準確率
圖表理解
解析圖表內容並回答相關問題
在ChartQA達到49.32%準確率
智能對話
多模態聊天助手
結合圖像和文本輸入進行自然對話
🚀 gemma-3-4b-it-quantized.w4a16
這是一個量化版本的模型,基於 google/gemma-3-4b-it 進行了優化,能夠在特定條件下更高效地運行推理任務。
🚀 快速開始
本模型可使用 vLLM 後端進行高效部署,以下是使用示例:
from vllm import LLM, SamplingParams
from vllm.assets.image import ImageAsset
from transformers import AutoProcessor
# Define model name once
model_name = "RedHatAI/gemma-3-4b-it-quantized.w4a16"
# Load image and processor
image = ImageAsset("cherry_blossom").pil_image.convert("RGB")
processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
# Build multimodal prompt
chat = [
{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "What is the content of this image?"}]},
{"role": "assistant", "content": []}
]
prompt = processor.apply_chat_template(chat, add_generation_prompt=True)
# Initialize model
llm = LLM(model=model_name, trust_remote_code=True)
# Run inference
inputs = {"prompt": prompt, "multi_modal_data": {"image": [image]}}
outputs = llm.generate(inputs, SamplingParams(temperature=0.2, max_tokens=64))
# Display result
print("RESPONSE:", outputs[0].outputs[0].text)
vLLM 也支持與 OpenAI 兼容的服務,更多詳細信息請參閱 文檔。
✨ 主要特性
- 模型架構:基於
google/gemma-3-4b-it
,輸入為視覺 - 文本,輸出為文本。 - 模型優化:
- 權重量化:採用 INT4 數據類型。
- 激活量化:採用 FP16 數據類型。
- 發佈日期:2025 年 4 月 6 日。
- 版本:1.0。
- 模型開發者:RedHatAI。
該模型是 google/gemma-3-4b-it 的量化版本,通過將權重量化為 INT4 數據類型,可使用 vLLM >= 0.8.0 進行推理。
📦 安裝指南
文檔中未提及具體安裝步驟,暫不提供。
💻 使用示例
基礎用法
from vllm import LLM, SamplingParams
from vllm.assets.image import ImageAsset
from transformers import AutoProcessor
# Define model name once
model_name = "RedHatAI/gemma-3-4b-it-quantized.w4a16"
# Load image and processor
image = ImageAsset("cherry_blossom").pil_image.convert("RGB")
processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
# Build multimodal prompt
chat = [
{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "What is the content of this image?"}]},
{"role": "assistant", "content": []}
]
prompt = processor.apply_chat_template(chat, add_generation_prompt=True)
# Initialize model
llm = LLM(model=model_name, trust_remote_code=True)
# Run inference
inputs = {"prompt": prompt, "multi_modal_data": {"image": [image]}}
outputs = llm.generate(inputs, SamplingParams(temperature=0.2, max_tokens=64))
# Display result
print("RESPONSE:", outputs[0].outputs[0].text)
📚 詳細文檔
模型創建
此模型使用 llm-compressor 創建,以下是創建代碼:
模型創建代碼
import base64
from io import BytesIO
import torch
from datasets import load_dataset
from transformers import AutoProcessor, Gemma3ForConditionalGeneration
from llmcompressor.modifiers.quantization import GPTQModifier
from llmcompressor.transformers import oneshot
# Load model.
model_id = "google/gemma-3-4b-it"
model = Gemma3ForConditionalGeneration.from_pretrained(
model_id,
device_map="auto",
torch_dtype="auto",
)
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
# Oneshot arguments
DATASET_ID = "neuralmagic/calibration"
DATASET_SPLIT = {"LLM": "train[:512]"}
NUM_CALIBRATION_SAMPLES = 512
MAX_SEQUENCE_LENGTH = 2048
# Load dataset and preprocess.
ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
ds = ds.shuffle(seed=42)
dampening_frac=0.05
def data_collator(batch):
assert len(batch) == 1, "Only batch size of 1 is supported for calibration"
item = batch[0]
collated = {}
import torch
for key, value in item.items():
if isinstance(value, torch.Tensor):
collated[key] = value.unsqueeze(0)
elif isinstance(value, list) and isinstance(value[0][0], int):
# Handle tokenized inputs like input_ids, attention_mask
collated[key] = torch.tensor(value)
elif isinstance(value, list) and isinstance(value[0][0], float):
# Handle possible float sequences
collated[key] = torch.tensor(value)
elif isinstance(value, list) and isinstance(value[0][0], torch.Tensor):
# Handle batched image data (e.g., pixel_values as [C, H, W])
collated[key] = torch.stack(value) # -> [1, C, H, W]
elif isinstance(value, torch.Tensor):
collated[key] = value
else:
print(f"[WARN] Unrecognized type in collator for key={key}, type={type(value)}")
return collated
# Recipe
recipe = [
GPTQModifier(
targets="Linear",
ignore=["re:.*lm_head.*", "re:.*embed_tokens.*", "re:vision_tower.*", "re:multi_modal_projector.*"],
sequential_update=True,
sequential_targets=["Gemma3DecoderLayer"],
dampening_frac=dampening_frac,
)
]
SAVE_DIR=f"{model_id.split('/')[1]}-quantized.w4a16"
# Perform oneshot
oneshot(
model=model,
tokenizer=model_id,
dataset=ds,
recipe=recipe,
max_seq_length=MAX_SEQUENCE_LENGTH,
num_calibration_samples=NUM_CALIBRATION_SAMPLES,
trust_remote_code_model=True,
data_collator=data_collator,
output_dir=SAVE_DIR
)
模型評估
該模型使用 lm_evaluation_harness 進行 OpenLLM v1 文本基準測試,評估命令如下:
評估命令
OpenLLM v1
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
準確性
類別 | 指標 | google/gemma-3-4b-it | RedHatAI/gemma-3-4b-it-quantized.w4a16 | 恢復率 (%) |
---|---|---|---|---|
OpenLLM V1 | ARC Challenge | 56.57% | 56.57% | 100.00% |
OpenLLM V1 | GSM8K | 76.12% | 72.33% | 95.02% |
OpenLLM V1 | Hellaswag | 74.96% | 73.35% | 97.86% |
OpenLLM V1 | MMLU | 58.38% | 56.33% | 96.49% |
OpenLLM V1 | Truthfulqa (mc2) | 51.87% | 50.81% | 97.96% |
OpenLLM V1 | Winogrande | 70.32% | 68.82% | 97.87% |
OpenLLM V1 | 平均得分 | 64.70% | 63.04% | 97.42% |
視覺評估 | MMMU (val) | 39.89% | 40.11% | 100.55% |
視覺評估 | ChartQA | 50.76% | 49.32% | 97.16% |
視覺評估 | 平均得分 | 45.33% | 44.72% | 98.86% |
📄 許可證
本模型的許可證為 gemma。
Clip Vit Large Patch14
CLIP是由OpenAI開發的視覺-語言模型,通過對比學習將圖像和文本映射到共享的嵌入空間,支持零樣本圖像分類
圖像生成文本
C
openai
44.7M
1,710
Clip Vit Base Patch32
CLIP是由OpenAI開發的多模態模型,能夠理解圖像和文本之間的關係,支持零樣本圖像分類任務。
圖像生成文本
C
openai
14.0M
666
Siglip So400m Patch14 384
Apache-2.0
SigLIP是基於WebLi數據集預訓練的視覺語言模型,採用改進的sigmoid損失函數,優化了圖像-文本匹配任務。
圖像生成文本
Transformers

S
google
6.1M
526
Clip Vit Base Patch16
CLIP是由OpenAI開發的多模態模型,通過對比學習將圖像和文本映射到共享的嵌入空間,實現零樣本圖像分類能力。
圖像生成文本
C
openai
4.6M
119
Blip Image Captioning Base
Bsd-3-clause
BLIP是一個先進的視覺-語言預訓練模型,擅長圖像描述生成任務,支持條件式和非條件式文本生成。
圖像生成文本
Transformers

B
Salesforce
2.8M
688
Blip Image Captioning Large
Bsd-3-clause
BLIP是一個統一的視覺-語言預訓練框架,擅長圖像描述生成任務,支持條件式和無條件式圖像描述生成。
圖像生成文本
Transformers

B
Salesforce
2.5M
1,312
Openvla 7b
MIT
OpenVLA 7B是一個基於Open X-Embodiment數據集訓練的開源視覺-語言-動作模型,能夠根據語言指令和攝像頭圖像生成機器人動作。
圖像生成文本
Transformers English

O
openvla
1.7M
108
Llava V1.5 7b
LLaVA 是一款開源多模態聊天機器人,基於 LLaMA/Vicuna 微調,支持圖文交互。
圖像生成文本
Transformers

L
liuhaotian
1.4M
448
Vit Gpt2 Image Captioning
Apache-2.0
這是一個基於ViT和GPT2架構的圖像描述生成模型,能夠為輸入圖像生成自然語言描述。
圖像生成文本
Transformers

V
nlpconnect
939.88k
887
Blip2 Opt 2.7b
MIT
BLIP-2是一個視覺語言模型,結合了圖像編碼器和大型語言模型,用於圖像到文本的生成任務。
圖像生成文本
Transformers English

B
Salesforce
867.78k
359
Featured Recommended AI Models
Llama 3 Typhoon V1.5x 8b Instruct
專為泰語設計的80億參數指令模型,性能媲美GPT-3.5-turbo,優化了應用場景、檢索增強生成、受限生成和推理任務
大型語言模型
Transformers Supports Multiple Languages

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一個基於SODA數據集訓練的超小型對話模型,專為邊緣設備推理設計,體積僅為Cosmo-3B模型的2%左右。
對話系統
Transformers English

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基於RoBERTa架構的中文抽取式問答模型,適用於從給定文本中提取答案的任務。
問答系統 Chinese
R
uer
2,694
98