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