aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/kernels/topk_v2_test.cc
blob: 16106fdafeeaaaf5206c2f4771f68a5075186e97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include <gtest/gtest.h>
#include "tensorflow/contrib/lite/c/builtin_op_data.h"
#include "tensorflow/contrib/lite/interpreter.h"
#include "tensorflow/contrib/lite/kernels/register.h"
#include "tensorflow/contrib/lite/kernels/test_util.h"
#include "tensorflow/contrib/lite/model.h"

namespace tflite {
namespace {

using ::testing::ElementsAreArray;

class TopKV2OpModel : public SingleOpModel {
 public:
  TopKV2OpModel(std::initializer_list<int> input_shape, TensorType input_type,
                int top_k) {
    input_ = AddInput(input_type);
    top_k_ = AddInput(TensorType_INT32);
    output_values_ = AddOutput(input_type);
    output_indexes_ = AddOutput(TensorType_INT32);
    SetBuiltinOp(BuiltinOperator_TOPK_V2, BuiltinOptions_TopKV2Options, 0);
    BuildInterpreter({input_shape, {1}});
    PopulateTensor<int32_t>(top_k_, {top_k});
  }

  void SetInputFloat(std::initializer_list<float> data) {
    PopulateTensor<float>(input_, data);
  }

  void SetInputUInt8(std::initializer_list<uint8_t> data) {
    PopulateTensor<uint8_t>(input_, data);
  }

  void SetInputInt32(std::initializer_list<int32_t> data) {
    PopulateTensor<int32_t>(input_, data);
  }

  void SetInputInt64(std::initializer_list<int64_t> data) {
    PopulateTensor<int64_t>(input_, data);
  }

  std::vector<int32_t> GetIndexes() {
    return ExtractVector<int32_t>(output_indexes_);
  }

  std::vector<float> GetValuesFloat() {
    return ExtractVector<float>(output_values_);
  }

  std::vector<uint8_t> GetValuesUInt8() {
    return ExtractVector<uint8_t>(output_values_);
  }

  std::vector<int32_t> GetValuesInt32() {
    return ExtractVector<int32_t>(output_values_);
  }

  std::vector<int64_t> GetValuesInt64() {
    return ExtractVector<int64_t>(output_values_);
  }

 protected:
  int input_;
  int top_k_;
  int output_indexes_;
  int output_values_;
};

// The test where the tensor dimension is equal to top.
TEST(TopKV2OpTest, EqualFloat) {
  TopKV2OpModel m({2, 2}, TensorType_FLOAT32, 2);
  m.SetInputFloat({-2.0, 0.2, 0.8, 0.1});
  m.Invoke();
  EXPECT_THAT(m.GetIndexes(), ElementsAreArray({1, 0, 0, 1}));
  EXPECT_THAT(m.GetValuesFloat(),
              ElementsAreArray(ArrayFloatNear({0.2, -2.0, 0.8, 0.1})));
}

// Test when internal dimension is k+1.
TEST(TopKV2OpTest, BorderFloat) {
  TopKV2OpModel m({2, 3}, TensorType_FLOAT32, 2);
  m.SetInputFloat({-2.0, -3.0, 0.2, 0.8, 0.1, -0.1});
  m.Invoke();
  EXPECT_THAT(m.GetIndexes(), ElementsAreArray({2, 0, 0, 1}));
  EXPECT_THAT(m.GetValuesFloat(),
              ElementsAreArray(ArrayFloatNear({0.2, -2.0, 0.8, 0.1})));
}
// Test when internal dimension is higher than k.
TEST(TopKV2OpTest, LargeFloat) {
  TopKV2OpModel m({2, 4}, TensorType_FLOAT32, 2);
  m.SetInputFloat({-2.0, -3.0, -4.0, 0.2, 0.8, 0.1, -0.1, -0.8});
  m.Invoke();
  EXPECT_THAT(m.GetIndexes(), ElementsAreArray({3, 0, 0, 1}));
  EXPECT_THAT(m.GetValuesFloat(),
              ElementsAreArray(ArrayFloatNear({0.2, -2.0, 0.8, 0.1})));
}

// Test 1D case.
TEST(TopKV2OpTest, VectorFloat) {
  TopKV2OpModel m({8}, TensorType_FLOAT32, 2);
  m.SetInputFloat({-2.0, -3.0, -4.0, 0.2, 0.8, 0.1, -0.1, -0.8});
  m.Invoke();
  EXPECT_THAT(m.GetIndexes(), ElementsAreArray({4, 3}));
  EXPECT_THAT(m.GetValuesFloat(), ElementsAreArray(ArrayFloatNear({0.8, 0.2})));
}

// Check that uint8_t works.
TEST(TopKV2OpTest, TypeUint8) {
  TopKV2OpModel m({2, 3}, TensorType_UINT8, 2);
  m.SetInputUInt8({1, 2, 3, 251, 250, 249});
  m.Invoke();
  EXPECT_THAT(m.GetIndexes(), ElementsAreArray({2, 1, 0, 1}));
  EXPECT_THAT(m.GetValuesUInt8(), ElementsAreArray({3, 2, 251, 250}));
}

// Check that int32_t works.
TEST(TopKV2OpTest, TypeInt32) {
  TopKV2OpModel m({2, 3}, TensorType_INT32, 2);
  m.SetInputInt32({1, 2, 3, 10251, 10250, 10249});
  m.Invoke();
  EXPECT_THAT(m.GetIndexes(), ElementsAreArray({2, 1, 0, 1}));
  EXPECT_THAT(m.GetValuesInt32(), ElementsAreArray({3, 2, 10251, 10250}));
}

// Check that int64 works.
TEST(TopKV2OpTest, TypeInt64) {
  TopKV2OpModel m({2, 3}, TensorType_INT64, 2);
  m.SetInputInt64({1, 2, 3, -1, -2, -3});
  m.Invoke();
  EXPECT_THAT(m.GetIndexes(), ElementsAreArray({2, 1, 0, 1}));
  EXPECT_THAT(m.GetValuesInt64(), ElementsAreArray({3, 2, -1, -2}));
}
}  // namespace
}  // namespace tflite

int main(int argc, char** argv) {
  ::tflite::LogToStderr();
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}