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_PAD_TESTER_H_ 17 #define TENSORFLOW_LITE_DELEGATES_XNNPACK_QUANTIZED_PAD_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 QuantizedPadTester { 31 public: 32 QuantizedPadTester() = default; 33 QuantizedPadTester(const QuantizedPadTester&) = delete; 34 QuantizedPadTester& operator=(const QuantizedPadTester&) = delete; 35 InputShape(std::initializer_list<int32_t> shape)36 inline QuantizedPadTester& InputShape(std::initializer_list<int32_t> shape) { 37 for (auto it = shape.begin(); it != shape.end(); ++it) { 38 EXPECT_GT(*it, 0); 39 } 40 input_shape_ = std::vector<int32_t>(shape.begin(), shape.end()); 41 return *this; 42 } 43 InputShape()44 inline const std::vector<int32_t>& InputShape() const { return input_shape_; } 45 InputPrePaddings(std::initializer_list<int32_t> paddings)46 inline QuantizedPadTester& InputPrePaddings( 47 std::initializer_list<int32_t> paddings) { 48 for (auto it = paddings.begin(); it != paddings.end(); ++it) { 49 EXPECT_GE(*it, 0); 50 } 51 input_pre_paddings_ = 52 std::vector<int32_t>(paddings.begin(), paddings.end()); 53 return *this; 54 } 55 InputPrePaddings()56 inline const std::vector<int32_t> InputPrePaddings() const { 57 return input_pre_paddings_; 58 } 59 InputPostPaddings(std::initializer_list<int32_t> paddings)60 inline QuantizedPadTester& InputPostPaddings( 61 std::initializer_list<int32_t> paddings) { 62 for (auto it = paddings.begin(); it != paddings.end(); ++it) { 63 EXPECT_GE(*it, 0); 64 } 65 input_post_paddings_ = 66 std::vector<int32_t>(paddings.begin(), paddings.end()); 67 return *this; 68 } 69 InputPostPaddings()70 inline const std::vector<int32_t> InputPostPaddings() const { 71 return input_post_paddings_; 72 } 73 74 std::vector<int32_t> OutputShape() const; 75 ZeroPoint(int32_t zero_point)76 inline QuantizedPadTester& ZeroPoint(int32_t zero_point) { 77 zero_point_ = zero_point; 78 return *this; 79 } 80 ZeroPoint()81 inline int32_t ZeroPoint() const { return zero_point_; } 82 Scale(float scale)83 inline QuantizedPadTester& Scale(float scale) { 84 scale_ = scale; 85 return *this; 86 } 87 Scale()88 inline float Scale() const { return scale_; } 89 Unsigned(bool is_unsigned)90 inline QuantizedPadTester& Unsigned(bool is_unsigned) { 91 unsigned_ = is_unsigned; 92 return *this; 93 } 94 Unsigned()95 inline bool Unsigned() const { return unsigned_; } 96 97 template <class T> 98 void Test(Interpreter* delegate_interpreter, 99 Interpreter* default_interpreter) const; 100 101 void Test(TfLiteDelegate* delegate) const; 102 103 private: 104 std::vector<char> CreateTfLiteModel() const; 105 106 static int32_t ComputeSize(const std::vector<int32_t>& shape); 107 108 std::vector<int32_t> input_shape_; 109 std::vector<int32_t> input_pre_paddings_; 110 std::vector<int32_t> input_post_paddings_; 111 int32_t zero_point_ = 7; 112 float scale_ = 0.8f; 113 bool unsigned_ = false; 114 }; 115 116 } // namespace xnnpack 117 } // namespace tflite 118 119 #endif // TENSORFLOW_LITE_DELEGATES_XNNPACK_QUANTIZED_PAD_TESTER_H_ 120