g
gcw_coj3XaOd/MooreThreads_MusaCoder-27B
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

MusaCoder-27B - 昇腾 NPU 推理部署

1. 模型简介

模型名称: MooreThreads/MusaCoder-27B

模型链接: HuggingFace

模型描述: MusaCoder-27B 是一个 270 亿参数的代码生成大语言模型,基于 Qwen3_5 架构,支持线性注意力(Linear Attention)和全注意力(Full Attention)混合机制。支持文本生成和多模态(图像+文本)推理。

模型架构: Qwen3_5ForConditionalGeneration

  • 64 层 Transformer(每 4 层含 3 层线性注意力 + 1 层全注意力)
  • 隐藏层维度: 5120
  • 注意力头: 24 (GQA, 4 KV heads)
  • 线性注意力 KV 头: 16 (dim=128), Value 头: 48 (dim=128)
  • 前馈网络维度: 17408
  • 词表大小: 248320
  • 最大位置编码: 262144
  • MTP (Multi-Token Prediction): 1 层

输入规格:

  • 文本: token IDs [seq_len], max 8192 (inference) / 262144 (prefill)
  • 图像: [batch, frames, channels, height, width], 最长边 ≤ 16777216

输出规格:

  • 生成文本 token IDs [gen_len]

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch-npu== 2.9.0昇腾 NPU 后端
vllm>= 0.18.0vLLM 推理引擎 (vllm-ascend)
transformers>= 5.15.1HuggingFace Transformers (需支持 qwen3_5)
sentencepiece>= 0.1.99Tokenizer 依赖
protobuf>= 3.20.0序列化依赖
Pillow>= 9.0.0图像处理
昇腾驱动CANN 8.5.1推荐版本

安装命令:

pip install torch-npu==2.9.0 vllm==0.18.0 transformers>=5.15.1 sentencepiece protobuf Pillow

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

# 验证 vllm-ascend
python3 -c "from vllm import LLM; print('vllm-ascend ready')"

3.2 模型下载

# 方式一:HuggingFace(推荐)
huggingface-cli download MooreThreads/MusaCoder-27B --local-dir /tmp/MooreThreads-MusaCoder-27B

# 方式二:AtomGit 镜像(HuggingFace 下载慢时使用)
# https://ai.gitcode.com/hf_mirrors/MooreThreads/MusaCoder-27B

3.3 运行推理

方式一:vLLM 服务(推荐)

vllm serve /tmp/MooreThreads-MusaCoder-27B \
  --dtype bfloat16 \
  --tensor-parallel-size 2 \
  --max-model-len 8192 \
  --max-num-seqs 16 \
  --enforce-eager \
  --limit-mm-per-prompt '{"image": 1, "video": 0}' \
  --port 8000

方式二:inference.py

python inference.py --model-path /tmp/MooreThreads-MusaCoder-27B --tp-size 2

3.4 推理参数说明

参数类型默认值说明
--model-pathstr/tmp/MooreThreads-MusaCoder-27B模型 checkpoint 路径
--tp-sizeint2NPU 卡数(Tensor Parallel)
--max-model-lenint8192最大序列长度
--portint8000API 服务端口
--imagestrNone多模态推理测试图片路径

3.5 API 调用示例

# 文本生成
curl http://127.0.0.1:8000/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"/tmp/MooreThreads-MusaCoder-27B","messages":[{"role":"user","content":"Write a Python quicksort"}],"max_tokens":256}'

# 多模态推理
curl http://127.0.0.1:8000/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"/tmp/MooreThreads-MusaCoder-27B","messages":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"data:image/jpeg;base64,..."}},{"type":"text","text":"Describe this image"}]}],"max_tokens":256}'

4. 推理成功日志

4.1 模型加载日志

Stage A: Dummy Fast Gate(快速验证)

vllm serve /tmp/MooreThreads-MusaCoder-27B --load-format dummy --dtype bfloat16 \
  --tensor-parallel-size 2 --max-model-len 8192 --max-num-seqs 16 --enforce-eager

[OK] Server started after 129s
[OK] Stage A inference successful!

Stage B: Real-Weight Validation(真实权重验证)

vllm serve /tmp/MooreThreads-MusaCoder-27B \
  --served-model-name MusaCoder-27B \
  --dtype bfloat16 --tensor-parallel-size 2 \
  --max-model-len 8192 --max-num-seqs 16 \
  --enforce-eager --limit-mm-per-prompt '{"image":0,"video":0}' --port 8000

