使用 Qualcomm AI Engine Direct 后端构建和运行 Llama 3 8B Instruct¶
本教程演示了如何将 Llama 3 8B Instruct 导出到 Qualcomm AI Engine Direct 后端并在 Qualcomm 设备上运行模型。
前置条件¶
如果您尚未设置 ExecuTorch 仓库和环境,请按照设置 ExecuTorch 设置仓库和开发环境。
阅读使用 Qualcomm AI Engine Direct 后端构建和运行 ExecuTorch 页面,了解如何在 Qualcomm 设备上使用 Qualcomm AI Engine Direct 后端导出和运行模型。
按照executorch llama 的 README 文档,了解如何通过 ExecuTorch 在移动设备上运行 llama 模型。
具有 16GB RAM 的 Qualcomm 设备
我们正在持续优化内存使用,以确保兼容内存较低的设备。
Qualcomm AI Engine Direct SDK 的版本为 2.28.0 或更高。
说明¶
步骤 1:从 Spin Quant 准备模型检查点和优化矩阵¶
对于 Llama 3 的分词器和检查点,请参考 https://github.com/meta-llama/llama-models/blob/main/README.md 以获取关于如何下载
tokenizer.model
、consolidated.00.pth
和params.json
的进一步说明。要获取优化矩阵,请参考 GitHub 上的 SpinQuant。您可以在“Quantized Models”部分下载优化后的旋转矩阵。请选择 LLaMA-3-8B/8B_W4A16KV16_lr_1.5_seed_0。
步骤 2:使用 Qualcomm AI Engine Direct 后端导出到 ExecuTorch¶
在设备上部署 Llama 3 等大型语言模型存在以下挑战
模型尺寸过大,无法完全载入设备内存进行推理。
模型加载和推理时间长。
量化困难。
为应对这些挑战,我们实现了以下解决方案
使用
--pt2e_quantize qnn_16a4w
对激活和权重进行量化,从而减小模型在磁盘上的尺寸并减轻推理时的内存压力。使用
--num_sharding 8
将模型分片为多个子部分。执行图转换,将操作转换为更适合加速器的操作或将其分解。
使用
--optimized_rotation_path <path_to_optimized_matrix>
应用 Spin Quant 的 R1 和 R2 以提高准确性。使用
--calibration_data "<|start_header_id|>system<|end_header_id|..."
确保在 Llama 3 8B Instruct 量化过程中,校准包含提示模板中的特殊 token。有关提示模板的更多详细信息,请参考meta llama3 instruct 的模型卡。
要使用 Qualcomm AI Engine Direct 后端导出 Llama 3 8B Instruct,请确保满足以下条件
宿主机器具有超过 100GB 的内存(RAM + 交换空间)。
整个过程需要几个小时。
# Please note that calibration_data must include the prompt template for special tokens.
python -m examples.models.llama.export_llama -t <path_to_tokenizer.model>
llama3/Meta-Llama-3-8B-Instruct/tokenizer.model -p <path_to_params.json> -c <path_to_checkpoint_for_Meta-Llama-3-8B-Instruct> --use_kv_cache --qnn --pt2e_quantize qnn_16a4w --disable_dynamic_shape --num_sharding 8 --calibration_tasks wikitext --calibration_limit 1 --calibration_seq_length 128 --optimized_rotation_path <path_to_optimized_matrix> --calibration_data "<|start_header_id|>system<|end_header_id|>\n\nYou are a funny chatbot.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nCould you tell me about Facebook?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
步骤 3:在配备 Qualcomm SoC 的 Android 智能手机上调用运行时¶
为 Android 构建带 Qualcomm AI Engine Direct 后端的 executorch
cmake \ -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK_ROOT}/build/cmake/android.toolchain.cmake" \ -DANDROID_ABI=arm64-v8a \ -DCMAKE_INSTALL_PREFIX=cmake-android-out \ -DCMAKE_BUILD_TYPE=Release \ -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \ -DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \ -DEXECUTORCH_BUILD_QNN=ON \ -DQNN_SDK_ROOT=${QNN_SDK_ROOT} \ -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \ -DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \ -Bcmake-android-out . cmake --build cmake-android-out -j16 --target install --config Release
为 Android 构建 llama runner
cmake \
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK_ROOT}"/build/cmake/android.toolchain.cmake \
-DANDROID_ABI=arm64-v8a \
-DCMAKE_INSTALL_PREFIX=cmake-android-out \
-DCMAKE_BUILD_TYPE=Release -DPYTHON_EXECUTABLE=python \
-DEXECUTORCH_BUILD_QNN=ON \
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
-Bcmake-android-out/examples/models/llama examples/models/llama
cmake --build cmake-android-out/examples/models/llama -j16 --config Release
通过 adb shell 在 Android 上运行 前提条件:确保您已在手机的开发者选项中启用 USB 调试
3.1 连接您的 Android 手机
3.2 我们需要将所需的 QNN 库推送到设备。
# make sure you have write-permission on below path.
DEVICE_DIR=/data/local/tmp/llama
adb shell mkdir -p ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnHtp.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnSystem.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnHtpV69Stub.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnHtpV73Stub.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnHtpV75Stub.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/hexagon-v69/unsigned/libQnnHtpV69Skel.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/hexagon-v73/unsigned/libQnnHtpV73Skel.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/hexagon-v75/unsigned/libQnnHtpV75Skel.so ${DEVICE_DIR}
3.3 将模型、分词器和 llama runner 二进制文件上传到手机
adb push <model.pte> ${DEVICE_DIR}
adb push <tokenizer.model> ${DEVICE_DIR}
adb push cmake-android-out/lib/libqnn_executorch_backend.so ${DEVICE_DIR}
adb push cmake-out-android/examples/models/llama/llama_main ${DEVICE_DIR}
3.4 运行模型
adb shell "cd ${DEVICE_DIR} && ./llama_main --model_path <model.pte> --tokenizer_path <tokenizer.model> --prompt \"<|start_header_id|>system<|end_header_id|>\n\nYou are a funny chatbot.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nCould you tell me about Facebook?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n\" --seq_len 128"
您应该会看到消息
<|start_header_id|>system<|end_header_id|>\n\nYou are a funny chatbot.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nCould you tell me about Facebook?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\nHello! I'd be delighted to chat with you about Facebook. Facebook is a social media platform that was created in 2004 by Mark Zuckerberg and his colleagues while he was a student at Harvard University. It was initially called "Facemaker" but later changed to Facebook, which is a combination of the words "face" and "book". The platform was initially intended for people to share their thoughts and share information with their friends, but it quickly grew to become one of the
未来展望¶
提升 Llama 3 Instruct 的性能
减轻推理时的内存压力,以支持 12GB Qualcomm 设备
支持更多 LLMs
常见问题¶
如果在复现本教程时遇到任何问题,请在 ExecuTorch 仓库上提交 GitHub Issue 并使用 #qcom_aisw
标签。