1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#include <gtest/gtest.h>
16#include "tensorflow/contrib/lite/builtin_op_data.h"
17#include "tensorflow/contrib/lite/interpreter.h"
18#include "tensorflow/contrib/lite/kernels/register.h"
19#include "tensorflow/contrib/lite/kernels/test_util.h"
20#include "tensorflow/contrib/lite/model.h"
21
22namespace tflite {
23namespace {
24
25using ::testing::ElementsAreArray;
26
27class GatherOpModel : public SingleOpModel {
28 public:
29  GatherOpModel(std::initializer_list<int> input_shape, TensorType input_type,
30                std::initializer_list<int> positions_shape) {
31    input_ = AddInput(input_type);
32    positions_ = AddInput(TensorType_INT32);
33    output_ = AddOutput(input_type);
34    SetBuiltinOp(BuiltinOperator_GATHER, BuiltinOptions_GatherOptions,
35                 CreateGatherOptions(builder_, 0).Union());
36    BuildInterpreter({input_shape, positions_shape});
37  }
38
39  void SetInputFloat(std::initializer_list<float> data) {
40    PopulateTensor<float>(input_, data);
41  }
42
43  void SetInputUint8(std::initializer_list<uint8_t> data) {
44    PopulateTensor<uint8_t>(input_, data);
45  }
46
47  void SetInput(std::initializer_list<string> data) {
48    PopulateStringTensor(input_, data);
49  }
50
51  void SetPositions(std::initializer_list<int> data) {
52    PopulateTensor<int>(positions_, data);
53  }
54
55  std::vector<float> GetOutputFloat() { return ExtractVector<float>(output_); }
56  std::vector<uint8_t> GetOutputUint8() {
57    return ExtractVector<uint8_t>(output_);
58  }
59  std::vector<string> GetOutputString() {
60    return ExtractVector<string>(output_);
61  }
62  std::vector<int> GetOutputShape() { return GetTensorShape(output_); }
63
64 protected:
65  int input_;
66  int positions_;
67  int output_;
68};
69
70TEST(GatherOpTest, Shuffle) {
71  GatherOpModel m({2, 2}, TensorType_FLOAT32, {2});
72  m.SetInputFloat({-2.0, 0.2, 0.7, 0.8});
73  m.SetPositions({1, 0});
74  m.Invoke();
75  EXPECT_THAT(m.GetOutputFloat(),
76              ElementsAreArray(ArrayFloatNear({0.7, 0.8, -2, 0.2})));
77}
78
79TEST(GatherOpTest, Test0DIndex) {
80  GatherOpModel m({2, 2}, TensorType_FLOAT32, {});
81  m.SetInputFloat({-2.0, 0.2, 0.7, 0.8});
82  m.SetPositions({1});
83  m.Invoke();
84  EXPECT_THAT(m.GetOutputFloat(), ElementsAreArray(ArrayFloatNear({0.7, 0.8})));
85  EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({2}));
86}
87
88TEST(GatherOpTest, Test0DIndexWith0DResult) {
89  // 0D tensor is special case in current TFLite. Test it once to make sure
90  // existing workarounds are fine with it.
91  GatherOpModel m({3}, TensorType_FLOAT32, {});
92  m.SetInputFloat({1.0, 2.0, 3.0});
93  m.SetPositions({1});
94  m.Invoke();
95  EXPECT_THAT(m.GetOutputFloat(), ElementsAreArray(ArrayFloatNear({2.0})));
96  EXPECT_TRUE(m.GetOutputShape().empty());
97}
98
99TEST(FloatGatherOpTest, Duplicate) {
100  GatherOpModel m({1, 2, 2}, TensorType_FLOAT32, {2});
101  m.SetInputFloat({-2.0, 0.2, 0.7, 0.8});
102  m.SetPositions({0, 0});
103  m.Invoke();
104  EXPECT_THAT(
105      m.GetOutputFloat(),
106      ElementsAreArray(ArrayFloatNear({-2, 0.2, 0.7, 0.8, -2, 0.2, 0.7, 0.8})));
107}
108
109TEST(FloatGatherOpTest, Slice) {
110  GatherOpModel m({4, 1}, TensorType_FLOAT32, {2});
111  m.SetInputFloat({-2.0, 0.2, 0.7, 0.8});
112  m.SetPositions({1, 3});
113  m.Invoke();
114  EXPECT_THAT(m.GetOutputFloat(), ElementsAreArray(ArrayFloatNear({0.2, 0.8})));
115}
116
117TEST(Uint8tGatherOpTest, Shuffle) {
118  GatherOpModel m({2, 2}, TensorType_UINT8, {2});
119  m.SetInputUint8({133, 134, 14, 15});
120  m.SetPositions({1, 0});
121  m.Invoke();
122
123  EXPECT_THAT(m.GetOutputUint8(), ElementsAreArray({14, 15, 133, 134}));
124}
125
126TEST(GatherOpTest, SimpleString) {
127  GatherOpModel m({3}, TensorType_STRING, {2});
128  m.SetInput({"A", "B", "C"});
129  m.SetPositions({0, 2});
130  m.Invoke();
131  ASSERT_THAT(m.GetOutputShape(), ElementsAreArray({2}));
132  EXPECT_THAT(m.GetOutputString(), ElementsAreArray({"A", "C"}));
133}
134}  // namespace
135}  // namespace tflite
136
137int main(int argc, char** argv) {
138  ::tflite::LogToStderr();
139  ::testing::InitGoogleTest(&argc, argv);
140  return RUN_ALL_TESTS();
141}
142