1
2 /* Copyright 2018 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 <cstdarg>
17 #include <gtest/gtest.h>
18 #include "tensorflow/lite/interpreter.h"
19 #include "tensorflow/lite/kernels/register.h"
20 #include "tensorflow/lite/kernels/test_util.h"
21 #include "tensorflow/lite/model.h"
22
23 namespace tflite {
24 namespace {
25
26 using ::testing::ElementsAreArray;
27
28 template <typename T>
29 class SparseToDenseOpModel : public SingleOpModel {
30 public:
SparseToDenseOpModel(std::initializer_list<int> indices_shape,std::initializer_list<int> output_shape_shape,std::initializer_list<int> values_shape,T default_value,TensorType tensor_index_type,TensorType tensor_input_type)31 SparseToDenseOpModel(std::initializer_list<int> indices_shape,
32 std::initializer_list<int> output_shape_shape,
33 std::initializer_list<int> values_shape, T default_value,
34 TensorType tensor_index_type,
35 TensorType tensor_input_type) {
36 indices_ = AddInput(tensor_index_type);
37 output_shape_ = AddInput(TensorType_INT32);
38 values_ = AddInput(tensor_input_type);
39 default_value_ = AddInput(tensor_input_type);
40 output_ = AddOutput(tensor_input_type);
41
42 SetBuiltinOp(BuiltinOperator_SPARSE_TO_DENSE,
43 BuiltinOptions_SparseToDenseOptions,
44 CreateSparseToDenseOptions(builder_, false).Union());
45 BuildInterpreter({indices_shape, output_shape_shape, values_shape, {1}});
46
47 PopulateTensor<T>(default_value_, {default_value});
48 }
49
indices()50 int indices() { return indices_; }
output_shape()51 int output_shape() { return output_shape_; }
values()52 int values() { return values_; }
53
GetOutput()54 std::vector<T> GetOutput() { return ExtractVector<T>(output_); }
GetOutputShape()55 std::vector<int> GetOutputShape() { return GetTensorShape(output_); }
56
57 private:
58 int indices_;
59 int output_shape_;
60 int values_;
61 int default_value_;
62 int output_;
63 };
64
TEST(SparseToDenseOpModelTest,ZeroDimensionTest)65 TEST(SparseToDenseOpModelTest, ZeroDimensionTest) {
66 SparseToDenseOpModel<float> m({1}, {1}, {1}, 0, TensorType_INT32,
67 TensorType_FLOAT32);
68 m.PopulateTensor<int32_t>(m.indices(), {3});
69 m.PopulateTensor<int32_t>(m.output_shape(), {5});
70 m.PopulateTensor<float>(m.values(), {7});
71 m.Invoke();
72
73 EXPECT_THAT(m.GetOutput(), ElementsAreArray({0, 0, 0, 7, 0}));
74 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({5}));
75 }
76
TEST(SparseToDenseOpModelTest,OneDimensionTest)77 TEST(SparseToDenseOpModelTest, OneDimensionTest) {
78 SparseToDenseOpModel<float> m({3}, {1}, {3}, 0, TensorType_INT32,
79 TensorType_FLOAT32);
80 m.PopulateTensor<int32_t>(m.indices(), {1, 3, 5});
81 m.PopulateTensor<int32_t>(m.output_shape(), {7});
82 m.PopulateTensor<float>(m.values(), {2, 4, 6});
83 m.Invoke();
84
85 EXPECT_THAT(m.GetOutput(), ElementsAreArray({0, 2, 0, 4, 0, 6, 0}));
86 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({7}));
87 }
88
TEST(SparseToDenseOpModelTest,TwoDimensionsTest)89 TEST(SparseToDenseOpModelTest, TwoDimensionsTest) {
90 SparseToDenseOpModel<float> m({3, 3}, {3}, {3}, 0, TensorType_INT32,
91 TensorType_FLOAT32);
92 m.PopulateTensor<int32_t>(m.indices(), {0, 0, 0, 1, 2, 1, 2, 0, 1});
93 m.PopulateTensor<int32_t>(m.output_shape(), {3, 3, 3});
94 m.PopulateTensor<float>(m.values(), {2, 4, 6});
95 m.Invoke();
96
97 EXPECT_THAT(m.GetOutput(),
98 ElementsAreArray({2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99 0, 0, 4, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0}));
100 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({3, 3, 3}));
101 }
102
TEST(SparseToDenseOpModelTest,DefaultValueTest)103 TEST(SparseToDenseOpModelTest, DefaultValueTest) {
104 SparseToDenseOpModel<float> m({3, 3}, {3}, {3}, -1, TensorType_INT32,
105 TensorType_FLOAT32);
106 m.PopulateTensor<int32_t>(m.indices(), {0, 0, 0, 1, 2, 1, 2, 0, 1});
107 m.PopulateTensor<int32_t>(m.output_shape(), {3, 3, 3});
108 m.PopulateTensor<float>(m.values(), {2, 4, 6});
109 m.Invoke();
110
111 EXPECT_THAT(
112 m.GetOutput(),
113 ElementsAreArray({2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
114 -1, -1, 4, -1, -1, 6, -1, -1, -1, -1, -1, -1, -1}));
115 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({3, 3, 3}));
116 }
117
TEST(SparseToDenseOpModelTest,IntegerValueTest)118 TEST(SparseToDenseOpModelTest, IntegerValueTest) {
119 SparseToDenseOpModel<int32_t> m({3, 3}, {3}, {3}, -1, TensorType_INT32,
120 TensorType_INT32);
121 m.PopulateTensor<int32_t>(m.indices(), {0, 0, 0, 1, 2, 1, 2, 0, 1});
122 m.PopulateTensor<int32_t>(m.output_shape(), {3, 3, 3});
123 m.PopulateTensor<int32_t>(m.values(), {2, 4, 6});
124 m.Invoke();
125
126 EXPECT_THAT(
127 m.GetOutput(),
128 ElementsAreArray({2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
129 -1, -1, 4, -1, -1, 6, -1, -1, -1, -1, -1, -1, -1}));
130 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({3, 3, 3}));
131 }
132
TEST(SparseToDenseOpModelTest,Int64IndexTest)133 TEST(SparseToDenseOpModelTest, Int64IndexTest) {
134 SparseToDenseOpModel<float> m({3, 3}, {3}, {3}, -1, TensorType_INT64,
135 TensorType_FLOAT32);
136 m.PopulateTensor<int64_t>(m.indices(), {0, 0, 0, 1, 2, 1, 2, 0, 1});
137 m.PopulateTensor<int32_t>(m.output_shape(), {3, 3, 3});
138 m.PopulateTensor<float>(m.values(), {2, 4, 6});
139 m.Invoke();
140
141 EXPECT_THAT(
142 m.GetOutput(),
143 ElementsAreArray({2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
144 -1, -1, 4, -1, -1, 6, -1, -1, -1, -1, -1, -1, -1}));
145 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({3, 3, 3}));
146 }
147
148 } // namespace
149 } // namespace tflite
150
main(int argc,char ** argv)151 int main(int argc, char** argv) {
152 ::tflite::LogToStderr();
153 ::testing::InitGoogleTest(&argc, argv);
154 return RUN_ALL_TESTS();
155 }
156