• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 // Unit test for TFLite sparse lookup op.
16 
17 #include <cmath>
18 #include <functional>
19 #include <initializer_list>
20 #include <memory>
21 #include <vector>
22 
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 #include "flatbuffers/flatbuffers.h"  // from @flatbuffers
26 #include "tensorflow/lite/interpreter.h"
27 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
28 #include "tensorflow/lite/kernels/test_util.h"
29 #include "tensorflow/lite/schema/schema_generated.h"
30 
31 namespace tflite {
32 namespace {
33 
34 using ::testing::ElementsAreArray;
35 
36 class EmbeddingLookupSparseOpModel : public SingleOpModel {
37  public:
EmbeddingLookupSparseOpModel(CombinerType type,std::initializer_list<int> lookup_shape,std::initializer_list<int> indices_shape,std::initializer_list<int> dense_shape_shape,std::initializer_list<int> value_shape)38   EmbeddingLookupSparseOpModel(CombinerType type,
39                                std::initializer_list<int> lookup_shape,
40                                std::initializer_list<int> indices_shape,
41                                std::initializer_list<int> dense_shape_shape,
42                                std::initializer_list<int> value_shape) {
43     lookup_ = AddInput(TensorType_INT32);
44     indices_ = AddInput(TensorType_INT32);
45     dense_shape_ = AddInput(TensorType_INT32);
46     weights_ = AddInput(TensorType_FLOAT32);
47     value_ = AddInput(TensorType_FLOAT32);
48     output_ = AddOutput(TensorType_FLOAT32);
49     SetBuiltinOp(BuiltinOperator_EMBEDDING_LOOKUP_SPARSE,
50                  BuiltinOptions_EmbeddingLookupSparseOptions,
51                  CreateEmbeddingLookupSparseOptions(builder_, type).Union());
52     BuildInterpreter({lookup_shape, indices_shape, dense_shape_shape,
53                       lookup_shape, value_shape});
54   }
55 
SetInput(std::initializer_list<int> lookup_data,std::initializer_list<int> indices_data,std::initializer_list<int> dense_shape_data,std::initializer_list<float> weights_data)56   void SetInput(std::initializer_list<int> lookup_data,
57                 std::initializer_list<int> indices_data,
58                 std::initializer_list<int> dense_shape_data,
59                 std::initializer_list<float> weights_data) {
60     PopulateTensor(lookup_, lookup_data);
61     PopulateTensor(indices_, indices_data);
62     PopulateTensor(dense_shape_, dense_shape_data);
63     PopulateTensor(weights_, weights_data);
64   }
65 
Set3DWeightMatrix(const std::function<float (int,int,int)> & function)66   void Set3DWeightMatrix(const std::function<float(int, int, int)>& function) {
67     TfLiteTensor* tensor = interpreter_->tensor(value_);
68     int rows = tensor->dims->data[0];
69     int columns = tensor->dims->data[1];
70     int features = tensor->dims->data[2];
71     float* tensor_ptr = GetTensorData<float>(tensor);
72     for (int i = 0; i < rows; i++) {
73       for (int j = 0; j < columns; j++) {
74         for (int k = 0; k < features; k++) {
75           tensor_ptr[(i * columns + j) * features + k] = function(i, j, k);
76         }
77       }
78     }
79   }
80 
GetOutput()81   std::vector<float> GetOutput() { return ExtractVector<float>(output_); }
82 
83  private:
84   int lookup_;
85   int weights_;
86   int indices_;
87   int dense_shape_;
88   int value_;
89   int output_;
90 };
91 
TEST(EmbeddingLookupSparseOpTest,SimpleTest)92 TEST(EmbeddingLookupSparseOpTest, SimpleTest) {
93   EmbeddingLookupSparseOpModel m(CombinerType_SUM, {3}, {3, 2}, {2}, {4, 3, 2});
94   m.SetInput({1, 3, 0}, {0, 0, 2, 0, 2, 1}, {3, 2}, {1.0, 2.0, 4.0});
95   m.Set3DWeightMatrix(
96       [](int i, int j, int k) { return i + j / 10.0f + k / 100.0f; });
97   m.Invoke();
98 
99   EXPECT_THAT(m.GetOutput(),
100               ElementsAreArray(ArrayFloatNear({
101                   1.00, 1.01, 1.10, 1.11, 1.20, 1.21,  // Row 1
102                   0.00, 0.00, 0.00, 0.00, 0.00, 0.00,  // -
103                   6.00, 6.06, 6.60, 6.66, 7.20, 7.26,  // 2 * Row 3 + 4 * Row 0
104               })));
105 }
106 
TEST(EmbeddingLookupSparseOpTest,SimpleTestMean)107 TEST(EmbeddingLookupSparseOpTest, SimpleTestMean) {
108   EmbeddingLookupSparseOpModel m(CombinerType_MEAN, {3}, {3, 2}, {2},
109                                  {4, 3, 2});
110   m.SetInput({1, 3, 0}, {0, 0, 2, 0, 2, 1}, {3, 2}, {1.0, 2.0, 4.0});
111   m.Set3DWeightMatrix(
112       [](int i, int j, int k) { return i + j / 10.0f + k / 100.0f; });
113   m.Invoke();
114 
115   EXPECT_THAT(m.GetOutput(),
116               ElementsAreArray(ArrayFloatNear({
117                   1.00, 1.01, 1.10, 1.11, 1.20, 1.21,  // Row 1
118                   0.00, 0.00, 0.00, 0.00, 0.00, 0.00,  // -
119                   1.00, 1.01, 1.10, 1.11, 1.20, 1.21,  // 2 * Row 3 + 4 * Row 0
120               })));
121 }
122 
TEST(EmbeddingLookupSparseOpTest,SimpleTestSqrtn)123 TEST(EmbeddingLookupSparseOpTest, SimpleTestSqrtn) {
124   EmbeddingLookupSparseOpModel m(CombinerType_SQRTN, {3}, {3, 2}, {2},
125                                  {4, 3, 2});
126   m.SetInput({1, 3, 0}, {0, 0, 2, 0, 2, 1}, {3, 2}, {1.0, 2.0, 4.0});
127   m.Set3DWeightMatrix(
128       [](int i, int j, int k) { return i + j / 10.0f + k / 100.0f; });
129   m.Invoke();
130 
131   EXPECT_THAT(m.GetOutput(),
132               ElementsAreArray(ArrayFloatNear({
133                   1.00, 1.01, 1.10, 1.11, 1.20, 1.21,  // Row 1
134                   0.00, 0.00, 0.00, 0.00, 0.00, 0.00,  // -
135                   6.00f / std::sqrt(20.0f), 6.06f / std::sqrt(20.0f),
136                   6.60f / std::sqrt(20.0f), 6.66f / std::sqrt(20.0f),
137                   7.20f / std::sqrt(20.0f),
138                   7.26f / std::sqrt(20.0f),  // 2 * Row 3 + 4 * Row 0,  // 2 *
139                                              // Row 3 + 4 * Row 0
140               })));
141 }
142 
TEST(EmbeddingLookupSparseOpTest,Indices3DTest)143 TEST(EmbeddingLookupSparseOpTest, Indices3DTest) {
144   EmbeddingLookupSparseOpModel m(CombinerType_SUM, {3}, {3, 3}, {3}, {4, 3, 2});
145   m.SetInput({1, 3, 0}, {0, 0, 0, 2, 0, 0, 2, 0, 1}, {3, 2, 2},
146              {1.0, 2.0, 4.0});
147   m.Set3DWeightMatrix(
148       [](int i, int j, int k) { return i + j / 10.0f + k / 100.0f; });
149   m.Invoke();
150 
151   EXPECT_THAT(m.GetOutput(),
152               ElementsAreArray(ArrayFloatNear({
153                   1.00, 1.01, 1.10, 1.11, 1.20, 1.21, 0.00, 0.00, 0.00,
154                   0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
155                   0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 6.00, 6.06, 6.60,
156                   6.66, 7.20, 7.26, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
157               })));
158 }
159 
160 }  // namespace
161 }  // namespace tflite
162