2025 年 3 月 27 日より、AOSP のビルドとコントリビューションには aosp-main
ではなく android-latest-release
を使用することをおすすめします。詳細については、AOSP の変更をご覧ください。
サービス品質
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Android 11 以降の NNAPI では、モデルの相対的な優先度、特定のモデルの準備にかかると想定される最大時間、特定の実行が完了するまでにかかると想定される最大時間をアプリが表示できるようにすることで、サービス品質(QoS)を改善しています。さらに、Android 11 では追加の NNAPI エラー値を導入することにより、サービスが障害発生時の状況をより正確に示し、クライアント アプリがより適切に反応して回復できるようになっています。
優先順位
Android 11 以降では、モデルは NN HAL 1.3 における優先順位に基づいて準備されます。この優先順位は、同じアプリが所有している他の準備済みモデルとの相関関係で設定されます。優先順位の高い実行内容は、優先順位の低い実行内容よりも多くのコンピューティング リソースを使用でき、優先順位の低い実行内容をプリエンプトする、または枯渇させる可能性があります。
明示的な引数として Priority
を含む NN HAL 1.3 呼び出しは IDevice::prepareModel_1_3
です。IDevice::prepareModelFromCache_1_3
のキャッシュ引数には、暗黙的に Priority
が含まれています。
ドライバとアクセラレータの機能に応じて、優先順位をサポートする多くの戦略が存在します。たとえば次のような戦略があります。
Android では、AID(Android UID)を使用することで、さまざまな通話アプリ間でサービスを区別できます。HIDL には、::android::hardware::IPCThreadState::getCallingUid
メソッドを使用して呼び出し側アプリの UID を取得するための組み込みメカニズムが用意されています。AID のリストは、libcutils/include/cutils/android_filesystem_config.h
でご確認いただけます。
期限
Android 11 以降では、OptionalTimePoint
deadline 引数を指定して、モデルの準備と実行を開始できます。この期限を設定することによって、タスクに要する時間を推測できるドライバでは、期限前にタスクを完了できないと推定した場合に、ドライバは開始する前にタスクを中止できます。同様に、期限を設定することによって、期限内に完了できないと推測した場合にドライバは進行中のタスクを中止できます。タスクが期限までに完了しなかった場合、または期限を経過した場合に、deadline 引数がタスクを中止するようドライバに強制することはありません。deadline 引数を使用すると、ドライバ内のコンピューティング リソースを解放し、期限が設定されていない場合よりも迅速にアプリに制御を戻すことができます。
OptionalTimePoint
の期限を引数として含む NN HAL 1.3 呼び出しを以下に掲載します。
IDevice::prepareModel_1_3
IDevice::prepareModelFromCache_1_3
IPreparedModel::execute_1_3
IPreparedModel::executeSynchronously_1_3
IPreparedModel::executeFenced
上述の各メソッドの期限に関する機能のリファレンス実装を確認するには、frameworks/ml/nn/driver/sample/SampleDriver.cpp
の NNAPI サンプル ドライバをご確認ください。
エラーコード
Android 11 では NN HAL 1.3 に 4 つのエラーコード値を導入しており、エラーレポートの機能が改善され、ドライバがより詳細に状態を伝達することにより、アプリが復旧処理をより適切に実施できるようになりました。以下に掲載しているのは、ErrorStatus
のエラーコード値です。
MISSED_DEADLINE_TRANSIENT
MISSED_DEADLINE_PERSISTENT
RESOURCE_EXHAUSTED_TRANSIENT
RESOURCE_EXHAUSTED_PERSISTENT
Android 10 以前の場合、ドライバでは GENERAL_FAILURE
エラーコード以外に失敗を示す方法がありませんでした。Android 11 以降では、2 つの MISSED_DEADLINE
エラーコードを使用して、期限に達したか、ドライバが期限までにワークロードを完了できないと予測したことが理由で、ワークロードが中止されたことを示すことができます。2 つの RESOURCE_EXHAUSTED
エラーコードを使用して、ドライバ内のリソース制限(呼び出しに必要なメモリがドライバに不足しているなど)によってタスクが失敗したことを示すことができます。
両方のエラーの TRANSIENT
バージョンは、問題が一時的なものであり、しばらくすると同じタスクに対する後続の呼び出しが成功する可能性があることを示しています。たとえば、先行する作業が長時間実行されている、またはリソースを大量に消費しているためにドライバがビジー状態にある場合でも、先行作業によってドライバがビジー状態でなければ新規のタスクが正常に完了する場合に、このエラーコードが返されます。どちらのエラーの PERSISTENT
バージョンも、同じタスクに対する今後の呼び出しが常に失敗すると予測されることを示しています。たとえば、ドライバが完全な条件下であってもタスクが期限までに完了しないと予測した場合、または性質上モデルが大きすぎてドライバのリソースを超過する場合に、このエラーコードが返されます。
検証
サービスの品質に関する機能は、NNAPI VTS テスト(VtsHalNeuralnetworksV1_3Target
)でテストされます。これには、ドライバが無効な優先順位を拒否することを確認するための一連のテスト(TestGenerated/ValidationTest#Test/
)と、ドライバが期限を適切に処理することを確認するための DeadlineTest
(TestGenerated/DeadlineTest#Test/
)と呼ばれる一連のテストが含まれます。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-03-26 UTC。
[[["わかりやすい","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-03-26 UTC。"],[],[],null,["# Quality of service\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\nStarting from Android 11, the NNAPI offers better\nquality of service (QoS) by allowing an app to indicate the relative priorities\nof its models, the maximum amount of time expected for a given model to be\nprepared, and the maximum amount of time expected for a given execution to be\ncompleted. Further, Android 11 introduces\nadditional NNAPI error values enabling a service to more accurately indicate\nwhat went wrong when a failure occurs so that the client app can better react\nand recover.\n\nPriority\n--------\n\nFor Android 11 or higher, models are prepared with a\npriority in the NN HAL 1.3. This priority is relative to other prepared models\nowned by the same app. Higher-priority executions can use more compute resources\nthan lower-priority executions, and can preempt or starve lower-priority\nexecutions.\n\nThe NN HAL 1.3 call that includes `Priority` as an explicit argument is\n[`IDevice::prepareModel_1_3`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/IDevice.hal;l=199).\nNote that\n[`IDevice::prepareModelFromCache_1_3`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/IDevice.hal;l=294)\nimplicitly includes `Priority` in the cache arguments.\n\nThere are many possible strategies for supporting priorities depending on the\ncapabilities of the driver and accelerator. Here are several strategies:\n\n- For drivers that have built-in priority support, directly propagate the `Priority` field to the accelerator.\n- Use a per-app priority queue to support different priorities even before an execution reaches the accelerator.\n- Pause or cancel low-priority models that are being executed to\n free the accelerator to execute high-priority models. Do this by either\n inserting *checkpoints* in low-priority models that, when reached, query\n a flag to determine whether the current execution should be halted\n prematurely or by partitioning the model into *submodels* and querying the\n flag between submodel executions. Note that the use of checkpoints or\n submodels in models prepared with a priority can introduce additional\n overhead that isn't present for models without a priority in versions\n lower than NN HAL 1.3.\n\n - To support preemption, preserve the execution context including the next operation or sub-model to be executed and any relevant intermediate operand data. Use this execution context to resume the execution at a later time.\n - Full preemption support isn't necessary, so the execution context doesn't need to be preserved. Because NNAPI model executions are deterministic, executions can be restarted from scratch at a later time.\n\nAndroid enables services to differentiate between different calling apps through\nthe use of an AID (Android UID). HIDL has built-in mechanisms to retrieve the\ncalling app's UID through the method\n`::android::hardware::IPCThreadState::getCallingUid`. A list of AIDs can be\nfound in\n[`libcutils/include/cutils/android_filesystem_config.h`](https://android.googlesource.com/platform/system/core/+/android16-release/libcutils/include/cutils/android_filesystem_config.h).\n\nDeadlines\n---------\n\nStarting from Android 11, model preparation and\nexecutions can be launched with an `OptionalTimePoint` deadline argument. For\ndrivers that can estimate how long a task takes, this deadline allows the driver\nto abort the task before it starts if the driver estimates that the task can't\nbe completed before the deadline. Similarly, the deadline allows the driver to\nabort an ongoing task that it estimates won't be completed before the deadline.\nThe deadline argument doesn't force a driver to abort a task if the task isn't\ncomplete by the deadline or if the deadline has passed. The deadline argument\ncan be used to free up compute resources within the driver and return control\nto the app faster than is possible without the deadline.\n\nThe NN HAL 1.3 calls that include `OptionalTimePoint` deadlines as an argument\nare:\n\n- `IDevice::prepareModel_1_3`\n- `IDevice::prepareModelFromCache_1_3`\n- `IPreparedModel::execute_1_3`\n- `IPreparedModel::executeSynchronously_1_3`\n- `IPreparedModel::executeFenced`\n\nTo see a reference implementation of the deadline feature for each of the above\nmethods, see the NNAPI sample driver at\n[`frameworks/ml/nn/driver/sample/SampleDriver.cpp`](https://cs.android.com/android/platform/superproject/+/android-latest-release:packages/modules/NeuralNetworks/driver/sample_hidl/SampleDriver.cpp?q=SampleDriver.cpp).\n\nError codes\n-----------\n\nAndroid 11 includes four error code values in\nNN HAL 1.3 to improve error reporting, allowing drivers to better communicate\ntheir state and apps to recover more gracefully. These are the error code\nvalues in `ErrorStatus`.\n\n- `MISSED_DEADLINE_TRANSIENT`\n- `MISSED_DEADLINE_PERSISTENT`\n- `RESOURCE_EXHAUSTED_TRANSIENT`\n- `RESOURCE_EXHAUSTED_PERSISTENT`\n\nIn Android 10 or lower, a driver could only indicate a failure through the\n`GENERAL_FAILURE` error code. From Android 11, the\ntwo `MISSED_DEADLINE` error codes can be used to indicate that the workload was\naborted because the deadline was reached or because the driver predicted the\nworkload wouldn't complete by the deadline. The two `RESOURCE_EXHAUSTED` error\ncodes can be used to indicate that the task failed because of a resource\nlimitation within the driver, such as the driver not having enough memory for\nthe call.\n\nThe `TRANSIENT` version of both errors indicates that the problem is temporary,\nand that future calls to the same task might succeed after a short delay. For\nexample, this error code should be returned when the driver is busy with prior\nlong-running or resource-intensive work, but that the new task would complete\nsuccessfully if the driver wasn't busy with the prior work. The `PERSISTENT`\nversion of both errors indicates that future calls to the same task are always\nexpected to fail. For example, this error code should be returned when the\ndriver estimates the task wouldn't complete by the deadline even under perfect\nconditions, or that the model is inherently too large and exceeds the driver's\nresources.\n\nValidation\n----------\n\nThe quality of service functionality is tested in the NNAPI VTS tests\n(`VtsHalNeuralnetworksV1_3Target`). This includes a set of tests for validation\n(`TestGenerated/ValidationTest#Test/`) to ensure that the driver rejects invalid\npriorities and a set of tests called `DeadlineTest`\n(`TestGenerated/DeadlineTest#Test/`) to ensure that the driver handles deadlines\ncorrectly."]]