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_RNN_H 18 #define FRAMEWORKS_ML_NN_RNN_H 19 20 #include "ActivationFunctor.h" 21 #include "HalOperation.h" 22 23 namespace android { 24 namespace nn { 25 26 struct RunTimeOperandInfo; 27 struct Shape; 28 29 class RNN { 30 public: 31 RNN(const Operation& operation, std::vector<RunTimeOperandInfo>& operands); 32 33 static bool Prepare(const Operation& operation, std::vector<RunTimeOperandInfo>& operands, 34 Shape* hiddenStateShape, Shape* outputShape); 35 bool Eval(); 36 37 static constexpr int kInputTensor = 0; 38 static constexpr int kWeightsTensor = 1; // Optional 39 static constexpr int kRecurrentWeightsTensor = 2; 40 static constexpr int kBiasTensor = 3; 41 static constexpr int kHiddenStateInTensor = 4; 42 static constexpr int kActivationParam = 5; 43 44 static constexpr int kHiddenStateOutTensor = 0; 45 static constexpr int kOutputTensor = 1; 46 47 template <typename T> 48 static bool RNNStep(const T* inputData, const Shape& inputShape, const T* hiddenStateInputData, 49 const T* biasData, const T* weightsData, const Shape& weightsShape, 50 const T* recurrentWeightsData, const Shape& recurrentWeightsShape, 51 int32_t activation, T* outputData); 52 53 template <typename T> 54 static bool RNNStep(const T* inputData, const Shape& inputShape, const T* auxInputData, 55 const Shape& auxInputShape, const T* hiddenStateInputData, 56 const T* biasData, const T* weightsData, const Shape& weightsShape, 57 const T* auxWeightsData, const Shape& auxWeightsShape, 58 const T* recurrentWeightsData, const Shape& recurrentWeightsShape, 59 int32_t activation, uint32_t outputBatchStride, uint32_t outputBatchStep, 60 T* outputData, T* hiddenStateOutput = nullptr); 61 62 private: 63 ActivationFn activation_; 64 65 const RunTimeOperandInfo* input_; 66 const RunTimeOperandInfo* weights_; 67 const RunTimeOperandInfo* recurrent_weights_; 68 const RunTimeOperandInfo* bias_; 69 const RunTimeOperandInfo* hidden_state_in_; 70 71 RunTimeOperandInfo* hidden_state_out_; 72 RunTimeOperandInfo* output_; 73 }; 74 75 } // namespace nn 76 } // namespace android 77 78 #endif // FRAMEWORKS_ML_NN_RNN_H 79