เอกสารนี้อธิบายวิธีใช้ TensorFlow Lite Hexagon Delegate ในแอปพลิเคชันของคุณโดยใช้ Java และ/หรือ C API ผู้รับมอบสิทธิ์ใช้ประโยชน์จากไลบรารี Qualcomm Hexagon เพื่อประมวลผลเคอร์เนลเชิงปริมาณบน DSP โปรดทราบว่าผู้รับมอบสิทธิ์มีวัตถุประสงค์เพื่อ เสริม การทำงานของ NNAPI โดยเฉพาะสำหรับอุปกรณ์ที่การเร่งความเร็ว NNAPI DSP ไม่พร้อมใช้งาน (เช่น บนอุปกรณ์รุ่นเก่า หรืออุปกรณ์ที่ยังไม่มีไดรเวอร์ DSP NNAPI)
อุปกรณ์ที่รองรับ:
ปัจจุบันรองรับสถาปัตยกรรมหกเหลี่ยมต่อไปนี้ ซึ่งรวมถึงแต่ไม่จำกัดเฉพาะ:
- หกเหลี่ยม 680
- ตัวอย่าง SoC: Snapdragon 821, 820, 660
- หกเหลี่ยม 682
- ตัวอย่าง SoC: Snapdragon 835
- หกเหลี่ยม 685
- ตัวอย่าง SoC: Snapdragon 845, Snapdragon 710, QCS410, QCS610, QCS605, QCS603
- หกเหลี่ยม 690
- ตัวอย่าง SoC: Snapdragon 855, RB5
รุ่นที่รองรับ:
ผู้ร่วมประชุมรุ่น Hexagon รองรับโมเดลทั้งหมดที่สอดคล้องกับ ข้อกำหนดด้านปริมาณแบบสมมาตร 8 บิต ของเรา รวมถึงรุ่นที่สร้างขึ้นโดยใช้ การวัดปริมาณจำนวนเต็มหลังการฝึกอบรม นอกจากนี้ยังรองรับโมเดล UInt8 ที่ได้รับการฝึกด้วยเส้นทาง การฝึกที่ทราบถึงปริมาณ แบบดั้งเดิม เช่น เวอร์ชันเชิงปริมาณเหล่านี้ บนหน้าโมเดลที่โฮสต์ของเรา
Java API ของผู้รับมอบสิทธิ์หกเหลี่ยม
public class HexagonDelegate implements Delegate, Closeable {
/*
* Creates a new HexagonDelegate object given the current 'context'.
* Throws UnsupportedOperationException if Hexagon DSP delegation is not
* available on this device.
*/
public HexagonDelegate(Context context) throws UnsupportedOperationException
/**
* Frees TFLite resources in C runtime.
*
* User is expected to call this method explicitly.
*/
@Override
public void close();
}
ตัวอย่างการใช้งาน
ขั้นตอนที่ 1 แก้ไข app/build.gradle เพื่อใช้ AAR ของผู้รับมอบสิทธิ์หกเหลี่ยมทุกคืน
dependencies {
...
implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'
implementation 'org.tensorflow:tensorflow-lite-hexagon:0.0.0-nightly-SNAPSHOT'
}
ขั้นตอนที่ 2 เพิ่มไลบรารี่หกเหลี่ยมลงในแอป Android ของคุณ
- ดาวน์โหลดและรัน hexagon_nn_skel.run ควรมีไลบรารีที่ใช้ร่วมกันที่แตกต่างกัน 3 แบบคือ "libhexagon_nn_skel.so", "libhexagon_nn_skel_v65.so", "libhexagon_nn_skel_v66.so"
- รวมทั้ง 3 รายการไว้ในแอปของคุณร่วมกับไลบรารีที่แชร์อื่นๆ ดู วิธีเพิ่มไลบรารีที่ใช้ร่วมกันลงในแอปของคุณ ผู้รับมอบสิทธิ์จะเลือกอันที่มีประสิทธิภาพดีที่สุดโดยอัตโนมัติ ทั้งนี้ขึ้นอยู่กับอุปกรณ์
ขั้นตอนที่ 3 สร้างผู้รับมอบสิทธิ์และเริ่มต้นล่าม TensorFlow Lite
import org.tensorflow.lite.HexagonDelegate;
// Create the Delegate instance.
try {
hexagonDelegate = new HexagonDelegate(activity);
tfliteOptions.addDelegate(hexagonDelegate);
} catch (UnsupportedOperationException e) {
// Hexagon delegate is not supported on this device.
}
tfliteInterpreter = new Interpreter(tfliteModel, tfliteOptions);
// Dispose after finished with inference.
tfliteInterpreter.close();
if (hexagonDelegate != null) {
hexagonDelegate.close();
}
API ของผู้รับมอบสิทธิ์หกเหลี่ยม
struct TfLiteHexagonDelegateOptions {
// This corresponds to the debug level in the Hexagon SDK. 0 (default)
// means no debug.
int debug_level;
// This corresponds to powersave_level in the Hexagon SDK.
// where 0 (default) means high performance which means more power
// consumption.
int powersave_level;
// If set to true, performance information about the graph will be dumped
// to Standard output, this includes cpu cycles.
// WARNING: Experimental and subject to change anytime.
bool print_graph_profile;
// If set to true, graph structure will be dumped to Standard output.
// This is usually beneficial to see what actual nodes executed on
// the DSP. Combining with 'debug_level' more information will be printed.
// WARNING: Experimental and subject to change anytime.
bool print_graph_debug;
};
// Return a delegate that uses Hexagon SDK for ops execution.
// Must outlive the interpreter.
TfLiteDelegate*
TfLiteHexagonDelegateCreate(const TfLiteHexagonDelegateOptions* options);
// Do any needed cleanup and delete 'delegate'.
void TfLiteHexagonDelegateDelete(TfLiteDelegate* delegate);
// Initializes the DSP connection.
// This should be called before doing any usage of the delegate.
// "lib_directory_path": Path to the directory which holds the
// shared libraries for the Hexagon NN libraries on the device.
void TfLiteHexagonInitWithPath(const char* lib_directory_path);
// Same as above method but doesn't accept the path params.
// Assumes the environment setup is already done. Only initialize Hexagon.
Void TfLiteHexagonInit();
// Clean up and switch off the DSP connection.
// This should be called after all processing is done and delegate is deleted.
Void TfLiteHexagonTearDown();
ตัวอย่างการใช้งาน
ขั้นตอนที่ 1 แก้ไข app/build.gradle เพื่อใช้ AAR ของผู้รับมอบสิทธิ์หกเหลี่ยมทุกคืน
dependencies {
...
implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'
implementation 'org.tensorflow:tensorflow-lite-hexagon:0.0.0-nightly-SNAPSHOT'
}
ขั้นตอนที่ 2 เพิ่มไลบรารี่หกเหลี่ยมลงในแอป Android ของคุณ
- ดาวน์โหลดและรัน hexagon_nn_skel.run ควรมีไลบรารีที่ใช้ร่วมกันที่แตกต่างกัน 3 แบบคือ "libhexagon_nn_skel.so", "libhexagon_nn_skel_v65.so", "libhexagon_nn_skel_v66.so"
- รวมทั้ง 3 รายการไว้ในแอปของคุณร่วมกับไลบรารีที่แชร์อื่นๆ ดู วิธีเพิ่มไลบรารีที่ใช้ร่วมกันลงในแอปของคุณ ผู้รับมอบสิทธิ์จะเลือกอันที่มีประสิทธิภาพดีที่สุดโดยอัตโนมัติ ทั้งนี้ขึ้นอยู่กับอุปกรณ์
ขั้นตอนที่ 3 รวมส่วนหัว C
- ไฟล์ส่วนหัว "hexagon_delegate.h" สามารถดาวน์โหลดได้จาก GitHub หรือแยกจาก AAR ของผู้รับมอบสิทธิ์ Hexagon
ขั้นตอนที่ 4 สร้างผู้รับมอบสิทธิ์และเริ่มต้นล่าม TensorFlow Lite
ในโค้ดของคุณ ตรวจสอบให้แน่ใจว่าได้โหลดไลบรารี่Hexagon ดั้งเดิมแล้ว ซึ่งสามารถทำได้โดยการเรียก
System.loadLibrary("tensorflowlite_hexagon_jni");
ในกิจกรรมหรือจุดเริ่มต้น Java ของคุณสร้างผู้รับมอบสิทธิ์ ตัวอย่าง:
#include "tensorflow/lite/delegates/hexagon/hexagon_delegate.h"
// Assuming shared libraries are under "/data/local/tmp/"
// If files are packaged with native lib in android App then it
// will typically be equivalent to the path provided by
// "getContext().getApplicationInfo().nativeLibraryDir"
const char[] library_directory_path = "/data/local/tmp/";
TfLiteHexagonInitWithPath(library_directory_path); // Needed once at startup.
::tflite::TfLiteHexagonDelegateOptions params = {0};
// 'delegate_ptr' Need to outlive the interpreter. For example,
// If your use case requires resizing the input or anything that can trigger
// re-applying delegates then 'delegate_ptr' must outlive the interpreter.
auto* delegate_ptr = ::tflite::TfLiteHexagonDelegateCreate(¶ms);
Interpreter::TfLiteDelegatePtr delegate(delegate_ptr,
[](TfLiteDelegate* delegate) {
::tflite::TfLiteHexagonDelegateDelete(delegate);
});
interpreter->ModifyGraphWithDelegate(delegate.get());
// After usage of delegate.
TfLiteHexagonTearDown(); // Needed once at end of app/DSP usage.
เพิ่มไลบรารีที่ใช้ร่วมกันลงในแอปของคุณ
- สร้างโฟลเดอร์ “app/src/main/jniLibs” และสร้างไดเร็กทอรีสำหรับสถาปัตยกรรมเป้าหมายแต่ละรายการ ตัวอย่างเช่น,
- ARM 64 บิต:
app/src/main/jniLibs/arm64-v8a
- ARM 32 บิต:
app/src/main/jniLibs/armeabi-v7a
- ARM 64 บิต:
- ใส่ .so ของคุณลงในไดเร็กทอรีที่ตรงกับสถาปัตยกรรม
ข้อเสนอแนะ
สำหรับปัญหา โปรดสร้างปัญหา GitHub พร้อมรายละเอียดการทำซ้ำที่จำเป็นทั้งหมด รวมถึงรุ่นโทรศัพท์และบอร์ดที่ใช้ ( adb shell getprop ro.product.device
และ adb shell getprop ro.board.platform
)
คำถามที่พบบ่อย
- การดำเนินการใดบ้างที่ได้รับการสนับสนุนจากผู้รับมอบสิทธิ์
- ดูรายการ การดำเนินการและข้อจำกัดที่รองรับ ในปัจจุบัน
- ฉันจะบอกได้อย่างไรว่าโมเดลกำลังใช้ DSP เมื่อฉันเปิดใช้งานผู้รับมอบสิทธิ์
- ข้อความบันทึกสองข้อความจะถูกพิมพ์เมื่อคุณเปิดใช้งานผู้รับมอบสิทธิ์ - ข้อความหนึ่งระบุว่าผู้รับมอบสิทธิ์ถูกสร้างขึ้นหรือไม่ และอีกข้อความหนึ่งเพื่อระบุจำนวนโหนดที่กำลังทำงานโดยใช้ผู้รับมอบสิทธิ์
Created TensorFlow Lite delegate for Hexagon.
Hexagon delegate: X nodes delegated out of Y nodes.
- ข้อความบันทึกสองข้อความจะถูกพิมพ์เมื่อคุณเปิดใช้งานผู้รับมอบสิทธิ์ - ข้อความหนึ่งระบุว่าผู้รับมอบสิทธิ์ถูกสร้างขึ้นหรือไม่ และอีกข้อความหนึ่งเพื่อระบุจำนวนโหนดที่กำลังทำงานโดยใช้ผู้รับมอบสิทธิ์
- ฉันจำเป็นต้องมี Ops ทั้งหมดในโมเดลเพื่อรองรับการเรียกใช้ผู้รับมอบสิทธิ์หรือไม่
- ไม่ โมเดลจะถูกแบ่งพาร์ติชันเป็นกราฟย่อยตามการดำเนินการที่รองรับ การดำเนินการที่ไม่รองรับจะทำงานบน CPU
- ฉันจะสร้าง AAR ผู้รับมอบสิทธิ์หกเหลี่ยมจากแหล่งที่มาได้อย่างไร
- ใช้
bazel build -c opt --config=android_arm64 tensorflow/lite/delegates/hexagon/java:tensorflow-lite-hexagon
- ใช้
- เหตุใดผู้รับมอบสิทธิ์หกเหลี่ยมจึงล้มเหลวในการเริ่มต้นแม้ว่าอุปกรณ์ Android ของฉันจะมี SoC ที่รองรับ
- ตรวจสอบว่าอุปกรณ์ของคุณมี SoC ที่รองรับจริงหรือไม่ เรียกใช้
adb shell cat /proc/cpuinfo | grep Hardware
และดูว่าส่งคืนบางอย่างเช่น "Hardware : Qualcomm Technologies, Inc MSMXXXX" หรือไม่ - ผู้ผลิตโทรศัพท์บางรายใช้ SoC ที่แตกต่างกันสำหรับโทรศัพท์รุ่นเดียวกัน ดังนั้น ผู้รับมอบสิทธิ์หกเหลี่ยมอาจใช้งานได้กับอุปกรณ์โทรศัพท์รุ่นเดียวกันบางรุ่นเท่านั้น
- ผู้ผลิตโทรศัพท์บางรายจงใจจำกัดการใช้ Hexagon DSP จากแอป Android ที่ไม่ใช่ระบบ ทำให้ผู้รับมอบสิทธิ์ของ Hexagon ไม่สามารถทำงานได้
- ตรวจสอบว่าอุปกรณ์ของคุณมี SoC ที่รองรับจริงหรือไม่ เรียกใช้
- โทรศัพท์ของฉันล็อคการเข้าถึง DSP ฉันรูทโทรศัพท์แล้ว แต่ยังไม่สามารถเรียกใช้ผู้รับมอบสิทธิ์ได้ จะต้องทำอย่างไร?
- ตรวจสอบให้แน่ใจว่าได้ปิดการใช้งานการบังคับใช้ SELinux โดยเรียกใช้
adb shell setenforce 0
- ตรวจสอบให้แน่ใจว่าได้ปิดการใช้งานการบังคับใช้ SELinux โดยเรียกใช้