技术博客

摩尔线程MTT GPU运维实战:MUSA环境配置与Kubernetes集成

详解摩尔线程MTT S80/S4000 GPU的运维配置:MUSA SDK安装与环境配置、mthreads-gmi监控工具使用、PyTorch MUSA适配安装、Kubernetes Device Plugin部署、vLLM推理引擎在MUSA上的部署方法,以及常见兼容性问题的排查解决。

摩尔线程Moore ThreadsMUSA国产GPUKubernetesAI推理AI基础设施

摩尔线程(Moore Threads)是国内自主研发 GPU 的代表之一,MUSA(Moore Threads Unified System Architecture)是其独立设计的 GPU 计算架构。相比昇腾和海光 DCU,摩尔线程在渲染和 AI 推理场景有独特优势,国内互联网和云厂商中有一定部署量。

MUSA 软件栈

应用层:  PyTorch / TensorFlow / 推理引擎(vLLM-MUSA / llama.cpp-MUSA)

MUSA SDK:
  musa runtime(类 CUDA runtime)
  musaBLAS / musaDNN(类 cuBLAS / cuDNN)
  mcc 编译器(类 nvcc,编译 .mu 文件)

MUSA 驱动:MoorEHub 驱动包

硬件:   MTT S80 / MTT S4000 / MTT S3000

一、系统准备与驱动安装

# 支持的操作系统
# - Ubuntu 20.04 / 22.04 LTS
# - CentOS 7.9 / 8.x

# 检查设备识别
lspci | grep -i "Moore Threads\|MTT"
# 0000:01:00.0 VGA compatible controller: Moore Threads Technology Co., Ltd MTT S80

# 检查内核版本(建议 5.15+)
uname -r

# 安装依赖
apt-get install -y dkms gcc make python3 python3-pip \
    linux-headers-$(uname -r) libelf-dev
# 从摩尔线程开发者平台下载驱动
# https://developer.mthreads.com/

# 文件示例:musa-driver-2.7.0-ubuntu22.04-x86_64.deb

# 安装驱动
dpkg -i musa-driver-*.deb
apt-get install -f   # 解决依赖

# 重启
reboot

# 验证驱动
lsmod | grep mtgpu   # 摩尔线程 GPU 内核模块
ls /dev/musa*        # MUSA 设备文件

二、安装 MUSA SDK

# 下载 MUSA SDK(开发工具包)
# musa-toolkit-2.7.0-ubuntu22.04-x86_64.deb

dpkg -i musa-toolkit-*.deb

# 配置环境变量
cat >> ~/.bashrc << 'EOF'
# MUSA 环境变量
export MUSA_HOME=/usr/local/musa
export PATH=$MUSA_HOME/bin:$PATH
export LD_LIBRARY_PATH=$MUSA_HOME/lib:$LD_LIBRARY_PATH
EOF
source ~/.bashrc

# 验证 SDK 安装
mcc --version     # MUSA C 编译器版本
musacc --version  # MUSA C++ 编译器

# 运行 SDK 示例
cd /usr/local/musa/samples/
make
./bin/vector_add   # 应该输出向量加法结果

三、mthreads-gmi 监控工具

mthreads-gmi 是摩尔线程的 GPU 监控工具(类 nvidia-smi,功能更基础)。

# 基本信息
mthreads-gmi

# 典型输出:
# +------------------------------------------------------------------+
# | MUSA Version: 2.7.0      Driver Version: 2.7.0                  |
# |----------------------------------+----------+--------------------+
# | GPU  Name        Persistence-M   | Bus-Id   | Disp.A  Volatile  |
# | Fan  Temp  Perf  Pwr:Usage/Cap   | Memory-Usage        GPU-Util |
# |==================================+==========+====================|
# |  0   MTT S80         Off         | 00000000:01:00.0 Off|   N/A  |
# | N/A   42C    P0    65W / 200W    | 2048MiB / 48000MiB |   35%  |
# +------------------------------------------------------------------+

# 常用查询
mthreads-gmi -q                     # 详细信息
mthreads-gmi --query-gpu=temperature.gpu,utilization.gpu,memory.used
mthreads-gmi --query-compute-apps=pid,used_memory  # 查看进程

# 持续监控
mthreads-gmi dmon                   # 持续监控模式(类 nvidia-smi dmon)
watch -n 2 mthreads-gmi

# 进程管理(杀死占用 GPU 的进程)
mthreads-gmi --query-compute-apps=pid | tail -n+2 | awk '{print $1}' | xargs kill -9

四、安装 PyTorch MUSA

# 方法1:使用摩尔线程官方 PyTorch(推荐)
# 下载:https://developer.mthreads.com/musa/sdk
# 文件示例:torch_musa-2.3.0+musa2.7.0-cp310-linux_x86_64.whl

pip3 install torch_musa-*.whl

# 方法2:源码编译(从 GitHub:https://github.com/MooreThreads/torch_musa)
git clone https://github.com/MooreThreads/torch_musa.git
cd torch_musa
pip3 install -r requirements.txt
bash scripts/install_pytorch.sh   # 安装兼容版 PyTorch
python3 setup.py install          # 编译 torch_musa

