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 "HashtableLookup.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(Lookup, int) \
49 ACTION(Key, int) \
50 ACTION(Value, float)
51
52 // For all output and intermediate states
53 #define FOR_ALL_OUTPUT_TENSORS(ACTION) \
54 ACTION(Output, float) \
55 ACTION(Hits, uint8_t)
56
57 class HashtableLookupOpModel {
58 public:
HashtableLookupOpModel(std::initializer_list<uint32_t> lookup_shape,std::initializer_list<uint32_t> key_shape,std::initializer_list<uint32_t> value_shape)59 HashtableLookupOpModel(std::initializer_list<uint32_t> lookup_shape,
60 std::initializer_list<uint32_t> key_shape,
61 std::initializer_list<uint32_t> value_shape) {
62 auto it_vs = value_shape.begin();
63 rows_ = *it_vs++;
64 features_ = *it_vs;
65
66 std::vector<uint32_t> inputs;
67
68 // Input and weights
69 OperandType LookupTy(Type::TENSOR_INT32, lookup_shape);
70 inputs.push_back(model_.addOperand(&LookupTy));
71
72 OperandType KeyTy(Type::TENSOR_INT32, key_shape);
73 inputs.push_back(model_.addOperand(&KeyTy));
74
75 OperandType ValueTy(Type::TENSOR_FLOAT32, value_shape);
76 inputs.push_back(model_.addOperand(&ValueTy));
77
78 // Output and other intermediate state
79 std::vector<uint32_t> outputs;
80
81 std::vector<uint32_t> out_dim(lookup_shape.begin(), lookup_shape.end());
82 out_dim.push_back(features_);
83
84 OperandType OutputOpndTy(Type::TENSOR_FLOAT32, out_dim);
85 outputs.push_back(model_.addOperand(&OutputOpndTy));
86
87 OperandType HitsOpndTy(Type::TENSOR_QUANT8_ASYMM, lookup_shape, 1.f, 0);
88 outputs.push_back(model_.addOperand(&HitsOpndTy));
89
90 auto multiAll = [](const std::vector<uint32_t>& dims) -> uint32_t {
91 uint32_t sz = 1;
92 for (uint32_t d : dims) {
93 sz *= d;
94 }
95 return sz;
96 };
97
98 Value_.insert(Value_.end(), multiAll(value_shape), 0.f);
99 Output_.insert(Output_.end(), multiAll(out_dim), 0.f);
100 Hits_.insert(Hits_.end(), multiAll(lookup_shape), 0);
101
102 model_.addOperation(ANEURALNETWORKS_HASHTABLE_LOOKUP, inputs, outputs);
103 model_.identifyInputsAndOutputs(inputs, outputs);
104
105 model_.finish();
106 }
107
Invoke()108 void Invoke() {
109 ASSERT_TRUE(model_.isValid());
110
111 Compilation compilation(&model_);
112 compilation.finish();
113 Execution execution(&compilation);
114
115 #define SetInputOrWeight(X, T) \
116 ASSERT_EQ(execution.setInput(HashtableLookup::k##X##Tensor, X##_.data(), \
117 sizeof(T) * X##_.size()), \
118 Result::NO_ERROR);
119
120 FOR_ALL_INPUT_AND_WEIGHT_TENSORS(SetInputOrWeight);
121
122 #undef SetInputOrWeight
123
124 #define SetOutput(X, T) \
125 ASSERT_EQ(execution.setOutput(HashtableLookup::k##X##Tensor, X##_.data(), \
126 sizeof(T) * X##_.size()), \
127 Result::NO_ERROR);
128
129 FOR_ALL_OUTPUT_TENSORS(SetOutput);
130
131 #undef SetOutput
132
133 ASSERT_EQ(execution.compute(), Result::NO_ERROR);
134 }
135
136 #define DefineSetter(X, T) \
137 void Set##X(const std::vector<T>& f) { X##_.insert(X##_.end(), f.begin(), f.end()); }
138
139 FOR_ALL_INPUT_AND_WEIGHT_TENSORS(DefineSetter);
140
141 #undef DefineSetter
142
SetHashtableValue(const std::function<float (uint32_t,uint32_t)> & function)143 void SetHashtableValue(const std::function<float(uint32_t, uint32_t)>& function) {
144 for (uint32_t i = 0; i < rows_; i++) {
145 for (uint32_t j = 0; j < features_; j++) {
146 Value_[i * features_ + j] = function(i, j);
147 }
148 }
149 }
150
GetOutput() const151 const std::vector<float>& GetOutput() const { return Output_; }
GetHits() const152 const std::vector<uint8_t>& GetHits() const { return Hits_; }
153
154 private:
155 Model model_;
156 uint32_t rows_;
157 uint32_t features_;
158
159 #define DefineTensor(X, T) std::vector<T> X##_;
160
161 FOR_ALL_INPUT_AND_WEIGHT_TENSORS(DefineTensor);
162 FOR_ALL_OUTPUT_TENSORS(DefineTensor);
163
164 #undef DefineTensor
165 };
166
TEST(HashtableLookupOpTest,BlackBoxTest)167 TEST(HashtableLookupOpTest, BlackBoxTest) {
168 HashtableLookupOpModel m({4}, {3}, {3, 2});
169
170 m.SetLookup({1234, -292, -11, 0});
171 m.SetKey({-11, 0, 1234});
172 m.SetHashtableValue([](int i, int j) { return i + j / 10.0f; });
173
174 m.Invoke();
175
176 EXPECT_THAT(m.GetOutput(), ElementsAreArray(ArrayFloatNear({
177 2.0, 2.1, // 2-rd item
178 0, 0, // Not found
179 0.0, 0.1, // 0-th item
180 1.0, 1.1, // 1-st item
181 })));
182 EXPECT_EQ(m.GetHits(), std::vector<uint8_t>({
183 1,
184 0,
185 1,
186 1,
187 }));
188 }
189
190 } // namespace wrapper
191 } // namespace nn
192 } // namespace android
193