使用 Qualcomm AI 引擎 Direct 后端构建和运行 Llama 3 8B Instruct¶
本教程演示了如何导出 Llama 3 8B Instruct 以用于 Qualcomm AI 引擎 Direct 后端,并在 Qualcomm 设备上运行模型。
前提条件¶
如果您尚未设置 ExecuTorch 仓库和环境,请按照“设置 ExecuTorch”来设置仓库和开发环境。
阅读“使用 Qualcomm AI 引擎 Direct 后端构建和运行 ExecuTorch”页面,了解如何在 Qualcomm 设备上使用 Qualcomm AI 引擎 Direct 后端导出和运行模型。
按照executorch llama 的 README 文件,了解如何通过 ExecuTorch 在移动设备上运行 llama 模型。
具有 16GB 内存的 Qualcomm 设备
我们正在不断优化内存使用,以确保与内存较低的设备兼容。
Qualcomm AI 引擎 Direct SDK 的版本为 2.26.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。您可以在“量化模型”部分下载优化的旋转矩阵。请选择 LLaMA-3-8B/8B_W4A16KV16_lr_1.5_seed_0。
步骤 2:使用 Qualcomm AI 引擎 Direct 后端导出到 ExecuTorch¶
在设备上部署像 Llama 3 这样的大型语言模型面临以下挑战
模型尺寸太大,无法装入设备内存进行推理。
模型加载和推理时间长。
量化困难。
为了应对这些挑战,我们实施了以下解决方案
使用
--pt2e_quantize qnn_16a4w
量化激活和权重,从而减小磁盘上的模型大小并减轻推理期间的内存压力。使用
--num_sharding 8
将模型分片为子部分。执行图转换以将操作转换或分解为更适合加速器的操作。
使用
--optimized_rotation_path <优化矩阵路径>
应用 Spin Quant 的 R1 和 R2 以提高准确性。使用
--calibration_data "<|start_header_id|>system<|end_header_id|..."
以确保在量化 Llama 3 8B instruct 期间,校准包括提示模板中的特殊标记。有关提示模板的更多详细信息,请参阅 meta llama3 instruct 的模型卡。
要使用 Qualcomm AI 引擎 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 引擎 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 运行器
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 运行器二进制文件上传到手机
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 设备
支持更多 LLM
常见问题解答¶
如果您在重现本教程时遇到任何问题,请在 ExecuTorch 仓库中提交 github issue 并标记 #qcom_aisw
标签