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_FULLY_CONNECTED_TESTER_H_ 17 #define TENSORFLOW_LITE_DELEGATES_XNNPACK_FULLY_CONNECTED_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 FullyConnectedTester { 30 public: 31 FullyConnectedTester() = default; 32 FullyConnectedTester(const FullyConnectedTester&) = delete; 33 FullyConnectedTester& operator=(const FullyConnectedTester&) = delete; 34 InputShape(std::initializer_list<int32_t> shape)35 inline FullyConnectedTester& InputShape( 36 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_ = 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 InputChannels(int32_t input_channels)49 inline FullyConnectedTester& InputChannels(int32_t input_channels) { 50 EXPECT_GT(input_channels, 0); 51 input_channels_ = input_channels; 52 return *this; 53 } 54 InputChannels()55 inline int32_t InputChannels() const { return input_channels_; } 56 OutputChannels(int32_t output_channels)57 inline FullyConnectedTester& OutputChannels(int32_t output_channels) { 58 EXPECT_GT(output_channels, 0); 59 output_channels_ = output_channels; 60 return *this; 61 } 62 OutputChannels()63 inline int32_t OutputChannels() const { return output_channels_; } 64 65 std::vector<int32_t> OutputShape() const; 66 KeepDims(bool keep_dims)67 inline FullyConnectedTester& KeepDims(bool keep_dims) { 68 keep_dims_ = keep_dims; 69 return *this; 70 } 71 KeepDims()72 inline bool KeepDims() const { return keep_dims_; } 73 FP16Weights()74 inline FullyConnectedTester& FP16Weights() { 75 fp16_weights_ = true; 76 return *this; 77 } 78 FP16Weights()79 inline bool FP16Weights() const { return fp16_weights_; } 80 ReluActivation()81 inline FullyConnectedTester& ReluActivation() { 82 activation_ = ::tflite::ActivationFunctionType_RELU; 83 return *this; 84 } 85 Relu6Activation()86 inline FullyConnectedTester& Relu6Activation() { 87 activation_ = ::tflite::ActivationFunctionType_RELU6; 88 return *this; 89 } 90 ReluMinus1To1Activation()91 inline FullyConnectedTester& ReluMinus1To1Activation() { 92 activation_ = ::tflite::ActivationFunctionType_RELU_N1_TO_1; 93 return *this; 94 } 95 96 void Test(TfLiteDelegate* delegate) const; 97 98 private: 99 std::vector<char> CreateTfLiteModel() const; 100 Activation()101 inline ::tflite::ActivationFunctionType Activation() const { 102 return activation_; 103 } 104 105 static int32_t ComputeSize(const std::vector<int32_t>& shape); 106 107 std::vector<int32_t> input_shape_; 108 int32_t input_size_ = 1; 109 int32_t input_channels_ = 1; 110 int32_t output_channels_ = 1; 111 bool keep_dims_ = false; 112 bool fp16_weights_ = false; 113 ::tflite::ActivationFunctionType activation_ = 114 ::tflite::ActivationFunctionType_NONE; 115 }; 116 117 } // namespace xnnpack 118 } // namespace tflite 119 120 #endif // TENSORFLOW_LITE_DELEGATES_XNNPACK_FULLY_CONNECTED_TESTER_H_ 121