New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[tflite] enable using the new NNAPI delegate in Java #28011
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
licenses(["notice"]) # Apache 2.0 | ||
|
||
filegroup( | ||
name = "nnapi_delegate_src", | ||
srcs = ["NnApiDelegate.java"], | ||
visibility = ["//visibility:public"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
package org.tensorflow.lite.nnapi; | ||
|
||
import org.tensorflow.lite.Delegate; | ||
import org.tensorflow.lite.Tensor; | ||
|
||
/** {@link Delegate} for NNAPI inference. */ | ||
public class NnApiDelegate implements Delegate, AutoCloseable { | ||
|
||
private static final long INVALID_DELEGATE_HANDLE = 0; | ||
|
||
private long delegateHandle; | ||
|
||
public NnApiDelegate() { | ||
delegateHandle = createDelegate(); | ||
} | ||
|
||
@Override | ||
public long getNativeHandle() { | ||
return delegateHandle; | ||
} | ||
|
||
/** | ||
* The NNAPI delegate is singleton. Nothing to delete | ||
* for now, so mark the handle invalid only. | ||
*/ | ||
@Override | ||
public void close() { | ||
if (delegateHandle != INVALID_DELEGATE_HANDLE) { | ||
delegateHandle = INVALID_DELEGATE_HANDLE; | ||
} | ||
} | ||
|
||
private static native long createDelegate(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Description: | ||
# Java Native Interface (JNI) library intended for implementing the | ||
# TensorFlow Lite GPU delegate Java API using the TensorFlow Lite CC library. | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load("//tensorflow/lite:build_def.bzl", "tflite_copts") | ||
|
||
licenses(["notice"]) # Apache 2.0 | ||
|
||
cc_library( | ||
name = "native", | ||
srcs = ["nnapi_delegate_jni.cc"], | ||
hdrs = ["nnapi_delegate_jni.h"], | ||
copts = tflite_copts(), | ||
tags = [ | ||
"manual", | ||
"notap", | ||
], | ||
deps = [ | ||
"//tensorflow/lite/delegates/nnapi:nnapi_delegate", | ||
"//tensorflow/lite/java/jni", | ||
], | ||
alwayslink = 1, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "tensorflow/lite/delegates/nnapi/nnapi_delegate.h" | ||
#include "tensorflow/lite/delegates/nnapi/java/src/main/native/nnapi_delegate_jni.h" | ||
|
||
JNIEXPORT jlong JNICALL Java_org_tensorflow_lite_nnapi_NnApiDelegate_createDelegate( | ||
JNIEnv* env, jclass clazz) { | ||
return reinterpret_cast<jlong>(tflite::NnApiDelegate()); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#ifndef TENSORFLOW_LITE_DELEGATES_NNAPI_JAVA_SRC_MAIN_NATIVE_NNAPI_DELEGATE_JNI_H_ | ||
#define TENSORFLOW_LITE_DELEGATES_NNAPI_JAVA_SRC_MAIN_NATIVE_NNAPI_DELEGATE_JNI_H_ | ||
|
||
#include <jni.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif // __cplusplus | ||
|
||
/* | ||
* Class: org_tensorflow_lite_nnapi_NnApiDelegate | ||
* Method: createDelegate | ||
* Signature: ()J | ||
*/ | ||
JNIEXPORT jlong JNICALL Java_org_tensorflow_lite_nnapi_NnApiDelegate_createDelegate( | ||
JNIEnv* env, jclass clazz); | ||
|
||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif // __cplusplus | ||
|
||
#endif // TENSORFLOW_LITE_DELEGATES_NNAPI_JAVA_SRC_MAIN_NATIVE_NNAPI_DELEGATE_JNI_H_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
import java.util.PriorityQueue; | ||
import org.tensorflow.lite.Interpreter; | ||
import org.tensorflow.lite.gpu.GpuDelegate; | ||
import org.tensorflow.lite.nnapi.NnApiDelegate; | ||
|
||
/** | ||
* Classifies images with Tensorflow Lite. | ||
|
@@ -96,6 +97,8 @@ public int compare(Map.Entry<String, Float> o1, Map.Entry<String, Float> o2) { | |
|
||
/** holds a gpu delegate */ | ||
GpuDelegate gpuDelegate = null; | ||
/** holds an nnapi delegate */ | ||
NnApiDelegate nnapiDelegate = null; | ||
|
||
/** Initializes an {@code ImageClassifier}. */ | ||
ImageClassifier(Activity activity) throws IOException { | ||
|
@@ -176,12 +179,12 @@ public void useGpu() { | |
} | ||
|
||
public void useCPU() { | ||
tfliteOptions.setUseNNAPI(false); | ||
recreateInterpreter(); | ||
} | ||
|
||
public void useNNAPI() { | ||
tfliteOptions.setUseNNAPI(true); | ||
nnapiDelegate = new NnApiDelegate(); | ||
tfliteOptions.addDelegate(nnapiDelegate); | ||
jdduke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
recreateInterpreter(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just confirming that you've tested this path on Android and it works? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it works on various devices I tested. Some of them need extra setAllowFp16PrecisionForFp32() though. |
||
} | ||
|
||
|
@@ -198,6 +201,10 @@ public void close() { | |
gpuDelegate.close(); | ||
gpuDelegate = null; | ||
} | ||
if (nnapiDelegate != null) { | ||
nnapiDelegate.close(); | ||
nnapiDelegate = null; | ||
} | ||
tfliteModel = null; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably add a close() method. In the future, we may make the delegate stateful, so it'd be good to get into that practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a quite simple close() for now