自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
探索和指派裝置
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 Android 10 中,Neural Networks API (NNAPI) 引進了可讓機器學習架構程式庫和應用程式取得可用裝置相關資訊的函式,並指定要執行模型的裝置。透過提供可用裝置相關資訊,可讓應用程式取得裝置上所安裝驅動程式的確切版本,從而避免已知的不相容問題。如果允許應用程式指定要使用哪些裝置執行模型的不同部分,應用程式就能針對自己部署所在的產品進行最佳化。
NN HAL 1.2 實作功能必須支援裝置探索和指派功能。
實作
如要支援 NNAPI 中的裝置探索和指派功能,請在 IDevice.hal
中實作 getType
和 getVersionString
,讓架構取得裝置類型和驅動程式版本。
針對每部裝置,將類型指定為下列任一類別,如 types.hal
中的 DeviceType
所述。
OTHER
:不屬於任何其他類別的裝置,包括異質介面,也就是用於管理多個裝置 (可能屬於不同類型) 的單一 IDevice
介面。具有異質介面的驅動程式也應公開與個別裝置相對應的 IDevice
介面,讓應用程式可從這些裝置中進行選擇。
CPU
:單核心或多核心 CPU。
GPU
:可執行 NNAPI 模型,並加速 OpenGL ES 和 Vulkan 等圖形 API 的 GPU。
ACCELERATOR
:專用神經處理單元 (NPU)。
在 IDevice.hal
中實作 getVersionString
,以便取得裝置實作的版本字串。這個方法必須傳回人類可讀的字串。字串的格式因供應商而異。每個新版驅動程式的版本字串都必須不同。
IDevice
介面的名稱必須遵循 {VENDOR}-{DEVICE_NAME}
格式。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# Device discovery and assignment\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\nIn Android 10, the\n[Neural Networks API (NNAPI)](/docs/core/interaction/neural-networks)\nintroduces functions that allow\nmachine learning framework\nlibraries and apps to get information about the devices available and\nspecify which devices to execute a model on. Providing information about the\navailable devices allows apps to get the exact version of the drivers\nfound on the device to avoid known incompatibilities. By giving apps the\nability to specify which devices are to execute different sections of a model,\napps can be optimized for the product on which they are deployed.\n\nSupport for device discovery and assignment is required for NN HAL 1.2\nimplementations.\n\nImplementation\n--------------\n\nTo support the device discovery and assignment features in NNAPI, implement\n[`getType`](https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/android16-release/neuralnetworks/1.2/IDevice.hal#76)\nand\n[`getVersionString`](https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/android16-release/neuralnetworks/1.2/IDevice.hal#56)\nin `IDevice.hal` to allow the framework to get the device type and driver\nversion.\n\nFor each device, specify the type as one of the following categories as\nspecified in\n[`DeviceType`](https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/android16-release/neuralnetworks/1.2/types.hal#4737)\nin\n[`types.hal`](https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/android16-release/neuralnetworks/1.2/types.hal).\n\n- **`OTHER`:** A device that doesn't fall into any of the other categories, including a heterogeneous interface, which is a single `IDevice` interface that manages multiple devices, possibly of different types. A driver with a heterogeneous interface should also expose separate `IDevice` interfaces that correspond to individual devices to allow an application to choose from those devices.\n- **`CPU`:** A single core or multicore CPU.\n- **`GPU`:** A GPU that can run NNAPI models and accelerate graphics APIs such as OpenGL ES and Vulkan.\n- **`ACCELERATOR`:** A dedicated neural processing unit (NPU).\n\nImplement\n[`getVersionString`](https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/android16-release/neuralnetworks/1.2/IDevice.hal#56)\nin `IDevice.hal` for getting the version string of the device implementation.\nThis method must return a string that is human readable. The format of the\nstring is vendor specific. The version string must be different for each new\nversion of a driver.\n\nThe name of the `IDevice` interface must follow the `{VENDOR}-{DEVICE_NAME}`\nformat."]]