Skip to content
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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,7 @@
licenses(["notice"]) # Apache 2.0

filegroup(
name = "nnapi_delegate_src",
srcs = ["NnApiDelegate.java"],
visibility = ["//visibility:public"],
)
@@ -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();
Copy link
Member

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.

Copy link
Contributor Author

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

}
25 changes: 25 additions & 0 deletions tensorflow/lite/delegates/nnapi/java/src/main/native/BUILD
@@ -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,
)
@@ -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());
}
@@ -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_
9 changes: 8 additions & 1 deletion tensorflow/lite/java/BUILD
Expand Up @@ -13,7 +13,12 @@ load("//tensorflow/lite/java:aar_with_jni.bzl", "aar_with_jni")

JAVA_SRCS = glob([
"src/main/java/org/tensorflow/lite/*.java",
])
]) + select({
"//tensorflow:android": [
"//tensorflow/lite/delegates/nnapi/java/src/main/java/org/tensorflow/lite/nnapi:nnapi_delegate_src"
],
"//conditions:default": []
})

# Building tensorflow-lite.aar including 4 variants of .so
# To build an aar for release, run below command:
Expand Down Expand Up @@ -262,6 +267,8 @@ tflite_jni_binary(
name = "libtensorflowlite_jni.so",
deps = [
"//tensorflow/lite/java/src/main/native",
"//tensorflow/lite/delegates/nnapi:nnapi_delegate",
"//tensorflow/lite/delegates/nnapi/java/src/main/native:native",
],
)

Expand Down
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

Expand All @@ -198,6 +201,10 @@ public void close() {
gpuDelegate.close();
gpuDelegate = null;
}
if (nnapiDelegate != null) {
nnapiDelegate.close();
nnapiDelegate = null;
}
tfliteModel = null;
}

Expand Down