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 #ifndef ANDROID_FRAMEWORKS_ML_NN_COMMON_OPERATIONS_LSHPROJECTION_H
18 #define ANDROID_FRAMEWORKS_ML_NN_COMMON_OPERATIONS_LSHPROJECTION_H
19 
20 #include <vector>
21 
22 #include "HalInterfaces.h"
23 
24 namespace android {
25 namespace nn {
26 
27 enum LSHProjectionType {
28     LSHProjectionType_UNKNOWN = 0,
29     LSHProjectionType_SPARSE_DEPRECATED = 1,
30     LSHProjectionType_DENSE = 2,
31     LSHProjectionType_SPARSE = 3,
32 };
33 
34 struct RunTimeOperandInfo;
35 struct Shape;
36 
37 class LSHProjection {
38    public:
39     LSHProjection(const hal::Operation& operation, RunTimeOperandInfo* operands);
40 
41     static bool Prepare(const hal::Operation& operation, RunTimeOperandInfo* operands,
42                         Shape* outputShape);
43     template <typename T>
44     bool Eval();
45 
46     static constexpr int kHashTensor = 0;
47     static constexpr int kInputTensor = 1;
48     static constexpr int kWeightTensor = 2;  // Optional
49 
50     static constexpr int kTypeParam = 3;
51 
52     static constexpr int kOutputTensor = 0;
53 
54    private:
55     LSHProjectionType type_;
56 
57     const RunTimeOperandInfo* hash_;
58     const RunTimeOperandInfo* input_;
59     const RunTimeOperandInfo* weight_;
60 
61     RunTimeOperandInfo* output_;
62 };
63 
64 template <typename T>
65 int runningSignBit(const RunTimeOperandInfo* input, const RunTimeOperandInfo* weight, T seed);
66 
67 template <typename T>
68 void SparseLshProjection(LSHProjectionType type, const RunTimeOperandInfo* hash,
69                          const RunTimeOperandInfo* input, const RunTimeOperandInfo* weight,
70                          int32_t* outBuffer);
71 
72 template <typename T>
73 void DenseLshProjection(const RunTimeOperandInfo* hash, const RunTimeOperandInfo* input,
74                         const RunTimeOperandInfo* weight, int32_t* outBuffer);
75 
76 }  // namespace nn
77 }  // namespace android
78 
79 #endif  // ANDROID_FRAMEWORKS_ML_NN_COMMON_OPERATIONS_LSHPROJECTION_H
80