• 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_REDUCE_TESTER_H_
17 #define TENSORFLOW_LITE_DELEGATES_XNNPACK_REDUCE_TESTER_H_
18 
19 #include <cstdint>
20 #include <unordered_set>
21 #include <vector>
22 
23 #include <gtest/gtest.h>
24 #include "tensorflow/lite/c/common.h"
25 #include "tensorflow/lite/schema/schema_generated.h"
26 
27 namespace tflite {
28 namespace xnnpack {
29 
30 class ReduceTester {
31  public:
32   ReduceTester() = default;
33   ReduceTester(const ReduceTester&) = delete;
34   ReduceTester& operator=(const ReduceTester&) = delete;
35 
InputShape(std::initializer_list<int32_t> shape)36   inline ReduceTester& 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     input_size_ = ReduceTester::ComputeSize(input_shape_);
42     return *this;
43   }
44 
InputShape()45   inline const std::vector<int32_t>& InputShape() const { return input_shape_; }
46 
InputSize()47   inline int32_t InputSize() const { return input_size_; }
48 
Axes(std::initializer_list<int32_t> axes)49   inline ReduceTester& Axes(std::initializer_list<int32_t> axes) {
50     for (auto it = axes.begin(); it != axes.end(); ++it) {
51       EXPECT_GE(*it, 0);
52     }
53     axes_ = std::vector<int32_t>(axes.begin(), axes.end());
54     return *this;
55   }
56 
Axes()57   inline const std::vector<int32_t>& Axes() const { return axes_; }
58 
KeepDims(bool keep_dims)59   inline ReduceTester& KeepDims(bool keep_dims) {
60     keep_dims_ = keep_dims;
61     return *this;
62   }
63 
KeepDims()64   inline bool KeepDims() const { return keep_dims_; }
65 
OutputShape()66   inline std::vector<int32_t> OutputShape() const {
67     std::vector<int32_t> output_shape;
68     output_shape.reserve(InputShape().size());
69     std::unordered_set<int32_t> axes_set(Axes().cbegin(), Axes().cend());
70     for (int32_t i = 0; i < InputShape().size(); i++) {
71       if (axes_set.count(i) != 0) {
72         if (KeepDims()) {
73           output_shape.push_back(1);
74         }
75       } else {
76         output_shape.push_back(InputShape()[i]);
77       }
78     }
79     return output_shape;
80   }
81 
OutputSize()82   inline int32_t OutputSize() const {
83     int32_t output_size = 1;
84     std::unordered_set<int32_t> axes_set(Axes().cbegin(), Axes().cend());
85     for (int32_t i = 0; i < InputShape().size(); i++) {
86       if (axes_set.count(i) == 0) {
87         output_size *= InputShape()[i];
88       }
89     }
90     return output_size;
91   }
92 
RelativeTolerance(float relative_tolerance)93   inline ReduceTester& RelativeTolerance(float relative_tolerance) {
94     relative_tolerance_ = relative_tolerance;
95     return *this;
96   }
97 
RelativeTolerance()98   inline float RelativeTolerance() const { return relative_tolerance_; }
99 
100   void Test(tflite::BuiltinOperator reduce_op, TfLiteDelegate* delegate) const;
101 
102  private:
103   std::vector<char> CreateTfLiteModel(tflite::BuiltinOperator reduce_op) const;
104 
105   static int32_t ComputeSize(const std::vector<int32_t>& shape);
106 
107   std::vector<int32_t> input_shape_;
108   std::vector<int32_t> axes_;
109   int32_t input_size_;
110   bool keep_dims_ = true;
111   float relative_tolerance_ = 10.0f;
112 };
113 
114 }  // namespace xnnpack
115 }  // namespace tflite
116 
117 #endif  // TENSORFLOW_LITE_DELEGATES_XNNPACK_REDUCE_TESTER_H_
118