chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
+108
View File
@@ -0,0 +1,108 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")
load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable")
load("//tensorflow/lite:special_rules.bzl", "if_nnapi")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
default_visibility = [
"//visibility:public",
],
licenses = ["notice"],
)
cc_library(
name = "nnapi_lib",
hdrs = [
"NeuralNetworksShim.h",
"NeuralNetworksTypes.h",
],
compatible_with = get_compatible_with_portable(),
linkopts = if_nnapi(["-ldl"]),
)
cc_library(
name = "nnapi_implementation_headers",
hdrs = ["nnapi_implementation.h"],
compatible_with = get_compatible_with_portable(),
deps = [
":nnapi_lib",
],
)
cc_library(
name = "nnapi_implementation",
srcs = if_nnapi(
not_supported = ["nnapi_implementation_disabled.cc"],
supported = ["nnapi_implementation.cc"],
),
hdrs = ["nnapi_implementation.h"],
compatible_with = get_compatible_with_portable(),
linkopts = if_nnapi(["-ldl"]) + if_nnapi(
supported = ["-lrt"],
supported_android = [],
),
deps = [
":nnapi_lib",
] + if_nnapi(["//tensorflow/lite/nnapi/sl:nnapi_support_library_headers"]),
)
# This target exists only to verify that nnapi_implementation_disabled.cc compiles.
cc_library(
name = "nnapi_implementation_disabled",
testonly = 1,
srcs = ["nnapi_implementation_disabled.cc"],
hdrs = ["nnapi_implementation.h"],
compatible_with = get_compatible_with_portable(),
deps = [":nnapi_lib"],
)
cc_library(
name = "nnapi_util",
srcs = ["nnapi_util.cc"],
hdrs = ["nnapi_util.h"],
compatible_with = get_compatible_with_portable(),
deps = [
":nnapi_implementation_headers",
"//tensorflow/lite:util",
"//tensorflow/lite/core/c:common",
],
)
cc_test(
name = "nnapi_implementation_test",
srcs = ["nnapi_implementation_test.cc"],
deps = [
":nnapi_implementation",
"@com_google_googletest//:gtest_main",
],
)
# Cannot inject NNAPI instance on ios and windows
cc_library(
name = "nnapi_handler",
srcs = if_nnapi(["nnapi_handler.cc"]),
hdrs = if_nnapi(["nnapi_handler.h"]),
deps = [
":nnapi_implementation",
":nnapi_lib",
"//tensorflow/core/platform:logging",
"//tensorflow/lite:framework",
"@com_google_absl//absl/log:check",
],
)
cc_test(
name = "nnapi_handler_test",
srcs = ["nnapi_handler_test.cc"],
tags = [
"no_mac",
"no_windows",
"tflite_not_portable_ios",
],
deps = [
":nnapi_handler",
":nnapi_implementation",
"@com_google_googletest//:gtest_main",
],
)
File diff suppressed because it is too large Load Diff
+919
View File
@@ -0,0 +1,919 @@
/* Copyright 2017 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_NNAPI_NEURALNETWORKSTYPES_H_
#define TENSORFLOW_LITE_NNAPI_NEURALNETWORKSTYPES_H_
#include <stdint.h>
#include <stdio.h>
#include <string>
// WARNING: this header file is DEPRECATED.
// See https://developer.android.com/ndk/guides/neuralnetworks/migration-guide.
typedef struct AHardwareBuffer AHardwareBuffer;
// NN api types based on NNAPI header file
// https://developer.android.com/ndk/reference/group/neural-networks
/**
* Operand types.
*
* The type of operands that can be added to a model.
*
* Although we define many types, most operators accept just a few
* types. Most used are ANEURALNETWORKS_TENSOR_FLOAT32,
* ANEURALNETWORKS_TENSOR_QUANT8_ASYMM, and ANEURALNETWORKS_INT32.
*/
enum {
ANEURALNETWORKS_FLOAT32 = 0,
ANEURALNETWORKS_INT32 = 1,
ANEURALNETWORKS_UINT32 = 2,
ANEURALNETWORKS_TENSOR_FLOAT32 = 3,
ANEURALNETWORKS_TENSOR_INT32 = 4,
ANEURALNETWORKS_TENSOR_QUANT8_ASYMM = 5,
ANEURALNETWORKS_BOOL = 6,
ANEURALNETWORKS_TENSOR_QUANT16_SYMM = 7,
ANEURALNETWORKS_TENSOR_FLOAT16 = 8,
ANEURALNETWORKS_TENSOR_BOOL8 = 9,
ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL = 11,
ANEURALNETWORKS_TENSOR_QUANT8_SYMM = 13,
ANEURALNETWORKS_TENSOR_QUANT8_ASYMM_SIGNED = 14,
};
/**
* Operation types.
*
* The type of operations that can be added to a model.
*/
enum {
ANEURALNETWORKS_ADD = 0,
ANEURALNETWORKS_AVERAGE_POOL_2D = 1,
ANEURALNETWORKS_CONCATENATION = 2,
ANEURALNETWORKS_CONV_2D = 3,
ANEURALNETWORKS_DEPTHWISE_CONV_2D = 4,
ANEURALNETWORKS_DEPTH_TO_SPACE = 5,
ANEURALNETWORKS_DEQUANTIZE = 6,
ANEURALNETWORKS_EMBEDDING_LOOKUP = 7,
ANEURALNETWORKS_FLOOR = 8,
ANEURALNETWORKS_FULLY_CONNECTED = 9,
ANEURALNETWORKS_HASHTABLE_LOOKUP = 10,
ANEURALNETWORKS_L2_NORMALIZATION = 11,
ANEURALNETWORKS_L2_POOL_2D = 12,
ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION = 13,
ANEURALNETWORKS_LOGISTIC = 14,
ANEURALNETWORKS_LSH_PROJECTION = 15,
ANEURALNETWORKS_LSTM = 16,
ANEURALNETWORKS_MAX_POOL_2D = 17,
ANEURALNETWORKS_MUL = 18,
ANEURALNETWORKS_RELU = 19,
ANEURALNETWORKS_RELU1 = 20,
ANEURALNETWORKS_RELU6 = 21,
ANEURALNETWORKS_RESHAPE = 22,
ANEURALNETWORKS_RESIZE_BILINEAR = 23,
ANEURALNETWORKS_RNN = 24,
ANEURALNETWORKS_SOFTMAX = 25,
ANEURALNETWORKS_SPACE_TO_DEPTH = 26,
ANEURALNETWORKS_SVDF = 27,
ANEURALNETWORKS_TANH = 28,
ANEURALNETWORKS_BATCH_TO_SPACE_ND = 29,
ANEURALNETWORKS_DIV = 30,
ANEURALNETWORKS_MEAN = 31,
ANEURALNETWORKS_PAD = 32,
ANEURALNETWORKS_SPACE_TO_BATCH_ND = 33,
ANEURALNETWORKS_SQUEEZE = 34,
ANEURALNETWORKS_STRIDED_SLICE = 35,
ANEURALNETWORKS_SUB = 36,
ANEURALNETWORKS_TRANSPOSE = 37,
ANEURALNETWORKS_ABS = 38,
ANEURALNETWORKS_ARGMAX = 39,
ANEURALNETWORKS_ARGMIN = 40,
ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM = 42,
ANEURALNETWORKS_CAST = 45,
ANEURALNETWORKS_EQUAL = 48,
ANEURALNETWORKS_EXP = 49,
ANEURALNETWORKS_EXPAND_DIMS = 50,
ANEURALNETWORKS_GATHER = 51,
ANEURALNETWORKS_GREATER = 53,
ANEURALNETWORKS_GREATER_EQUAL = 54,
ANEURALNETWORKS_GROUPED_CONV_2D = 55,
ANEURALNETWORKS_LESS = 58,
ANEURALNETWORKS_LESS_EQUAL = 59,
ANEURALNETWORKS_LOG = 60,
ANEURALNETWORKS_LOGICAL_AND = 61,
ANEURALNETWORKS_LOGICAL_NOT = 62,
ANEURALNETWORKS_LOGICAL_OR = 63,
ANEURALNETWORKS_LOG_SOFTMAX = 64,
ANEURALNETWORKS_MAXIMUM = 65,
ANEURALNETWORKS_MINIMUM = 66,
ANEURALNETWORKS_NEG = 67,
ANEURALNETWORKS_NOT_EQUAL = 68,
ANEURALNETWORKS_PAD_V2 = 69,
ANEURALNETWORKS_POW = 70,
ANEURALNETWORKS_PRELU = 71,
ANEURALNETWORKS_QUANTIZE = 72,
ANEURALNETWORKS_QUANTIZED_16BIT_LSTM = 73,
ANEURALNETWORKS_REDUCE_ANY = 76,
ANEURALNETWORKS_REDUCE_MAX = 77,
ANEURALNETWORKS_REDUCE_MIN = 78,
ANEURALNETWORKS_REDUCE_PROD = 79,
ANEURALNETWORKS_REDUCE_SUM = 80,
ANEURALNETWORKS_RSQRT = 83,
ANEURALNETWORKS_SELECT = 84,
ANEURALNETWORKS_SIN = 85,
ANEURALNETWORKS_SLICE = 86,
ANEURALNETWORKS_SPLIT = 87,
ANEURALNETWORKS_SQRT = 88,
ANEURALNETWORKS_TILE = 89,
ANEURALNETWORKS_TOPK_V2 = 90,
ANEURALNETWORKS_TRANSPOSE_CONV = 91,
ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM = 92,
ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN = 93,
ANEURALNETWORKS_RESIZE_NEAREST_NEIGHBOR = 94,
ANEURALNETWORKS_QUANTIZED_LSTM = 95,
ANEURALNETWORKS_IF = 96,
ANEURALNETWORKS_WHILE = 97,
ANEURALNETWORKS_ELU = 98,
ANEURALNETWORKS_HARD_SWISH = 99,
ANEURALNETWORKS_FILL = 100,
ANEURALNETWORKS_RANK = 101,
ANEURALNETWORKS_BATCH_MATMUL = 102,
ANEURALNETWORKS_PACK = 103,
ANEURALNETWORKS_MIRROR_PAD = 104,
ANEURALNETWORKS_REVERSE = 105,
};
/**
* Fused activation function types.
*
*/
enum {
ANEURALNETWORKS_FUSED_NONE = 0,
ANEURALNETWORKS_FUSED_RELU = 1,
ANEURALNETWORKS_FUSED_RELU1 = 2,
ANEURALNETWORKS_FUSED_RELU6 = 3,
};
/**
* Execution preferences.
*/
enum {
ANEURALNETWORKS_PREFER_LOW_POWER = 0,
ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER = 1,
ANEURALNETWORKS_PREFER_SUSTAINED_SPEED = 2,
};
/**
* Result codes.
*/
// LINT.IfChange
enum {
ANEURALNETWORKS_NO_ERROR = 0,
ANEURALNETWORKS_OUT_OF_MEMORY = 1,
ANEURALNETWORKS_INCOMPLETE = 2,
ANEURALNETWORKS_UNEXPECTED_NULL = 3,
ANEURALNETWORKS_BAD_DATA = 4,
ANEURALNETWORKS_OP_FAILED = 5,
ANEURALNETWORKS_BAD_STATE = 6,
ANEURALNETWORKS_UNMAPPABLE = 7,
ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE = 8,
ANEURALNETWORKS_UNAVAILABLE_DEVICE = 9,
ANEURALNETWORKS_MISSED_DEADLINE_TRANSIENT = 10,
ANEURALNETWORKS_MISSED_DEADLINE_PERSISTENT = 11,
ANEURALNETWORKS_RESOURCE_EXHAUSTED_TRANSIENT = 12,
ANEURALNETWORKS_RESOURCE_EXHAUSTED_PERSISTENT = 13,
ANEURALNETWORKS_DEAD_OBJECT = 14,
};
// LINT.ThenChange(//tensorflow/lite/delegates/nnapi/nnapi_delegate.cc:NnApiErrorDescription)
/**
* Implicit padding algorithms.
*/
enum {
ANEURALNETWORKS_PADDING_SAME = 1,
ANEURALNETWORKS_PADDING_VALID = 2,
};
/**
* Device types.
*
* The type of NNAPI device.
*/
enum {
/** The device type cannot be provided. */
ANEURALNETWORKS_DEVICE_UNKNOWN = 0,
/** The device does not fall into any category below. */
ANEURALNETWORKS_DEVICE_OTHER = 1,
/** The device runs NNAPI models on single or multi-core CPU. */
ANEURALNETWORKS_DEVICE_CPU = 2,
/** The device can run NNAPI models and also accelerate graphics APIs such
* as OpenGL ES and Vulkan. */
ANEURALNETWORKS_DEVICE_GPU = 3,
/** Dedicated accelerator for Machine Learning workloads. */
ANEURALNETWORKS_DEVICE_ACCELERATOR = 4,
};
/**
* Relative execution priority.
*
* Available since API level 30.
*/
enum {
ANEURALNETWORKS_PRIORITY_LOW = 90,
ANEURALNETWORKS_PRIORITY_MEDIUM = 100,
ANEURALNETWORKS_PRIORITY_HIGH = 110,
ANEURALNETWORKS_PRIORITY_DEFAULT = ANEURALNETWORKS_PRIORITY_MEDIUM,
};
/**
* NNAPI feature levels.
*
* Each update of the NNAPI specification yields a new NNAPI feature level enum
* value. NNAPI feature level corrseponds to an NNAPI specification version that
* a driver and/or the NNAPI runtime can implement.
*/
enum {
/** NNAPI specification available in Android O-MR1, Android NNAPI feature
level 1 */
ANEURALNETWORKS_FEATURE_LEVEL_1 = 27,
/** NNAPI specification available in Android P, Android NNAPI feature level 2
*/
ANEURALNETWORKS_FEATURE_LEVEL_2 = 28,
/** NNAPI specification available in Android Q, Android NNAPI feature level 3
*/
ANEURALNETWORKS_FEATURE_LEVEL_3 = 29,
/** NNAPI specification available in Android R, Android NNAPI feature level 4
*/
ANEURALNETWORKS_FEATURE_LEVEL_4 = 30,
/**
* NNAPI specification available in Android S, Android NNAPI feature level 5.
* After Android S, the NNAPI specification can be updated between Android
* API releases.
*/
ANEURALNETWORKS_FEATURE_LEVEL_5 = 31,
/** Android NNAPI feature level 6 */
ANEURALNETWORKS_FEATURE_LEVEL_6 = 1000006,
/** Android NNAPI feature level 7 */
ANEURALNETWORKS_FEATURE_LEVEL_7 = 1000007,
/** Android NNAPI feature level 8 */
ANEURALNETWORKS_FEATURE_LEVEL_8 = 1000008,
};
/**
* ANeuralNetworksMemoryDesc is an opaque type that represents a memory
* descriptor.
*
* A memory descriptor describes the properties of a memory object, and is used
* by
* {@link ANeuralNetworksMemory_createFromDesc}.
*
* To use:
* - Create a new memory descriptor by calling
* {@link ANeuralNetworksMemoryDesc_create}.
* - Specify all of the intended input and output roles by calling
* {@link ANeuralNetworksMemoryDesc_addInputRole} and
* {@link ANeuralNetworksMemoryDesc_addOutputRole}.
* - Optionally, specify the memory dimensions by calling
* {@link ANeuralNetworksMemoryDesc_setDimensions}.
* - Complete the memory descriptor with {@link
* ANeuralNetworksMemoryDesc_finish}.
* - Use the memory descriptor as many times as needed with
* {@link ANeuralNetworksMemory_createFromDesc}.
* - Destroy the memory descriptor with {@link
* ANeuralNetworksMemoryDesc_free}.
*
* A memory descriptor is completed by calling {@link
* ANeuralNetworksMemoryDesc_finish}. A memory descriptor is destroyed by
* calling {@link ANeuralNetworksMemoryDesc_free}.
*
* A memory descriptor must not be modified once
* {@link ANeuralNetworksMemoryDesc_finish}
* has been called on it.
*
* It is the application's responsibility to make sure that only
* one thread modifies a memory descriptor at a given time. It is however
* safe for more than one thread to use the memory descriptor once
* {@link ANeuralNetworksMemoryDesc_finish} has returned.
*
* It is also the application's responsibility to ensure that there are no other
* uses of the memory descriptor after calling {@link
* ANeuralNetworksMemoryDesc_free}. It is however safe to continue using a
* {@link ANeuralNetworksMemory} object created from the memory descriptor.
*
* Available since API level 30.
*/
typedef struct ANeuralNetworksMemoryDesc ANeuralNetworksMemoryDesc;
/**
* ANeuralNetworksMemory is an opaque type that represents memory.
*
* This type is used to represent shared memory, memory mapped files,
* and similar memories.
*
* By using shared memory, a program can efficiently communicate to the
* runtime and drivers the tensors that define a model. See
* {@link ANeuralNetworksModel_setOperandValueFromMemory}. An application
* should typically create one shared memory object that contains every tensor
* needed to define a model. {@link ANeuralNetworksMemory_createFromFd} can be
* used to create shared memory from a file handle. {@link
* ANeuralNetworksMemory_createShared} can be used to directly created shared
* memory.
*
* Memory objects can also be used to specify the input and output arguments of
* an execution. See {@link ANeuralNetworksExecution_setInputFromMemory}
* and {@link ANeuralNetworksExecution_setOutputFromMemory}.
*/
typedef struct ANeuralNetworksMemory ANeuralNetworksMemory;
/**
* ANeuralNetworksModel is an opaque type that contains a description of the
* mathematical operations that constitute the model.
*
* <p>The model will be built by calling<ul>
* <li>{@link ANeuralNetworksModel_create},</li>
* <li>{@link ANeuralNetworksModel_addOperation},</li>
* <li>{@link ANeuralNetworksModel_addOperand},</li>
* </ul>
*
* A model is completed by calling {@link ANeuralNetworksModel_finish}.
* A model is destroyed by calling {@link ANeuralNetworksModel_free}.
*
* <p>It is the application's responsibility to make sure that only one thread
* modifies a model at a given time. It is however safe for more than one
* thread to use the model once {@link ANeuralNetworksModel_finish} has
* returned.</p>
*
* <p>It is also the application's responsibility to ensure that there are no
* other uses of the model after calling {@link ANeuralNetworksModel_free}. This
* includes any compilation or execution object created using the model.</p>
*/
typedef struct ANeuralNetworksModel ANeuralNetworksModel;
/**
* ANeuralNetworksCompilation is an opaque type that can be used to compile
* a machine learning model.
*
* <p>To use:<ul>
* <li>Create a new compilation instance by calling the
* {@link ANeuralNetworksCompilation_create} function.</li>
* <li>Perform the compilation with {@link
* ANeuralNetworksCompilation_start}.</li> <li>Wait for the compilation to
* complete with {@link ANeuralNetworksCompilation_wait}.</li> <li>Use the
* compilation as many times as needed with {@link
* ANeuralNetworksExecution_create}.</li> <li>Destroy the compilation with
* {@link ANeuralNetworksCompilation_free} once all executions using the
* compilation have completed.</li></ul></p>
*
* <p>A compilation cannot be modified once {@link
* ANeuralNetworksCompilation_start} has been called on it.</p>
*
* <p>It is the application's responsibility to make sure that only one thread
* modifies a compilation at a given time. It is however safe for more than one
* thread to use {@link ANeuralNetworksCompilation_wait} at the same time.
* It is also safe for multiple threads to use a compilation object once
* {@link ANeuralNetworksCompilation_wait} has completed.</p>
*
* <p>It is also the application's responsibility to ensure that there are no
* other uses of the compilation after calling {@link
* ANeuralNetworksCompilation_free}. This includes any execution object created
* using the compilation.</p>
*/
typedef struct ANeuralNetworksCompilation ANeuralNetworksCompilation;
/**
* ANeuralNetworksExecution is an opaque type that can be used to apply a
* machine learning model to a set of inputs.
*
* <p>To use:<ul>
* <li>Create a new execution instance by calling the
* {@link ANeuralNetworksExecution_create} function.</li>
* <li>Associate data to the model inputs with
* {@link ANeuralNetworksExecution_setInput} or
* {@link ANeuralNetworksExecution_setInputFromMemory}.</li>
* <li>Associate output buffers to the model outputs with
* {@link ANeuralNetworksExecution_setOutput} or
* {@link ANeuralNetworksExecution_setOutputFromMemory}.</li>
* <li>Apply the model with {@link
* ANeuralNetworksExecution_startCompute}.</li> <li>Wait for the execution to
* complete with {@link ANeuralNetworksExecution_wait}.</li> <li>Destroy the
* execution with
* {@link ANeuralNetworksExecution_free}.</li></ul></p>
*
* <p>An execution cannot be modified once {@link
* ANeuralNetworksExecution_start} has been called on it.</p>
*
* <p>An execution can be applied to a model with
* {@link ANeuralNetworksExecution_startCompute} only once. Create new
* executions to do new evaluations of the model.</p>
*
* <p>It is the application's responsibility to make sure that only one thread
* modifies an execution at a given time. It is however safe for more than one
* thread to use {@link ANeuralNetworksExecution_wait} at the same time.</p>
*
* <p>It is also the application's responsibility to ensure that there are no
* other uses of the request after calling {@link
* ANeuralNetworksRequest_free}.</p>
*/
typedef struct ANeuralNetworksExecution ANeuralNetworksExecution;
/**
* Parameters for ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL operand.
*/
typedef struct ANeuralNetworksSymmPerChannelQuantParams {
/* The index of the channel dimension. */
uint32_t channelDim;
/** The size of the scale array. Should be equal to dimension[channelDim] of
* the Operand. */
uint32_t scaleCount;
/** The array of scaling values for each channel. Each value must be greater
* than zero. */
const float* scales;
} ANeuralNetworksSymmPerChannelQuantParams;
/**
* ANeuralNetworksBurst is an opaque type that can be used to reduce the latency
* of a rapid sequence of executions. It will likely cause overhead if only used
* for a single execution.
*
* ANeuralNetworksBurst serves as a context object for any number of inferences
* using {@link ANeuralNetworksExecution} objects. An ANeuralNetworksBurst
* object and the {@link ANeuralNetworksExecution} objects used with it must all
* have been created from the same {@link ANeuralNetworksCompilation} object.
*
* This object is also used as a hint to drivers, providing insight to the
* lifetime of a rapid sequence of executions. For example, a driver may choose
* to increase the clock frequency of its accelerator for the lifetime of a
* burst object.
*
* <p>To use:<ul>
* <li>Create a new burst object by calling the
* {@link ANeuralNetworksBurst_create} function.</li>
* <li>For each execution:</li><ul>
* <li>Create {@link ANeuralNetworksExecution} and configure its
* properties (see {@link ANeuralNetworksExecution} for
* details).</li> <li>Apply the model synchronously with
* {@link ANeuralNetworksExecution_burstCompute}, reusing the same
* {@link ANeuralNetworksBurst} with the new
* {@link ANeuralNetworksExecution}.</li>
* <li>Use and free the {@link ANeuralNetworksExecution}.</li></ul>
* <li>Destroy the burst with
* {@link ANeuralNetworksBurst_free}.</li></ul></p>
*
* Available since API level 29.
*/
typedef struct ANeuralNetworksBurst ANeuralNetworksBurst;
/**
* ANeuralNetworksOperandType describes the type of an operand.
* This structure is used to describe both scalars and tensors.
*/
typedef struct ANeuralNetworksOperandType {
/** The data type, e.g ANEURALNETWORKS_INT8. */
int32_t type;
/** The number of dimensions. It should be 0 for scalars. */
uint32_t dimensionCount;
/** The dimensions of the tensor. It should be nullptr for scalars. */
const uint32_t* dimensions;
/** These two fields are only used for quantized tensors.
* They should be zero for scalars and non-fixed point tensors.
* The dequantized value of each entry is (value - offset) * scale.
*/
float scale;
int32_t zeroPoint;
} ANeuralNetworksOperandType;
/**
* ANeuralNetworksEvent is an opaque type that represents an event
* that will be signaled once an execution completes.
*/
typedef struct ANeuralNetworksEvent ANeuralNetworksEvent;
typedef int32_t ANeuralNetworksOperationType;
/**
* ANeuralNetworksDevice is an opaque type that represents a device.
*
* This type is used to query basic properties and supported operations of the
* corresponding device, and control which device(s) a model is to be run on.
*
* Available since API level 29.
*/
typedef struct ANeuralNetworksDevice ANeuralNetworksDevice;
/**
* Diagnostic result codes.
*/
typedef enum {
ANNDIAG_NO_ERROR = 0,
/**
* Failure caused by failure to load support library driver.
*/
ANNDIAG_FAILED_TO_LOAD_SL = 1,
/**
* Failure caused by failure to register HAL service.
*/
ANNDIAG_FAILED_TO_REGISTER_SERVICE = 2,
/**
* General failure.
*/
ANNDIAG_GENERAL_ERROR = 3,
/**
* Invalid argument
*/
ANNDIAG_INVALID_ARGUMENT = 4,
} ANeuralNetworksDiagnosticResultCode;
/**
* Diagnostic data class.
*/
typedef enum {
ANNDIAG_DATA_CLASS_UNKNOWN = 0,
ANNDIAG_DATA_CLASS_OTHER = 1,
ANNDIAG_DATA_CLASS_FLOAT32 = 2,
ANNDIAG_DATA_CLASS_FLOAT16 = 3,
ANNDIAG_DATA_CLASS_QUANT = 4,
ANNDIAG_DATA_CLASS_MIXED = 5
} ANeuralNetworksDiagnosticDataClass;
/**
* Diagnostic execution mode.
*/
typedef enum {
ANNDIAG_EXECUTION_MODE_UNKNOWN = 0,
ANNDIAG_EXECUTION_MODE_ASYNC = 1,
ANNDIAG_EXECUTION_MODE_SYNC = 2,
ANNDIAG_EXECUTION_MODE_BURST = 3,
ANNDIAG_EXECUTION_MODE_ASYNC_WITH_DEPS = 4,
} ANeuralNetworksDiagnosticExecutionMode;
typedef struct ANeuralNetworksDiagnosticCompilationInfo
ANeuralNetworksDiagnosticCompilationInfo;
typedef struct ANeuralNetworksDiagnosticExecutionInfo
ANeuralNetworksDiagnosticExecutionInfo;
typedef void (*ANeuralNetworksDiagnosticCompilationFinishedCallback)(
const void* context, const ANeuralNetworksDiagnosticCompilationInfo* info);
typedef void (*ANeuralNetworksDiagnosticExecutionFinishedCallback)(
const void* context, const ANeuralNetworksDiagnosticExecutionInfo* info);
// nn api function types
typedef int (*ANeuralNetworksMemory_createFromFd_fn)(
size_t size, int protect, int fd, size_t offset,
ANeuralNetworksMemory** memory);
typedef void (*ANeuralNetworksMemory_free_fn)(ANeuralNetworksMemory* memory);
typedef int (*ANeuralNetworksModel_create_fn)(ANeuralNetworksModel** model);
typedef int (*ANeuralNetworksModel_finish_fn)(ANeuralNetworksModel* model);
typedef void (*ANeuralNetworksModel_free_fn)(ANeuralNetworksModel* model);
typedef int (*ANeuralNetworksCompilation_create_fn)(
ANeuralNetworksModel* model, ANeuralNetworksCompilation** compilation);
typedef void (*ANeuralNetworksCompilation_free_fn)(
ANeuralNetworksCompilation* compilation);
typedef int (*ANeuralNetworksCompilation_setPreference_fn)(
ANeuralNetworksCompilation* compilation, int32_t preference);
typedef int (*ANeuralNetworksCompilation_finish_fn)(
ANeuralNetworksCompilation* compilation);
typedef int (*ANeuralNetworksModel_addOperand_fn)(
ANeuralNetworksModel* model, const ANeuralNetworksOperandType* type);
typedef int (*ANeuralNetworksModel_setOperandValue_fn)(
ANeuralNetworksModel* model, int32_t index, const void* buffer,
size_t length);
typedef int (*ANeuralNetworksModel_setOperandSymmPerChannelQuantParams_fn)(
ANeuralNetworksModel* model, int32_t index,
const ANeuralNetworksSymmPerChannelQuantParams* channelQuant);
typedef int (*ANeuralNetworksModel_setOperandValueFromMemory_fn)(
ANeuralNetworksModel* model, int32_t index,
const ANeuralNetworksMemory* memory, size_t offset, size_t length);
typedef int (*ANeuralNetworksModel_addOperation_fn)(
ANeuralNetworksModel* model, ANeuralNetworksOperationType type,
uint32_t inputCount, const uint32_t* inputs, uint32_t outputCount,
const uint32_t* outputs);
typedef int (*ANeuralNetworksModel_identifyInputsAndOutputs_fn)(
ANeuralNetworksModel* model, uint32_t inputCount, const uint32_t* inputs,
uint32_t outputCount, const uint32_t* outputs);
typedef int (*ANeuralNetworksModel_relaxComputationFloat32toFloat16_fn)(
ANeuralNetworksModel* model, bool allow);
typedef int (*ANeuralNetworksExecution_create_fn)(
ANeuralNetworksCompilation* compilation,
ANeuralNetworksExecution** execution);
typedef void (*ANeuralNetworksExecution_free_fn)(
ANeuralNetworksExecution* execution);
typedef int (*ANeuralNetworksExecution_setInput_fn)(
ANeuralNetworksExecution* execution, int32_t index,
const ANeuralNetworksOperandType* type, const void* buffer, size_t length);
typedef int (*ANeuralNetworksExecution_setInputFromMemory_fn)(
ANeuralNetworksExecution* execution, int32_t index,
const ANeuralNetworksOperandType* type, const ANeuralNetworksMemory* memory,
size_t offset, size_t length);
typedef int (*ANeuralNetworksExecution_setOutput_fn)(
ANeuralNetworksExecution* execution, int32_t index,
const ANeuralNetworksOperandType* type, void* buffer, size_t length);
typedef int (*ANeuralNetworksExecution_setOutputFromMemory_fn)(
ANeuralNetworksExecution* execution, int32_t index,
const ANeuralNetworksOperandType* type, const ANeuralNetworksMemory* memory,
size_t offset, size_t length);
typedef int (*ANeuralNetworksExecution_startCompute_fn)(
ANeuralNetworksExecution* execution, ANeuralNetworksEvent** event);
typedef int (*ANeuralNetworksEvent_wait_fn)(ANeuralNetworksEvent* event);
typedef void (*ANeuralNetworksEvent_free_fn)(ANeuralNetworksEvent* event);
typedef int (*ASharedMemory_create_fn)(const char* name, size_t size);
typedef int (*ANeuralNetworks_getDeviceCount_fn)(uint32_t* numDevices);
typedef int (*ANeuralNetworks_getDevice_fn)(uint32_t devIndex,
ANeuralNetworksDevice** device);
typedef int (*ANeuralNetworksDevice_getName_fn)(
const ANeuralNetworksDevice* device, const char** name);
typedef int (*ANeuralNetworksDevice_getType_fn)(
const ANeuralNetworksDevice* device, int32_t* type);
typedef int (*ANeuralNetworksDevice_getVersion_fn)(
const ANeuralNetworksDevice* device, const char** version);
typedef int (*ANeuralNetworksDevice_getFeatureLevel_fn)(
const ANeuralNetworksDevice* device, int64_t* featureLevel);
typedef int (*ANeuralNetworksModel_getSupportedOperationsForDevices_fn)(
const ANeuralNetworksModel* model,
const ANeuralNetworksDevice* const* devices, uint32_t numDevices,
bool* supportedOps);
typedef int (*ANeuralNetworksCompilation_createForDevices_fn)(
ANeuralNetworksModel* model, const ANeuralNetworksDevice* const* devices,
uint32_t numDevices, ANeuralNetworksCompilation** compilation);
typedef int (*ANeuralNetworksCompilation_setCaching_fn)(
ANeuralNetworksCompilation* compilation, const char* cacheDir,
const uint8_t* token);
typedef int (*ANeuralNetworksCompilation_setTimeout_fn)(
ANeuralNetworksCompilation* compilation, uint64_t duration);
typedef int (*ANeuralNetworksCompilation_setPriority_fn)(
ANeuralNetworksCompilation* compilation, int priority);
typedef int (*ANeuralNetworksExecution_compute_fn)(
ANeuralNetworksExecution* execution);
typedef int (*ANeuralNetworksExecution_setTimeout_fn)(
ANeuralNetworksExecution* execution, uint64_t duration);
typedef int (*ANeuralNetworksExecution_setLoopTimeout_fn)(
ANeuralNetworksExecution* execution, uint64_t duration);
typedef int (*ANeuralNetworksExecution_getOutputOperandRank_fn)(
ANeuralNetworksExecution* execution, int32_t index, uint32_t* rank);
typedef int (*ANeuralNetworksExecution_getOutputOperandDimensions_fn)(
ANeuralNetworksExecution* execution, int32_t index, uint32_t* dimensions);
typedef int (*ANeuralNetworksBurst_create_fn)(
ANeuralNetworksCompilation* compilation, ANeuralNetworksBurst** burst);
typedef void (*ANeuralNetworksBurst_free_fn)(ANeuralNetworksBurst* burst);
typedef int (*ANeuralNetworksExecution_burstCompute_fn)(
ANeuralNetworksExecution* execution, ANeuralNetworksBurst* burst);
typedef int (*ANeuralNetworksMemory_createFromAHardwareBuffer_fn)(
const AHardwareBuffer* ahwb, ANeuralNetworksMemory** memory);
typedef int (*ANeuralNetworksExecution_setMeasureTiming_fn)(
ANeuralNetworksExecution* execution, bool measure);
typedef enum {
// Execution time on hardware (not driver, which runs on host processor).
ANEURALNETWORKS_DURATION_ON_HARDWARE = 0,
// Execution time in driver (including time on hardware). Excludes overhead
// such as that of the runtime itself and the IPC needed for the runtime to
// communicate with the driver.
ANEURALNETWORKS_DURATION_IN_DRIVER = 1,
// Execution time on hardware, after all dependencies have been signaled.
// If no dependencies specified (for example, if the execution was scheduled
// other
// than with {@link ANeuralNetworksExecution_startComputeWithDependencies}),
// the
// reported time will be the same as ANEURALNETWORKS_DURATION_ON_HARDWARE.
// Available since API level 30.
ANEURALNETWORKS_FENCED_DURATION_ON_HARDWARE = 2,
// Execution time in driver, after all dependencies have been signaled.
// Excludes
// overhead such as that of the runtime itself and the IPC needed for the
// runtime
// to communicate with the driver.
// If no dependencies specified (for example, if the execution was scheduled
// other
// than with {@link ANeuralNetworksExecution_startComputeWithDependencies}),
// the
// reported time will be the same as ANEURALNETWORKS_DURATION_IN_DRIVER.
// Available since API level 30.
ANEURALNETWORKS_FENCED_DURATION_IN_DRIVER = 3,
} DurationCode;
typedef int (*ANeuralNetworksExecution_getDuration_fn)(
const ANeuralNetworksExecution* execution, int32_t durationCode,
uint64_t* duration);
typedef int (*ANeuralNetworksDevice_getExtensionSupport_fn)(
const ANeuralNetworksDevice* device, const char* extensionName,
bool* isExtensionSupported);
typedef int (*ANeuralNetworksModel_getExtensionOperandType_fn)(
ANeuralNetworksModel* model, const char* extensionName,
uint16_t operandCodeWithinExtension, int32_t* type);
typedef int (*ANeuralNetworksModel_getExtensionOperationType_fn)(
ANeuralNetworksModel* model, const char* extensionName,
uint16_t operationCodeWithinExtension, ANeuralNetworksOperationType* type);
typedef int (*ANeuralNetworksModel_setOperandExtensionData_fn)(
ANeuralNetworksModel* model, int32_t index, const void* data,
size_t length);
typedef int (*ANeuralNetworksMemoryDesc_create_fn)(
ANeuralNetworksMemoryDesc** desc);
typedef void (*ANeuralNetworksMemoryDesc_free_fn)(
ANeuralNetworksMemoryDesc* desc);
typedef int (*ANeuralNetworksMemoryDesc_addInputRole_fn)(
ANeuralNetworksMemoryDesc* desc,
const ANeuralNetworksCompilation* compilation, uint32_t index,
float frequency);
typedef int (*ANeuralNetworksMemoryDesc_addOutputRole_fn)(
ANeuralNetworksMemoryDesc* desc,
const ANeuralNetworksCompilation* compilation, uint32_t index,
float frequency);
typedef int (*ANeuralNetworksMemoryDesc_setDimensions_fn)(
ANeuralNetworksMemoryDesc* desc, uint32_t rank, const uint32_t* dimensions);
typedef int (*ANeuralNetworksMemoryDesc_finish_fn)(
ANeuralNetworksMemoryDesc* desc);
typedef int (*ANeuralNetworksMemory_createFromDesc_fn)(
const ANeuralNetworksMemoryDesc* desc, ANeuralNetworksMemory** memory);
typedef int (*ANeuralNetworksMemory_copy_fn)(const ANeuralNetworksMemory* src,
const ANeuralNetworksMemory* dst);
typedef int (*ANeuralNetworksEvent_createFromSyncFenceFd_fn)(
int sync_fence_fd, ANeuralNetworksEvent** event);
typedef int (*ANeuralNetworksEvent_getSyncFenceFd_fn)(
const ANeuralNetworksEvent* event, int* sync_fence_fd);
typedef int (*ANeuralNetworksExecution_startComputeWithDependencies_fn)(
ANeuralNetworksExecution* execution,
const ANeuralNetworksEvent* const* dependencies, uint32_t num_dependencies,
uint64_t duration, ANeuralNetworksEvent** event);
typedef int (*ANeuralNetworksExecution_enableInputAndOutputPadding_fn)(
ANeuralNetworksExecution* execution, bool enable);
typedef int (*ANeuralNetworksExecution_setReusable_fn)(
ANeuralNetworksExecution* execution, bool reusable);
typedef int64_t (*ANeuralNetworks_getRuntimeFeatureLevel_fn)();
typedef int32_t (*SL_ANeuralNetworksDiagnosticCompilationInfo_getSessionId_fn)(
const ANeuralNetworksDiagnosticCompilationInfo* diagnosticCompilationInfo);
typedef int64_t (
*SL_ANeuralNetworksDiagnosticCompilationInfo_getNnApiVersion_fn)(
const ANeuralNetworksDiagnosticCompilationInfo* diagnosticCompilationInfo);
typedef const uint8_t* (
*SL_ANeuralNetworksDiagnosticCompilationInfo_getModelArchHash_fn)(
const ANeuralNetworksDiagnosticCompilationInfo* diagnosticCompilationInfo);
typedef const char* (
*SL_ANeuralNetworksDiagnosticCompilationInfo_getDeviceIds_fn)(
const ANeuralNetworksDiagnosticCompilationInfo* diagnosticCompilationInfo);
typedef int32_t (*SL_ANeuralNetworksDiagnosticCompilationInfo_getErrorCode_fn)(
const ANeuralNetworksDiagnosticCompilationInfo* diagnosticCompilationInfo);
typedef ANeuralNetworksDiagnosticDataClass (
*SL_ANeuralNetworksDiagnosticCompilationInfo_getInputDataClass_fn)(
const ANeuralNetworksDiagnosticCompilationInfo* diagnosticCompilationInfo);
typedef ANeuralNetworksDiagnosticDataClass (
*SL_ANeuralNetworksDiagnosticCompilationInfo_getOutputDataClass_fn)(
const ANeuralNetworksDiagnosticCompilationInfo* diagnosticCompilationInfo);
typedef uint64_t (
*SL_ANeuralNetworksDiagnosticCompilationInfo_getCompilationTimeNanos_fn)(
const ANeuralNetworksDiagnosticCompilationInfo* diagnosticCompilationInfo);
typedef bool (*SL_ANeuralNetworksDiagnosticCompilationInfo_isCachingEnabled_fn)(
const ANeuralNetworksDiagnosticCompilationInfo* diagnosticCompilationInfo);
typedef bool (
*SL_ANeuralNetworksDiagnosticCompilationInfo_isControlFlowUsed_fn)(
const ANeuralNetworksDiagnosticCompilationInfo* diagnosticCompilationInfo);
typedef bool (
*SL_ANeuralNetworksDiagnosticCompilationInfo_areDynamicTensorsUsed_fn)(
const ANeuralNetworksDiagnosticCompilationInfo* diagnosticCompilationInfo);
typedef int32_t (*SL_ANeuralNetworksDiagnosticExecutionInfo_getSessionId_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef int64_t (*SL_ANeuralNetworksDiagnosticExecutionInfo_getNnApiVersion_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef const uint8_t* (
*SL_ANeuralNetworksDiagnosticExecutionInfo_getModelArchHash_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef const char* (
*SL_ANeuralNetworksDiagnosticExecutionInfo_getDeviceIds_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef ANeuralNetworksDiagnosticExecutionMode (
*SL_ANeuralNetworksDiagnosticExecutionInfo_getExecutionMode_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef ANeuralNetworksDiagnosticDataClass (
*SL_ANeuralNetworksDiagnosticExecutionInfo_getInputDataClass_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef ANeuralNetworksDiagnosticDataClass (
*SL_ANeuralNetworksDiagnosticExecutionInfo_getOutputDataClass_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef uint32_t (*SL_ANeuralNetworksDiagnosticExecutionInfo_getErrorCode_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef uint64_t (
*SL_ANeuralNetworksDiagnosticExecutionInfo_getRuntimeExecutionTimeNanos_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef uint64_t (
*SL_ANeuralNetworksDiagnosticExecutionInfo_getDriverExecutionTimeNanos_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef uint64_t (
*SL_ANeuralNetworksDiagnosticExecutionInfo_getHardwareExecutionTimeNanos_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef bool (*SL_ANeuralNetworksDiagnosticExecutionInfo_isCachingEnabled_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef bool (*SL_ANeuralNetworksDiagnosticExecutionInfo_isControlFlowUsed_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef bool (
*SL_ANeuralNetworksDiagnosticExecutionInfo_areDynamicTensorsUsed_fn)(
const ANeuralNetworksDiagnosticExecutionInfo* diagnosticExecutionInfo);
typedef void (*SL_ANeuralNetworksDiagnostic_registerCallbacks_fn)(
ANeuralNetworksDiagnosticCompilationFinishedCallback compilationCallback,
ANeuralNetworksDiagnosticExecutionFinishedCallback executionCallback,
void* callbackContext);
#endif // TENSORFLOW_LITE_NNAPI_NEURALNETWORKSTYPES_H_
+15
View File
@@ -0,0 +1,15 @@
# Android Neural Network API
The Android Neural Networks API (NNAPI) is an Android C API designed for running
computationally intensive operators for machine learning on mobile devices.
Tensorflow Lite is designed to use the NNAPI to perform hardware-accelerated
inference operators on supported devices.
Based on the apps requirements and the hardware capabilities on a device, the
NNAPI can distribute the computation workload across available on-device
processors, including dedicated neural network hardware, graphics processing
units (GPUs), and digital signal processors (DSPs).
For devices that lack a specialized vendor driver, the NNAPI runtime relies on
optimized code to execute requests on the CPU. For more information about the
NNAPI, please refer to the [NNAPI documentation](https://developer.android.com/ndk/guides/neuralnetworks/index.html)
+158
View File
@@ -0,0 +1,158 @@
/* 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_handler.h"
#include <cstdint>
#include <cstring>
#include <string>
#include "tensorflow/lite/nnapi/nnapi_implementation.h"
namespace tflite {
namespace nnapi {
// static
const char NnApiHandler::kNnapiReferenceDeviceName[] = "nnapi-reference";
// static
const int NnApiHandler::kNnapiReferenceDevice = 1;
// static
const int NnApiHandler::kNnapiDevice = 2;
char* NnApiHandler::nnapi_device_name_ = nullptr;
int NnApiHandler::nnapi_device_feature_level_;
const NnApi* NnApiPassthroughInstance() {
static const NnApi orig_nnapi_copy = *NnApiImplementation();
return &orig_nnapi_copy;
}
// static
NnApiHandler* NnApiHandler::Instance() {
// Ensuring that the original copy of nnapi is saved before we return
// access to NnApiHandler
NnApiPassthroughInstance();
static NnApiHandler handler{const_cast<NnApi*>(NnApiImplementation())};
return &handler;
}
void NnApiHandler::Reset() {
// Restores global NNAPI to original value
*nnapi_ = *NnApiPassthroughInstance();
}
void NnApiHandler::SetAndroidSdkVersion(int version,
bool set_unsupported_ops_to_null) {
nnapi_->android_sdk_version = version;
nnapi_->nnapi_runtime_feature_level = version;
if (!set_unsupported_ops_to_null) {
return;
}
if (version < 29) {
nnapi_->ANeuralNetworks_getDeviceCount = nullptr;
nnapi_->ANeuralNetworks_getDevice = nullptr;
nnapi_->ANeuralNetworksDevice_getName = nullptr;
nnapi_->ANeuralNetworksDevice_getVersion = nullptr;
nnapi_->ANeuralNetworksDevice_getFeatureLevel = nullptr;
nnapi_->ANeuralNetworksDevice_getType = nullptr;
nnapi_->ANeuralNetworksModel_getSupportedOperationsForDevices = nullptr;
nnapi_->ANeuralNetworksCompilation_createForDevices = nullptr;
nnapi_->ANeuralNetworksCompilation_setCaching = nullptr;
nnapi_->ANeuralNetworksExecution_compute = nullptr;
nnapi_->ANeuralNetworksExecution_getOutputOperandRank = nullptr;
nnapi_->ANeuralNetworksExecution_getOutputOperandDimensions = nullptr;
nnapi_->ANeuralNetworksBurst_create = nullptr;
nnapi_->ANeuralNetworksBurst_free = nullptr;
nnapi_->ANeuralNetworksExecution_burstCompute = nullptr;
nnapi_->ANeuralNetworksMemory_createFromAHardwareBuffer = nullptr;
nnapi_->ANeuralNetworksExecution_setMeasureTiming = nullptr;
nnapi_->ANeuralNetworksExecution_getDuration = nullptr;
nnapi_->ANeuralNetworksDevice_getExtensionSupport = nullptr;
nnapi_->ANeuralNetworksModel_getExtensionOperandType = nullptr;
nnapi_->ANeuralNetworksModel_getExtensionOperationType = nullptr;
nnapi_->ANeuralNetworksModel_setOperandExtensionData = nullptr;
}
if (version < 28) {
nnapi_->ANeuralNetworksModel_relaxComputationFloat32toFloat16 = nullptr;
}
}
void NnApiHandler::SetDeviceName(const std::string& name) {
delete[] nnapi_device_name_;
nnapi_device_name_ = new char[name.size() + 1];
std::strcpy(nnapi_device_name_, name.c_str()); // NOLINT
}
void NnApiHandler::GetDeviceNameReturnsName(const std::string& name) {
NnApiHandler::SetDeviceName(name);
GetDeviceNameReturns<0>();
}
void NnApiHandler::SetNnapiSupportedDevice(const std::string& name,
int feature_level) {
NnApiHandler::SetDeviceName(name);
nnapi_device_feature_level_ = feature_level;
GetDeviceCountReturnsCount<2>();
nnapi_->ANeuralNetworks_getDevice =
[](uint32_t devIndex, ANeuralNetworksDevice** device) -> int {
if (devIndex > 1) {
return ANEURALNETWORKS_BAD_DATA;
}
if (devIndex == 1) {
*device =
reinterpret_cast<ANeuralNetworksDevice*>(NnApiHandler::kNnapiDevice);
} else {
*device = reinterpret_cast<ANeuralNetworksDevice*>(
NnApiHandler::kNnapiReferenceDevice);
}
return ANEURALNETWORKS_NO_ERROR;
};
nnapi_->ANeuralNetworksDevice_getName =
[](const ANeuralNetworksDevice* device, const char** name) -> int {
if (device ==
reinterpret_cast<ANeuralNetworksDevice*>(NnApiHandler::kNnapiDevice)) {
*name = NnApiHandler::nnapi_device_name_;
return ANEURALNETWORKS_NO_ERROR;
}
if (device == reinterpret_cast<ANeuralNetworksDevice*>(
NnApiHandler::kNnapiReferenceDevice)) {
*name = NnApiHandler::kNnapiReferenceDeviceName;
return ANEURALNETWORKS_NO_ERROR;
}
return ANEURALNETWORKS_BAD_DATA;
};
nnapi_->ANeuralNetworksDevice_getFeatureLevel =
[](const ANeuralNetworksDevice* device, int64_t* featureLevel) -> int {
if (device ==
reinterpret_cast<ANeuralNetworksDevice*>(NnApiHandler::kNnapiDevice)) {
*featureLevel = NnApiHandler::nnapi_device_feature_level_;
return ANEURALNETWORKS_NO_ERROR;
}
if (device == reinterpret_cast<ANeuralNetworksDevice*>(
NnApiHandler::kNnapiReferenceDevice)) {
*featureLevel = 1000;
return ANEURALNETWORKS_NO_ERROR;
}
return ANEURALNETWORKS_BAD_DATA;
};
}
} // namespace nnapi
} // namespace tflite
+350
View File
@@ -0,0 +1,350 @@
/* 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_NNAPI_NNAPI_HANDLER_H_
#define TENSORFLOW_LITE_NNAPI_NNAPI_HANDLER_H_
#include <cstddef>
#include <cstdint>
#include <string>
#include "absl/log/check.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/lite/nnapi/NeuralNetworksTypes.h"
#include "tensorflow/lite/nnapi/nnapi_implementation.h"
// WARNING: this header file is DEPRECATED.
// See https://developer.android.com/ndk/guides/neuralnetworks/migration-guide.
namespace tflite {
namespace nnapi {
// Offers an interface to alter the behaviour of the NNAPI instance.
// As for NNAPI, it is designed to be a singleton.
// It allows to change the behaviour of some of the methods with some stub
// implementation and then to reset the behavior to the original one using
// Reset().
//
class NnApiHandler {
public:
// No destructor defined to allow this class to be used as singleton.
// Factory method, only one instance per process/jni library.
static NnApiHandler* Instance();
// Makes the current object a transparent proxy again, resetting any
// applied changes to its methods.
void Reset();
// Using templates in the ...Returns methods because the functions need to be
// stateless and the template generated code is more readable than using a
// file-local variable in the method implementation to store the configured
// result.
template <int Value>
void GetDeviceCountReturns() {
nnapi_->ANeuralNetworks_getDeviceCount = [](uint32_t* numDevices) -> int {
*numDevices = 1;
return Value;
};
}
template <int DeviceCount>
void GetDeviceCountReturnsCount() {
nnapi_->ANeuralNetworks_getDeviceCount = [](uint32_t* numDevices) -> int {
*numDevices = DeviceCount;
return ANEURALNETWORKS_NO_ERROR;
};
}
void StubGetDeviceCountWith(int(stub)(uint32_t*)) {
nnapi_->ANeuralNetworks_getDeviceCount = stub;
}
template <int Value>
void GetDeviceReturns() {
nnapi_->ANeuralNetworks_getDevice =
[](uint32_t devIndex, ANeuralNetworksDevice** device) -> int {
*device =
reinterpret_cast<ANeuralNetworksDevice*>(NnApiHandler::kNnapiDevice);
return Value;
};
}
void StubGetDeviceWith(int(stub)(uint32_t, ANeuralNetworksDevice**)) {
nnapi_->ANeuralNetworks_getDevice = stub;
}
template <int Value>
void GetDeviceNameReturns() {
nnapi_->ANeuralNetworksDevice_getName =
[](const ANeuralNetworksDevice* device, const char** name) -> int {
*name = NnApiHandler::nnapi_device_name_;
return Value;
};
}
void GetDeviceNameReturnsName(const std::string& name);
void StubGetDeviceNameWith(int(stub)(const ANeuralNetworksDevice*,
const char**)) {
nnapi_->ANeuralNetworksDevice_getName = stub;
}
// Configure all the functions related to device browsing to support
// a device with the given name and the cpu fallback nnapi-reference.
// The extra device will return support the specified feature level
void SetNnapiSupportedDevice(const std::string& name, int feature_level = 29);
template <int Value>
void ModelCreateReturns() {
nnapi_->ANeuralNetworksModel_create = [](ANeuralNetworksModel** model) {
*model = reinterpret_cast<ANeuralNetworksModel*>(1);
return Value;
};
}
void StubModelCreateWith(int(stub)(ANeuralNetworksModel** model)) {
nnapi_->ANeuralNetworksModel_create = stub;
}
template <int Value>
void AddOperandReturns() {
nnapi_->ANeuralNetworksModel_addOperand =
[](ANeuralNetworksModel* model,
const ANeuralNetworksOperandType* type) { return Value; };
}
void StubAddOperandWith(int(stub)(ANeuralNetworksModel* model,
const ANeuralNetworksOperandType* type)) {
nnapi_->ANeuralNetworksModel_addOperand = stub;
}
template <int Value>
void SetOperandValueReturns() {
nnapi_->ANeuralNetworksModel_setOperandValue =
[](ANeuralNetworksModel* model, int32_t index, const void* buffer,
size_t length) { return Value; };
}
template <int Value>
void AddOperationReturns() {
nnapi_->ANeuralNetworksModel_addOperation =
[](ANeuralNetworksModel* model, ANeuralNetworksOperationType type,
uint32_t inputCount, const uint32_t* inputs, uint32_t outputCount,
const uint32_t* outputs) { return Value; };
}
void StubAddOperationWith(
int(stub)(ANeuralNetworksModel* model, ANeuralNetworksOperationType type,
uint32_t inputCount, const uint32_t* inputs,
uint32_t outputCount, const uint32_t* outputs)) {
nnapi_->ANeuralNetworksModel_addOperation = stub;
}
template <int Value>
void IdentifyInputAndOutputsReturns() {
nnapi_->ANeuralNetworksModel_identifyInputsAndOutputs =
[](ANeuralNetworksModel* model, uint32_t inputCount,
const uint32_t* inputs, uint32_t outputCount,
const uint32_t* outputs) { return Value; };
}
template <int Value>
void RelaxComputationFloatReturns() {
nnapi_->ANeuralNetworksModel_relaxComputationFloat32toFloat16 =
[](ANeuralNetworksModel* model, bool allow) { return Value; };
}
template <int Value>
void ModelFinishReturns() {
nnapi_->ANeuralNetworksModel_finish = [](ANeuralNetworksModel* model) {
return Value;
};
}
template <int Value>
void MemoryCreateFromFdReturns() {
nnapi_->ANeuralNetworksMemory_createFromFd =
[](size_t size, int protect, int fd, size_t offset,
ANeuralNetworksMemory** memory) {
*memory = reinterpret_cast<ANeuralNetworksMemory*>(2);
return Value;
};
}
template <int Value>
void CompilationCreateReturns() {
nnapi_->ANeuralNetworksCompilation_create =
[](ANeuralNetworksModel* model,
ANeuralNetworksCompilation** compilation) {
*compilation = reinterpret_cast<ANeuralNetworksCompilation*>(3);
return Value;
};
}
template <int Value>
void CompilationCreateForDevicesReturns() {
nnapi_->ANeuralNetworksCompilation_createForDevices =
[](ANeuralNetworksModel* model,
const ANeuralNetworksDevice* const* devices, uint32_t numDevices,
ANeuralNetworksCompilation** compilation) {
*compilation = reinterpret_cast<ANeuralNetworksCompilation*>(3);
return Value;
};
}
void StubCompilationCreateForDevicesWith(int(stub)(
ANeuralNetworksModel* model, const ANeuralNetworksDevice* const* devices,
uint32_t numDevices, ANeuralNetworksCompilation** compilation)) {
nnapi_->ANeuralNetworksCompilation_createForDevices = stub;
}
template <int Value>
void CompilationFinishReturns() {
nnapi_->ANeuralNetworksCompilation_finish =
[](ANeuralNetworksCompilation* compilation) { return Value; };
}
template <int Value>
void ExecutionCreateReturns() {
nnapi_->ANeuralNetworksExecution_create =
[](ANeuralNetworksCompilation* compilation,
ANeuralNetworksExecution** execution) {
if (compilation == nullptr) return 1;
*execution = reinterpret_cast<ANeuralNetworksExecution*>(4);
return Value;
};
}
template <int Value>
void ExecutionSetInputFromMemoryReturns() {
nnapi_->ANeuralNetworksExecution_setInputFromMemory =
[](ANeuralNetworksExecution* execution, int32_t index,
const ANeuralNetworksOperandType* type,
const ANeuralNetworksMemory* memory, size_t offset,
size_t length) { return Value; };
}
template <int Value>
void ExecutionSetOutputFromMemoryReturns() {
nnapi_->ANeuralNetworksExecution_setOutputFromMemory =
[](ANeuralNetworksExecution* execution, int32_t index,
const ANeuralNetworksOperandType* type,
const ANeuralNetworksMemory* memory, size_t offset,
size_t length) { return Value; };
}
template <int Value>
void ExecutionComputeReturns() {
nnapi_->ANeuralNetworksExecution_compute =
[](ANeuralNetworksExecution* execution) { return Value; };
}
template <int Value>
void GetSupportedOperationsForDevicesReturns() {
nnapi_->ANeuralNetworksModel_getSupportedOperationsForDevices =
[](const ANeuralNetworksModel* model,
const ANeuralNetworksDevice* const* devices, uint32_t numDevices,
bool* supportedOps) { return Value; };
}
void StubGetSupportedOperationsForDevicesWith(
int(stub)(const ANeuralNetworksModel* model,
const ANeuralNetworksDevice* const* devices,
uint32_t numDevices, bool* supportedOps)) {
nnapi_->ANeuralNetworksModel_getSupportedOperationsForDevices = stub;
}
template <int Value>
void ExecutionStartComputeReturns() {
nnapi_->ANeuralNetworksExecution_startCompute =
[](ANeuralNetworksExecution* execution, ANeuralNetworksEvent** event) {
*event = reinterpret_cast<ANeuralNetworksEvent*>(1);
return Value;
};
}
template <int Value>
void EventWaitReturns() {
nnapi_->ANeuralNetworksEvent_wait = [](ANeuralNetworksEvent* event) {
return Value;
};
}
template <int Value>
void SetPriorityReturns() {
nnapi_->ANeuralNetworksCompilation_setPriority =
[](ANeuralNetworksCompilation* compilation, int priority) -> int {
return Value;
};
}
template <int Value>
void SetOperandSymmPerChannelQuantParamsReturns() {
nnapi_->ANeuralNetworksModel_setOperandSymmPerChannelQuantParams =
[](ANeuralNetworksModel* model, int32_t index,
const ANeuralNetworksSymmPerChannelQuantParams* channelQuant) {
return Value;
};
}
/*
* Sets the SDK Version in the nnapi structure.
* If set_unsupported_ops_to_null is set to true, all the functions not
* available at the given sdk level will be set to null too.
*/
void SetAndroidSdkVersion(int version,
bool set_unsupported_ops_to_null = false);
const NnApi* GetNnApi() { return nnapi_; }
protected:
explicit NnApiHandler(NnApi* nnapi) : nnapi_(nnapi) { DCHECK(nnapi); }
NnApi* nnapi_;
static const char kNnapiReferenceDeviceName[];
static const int kNnapiReferenceDevice;
static const int kNnapiDevice;
static void SetDeviceName(const std::string& name);
private:
static char* nnapi_device_name_;
static int nnapi_device_feature_level_;
};
// Returns a pointer to an unaltered instance of NNAPI. Is intended
// to be used by stub methods when wanting to pass-through to original
// implementation for example:
//
// NnApiTestUtility()->StubGetDeviceWith(
// [](uint32_t devIndex, ANeuralNetworksDevice** device) -> int {
// static int count = 0;
// if (count++ < 1) {
// NnApiPassthroughInstance()->ANeuralNetworks_getDevice(
// devIndex, device);
// } else {
// return ANEURALNETWORKS_BAD_DATA;
// }
// });
const NnApi* NnApiPassthroughInstance();
// Returns an instance of NnApiProxy that can be used to alter
// the behaviour of the TFLite wide instance of NnApi.
NnApiHandler* NnApiProxyInstance();
} // namespace nnapi
} // namespace tflite
#endif // TENSORFLOW_LITE_NNAPI_NNAPI_HANDLER_H_
+229
View File
@@ -0,0 +1,229 @@
/* 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_handler.h"
#include <cstdint>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "tensorflow/lite/nnapi/nnapi_implementation.h"
namespace tflite {
namespace nnapi {
using testing::Eq;
using testing::Ne;
using testing::NotNull;
void ExpectEquals(const NnApi& left, const NnApi& right);
class NnApiHandlerTest : public ::testing::Test {
protected:
~NnApiHandlerTest() override { NnApiHandler::Instance()->Reset(); }
};
TEST_F(NnApiHandlerTest, ShouldAlterNnApiInstanceBehaviour) {
const NnApi* nnapi = NnApiImplementation();
const auto device_count_stub = [](uint32_t* device_count) -> int {
*device_count = 999;
return ANEURALNETWORKS_NO_ERROR;
};
NnApiHandler::Instance()->StubGetDeviceCountWith(device_count_stub);
ASSERT_THAT(nnapi->ANeuralNetworks_getDeviceCount, NotNull());
uint32_t device_count = 0;
nnapi->ANeuralNetworks_getDeviceCount(&device_count);
EXPECT_THAT(device_count, Eq(999));
}
TEST_F(NnApiHandlerTest, ShouldRestoreNnApiToItsOriginalValueWithReset) {
NnApi nnapi_orig_copy = *NnApiImplementation();
auto device_count_override = [](uint32_t* device_count) -> int {
*device_count = 777;
return ANEURALNETWORKS_NO_ERROR;
};
NnApiHandler::Instance()->StubGetDeviceCountWith(device_count_override);
EXPECT_THAT(nnapi_orig_copy.ANeuralNetworks_getDeviceCount,
Ne(NnApiImplementation()->ANeuralNetworks_getDeviceCount));
NnApiHandler::Instance()->Reset();
ExpectEquals(nnapi_orig_copy, *NnApiImplementation());
}
int (*device_count_ptr)(uint32_t*);
TEST_F(NnApiHandlerTest, ShouldSupportPassthroughCalls) {
const NnApi* nnapi = NnApiImplementation();
device_count_ptr = nnapi->ANeuralNetworks_getDeviceCount;
NnApiHandler::Instance()->StubGetDeviceCountWith(
[](uint32_t* device_count) -> int {
return NnApiPassthroughInstance()->ANeuralNetworks_getDeviceCount ==
device_count_ptr;
});
uint32_t device_count = 0;
EXPECT_THAT(nnapi->ANeuralNetworks_getDeviceCount(&device_count), Eq(1));
}
TEST_F(NnApiHandlerTest, ShouldSetNnApiMembersToNullAsPerSdkVersion_NNAPI11) {
auto* handler = NnApiHandler::Instance();
// Setting non null values for nnapi functions
handler->SetNnapiSupportedDevice("devvice", 1000);
handler->GetSupportedOperationsForDevicesReturns<1>();
handler->CompilationCreateForDevicesReturns<1>();
handler->ExecutionComputeReturns<1>();
handler->MemoryCreateFromFdReturns<1>();
handler->SetAndroidSdkVersion(28, /*set_unsupported_ops_to_null=*/true);
const NnApi* nnapi = NnApiImplementation();
using ::testing::IsNull;
EXPECT_THAT(nnapi->ANeuralNetworks_getDeviceCount, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworks_getDevice, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksDevice_getName, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksDevice_getVersion, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksDevice_getFeatureLevel, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksDevice_getType, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksModel_getSupportedOperationsForDevices,
IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksCompilation_createForDevices, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksCompilation_setCaching, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksExecution_compute, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksExecution_getOutputOperandRank, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksExecution_getOutputOperandDimensions,
IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksBurst_create, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksBurst_free, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksExecution_burstCompute, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksMemory_createFromAHardwareBuffer, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksExecution_setMeasureTiming, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksExecution_getDuration, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksDevice_getExtensionSupport, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksModel_getExtensionOperandType, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksModel_getExtensionOperationType, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksModel_setOperandExtensionData, IsNull());
}
TEST_F(NnApiHandlerTest, ShouldSetNnApiMembersToNullAsPerSdkVersion_NNAPI10) {
auto* handler = NnApiHandler::Instance();
// Setting non null values for nnapi functions
handler->SetNnapiSupportedDevice("devvice", 1000);
handler->GetSupportedOperationsForDevicesReturns<1>();
handler->CompilationCreateForDevicesReturns<1>();
handler->ExecutionComputeReturns<1>();
handler->MemoryCreateFromFdReturns<1>();
handler->SetAndroidSdkVersion(27, /*set_unsupported_ops_to_null=*/true);
const NnApi* nnapi = NnApiImplementation();
using ::testing::IsNull;
EXPECT_THAT(nnapi->ANeuralNetworks_getDeviceCount, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworks_getDevice, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksDevice_getName, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksDevice_getVersion, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksDevice_getFeatureLevel, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksDevice_getType, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksModel_getSupportedOperationsForDevices,
IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksCompilation_createForDevices, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksCompilation_setCaching, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksExecution_compute, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksExecution_getOutputOperandRank, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksExecution_getOutputOperandDimensions,
IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksBurst_create, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksBurst_free, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksExecution_burstCompute, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksMemory_createFromAHardwareBuffer, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksExecution_setMeasureTiming, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksExecution_getDuration, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksDevice_getExtensionSupport, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksModel_getExtensionOperandType, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksModel_getExtensionOperationType, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksModel_setOperandExtensionData, IsNull());
EXPECT_THAT(nnapi->ANeuralNetworksModel_relaxComputationFloat32toFloat16,
IsNull());
}
void ExpectEquals(const NnApi& left, const NnApi& right) {
#define EXPECT_NNAPI_MEMBER_EQ(name) EXPECT_EQ(left.name, right.name)
EXPECT_NNAPI_MEMBER_EQ(nnapi_exists);
EXPECT_NNAPI_MEMBER_EQ(android_sdk_version);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksMemory_createFromFd);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksMemory_free);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksModel_create);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksModel_free);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksModel_finish);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksModel_addOperand);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksModel_setOperandValue);
EXPECT_NNAPI_MEMBER_EQ(
ANeuralNetworksModel_setOperandSymmPerChannelQuantParams);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksModel_setOperandValueFromMemory);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksModel_addOperation);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksModel_identifyInputsAndOutputs);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksModel_relaxComputationFloat32toFloat16);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksCompilation_create);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksCompilation_free);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksCompilation_setPreference);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksCompilation_finish);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_create);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_free);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_setInput);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_setInputFromMemory);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_setOutput);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_setOutputFromMemory);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_startCompute);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksEvent_wait);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksEvent_free);
EXPECT_NNAPI_MEMBER_EQ(ASharedMemory_create);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworks_getDeviceCount);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworks_getDevice);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksDevice_getName);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksDevice_getVersion);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksDevice_getFeatureLevel);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksDevice_getType);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksModel_getSupportedOperationsForDevices);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksCompilation_createForDevices);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksCompilation_setCaching);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_compute);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_getOutputOperandRank);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_getOutputOperandDimensions);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksBurst_create);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksBurst_free);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_burstCompute);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksMemory_createFromAHardwareBuffer);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_setMeasureTiming);
EXPECT_NNAPI_MEMBER_EQ(ANeuralNetworksExecution_getDuration);
#undef EXPECT_NNAPI_MEMBER_EQ
}
} // namespace nnapi
} // namespace tflite
@@ -0,0 +1,603 @@
/* Copyright 2017 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"
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <algorithm>
#include <cstdlib>
#include <memory>
#include "tensorflow/lite/nnapi/sl/public/NeuralNetworksSupportLibraryImpl.h"
#ifdef __ANDROID__
#include <sys/system_properties.h>
#endif // __ANDROID__
#define EXPAND_VA_ARGS(...) , ##__VA_ARGS__
#define NNAPI_LOG(format, ...) \
fprintf(stderr, format "\n" EXPAND_VA_ARGS(__VA_ARGS__));
namespace {
#ifdef __ANDROID__
// See frameworks/base/core/java/android/os/Process.java in AOSP.
const int kFirstIsolatedUid = 99000;
const int kLastIsolatedUid = 99999;
const int kFirstAppZygoteIsolatedUid = 90000;
const int kLastAppZygoteIsolatedUid = 98999;
bool IsIsolatedProcess() {
int uid = getuid();
return (uid >= kFirstIsolatedUid && uid <= kLastIsolatedUid) ||
(uid >= kFirstAppZygoteIsolatedUid &&
uid <= kLastAppZygoteIsolatedUid);
}
int32_t GetAndroidSdkVersion() {
const char* sdkProp = "ro.build.version.sdk";
char sdkVersion[PROP_VALUE_MAX];
int length = __system_property_get(sdkProp, sdkVersion);
if (length != 0) {
int32_t result = 0;
for (int i = 0; i < length; ++i) {
int digit = sdkVersion[i] - '0';
if (digit < 0 || digit > 9) {
// Non-numeric SDK version, assume it's higher than expected;
return 0xffff;
}
result = result * 10 + digit;
}
return result;
}
return 0;
}
#endif // __ANDROID__
void* LoadFunction(void* handle, const char* name, bool optional) {
if (handle == nullptr) {
return nullptr;
}
void* fn = dlsym(handle, name);
if (fn == nullptr && !optional) {
NNAPI_LOG("nnapi error: unable to open function %s", 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) {
// Each call to ASharedMemory_create produces a unique memory space, hence
// name should be unique, otherwise two calls to create memory regions using
// the same 'name', will collide.
// Caller is responsible to provide a unique name.
// Make sure new shared memory region is created: shm_open return an error if
// shm object with given name already exists (O_CREAT | O_EXCL)
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd < 0) {
return fd;
}
int result = ftruncate(fd, size);
if (result < 0) {
close(fd);
return -1;
}
return fd;
}
// Determine the NnApi version from loaded entry points
uint32_t CalculateAndroidSdkVersion(NnApi const& nnapi) {
// Test for specific NNAPI 1.0, 1.1, 1.2 and 1.3 functions
bool has_10 = nnapi.ANeuralNetworksMemory_createFromFd != nullptr;
bool has_11 =
nnapi.ANeuralNetworksModel_relaxComputationFloat32toFloat16 != nullptr;
bool has_12 = nnapi.ANeuralNetworks_getDeviceCount != nullptr;
bool has_13 = nnapi.ANeuralNetworksCompilation_setTimeout != nullptr;
bool has_14 = nnapi.ANeuralNetworks_getRuntimeFeatureLevel != nullptr;
uint32_t sdk_version = 0;
if (has_10) {
sdk_version = 27;
}
if (sdk_version == 27 && has_11) {
sdk_version = 28;
}
if (sdk_version == 28 && has_12) {
sdk_version = 29;
}
if (sdk_version == 29 && has_13) {
sdk_version = 30;
}
if (sdk_version == 30 && has_14) {
sdk_version = 31;
}
return sdk_version;
}
#else
ASharedMemory_create_fn getASharedMemory_create() {
// ASharedMemory_create has different implementations in Android depending on
// the partition. Generally it can be loaded from libandroid.so but in vendor
// partition (e.g. if a HAL wants to use NNAPI) it is only accessible through
// libcutils.
void* libandroid = nullptr;
libandroid = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
if (libandroid != nullptr) {
return reinterpret_cast<ASharedMemory_create_fn>(
LoadFunction(libandroid, "ASharedMemory_create", false));
}
std::string libandroid_error = dlerror();
void* cutils_handle = dlopen("libcutils.so", RTLD_LAZY | RTLD_LOCAL);
if (cutils_handle != nullptr) {
return reinterpret_cast<ASharedMemory_create_fn>(
LoadFunction(cutils_handle, "ashmem_create_region", false));
}
NNAPI_LOG(
"nnapi error: unable to open both library %s (%s) and library %s "
"(%s)",
"libandroid.so", libandroid_error.c_str(), "libcutils.so", dlerror());
return nullptr;
}
#endif // __ANDROID__
#define LOAD_FUNCTION(handle, name) \
nnapi.name = reinterpret_cast<name##_fn>( \
LoadFunction(handle, #name, /*optional*/ false));
#define LOAD_FUNCTION_OPTIONAL(handle, name) \
nnapi.name = reinterpret_cast<name##_fn>( \
LoadFunction(handle, #name, /*optional*/ true));
#define LOAD_FUNCTION_RENAME(handle, name, symbol) \
nnapi.name = reinterpret_cast<name##_fn>( \
LoadFunction(handle, symbol, /*optional*/ false));
const NnApi LoadNnApi() {
NnApi nnapi = {};
nnapi.android_sdk_version = 0;
#ifdef __ANDROID__
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);
nnapi.nnapi_exists = false;
return nnapi;
}
// Disable NNAPI for Android 13 and earlier if running in isolated process.
if (nnapi.android_sdk_version <= 33 && IsIsolatedProcess()) {
NNAPI_LOG("NNAPI is disabled in an isolated process");
nnapi.nnapi_exists = false;
return nnapi;
}
#endif // __ANDROID__
void* libneuralnetworks = nullptr;
// TODO(b/123243014): change RTLD_LOCAL? Assumes there can be multiple
// instances of nn api RT
static const char nnapi_library_name[] = "libneuralnetworks.so";
libneuralnetworks = dlopen(nnapi_library_name, RTLD_LAZY | RTLD_LOCAL);
#ifdef __ANDROID__
// Note: If there is an problem trying to open the NNAPI library on a
// non-Android system, the error message is suppressed. This is to avoid
// showing confusing errors when running in environments that do not support
// NNAPI. As more platforms support NNAPI, the #ifdef logic above can be
// expanded.
if (libneuralnetworks == nullptr) {
const char* error = dlerror();
if (error) {
NNAPI_LOG("%s\n", error);
}
NNAPI_LOG("nnapi error: unable to open library %s", nnapi_library_name);
}
#endif // __ANDROID__
nnapi.nnapi_exists = libneuralnetworks != nullptr;
// API 27 (NN 1.0) methods.
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksMemory_createFromFd);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksMemory_free);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksModel_create);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksModel_free);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksModel_finish);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksModel_addOperand);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksModel_setOperandValue);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
ANeuralNetworksModel_setOperandSymmPerChannelQuantParams);
LOAD_FUNCTION(libneuralnetworks,
ANeuralNetworksModel_setOperandValueFromMemory);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksModel_addOperation);
LOAD_FUNCTION(libneuralnetworks,
ANeuralNetworksModel_identifyInputsAndOutputs);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksCompilation_create);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksCompilation_free);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksCompilation_setPreference);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksCompilation_finish);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksExecution_create);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksExecution_free);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksExecution_setInput);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksExecution_setInputFromMemory);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksExecution_setOutput);
LOAD_FUNCTION(libneuralnetworks,
ANeuralNetworksExecution_setOutputFromMemory);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksExecution_startCompute);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksEvent_wait);
LOAD_FUNCTION(libneuralnetworks, ANeuralNetworksEvent_free);
#ifdef __ANDROID__
nnapi.ASharedMemory_create = getASharedMemory_create();
#else
// Mock ASharedMemory_create only if libneuralnetworks.so was successfully
// loaded. This ensures identical behaviour on platforms which use this
// implementation, but don't have libneuralnetworks.so library, and
// platforms which use nnapi_implementation_disabled.cc stub.
if (libneuralnetworks != nullptr) {
nnapi.ASharedMemory_create = ASharedMemory_create;
}
#endif // __ANDROID__
// API 28 (NN 1.1) methods.
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksModel_relaxComputationFloat32toFloat16);
// API 29 (NN 1.2) methods.
LOAD_FUNCTION_OPTIONAL(libneuralnetworks, ANeuralNetworks_getDeviceCount);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks, ANeuralNetworks_getDevice);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks, ANeuralNetworksDevice_getName);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks, ANeuralNetworksDevice_getVersion);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksDevice_getFeatureLevel);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks, ANeuralNetworksDevice_getType);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksModel_getSupportedOperationsForDevices);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksCompilation_createForDevices);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksCompilation_setCaching);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks, ANeuralNetworksExecution_compute);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksExecution_getOutputOperandRank);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksExecution_getOutputOperandDimensions);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks, ANeuralNetworksBurst_create);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks, ANeuralNetworksBurst_free);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksExecution_burstCompute);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksMemory_createFromAHardwareBuffer);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksExecution_setMeasureTiming);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksExecution_getDuration);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksDevice_getExtensionSupport);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksModel_getExtensionOperandType);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksModel_getExtensionOperationType);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksModel_setOperandExtensionData);
// API 30 (NNAPI 1.3) methods.
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksCompilation_setTimeout);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksCompilation_setPriority);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksExecution_setTimeout);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksExecution_setLoopTimeout);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks, ANeuralNetworksMemoryDesc_create);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks, ANeuralNetworksMemoryDesc_free);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksMemoryDesc_addInputRole);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksMemoryDesc_addOutputRole);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksMemoryDesc_setDimensions);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks, ANeuralNetworksMemoryDesc_finish);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksMemory_createFromDesc);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks, ANeuralNetworksMemory_copy);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksEvent_createFromSyncFenceFd);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksEvent_getSyncFenceFd);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksExecution_startComputeWithDependencies);
// API 31 methods
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworks_getRuntimeFeatureLevel);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksExecution_enableInputAndOutputPadding);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
ANeuralNetworksExecution_setReusable);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticCompilationInfo_getSessionId);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticCompilationInfo_getNnApiVersion);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticCompilationInfo_getModelArchHash);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticCompilationInfo_getDeviceIds);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticCompilationInfo_getErrorCode);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticCompilationInfo_getInputDataClass);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticCompilationInfo_getOutputDataClass);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticCompilationInfo_getCompilationTimeNanos);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticCompilationInfo_isCachingEnabled);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticCompilationInfo_isControlFlowUsed);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticCompilationInfo_areDynamicTensorsUsed);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_getSessionId);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_getNnApiVersion);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_getModelArchHash);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_getDeviceIds);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_getExecutionMode);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_getInputDataClass);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_getOutputDataClass);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_getErrorCode);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_getRuntimeExecutionTimeNanos);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_getDriverExecutionTimeNanos);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_getHardwareExecutionTimeNanos);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_isCachingEnabled);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_isControlFlowUsed);
LOAD_FUNCTION_OPTIONAL(
libneuralnetworks,
SL_ANeuralNetworksDiagnosticExecutionInfo_areDynamicTensorsUsed);
LOAD_FUNCTION_OPTIONAL(libneuralnetworks,
SL_ANeuralNetworksDiagnostic_registerCallbacks);
#ifndef __ANDROID__
// If libneuralnetworks.so is loaded, but android_sdk_version is not set,
// then determine android_sdk_version by testing which functions are
// available.
if (nnapi.nnapi_exists && nnapi.android_sdk_version == 0) {
nnapi.android_sdk_version = CalculateAndroidSdkVersion(nnapi);
}
#endif // __ANDROID__
// Determin NNAPI Runtime feature level.
if (nnapi.ANeuralNetworks_getRuntimeFeatureLevel) {
nnapi.nnapi_runtime_feature_level =
nnapi.ANeuralNetworks_getRuntimeFeatureLevel();
} else {
nnapi.nnapi_runtime_feature_level = nnapi.android_sdk_version;
}
return nnapi;
}
} // namespace
std::unique_ptr<const NnApi> CreateNnApiFromSupportLibrary(
const NnApiSLDriverImplFL5* nnapi_support_library_driver) {
auto nnapi = std::make_unique<NnApi>();
nnapi->nnapi_exists = true;
nnapi->android_sdk_version = ANEURALNETWORKS_FEATURE_LEVEL_5;
nnapi->nnapi_runtime_feature_level =
nnapi_support_library_driver->base.implFeatureLevel;
#define ASSIGN_SL_FUNCTION_TO_NNAPI(name) \
nnapi->name = nnapi_support_library_driver->name;
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksMemory_createFromFd);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksMemory_free);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksModel_create);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksModel_free);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksModel_finish);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksModel_addOperand);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksModel_setOperandValue);
ASSIGN_SL_FUNCTION_TO_NNAPI(
ANeuralNetworksModel_setOperandSymmPerChannelQuantParams);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksModel_setOperandValueFromMemory);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksModel_addOperation);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksModel_identifyInputsAndOutputs);
ASSIGN_SL_FUNCTION_TO_NNAPI(
ANeuralNetworksModel_relaxComputationFloat32toFloat16);
// ANeuralNetworksCompilation_create is not available in the support library
// because its clients are expected to know which accelerator they want to
// use. ANeuralNetworksCompilation_createForDevices is available to create
// compilation for specified devices.
nnapi->ANeuralNetworksCompilation_create = nullptr;
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksCompilation_free);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksCompilation_setPreference);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksCompilation_finish);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_create);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_free);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_setInput);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_setInputFromMemory);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_setOutput);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_setOutputFromMemory);
// Support library doesn't support regular asynchronous execution.
nnapi->ANeuralNetworksExecution_startCompute = nullptr;
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksEvent_wait);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksEvent_free);
#ifdef __ANDROID__
nnapi->ASharedMemory_create = getASharedMemory_create();
#else
nnapi->ASharedMemory_create = ASharedMemory_create;
#endif // __ANDROID__
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworks_getDeviceCount);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworks_getDevice);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksDevice_getName);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksDevice_getVersion);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksDevice_getFeatureLevel);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksDevice_getType);
ASSIGN_SL_FUNCTION_TO_NNAPI(
ANeuralNetworksModel_getSupportedOperationsForDevices);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksCompilation_createForDevices);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksCompilation_setCaching);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksCompilation_setTimeout);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksCompilation_setPriority);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_compute);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_setTimeout);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_setLoopTimeout);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_getOutputOperandRank);
ASSIGN_SL_FUNCTION_TO_NNAPI(
ANeuralNetworksExecution_getOutputOperandDimensions);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksBurst_create);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksBurst_free);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_burstCompute);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksMemory_createFromAHardwareBuffer);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_setMeasureTiming);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_getDuration);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksDevice_getExtensionSupport);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksModel_getExtensionOperandType);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksModel_getExtensionOperationType);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksModel_setOperandExtensionData);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksMemoryDesc_create);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksMemoryDesc_free);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksMemoryDesc_addInputRole);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksMemoryDesc_addOutputRole);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksMemoryDesc_setDimensions);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksMemoryDesc_finish);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksMemory_createFromDesc);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksMemory_copy);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksEvent_createFromSyncFenceFd);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksEvent_getSyncFenceFd);
ASSIGN_SL_FUNCTION_TO_NNAPI(
ANeuralNetworksExecution_startComputeWithDependencies);
ASSIGN_SL_FUNCTION_TO_NNAPI(
ANeuralNetworksExecution_enableInputAndOutputPadding);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworksExecution_setReusable);
ASSIGN_SL_FUNCTION_TO_NNAPI(ANeuralNetworks_getRuntimeFeatureLevel);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticCompilationInfo_getSessionId);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticCompilationInfo_getNnApiVersion);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticCompilationInfo_getModelArchHash);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticCompilationInfo_getDeviceIds);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticCompilationInfo_getErrorCode);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticCompilationInfo_getInputDataClass);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticCompilationInfo_getOutputDataClass);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticCompilationInfo_getCompilationTimeNanos);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticCompilationInfo_isCachingEnabled);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticCompilationInfo_isControlFlowUsed);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticCompilationInfo_areDynamicTensorsUsed);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_getSessionId);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_getNnApiVersion);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_getModelArchHash);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_getDeviceIds);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_getExecutionMode);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_getInputDataClass);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_getOutputDataClass);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_getErrorCode);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_getRuntimeExecutionTimeNanos);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_getDriverExecutionTimeNanos);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_getHardwareExecutionTimeNanos);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_isCachingEnabled);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_isControlFlowUsed);
ASSIGN_SL_FUNCTION_TO_NNAPI(
SL_ANeuralNetworksDiagnosticExecutionInfo_areDynamicTensorsUsed);
ASSIGN_SL_FUNCTION_TO_NNAPI(SL_ANeuralNetworksDiagnostic_registerCallbacks);
// There are several functions that are defined in the SL but are not yet used
// in the delegate:
// * ANeuralNetworksDevice_wait
// * ANeuralNetworksModel_setOperandValueFromModel
// * ANeuralNetworks_getDefaultLoopTimeout
// * ANeuralNetworks_getMaximumLoopTimeout
return nnapi;
}
const NnApi* NnApiImplementation() {
static const NnApi nnapi = LoadNnApi();
return &nnapi;
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,31 @@
/* 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 <memory>
#include "tensorflow/lite/nnapi/nnapi_implementation.h"
// Stub NNAPI implementation for platforms that don't support NNAPI.
const NnApi* NnApiImplementation() {
static const NnApi nnapi = {};
return &nnapi;
}
std::unique_ptr<const NnApi> CreateNnApiFromSupportLibrary(
const NnApiSLDriverImplFL5* /*nnapi_support_library_driver*/) {
std::unique_ptr<NnApi> nnapi = std::make_unique<NnApi>();
*nnapi = *NnApiImplementation();
return nnapi;
}
@@ -0,0 +1,142 @@
/* Copyright 2018 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"
#include <gtest/gtest.h>
namespace {
TEST(NnapiLibTest, NnApiImplementation) {
const NnApi* nnapi = NnApiImplementation();
EXPECT_NE(nnapi, nullptr);
#ifdef __ANDROID__
EXPECT_GT(nnapi->android_sdk_version, 0);
if (nnapi.android_sdk_version < 27) {
EXPECT_FALSE(nnapi->nnapi_exists);
EXPECT_EQ(nnapi->ANeuralNetworksMemory_createFromFd, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksMemory_free, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_create, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_free, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_finish, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_addOperand, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_setOperandValue, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_setOperandValueFromMemory, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_addOperation, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_identifyInputsAndOutputs, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_relaxComputationFloat32toFloat16,
nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksCompilation_create, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksCompilation_free, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksCompilation_setPreference, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksCompilation_finish, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_create, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_free, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_setInput, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_setInputFromMemory, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_setOutput, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_setOutputFromMemory, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_startCompute, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksEvent_wait, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksEvent_free, nullptr);
EXPECT_EQ(nnapi->ASharedMemory_create, nullptr);
} else {
EXPECT_TRUE(nnapi->nnapi_exists);
EXPECT_NE(nnapi->ANeuralNetworksMemory_createFromFd, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksMemory_free, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksModel_create, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksModel_free, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksModel_finish, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksModel_addOperand, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksModel_setOperandValue, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksModel_setOperandValueFromMemory, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksModel_addOperation, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksModel_identifyInputsAndOutputs, nullptr);
if (nnapi->android_sdk_version >= 28) {
// relaxComputationFloat32toFloat16 only available with Android 9.0 (P).
EXPECT_NE(nnapi->ANeuralNetworksModel_relaxComputationFloat32toFloat16,
nullptr);
} else {
EXPECT_EQ(nnapi->ANeuralNetworksModel_relaxComputationFloat32toFloat16,
nullptr);
}
EXPECT_NE(nnapi->ANeuralNetworksCompilation_create, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksCompilation_free, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksCompilation_setPreference, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksCompilation_finish, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksExecution_create, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksExecution_free, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksExecution_setInput, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksExecution_setInputFromMemory, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksExecution_setOutput, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksExecution_setOutputFromMemory, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksExecution_startCompute, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksEvent_wait, nullptr);
EXPECT_NE(nnapi->ANeuralNetworksEvent_free, nullptr);
EXPECT_NE(nnapi->ASharedMemory_create, nullptr);
// TODO(b/123423795): Test Q-specific APIs after release.
}
#else
EXPECT_FALSE(nnapi->nnapi_exists);
EXPECT_EQ(nnapi->android_sdk_version, 0);
EXPECT_EQ(nnapi->ANeuralNetworksMemory_createFromFd, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksMemory_free, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_create, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_free, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_finish, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_addOperand, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_setOperandValue, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_setOperandSymmPerChannelQuantParams,
nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_setOperandValueFromMemory, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_addOperation, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_identifyInputsAndOutputs, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_relaxComputationFloat32toFloat16,
nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksCompilation_create, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksCompilation_free, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksCompilation_setPreference, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksCompilation_finish, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_create, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_free, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_setInput, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_setInputFromMemory, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_setOutput, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_setOutputFromMemory, nullptr);
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_EQ(nnapi->ANeuralNetworks_getDeviceCount, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworks_getDevice, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksDevice_getName, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksDevice_getVersion, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksDevice_getFeatureLevel, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksModel_getSupportedOperationsForDevices,
nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksCompilation_createForDevices, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksCompilation_setCaching, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_compute, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_getOutputOperandRank, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_getOutputOperandDimensions,
nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksBurst_create, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksBurst_free, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_burstCompute, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksMemory_createFromAHardwareBuffer, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_setMeasureTiming, nullptr);
EXPECT_EQ(nnapi->ANeuralNetworksExecution_getDuration, nullptr);
#endif
}
} // namespace
+82
View File
@@ -0,0 +1,82 @@
/* 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_util.h"
#include <cstdint>
#include <string>
#include <vector>
#include "tensorflow/lite/nnapi/nnapi_implementation.h"
#include "tensorflow/lite/util.h"
namespace tflite {
namespace nnapi {
namespace {
std::string SimpleJoin(const std::vector<const char*>& elements,
const char* separator) {
// Note that we avoid use of sstream to avoid binary size bloat.
std::string joined_elements;
for (auto it = elements.begin(); it != elements.end(); ++it) {
if (separator && it != elements.begin()) {
joined_elements += separator;
}
if (*it) {
joined_elements += *it;
}
}
return joined_elements;
}
} // namespace
std::vector<const char*> GetDeviceNamesList() {
return GetDeviceNamesList(NnApiImplementation());
}
std::vector<const char*> GetDeviceNamesList(const NnApi* nnapi) {
std::vector<const char*> device_names;
// Only build the list if NnApiImplementation has the methods we need,
// leaving it empty otherwise.
if (nnapi->ANeuralNetworks_getDeviceCount != nullptr) {
uint32_t num_devices = 0;
nnapi->ANeuralNetworks_getDeviceCount(&num_devices);
for (uint32_t i = 0; i < num_devices; i++) {
ANeuralNetworksDevice* device = nullptr;
const char* buffer = nullptr;
nnapi->ANeuralNetworks_getDevice(i, &device);
nnapi->ANeuralNetworksDevice_getName(device, &buffer);
device_names.push_back(buffer);
}
}
return device_names;
}
std::string GetStringDeviceNamesList() {
return GetStringDeviceNamesList(NnApiImplementation());
}
std::string GetStringDeviceNamesList(const NnApi* nnapi) {
std::vector<const char*> device_names = GetDeviceNamesList(nnapi);
return SimpleJoin(device_names, ",");
}
} // namespace nnapi
} // namespace tflite
+50
View File
@@ -0,0 +1,50 @@
/* 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.
==============================================================================*/
// This file provides general C++ utility functions for interacting with NNAPI.
#ifndef TENSORFLOW_LITE_NNAPI_NNAPI_UTIL_H_
#define TENSORFLOW_LITE_NNAPI_NNAPI_UTIL_H_
#include <string>
#include <vector>
#include "tensorflow/lite/nnapi/nnapi_implementation.h"
// WARNING: this header file is DEPRECATED.
// See https://developer.android.com/ndk/guides/neuralnetworks/migration-guide.
namespace tflite {
namespace nnapi {
// Return std::vector consisting of pointers to null-terminated device names.
// These names are guaranteed to be valid for the lifetime of the application.
std::vector<const char*> GetDeviceNamesList();
// An overload that uses a client-provided NnApi* structure to request available
// devices instead of the static one provided by NnApiImplementation().
// The names are guaranteed to be valid for the lifetime of the application.
std::vector<const char*> GetDeviceNamesList(const NnApi* nnapi);
// Return a string containing the names of all available devices.
// Will take the format: "DeviceA,DeviceB,DeviceC"
std::string GetStringDeviceNamesList();
// An overload that uses a client-provided NnApi* structure to request available
// devices instead of the static one provided by NnApiImplementation().
std::string GetStringDeviceNamesList(const NnApi* nnapi);
} // namespace nnapi
} // namespace tflite
#endif // TENSORFLOW_LITE_NNAPI_NNAPI_UTIL_H_
+42
View File
@@ -0,0 +1,42 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable")
load("//tensorflow/lite:build_def.bzl", "tflite_copts", "tflite_copts_warnings")
load("//tensorflow/lite:special_rules.bzl", "nnapi_sl_visibility_allowlist")
_DEFAULT_VISIBILITY = ["//tensorflow/lite:__subpackages__"]
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
default_visibility = _DEFAULT_VISIBILITY,
licenses = ["notice"],
)
cc_library(
name = "nnapi_support_library_headers",
hdrs = [
"include/SupportLibrary.h",
"include/SupportLibrarySymbols.h",
"public/NeuralNetworksSupportLibraryImpl.h",
],
compatible_with = get_compatible_with_portable(),
visibility = _DEFAULT_VISIBILITY + nnapi_sl_visibility_allowlist(),
deps = [
"//tensorflow/lite/kernels/internal:compatibility",
"//tensorflow/lite/nnapi:nnapi_lib",
],
)
cc_library(
name = "nnapi_support_library",
srcs = [
"SupportLibrary.cc",
],
compatible_with = get_compatible_with_portable(),
copts = tflite_copts() + tflite_copts_warnings(),
visibility = _DEFAULT_VISIBILITY + nnapi_sl_visibility_allowlist(),
deps = [
":nnapi_support_library_headers",
"//tensorflow/lite:minimal_logging",
"//tensorflow/lite/nnapi:nnapi_lib",
],
)
+21
View File
@@ -0,0 +1,21 @@
# NNAPI Support Library
Files in this directory are a copy of NNAPI Support Library
[files](https://cs.android.com/android/platform/superproject/+/master:packages/modules/NeuralNetworks/shim_and_sl/;drc=629cea610b447266b1e6b01e4cb6a952dcb56e7e)
in AOSP.
The files had to be modified to make them work in TF Lite context. Here is the
list of differences from the AOSP version:
* `#include` directives use fully-qualified paths.
* `#pragma once` directives are changed to header guards. Android paths in
header guards are changed to TF Lite paths.
* `tensorflow/lite/nnapi/NeuralNetworksTypes.h` is used for definitions of
NNAPI types instead of
[`NeuralNetworksTypes.h` from AOSP](https://cs.android.com/android/_/android/platform/packages/modules/NeuralNetworks/+/6f0a05b9abdfe0d17afe0269c5329340809175b5:runtime/include/NeuralNetworksTypes.h;drc=a62e56b26b7382a62c5aa0e5964266eba55853d8).
* `loadNnApiSupportLibrary(...)` is using `tensorflow/lite/minimal_logging.h`
for logging on errors.
* `SupportLibrary.h` declarations are wrapped into `tflite::nnapi` namespace.
* `__BEGIN_DECLS` and `__END_DECLS` are changed to explicit `extern "C"`
blocks.
* Copyright notice is changed to the one used in Tensorflow project.
@@ -0,0 +1,93 @@
/* Copyright 2021 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.
==============================================================================*/
// Changed when importing from AOSP
#include "tensorflow/lite/nnapi/sl/include/SupportLibrary.h"
// Changed when importing from AOSP
#include <dlfcn.h>
#include <cinttypes>
#include <memory>
#include <string>
#include "tensorflow/lite/minimal_logging.h"
#include "tensorflow/lite/nnapi/NeuralNetworksTypes.h"
namespace tflite {
namespace nnapi {
using tflite::TFLITE_LOG_ERROR;
std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary(
const std::string& libName) {
void* libHandle = dlopen(libName.c_str(), RTLD_LAZY | RTLD_LOCAL);
if (libHandle == nullptr) {
TFLITE_LOG(TFLITE_LOG_ERROR, "nnapi error: unable to open library %s: %s",
libName.c_str(), dlerror());
return nullptr;
}
auto result = loadNnApiSupportLibrary(libHandle);
if (!result) {
dlclose(libHandle);
}
return result;
}
std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary(
void* libHandle) {
NnApiSLDriverImpl* (*getSlDriverImpl)();
getSlDriverImpl = reinterpret_cast<decltype(getSlDriverImpl)>(
dlsym(libHandle, "ANeuralNetworks_getSLDriverImpl"));
if (getSlDriverImpl == nullptr) {
TFLITE_LOG(TFLITE_LOG_ERROR,
"Failed to find ANeuralNetworks_getSLDriverImpl symbol");
return nullptr;
}
NnApiSLDriverImpl* impl = getSlDriverImpl();
if (impl == nullptr) {
TFLITE_LOG(TFLITE_LOG_ERROR,
"ANeuralNetworks_getSLDriverImpl returned nullptr");
return nullptr;
}
if (impl->implFeatureLevel < ANEURALNETWORKS_FEATURE_LEVEL_5 ||
impl->implFeatureLevel > ANEURALNETWORKS_FEATURE_LEVEL_7) {
TFLITE_LOG(TFLITE_LOG_ERROR,
"Unsupported NnApiSLDriverImpl->implFeatureLevel: %" PRId64,
impl->implFeatureLevel);
return nullptr;
}
if (impl->implFeatureLevel == ANEURALNETWORKS_FEATURE_LEVEL_5) {
return std::make_unique<NnApiSupportLibrary>(
reinterpret_cast<NnApiSLDriverImplFL5*>(impl), libHandle);
}
if (impl->implFeatureLevel == ANEURALNETWORKS_FEATURE_LEVEL_6) {
return std::make_unique<NnApiSupportLibrary>(
reinterpret_cast<NnApiSLDriverImplFL6*>(impl), libHandle);
}
if (impl->implFeatureLevel == ANEURALNETWORKS_FEATURE_LEVEL_7) {
return std::make_unique<NnApiSupportLibrary>(
reinterpret_cast<NnApiSLDriverImplFL7*>(impl), libHandle);
}
return nullptr;
}
} // namespace nnapi
} // namespace tflite
@@ -0,0 +1,99 @@
/* Copyright 2021 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_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_H_
#define TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_H_
// WARNING: this header file is DEPRECATED.
// See https://developer.android.com/ndk/guides/neuralnetworks/migration-guide.
#include <dlfcn.h>
#include <cstdlib>
#include <memory>
#include <string>
#include <variant>
// Changed when importing from AOSP
#include "tensorflow/lite/kernels/internal/compatibility.h"
#include "tensorflow/lite/nnapi/NeuralNetworksTypes.h"
#include "tensorflow/lite/nnapi/sl/public/NeuralNetworksSupportLibraryImpl.h"
namespace tflite {
namespace nnapi {
#ifndef __NNAPI_FL5_MIN_ANDROID_API__
#define __NNAPI_FL5_MIN_ANDROID_API__ __ANDROID_API_S__
#endif
/**
* Helper struct, wraps different versions of NnApiSLDriverImpl.
*
* Owns the .so handle, and will close it in destructor.
* Sets proper implStructFeatureLevel in constructor.
*
* There's expectation that for M>N, NnApiSLDriverImplFL(M) is
* a strict superset of NnApiSLDriverImplFL(N), and *NnApiSLDriverImplFL(M) can
* be reinterpret_cast to *NnApiSLDriverImplFL(N) safely.
*
* The base->implFeatureLevel is set to the actual Feature Level
* implemented by the SLDriverImpl,
*/
struct NnApiSupportLibrary {
NnApiSupportLibrary(const NnApiSLDriverImplFL5* impl, void* libHandle)
: libHandle(libHandle), fl5(impl) {}
// No need for ctor below since FL6&7 are typedefs of FL5
// NnApiSupportLibrary(const NnApiSLDriverImplFL6& impl, void* libHandle):
// impl(impl), NnApiSupportLibrary(const NnApiSLDriverImplFL7& impl, void*
// libHandle): impl(impl), libHandle(libHandle) {}
~NnApiSupportLibrary() {
if (libHandle != nullptr) {
dlclose(libHandle);
libHandle = nullptr;
}
}
NnApiSupportLibrary(const NnApiSupportLibrary&) = delete;
NnApiSupportLibrary& operator=(const NnApiSupportLibrary&) = delete;
int64_t getFeatureLevel() const { return fl5->base.implFeatureLevel; }
const NnApiSLDriverImplFL5* getFL5() const { return fl5; }
const NnApiSLDriverImplFL6* getFL6() const {
TFLITE_CHECK_GE(getFeatureLevel(), ANEURALNETWORKS_FEATURE_LEVEL_6);
return reinterpret_cast<const NnApiSLDriverImplFL6*>(&fl5);
}
const NnApiSLDriverImplFL7* getFL7() const {
TFLITE_CHECK_GE(getFeatureLevel(), ANEURALNETWORKS_FEATURE_LEVEL_7);
return reinterpret_cast<const NnApiSLDriverImplFL6*>(&fl5);
}
void* libHandle = nullptr;
const NnApiSLDriverImplFL5* fl5;
};
/**
* Loads the NNAPI support library.
* The NnApiSupportLibrary structure is filled with all the pointers. If one
* function doesn't exist, a null pointer is stored.
*/
std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary(
const std::string& libName);
std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary(
void* libHandle);
} // namespace nnapi
} // namespace tflite
#endif // TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_H_
@@ -0,0 +1,43 @@
/* Copyright 2021 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_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_SYMBOLS_H_
#define TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_SYMBOLS_H_
// WARNING: this header file is DEPRECATED.
// See https://developer.android.com/ndk/guides/neuralnetworks/migration-guide.
#include <stddef.h>
// Changed when importing from AOSP
#include "tensorflow/lite/nnapi/sl/public/NeuralNetworksSupportLibraryImpl.h"
// If you are linking against SL driver implementation through DT_NEEDED,
// you can use this declaration to access its implementation instead
// of doing dlsym.
#ifdef __cplusplus
extern "C" {
#endif
/**
* Get the NNAPI SL Driver NnApiSLDriverImpl with all
* driver functions.
*/
NnApiSLDriverImpl* ANeuralNetworks_getSLDriverImpl();
#ifdef __cplusplus
} // extern "C"
#endif
#endif // TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_SYMBOLS_H_
File diff suppressed because it is too large Load Diff