1 2 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 ==============================================================================*/ 16 #include <gtest/gtest.h> 17 #include "tensorflow/contrib/lite/builtin_op_data.h" 18 #include "tensorflow/contrib/lite/interpreter.h" 19 #include "tensorflow/contrib/lite/kernels/register.h" 20 #include "tensorflow/contrib/lite/kernels/test_util.h" 21 #include "tensorflow/contrib/lite/model.h" 22 23 namespace tflite { 24 namespace { 25 26 using ::testing::ElementsAreArray; 27 28 class TopKV2OpModel : public SingleOpModel { 29 public: 30 TopKV2OpModel(std::initializer_list<int> input_shape, TensorType input_type, 31 int top_k) { 32 input_ = AddInput(input_type); 33 top_k_ = AddInput(TensorType_INT32); 34 output_indexes_ = AddOutput(TensorType_INT32); 35 output_values_ = AddOutput(input_type); 36 SetBuiltinOp(BuiltinOperator_TOPK_V2, BuiltinOptions_TopKV2Options, 0); 37 BuildInterpreter({input_shape, {1}}); 38 PopulateTensor<int32_t>(top_k_, {top_k}); 39 } 40 41 void SetInputFloat(std::initializer_list<float> data) { 42 PopulateTensor<float>(input_, data); 43 } 44 45 void SetInputUInt8(std::initializer_list<uint8> data) { 46 PopulateTensor<uint8>(input_, data); 47 } 48 49 void SetInputInt32(std::initializer_list<int32> data) { 50 PopulateTensor<int32>(input_, data); 51 } 52 53 void SetInputInt64(std::initializer_list<int64_t> data) { 54 PopulateTensor<int64_t>(input_, data); 55 } 56 57 std::vector<int32> GetIndexes() { 58 return ExtractVector<int32>(output_indexes_); 59 } 60 61 std::vector<float> GetValuesFloat() { 62 return ExtractVector<float>(output_values_); 63 } 64 65 std::vector<uint8> GetValuesUInt8() { 66 return ExtractVector<uint8>(output_values_); 67 } 68 69 std::vector<int32> GetValuesInt32() { 70 return ExtractVector<int32>(output_values_); 71 } 72 73 std::vector<int64_t> GetValuesInt64() { 74 return ExtractVector<int64_t>(output_values_); 75 } 76 77 protected: 78 int input_; 79 int top_k_; 80 int output_indexes_; 81 int output_values_; 82 }; 83 84 // The test where the tensor dimension is equal to top. 85 TEST(TopKV2OpTest, EqualFloat) { 86 TopKV2OpModel m({2, 2}, TensorType_FLOAT32, 2); 87 m.SetInputFloat({-2.0, 0.2, 0.8, 0.1}); 88 m.Invoke(); 89 EXPECT_THAT(m.GetIndexes(), ElementsAreArray({1, 0, 0, 1})); 90 EXPECT_THAT(m.GetValuesFloat(), 91 ElementsAreArray(ArrayFloatNear({0.2, -2.0, 0.8, 0.1}))); 92 } 93 94 // Test when internal dimension is k+1. 95 TEST(TopKV2OpTest, BorderFloat) { 96 TopKV2OpModel m({2, 3}, TensorType_FLOAT32, 2); 97 m.SetInputFloat({-2.0, -3.0, 0.2, 0.8, 0.1, -0.1}); 98 m.Invoke(); 99 EXPECT_THAT(m.GetIndexes(), ElementsAreArray({2, 0, 0, 1})); 100 EXPECT_THAT(m.GetValuesFloat(), 101 ElementsAreArray(ArrayFloatNear({0.2, -2.0, 0.8, 0.1}))); 102 } 103 // Test when internal dimension is higher than k. 104 TEST(TopKV2OpTest, LargeFloat) { 105 TopKV2OpModel m({2, 4}, TensorType_FLOAT32, 2); 106 m.SetInputFloat({-2.0, -3.0, -4.0, 0.2, 0.8, 0.1, -0.1, -0.8}); 107 m.Invoke(); 108 EXPECT_THAT(m.GetIndexes(), ElementsAreArray({3, 0, 0, 1})); 109 EXPECT_THAT(m.GetValuesFloat(), 110 ElementsAreArray(ArrayFloatNear({0.2, -2.0, 0.8, 0.1}))); 111 } 112 113 // Test 1D case. 114 TEST(TopKV2OpTest, VectorFloat) { 115 TopKV2OpModel m({8}, TensorType_FLOAT32, 2); 116 m.SetInputFloat({-2.0, -3.0, -4.0, 0.2, 0.8, 0.1, -0.1, -0.8}); 117 m.Invoke(); 118 EXPECT_THAT(m.GetIndexes(), ElementsAreArray({4, 3})); 119 EXPECT_THAT(m.GetValuesFloat(), ElementsAreArray(ArrayFloatNear({0.8, 0.2}))); 120 } 121 122 // Check that uint8 works. 123 TEST(TopKV2OpTest, TypeUint8) { 124 TopKV2OpModel m({2, 3}, TensorType_UINT8, 2); 125 m.SetInputUInt8({1, 2, 3, 251, 250, 249}); 126 m.Invoke(); 127 EXPECT_THAT(m.GetIndexes(), ElementsAreArray({2, 1, 0, 1})); 128 EXPECT_THAT(m.GetValuesUInt8(), ElementsAreArray({3, 2, 251, 250})); 129 } 130 131 // Check that int32 works. 132 TEST(TopKV2OpTest, TypeInt32) { 133 TopKV2OpModel m({2, 3}, TensorType_INT32, 2); 134 m.SetInputInt32({1, 2, 3, 10251, 10250, 10249}); 135 m.Invoke(); 136 EXPECT_THAT(m.GetIndexes(), ElementsAreArray({2, 1, 0, 1})); 137 EXPECT_THAT(m.GetValuesInt32(), ElementsAreArray({3, 2, 10251, 10250})); 138 } 139 140 // Check that int64 works. 141 TEST(TopKV2OpTest, TypeInt64) { 142 TopKV2OpModel m({2, 3}, TensorType_INT64, 2); 143 m.SetInputInt64({1, 2, 3, -1, -2, -3}); 144 m.Invoke(); 145 EXPECT_THAT(m.GetIndexes(), ElementsAreArray({2, 1, 0, 1})); 146 EXPECT_THAT(m.GetValuesInt64(), ElementsAreArray({3, 2, -1, -2})); 147 } 148 } // namespace 149 } // namespace tflite 150 151 int main(int argc, char** argv) { 152 ::tflite::LogToStderr(); 153 ::testing::InitGoogleTest(&argc, argv); 154 return RUN_ALL_TESTS(); 155 } 156