1/* Copyright 2018 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/interpreter.h"
17#include "tensorflow/contrib/lite/kernels/register.h"
18#include "tensorflow/contrib/lite/kernels/test_util.h"
19#include "tensorflow/contrib/lite/model.h"
20
21namespace tflite {
22namespace {
23
24using ::testing::ElementsAreArray;
25
26constexpr int kAxisIsATensor = -1000;
27
28class SplitOpModel : public SingleOpModel {
29 public:
30  SplitOpModel(const TensorData& input, int num_splits,
31               int axis = kAxisIsATensor) {
32    if (axis == kAxisIsATensor) {
33      axis_ = AddInput({TensorType_INT32, {1}});
34    } else {
35      axis_ = AddConstInput(TensorType_INT32, {axis}, {1});
36    }
37    input_ = AddInput(input);
38    for (int i = 0; i < num_splits; ++i) {
39      outputs_.push_back(AddOutput(input.type));
40    }
41    SetBuiltinOp(BuiltinOperator_SPLIT, BuiltinOptions_SplitOptions,
42                 CreateSplitOptions(builder_, num_splits).Union());
43    if (axis == kAxisIsATensor) {
44      BuildInterpreter({GetShape(axis_), GetShape(input_)});
45    } else {
46      BuildInterpreter({{}, GetShape(input_)});
47    }
48  }
49
50  void SetInput(std::initializer_list<float> data) {
51    PopulateTensor(input_, data);
52  }
53  void SetAxis(int axis) { PopulateTensor(axis_, {axis}); }
54
55  std::vector<float> GetOutput(int i) {
56    return ExtractVector<float>(outputs_[i]);
57  }
58  std::vector<int> GetOutputShape(int i) { return GetTensorShape(outputs_[i]); }
59
60 private:
61  int input_;
62  int axis_;
63  std::vector<int> outputs_;
64};
65
66using TensorValues = std::initializer_list<float>;
67
68void Check(int axis, int num_splits, std::initializer_list<int> input_shape,
69           std::initializer_list<int> output_shape,
70           const TensorValues& input_data,
71           const std::vector<TensorValues>& output_data) {
72  auto debug = [&](int i) {
73    std::stringstream ss;
74    ss << "for output tensor " << i << " axis=" << axis
75       << " and num_splits=" << num_splits;
76    return ss.str();
77  };
78  SplitOpModel m({TensorType_FLOAT32, input_shape}, num_splits);
79  m.SetInput(input_data);
80  m.SetAxis(axis);
81  m.Invoke();
82  for (int i = 0; i < num_splits; ++i) {
83    EXPECT_THAT(m.GetOutput(i), ElementsAreArray(output_data[i])) << debug(i);
84    EXPECT_THAT(m.GetOutputShape(i), ElementsAreArray(output_shape))
85        << debug(i);
86  }
87
88  SplitOpModel const_m({TensorType_FLOAT32, input_shape}, num_splits, axis);
89  const_m.SetInput(input_data);
90  const_m.Invoke();
91  for (int i = 0; i < num_splits; ++i) {
92    EXPECT_THAT(const_m.GetOutput(i), ElementsAreArray(output_data[i]))
93        << debug(i);
94    EXPECT_THAT(const_m.GetOutputShape(i), ElementsAreArray(output_shape))
95        << debug(i);
96  }
97}
98
99TEST(SplitOpTest, FourDimensional) {
100  Check(/*axis=*/0, /*num_splits=*/2, {2, 2, 2, 2}, {1, 2, 2, 2},
101        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
102        {
103            {1, 2, 3, 4, 5, 6, 7, 8},
104            {9, 10, 11, 12, 13, 14, 15, 16},
105        });
106  Check(/*axis=*/1, /*num_splits=*/2, {2, 2, 2, 2}, {2, 1, 2, 2},
107        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
108        {
109            {1, 2, 3, 4, 9, 10, 11, 12},
110            {5, 6, 7, 8, 13, 14, 15, 16},
111        });
112  Check(/*axis=*/2, /*num_splits=*/2, {2, 2, 2, 2}, {2, 2, 1, 2},
113        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
114        {
115            {1, 2, 5, 6, 9, 10, 13, 14},
116            {3, 4, 7, 8, 11, 12, 15, 16},
117        });
118  Check(/*axis=*/3, /*num_splits=*/2, {2, 2, 2, 2}, {2, 2, 2, 1},
119        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
120        {
121            {1, 3, 5, 7, 9, 11, 13, 15},
122            {2, 4, 6, 8, 10, 12, 14, 16},
123        });
124}
125
126TEST(SplitOpTest, OneDimensional) {
127  Check(/*axis=*/0, /*num_splits=*/8, {8}, {1}, {1, 2, 3, 4, 5, 6, 7, 8},
128        {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}});
129}
130
131TEST(SplitOpTest, NegativeAxis) {
132  Check(/*axis=*/-4, /*num_splits=*/2, {2, 2, 2, 2}, {1, 2, 2, 2},
133        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
134        {
135            {1, 2, 3, 4, 5, 6, 7, 8},
136            {9, 10, 11, 12, 13, 14, 15, 16},
137        });
138}
139
140}  // namespace
141}  // namespace tflite
142
143int main(int argc, char** argv) {
144  ::tflite::LogToStderr();
145  ::testing::InitGoogleTest(&argc, argv);
146  return RUN_ALL_TESTS();
147}
148