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_DEQUANTIZE_TESTER_H_ 17 #define TENSORFLOW_LITE_DELEGATES_XNNPACK_DEQUANTIZE_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 DequantizeTester { 31 public: 32 DequantizeTester() = default; 33 DequantizeTester(const DequantizeTester&) = delete; 34 DequantizeTester& operator=(const DequantizeTester&) = delete; 35 Shape(std::initializer_list<int32_t> shape)36 inline DequantizeTester& 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_ = DequantizeTester::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 DequantizeTester& 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 DequantizeTester& InputScale(float input_scale) { 57 input_scale_ = input_scale; 58 return *this; 59 } 60 InputScale()61 inline float InputScale() const { return input_scale_; } 62 Unsigned(bool is_unsigned)63 inline DequantizeTester& Unsigned(bool is_unsigned) { 64 unsigned_ = is_unsigned; 65 return *this; 66 } 67 Unsigned()68 inline bool Unsigned() const { return unsigned_; } 69 70 template <class T> 71 void Test(Interpreter* delegate_interpreter, 72 Interpreter* default_interpreter) const; 73 74 void Test(TfLiteDelegate* delegate) const; 75 76 private: 77 std::vector<char> CreateTfLiteModel() const; 78 79 static int32_t ComputeSize(const std::vector<int32_t>& shape); 80 81 std::vector<int32_t> shape_; 82 int32_t size_; 83 int32_t input_zero_point_ = 0; 84 float input_scale_ = 1.0f; 85 bool unsigned_ = false; 86 }; 87 88 } // namespace xnnpack 89 } // namespace tflite 90 91 #endif // TENSORFLOW_LITE_DELEGATES_XNNPACK_DEQUANTIZE_TESTER_H_ 92