ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
แนวทางปฏิบัติแนะนำในการใช้งานไดรเวอร์ NNAPI
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หน้านี้อธิบายแนวทางปฏิบัติแนะนำในการใช้งานไดรเวอร์ Neural Networks API (NNAPI) เพื่อช่วยให้นักพัฒนาแอปนำ NNAPI ไปใช้ได้แบบกว้าง
ทำให้เวลาเริ่มต้นสั้น
หากไดรเวอร์เปลี่ยนรูปแบบน้ำหนักของโมเดลในการใช้งานครั้งแรก ให้ตรวจสอบว่าไดรเวอร์รองรับการแคชการคอมไพล์ ซึ่งจะช่วยลดเวลาที่ใช้ในการคอมไพล์เมื่อแอปเริ่มต้น ซึ่งสำคัญเนื่องจากแอปอาจหลีกเลี่ยงการใช้การเร่งด้วยฮาร์ดแวร์หากเวลาเริ่มต้นนานเกินไป เช่น แอปบางแอปมีน้ำหนักมากกว่า 100 MB และการเปลี่ยนรูปแบบแอปเหล่านี้ทุกครั้งที่เปิดแอปนั้นสิ้นเปลือง
ลดเวลาในการตอบสนองขั้นต่ำ
คุณควรลดเวลาในการตอบสนองขั้นต่ำในไดรเวอร์เพื่อให้โมเดลใช้การเร่งด้วยฮาร์ดแวร์ แอปจํานวนมากใช้โมเดลขนาดเล็กที่เรียกใช้หลายครั้ง และหากเวลาในการตอบสนองต่ำสุดในการเรียกใช้เวิร์กโหลดสูงเกินไป เช่น 2-3 มิลลิวินาที โมเดลอาจเรียกใช้เวิร์กโหลดใน CPU ซึ่งใช้เวลาเพียง 1-2 มิลลิวินาทีแทนที่จะใช้การเร่งด้วยฮาร์ดแวร์ ระวังการซิงค์เธรดที่สิ้นเปลือง
ใช้กลุ่ม NN HAL SchedTune
ตั้งแต่ Android 11 ขึ้นไป AOSP จะมีกลุ่ม SchedTune สำหรับ NN HAL โดยเฉพาะ ซึ่งช่วยให้กระบวนการ NN HAL ระหว่างกระบวนการต่างๆ ใช้ Big Core ได้ คล้ายกับการใช้งานกระบวนการเดียวกันภายใน top-app
cgroup ที่กําหนดไว้ล่วงหน้า การใช้กลุ่ม SchedTune นี้ช่วยลดค่าใช้จ่ายเพิ่มเติมของไดรเวอร์ โดยเฉพาะสำหรับรุ่นขนาดเล็ก
หากต้องการใช้กลุ่ม SchedTune ให้เพิ่มบรรทัดต่อไปนี้ลงในไฟล์ init.rc
ของกระบวนการ NN HAL
writepid /dev/stune/nnapi-hal/tasks
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา 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,["# NNAPI driver implementation best practices\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\nThis page describes best practices for implementing Neural Networks API (NNAPI)\ndrivers to allow for broad adoption of the NNAPI by app developers.\n\nKeep startup times short\n------------------------\n\nIf your driver transforms the weights of a model on first use, make sure the\ndriver supports compilation caching, which reduces the time used for compilation\nwhen an app starts. This is important as apps might avoid using hardware\nacceleration if start-up times are too long. For example, some apps have\nmore than 100 MB of weights and transforming these each time the app\nlaunches is wasteful.\n\nReduce minimal latency\n----------------------\n\nTo ensure that models use hardware acceleration, it's important to reduce the\nminimal latency in drivers. Many apps use small models that are executed\nmultiple times and if the minimal latency to execute a workload is too high,\nsuch as a few milliseconds, models might run the workload on the CPU, which only\ntakes one or two milliseconds, instead of\nusing hardware accelerations. Be careful of costly thread synchronization.\n\nUse the NN HAL SchedTune group\n------------------------------\n\nFrom Android 11 or higher, AOSP includes a dedicated\nNN HAL\n[SchedTune](https://android.googlesource.com/kernel/msm/+/android-msm-marlin-3.18-nougat-dr1/Documentation/scheduler/sched-tune.txt)\ngroup that allows interprocess NN HAL processes to use big\ncores, similar to same-process implementation within the predefined\n`top-app` [cgroup](/docs/core/perf/cgroups). Using this\nSchedTune group reduces driver overhead, especially for small models.\n\nTo use the SchedTune group, add the following line to the `init.rc` file of\nthe NN HAL process: \n\n writepid /dev/stune/nnapi-hal/tasks"]]