Bug_Factory_w/mythos_fast
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

mythos_fast 昇腾 NPU 迁移与服务化推理指南(中文版)

本目录包含 mythos_fast(WeiboAI/VibeThinker-3B 工具调用微调版,Qwen2ForCausalLM 架构)的昇腾 NPU 适配交付物。 原始模型权重位于 /mnt/old_data/whl/models/1/Shadow0482/mythos_fast(官方仅发布 GGUF 格式), 已通过 PyTorch 生态(vLLM-Ascend) 技术栈迁移至本地华为昇腾 Ascend910 NPU。

1. 项目概览

项目说明
模型Shadow0482/mythos_fast(VibeThinker-3B 微调,约 3B 参数)
架构Qwen2ForCausalLM(带注意力 q/k/v bias 的 Qwen2 变体,36 层 / hidden 2048 / 16 头 / 2 KV 头)
原始权重仅 GGUF(VibeThinker-3B.BF16.gguf 6.1GB,434 张量;另含 Q2~Q8 量化版)
适配方式GGUF → HF safetensors 无损转换(max_diff=0 抽样校验)→ vLLM-Ascend 加载
迁移框架PyTorch 2.9.0 + torch_npu 2.9.0.post1 + vLLM 0.18.0 + vLLM-Ascend 0.18.0
推理硬件昇腾 Ascend910(单卡,bf16 权重约 6.1GB)
服务框架FastAPI + uvicorn(OpenAI 兼容 HTTP 服务)
本机实测中文/英文对话、工具调用场景推理全部通过(见 results/test_results.txt)

2. 目录结构

mythos_fast-npu/
├── README.md                   # 本指南(中文)
├── requirements.txt            # 运行环境依赖
├── inference.py                # 服务化推理脚本(OpenAI 兼容 HTTP 服务)
├── scripts/
│   └── gguf_to_safetensors.py  # GGUF → HF safetensors 无损转换脚本
├── tests/
│   ├── test_inference.py       # 端到端测试用例(6 项)
│   └── test_results.txt        # 测试结果记录
├── results/
│   ├── test_results.json       # 测试结果 (JSON)
│   ├── test_results.txt        # 测试结果 (文本)
│   └── server.log              # 服务运行日志
└── assets/
    ├── npu_device_call.png     # NPU 设备调用截图
    ├── model_result.png        # 测试结果截图
    ├── agent_workflow.png      # 适配过程截图
    └── _make_screenshots.py    # 截图渲染脚本

3. 环境要求

  • 硬件:昇腾 Ascend910 系列(至少 1 卡,模型约需 7GB 显存)
  • OS:openEuler / Ubuntu(aarch64)
  • CANN:≥ 25.x(本项目 25.5.0)
  • Python:3.10 – 3.11
  • PyTorch:2.9.0(与 CANN 配套)+ torch_npu 2.9.0.post1
  • vLLM / vLLM-Ascend:0.18.0(V1 引擎)
  • transformers:4.57.x(Qwen2 注意力 bias 支持)

4. 快速开始

4.1 安装依赖

# 加载 CANN 环境
source /usr/local/Ascend/ascend-toolkit/set_env.sh

# 安装依赖(华为镜像源)
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
pip install -r requirements.txt

4.2 权重转换(GGUF → safetensors,一次性)

vLLM-Ascend 0.18 不支持 GGUF 量化格式(报错 gguf quantization is currently not supported in npu),必须先将 BF16 GGUF 权重转换为 HF safetensors。

cd /mnt/old_data/whl/models/shipei-code/mythos_fast-npu
python scripts/gguf_to_safetensors.py \
    --gguf /mnt/old_data/whl/models/1/Shadow0482/mythos_fast/VibeThinker-3B.BF16.gguf \
    --out  /mnt/old_data/whl/models/1/Shadow0482/mythos_fast-hf

转换产物(mythos_fast-hf/):

  • model.safetensors(5.75 GiB,434 张量,BF16 权重 + FP32 norm)
  • config.json / generation_config.json
  • tokenizer.json / tokenizer_config.json(官方 Qwen3 词表,与 GGUF 逐项比对一致; 追加 GGUF 独有 token:<think>=151665、</think>=151666、<|PAD_TOKEN|>=151667; chat_template 从 GGUF 原样提取)

4.3 启动服务化推理

