自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
设备发现和分配
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在 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 模型以及加快图形 API(如 OpenGL ES 和 Vulkan)的速度的 GPU。
ACCELERATOR
:专用的神经处理单元 (NPU)。
在 IDevice.hal
中实现 getVersionString
以获取设备实现的版本字符串。
此方法必须返回一个可读性良好的字符串。字符串的格式取决于供应商。驱动程序的每个新版本的版本字符串必须不同。
IDevice
接口的名称必须采用 {VENDOR}-{DEVICE_NAME}
格式。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[[["易于理解","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"]],["最后更新时间 (UTC):2025-03-26。"],[],[],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."]]