blob: d08c5f3962f218210e786f57ef30358815b15b68 [file] [log] [blame]
Richard Burton00553462021-11-10 16:27:14 +00001/*
Liam Barrycca34a92023-11-03 15:49:34 +00002 * SPDX-FileCopyrightText: Copyright 2021,2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Richard Burton00553462021-11-10 16:27:14 +00003 * 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#include "RNNoiseModel.hpp"
Richard Burton00553462021-11-10 16:27:14 +000018#include "TensorFlowLiteMicro.hpp"
19#include "TestData_noise_reduction.hpp"
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010020#include "BufAttributes.hpp"
Richard Burton00553462021-11-10 16:27:14 +000021
22#include <catch.hpp>
23#include <random>
24
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010025namespace arm {
Liam Barry213a5432022-05-09 17:06:19 +010026namespace app {
27 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
28 namespace rnn {
29 extern uint8_t* GetModelPointer();
30 extern size_t GetModelLen();
31 } /* namespace rnn */
32} /* namespace app */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010033} /* namespace arm */
34
Richard Burton00553462021-11-10 16:27:14 +000035bool RunInference(arm::app::Model& model, std::vector<int8_t> vec,
36 const size_t sizeRequired, const size_t dataInputIndex)
37{
38 TfLiteTensor* inputTensor = model.GetInputTensor(dataInputIndex);
39 REQUIRE(inputTensor);
40 size_t copySz = inputTensor->bytes < sizeRequired ? inputTensor->bytes : sizeRequired;
41 const int8_t* vecData = vec.data();
42 memcpy(inputTensor->data.data, vecData, copySz);
43 return model.RunInference();
44}
45
46void genRandom(size_t bytes, std::vector<int8_t>& randomAudio)
47{
48 randomAudio.resize(bytes);
49 std::random_device rndDevice;
50 std::mt19937 mersenneGen{rndDevice()};
51 std::uniform_int_distribution<short> dist {-128, 127};
52 auto gen = [&dist, &mersenneGen](){
53 return dist(mersenneGen);
54 };
55 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
56}
57
58bool RunInferenceRandom(arm::app::Model& model, const size_t dataInputIndex)
59{
60 std::array<size_t, 4> inputSizes = {IFM_0_DATA_SIZE, IFM_1_DATA_SIZE, IFM_2_DATA_SIZE, IFM_3_DATA_SIZE};
61 std::vector<int8_t> randomAudio;
62 TfLiteTensor* inputTensor = model.GetInputTensor(dataInputIndex);
63 REQUIRE(inputTensor);
64 genRandom(inputTensor->bytes, randomAudio);
65
66 REQUIRE(RunInference(model, randomAudio, inputSizes[dataInputIndex], dataInputIndex));
67 return true;
68}
69
70TEST_CASE("Running random inference with TensorFlow Lite Micro and RNNoiseModel Int8", "[RNNoise]")
71{
72 arm::app::RNNoiseModel model{};
73
74 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010075 REQUIRE(model.Init(arm::app::tensorArena,
76 sizeof(arm::app::tensorArena),
Liam Barry213a5432022-05-09 17:06:19 +010077 arm::app::rnn::GetModelPointer(),
78 arm::app::rnn::GetModelLen()));
Richard Burton00553462021-11-10 16:27:14 +000079 REQUIRE(model.IsInited());
80
81 model.ResetGruState();
82
83 for (int i = 1; i < 4; i++ ) {
84 TfLiteTensor* inputGruStateTensor = model.GetInputTensor(i);
85 auto* inputGruState = tflite::GetTensorData<int8_t>(inputGruStateTensor);
86 for (size_t tIndex = 0; tIndex < inputGruStateTensor->bytes; tIndex++) {
87 REQUIRE(inputGruState[tIndex] == arm::app::GetTensorQuantParams(inputGruStateTensor).offset);
88 }
89 }
90
91 REQUIRE(RunInferenceRandom(model, 0));
92}
93
94class TestRNNoiseModel : public arm::app::RNNoiseModel
95{
96public:
97 bool CopyGruStatesTest() {
98 return RNNoiseModel::CopyGruStates();
99 }
100
101 std::vector<std::pair<size_t, size_t>> GetStateMap() {
102 return m_gruStateMap;
103 }
104
105};
106
Richard Burton00553462021-11-10 16:27:14 +0000107/* This is true for gcc x86 platform, not guaranteed for other compilers and platforms. */
108TEST_CASE("Test initial GRU out state is 0", "[RNNoise]")
109{
110 TestRNNoiseModel model{};
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100111 model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +0100112 sizeof(arm::app::tensorArena),
113 arm::app::rnn::GetModelPointer(),
114 arm::app::rnn::GetModelLen());
Richard Burton00553462021-11-10 16:27:14 +0000115
116 auto map = model.GetStateMap();
Richard Burton00553462021-11-10 16:27:14 +0000117 for(auto& mapping: map) {
118 TfLiteTensor* gruOut = model.GetOutputTensor(mapping.first);
119 auto* outGruState = tflite::GetTensorData<uint8_t>(gruOut);
120
Richard Burton00553462021-11-10 16:27:14 +0000121 for (size_t tIndex = 0; tIndex < gruOut->bytes; tIndex++) {
122 REQUIRE(outGruState[tIndex] == 0);
123 }
124 }
125
126}
127
128TEST_CASE("Test GRU state copy", "[RNNoise]")
129{
130 TestRNNoiseModel model{};
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100131 model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +0100132 sizeof(arm::app::tensorArena),
133 arm::app::rnn::GetModelPointer(),
134 arm::app::rnn::GetModelLen());
Richard Burton00553462021-11-10 16:27:14 +0000135 REQUIRE(RunInferenceRandom(model, 0));
136
137 auto map = model.GetStateMap();
138
139 std::vector<std::vector<uint8_t>> oldStates;
140 for(auto& mapping: map) {
141
142 TfLiteTensor* gruOut = model.GetOutputTensor(mapping.first);
143 auto* outGruState = tflite::GetTensorData<uint8_t>(gruOut);
144 /* Save old output state. */
145 std::vector<uint8_t> oldState(gruOut->bytes);
146 memcpy(oldState.data(), outGruState, gruOut->bytes);
147 oldStates.push_back(oldState);
148 }
149
150 model.CopyGruStatesTest();
151 auto statesIter = oldStates.begin();
152 for(auto& mapping: map) {
153 TfLiteTensor* gruInput = model.GetInputTensor(mapping.second);
154 auto* inGruState = tflite::GetTensorData<uint8_t>(gruInput);
155 for (size_t tIndex = 0; tIndex < gruInput->bytes; tIndex++) {
156 REQUIRE((*statesIter)[tIndex] == inGruState[tIndex]);
157 }
158 statesIter++;
159 }
160
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100161}