25 #define DLNNAPI_DEBUG_LOG 0 27 #include <android/log.h> 28 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, "NNAPI", __VA_ARGS__) 31 #define TAG_API_27 "\x01" 34 static const char function_names[] =
35 TAG_API_27
"ANeuralNetworksMemory_createFromFd\0" 36 TAG_API_27
"ANeuralNetworksMemory_free\0" 37 TAG_API_27
"ANeuralNetworksModel_create\0" 38 TAG_API_27
"ANeuralNetworksModel_finish\0" 39 TAG_API_27
"ANeuralNetworksModel_free\0" 40 TAG_API_27
"ANeuralNetworksCompilation_create\0" 41 TAG_API_27
"ANeuralNetworksCompilation_free\0" 42 TAG_API_27
"ANeuralNetworksCompilation_setPreference\0" 43 TAG_API_27
"ANeuralNetworksCompilation_finish\0" 44 TAG_API_27
"ANeuralNetworksModel_addOperand\0" 45 TAG_API_27
"ANeuralNetworksModel_setOperandValue\0" 46 TAG_API_27
"ANeuralNetworksModel_setOperandValueFromMemory\0" 47 TAG_API_27
"ANeuralNetworksModel_addOperation\0" 48 TAG_API_27
"ANeuralNetworksModel_identifyInputsAndOutputs\0" 49 TAG_API_27
"ANeuralNetworksExecution_create\0" 50 TAG_API_27
"ANeuralNetworksExecution_free\0" 51 TAG_API_27
"ANeuralNetworksExecution_setInput\0" 52 TAG_API_27
"ANeuralNetworksExecution_setInputFromMemory\0" 53 TAG_API_27
"ANeuralNetworksExecution_setOutput\0" 54 TAG_API_27
"ANeuralNetworksExecution_setOutputFromMemory\0" 55 TAG_API_27
"ANeuralNetworksExecution_startCompute\0" 56 TAG_API_27
"ANeuralNetworksEvent_wait\0" 57 TAG_API_27
"ANeuralNetworksEvent_free\0";
60 bool dlnnapi_load(
struct dlnnapi* nnapi, uint32_t flags) {
65 memset(nnapi, 0,
sizeof(
struct dlnnapi));
66 if (!(flags & DLNNAPI_FLAG_VERSION_27)) {
74 nnapi->handle = dlopen(
"libneuralnetworks.so", RTLD_LAZY | RTLD_LOCAL);
75 if (nnapi->handle != NULL) {
77 LOGI(
"note: loaded libneuralnetworks.so\n");
80 uint8_t version_flags = (uint8_t)(flags & DLNNAPI_FLAG_VERSION_MASK);
81 const char* function_name = function_names;
82 for (
size_t i = 0; i < DLNNAPI_FUNCTION_COUNT; i++) {
83 const uint8_t tag = (uint8_t)*function_name++;
84 if ((tag & version_flags) != 0) {
85 void*
function = dlsym(nnapi->handle, function_name);
86 if (
function == NULL) {
89 "note: failed to locate %s in libneuralnetworks.so: %s\n",
93 version_flags &= ~tag;
94 if (version_flags == 0) {
98 nnapi->functions[i] =
function;
101 function_name += strlen(function_name) + 1;
103 nnapi->flags = (uint32_t)version_flags;
107 #if DLNNAPI_DEBUG_LOG 108 LOGI(
"note: failed to load libneuralnetworks.so: %s\n", dlerror());
116 void dlnnapi_free(
struct dlnnapi* nnapi) {
118 if (nnapi->handle != NULL) {
121 if (dlclose(nnapi->handle) != 0) {
122 #if DLNNAPI_DEBUG_LOG 123 LOGI(
"note: failed to unload libneuralnetworks.so: %s\n", dlerror());
127 memset(nnapi, 0,
sizeof(
struct dlnnapi));