1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 #ifndef TENSORFLOW_LITE_DELEGATES_NNAPI_NNAPI_DELEGATE_PLUGIN_H_ 16 #define TENSORFLOW_LITE_DELEGATES_NNAPI_NNAPI_DELEGATE_PLUGIN_H_ 17 18 #include "tensorflow/lite/c/common.h" 19 #include "tensorflow/lite/nnapi/NeuralNetworksTypes.h" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif // __cplusplus 24 25 // The mapping utils intended for vendor plugin to track: 26 // - TFLite tensor indices to NN API tensor indices mapping. 27 // - TFLite node indices to NN API operation indices mapping. 28 // WARNING: This is an experimental interface that is subject to change. 29 typedef struct NnapiMappingUtilCInterface { 30 // Given a TFLite index, return the ANN index. If it doesn't exist 31 // return -1. 32 int (*TfLiteIndexToNnIndex)(NnapiMappingUtilCInterface* mapping, int index); 33 34 // When adding a non-tensor TFLite node parameter to NNAPI as an 35 // ANeuralNetworksOperand, notify NNAPI delegate to increment the operand 36 // count. 37 int (*AddNewNonTensorOperand)(NnapiMappingUtilCInterface* mapping); 38 39 // When adding a TFLite tensor to NNAPI as an ANeuralNetworksOperand, notify 40 // NNAPI delegate to add a new mapping from `tflite_index` and return the NN 41 // API tensor index. 42 int (*AddNewNnTensorIndex)(NnapiMappingUtilCInterface* mapping, 43 int tflite_index); 44 45 // When adding a TFLite tensor to NNAPI as multiple ANeuralNetworksOperand 46 // objects, for example when splitting one input into several ones, notify 47 // NNAPI delegate to increment the operand count. 48 int (*AddDelegateGeneratedInputAnnTensorOperand)( 49 NnapiMappingUtilCInterface* mapping); 50 51 // Given a TFLite index returns a TFLite type to which a tensor must be 52 // converted during copying the data to the memory allocated for NN API. 53 // kTfLiteNoType means no conversion is needed. 54 TfLiteType (*TfLiteIndexToNnTypeConversion)( 55 NnapiMappingUtilCInterface* mapping, int index); 56 57 // Add a new mapping from TFLite tensor index to a type conversion. 58 void (*AddTypeConversion)(NnapiMappingUtilCInterface* mapping, 59 int tflite_index, TfLiteType tflite_type); 60 61 // Add a new mapping from TFLite node index to NNAPI op index. 62 void (*AddNnapiToTfliteOpMapping)(NnapiMappingUtilCInterface* mapping, 63 int tflite_node_index); 64 65 // opaque handle for the mapping context. Only intended for the NNAPI Delegate 66 // to use. 67 void* context; 68 } NnapiMappingUtilCInterface; 69 70 // The interface for NNAPI Vendor Plugin. 71 // The interface exposes necessary functionalities for NNAPI delegate to 72 // interact with the vendor plugin. 73 // WARNING: This is an experimental interface that is subject to change. 74 typedef struct NnapiDelegateVendorPlugin { 75 // Validate whether the given TFLite node is supported by the plugin. 76 bool (*ValidateNode)(const TfLiteContext* context, 77 const TfLiteRegistration* registration, 78 const TfLiteNode* node); 79 80 // Translate a TFLite node into corresponding NNAPI operands and operation. 81 // It assumes that the call to Validate for has been successful for 82 // the operation. In case of success it returns kTfLiteOk and stores the 83 // corresponding NNAPI operand indices and operation code through the mapping 84 // utility interface. Returns kTfLiteError in case of failures during mapping. 85 TfLiteStatus (*MapNode)(TfLiteContext* context, const TfLiteNode* node, 86 int node_index, NnapiMappingUtilCInterface* mapping, 87 ANeuralNetworksModel* model); 88 89 // Parse the provided compilation_hints string and configure it for the given 90 // ANeuralNetworksCompilation handle. 91 TfLiteStatus (*ConfigureCompilationHints)( 92 const char* compilation_hints, ANeuralNetworksCompilation* compilation); 93 94 // Parse the provided execution_hints string and configure it for the given 95 // ANeuralNetworksExecution handle. 96 TfLiteStatus (*ConfigureExecutionHints)(const char* execution_hints, 97 ANeuralNetworksExecution* execution); 98 } NnapiDelegateVendorPlugin; 99 100 #ifdef __cplusplus 101 } // extern "C" 102 #endif // __cplusplus 103 104 #endif // TENSORFLOW_LITE_DELEGATES_NNAPI_NNAPI_DELEGATE_PLUGIN_H_ 105