1 /* Copyright 2017 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_CONTRIB_LITE_NNAPI_DELEGATE_H_ 16 #define TENSORFLOW_CONTRIB_LITE_NNAPI_DELEGATE_H_ 17 18 #include "tensorflow/contrib/lite/allocation.h" 19 #include "tensorflow/contrib/lite/context.h" 20 #include "tensorflow/contrib/lite/error_reporter.h" 21 #include "tensorflow/contrib/lite/interpreter.h" 22 #include "tensorflow/contrib/lite/nnapi/NeuralNetworksShim.h" 23 24 class ANeuralNetworsModel; 25 26 namespace tflite { 27 28 class NNAPIAllocation : public MMAPAllocation { 29 public: 30 NNAPIAllocation(const char* filename, ErrorReporter* error_reporter); 31 ~NNAPIAllocation(); 32 33 size_t offset(const void* ptr) const { 34 auto signed_offset = reinterpret_cast<const uint8_t*>(ptr) - 35 reinterpret_cast<const uint8_t*>(mmapped_buffer_); 36 37 return static_cast<size_t>(signed_offset); 38 } 39 40 ANeuralNetworksMemory* memory() const { return handle_; } 41 bool valid() const override { return handle_ != nullptr; } 42 43 private: 44 mutable ANeuralNetworksMemory* handle_ = nullptr; 45 }; 46 47 class NNAPIDelegate { 48 public: 49 ~NNAPIDelegate(); 50 51 // Convert a tflite graph to NNAPI 52 TfLiteStatus BuildGraph(Interpreter* interpreter); 53 54 // Run 55 TfLiteStatus Invoke(Interpreter* interpreter); 56 57 private: 58 // The NN API model handle 59 ANeuralNetworksModel* nn_model_ = nullptr; 60 // The NN API compilation handle 61 ANeuralNetworksCompilation* nn_compiled_model_ = nullptr; 62 }; 63 64 } // namespace tflite 65 66 #endif // TENSORFLOW_CONTRIB_LITE_NNAPI_DELEGATE_H_ 67