[OK] Model loaded: 12/12 shards (51.0GB)
[OK] KV cache: 236,544 tokens, max concurrency 68.56x
[OK] Engine init: 8.28s (profile + KV cache)
[OK] Application startup complete

4.2 NPU 设备信息(Stage B 真实推理)

+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.5                   Version: 25.5.5                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip  Phy-ID              | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 7     Ascend910           | OK            | 165.1       45                0    / 0             |
| 0     14                  | 0000:0B:00.0  | 0           0    / 0          59623/ 65536         |
+---------------------------+---------------+----------------------------------------------------+
| 7     Ascend910           | OK            | -           44                0    / 0             |
| 1     15                  | 0000:0A:00.0  | 0           0    / 0          59380/ 65536         |
+===========================+===============+====================================================+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 7       0                 | 344520        | VLLMWorker_TP            | 56565                   |
| 7       1                 | 344521        | VLLMWorker_TP            | 56565                   |
+===========================+===============+====================================================+

5. 测试样例及输出结果(Stage B 真实权重)

样例 1:文本生成 - 代码生成

运行命令:

curl -s http://127.0.0.1:8000/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"MusaCoder-27B","messages":[{"role":"user","content":"Write a Python function to compute Fibonacci numbers using dynamic programming."}],"temperature":0.7,"max_tokens":512}'

输出(Stage B 真实权重):

{
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "We are going to write a function that calculates the nth Fibonacci number using dynamic programming. The Fibonacci sequence is defined as: F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) for n >= 2.\\n\\nWe can use dynamic programming to avoid recalculating the same values multiple times...",
      "refusal": null
    },
    "finish_reason": "length"
  }],
  "usage": {"prompt_tokens": 23, "completion_tokens": 512, "total_tokens": 535}
}

性能: 512 tokens in 64.5s → 7.94 tokens/s

样例 2:代码补全

运行命令:

curl -s http://127.0.0.1:8000/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"MusaCoder-27B","messages":[{"role":"user","content":"def quicksort(arr):\\n    \\"\\"\\"Sort an array using quicksort algorithm.\\"\\"\\"\\n    "}],"temperature":0.7,"max_tokens":256}'

输出(Stage B 真实权重):

{
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "We are going to implement the quicksort algorithm. Steps:\\n 1. If the array has 0 or 1 element, it is already sorted.\\n 2. Otherwise, choose a pivot (we will choose the middle element for simplicity)...",
      "refusal": null
    },
    "finish_reason": "length"
  }],
  "usage": {"prompt_tokens": 26, "completion_tokens": 256, "total_tokens": 282}
}

性能: 256 tokens in 34.2s → 7.48 tokens/s

样例 3:知识问答

运行命令:

curl -s http://127.0.0.1:8000/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"MusaCoder-27B","messages":[{"role":"user","content":"What are the main differences between Python and C++? List at least 5 key differences."}],"temperature":0.7,"max_tokens":512}'

输出(Stage B 真实权重):

{
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "We are going to list at least 5 key differences between Python and C++. Let's think about the major aspects: compilation, typing, memory management, performance, and syntax.\\n\\n1. **Compilation vs Interpretation**: C++ is compiled; Python is interpreted...",
      "refusal": null
    },
    "finish_reason": "length"
  }],
  "usage": {"prompt_tokens": 29, "completion_tokens": 512, "total_tokens": 541}
}

性能: 512 tokens in 73.5s → 6.96 tokens/s


6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. NPU 配置说明

  • NPU 型号: Ascend 910
  • NPU 数量: 2 卡
  • Tensor Parallel: TP=2
  • 单卡 HBM: 65536 MB
  • 模型占用 HBM: ~57GB per NPU(51GB 模型权重 + KV cache 开销)
  • 推理吞吐: ~7.0-8.0 tokens/s(BF16, 8192 max tokens)
  • CANN 版本: 8.5.1
  • 最大序列长度: 8192 (inference) / 262144 (prefill capacity)

8. 已知问题

  1. Transformers 版本: 需要 >= 5.15.1 以支持 qwen3_5 模型类型
  2. Graph Compilation: 首次加载需要 ~60s 图编译时间,建议使用 --enforce-eager 跳过
  3. 多模态: 需要下载 preprocessor_config.json 和 tokenizer 文件