• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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 
16 #ifndef TENSORFLOW_LITE_DELEGATES_XNNPACK_QUANTIZE_TESTER_H_
17 #define TENSORFLOW_LITE_DELEGATES_XNNPACK_QUANTIZE_TESTER_H_
18 
19 #include <cstdint>
20 #include <vector>
21 
22 #include <gtest/gtest.h>
23 #include "tensorflow/lite/c/common.h"
24 #include "tensorflow/lite/interpreter.h"
25 #include "tensorflow/lite/schema/schema_generated.h"
26 
27 namespace tflite {
28 namespace xnnpack {
29 
30 class QuantizeTester {
31  public:
32   QuantizeTester() = default;
33   QuantizeTester(const QuantizeTester&) = delete;
34   QuantizeTester& operator=(const QuantizeTester&) = delete;
35 
Shape(std::initializer_list<int32_t> shape)36   inline QuantizeTester& Shape(std::initializer_list<int32_t> shape) {
37     for (auto it = shape.begin(); it != shape.end(); ++it) {
38       EXPECT_GT(*it, 0);
39     }
40     shape_ = std::vector<int32_t>(shape.begin(), shape.end());
41     size_ = QuantizeTester::ComputeSize(shape_);
42     return *this;
43   }
44 
Shape()45   const std::vector<int32_t>& Shape() const { return shape_; }
46 
Size()47   int32_t Size() const { return size_; }
48 
InputZeroPoint(int32_t input_zero_point)49   inline QuantizeTester& InputZeroPoint(int32_t input_zero_point) {
50     input_zero_point_ = input_zero_point;
51     return *this;
52   }
53 
InputZeroPoint()54   inline int32_t InputZeroPoint() const { return input_zero_point_; }
55 
InputScale(float input_scale)56   inline QuantizeTester& InputScale(float input_scale) {
57     input_scale_ = input_scale;
58     return *this;
59   }
60 
InputScale()61   inline float InputScale() const { return input_scale_; }
62 
OutputZeroPoint(int32_t output_zero_point)63   inline QuantizeTester& OutputZeroPoint(int32_t output_zero_point) {
64     output_zero_point_ = output_zero_point;
65     return *this;
66   }
67 
OutputZeroPoint()68   inline int32_t OutputZeroPoint() const { return output_zero_point_; }
69 
OutputScale(float output_scale)70   inline QuantizeTester& OutputScale(float output_scale) {
71     output_scale_ = output_scale;
72     return *this;
73   }
74 
OutputScale()75   inline float OutputScale() const { return output_scale_; }
76 
Unsigned(bool is_unsigned)77   inline QuantizeTester& Unsigned(bool is_unsigned) {
78     unsigned_ = is_unsigned;
79     return *this;
80   }
81 
Unsigned()82   inline bool Unsigned() const { return unsigned_; }
83 
84   template <class T>
85   void PopulateInput(Interpreter* delegate_interpreter,
86                      Interpreter* default_interpreter) const;
87 
88   template <class T>
89   void InvokeAndCheckOutput(Interpreter* delegate_interpreter,
90                             Interpreter* default_interpreter) const;
91 
92   void Test(TensorType input_type, TensorType output_type,
93             TfLiteDelegate* delegate) const;
94 
95  private:
96   std::vector<char> CreateTfLiteModel(TensorType input_type,
97                                       TensorType output_type) const;
98 
99   static int32_t ComputeSize(const std::vector<int32_t>& shape);
100 
101   std::vector<int32_t> shape_;
102   int32_t size_;
103   int32_t input_zero_point_ = 0;
104   float input_scale_ = 1.0f;
105   int32_t output_zero_point_ = 0;
106   float output_scale_ = 1.0f;
107   bool unsigned_ = false;
108 };
109 
110 }  // namespace xnnpack
111 }  // namespace tflite
112 
113 #endif  // TENSORFLOW_LITE_DELEGATES_XNNPACK_QUANTIZE_TESTER_H_
114