Allow using libneuralnetworks.so on non-Android platforms.

PiperOrigin-RevId: 230957760
This commit is contained in:
A. Unique TensorFlower 2019-01-25 13:25:03 -08:00 committed by TensorFlower Gardener
parent 1a489ccbde
commit 17b71f26ab
4 changed files with 78 additions and 32 deletions

View File

@ -15,13 +15,26 @@ cc_library(
cc_library(
name = "nnapi_implementation",
srcs = [
"nnapi_implementation.cc",
],
srcs = select({
"//tensorflow:ios": [
"nnapi_implementation_disabled.cc",
],
"//tensorflow:windows": [
"nnapi_implementation_disabled.cc",
],
"//conditions:default": [
"nnapi_implementation.cc",
],
}),
hdrs = [
"nnapi_implementation.h",
],
linkopts = ["-ldl"],
linkopts = ["-ldl"] + select({
"//tensorflow:android": [],
"//tensorflow:ios": [],
"//tensorflow:windows": [],
"//conditions:default": ["-lrt"],
}),
deps = [
"//tensorflow/lite/nnapi:nnapi_lib",
],

View File

@ -14,14 +14,17 @@ limitations under the License.
==============================================================================*/
#include "tensorflow/lite/nnapi/nnapi_implementation.h"
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstdlib>
#ifdef __ANDROID__
#include <dlfcn.h>
#include <sys/mman.h>
#include <sys/system_properties.h>
#include <unistd.h>
#endif
#endif // __ANDROID__
#define NNAPI_LOG(format, ...) fprintf(stderr, format "\n", __VA_ARGS__);
@ -46,6 +49,7 @@ int32_t GetAndroidSdkVersion() {
}
return 0;
}
#endif // __ANDROID__
void* LoadFunction(void* handle, const char* name) {
if (handle == nullptr) {
@ -58,43 +62,48 @@ void* LoadFunction(void* handle, const char* name) {
return fn;
}
#ifndef __ANDROID__
// Add /dev/shm implementation of shared memory for non-Android platforms
int ASharedMemory_create(const char* name, size_t size) {
int fd = shm_open(name, O_RDWR | O_CREAT, 0644);
if (fd >= 0) {
ftruncate(fd, size);
}
return fd;
}
#endif // __ANDROID__
#define LOAD_FUNCTION(handle, name) \
nnapi.name = reinterpret_cast<name##_fn>(LoadFunction(handle, #name));
#else
#define LOAD_FUNCTION(handle, name) nnapi.name = nullptr;
#endif
const NnApi LoadNnApi() {
NnApi nnapi = {};
nnapi.android_sdk_version = 0;
#ifdef __ANDROID__
void* libneuralnetworks = nullptr;
void* libandroid = nullptr;
nnapi.android_sdk_version = GetAndroidSdkVersion();
if (nnapi.android_sdk_version < 27) {
NNAPI_LOG("nnapi error: requires android sdk version to be at least %d",
27);
} else {
// TODO: change RTLD_LOCAL? Assumes there can be multiple instances of nn
// api RT
libneuralnetworks = dlopen("libneuralnetworks.so", RTLD_LAZY | RTLD_LOCAL);
if (libneuralnetworks == nullptr) {
NNAPI_LOG("nnapi error: unable to open library %s",
"libneuralnetworks.so");
}
libandroid = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
if (libandroid == nullptr) {
NNAPI_LOG("nnapi error: unable to open library %s", "libandroid.so");
}
nnapi.nnapi_exists = false;
return nnapi;
}
libandroid = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
if (libandroid == nullptr) {
NNAPI_LOG("nnapi error: unable to open library %s", "libandroid.so");
}
#endif // __ANDROID__
void* libneuralnetworks = nullptr;
// TODO(b/123243014): change RTLD_LOCAL? Assumes there can be multiple
// instances of nn api RT
libneuralnetworks = dlopen("libneuralnetworks.so", RTLD_LAZY | RTLD_LOCAL);
if (libneuralnetworks == nullptr) {
NNAPI_LOG("nnapi error: unable to open library %s", "libneuralnetworks.so");
}
nnapi.nnapi_exists = libneuralnetworks != nullptr;
#else
nnapi.nnapi_exists = false;
nnapi.android_sdk_version = 0;
#endif
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksMemory_createFromFd);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksMemory_free);
@ -124,7 +133,11 @@ const NnApi LoadNnApi() {
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksExecution_startCompute);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksEvent_wait);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksEvent_free);
#ifdef __ANDROID__
LOAD_FUNCTION(libandroid, ASharedMemory_create);
#else
nnapi.ASharedMemory_create = ASharedMemory_create;
#endif // __ANDROID__
return nnapi;
}

View File

@ -0,0 +1,20 @@
/* 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/nnapi/nnapi_implementation.h"
const NnApi* NnApiImplementation() {
static const NnApi nnapi = {};
return &nnapi;
}

View File

@ -113,7 +113,7 @@ TEST(NnapiLibTest, NnApiImplementation) {
EXPECT_EQ(nnapi->ANeuralNetworksExecution_startCompute, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksEvent_wait, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksEvent_free, nullptr);
EXPECT_EQ(nnapi->ASharedMemory_create, nullptr);
EXPECT_NE(nnapi->ASharedMemory_create, nullptr);
#endif
}