このドキュメントでは、Java および/または C API を使用してアプリケーションで TensorFlow Lite Hexagon Delegate を使用する方法を説明します。デリゲートは Qualcomm Hexagon ライブラリを利用して、DSP 上で量子化カーネルを実行します。デリゲートは、特に NNAPI DSP アクセラレーションが利用できないデバイス (古いデバイス、または DSP NNAPI ドライバーがまだないデバイスなど) の 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 デリゲートは、トレーニング後の整数量子化を使用して生成されたモデルを含む、 8 ビット対称量子化仕様に準拠するすべてのモデルをサポートします。従来の量子化対応トレーニングパスでトレーニングされた UInt8 モデルもサポートされています。たとえば、ホスト モデル ページにあるこれらの量子化バージョンなどです。
Hexagon デリゲート 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 を編集して夜間の Hexagon デリゲート 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 つすべてを含めます。 「アプリに共有ライブラリを追加する方法」を参照してください。デリゲートは、デバイスに応じて最高のパフォーマンスを持つものを自動的に選択します。
ステップ 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 デリゲート 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 を編集して夜間の Hexagon デリゲート 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 つすべてを含めます。 「アプリに共有ライブラリを追加する方法」を参照してください。デリゲートは、デバイスに応じて最高のパフォーマンスを持つものを自動的に選択します。
ステップ 3. C ヘッダーを含める
- ヘッダー ファイル「hexagon_delegate.h」は、 GitHubからダウンロードするか、Hexagon デリゲート AAR から抽出できます。
ステップ 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 をアーキテクチャに一致するディレクトリに置きます。
フィードバック
問題については、使用する電話モデルやボード ( adb shell getprop ro.product.device
およびadb shell getprop ro.board.platform
) など、必要な再現の詳細をすべて記載したGitHub の問題を作成してください。
よくある質問
- どの操作がデリゲートによってサポートされていますか?
- サポートされている演算と制約の現在のリストを参照してください。
- デリゲートを有効にしたときにモデルが DSP を使用していることをどうやって確認できますか?
- デリゲートを有効にすると 2 つのログ メッセージが出力されます。1 つはデリゲートが作成されたかどうかを示し、もう 1 つはデリゲートを使用して実行されているノードの数を示します。
Created TensorFlow Lite delegate for Hexagon.
Hexagon delegate: X nodes delegated out of Y nodes.
- デリゲートを有効にすると 2 つのログ メッセージが出力されます。1 つはデリゲートが作成されたかどうかを示し、もう 1 つはデリゲートを使用して実行されているノードの数を示します。
- デリゲートを実行するには、モデル内のすべての Ops がサポートされる必要がありますか?
- いいえ、モデルはサポートされている操作に基づいてサブグラフに分割されます。サポートされていない操作はすべて CPU 上で実行されます。
- Hexagon デリゲート AAR をソースから構築するにはどうすればよいですか?
-
bazel build -c opt --config=android_arm64 tensorflow/lite/delegates/hexagon/java:tensorflow-lite-hexagon
使用します。
-
- Android デバイスにサポートされている SoC があるにもかかわらず、Hexagon デリゲートの初期化に失敗するのはなぜですか?
- デバイスにサポートされている SoC が実際に搭載されているかどうかを確認してください。
adb shell cat /proc/cpuinfo | grep Hardware
して、「Hardware : Qualcomm Technologies, Inc MSMXXXX」のようなものが返されるかどうかを確認します。 - 一部の携帯電話メーカーは、同じ電話モデルに異なる SoC を使用しています。したがって、Hexagon デリゲートは、同じ電話モデルのすべてのデバイスではなく、一部のデバイスでのみ動作する可能性があります。
- 一部の携帯電話メーカーは、システム以外の Android アプリからの Hexagon DSP の使用を意図的に制限し、Hexagon デリゲートが動作できないようにしています。
- デバイスにサポートされている SoC が実際に搭載されているかどうかを確認してください。
- 私の携帯電話は DSP アクセスをロックしました。電話機をルート化しましたが、まだデリゲートを実行できません。どうすればよいですか?
-
adb shell setenforce 0
を実行して、SELinux enforce を必ず無効にしてください。
-