2025 年 3 月 27 日より、AOSP のビルドとコントリビューションには aosp-main
ではなく android-latest-release
を使用することをおすすめします。詳細については、AOSP の変更をご覧ください。
制御フロー
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Android 11 以降、NNAPI には 2 つの制御フロー オペレーション(IF
、WHILE
)が追加されました。これは他のモデルを引数に取り、それらのモデルを条件付きで実行(IF
)、または反復実行(WHILE
)します。これにより、入力値に基づいて異なるオペレーションを実行するモデルや、展開することなくオペレーションを複数回実行するモデルを構築できます。これは、動的 RNN や seq2seq などのユースケースに重要です。
NN HAL 1.3 では、実行の入力と出力の指定に使用するメイン サブグラフなど、複数のサブグラフがモデルに組み込まれています。サブグラフは、SUBGRAPH
型のオペランドを使用して、他のサブグラフを参照できます。フレームワークは、その制御フロー オペレーションで参照されるすべてのサブグラフ内のすべてのオペレーションをアクセラレータがサポートしている場合にのみ、そのアクセラレータに制御フロー オペレーションを送信できます。
HAL インターフェース
NN HAL 1.3 では、制御フローに関連する定義は types.hal
にあります。
IDevice.hal
には IDevice
が含まれ、そのメソッド getSupportedOperations_1_3()
は IF
と WHILE
を他のオペレーションとは異なる方法で処理する必要があります。
IPreparedModel.hal
には IPreparedModel
が含まれ、そのメソッド execute_1_3()
、executeSynchronously_1_3()
、executeFenced()
はオプションの loopTimeoutDuration
引数を取ります。
ドライバの実装
サンプル オペレーションの実装については、CpuExecutor::executeIfOperation
と CpuExecutor::executeWhileOperation
をご覧ください。サンプル オペレーションの検証ロジックについては、validateIfOperation()
と validateWhileOperation()
をご覧ください。
形状 1
の TENSOR_INT32
オペランドについては、ループカウンタとして使用できることから、算術演算と比較演算をサポートすることが重要です。同様に、形状 1
の TENSOR_BOOL8
オペランドを生成する演算では、IF
条件および WHILE
条件を併用する必要があります。
WHILE ループ実行のタイムアウト
無限ループを回避するため、IPreparedModel::execute_1_3()
、IPreparedModel::executeSynchronously_1_3()
、または IPreparedModel::executeFenced()
の呼び出しに渡された loopTimeoutDuration
値(省略された場合はデフォルト値)よりも WHILE
ループに長い時間を要する場合は、実行を中止する必要があります。
検証
制御フローテストは、CTS と VTS のテストスイートの一部です。詳細は、検証でご確認いただけます。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。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,["# Control flow\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 includes two\ncontrol flow operations, `IF` and `WHILE`, that take other models as arguments\nand execute them conditionally (`IF`) or repeatedly (`WHILE`). This allows for\nconstructing models that execute different operations based on the input values\nor execute operations multiple times without unrolling. This is important for\nuse cases such as dynamic RNN and seq2seq.\n\nIn NN HAL 1.3, the model incorporates multiple subgraphs, including the main\nsubgraph that's used for specifying inputs and outputs of an execution. A\nsubgraph can reference other subgraphs using operands of type `SUBGRAPH`. The\nframework can send a control flow operation to an accelerator only if the\naccelerator supports all of the operations in all subgraphs referenced by that\ncontrol flow operation.\n\nHAL interfaces\n--------------\n\nIn NN HAL 1.3, the definitions related to control flow are in\n[`types.hal`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/types.hal).\n\n- [`IF`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/types.hal?q=%22IF%20%3D%2096%22) and [`WHILE`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/types.hal?q=%22WHILE%20%3D%22) operation types\n- [`SUBGRAPH`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/types.hal?q=%22SUBGRAPH%20%3D%22) operand type and the corresponding [`SUBGRAPH`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/types.hal?q=case:yes%20%22SUBGRAPH,%22) operand lifetime\n- [`Model`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/types.hal?q=%22struct%20Model%22) structure that contains the main [subgraph](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/types.hal?q=%22struct%20Subgraph%22) and a list of referenced subgraphs\n- [`Capabilities`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/types.hal?q=%22struct%20Capabilities%22) structure that contains [`ifPerformance`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/types.hal?q=%22PerformanceInfo%20ifPerformance%22) and [`whilePerformance`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/types.hal?q=%22PerformanceInfo%20whilePerformance%22)\n\n[`IDevice.hal`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/IDevice.hal)\ncontains `IDevice`, whose method `getSupportedOperations_1_3()` must treat `IF`\nand `WHILE` differently from other operations.\n\n[`IPreparedModel.hal`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/neuralnetworks/1.3/IPreparedModel.hal)\ncontains `IPreparedModel`, whose methods `execute_1_3()`,\n`executeSynchronously_1_3()`, and `executeFenced()` take an optional\n`loopTimeoutDuration` argument.\n\nDriver implementation\n---------------------\n\nFor a sample operation implementation, see\n[`CpuExecutor::executeIfOperation`](https://cs.android.com/android/platform/superproject/+/android-latest-release:packages/modules/NeuralNetworks/common/CpuExecutor.cpp?q=func:CpuExecutor::executeIfOperation)\nand\n[`CpuExecutor::executeWhileOperation`](https://cs.android.com/android/platform/superproject/+/android-latest-release:packages/modules/NeuralNetworks/common/CpuExecutor.cpp?q=func:CpuExecutor::executeWhileOperation).\nFor sample operation validation logic, see\n[`validateIfOperation()`](https://cs.android.com/android/platform/superproject/+/android-latest-release:packages/modules/NeuralNetworks/common/LegacyUtils.cpp?q=func:validateIfOperation)\nand\n[`validateWhileOperation()`](https://cs.android.com/android/platform/superproject/+/android-latest-release:packages/modules/NeuralNetworks/common/LegacyUtils.cpp?q=func:validateWhileOperation).\n\nNote that it's important to support arithmetic and comparison operations on\n`TENSOR_INT32` operands of shape `1`, as these can be used as loop counters.\nSimilarly, operations producing `TENSOR_BOOL8` operands of shape `1` should be\nused with `IF` and `WHILE` conditions.\n\nWHILE loop execution timeout\n----------------------------\n\nTo prevent infinite loops, execution must be aborted if a `WHILE` loop takes\nlonger than the `loopTimeoutDuration` value passed to a call of\n`IPreparedModel::execute_1_3()`, `IPreparedModel::executeSynchronously_1_3()`,\nor `IPreparedModel::executeFenced()` (or the default value if omitted).\n\nValidation\n----------\n\nControl flow tests are part of the CTS and VTS test suites. For more\ninformation, see [Validation](/docs/core/interaction/neural-networks#validation)."]]