cd /mnt/old_data/whl/models/shipei-code/mythos_fast-npu
python inference.py --ckpt /mnt/old_data/whl/models/1/Shadow0482/mythos_fast-hf \
    --port 8000 --npus 0

脚本自动选择 npu:0(torch_npu 可用时)。启动过程:初始化 vLLM 引擎 → 加载 6.1GB 权重到 NPU(约 1~2 分钟,含算子图编译)→ 监听 HTTP 服务。

提示:默认端口 8000,可用 --port 修改(本机 8000/8001 已被其他模型服务 占用,本交付实测使用 8002);--npus 指定物理 NPU(需选空闲卡, 如被占用的卡空闲显存不足会导致 gpu_memory_utilization 检查失败, 可用 --gpu-memory-utilization 0.5 降低要求); 首次启动若遇算子编译慢可加 --enforce-eager。

4.4 运行测试用例

cd /mnt/old_data/whl/models/shipei-code/mythos_fast-npu
python tests/test_inference.py --port 8000

测试覆盖:服务启动 / 健康检查 / NPU 设备调用 / 模型列表 / 中文对话 / 英文对话(工具调用场景)/ 原始 prompt 生成,结果写入 results/。

4.5 调用示例(OpenAI 兼容)

# 对话补全
curl -s http://127.0.0.1:8000/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"mythos_fast","messages":[{"role":"user","content":"请用一句话介绍什么是昇腾NPU"}],"max_tokens":128}'

# 原始 prompt 生成
curl -s http://127.0.0.1:8000/infer \
  -H 'Content-Type: application/json' \
  -d '{"prompt":"1+1=?","max_tokens":32}'

5. 适配要点与踩坑记录

#问题现象解法
1vLLM-Ascend 不支持 GGUFgguf quantization is currently not supported in npuGGUF → safetensors 无损转换(BF16 直接保留)
2paddlex 插件冲突import vllm 时 C++ std::bad_alloc 静默崩溃VLLM_PLUGINS 白名单仅加载 ascend 系插件
3GGUF 维度顺序GGUF shape 数组是 torch shape 的反转按 memmap 实际行数 reshape,转换后抽样校验 max_diff=0
4模型带注意力 biasq/k/v bias 非零(k bias 均值 6.68),丢弃会损精度当前 transformers 4.57.6 与 vLLM 0.18 Qwen2 实现原生支持 bias
5显式 stop 词崩溃vLLM V1 引擎在 Ascend 后端对 stop 列表 use-after-free不传 stop,EOS(`<
6tokenizer 差异GGUF 词表含官方 Qwen3 没有的 <think>/`<PAD_TOKEN

6. 服务接口说明

接口方法说明
/healthGET健康检查 + NPU 设备信息
/deviceGETNPU 设备调用信息(device_count / device_name / 显存)
/v1/modelsGET模型列表(OpenAI 兼容)
/v1/chat/completionsPOST对话补全(OpenAI 兼容,支持 messages 多轮)
/inferPOST简单文本生成(prompt in → text out)

7. 实测结果

  • 模型加载:6.1GB 权重 → npu:1,约 47 秒(冒烟测试),服务化启动约 1~2 分钟(含算子图编译)
  • 推理设备:Ascend910_9382 @ npu:1(本机 16 卡,torch.npu.device_count()=16)
  • 服务端口:8002(8000/8001 被其他模型服务占用)
  • 测试用例:7/7 全部通过(service_start / health / device / list_models / chat_zh / chat_en / infer)
  • 中文对话:✅ 435 字符生成,2.15s(首次含算子编译)
  • 英文/工具调用场景:✅ 598 字符生成,1.85s
  • /infer:✅ 0.43s
  • NPU 设备调用:✅ /device 返回 device_type=Ascend NPU、device_name=Ascend910_9382
  • 详细结果见 results/test_results.txt / results/test_results.json,截图见 assets/。

8. 代码注释约定

本目录所有 Python 脚本(inference.py、scripts/gguf_to_safetensors.py、 tests/test_inference.py、assets/_make_screenshots.py)均包含 2 个 # Bug_Factory 标记注释:

  • 文件首行(shebang 之后紧邻行)——标识文件用途;
  • 文件中部随机一行(不在前 10 行 / 最后 10 行)——按 Bug_Factory 注释规范标记的随机锚点。 此外,代码中保留了针对本模型适配过程中实际踩坑点的 # Bug_Factory 上下文注释(如 paddlex 插件冲突、GGUF 不支持、stop 词崩溃等)。