本文基于 Qualcomm 官方文档整理
官方文档: https://docs.qualcomm.com/doc/80-70015-15BY/topic/aiml-developer-workflow.html
文档编号: 80-70015-15BY Rev: AE
更新时间: 2026年1月26日
前言 高通(Qualcomm)作为移动计算和边缘AI领域的领导者,提供了一套完整的AI/ML开发者工作流程。本文将详细介绍如何在高通平台上从预训练模型到最终部署的完整流程,特别适合物联网和嵌入式开发者系统学习。
官方培训视频(29分56秒)可在上述链接观看。
一、工作流程概览 1.1 完整流程图 高通AI/ML开发者工作流程包含5个核心阶段:
1 2 3 ┌─────────────┐ ┌──────────────────┐ ┌─────────────────┐ ┌────────────────────────┐ ┌────────┐ │ 预训练模型 │ → │ 编译并优化模型 │ → │ 优化后的模型 │ → │ 编译/定制AI/ ML应用程序 │ → │ 部署 │ └─────────────┘ └──────────────────┘ └─────────────────┘ └────────────────────────┘ └────────┘
1.2 培训课程介绍
加入我们来观看本次培训课程,该培训重点介绍 Qualcomm Linux AI 开发者工作流程 。
专为物联网领域的开发者和专业人士量身定制的,满足您对提高部署设备上 AI 解决方案的理解和技能。
1.3 两步核心流程 Qualcomm Linux 中的 AI/ML 开发者工作流程主要分为两个步骤:
步骤
名称
详细说明
步骤 1
编译并优化模型
编译并优化来自第三方 AI 框架的模型,以便在 Qualcomm 硬件上高效运行。例如,可以将 TensorFlow 模型导出为 TFLite 模型。或者,使用硬件特定的定制对性能和精确度进行量化和微调。
步骤 2
编译应用程序,使用优化后的模型运行设备推理
将 AI 模型集成到用例 pipeline 中。交叉编译应用程序,用以生成使用依赖库的可执行二进制文件。
二、概述 - AI 硬件与软件 2.1 AI 硬件 高通提供三种AI硬件加速器,满足不同场景需求:
硬件
特点
适用场景
Qualcomm Kryo™ CPU
业界领先的CPU,具有高性能和卓越的能效
通用AI推理、预处理/后处理
Qualcomm Adreno GPU
适合在需要平衡功耗与性能的情况下执行AI工作负载,可通过OpenCL内核加速
图像处理、中等复杂度模型
Qualcomm Hexagon 张量处理器 (HTP)
又称NPU/DSP/HMX,适合以低功耗、高性能执行AI工作负载,需量化到支持的精度
深度学习推理、低功耗场景
2.2 AI 软件 AI 堆栈包含各种 SDK,以便利用 AI 硬件加速器的强大功能。开发者可以使用自己选择的一种 SDK 来部署 AI 工作负载。
1 2 3 4 5 6 7 8 9 10 11 ┌─────────────────────────────────────────────────────────────────────────┐ │ 应用程序 │ ├─────────────────────────────────────────────────────────────────────────┤ │ 框架转换器 │ 运行时框架 │ 低级库 │ AI 引擎 │ ├─────────────────────────────────────────────────────────────────────────┤ │ 开源:TensorFlow Lite │ TensorFlow │ PyTorch │ ONNX │ ├─────────────────────────────────────────────────────────────────────────┤ │ Qualcomm:TFLite │ AI Engine Direct (QNN) │ Neural Processing (SNPE) │ ├─────────────────────────────────────────────────────────────────────────┤ │ 后端库:GPU 后端库 │ NPU 后端库 │ CPU 后端库 │ └─────────────────────────────────────────────────────────────────────────┘
开发者可以从 ONNX、PyTorch、TensorFlow 或 TFLite 引入模型,并使用 Qualcomm AI SDK 在 Qualcomm AI 硬件 - HTP (NPU)、GPU、CPU 上高效运行这些模型。
2.3 TFLite 运行时 TFLite 模型可以在使用以下 Delegate 进行加速的条件下在 Qualcomm 硬件上本地执行:
Delegate
加速硬件
AI Engine Direct Delegate (QNN Delegate)
CPU、GPU 和 HTP
XNNPACK Delegate
CPU
GPU Delegate
GPU
2.4 Qualcomm Neural Processing Engine SDK (SNPE)
Qualcomm Neural Processing Engine (SNPE) 是一种用于执行深度神经网络的软件加速 runtime。SNPE 提供相关工具来对神经网络进行转换、量化,并在 CPU、GPU 和 HTP 等硬件加速器上对其进行加速。
SNPE 核心功能:
模型转换(DLC格式)
模型量化(FP32 → INT8/INT16)
多硬件加速器支持
性能分析工具
2.5 Qualcomm AI Engine Direct (QNN)
Qualcomm AI Engine Direct 是 Qualcomm 芯片组和 AI 加速核心中适用于 AI/ML 用例场景的一种软件架构。该架构旨在提供统一的 API,模块化和可扩展的预加速库,从而基于这种可重用的结构打造全栈 AI 解决方案。它可为 Qualcomm Neural Processing SDK、TFLite AI Engine Direct Delegate 等 runtime 提供支持。
QNN 架构特点:
统一 API
模块化设计
可扩展预加速库
支持多种 runtime
三、步骤1:编译并优化模型 3.1 目标 将来自第三方AI框架的模型编译为高通硬件友好的格式,实现高效推理。
3.2 支持的框架
框架
转换目标
工具
TensorFlow
TFLite
TensorFlow Lite Converter
PyTorch
ONNX → TFLite
ONNX Converter
ONNX
TFLite/DLC
SNPE/QNN
Caffe
DLC
SNPE
3.3 优化技术 3.3.1 量化(Quantization) 将浮点模型转换为定点模型,减少内存占用和计算量:
量化方式对比:
方式
精度损失
性能提升
适用场景
训练后量化(PTQ)
较小
2-4x
快速部署
量化感知训练(QAT)
最小
2-4x
高精度要求
动态量化
无
1.5-2x
模型敏感
3.3.2 模型剪枝(Pruning) 移除冗余参数,减小模型体积:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import tensorflow_model_optimization as tfmot prune_low_magnitude = tfmot.sparsity.keras.prune_low_magnitude pruning_params = { 'pruning_schedule' : tfmot.sparsity.keras.ConstantSparsity( target_sparsity=0.5 , begin_step=0 , frequency=100 ) } model_for_pruning = prune_low_magnitude(model, **pruning_params)
3.3.3 算子融合(Operator Fusion) 合并多个算子减少内存访问:
1 Conv + BN + ReLU → FusedConvBNReLU
3.4 QNN 工具链
工具
功能
qnn-model-lib-generator
生成模型库
qnn-context-binary-generator
生成上下文二进制
qnn-net-run
运行推理
qnn-profile
性能分析
3.5 实战:TFLite模型优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import tensorflow as tf converter = tf.lite.TFLiteConverter.from_saved_model('saved_model' ) tflite_model = converter.convert() converter.optimizations = [tf.lite.Optimize.DEFAULT] converter.representative_dataset = representative_data_gen converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8] converter.inference_input_type = tf.int8 converter.inference_output_type = tf.int8 quantized_model = converter.convert() with open('model_quantized.tflite' , 'wb' ) as f: f.write(quantized_model)
四、步骤2:编译应用程序 4.1 目标 将AI模型集成到应用pipeline中,交叉编译生成可执行文件。
4.2 应用架构 1 2 3 4 5 6 7 8 9 10 11 ┌──────────────────────────────────────────────────────┐ │ AI Application │ ├──────────────────────────────────────────────────────┤ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │ │ 预处理 │→│ 模型推理 │→│ 后处理 │ │ │ └──────────┘ └──────────┘ └──────────┘ │ ├──────────────────────────────────────────────────────┤ │ Input Pipeline │ QNN / TFLite Runtime │ Output │ ├──────────────────────────────────────────────────────┤ │ Qualcomm AI Engine │ └──────────────────────────────────────────────────────┘
4.3 依赖库
库
功能
libQNN
QNN推理引擎
libSNPE
SNPE推理引擎
libOpenCL
GPU加速
libhexagon_nn
DSP加速
4.4 交叉编译流程 1 2 3 4 5 6 7 8 9 10 11 12 export ANDROID_NDK=/path/to/ndkexport TOOLCHAIN=$ANDROID_NDK /build/cmake/android.toolchain.cmake cmake -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN \ -DANDROID_ABI=arm64-v8a \ -DANDROID_PLATFORM=android-29 \ -B build cmake --build build
4.5 CMake示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 cmake_minimum_required (VERSION 3.10 )project (ims_perception)set (CMAKE_CXX_STANDARD 17 )find_package (QNN REQUIRED)add_executable (ims_app src/main.cpp src/preprocess.cpp src/inference.cpp src/postprocess.cpp )target_link_libraries (ims_app QNN::QNNRuntime QNN::QNNBackend ${OpenCL_LIBRARIES} )
五、部署 5.1 部署架构 1 2 3 4 5 6 7 8 9 10 11 12 ┌─────────────────────────────────────────────────────┐ │ 设备端 │ ├─────────────────────────────────────────────────────┤ │ ┌─────────────┐ ┌─────────────┐ │ │ │ App APK │ │ Model │ │ │ │ (Java/C++) │ │ (.tflite) │ │ │ └─────────────┘ └─────────────┘ │ ├─────────────────────────────────────────────────────┤ │ AI Engine Runtime │ ├─────────────────────────────────────────────────────┤ │ CPU │ GPU │ DSP │ NPU │ └─────────────────────────────────────────────────────┘
5.2 部署步骤 1 2 3 4 5 6 7 8 9 10 11 adb push model.tflite /data/local/tmp/ adb push ims_app /data/local/tmp/ adb shell chmod +x /data/local/tmp/ims_app adb shell /data/local/tmp/ims_app
5.3 性能调优 性能分析工具:
工具
用途
Snapdragon Profiler
GPU/CPU/DSP分析
QNN Profiler
模型层分析
SimplePerf
系统性能分析
优化建议:
优化方向
方法
内存
使用共享内存、零拷贝
计算
选择合适后端(DSP/NPU)
流水线
异步推理、多线程
批处理
动态batch size
六、完整示例:DMS疲劳检测部署 6.1 模型准备 1 2 3 4 5 6 7 8 wget https://example.com/dms_model.pb python convert_model.py --input dms_model.pb --output dms_model.tflite python quantize_model.py --input dms_model.tflite --output dms_model_int8.tflite
6.2 应用代码结构 1 2 3 4 5 6 7 8 9 10 11 12 13 14 dms_app/ ├── CMakeLists.txt ├── src/ │ ├── main.cpp │ ├── camera.cpp │ ├── face_detector.cpp │ ├── eye_tracker.cpp │ ├── fatigue_eval.cpp │ └── inference.cpp ├── models/ │ ├── face_detection.tflite │ └── fatigue_model_int8.tflite └── include/ └── dms_types.h
6.3 推理代码示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include "tensorflow/lite/c/common.h" #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" class DMSInference {public : bool Initialize (const std::string& model_path) { model_ = tflite::FlatBufferModel::BuildFromFile (model_path.c_str ()); if (!model_) return false ; tflite::InterpreterBuilder builder (*model_, resolver_) ; builder (&interpreter_); interpreter_->AllocateTensors (); return true ; } void RunInference (const cv::Mat& input, float * output) { Preprocess (input); interpreter_->Invoke (); float * output_data = interpreter_->typed_output_tensor <float >(0 ); memcpy (output, output_data, output_size_ * sizeof (float )); }private : std::unique_ptr<tflite::FlatBufferModel> model_; std::unique_ptr<tflite::Interpreter> interpreter_; tflite::ops::builtin::BuiltinOpResolver resolver_; };
七、常见问题与解决方案 7.1 模型转换问题
问题
原因
解决方案
算子不支持
QNN不支持某些算子
使用自定义算子或替换
精度下降
量化导致精度损失
使用QAT或调整量化范围
内存不足
模型过大
剪枝、知识蒸馏
7.2 性能问题
问题
原因
解决方案
推理慢
使用CPU后端
切换到DSP/NPU
延迟高
内存拷贝过多
使用零拷贝
功耗高
GPU利用率高
平衡CPU/DSP/GPU
7.3 调试技巧 1 2 3 4 5 6 7 8 9 10 qnn-net-run --model model.tflite --info qnn-profile --model model.tflite --output profile.json qnn-net-run --model model.tflite --backend QnnCpuBackend.so qnn-net-run --model model.tflite --backend QnnGpuBackend.so qnn-net-run --model model.tflite --backend QnnDspBackend.so
八、总结 高通AI/ML开发者工作流程为边缘AI部署提供了完整的解决方案:
阶段
关键点
模型优化
量化、剪枝、算子融合
应用编译
交叉编译、库链接
部署运行
后端选择、性能调优
最佳实践:
使用INT8量化获得最佳性能
优先使用DSP/NPU后端(HTP)
使用Snapdragon Profiler进行性能分析
实现流水线并行提高吞吐量
参考资料
Qualcomm 官方文档: https://docs.qualcomm.com/doc/80-70015-15BY/topic/aiml-developer-workflow.html
Qualcomm AI Engine Direct SDK: https://developer.qualcomm.com/software/qualcomm-ai-engine-direct-sdk
Qualcomm Neural Processing SDK (SNPE): https://developer.qualcomm.com/software/qualcomm-neural-processing-sdk
TensorFlow Lite Model Optimization: https://www.tensorflow.org/lite/performance/model_optimization
Snapdragon Profiler: https://developer.qualcomm.com/software/snapdragon-profiler
本文整理自 Qualcomm 官方文档 80-70015-15BY,文档最后更新于 2026年1月26日