החל מ-27 במרץ 2025, מומלץ להשתמש ב-android-latest-release
במקום ב-aosp-main
כדי ליצור תרומות ל-AOSP. מידע נוסף זמין במאמר שינויים ב-AOSP.
בקרת זרימה
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
החל מ-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
, שה-methods שלו, execute_1_3()
, executeSynchronously_1_3()
ו-executeFenced()
, מקבלים ארגומנט loopTimeoutDuration
אופציונלי.
הטמעת מנהל
דוגמה להטמעת פעולה מפורטת במאמרים CpuExecutor::executeIfOperation
ו-CpuExecutor::executeWhileOperation
.
לקבלת דוגמאות ללוגיקה של אימות פעולות, ראו validateIfOperation()
ו-validateWhileOperation()
.
חשוב לתמוך בפעולות אריתמטיות ובפעולות השוואה על אופרטורים של TENSOR_INT32
בפורמט 1
, כי אפשר להשתמש בהם כמספרים של לולאות.
באופן דומה, צריך להשתמש בתנאי IF
ו-WHILE
בפעולות שמניבות אופרטורים מסוג TENSOR_BOOL8
בפורמט 1
.
זמן קצוב לתפוגה של ביצוע לולאת WHILE
כדי למנוע לולאות אינסופיות, צריך לבטל את הביצוע אם לולאה של WHILE
נמשכת יותר מהערך של loopTimeoutDuration
שמוענק לקריאה של IPreparedModel::execute_1_3()
, IPreparedModel::executeSynchronously_1_3()
או IPreparedModel::executeFenced()
(או ערך ברירת המחדל אם הוא לא צוין).
אימות
בדיקות של תהליך הבקרה הן חלק מחבילות הבדיקות של CTS ו-VTS. למידע נוסף, ראו אימות.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-27 (שעון 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-07-27 (שעון 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)."]]