Llama 4(Scout 与 Maverick)
在 Clore.ai 的 GPU 上运行 Meta 的 Llama 4 Scout 与 Maverick MoE 模型
最后更新于
这有帮助吗?
这有帮助吗?
# 安装 Ollama
curl -fsSL https://ollama.com/install.sh | sh
# 运行 Scout(量化,约 12GB 显存)
ollama run llama4-scout
# 如需更长上下文(占用更多显存)
ollama run llama4-scout --ctx-size 32768# 在后台启动服务器
ollama serve &
# 拉取模型
ollama pull llama4-scout
# 通过与 OpenAI 兼容的 API 查询
curl http://localhost:11434/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "llama4-scout",
"messages": [{"role": "user", "content": "用三句话解释 MoE 架构"}]
}'# 安装 vLLM
pip install vllm
# 在单 GPU 上(量化)部署 Scout
vllm serve meta-llama/Llama-4-Scout-17B-16E-Instruct \
--max-model-len 32768 \
--gpu-memory-utilization 0.90
# 在 2 张 GPU 上部署 Scout(更长上下文)
vllm serve meta-llama/Llama-4-Scout-17B-16E-Instruct \
--tensor-parallel-size 2 \
--max-model-len 128000 \
--gpu-memory-utilization 0.90
# 在 4 张 GPU 上部署 Maverick
vllm serve meta-llama/Llama-4-Maverick-17B-128E-Instruct \
--tensor-parallel-size 4 \
--max-model-len 65536from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1", api_key="dummy")
response = client.chat.completions.create(
model="meta-llama/Llama-4-Scout-17B-16E-Instruct",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "写一个用于计算斐波那契数的 Python 函数"}
],
temperature=0.7,
max_tokens=1024
)
print(response.choices[0].message.content)import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "meta-llama/Llama-4-Scout-17B-16E-Instruct"
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 使用 4-bit 量化
)
messages = [
{"role": "system", "content": "你是一个乐于助人的编码助理。"},
{"role": "user", "content": "用 FastAPI 编写一个管理待办事项的 REST API"}
]
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))# 使用 vLLM Docker 镜像
docker run --gpus all -p 8000:8000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
vllm/vllm-openai:latest \
--model meta-llama/Llama-4-Scout-17B-16E-Instruct \
--max-model-len 32768