• 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_QUANTIZED_UNARY_ELEMENTWISE_TESTER_H_
17 #define TENSORFLOW_LITE_DELEGATES_XNNPACK_QUANTIZED_UNARY_ELEMENTWISE_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 QuantizedUnaryElementwiseTester {
31  public:
32   QuantizedUnaryElementwiseTester() = default;
33   QuantizedUnaryElementwiseTester(const QuantizedUnaryElementwiseTester&) =
34       delete;
35   QuantizedUnaryElementwiseTester& operator=(
36       const QuantizedUnaryElementwiseTester&) = delete;
37 
Shape(std::initializer_list<int32_t> shape)38   inline QuantizedUnaryElementwiseTester& Shape(
39       std::initializer_list<int32_t> shape) {
40     for (auto it = shape.begin(); it != shape.end(); ++it) {
41       EXPECT_GT(*it, 0);
42     }
43     shape_ = std::vector<int32_t>(shape.begin(), shape.end());
44     size_ = QuantizedUnaryElementwiseTester::ComputeSize(shape_);
45     return *this;
46   }
47 
Shape()48   const std::vector<int32_t>& Shape() const { return shape_; }
49 
Size()50   int32_t Size() const { return size_; }
51 
InputZeroPoint(int32_t input_zero_point)52   inline QuantizedUnaryElementwiseTester& InputZeroPoint(
53       int32_t input_zero_point) {
54     input_zero_point_ = input_zero_point;
55     return *this;
56   }
57 
InputZeroPoint()58   inline int32_t InputZeroPoint() const { return input_zero_point_; }
59 
OutputZeroPoint(int32_t output_zero_point)60   inline QuantizedUnaryElementwiseTester& OutputZeroPoint(
61       int32_t output_zero_point) {
62     output_zero_point_ = output_zero_point;
63     return *this;
64   }
65 
OutputZeroPoint()66   inline int32_t OutputZeroPoint() const { return output_zero_point_; }
67 
InputScale(float input_scale)68   inline QuantizedUnaryElementwiseTester& InputScale(float input_scale) {
69     input_scale_ = input_scale;
70     return *this;
71   }
72 
InputScale()73   inline float InputScale() const { return input_scale_; }
74 
OutputScale(float output_scale)75   inline QuantizedUnaryElementwiseTester& OutputScale(float output_scale) {
76     output_scale_ = output_scale;
77     return *this;
78   }
79 
OutputScale()80   inline float OutputScale() const { return output_scale_; }
81 
Unsigned(bool is_unsigned)82   inline QuantizedUnaryElementwiseTester& Unsigned(bool is_unsigned) {
83     unsigned_ = is_unsigned;
84     return *this;
85   }
86 
Unsigned()87   inline bool Unsigned() const { return unsigned_; }
88 
89   template <class T>
90   void Test(Interpreter* delegate_interpreter,
91             Interpreter* default_interpreter) const;
92 
93   void Test(tflite::BuiltinOperator unary_op, TfLiteDelegate* delegate) const;
94 
95  private:
96   std::vector<char> CreateTfLiteModel(tflite::BuiltinOperator unary_op) const;
97 
98   static int32_t ComputeSize(const std::vector<int32_t>& shape);
99 
100   std::vector<int32_t> shape_;
101   int32_t size_;
102   int32_t input_zero_point_ = 0;
103   int32_t output_zero_point_ = 0;
104   float input_scale_ = 1.0f;
105   float output_scale_ = 1.0f;
106   bool unsigned_ = false;
107 };
108 
109 }  // namespace xnnpack
110 }  // namespace tflite
111 
112 #endif  // TENSORFLOW_LITE_DELEGATES_XNNPACK_QUANTIZED_UNARY_ELEMENTWISE_TESTER_H_
113