一键导入
onnx-model-conversion-and-deployment
MindSpore Lite云侧推理 Ascend 后端离线转换(ONNX → MindIR)与推理部署全流程。覆盖固定 shape、动态分档、纯动态 shape 的转换策略,以及 MindIR 推理验证与部署注意事项。
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
MindSpore Lite云侧推理 Ascend 后端离线转换(ONNX → MindIR)与推理部署全流程。覆盖固定 shape、动态分档、纯动态 shape 的转换策略,以及 MindIR 推理验证与部署注意事项。
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
把开源算法模型适配到 MindSpore Lite 部署管线:按网络结构拆分导出 ONNX、ONNX Runtime 推理验证、ONNX→MindIR 转换、MindSpore Lite 推理实现,并交付文档与常见问题。用户想把某个开源模型迁移到 MSLite 部署时调用。
MindSpore Lite(Ascend)模型性能优化总攻略。做基线/profiling、融合算子改写、推理免拷贝、PTQ int8 量化、精度对齐与归档时调用。本文为总览与索引,细化策略见 references/。
Build configuration, CMake options, cross-compilation and packaging. Use when building MindSpore Lite, configuring CMake, cross-compiling for ARM/iOS/MCU, packaging release archives, or troubleshooting build errors.
Model conversion pipeline, parser development, optimization passes and quantization. Use when converting models to .ms, writing parser code, implementing optimizer passes, or configuring quantization.
Debugging, unit testing, benchmarking and performance analysis. Use when running gtest, benchmark tools, profiling latency or accuracy, diagnosing operator precision issues, delegate fallback, or memory leaks.
Device-side inference with LiteRT, NNACL and hardware delegates. Use for mobile/IoT inference, Android/iOS integration, NPU/GPU/CoreML delegates, Micro codegen for MCU, on-device training, or C/C++/Java/Python API usage with .ms models.
| name | onnx-model-conversion-and-deployment |
| description | MindSpore Lite云侧推理 Ascend 后端离线转换(ONNX → MindIR)与推理部署全流程。覆盖固定 shape、动态分档、纯动态 shape 的转换策略,以及 MindIR 推理验证与部署注意事项。 |
本技能聚焦“MindSpore Lite 云侧推理 Ascend 后端离线转换”场景:输入 ONNX 模型,通过 MindSpore Lite converter_lite 转换工具生成可在 Ascend 执行的 MindIR 模型文件(并在离线阶段完成图编译优化)。
converter_lite --optimize=ascend_oriented 转换失败,需要定位失败节点与修复策略converter_lite 转换工具,转换成固定 shape、动态分档、纯动态 shape。若模型转换失败,需要定位失败节点与修复策略converter_lite(或环境里别名 Convert).mindir 模型文件,支持 Ascend ACL 离线推理--optimize=ascend_oriented检查当前环境是否存在MindSpore Lite二进制转换工具converter_lite(可以通过环境变量中LD_LIBRARY_PATH确认,MindSpore Lite转换工具安装路径),若不存在,需要基于昇腾环境编译MindSpore Lite云侧推理转换工具二进制包,编译流程如下:
export MSLITE_ENABLE_ACL=on
export MSLITE_ENABLE_CLOUD_INFERENCE=on
bash build.sh -I arm64 -j16
编译产物通常在 output/目录下的tar包中
# 固定shape场景(必须配置`--optimize=ascend_oriented`参数)
./converter_lite \
--fmk=ONNX \
--modelFile=prefill.onnx \
--outputFile=prefill \
--optimize=ascend_oriented \
--saveType=MINDIR \
--inputShape="input_ids:1,128;attention_mask:1,128;position_ids:1,128"
# 动态分档场景(必须配置`--optimize=ascend_oriented`参数)
./converter_lite \
--fmk=ONNX \
--modelFile=prefill.onnx \
--outputFile=prefill \
--optimize=ascend_oriented \
--saveType=MINDIR \
--configFile=config.ini
# config.ini
# 动态分档场景配置文件
[acl_build_options]
input_format="ND"
input_shape="input_ids:1,-1;attention_mask:1,-1;position_ids:1,-1"
ge.dynamicDims="128,128,128;256,256,256;512,512,512"
当模型包含动态维度(batch/seq_len/image_size),离线构图往往需要 --inputShape 约束到“可编译形态”:
# 纯动态shape场景(必须配置`--optimize=ascend_oriented`参数)
./converter_lite \
--fmk=ONNX \
--modelFile=model.onnx \
--outputFile=out/model \
--optimize=ascend_oriented \
--saveType=MINDIR \
--configFile=config.ini
# config.ini
# 纯动态shape场景配置文件
[acl_build_options]
input_format="ND"
input_shape="input_ids:1,-1;attention_mask:1,-1;position_ids:1,-1"
## 开启plugin_custom_ops
[ascend_context]
plugin_custom_ops=All
本节给出“converter_lite 离线转换成功后”的推理部署闭环:如何基于现有环境的推理包、加载 MindIR 并完成一次推理。
Ascend 驱动与 CANN
npu-smi info 可正常显示设备信息MindSpore Lite 推理包
python -c "import mindspore_lite; print(mindspore_lite.__file__)"
离线转换成功后,输出目录通常包含:
${model}_graph.mindir${model}_variables/data_0部署时必须保证 *_graph.mindir 与同级的 *_variables/ 目录同时存在,且 data_0 完整。
import numpy as np
import mindspore_lite as mslite
context = mslite.Context()
context.target = ["ascend"]
context.ascend.device_id = 0
model = mslite.Model()
model.build_from_file("model_graph.mindir", mslite.ModelType.MINDIR, context)
inputs = model.get_inputs()
print([(t.name, t.shape, t.dtype) for t in inputs])
# 按模型输入名/shape 构造 numpy 数据(示例)
x0 = np.zeros(inputs[0].shape, dtype=np.int32)
outputs = model.predict([mslite.Tensor(x0)])
print([o.get_data_to_numpy().shape for o in outputs])
部署时关注点:
适用前提:
seq_len(例如 256)推理流程(高层):
input_ids/attention_mask/position_ids(固定 256) → 输出 logits_last 与 present_kv(KV 总长建议直接固定到 512)input_ids[1,1] + attention_mask[1,512] + position_id[1,1] + past_kv → 输出下一 token 的 logits 与新的 present_kv部署时关注点:
-1 维度进入离线构图/推理max_total_lenWARNING: Ascend custom operator path not found
LD_LIBRARY_PATH/ASCEND_OPP_PATH/PYTHONPATH)Custom 节点(CANN 融合算子),必须确保相应 OPP/自定义算子已在当前环境可见build_from_file 很慢/首次编译耗时长
建议以“同一输入的 logits/next_token 一致性”为主线做最小闭环验证,避免在 LLM 场景引入过长序列导致对齐复杂。
CONVERT RESULT SUCCESS:0*_graph.mindir 与同级 *_variables/data_0--optimize=ascend_oriented