ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
ควบคุมโฟลว์
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ตั้งแต่ Android 11 เป็นต้นไป NNAPI จะมีการดำเนินการควบคุมการไหล 2 รายการ ได้แก่ IF
และ WHILE
ซึ่งใช้รูปแบบอื่นๆ เป็นอาร์กิวเมนต์ และดำเนินการแบบมีเงื่อนไข (IF
) หรือซ้ำๆ (WHILE
) ซึ่งช่วยให้สร้างรูปแบบที่ดำเนินการต่างๆ ตามค่าอินพุต หรือดำเนินการหลายครั้งได้โดยไม่ต้องยกเลิกการม้วน ซึ่งสำคัญอย่างยิ่งสำหรับ Use Case เช่น RNN แบบไดนามิกและ seq2seq
ใน NN HAL 1.3 โมเดลจะรวมซับกราฟหลายรายการเข้าด้วยกัน รวมถึงซับกราฟหลักที่ใช้ระบุอินพุตและเอาต์พุตของการดำเนินการ กราฟย่อยจะอ้างอิงกราฟย่อยอื่นๆ ได้โดยใช้โอเปอเรนด์ประเภท SUBGRAPH
เฟรมเวิร์กจะส่งการดำเนินการของโฟลว์การควบคุมไปยัง Accelerator ได้ก็ต่อเมื่อ Accelerator รองรับการดำเนินการทั้งหมดในกราฟย่อยทั้งหมดที่การดำเนินการของโฟลว์การควบคุมนั้นอ้างอิง
อินเทอร์เฟซ 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 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)."]]