Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. All rights reserved. |
| 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | #ifndef RNNOISE_MODEL_HPP |
| 18 | #define RNNOISE_MODEL_HPP |
| 19 | |
| 20 | #include "Model.hpp" |
| 21 | |
| 22 | extern const uint32_t g_NumInputFeatures; |
| 23 | extern const uint32_t g_FrameLength; |
| 24 | extern const uint32_t g_FrameStride; |
| 25 | |
| 26 | namespace arm { |
| 27 | namespace app { |
| 28 | |
| 29 | class RNNoiseModel : public Model { |
| 30 | public: |
| 31 | /** |
| 32 | * @brief Runs inference for RNNoise model. |
| 33 | * |
| 34 | * Call CopyGruStates so GRU state outputs are copied to GRU state inputs before the inference run. |
| 35 | * Run ResetGruState() method to set states to zero before starting processing logically related data. |
| 36 | * @return True if inference succeeded, False - otherwise |
| 37 | */ |
| 38 | bool RunInference() override; |
| 39 | |
| 40 | /** |
| 41 | * @brief Sets GRU input states to zeros. |
| 42 | * Call this method before starting processing the new sequence of logically related data. |
| 43 | */ |
| 44 | void ResetGruState(); |
| 45 | |
| 46 | /** |
| 47 | * @brief Copy current GRU output states to input states. |
| 48 | * Call this method before starting processing the next sequence of logically related data. |
| 49 | */ |
| 50 | bool CopyGruStates(); |
| 51 | |
| 52 | /* Which index of model outputs does the main output (gains) come from. */ |
| 53 | const size_t m_indexForModelOutput = 1; |
| 54 | |
| 55 | protected: |
| 56 | /** @brief Gets the reference to op resolver interface class. */ |
| 57 | const tflite::MicroOpResolver& GetOpResolver() override; |
| 58 | |
| 59 | /** @brief Adds operations to the op resolver instance. */ |
| 60 | bool EnlistOperations() override; |
| 61 | |
| 62 | const uint8_t* ModelPointer() override; |
| 63 | |
| 64 | size_t ModelSize() override; |
| 65 | |
| 66 | /* |
| 67 | Each inference after the first needs to copy 3 GRU states from a output index to input index (model dependent): |
| 68 | 0 -> 3, 2 -> 2, 3 -> 1 |
| 69 | */ |
| 70 | const std::vector<std::pair<size_t, size_t>> m_gruStateMap = {{0,3}, {2, 2}, {3, 1}}; |
| 71 | private: |
| 72 | /* Maximum number of individual operations that can be enlisted. */ |
| 73 | static constexpr int ms_maxOpCnt = 15; |
| 74 | |
| 75 | /* A mutable op resolver instance. */ |
| 76 | tflite::MicroMutableOpResolver<ms_maxOpCnt> m_opResolver; |
| 77 | }; |
| 78 | |
| 79 | } /* namespace app */ |
| 80 | } /* namespace arm */ |
| 81 | |
| 82 | #endif /* RNNOISE_MODEL_HPP */ |