1 /* Copyright 2020 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_SUPPORT_CC_PORT_DEFAULT_TFLITE_WRAPPER_H_ 16 #define TENSORFLOW_LITE_SUPPORT_CC_PORT_DEFAULT_TFLITE_WRAPPER_H_ 17 18 #include <memory> 19 #include <utility> 20 21 #include "absl/status/status.h" 22 #include "tensorflow/lite/experimental/acceleration/configuration/configuration.pb.h" 23 #include "tensorflow/lite/interpreter.h" 24 25 namespace tflite { 26 namespace support { 27 28 // Wrapper for a TfLiteInterpreter that may be accelerated[1]. This is NOT yet 29 // implemented: this class only provides a first, minimal interface in the 30 // meanwhile. 31 // 32 // [1] See tensorflow/lite/experimental/acceleration for more details. 33 class TfLiteInterpreterWrapper { 34 public: 35 TfLiteInterpreterWrapper() = default; 36 37 virtual ~TfLiteInterpreterWrapper() = default; 38 39 // Calls `interpreter_initializer` and then `AllocateTensors`. Future 40 // implementation of this method will attempt to apply the provided 41 // `compute_settings` with a graceful fallback in case a failure occurs. 42 // Note: before this gets implemented, do NOT call this method with non-empty 43 // `compute_settings` otherwise an unimplemented error occurs. 44 absl::Status InitializeWithFallback( 45 std::function<absl::Status(std::unique_ptr<tflite::Interpreter>*)> 46 interpreter_initializer, 47 const tflite::proto::ComputeSettings& compute_settings); 48 49 // Calls `set_inputs` and then Invoke() on the interpreter. Future 50 // implementation of this method will perform a graceful fallback in case a 51 // failure occur due to the `compute_settings` provided at initialization 52 // time. 53 absl::Status InvokeWithFallback( 54 const std::function<absl::Status(tflite::Interpreter* interpreter)>& 55 set_inputs); 56 57 // Calls Invoke() on the interpreter. Caller must have set up inputs 58 // before-hand. 59 absl::Status InvokeWithoutFallback(); 60 61 // Cancels the current running TFLite invocation on CPU. This method is not 62 // yet implemented though it is safe to use it as it acts as a NOP. 63 void Cancel(); 64 65 // Accesses the underlying interpreter for other methods. 66 tflite::Interpreter& operator*() { return *interpreter_; } 67 tflite::Interpreter* operator->() { return interpreter_.get(); } 68 tflite::Interpreter& operator*() const { return *interpreter_; } 69 tflite::Interpreter* operator->() const { return interpreter_.get(); } get()70 tflite::Interpreter* get() const { return interpreter_.get(); } 71 72 TfLiteInterpreterWrapper(const TfLiteInterpreterWrapper&) = delete; 73 TfLiteInterpreterWrapper& operator=(const TfLiteInterpreterWrapper&) = delete; 74 75 private: 76 std::unique_ptr<tflite::Interpreter> interpreter_; 77 }; 78 79 } // namespace support 80 } // namespace tflite 81 82 #endif // TENSORFLOW_LITE_SUPPORT_CC_PORT_DEFAULT_TFLITE_WRAPPER_H_ 83