# 验证 MUSA PyTorch
python3 << 'EOF'
import torch
import torch_musa

print(f"PyTorch 版本: {torch.__version__}")
print(f"MUSA 可用: {torch.musa.is_available()}")
print(f"MUSA 设备数: {torch.musa.device_count()}")

# 简单测试
x = torch.randn(1000, 1000).to("musa")   # 注意:是 "musa" 不是 "cuda"
y = torch.randn(1000, 1000).to("musa")
z = x @ y
print(f"矩阵乘法成功: {z.shape}")

# 模型迁移(与 CUDA 不同,需要用 .musa() 而非 .cuda())
import torch.nn as nn
model = nn.Linear(512, 256).to("musa")
input_data = torch.randn(32, 512).to("musa")
output = model(input_data)
print(f"推理成功: {output.shape}")
EOF

注意: 摩尔线程与海光 DCU 不同,需要用 .to("musa").musa()不能用 .cuda(),这是代码迁移时最容易踩的坑。


五、vLLM 推理部署(MUSA 版)

# 摩尔线程维护了 vLLM 的 MUSA 分支
git clone https://github.com/MooreThreads/vllm-musa.git
cd vllm-musa

# 安装依赖
pip3 install -r requirements-musa.txt

# 安装 vLLM-MUSA
pip3 install -e .

# 启动 LLM 推理服务(以 Qwen2.5-7B 为例)
python3 -m vllm.entrypoints.openai.api_server \
    --model Qwen/Qwen2.5-7B-Instruct \
    --device musa \                     # 指定 MUSA 设备
    --tensor-parallel-size 2 \          # 2 卡并行
    --dtype bfloat16 \
    --port 8000

# 测试推理
curl http://localhost:8000/v1/chat/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "Qwen/Qwen2.5-7B-Instruct",
        "messages": [{"role": "user", "content": "你好"}],
        "max_tokens": 100
    }'

六、Kubernetes Device Plugin

# mthreads-device-plugin.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: mthreads-device-plugin
  namespace: kube-system
spec:
  selector:
    matchLabels:
      name: mthreads-device-plugin
  template:
    metadata:
      labels:
        name: mthreads-device-plugin
    spec:
      nodeSelector:
        accelerator: mthreads-gpu
      containers:
        - name: mthreads-device-plugin
          image: mthreads/device-plugin:latest
          securityContext:
            privileged: true
          volumeMounts:
            - name: device-plugins
              mountPath: /var/lib/kubelet/device-plugins
            - name: dev
              mountPath: /dev
      volumes:
        - name: device-plugins
          hostPath:
            path: /var/lib/kubelet/device-plugins
        - name: dev
          hostPath:
            path: /dev
# 使用 MUSA GPU 的 Pod
apiVersion: v1
kind: Pod
metadata:
  name: musa-test-pod
spec:
  nodeSelector:
    accelerator: mthreads-gpu
  containers:
    - name: musa-container
      image: my-pytorch-musa:latest
      resources:
        limits:
          mthreads.com/gpu: 1     # 申请 1 个 MTT GPU
      command: ["python3", "inference.py"]

七、常见问题

# 问题1:torch.musa.is_available() 返回 False
# 检查驱动是否加载
lsmod | grep mtgpu
mthreads-gmi   # 如果报错,驱动未正常加载

# 检查设备文件权限
ls -la /dev/musa*
# 如果没有权限,加入 musa 用户组
sudo usermod -aG musa $USER

# 问题2:"MUSA error: no kernel image is available for execution"
# 原因:torch_musa 版本与 MUSA 驱动版本不匹配
mthreads-gmi | grep "MUSA Version"   # 查看驱动版本
python3 -c "import torch_musa; print(torch_musa.__version__)"  # 查看 SDK 版本
# 必须版本对应

# 问题3:多卡推理 OOM
# MTT S80 显存 48GB,S4000 显存更大
# 调整 vLLM tensor_parallel_size,或开启 CPU offload
python3 -m vllm.entrypoints.openai.api_server \
    --model LLM_MODEL \
    --device musa \
    --gpu-memory-utilization 0.85 \   # 控制显存使用率
    --swap-space 4 \                   # CPU swap 空间(GB)
    --tensor-parallel-size 2

# 问题4:代码中写了 .cuda(),在 MUSA 上报错
# 摩尔线程不支持 torch.cuda.* API(与海光 DCU 不同)
# 需要改为 .to("musa") 或 .musa()
# 批量替换脚本:
find ./src -name "*.py" | xargs sed -i \
    's/\.cuda()/.to("musa")/g; s/torch\.cuda\./torch.musa./g'

小结

摩尔线程 MUSA 与海光 DCU 最大的区别是:不支持 torch.cuda. 透明兼容*,必须显式使用 torch.musa.* API,代码迁移工作量比 DCU 大。其优势在于渲染+AI 混合场景和较大的显存(S80 为 48GB GDDR6),适合部署中等规模的 LLM 推理服务。生产建议:优先使用摩尔线程维护的 vLLM-MUSA 分支做推理,避免自己从头适配框架;对于训练场景,需评估 MUSA 算子库的完整度,有缺失的算子会 fallback 到 CPU 导致性能严重下降。