自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
控制流程
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
從 Android 11 開始,NNAPI 包含兩個控制流程作業 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 迴圈執行逾時
為避免無限迴圈,如果 WHILE
迴圈的執行時間超過傳遞至 IPreparedModel::execute_1_3()
、IPreparedModel::executeSynchronously_1_3()
或 IPreparedModel::executeFenced()
呼叫的 loopTimeoutDuration
值 (或省略時的預設值),則必須中止執行。
驗證
控制流程測試是 CTS 和 VTS 測試套件的一部分。詳情請參閱「驗證」。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# 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)."]]