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