1 #ifndef TENSORFLOW_LITE_INTERPRETER_TEST_UTIL_H_ 2 #define TENSORFLOW_LITE_INTERPRETER_TEST_UTIL_H_ 3 4 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 ==============================================================================*/ 18 19 #include <stdint.h> 20 21 #include <map> 22 #include <memory> 23 #include <string> 24 #include <vector> 25 26 #include <gtest/gtest.h> 27 #include "tensorflow/lite/internal/signature_def.h" 28 #include "tensorflow/lite/interpreter.h" 29 #include "tensorflow/lite/kernels/internal/compatibility.h" 30 #include "tensorflow/lite/string_util.h" 31 32 namespace tflite { 33 34 // Test helper for accessing private Intrepreter members and methods. 35 class InterpreterTest : public ::testing::Test { 36 public: InterpreterTest()37 InterpreterTest() : interpreter_(new Interpreter) {} 38 39 template <typename Delegate> ModifyGraphWithDelegate(Interpreter * interpreter,std::unique_ptr<Delegate> delegate)40 static TfLiteStatus ModifyGraphWithDelegate( 41 Interpreter* interpreter, std::unique_ptr<Delegate> delegate) { 42 return interpreter->ModifyGraphWithDelegate(std::move(delegate)); 43 } 44 45 protected: GetInterpreterContext()46 TfLiteContext* GetInterpreterContext() { return interpreter_->context_; } 47 mutable_lazy_delegate_providers()48 Interpreter::TfLiteDelegateCreators* mutable_lazy_delegate_providers() { 49 return &interpreter_->lazy_delegate_providers_; 50 } 51 HasDelegates()52 bool HasDelegates() { return interpreter_->HasDelegates(); } 53 IsFullyDelegated()54 bool IsFullyDelegated() const { return interpreter_->IsFullyDelegated(); } 55 ApplyLazyDelegateProviders()56 TfLiteStatus ApplyLazyDelegateProviders() { 57 return interpreter_->ApplyLazyDelegateProviders(); 58 } 59 BuildSignature(const std::string & signature_key,const std::map<std::string,uint32_t> & inputs,const std::map<std::string,uint32_t> & outputs)60 void BuildSignature(const std::string& signature_key, 61 const std::map<std::string, uint32_t>& inputs, 62 const std::map<std::string, uint32_t>& outputs) { 63 internal::SignatureDef signature; 64 signature.inputs = inputs; 65 signature.outputs = outputs; 66 signature.signature_key = signature_key; 67 signature.subgraph_index = 0; 68 interpreter_->SetSignatureDef({signature}); 69 } 70 SetExecutionPlan(const std::vector<int> & new_plan)71 TfLiteStatus SetExecutionPlan(const std::vector<int>& new_plan) { 72 return interpreter_->SetExecutionPlan(new_plan); 73 } 74 75 void AddSubgraphs(int subgraphs_to_add, 76 int* first_new_subgraph_index = nullptr) { 77 interpreter_->AddSubgraphs(subgraphs_to_add, first_new_subgraph_index); 78 } 79 80 std::unique_ptr<Interpreter> interpreter_; 81 }; 82 83 } // namespace tflite 84 85 #endif // TENSORFLOW_LITE_INTERPRETER_TEST_UTIL_H_ 86