Đại biểu lục giác TensorFlow Lite,đại biểu lục giác TensorFlow Lite

Tài liệu này giải thích cách sử dụng Đại biểu lục giác TensorFlow Lite trong ứng dụng của bạn bằng API Java và/hoặc C. Đại biểu tận dụng thư viện Qualcomm Hexagon để thực thi các hạt nhân lượng tử hóa trên DSP. Lưu ý rằng đại biểu nhằm mục đích bổ sung chức năng NNAPI, đặc biệt đối với các thiết bị không có khả năng tăng tốc NNAPI DSP (ví dụ: trên các thiết bị cũ hơn hoặc các thiết bị chưa có trình điều khiển DSP NNAPI).

Thiết bị hỗ trợ:

Hiện tại, kiến ​​trúc Hexagon sau được hỗ trợ, bao gồm nhưng không giới hạn ở:

  • Lục giác 680
    • Ví dụ về SoC: Snapdragon 821, 820, 660
  • Lục giác 682
    • Ví dụ về SoC: Snapdragon 835
  • Lục giác 685
    • Ví dụ về SoC: Snapdragon 845, Snapdragon 710, QCS410, QCS610, QCS605, QCS603
  • Lục giác 690
    • Ví dụ về SoC: Snapdragon 855, RB5

Các mô hình được hỗ trợ:

Đại biểu Hexagon hỗ trợ tất cả các mô hình phù hợp với thông số lượng tử hóa đối xứng 8 bit của chúng tôi, bao gồm cả các mô hình được tạo bằng cách sử dụng lượng tử hóa số nguyên sau đào tạo . Các mô hình UInt8 được đào tạo bằng lộ trình đào tạo nhận biết lượng tử hóa cũ cũng được hỗ trợ, chẳng hạn như các phiên bản lượng tử hóa này trên trang Mô hình được lưu trữ của chúng tôi.

API Java đại biểu lục giác

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

Cách sử dụng ví dụ

Bước 1. Chỉnh sửa app/build.gradle để sử dụng AAR ủy quyền Hexagon hàng đêm

dependencies {
  ...
  implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'
  implementation 'org.tensorflow:tensorflow-lite-hexagon:0.0.0-nightly-SNAPSHOT'
}

Bước 2. Thêm thư viện Hexagon vào ứng dụng Android của bạn

  • Tải xuống và chạy hex_nn_skel.run. Nó sẽ cung cấp 3 thư viện chia sẻ khác nhau “libhexagon_nn_skel.so”, “libhexagon_nn_skel_v65.so”, “libhexagon_nn_skel_v66.so”

Bước 3. Tạo một đại biểu và khởi tạo Trình thông dịch 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 C đại biểu lục giác

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

Cách sử dụng ví dụ

Bước 1. Chỉnh sửa app/build.gradle để sử dụng AAR ủy quyền Hexagon hàng đêm

dependencies {
  ...
  implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'
  implementation 'org.tensorflow:tensorflow-lite-hexagon:0.0.0-nightly-SNAPSHOT'
}

Bước 2. Thêm thư viện Hexagon vào ứng dụng Android của bạn

  • Tải xuống và chạy hex_nn_skel.run. Nó sẽ cung cấp 3 thư viện chia sẻ khác nhau “libhexagon_nn_skel.so”, “libhexagon_nn_skel_v65.so”, “libhexagon_nn_skel_v66.so”

Bước 3. Bao gồm tiêu đề C

  • Có thể tải xuống tệp tiêu đề "hexagon_delegate.h" từ GitHub hoặc trích xuất từ ​​AAR của đại biểu Hexagon.

Bước 4. Tạo một đại biểu và khởi tạo Trình thông dịch TensorFlow Lite

  • Trong mã của bạn, hãy đảm bảo thư viện Hexagon gốc đã được tải. Điều này có thể được thực hiện bằng cách gọi System.loadLibrary("tensorflowlite_hexagon_jni");
    trong điểm vào Hoạt động hoặc Java của bạn.

  • Tạo một đại biểu, ví dụ:

#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(&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.

Thêm thư viện dùng chung vào ứng dụng của bạn

  • Tạo thư mục “app/src/main/jniLibs” và tạo thư mục cho từng kiến ​​trúc đích. Ví dụ,
    • ARM 64-bit: app/src/main/jniLibs/arm64-v8a
    • ARM 32-bit: app/src/main/jniLibs/armeabi-v7a
  • Đặt .so của bạn vào thư mục phù hợp với kiến ​​trúc.

Nhận xét

Đối với các vấn đề, vui lòng tạo một vấn đề GitHub với tất cả các chi tiết repro cần thiết, bao gồm kiểu điện thoại và bo mạch được sử dụng ( adb shell getprop ro.product.deviceadb shell getprop ro.board.platform ).

Câu hỏi thường gặp

  • Những hoạt động nào được đại biểu hỗ trợ?
  • Làm cách nào để biết mô hình đang sử dụng DSP khi tôi bật đại biểu?
    • Hai thông báo tường trình sẽ được in khi bạn kích hoạt đại biểu - một thông báo cho biết liệu đại biểu đã được tạo hay chưa và một thông báo khác cho biết có bao nhiêu nút đang chạy bằng đại biểu.
      Created TensorFlow Lite delegate for Hexagon.
      Hexagon delegate: X nodes delegated out of Y nodes.
  • Tôi có cần hỗ trợ tất cả các Hoạt động trong mô hình để điều hành đại biểu không?
    • Không, Mô hình sẽ được phân chia thành các sơ đồ con dựa trên các hoạt động được hỗ trợ. Mọi hoạt động không được hỗ trợ sẽ chạy trên CPU.
  • Làm cách nào tôi có thể xây dựng AAR đại biểu Hexagon từ nguồn?
    • Sử dụng bazel build -c opt --config=android_arm64 tensorflow/lite/delegates/hexagon/java:tensorflow-lite-hexagon .
  • Tại sao đại biểu Hexagon không khởi tạo được mặc dù thiết bị Android của tôi có SoC được hỗ trợ?
    • Xác minh xem thiết bị của bạn có thực sự có SoC được hỗ trợ hay không. Chạy adb shell cat /proc/cpuinfo | grep Hardware và xem liệu nó có trả về nội dung như "Phần cứng: Qualcomm Technologies, Inc MSMXXXX" hay không.
    • Một số nhà sản xuất điện thoại sử dụng các SoC khác nhau cho cùng một kiểu điện thoại. Do đó, đại biểu Hexagon chỉ có thể hoạt động trên một số chứ không phải tất cả các thiết bị của cùng một kiểu điện thoại.
    • Một số nhà sản xuất điện thoại cố tình hạn chế việc sử dụng Hexagon DSP từ các ứng dụng Android không thuộc hệ thống, khiến đại biểu Hexagon không thể hoạt động.
  • Điện thoại của tôi đã khóa quyền truy cập DSP. Mình đã root máy mà vẫn không chạy được delegate thì phải làm sao?
    • Đảm bảo tắt tính năng thực thi SELinux bằng cách chạy adb shell setenforce 0