自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
NNAPI 驅動程式實作最佳做法
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本頁說明實作 Neural Networks API (NNAPI) 驅動程式的最佳做法,讓應用程式開發人員廣泛採用 NNAPI。
縮短啟動時間
如果驅動程式在首次使用時轉換模型的權重,請確認驅動程式支援編譯快取,這樣可縮短應用程式啟動時所需的編譯時間。這點很重要,因為如果應用程式啟動時間過長,可能會避免使用硬體加速功能。舉例來說,有些應用程式會產生超過 100 MB 的權重,每次啟動應用程式時轉換這些權重會造成資源浪費。
減少最小延遲時間
為確保模型使用硬體加速功能,請務必減少驅動程式中的最小延遲時間。許多應用程式會使用多次執行的小型模型,如果執行工作負載的最低延遲時間過高 (例如幾毫秒),模型可能會在 CPU 上執行工作負載,這只需要一或兩毫秒,而不會使用硬體加速功能。請小心耗時的執行緒同步作業。
使用 NN HAL SchedTune 群組
從 Android 11 以上版本開始,AOSP 包含專屬的 NN HAL SchedTune 群組,可讓跨程序 NN HAL 程序使用大核心,類似於預先定義 top-app
cgroup 中的同程序實作。使用這個 SchedTune 群組可減少驅動程式負擔,特別是對於小型模型。
如要使用 SchedTune 群組,請在 NN HAL 程序的 init.rc
檔案中新增下列程式碼行:
writepid /dev/stune/nnapi-hal/tasks
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# NNAPI driver implementation best practices\n\n| **Deprecated:** Starting in Android 15, the\n| [NNAPI (NDK API)](https://developer.android.com/ndk/guides/neuralnetworks) is deprecated. The Neural Networks HAL interface\n| continues to be supported.\n|\n| For more information, see the\n| [NNAPI Migration Guide](https://developer.android.com/ndk/guides/neuralnetworks/migration-guide).\n\nThis page describes best practices for implementing Neural Networks API (NNAPI)\ndrivers to allow for broad adoption of the NNAPI by app developers.\n\nKeep startup times short\n------------------------\n\nIf your driver transforms the weights of a model on first use, make sure the\ndriver supports compilation caching, which reduces the time used for compilation\nwhen an app starts. This is important as apps might avoid using hardware\nacceleration if start-up times are too long. For example, some apps have\nmore than 100 MB of weights and transforming these each time the app\nlaunches is wasteful.\n\nReduce minimal latency\n----------------------\n\nTo ensure that models use hardware acceleration, it's important to reduce the\nminimal latency in drivers. Many apps use small models that are executed\nmultiple times and if the minimal latency to execute a workload is too high,\nsuch as a few milliseconds, models might run the workload on the CPU, which only\ntakes one or two milliseconds, instead of\nusing hardware accelerations. Be careful of costly thread synchronization.\n\nUse the NN HAL SchedTune group\n------------------------------\n\nFrom Android 11 or higher, AOSP includes a dedicated\nNN HAL\n[SchedTune](https://android.googlesource.com/kernel/msm/+/android-msm-marlin-3.18-nougat-dr1/Documentation/scheduler/sched-tune.txt)\ngroup that allows interprocess NN HAL processes to use big\ncores, similar to same-process implementation within the predefined\n`top-app` [cgroup](/docs/core/perf/cgroups). Using this\nSchedTune group reduces driver overhead, especially for small models.\n\nTo use the SchedTune group, add the following line to the `init.rc` file of\nthe NN HAL process: \n\n writepid /dev/stune/nnapi-hal/tasks"]]