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_POOL_2D_TESTER_H_ 17 #define TENSORFLOW_LITE_DELEGATES_XNNPACK_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/schema/schema_generated.h" 25 26 namespace tflite { 27 namespace xnnpack { 28 29 class Pool2DTester { 30 public: 31 Pool2DTester() = default; 32 Pool2DTester(const Pool2DTester&) = delete; 33 Pool2DTester& operator=(const Pool2DTester&) = delete; 34 BatchSize(int32_t batch_size)35 inline Pool2DTester& BatchSize(int32_t batch_size) { 36 EXPECT_GT(batch_size, 0); 37 batch_size_ = batch_size; 38 return *this; 39 } 40 BatchSize()41 inline int32_t BatchSize() const { return batch_size_; } 42 Channels(int32_t channels)43 inline Pool2DTester& Channels(int32_t channels) { 44 EXPECT_GT(channels, 0); 45 channels_ = channels; 46 return *this; 47 } 48 Channels()49 inline int32_t Channels() const { return channels_; } 50 InputHeight(int32_t input_height)51 inline Pool2DTester& InputHeight(int32_t input_height) { 52 EXPECT_GT(input_height, 0); 53 input_height_ = input_height; 54 return *this; 55 } 56 InputHeight()57 inline int32_t InputHeight() const { return input_height_; } 58 InputWidth(int32_t input_width)59 inline Pool2DTester& InputWidth(int32_t input_width) { 60 EXPECT_GT(input_width, 0); 61 input_width_ = input_width; 62 return *this; 63 } 64 InputWidth()65 inline int32_t InputWidth() const { return input_width_; } 66 OutputWidth()67 inline int32_t OutputWidth() const { 68 if (Padding() == ::tflite::Padding_SAME) { 69 return (InputWidth() - 1) / StrideWidth() + 1; 70 } else { 71 return (InputWidth() - PoolingWidth()) / StrideWidth() + 1; 72 } 73 } 74 OutputHeight()75 inline int32_t OutputHeight() const { 76 if (Padding() == ::tflite::Padding_SAME) { 77 return (InputHeight() - 1) / StrideHeight() + 1; 78 } else { 79 return (InputHeight() - PoolingHeight()) / StrideHeight() + 1; 80 } 81 } 82 PoolingHeight(int32_t pooling_height)83 inline Pool2DTester& PoolingHeight(int32_t pooling_height) { 84 EXPECT_GT(pooling_height, 0); 85 pooling_height_ = pooling_height; 86 return *this; 87 } 88 PoolingHeight()89 inline int32_t PoolingHeight() const { return pooling_height_; } 90 PoolingWidth(int32_t pooling_width)91 inline Pool2DTester& PoolingWidth(int32_t pooling_width) { 92 EXPECT_GT(pooling_width, 0); 93 pooling_width_ = pooling_width; 94 return *this; 95 } 96 PoolingWidth()97 inline int32_t PoolingWidth() const { return pooling_width_; } 98 StrideHeight(int32_t stride_height)99 inline Pool2DTester& StrideHeight(int32_t stride_height) { 100 EXPECT_GT(stride_height, 0); 101 stride_height_ = stride_height; 102 return *this; 103 } 104 StrideHeight()105 inline int32_t StrideHeight() const { return stride_height_; } 106 StrideWidth(int32_t stride_width)107 inline Pool2DTester& StrideWidth(int32_t stride_width) { 108 EXPECT_GT(stride_width, 0); 109 stride_width_ = stride_width; 110 return *this; 111 } 112 StrideWidth()113 inline int32_t StrideWidth() const { return stride_width_; } 114 SamePadding()115 inline Pool2DTester& SamePadding() { 116 padding_ = ::tflite::Padding_SAME; 117 return *this; 118 } 119 ValidPadding()120 inline Pool2DTester& ValidPadding() { 121 padding_ = ::tflite::Padding_VALID; 122 return *this; 123 } 124 ReluActivation()125 inline Pool2DTester& ReluActivation() { 126 activation_ = ::tflite::ActivationFunctionType_RELU; 127 return *this; 128 } 129 Relu6Activation()130 inline Pool2DTester& Relu6Activation() { 131 activation_ = ::tflite::ActivationFunctionType_RELU6; 132 return *this; 133 } 134 ReluMinus1To1Activation()135 inline Pool2DTester& ReluMinus1To1Activation() { 136 activation_ = ::tflite::ActivationFunctionType_RELU_N1_TO_1; 137 return *this; 138 } 139 TanhActivation()140 inline Pool2DTester& TanhActivation() { 141 activation_ = ::tflite::ActivationFunctionType_TANH; 142 return *this; 143 } 144 SignBitActivation()145 inline Pool2DTester& SignBitActivation() { 146 activation_ = ::tflite::ActivationFunctionType_SIGN_BIT; 147 return *this; 148 } 149 150 void Test(tflite::BuiltinOperator pool_op, TfLiteDelegate* delegate) const; 151 152 private: 153 std::vector<char> CreateTfLiteModel(tflite::BuiltinOperator pool_op) const; 154 Padding()155 inline ::tflite::Padding Padding() const { return padding_; } 156 Activation()157 inline ::tflite::ActivationFunctionType Activation() const { 158 return activation_; 159 } 160 161 int32_t batch_size_ = 1; 162 int32_t channels_ = 1; 163 int32_t input_height_ = 1; 164 int32_t input_width_ = 1; 165 int32_t pooling_height_ = 1; 166 int32_t pooling_width_ = 1; 167 int32_t stride_height_ = 1; 168 int32_t stride_width_ = 1; 169 ::tflite::Padding padding_ = ::tflite::Padding_VALID; 170 ::tflite::ActivationFunctionType activation_ = 171 ::tflite::ActivationFunctionType_NONE; 172 }; 173 174 } // namespace xnnpack 175 } // namespace tflite 176 177 #endif // TENSORFLOW_LITE_DELEGATES_XNNPACK_POOL_2D_TESTER_H_ 178