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