نماینده شش ضلعی TensorFlow Lite، نماینده شش گوشه TensorFlow Lite

این سند نحوه استفاده از TensorFlow Lite Hexagon Delegate را در برنامه خود با استفاده از Java و/یا C API توضیح می دهد. نماینده از کتابخانه Qualcomm Hexagon برای اجرای هسته های کوانتیزه شده در DSP استفاده می کند. توجه داشته باشید که نماینده برای تکمیل عملکرد NNAPI در نظر گرفته شده است، به‌ویژه برای دستگاه‌هایی که شتاب NNAPI DSP در دسترس نیست (مثلاً در دستگاه‌های قدیمی‌تر یا دستگاه‌هایی که هنوز درایور DSP NNAPI ندارند).

دستگاه های پشتیبانی شده:

در حال حاضر معماری شش ضلعی زیر پشتیبانی می شود، از جمله اما نه محدود به:

  • شش ضلعی 680
    • نمونه های SoC: اسنپدراگون 821، 820، 660
  • شش ضلعی 682
    • نمونه های SoC: اسنپدراگون 835
  • شش ضلعی 685
    • نمونه های SoC: اسنپدراگون 845، اسنپدراگون 710، QCS410، QCS610، QCS605، QCS603
  • شش ضلعی 690
    • نمونه های SoC: اسنپدراگون 855، RB5

مدل های پشتیبانی شده:

نماینده شش ضلعی از تمام مدل هایی که با مشخصات کوانتیزاسیون متقارن 8 بیتی ما مطابقت دارند پشتیبانی می کند، از جمله مدل هایی که با استفاده از کوانتیزاسیون اعداد صحیح پس از آموزش تولید می شوند. مدل‌های UInt8 آموزش‌دیده شده با مسیر آموزشی آگاه از کمیت‌سازی قدیمی نیز پشتیبانی می‌شوند، برای مثال، این نسخه‌های کوانتیزه‌شده در صفحه مدل‌های میزبانی ما.

Hexagon delegate 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 را به برنامه اندروید خود اضافه کنید

مرحله 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();
}

Hexagon delegate 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 را به برنامه اندروید خود اضافه کنید

مرحله 3. هدر C را وارد کنید

  • فایل هدر "hexagon_delegate.h" را می توان از GitHub دانلود کرد یا از AAR نماینده Hexagon استخراج کرد.

مرحله 4. یک نماینده ایجاد کنید و یک مترجم TensorFlow Lite را راه اندازی کنید

  • در کد خود، مطمئن شوید که کتابخانه اصلی Hexagon بارگذاری شده است. این کار را می توان با فراخوانی System.loadLibrary("tensorflowlite_hexagon_jni");
    در Activity یا جاوا نقطه ورود شما.

  • یک نماینده ایجاد کنید، به عنوان مثال:

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

کتابخانه مشترک را به برنامه خود اضافه کنید

  • پوشه "app/src/main/jniLibs" را ایجاد کنید و یک دایرکتوری برای هر معماری هدف ایجاد کنید. مثلا،
    • ARM 64 بیتی: app/src/main/jniLibs/arm64-v8a
    • ARM 32 بیتی: app/src/main/jniLibs/armeabi-v7a
  • .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.
  • آیا برای اجرای delegate نیاز به پشتیبانی از تمام عملیات های مدل دارم؟
    • خیر، مدل بر اساس عملیات پشتیبانی شده به زیرگراف ها تقسیم می شود. هر عملیات پشتیبانی نشده روی CPU اجرا می شود.
  • چگونه می توانم AAR نماینده Hexagon را از منبع بسازم؟
    • bazel build -c opt --config=android_arm64 tensorflow/lite/delegates/hexagon/java:tensorflow-lite-hexagon استفاده کنید.
  • چرا Hexagon delegate در مقداردهی اولیه ناموفق است، اگرچه دستگاه Android من دارای SoC پشتیبانی شده است؟
    • بررسی کنید که آیا دستگاه شما واقعاً دارای SoC پشتیبانی شده است یا خیر. adb shell cat /proc/cpuinfo | grep Hardware اجرا کنید adb shell cat /proc/cpuinfo | grep Hardware و ببینید که آیا چیزی شبیه "Hardware: Qualcomm Technologies, Inc MSMXX" را برمی گرداند یا خیر.
    • برخی از تولیدکنندگان گوشی از SoC های مختلف برای یک مدل گوشی استفاده می کنند. بنابراین، نمایندگی Hexagon ممکن است فقط روی برخی از دستگاه‌های یک مدل تلفن کار کند، اما نه همه آنها.
    • برخی از تولیدکنندگان گوشی عمداً استفاده از Hexagon DSP را از برنامه‌های غیر سیستمی اندروید محدود می‌کنند، که باعث می‌شود نماینده Hexagon قادر به کار نباشد.
  • گوشی من دسترسی DSP را قفل کرده است. من گوشی رو روت کردم و هنوز نمیتونم delegate رو اجرا کنم، چیکار کنم؟
    • با اجرای adb shell setenforce 0 اجرای SELinux را غیرفعال کنید