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_UNARY_ELEMENTWISE_TESTER_H_ 17 #define TENSORFLOW_LITE_DELEGATES_XNNPACK_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/schema/schema_generated.h" 25 26 namespace tflite { 27 namespace xnnpack { 28 29 class UnaryElementwiseTester { 30 public: 31 UnaryElementwiseTester() = default; 32 UnaryElementwiseTester(const UnaryElementwiseTester&) = delete; 33 UnaryElementwiseTester& operator=(const UnaryElementwiseTester&) = delete; 34 Shape(std::initializer_list<int32_t> shape)35 inline UnaryElementwiseTester& Shape(std::initializer_list<int32_t> shape) { 36 for (auto it = shape.begin(); it != shape.end(); ++it) { 37 EXPECT_GT(*it, 0); 38 } 39 shape_ = std::vector<int32_t>(shape.begin(), shape.end()); 40 size_ = UnaryElementwiseTester::ComputeSize(shape_); 41 return *this; 42 } 43 Shape()44 const std::vector<int32_t>& Shape() const { return shape_; } 45 Size()46 int32_t Size() const { return size_; } 47 RelativeTolerance(float relative_tolerance)48 inline UnaryElementwiseTester& RelativeTolerance(float relative_tolerance) { 49 relative_tolerance_ = relative_tolerance; 50 return *this; 51 } 52 RelativeTolerance()53 float RelativeTolerance() const { return relative_tolerance_; } 54 55 void Test(tflite::BuiltinOperator unary_op, TfLiteDelegate* delegate) const; 56 57 private: 58 std::vector<char> CreateTfLiteModel(tflite::BuiltinOperator unary_op) const; 59 60 static int32_t ComputeSize(const std::vector<int32_t>& shape); 61 62 std::vector<int32_t> shape_; 63 int32_t size_; 64 float relative_tolerance_{10.0f}; 65 }; 66 67 } // namespace xnnpack 68 } // namespace tflite 69 70 #endif // TENSORFLOW_LITE_DELEGATES_XNNPACK_UNARY_ELEMENTWISE_TESTER_H_ 71