टेन्सरफ्लो लाइट हेक्सागोन प्रतिनिधि, टेन्सरफ्लो लाइट हेक्सागोन प्रतिनिधि

यह दस्तावेज़ बताता है कि जावा और/या सी एपीआई का उपयोग करके अपने एप्लिकेशन में टेन्सरफ्लो लाइट हेक्सागोन डेलीगेट का उपयोग कैसे करें। प्रतिनिधि डीएसपी पर परिमाणित कर्नेल निष्पादित करने के लिए क्वालकॉम हेक्सागोन लाइब्रेरी का लाभ उठाता है। ध्यान दें कि प्रतिनिधि का उद्देश्य एनएनएपीआई कार्यक्षमता को पूरक करना है, विशेष रूप से उन उपकरणों के लिए जहां एनएनएपीआई डीएसपी त्वरण अनुपलब्ध है (उदाहरण के लिए, पुराने उपकरणों पर, या जिन उपकरणों में अभी तक डीएसपी एनएनएपीआई ड्राइवर नहीं है)।

समर्थित उपकरणों:

वर्तमान में निम्नलिखित हेक्सागोन आर्किटेक्चर समर्थित हैं, जिनमें ये शामिल हैं, लेकिन इन्हीं तक सीमित नहीं हैं:

  • षट्कोण 680
    • SoC उदाहरण: स्नैपड्रैगन 821, 820, 660
  • षट्कोण 682
    • SoC उदाहरण: स्नैपड्रैगन 835
  • षट्कोण 685
    • SoC उदाहरण: स्नैपड्रैगन 845, स्नैपड्रैगन 710, QCS410, QCS610, QCS605, QCS603
  • षट्कोण 690
    • SoC उदाहरण: स्नैपड्रैगन 855, RB5

समर्थित मॉडल:

हेक्सागोन प्रतिनिधि उन सभी मॉडलों का समर्थन करता है जो हमारे 8-बिट सममित परिमाणीकरण विनिर्देश के अनुरूप हैं, जिनमें प्रशिक्षण के बाद पूर्णांक परिमाणीकरण का उपयोग करके उत्पन्न मॉडल भी शामिल हैं। विरासत परिमाणीकरण-जागरूक प्रशिक्षण पथ के साथ प्रशिक्षित UInt8 मॉडल भी समर्थित हैं, उदाहरण के लिए, हमारे होस्ट किए गए मॉडल पृष्ठ पर ये परिमाणित संस्करण

हेक्सागोन प्रतिनिधि जावा एपीआई

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. रात्रिकालीन हेक्सागोन प्रतिनिधि एएआर का उपयोग करने के लिए ऐप/बिल्ड.ग्रेडल संपादित करें

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

चरण 2. अपने एंड्रॉइड ऐप में हेक्सागोन लाइब्रेरी जोड़ें

  • हेक्सागोन_एनएन_स्केल.रन डाउनलोड करें और चलाएं। इसे 3 अलग-अलग साझा लाइब्रेरी प्रदान करनी चाहिए "libexgon_nn_stel.so", "libexgon_nn_sgel_v65.so", "libexgon_nn_sgel_v66.so"

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

हेक्सागोन प्रतिनिधि सी एपीआई

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. रात्रिकालीन हेक्सागोन प्रतिनिधि एएआर का उपयोग करने के लिए ऐप/बिल्ड.ग्रेडल संपादित करें

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

चरण 2. अपने एंड्रॉइड ऐप में हेक्सागोन लाइब्रेरी जोड़ें

  • हेक्सागोन_एनएन_स्केल.रन डाउनलोड करें और चलाएं। इसे 3 अलग-अलग साझा लाइब्रेरी प्रदान करनी चाहिए "libexgon_nn_stel.so", "libexgon_nn_sgel_v65.so", "libexgon_nn_sgel_v66.so"

चरण 3. सी हेडर शामिल करें

  • हेडर फ़ाइल "hexgon_delegate.h" को GitHub से डाउनलोड किया जा सकता है या हेक्सागोन प्रतिनिधि AAR से निकाला जा सकता है।

