• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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_POOL_2D_TESTER_H_
17 #define TENSORFLOW_LITE_DELEGATES_XNNPACK_QUANTIZED_POOL_2D_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 QuantizedPool2DTester {
31  public:
32   QuantizedPool2DTester() = default;
33   QuantizedPool2DTester(const QuantizedPool2DTester&) = delete;
34   QuantizedPool2DTester& operator=(const QuantizedPool2DTester&) = delete;
35 
BatchSize(int32_t batch_size)36   inline QuantizedPool2DTester& BatchSize(int32_t batch_size) {
37     EXPECT_GT(batch_size, 0);
38     batch_size_ = batch_size;
39     return *this;
40   }
41 
BatchSize()42   inline int32_t BatchSize() const { return batch_size_; }
43 
Channels(int32_t channels)44   inline QuantizedPool2DTester& Channels(int32_t channels) {
45     EXPECT_GT(channels, 0);
46     channels_ = channels;
47     return *this;
48   }
49 
Channels()50   inline int32_t Channels() const { return channels_; }
51 
InputHeight(int32_t input_height)52   inline QuantizedPool2DTester& InputHeight(int32_t input_height) {
53     EXPECT_GT(input_height, 0);
54     input_height_ = input_height;
55     return *this;
56   }
57 
InputHeight()58   inline int32_t InputHeight() const { return input_height_; }
59 
InputWidth(int32_t input_width)60   inline QuantizedPool2DTester& InputWidth(int32_t input_width) {
61     EXPECT_GT(input_width, 0);
62     input_width_ = input_width;
63     return *this;
64   }
65 
InputWidth()66   inline int32_t InputWidth() const { return input_width_; }
67 
OutputWidth()68   inline int32_t OutputWidth() const {
69     if (Padding() == ::tflite::Padding_SAME) {
70       return (InputWidth() - 1) / StrideWidth() + 1;
71     } else {
72       return (InputWidth() - PoolingWidth()) / StrideWidth() + 1;
73     }
74   }
75 
OutputHeight()76   inline int32_t OutputHeight() const {
77     if (Padding() == ::tflite::Padding_SAME) {
78       return (InputHeight() - 1) / StrideHeight() + 1;
79     } else {
80       return (InputHeight() - PoolingHeight()) / StrideHeight() + 1;
81     }
82   }
83 
PoolingHeight(int32_t pooling_height)84   inline QuantizedPool2DTester& PoolingHeight(int32_t pooling_height) {
85     EXPECT_GT(pooling_height, 0);
86     pooling_height_ = pooling_height;
87     return *this;
88   }
89 
PoolingHeight()90   inline int32_t PoolingHeight() const { return pooling_height_; }
91 
PoolingWidth(int32_t pooling_width)92   inline QuantizedPool2DTester& PoolingWidth(int32_t pooling_width) {
93     EXPECT_GT(pooling_width, 0);
94     pooling_width_ = pooling_width;
95     return *this;
96   }
97 
PoolingWidth()98   inline int32_t PoolingWidth() const { return pooling_width_; }
99 
StrideHeight(int32_t stride_height)100   inline QuantizedPool2DTester& StrideHeight(int32_t stride_height) {
101     EXPECT_GT(stride_height, 0);
102     stride_height_ = stride_height;
103     return *this;
104   }
105 
StrideHeight()106   inline int32_t StrideHeight() const { return stride_height_; }
107 
StrideWidth(int32_t stride_width)108   inline QuantizedPool2DTester& StrideWidth(int32_t stride_width) {
109     EXPECT_GT(stride_width, 0);
110     stride_width_ = stride_width;
111     return *this;
112   }
113 
StrideWidth()114   inline int32_t StrideWidth() const { return stride_width_; }
115 
SamePadding()116   inline QuantizedPool2DTester& SamePadding() {
117     padding_ = ::tflite::Padding_SAME;
118     return *this;
119   }
120 
ValidPadding()121   inline QuantizedPool2DTester& ValidPadding() {
122     padding_ = ::tflite::Padding_VALID;
123     return *this;
124   }
125 
ReluActivation()126   inline QuantizedPool2DTester& ReluActivation() {
127     activation_ = ::tflite::ActivationFunctionType_RELU;
128     return *this;
129   }
130 
Relu6Activation()131   inline QuantizedPool2DTester& Relu6Activation() {
132     activation_ = ::tflite::ActivationFunctionType_RELU6;
133     return *this;
134   }
135 
ReluMinus1To1Activation()136   inline QuantizedPool2DTester& ReluMinus1To1Activation() {
137     activation_ = ::tflite::ActivationFunctionType_RELU_N1_TO_1;
138     return *this;
139   }
140 
TanhActivation()141   inline QuantizedPool2DTester& TanhActivation() {
142     activation_ = ::tflite::ActivationFunctionType_TANH;
143     return *this;
144   }
145 
SignBitActivation()146   inline QuantizedPool2DTester& SignBitActivation() {
147     activation_ = ::tflite::ActivationFunctionType_SIGN_BIT;
148     return *this;
149   }
150 
ZeroPoint(int32_t zero_point)151   inline QuantizedPool2DTester& ZeroPoint(int32_t zero_point) {
152     zero_point_ = zero_point;
153     return *this;
154   }
155 
ZeroPoint()156   inline int32_t ZeroPoint() const { return zero_point_; }
157 
Scale(float scale)158   inline QuantizedPool2DTester& Scale(float scale) {
159     scale_ = scale;
160     return *this;
161   }
162 
Scale()163   inline float Scale() const { return scale_; }
164 
Unsigned(bool is_unsigned)165   inline QuantizedPool2DTester& Unsigned(bool is_unsigned) {
166     unsigned_ = is_unsigned;
167     return *this;
168   }
169 
Unsigned()170   inline bool Unsigned() const { return unsigned_; }
171 
172   template <class T>
173   void Test(tflite::BuiltinOperator pool_op, Interpreter* delegate_interpreter,
174             Interpreter* default_interpreter) const;
175 
176   void Test(tflite::BuiltinOperator pool_op, TfLiteDelegate* delegate) const;
177 
178  private:
179   std::vector<char> CreateTfLiteModel(tflite::BuiltinOperator pool_op) const;
180 
Padding()181   inline ::tflite::Padding Padding() const { return padding_; }
182 
Activation()183   inline ::tflite::ActivationFunctionType Activation() const {
184     return activation_;
185   }
186 
187   int32_t batch_size_ = 1;
188   int32_t channels_ = 1;
189   int32_t input_height_ = 1;
190   int32_t input_width_ = 1;
191   int32_t pooling_height_ = 1;
192   int32_t pooling_width_ = 1;
193   int32_t stride_height_ = 1;
194   int32_t stride_width_ = 1;
195   ::tflite::Padding padding_ = ::tflite::Padding_VALID;
196   ::tflite::ActivationFunctionType activation_ =
197       ::tflite::ActivationFunctionType_NONE;
198   int32_t zero_point_ = 7;
199   float scale_ = 0.5f;
200   bool unsigned_ = false;
201 };
202 
203 }  // namespace xnnpack
204 }  // namespace tflite
205 
206 #endif  // TENSORFLOW_LITE_DELEGATES_XNNPACK_QUANTIZED_POOL_2D_TESTER_H_
207