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_SVDF_H
18 #define ANDROID_FRAMEWORKS_ML_NN_COMMON_OPERATIONS_SVDF_H
19 
20 #include <tensorflow/lite/kernels/internal/tensor_utils.h>
21 
22 #include <algorithm>
23 #include <cmath>
24 #include <vector>
25 
26 #include "HalInterfaces.h"
27 
28 namespace android {
29 namespace nn {
30 
31 struct SVDFParams {
32     int rank_;
33     TfLiteFusedActivation activation_;
34 };
35 
36 struct RunTimeOperandInfo;
37 struct Shape;
38 
39 class SVDF {
40    public:
41     SVDF(const hal::Operation& operation, RunTimeOperandInfo* operands);
42 
43     static bool Prepare(const hal::Operation& operation, RunTimeOperandInfo* operands,
44                         Shape* stateShape, Shape* outputShape);
45     bool Eval();
46 
47     static constexpr int kInputTensor = 0;
48     static constexpr int kWeightsFeatureTensor = 1;
49     static constexpr int kWeightsTimeTensor = 2;
50     static constexpr int kBiasTensor = 3;  // Optional
51     static constexpr int kStateInTensor = 4;
52     static constexpr int kRankParam = 5;
53     static constexpr int kActivationParam = 6;
54 
55     static constexpr int kStateOutTensor = 0;
56     static constexpr int kOutputTensor = 1;
57 
58    private:
59     void EvalFloat32(const float* inputData, const float* inputStateData, const float* biasData,
60                      const float* weightsFeatureData, const float* weightsTimeData,
61                      float* outputData, float* outputStateData);
62 
63     SVDFParams params_;
64 
65     const RunTimeOperandInfo* input_;
66     const RunTimeOperandInfo* weights_feature_;
67     const RunTimeOperandInfo* weights_time_;
68     const RunTimeOperandInfo* bias_;
69     const RunTimeOperandInfo* state_in_;
70 
71     RunTimeOperandInfo* state_out_;
72     RunTimeOperandInfo* output_;
73 };
74 
75 }  // namespace nn
76 }  // namespace android
77 
78 #endif  // ANDROID_FRAMEWORKS_ML_NN_COMMON_OPERATIONS_SVDF_H
79