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