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// Unit test for TFLite Lookup op.
16
17#include <iomanip>
18#include <vector>
19
20#include <gmock/gmock.h>
21#include <gtest/gtest.h>
22#include "tensorflow/contrib/lite/interpreter.h"
23#include "tensorflow/contrib/lite/kernels/register.h"
24#include "tensorflow/contrib/lite/kernels/test_util.h"
25#include "tensorflow/contrib/lite/model.h"
26
27namespace tflite {
28namespace {
29
30using ::testing::ElementsAreArray;
31
32class EmbeddingLookupOpModel : public SingleOpModel {
33 public:
34  EmbeddingLookupOpModel(std::initializer_list<int> index_shape,
35                         std::initializer_list<int> weight_shape) {
36    input_ = AddInput(TensorType_INT32);
37    weight_ = AddInput(TensorType_FLOAT32);
38    output_ = AddOutput(TensorType_FLOAT32);
39    SetBuiltinOp(BuiltinOperator_EMBEDDING_LOOKUP, BuiltinOptions_NONE, 0);
40    BuildInterpreter({index_shape, weight_shape});
41  }
42
43  void SetInput(std::initializer_list<int> data) {
44    PopulateTensor(input_, data);
45  }
46
47  void Set3DWeightMatrix(const std::function<float(int, int, int)>& function) {
48    TfLiteTensor* tensor = interpreter_->tensor(weight_);
49    int rows = tensor->dims->data[0];
50    int columns = tensor->dims->data[1];
51    int features = tensor->dims->data[2];
52    for (int i = 0; i < rows; i++) {
53      for (int j = 0; j < columns; j++) {
54        for (int k = 0; k < features; k++) {
55          tensor->data.f[(i * columns + j) * features + k] = function(i, j, k);
56        }
57      }
58    }
59  }
60
61  std::vector<float> GetOutput() { return ExtractVector<float>(output_); }
62
63 private:
64  int input_;
65  int weight_;
66  int output_;
67};
68
69// TODO(ahentz): write more tests that exercise the details of the op, such as
70// lookup errors and variable input shapes.
71TEST(EmbeddingLookupOpTest, SimpleTest) {
72  EmbeddingLookupOpModel m({3}, {3, 2, 4});
73  m.PopulateTensor<int>(0, {1, 0, 2});
74  m.Set3DWeightMatrix(
75      [](int i, int j, int k) { return i + j / 10.0f + k / 100.0f; });
76
77  m.Invoke();
78
79  EXPECT_THAT(m.GetOutput(),
80              ElementsAreArray(ArrayFloatNear({
81                  1.00, 1.01, 1.02, 1.03, 1.10, 1.11, 1.12, 1.13,  // Row 1
82                  0.00, 0.01, 0.02, 0.03, 0.10, 0.11, 0.12, 0.13,  // Row 0
83                  2.00, 2.01, 2.02, 2.03, 2.10, 2.11, 2.12, 2.13,  // Row 2
84              })));
85}
86
87}  // namespace
88}  // namespace tflite
89
90int main(int argc, char** argv) {
91  ::tflite::LogToStderr();
92  ::testing::InitGoogleTest(&argc, argv);
93  return RUN_ALL_TESTS();
94}
95