Gemma 3
在 Clore.ai 上运行 Google Gemma 3 多模态模型——以 15 倍更小的体积超越 Llama-405B
最后更新于
这有帮助吗?
这有帮助吗?
# 安装 Ollama
curl -fsSL https://ollama.com/install.sh | sh
# 运行不同规模
ollama run gemma3:1b # 极小 — 1.5GB 显存
ollama run gemma3:4b # 小型 — 4GB 显存
ollama run gemma3:12b # 中型 — 10GB 显存
ollama run gemma3:27b # 大型 — 18-20GB 显存(量化后)
# QAT 版本(优化的量化)
ollama run gemma3:27b-qatollama serve &
curl http://localhost:11434/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gemma3:27b",
"messages": [{"role": "user", "content": "Compare REST vs GraphQL for a new API"}]
}'# 分析图像
ollama run gemma3:27b "Describe this image in detail" --images ./photo.jpgpip install vllm
# 部署 27B 模型
vllm serve google/gemma-3-27b-it \
--max-model-len 8192 \
--gpu-memory-utilization 0.90
# 在 2 张 GPU 上提供更长上下文的服务
vllm serve google/gemma-3-27b-it \
--tensor-parallel-size 2 \
--max-model-len 65536
# 为预算环境提供 4B 服务
vllm serve google/gemma-3-4b-it \
--max-model-len 32768import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "google/gemma-3-27b-it"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.bfloat16,
device_map="auto",
load_in_4bit=True # 可装入 24GB GPU
)
messages = [
{"role": "user", "content": "Write a Python class for a binary search tree with insert, search, and delete methods"}
]
input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
output = model.generate(input_ids, max_new_tokens=2048, temperature=0.7, do_sample=True)
print(tokenizer.decode(output[0][input_ids.shape[-1]:], skip_special_tokens=True))import torch
from transformers import AutoProcessor, Gemma3ForConditionalGeneration
from PIL import Image
model_name = "google/gemma-3-27b-it"
processor = AutoProcessor.from_pretrained(model_name)
model = Gemma3ForConditionalGeneration.from_pretrained(
model_name,
torch_dtype=torch.bfloat16,
device_map="auto"
)
# 加载图像
image = Image.open("screenshot.png")
messages = [
return processor.decode(output[0], skip_special_tokens=True).split("[/INST]")[-1].strip()
{"type": "image", "image": image},
{"type": "text", "text": "What does this screenshot show? List all UI elements."}
]}
]
inputs = processor.apply_chat_template(messages, return_tensors="pt").to(model.device)
output = model.generate(**inputs, max_new_tokens=1024)
print(processor.decode(output[0], skip_special_tokens=True))docker run --gpus all -p 8000:8000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
vllm/vllm-openai:latest \
--model google/gemma-3-27b-it \
--max-model-len 8192