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_LITE_TESTING_TFLITE_DRIVER_H_
16 #define TENSORFLOW_LITE_TESTING_TFLITE_DRIVER_H_
17 
18 #include <map>
19 #include <memory>
20 
21 #if !defined(__APPLE__)
22 #include "tensorflow/lite/delegates/flex/delegate.h"
23 #endif
24 #include "tensorflow/lite/interpreter.h"
25 #include "tensorflow/lite/kernels/register.h"
26 #include "tensorflow/lite/kernels/register_ref.h"
27 #include "tensorflow/lite/model.h"
28 #include "tensorflow/lite/testing/test_runner.h"
29 
30 namespace tflite {
31 namespace testing {
32 
33 // A test runner that feeds inputs into TF Lite and verifies its outputs.
34 class TfLiteDriver : public TestRunner {
35  public:
36   enum class DelegateType {
37     kNone,
38     kNnapi,
39     kGpu,
40     kFlex,
41   };
42 
43   // Initialize the global test delegate providers from commandline arguments
44   // and returns true if successful.
45   static bool InitTestDelegateProviders(int* argc, const char** argv);
46 
47   /**
48    * Creates a new TfLiteDriver
49    * @param  delegate         The (optional) delegate to use.
50    * @param  reference_kernel Whether to use the builtin reference kernel
51    * ops.
52    */
53   explicit TfLiteDriver(DelegateType delegate_type = DelegateType::kNone,
54                         bool reference_kernel = false);
55   ~TfLiteDriver() override;
56 
57   void LoadModel(const string& bin_file_path) override;
GetInputs()58   const std::vector<int>& GetInputs() override {
59     return interpreter_->inputs();
60   }
GetOutputs()61   const std::vector<int>& GetOutputs() override {
62     return interpreter_->outputs();
63   }
64   void ReshapeTensor(int id, const string& csv_values) override;
65   void AllocateTensors() override;
66   void ResetTensor(int id) override;
67   void SetInput(int id, const string& csv_values) override;
68   void SetExpectation(int id, const string& csv_values) override;
69   void SetShapeExpectation(int id, const string& csv_values) override;
70   void Invoke() override;
71   bool CheckResults() override;
72   string ReadOutput(int id) override;
73   void SetThreshold(double relative_threshold, double absolute_threshold);
74   void SetQuantizationErrorMultiplier(int quantization_error_multiplier);
75 
76  protected:
77   Interpreter::TfLiteDelegatePtr delegate_;
78 
79  private:
DeallocateStringTensor(TfLiteTensor * t)80   void DeallocateStringTensor(TfLiteTensor* t) {
81     if (t) {
82       free(t->data.raw);
83       t->data.raw = nullptr;
84     }
85   }
AllocateStringTensor(int id,size_t num_bytes,TfLiteTensor * t)86   void AllocateStringTensor(int id, size_t num_bytes, TfLiteTensor* t) {
87     t->data.raw = reinterpret_cast<char*>(malloc(num_bytes));
88     t->bytes = num_bytes;
89     tensors_to_deallocate_[id] = t;
90   }
91 
92   void ResetLSTMStateTensors();
93 
94   class DataExpectation;
95   class ShapeExpectation;
96 
97   std::unique_ptr<OpResolver> resolver_;
98   std::unique_ptr<FlatBufferModel> model_;
99   std::unique_ptr<Interpreter> interpreter_;
100   std::map<int, std::unique_ptr<DataExpectation>> expected_output_;
101   std::map<int, std::unique_ptr<ShapeExpectation>> expected_output_shape_;
102   bool must_allocate_tensors_ = true;
103   std::map<int, TfLiteTensor*> tensors_to_deallocate_;
104   double relative_threshold_;
105   double absolute_threshold_;
106   int quantization_error_multiplier_;
107 };
108 
109 }  // namespace testing
110 }  // namespace tflite
111 
112 #endif  // TENSORFLOW_LITE_TESTING_TFLITE_DRIVER_H_
113