核心内容摘要
《妈妈你真棒》插曲:一曲唱尽心声,拯救你的耳朵与心灵
系统与硬件环境说明模块说明CPU鲲鹏 920 / 930 系列ARM64架构GPU/NPU华为昇腾 910B2 × 2支持BF
INT8量化内存≥ 256GB存储≥ 2TB SSD操作系统openEuler / 麒麟 / 统信UOS国产信创OS驱动CANN ≥
0Ascend Toolkit ≥
0框架环境MindSpore ≥
3torch-npu ≥
1兼容PyTorch
1部署工具XInference ≥
12Ascend ATC 工具
模型来源与基本结构模型名称来源参数量框架当前格式量化类型用途Qwen
B-Base从阿里巴巴官方 / Hugging Face 下载32BPyTorch.bin/.safetensorsFP16 / BF16原始预训练模型Qwen
B-Finetuned在昇腾910B2上进行微调使用MindSpore框架32BMindSpore.ckptINT8 / BF16行业定制微调模型
模型格式转换流程模型部署前需统一格式昇腾平台推荐使用MindIR 或 OM格式。
1️⃣ 从 PyTorch 格式转换到 MindIR 格式方法 1通过MindConverterMindConverter 是昇腾官方提供的跨框架模型转换工具。
mindconverter --in_framework pytorch\--model_file /models/qwen32b/pytorch_model.bin\--input_shape[1,1024]\--output_file /models/qwen32b/qwen32b.mindir方法 2通过ONNX 中转如果MindConverter转换失败可通过ONNX作为中间层# PyTorch - ONNXimporttorch dummy_inputtorch.ones(1,1024,dtypetorch.int
torch.onnx.export(model,dummy_input,qwen32b.onnx,opset_version
# ONNX - MindIRatc --modelqwen32b.onnx\--framework5\--outputqwen32b_mindir\--soc_versionAscend910B22️⃣ MindIR → OM部署优化格式OMOffline Model是昇腾芯片的离线执行优化格式部署时加载性能更优。
atc --modelqwen32b.mindir\--framework1\--outputqwen32b_om\--input_formatND\--input_shapeinput_ids:1,1024\--soc_versionAscend910B2\--precision_modeallow_mix_precision⚙️ 注意soc_version必须与昇腾芯片型号一致。
MindIR 格式支持训练和部署OM 格式仅支持推理。
模型部署方式我们希望部署两个模型Base 与 Finetuned并可在XInference中动态切换。
1️⃣ 环境目录结构/models/ │ ├── qwen32b_base/ │ ├── qwen32b_om.om │ └── config.json │ └── qwen32b_finetune/ ├── qwen32b_finetune_om.om └── config.json2️⃣ 启动 XInference 服务# 启动 Base 模型xinf start --model Qwen
B-Base\--model-path /models/qwen32b_base\--device ascend\--precision bf16\--port8081# 启动 Finetuned 模型xinf start --model Qwen
B-Finetuned\--model-path /models/qwen32b_finetune\--device ascend\--precision int8\--port8082✅ 支持使用 Docker 容器化部署xinf ascend-runtime官方镜像3️⃣ 模型注册配置xinf.json{models:[{name:Qwen
B-Base,path:/models/qwen32b_base/qwen32b_om.om,device:ascend,port:8081,precision:bf16},{name:Qwen
B-Finetuned,path:/models/qwen32b_finetune/qwen32b_finetune_om.om,device:ascend,port:8082,precision:int8}]}
XInference 模型切换与调用1️⃣ 模型动态切换REST APIcurl-X POST http://localhost:8000/api/models/select\-HContent-Type: application/json\-d{model_name: Qwen
B-Finetuned}切换回原始模型curl-X POST http://localhost:8000/api/models/select\-d{model_name: Qwen
B-Base}2️⃣ 推理调用示例Pythonimportrequestsdefquery_model(port,prompt):urlfhttp://localhost:{port}/v1/completionspayload{prompt:prompt,max_tokens:256,temperature:
8}headers{Content-Type:application/json}responserequests.post(url,headersheaders,jsonpayload)returnresponse.json()[choices][0][text]print(Base模型回答)print(query_model(8081,介绍一下鲲鹏920的CPU架构优势))print(\nFinetuned模型回答)print(query_model(8082,解释一下信创生态下AI算法的优势))
模型推理性能与优化优化手段说明效果INT8量化微调模型在导出时使用量化感知训练QAT显存降低约 50%速度提升约
6×双卡并行推理使用两张910B2卡分别部署两个模型实现模型并行调用Pipeline切分模型前向传播分段执行适合显存不足情况XInference异步队列支持多请求并发分发提升吞吐量 2~3倍
七、
常见问题与解决建议问题可能原因解决方案CANN不支持该算子转换时ONNX算子不兼容在atc命令中加--customize_op或使用MindSpore原生模型内存不足模型显存开销大开启INT8量化或分层加载XInference无法加载模型路径或格式不匹配确认.om文件路径与配置文件一致请求延迟高同步阻塞使用异步调用API或多进程部署
八、