跳到正文
Skillpedia
部署与云(DevOps)12k · #11536

minicpm5-deploy-transformers

它能做什么

这个技能指导你的 AI 用一个 Python 脚本,借助 Hugging Face Transformers 库运行 MiniCPM5。适合希望无需设置服务器就能快速测试的人员。

适用场景

  • ✓你想用一个简短的 Python 脚本从 MiniCPM5 生成文本的时候。
  • ✓你只有一块 GPU,并想以 bfloat16 运行模型的时候。
  • ✓你没有 GPU,需要在 CPU 上运行模型的时候。

技能链接

https://github.com/OpenBMB/MiniCPM/blob/main/skills/minicpm5-deploy-transformers/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/OpenBMB/MiniCPM/blob/main/skills/minicpm5-deploy-transformers/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请为我编写一个简短的 Python 脚本,运行 MiniCPM5 并回答一个问题,不需要服务器。”

每周 5 个新技能,1 分钟读完。

工作原理

这个技能安装所需库,加载模型和分词器,构建聊天提示,并在一个脚本中生成答案。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Deploy MiniCPM5-1B and MiniCPM5-2B with HF Transformers

One-shot Python generation. No server. Works on a single GPU (bfloat16) or CPU only (fp32).

Required input

Var Example Default
MODEL_PATH openbmb/MiniCPM5-2B or local dir required; openbmb/MiniCPM5-1B also works
MODE think or nothink (nothink is 1B-only) think

Steps

1. Install (once)

pip install -U "transformers>=5.6,<6" "torch>=2.11" accelerate     # latest (CUDA 13.x driver hosts)
# pip install -U "transformers==4.57.3" "torch==2.7.1" accelerate  # fallback for CUDA 12.x driver hosts

2. Run

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_path = "${MODEL_PATH}"      # ← replace
tok = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(
    model_path,
    torch_dtype=torch.bfloat16,    # CPU users: torch.float32 + device_map="cpu"
    device_map="auto",
).eval()

messages = [{"role": "user", "content": "用一句话解释什么是 GQA。"}]
inputs = tok.apply_chat_template(
    messages,
    add_generation_prompt=True,
    enable_thinking=True,
    return_tensors="pt",
    return_dict=True,
).to(model.device)

with torch.no_grad():
    out = model.generate(
        **inputs,
        max_new_tokens=1024,
        do_sample=True,
        temperature=1.0,
        top_p=0.95,
    )

prompt_len = inputs["input_ids"].shape[-1]
print(tok.decode(out[0][prompt_len:], skip_special_tokens=True))

For CPU only: change torch_dtype=torch.float32, device_map="cpu". Keep enable_thinking=True and temperature=1.0 for MiniCPM5-2B; use enable_thinking=False and temperature=0.7 only for MiniCPM5-1B No-think mode.

Sampling defaults

Mode enable_thinking temperature top_p
MiniCPM5-2B Think True 1.0 0.95
MiniCPM5-1B Think True 0.9 0.95
MiniCPM5-1B No-think False 0.7 0.95

Validate

A coherent answer to 1+1=? (e.g. "2" or "答案是 2").

LoRA inference

from peft import PeftModel
base = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.bfloat16, device_map="auto")
model = PeftModel.from_pretrained(base, "/path/to/adapter").eval()

Adapters from any of the minicpm5-finetune-* skills load directly with no surgery.

When NOT to use

  • Need an OpenAI-compatible HTTP server → minicpm5-deploy-vllm or minicpm5-deploy-sglang
  • Apple Silicon → minicpm5-deploy-mlx is faster
  • CPU-only or low-VRAM laptop → minicpm5-deploy-llama-cpp with Q4_K_M is faster

Reference

docs/deployment/transformers.md

常见问题

没有 GPU 也能运行吗?

可以。这个技能说明在 CPU 上使用 float32,并将设备设置为 cpu。

它支持思考模式吗?

支持。这个技能包含 enable_thinking 设置,并说明 nothink 模式仅适用于 1B 模型。

需要哪些版本?

这个技能要求 transformers 5.6 或更新版本(但低于 6),以及 torch 2.11 或更新版本。

详情

作者的原始描述

Run MiniCPM5-1B or MiniCPM5-2B with Hugging Face Transformers for one-shot Python generation on GPU (bfloat16) or CPU (float32). Use when the user wants a quick Python script, no server, no extra deps, or asks for "Transformers", "AutoModelForCausalLM", "model.generate" with MiniCPM5.

repo: OpenBMB/MiniCPM

热度

GitHub 星标
12k
排名
#11536
许可证
Apache-2.0
更新时间
2026/9/21

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能