اعتبارًا من 27 آذار (مارس) 2025، ننصحك باستخدام android-latest-release
بدلاً من aosp-main
لإنشاء AOSP والمساهمة فيه. لمزيد من المعلومات، يُرجى الاطّلاع على التغييرات في AOSP.
التحكّم في التدفق
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
بدءًا من الإصدار Android 11، يتضمّن NNAPI عمليتَي تدفق تحكّم، وهما IF
وWHILE
، اللتان تأخذان نماذج أخرى كوسيطات، ثم تنفِّذهما بشكل مشروط (IF
) أو بشكل متكرّر (WHILE
). ويتيح ذلك IF
إنشاء نماذج تنفِّذ عمليات مختلفة استنادًا إلى قيم الإدخالWHILE
أو تنفِّذ العمليات عدة مرات بدون إلغاء التفاف. وهذا مهم لحالات الاستخدام
مثل الشبكات العصبية التسلسلية الديناميكية و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()
.
يُرجى العلم أنّه من المهمّ أن تتيح العمليات الحسابية وعمليات المقارنة على
TENSOR_INT32
المُشغِّلات ذات الشكل 1
، لأنّه يمكن استخدامها كمعدّلات للحلقات.
وبالمثل، يجب استخدام العمليات التي تُنشئ عوامل تشغيل TENSOR_BOOL8
ذات شكل 1
مع شروط IF
وWHILE
.
مهلة تنفيذ حلقة التكرار WHILE
لمنع تكرار الحلقات إلى ما لا نهاية، يجب إيقاف التنفيذ إذا استغرقت حلقة WHILE
وقتًا أطول من قيمة loopTimeoutDuration
التي تم تمريرها إلى طلب
IPreparedModel::execute_1_3()
أو IPreparedModel::executeSynchronously_1_3()
أو
IPreparedModel::executeFenced()
(أو القيمة التلقائية في حال حذفها).
التحقُّق
تشكّل اختبارات تدفّق التحكّم جزءًا من مجموعتَي اختبار CTS وVTS. لمزيد من
المعلومات، يُرجى الاطّلاع على التحقّق.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 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)."]]