blob: 0cc08098618536ff8be604d8680db9fc23f991d3 [file] [log] [blame]
Richard Burton00553462021-11-10 16:27:14 +00001/*
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
Richard Burton00553462021-11-10 16:27:14 +000022namespace arm {
23namespace app {
Liam Barry213a5432022-05-09 17:06:19 +010024 namespace rnn {
25 extern const uint32_t g_NumInputFeatures;
26 extern const uint32_t g_FrameLength;
27 extern const uint32_t g_FrameStride;
28 } /* namespace rnn */
Richard Burton00553462021-11-10 16:27:14 +000029
30 class RNNoiseModel : public Model {
31 public:
32 /**
33 * @brief Runs inference for RNNoise model.
34 *
35 * Call CopyGruStates so GRU state outputs are copied to GRU state inputs before the inference run.
36 * Run ResetGruState() method to set states to zero before starting processing logically related data.
37 * @return True if inference succeeded, False - otherwise
38 */
39 bool RunInference() override;
40
41 /**
42 * @brief Sets GRU input states to zeros.
43 * Call this method before starting processing the new sequence of logically related data.
44 */
45 void ResetGruState();
46
47 /**
48 * @brief Copy current GRU output states to input states.
49 * Call this method before starting processing the next sequence of logically related data.
50 */
51 bool CopyGruStates();
52
53 /* Which index of model outputs does the main output (gains) come from. */
54 const size_t m_indexForModelOutput = 1;
55
56 protected:
57 /** @brief Gets the reference to op resolver interface class. */
58 const tflite::MicroOpResolver& GetOpResolver() override;
59
60 /** @brief Adds operations to the op resolver instance. */
61 bool EnlistOperations() override;
62
Richard Burton00553462021-11-10 16:27:14 +000063 /*
64 Each inference after the first needs to copy 3 GRU states from a output index to input index (model dependent):
65 0 -> 3, 2 -> 2, 3 -> 1
66 */
67 const std::vector<std::pair<size_t, size_t>> m_gruStateMap = {{0,3}, {2, 2}, {3, 1}};
68 private:
69 /* Maximum number of individual operations that can be enlisted. */
70 static constexpr int ms_maxOpCnt = 15;
71
72 /* A mutable op resolver instance. */
73 tflite::MicroMutableOpResolver<ms_maxOpCnt> m_opResolver;
74 };
75
76} /* namespace app */
77} /* namespace arm */
78
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010079#endif /* RNNOISE_MODEL_HPP */