चरण 4. एक प्रतिनिधि बनाएं और एक TensorFlow Lite इंटरप्रेटर प्रारंभ करें

  • अपने कोड में, सुनिश्चित करें कि मूल हेक्सागोन लाइब्रेरी लोड हो गई है। यह System.loadLibrary("tensorflowlite_hexagon_jni");
    आपकी गतिविधि या जावा प्रवेश-बिंदु में।

  • एक प्रतिनिधि बनाएँ, उदाहरण:

#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" बनाएं, और प्रत्येक लक्ष्य आर्किटेक्चर के लिए एक निर्देशिका बनाएं। उदाहरण के लिए,
    • एआरएम 64-बिट: app/src/main/jniLibs/arm64-v8a
    • एआरएम 32-बिट: app/src/main/jniLibs/armeabi-v7a
  • अपने .so को उस निर्देशिका में रखें जो आर्किटेक्चर से मेल खाती हो।

प्रतिक्रिया

समस्याओं के लिए, कृपया उपयोग किए गए फोन मॉडल और बोर्ड ( adb shell getprop ro.product.device और adb shell getprop ro.board.platform ) सहित सभी आवश्यक रिप्रो विवरणों के साथ एक GitHub मुद्दा बनाएं।

सामान्य प्रश्न

  • कौन से ऑप्स प्रतिनिधि द्वारा समर्थित हैं?
  • जब मैं प्रतिनिधि को सक्षम करता हूं तो मैं कैसे बता सकता हूं कि मॉडल डीएसपी का उपयोग कर रहा है?
    • जब आप प्रतिनिधि को सक्षम करेंगे तो दो लॉग संदेश मुद्रित होंगे - एक यह इंगित करने के लिए कि क्या प्रतिनिधि बनाया गया था और दूसरा यह इंगित करने के लिए कि प्रतिनिधि का उपयोग करके कितने नोड चल रहे हैं।
      Created TensorFlow Lite delegate for Hexagon.
      Hexagon delegate: X nodes delegated out of Y nodes.
  • क्या मुझे प्रतिनिधि को चलाने के लिए मॉडल के सभी ऑप्स के समर्थन की आवश्यकता है?
    • नहीं, मॉडल को समर्थित ऑप्स के आधार पर सबग्राफ में विभाजित किया जाएगा। कोई भी असमर्थित ऑप्स सीपीयू पर चलेगा।
  • मैं स्रोत से हेक्सागोन प्रतिनिधि एएआर कैसे बना सकता हूं?
    • bazel build -c opt --config=android_arm64 tensorflow/lite/delegates/hexagon/java:tensorflow-lite-hexagon उपयोग करें।
  • मेरे एंड्रॉइड डिवाइस में समर्थित SoC होने के बावजूद हेक्सागोन प्रतिनिधि प्रारंभ करने में विफल क्यों होता है?
    • सत्यापित करें कि क्या आपके डिवाइस में वास्तव में समर्थित SoC है। adb shell cat /proc/cpuinfo | grep Hardware चलाएँ adb shell cat /proc/cpuinfo | grep Hardware और देखें कि क्या यह "हार्डवेयर: क्वालकॉम टेक्नोलॉजीज, इंक MSMXXXX" जैसा कुछ लौटाता है।
    • कुछ फ़ोन निर्माता एक ही फ़ोन मॉडल के लिए भिन्न SoCs का उपयोग करते हैं। इसलिए, हेक्सागोन प्रतिनिधि एक ही फोन मॉडल के केवल कुछ उपकरणों पर ही काम कर सकता है, लेकिन सभी उपकरणों पर नहीं।
    • कुछ फ़ोन निर्माता जानबूझकर गैर-सिस्टम एंड्रॉइड ऐप्स से हेक्सागोन डीएसपी के उपयोग को प्रतिबंधित करते हैं, जिससे हेक्सागोन प्रतिनिधि काम करने में असमर्थ हो जाता है।
  • मेरे फ़ोन ने DSP एक्सेस लॉक कर दिया है। मैंने फ़ोन रूट कर दिया है और अभी भी डेलीगेट नहीं चला पा रहा हूँ, क्या करें?
    • adb shell setenforce 0 चलाकर SELinux Enforce को अक्षम करना सुनिश्चित करें