1 /*
2 * Copyright (C) 2017 The Android Open Source Project
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
17 #include "EmbeddingLookup.h"
18
19 #include "NeuralNetworksWrapper.h"
20
21 #include <gmock/gmock-matchers.h>
22 #include <gtest/gtest.h>
23
24 using ::testing::FloatNear;
25 using ::testing::Matcher;
26
27 namespace android {
28 namespace nn {
29 namespace wrapper {
30
31 namespace {
32
ArrayFloatNear(const std::vector<float> & values,float max_abs_error=1.e-6)33 std::vector<Matcher<float>> ArrayFloatNear(const std::vector<float>& values,
34 float max_abs_error = 1.e-6) {
35 std::vector<Matcher<float>> matchers;
36 matchers.reserve(values.size());
37 for (const float& v : values) {
38 matchers.emplace_back(FloatNear(v, max_abs_error));
39 }
40 return matchers;
41 }
42
43 } // namespace
44
45 using ::testing::ElementsAreArray;
46
47 #define FOR_ALL_INPUT_AND_WEIGHT_TENSORS(ACTION) \
48 ACTION(Value, float) \
49 ACTION(Lookup, int)
50
51 // For all output and intermediate states
52 #define FOR_ALL_OUTPUT_TENSORS(ACTION) ACTION(Output, float)
53
54 class EmbeddingLookupOpModel {
55 public:
EmbeddingLookupOpModel(std::initializer_list<uint32_t> index_shape,std::initializer_list<uint32_t> weight_shape)56 EmbeddingLookupOpModel(std::initializer_list<uint32_t> index_shape,
57 std::initializer_list<uint32_t> weight_shape) {
58 auto it = weight_shape.begin();
59 rows_ = *it++;
60 columns_ = *it++;
61 features_ = *it;
62
63 std::vector<uint32_t> inputs;
64
65 OperandType LookupTy(Type::TENSOR_INT32, index_shape);
66 inputs.push_back(model_.addOperand(&LookupTy));
67
68 OperandType ValueTy(Type::TENSOR_FLOAT32, weight_shape);
69 inputs.push_back(model_.addOperand(&ValueTy));
70
71 std::vector<uint32_t> outputs;
72
73 OperandType OutputOpndTy(Type::TENSOR_FLOAT32, weight_shape);
74 outputs.push_back(model_.addOperand(&OutputOpndTy));
75
76 auto multiAll = [](const std::vector<uint32_t>& dims) -> uint32_t {
77 uint32_t sz = 1;
78 for (uint32_t d : dims) {
79 sz *= d;
80 }
81 return sz;
82 };
83
84 Value_.insert(Value_.end(), multiAll(weight_shape), 0.f);
85 Output_.insert(Output_.end(), multiAll(weight_shape), 0.f);
86
87 model_.addOperation(ANEURALNETWORKS_EMBEDDING_LOOKUP, inputs, outputs);
88 model_.identifyInputsAndOutputs(inputs, outputs);
89
90 model_.finish();
91 }
92
Invoke()93 void Invoke() {
94 ASSERT_TRUE(model_.isValid());
95
96 Compilation compilation(&model_);
97 compilation.finish();
98 Execution execution(&compilation);
99
100 #define SetInputOrWeight(X, T) \
101 ASSERT_EQ(execution.setInput(EmbeddingLookup::k##X##Tensor, X##_.data(), \
102 sizeof(T) * X##_.size()), \
103 Result::NO_ERROR);
104
105 FOR_ALL_INPUT_AND_WEIGHT_TENSORS(SetInputOrWeight);
106
107 #undef SetInputOrWeight
108
109 #define SetOutput(X, T) \
110 ASSERT_EQ(execution.setOutput(EmbeddingLookup::k##X##Tensor, X##_.data(), \
111 sizeof(T) * X##_.size()), \
112 Result::NO_ERROR);
113
114 FOR_ALL_OUTPUT_TENSORS(SetOutput);
115
116 #undef SetOutput
117
118 ASSERT_EQ(execution.compute(), Result::NO_ERROR);
119 }
120
121 #define DefineSetter(X, T) \
122 void Set##X(const std::vector<T>& f) { X##_.insert(X##_.end(), f.begin(), f.end()); }
123
124 FOR_ALL_INPUT_AND_WEIGHT_TENSORS(DefineSetter);
125
126 #undef DefineSetter
127
Set3DWeightMatrix(const std::function<float (int,int,int)> & function)128 void Set3DWeightMatrix(const std::function<float(int, int, int)>& function) {
129 for (uint32_t i = 0; i < rows_; i++) {
130 for (uint32_t j = 0; j < columns_; j++) {
131 for (uint32_t k = 0; k < features_; k++) {
132 Value_[(i * columns_ + j) * features_ + k] = function(i, j, k);
133 }
134 }
135 }
136 }
137
GetOutput() const138 const std::vector<float>& GetOutput() const { return Output_; }
139
140 private:
141 Model model_;
142 uint32_t rows_;
143 uint32_t columns_;
144 uint32_t features_;
145
146 #define DefineTensor(X, T) std::vector<T> X##_;
147
148 FOR_ALL_INPUT_AND_WEIGHT_TENSORS(DefineTensor);
149 FOR_ALL_OUTPUT_TENSORS(DefineTensor);
150
151 #undef DefineTensor
152 };
153
154 // TODO: write more tests that exercise the details of the op, such as
155 // lookup errors and variable input shapes.
TEST(EmbeddingLookupOpTest,SimpleTest)156 TEST(EmbeddingLookupOpTest, SimpleTest) {
157 EmbeddingLookupOpModel m({3}, {3, 2, 4});
158 m.SetLookup({1, 0, 2});
159 m.Set3DWeightMatrix([](int i, int j, int k) { return i + j / 10.0f + k / 100.0f; });
160
161 m.Invoke();
162
163 EXPECT_THAT(m.GetOutput(), ElementsAreArray(ArrayFloatNear({
164 1.00, 1.01, 1.02, 1.03, 1.10, 1.11, 1.12, 1.13, // Row 1
165 0.00, 0.01, 0.02, 0.03, 0.10, 0.11, 0.12, 0.13, // Row 0
166 2.00, 2.01, 2.02, 2.03, 2.10, 2.11, 2.12, 2.13, // Row 2
167 })));
168 }
169
170 } // namespace wrapper
171 } // namespace nn
172 } // namespace android
173