ขอขอบคุณที่เข้าร่วม Google I/O ดูเซสชั่นทั้งหมดตามความต้องการ ดูตามความต้องการ

ตัวแทน TensorFlow Lite Hexagon

เอกสารนี้อธิบายวิธีใช้ TensorFlow Lite Hexagon Delegate ในแอปพลิเคชันของคุณโดยใช้ Java และ/หรือ C API ผู้รับมอบสิทธิ์ใช้ประโยชน์จากไลบรารี Qualcomm Hexagon เพื่อรันเคอร์เนลเชิงปริมาณบน DSP โปรดทราบว่าผู้รับมอบสิทธิ์มีจุดประสงค์เพื่อ เสริม การทำงานของ NNAPI โดยเฉพาะสำหรับอุปกรณ์ที่ไม่สามารถเร่งความเร็ว NNAPI DSP ได้ (เช่น ในอุปกรณ์รุ่นเก่า หรืออุปกรณ์ที่ยังไม่มีไดรเวอร์ DSP NNAPI)

อุปกรณ์ที่รองรับ:

ปัจจุบันรองรับสถาปัตยกรรม Hexagon ต่อไปนี้ ซึ่งรวมถึงแต่ไม่จำกัดเพียง:

  • หกเหลี่ยม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 รองรับทุกรุ่นที่สอดคล้องกับข้อกำหนด quantization สมมาตร 8 บิต ของเรา รวมถึงที่สร้างขึ้นโดยใช้ quantization จำนวนเต็มหลังการฝึก โมเดล 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. เพิ่มไลบรารี Hexagon ลงในแอพ Android ของคุณ

  • ดาวน์โหลดและเรียกใช้ hexagon_nn_skel.run ควรมีไลบรารีที่ใช้ร่วมกัน 3 แห่ง "libhexagon_nn_skel.so", "libhexagon_nn_skel_v65.so", "libhexagon_nn_skel_v66.so"

ขั้นตอนที่ 3 สร้างผู้รับมอบสิทธิ์และเริ่มต้น TensorFlow Lite Interpreter

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();
}

ผู้รับมอบสิทธิ์หกเหลี่ยม C 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. เพิ่มไลบรารี Hexagon ลงในแอพ Android ของคุณ

  • ดาวน์โหลดและเรียกใช้ hexagon_nn_skel.run ควรมีไลบรารีที่ใช้ร่วมกัน 3 แห่ง "libhexagon_nn_skel.so", "libhexagon_nn_skel_v65.so", "libhexagon_nn_skel_v66.so"

ขั้นตอนที่ 3 รวมส่วนหัว C

  • ไฟล์ส่วนหัว "hexagon_delegate.h" สามารถดาวน์โหลดได้จาก GitHub หรือแยกจาก AAR ของผู้รับมอบสิทธิ์ Hexagon

ขั้นตอนที่ 4 สร้างผู้รับมอบสิทธิ์และเริ่มต้น TensorFlow Lite Interpreter

  • ในโค้ดของคุณ ตรวจสอบให้แน่ใจว่าได้โหลดไลบรารี 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 use case will need to resize input or anything that can trigger
// re-applying delegates then 'delegate_ptr' need to outlive the interpreter.
auto* delegate_ptr = ::tflite::TfLiteHexagonDelegateCreate(&params);
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
  • ใส่ .so ของคุณในไดเร็กทอรีที่ตรงกับสถาปัตยกรรม

ข้อเสนอแนะ

สำหรับปัญหา โปรดสร้างปัญหา GitHub พร้อมรายละเอียด repro ที่จำเป็นทั้งหมด รวมถึงรุ่นโทรศัพท์และบอร์ดที่ใช้ ( adb shell getprop ro.product.device และ adb shell getprop ro.board.platform )

คำถามที่พบบ่อย

  • ops ใดที่ได้รับการสนับสนุนโดยผู้รับมอบสิทธิ์
  • ฉันจะทราบได้อย่างไรว่าโมเดลกำลังใช้ DSP เมื่อฉันเปิดใช้งานผู้รับมอบสิทธิ์
    • ข้อความบันทึกสองรายการจะถูกพิมพ์เมื่อคุณเปิดใช้งานผู้รับมอบสิทธิ์ ข้อความหนึ่งระบุว่าสร้างผู้รับมอบสิทธิ์หรือไม่ และอีกข้อความหนึ่งระบุจำนวนโหนดที่เรียกใช้โดยใช้ผู้รับมอบสิทธิ์
      Created TensorFlow Lite delegate for Hexagon.
      Hexagon delegate: X nodes delegated out of Y nodes.
  • ฉันต้องการ Ops ทั้งหมดในโมเดลเพื่อรองรับการเรียกใช้ผู้รับมอบสิทธิ์หรือไม่
    • ไม่ โมเดลจะถูกแบ่งส่วนออกเป็นกราฟย่อยตาม ops ที่รองรับ ops ที่ไม่รองรับจะทำงานบน CPU
  • ฉันจะสร้าง AAR ผู้รับมอบสิทธิ์ Hexagon จากแหล่งที่มาได้อย่างไร
    • ใช้ bazel build -c opt --config=android_arm64 tensorflow/lite/delegates/hexagon/java:tensorflow-lite-hexagon
  • เหตุใดผู้รับมอบสิทธิ์ Hexagon จึงไม่สามารถเริ่มต้นแม้ว่าอุปกรณ์ Android ของฉันจะมี SoC ที่รองรับ
    • ตรวจสอบว่าอุปกรณ์ของคุณมี SoC ที่รองรับหรือไม่ เรียกใช้ adb shell cat /proc/cpuinfo | grep Hardware และดูว่ามันส่งคืนบางอย่างเช่น "ฮาร์ดแวร์: Qualcomm Technologies, Inc MSMXXXX"
    • ผู้ผลิตโทรศัพท์บางรายใช้ SoC ต่างกันสำหรับโทรศัพท์รุ่นเดียวกัน ดังนั้น ผู้รับมอบสิทธิ์ Hexagon อาจใช้งานได้กับอุปกรณ์บางรุ่นเท่านั้นแต่ไม่ใช่ทุกเครื่องของโทรศัพท์รุ่นเดียวกัน
    • ผู้ผลิตโทรศัพท์บางรายจงใจจำกัดการใช้ Hexagon DSP จากแอป Android ที่ไม่ใช่ระบบ ทำให้ผู้รับมอบสิทธิ์ Hexagon ไม่สามารถทำงานได้
  • โทรศัพท์ของฉันล็อกการเข้าถึง DSP ฉันรูทโทรศัพท์แล้ว แต่ยังเรียกใช้ผู้รับมอบสิทธิ์ไม่ได้ ต้องทำอย่างไร ?
    • ตรวจสอบให้แน่ใจว่าได้ปิดใช้งานการบังคับใช้ SELinux โดยเรียกใช้ adb shell setenforce 0