blob: 7bd83b11968aab2dd736824868b6449e3f6fc63b [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#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
107template <class T>
108void printArray(size_t dataSz, T data){
109 char strhex[8];
110 std::string strdump;
111
112 for (size_t i = 0; i < dataSz; ++i) {
113 if (0 == i % 8) {
114 printf("%s\n\t", strdump.c_str());
115 strdump.clear();
116 }
117 snprintf(strhex, sizeof(strhex) - 1,
118 "0x%02x, ", data[i]);
119 strdump += std::string(strhex);
120 }
121
122 if (!strdump.empty()) {
123 printf("%s\n", strdump.c_str());
124 }
125}
126
127/* This is true for gcc x86 platform, not guaranteed for other compilers and platforms. */
128TEST_CASE("Test initial GRU out state is 0", "[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
136 auto map = model.GetStateMap();
137
138 for(auto& mapping: map) {
139 TfLiteTensor* gruOut = model.GetOutputTensor(mapping.first);
140 auto* outGruState = tflite::GetTensorData<uint8_t>(gruOut);
141
142 printf("gru out state:");
143 printArray(gruOut->bytes, outGruState);
144
145 for (size_t tIndex = 0; tIndex < gruOut->bytes; tIndex++) {
146 REQUIRE(outGruState[tIndex] == 0);
147 }
148 }
149
150}
151
152TEST_CASE("Test GRU state copy", "[RNNoise]")
153{
154 TestRNNoiseModel model{};
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100155 model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +0100156 sizeof(arm::app::tensorArena),
157 arm::app::rnn::GetModelPointer(),
158 arm::app::rnn::GetModelLen());
Richard Burton00553462021-11-10 16:27:14 +0000159 REQUIRE(RunInferenceRandom(model, 0));
160
161 auto map = model.GetStateMap();
162
163 std::vector<std::vector<uint8_t>> oldStates;
164 for(auto& mapping: map) {
165
166 TfLiteTensor* gruOut = model.GetOutputTensor(mapping.first);
167 auto* outGruState = tflite::GetTensorData<uint8_t>(gruOut);
168 /* Save old output state. */
169 std::vector<uint8_t> oldState(gruOut->bytes);
170 memcpy(oldState.data(), outGruState, gruOut->bytes);
171 oldStates.push_back(oldState);
172 }
173
174 model.CopyGruStatesTest();
175 auto statesIter = oldStates.begin();
176 for(auto& mapping: map) {
177 TfLiteTensor* gruInput = model.GetInputTensor(mapping.second);
178 auto* inGruState = tflite::GetTensorData<uint8_t>(gruInput);
179 for (size_t tIndex = 0; tIndex < gruInput->bytes; tIndex++) {
180 REQUIRE((*statesIter)[tIndex] == inGruState[tIndex]);
181 }
182 statesIter++;
183 }
184
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100185}