blob: 7798975d9ef09a73e807eebbea6bf619cca9dca6 [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"
20
21#include <catch.hpp>
22#include <random>
23
24bool RunInference(arm::app::Model& model, std::vector<int8_t> vec,
25 const size_t sizeRequired, const size_t dataInputIndex)
26{
27 TfLiteTensor* inputTensor = model.GetInputTensor(dataInputIndex);
28 REQUIRE(inputTensor);
29 size_t copySz = inputTensor->bytes < sizeRequired ? inputTensor->bytes : sizeRequired;
30 const int8_t* vecData = vec.data();
31 memcpy(inputTensor->data.data, vecData, copySz);
32 return model.RunInference();
33}
34
35void genRandom(size_t bytes, std::vector<int8_t>& randomAudio)
36{
37 randomAudio.resize(bytes);
38 std::random_device rndDevice;
39 std::mt19937 mersenneGen{rndDevice()};
40 std::uniform_int_distribution<short> dist {-128, 127};
41 auto gen = [&dist, &mersenneGen](){
42 return dist(mersenneGen);
43 };
44 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
45}
46
47bool RunInferenceRandom(arm::app::Model& model, const size_t dataInputIndex)
48{
49 std::array<size_t, 4> inputSizes = {IFM_0_DATA_SIZE, IFM_1_DATA_SIZE, IFM_2_DATA_SIZE, IFM_3_DATA_SIZE};
50 std::vector<int8_t> randomAudio;
51 TfLiteTensor* inputTensor = model.GetInputTensor(dataInputIndex);
52 REQUIRE(inputTensor);
53 genRandom(inputTensor->bytes, randomAudio);
54
55 REQUIRE(RunInference(model, randomAudio, inputSizes[dataInputIndex], dataInputIndex));
56 return true;
57}
58
59TEST_CASE("Running random inference with TensorFlow Lite Micro and RNNoiseModel Int8", "[RNNoise]")
60{
61 arm::app::RNNoiseModel model{};
62
63 REQUIRE_FALSE(model.IsInited());
64 REQUIRE(model.Init());
65 REQUIRE(model.IsInited());
66
67 model.ResetGruState();
68
69 for (int i = 1; i < 4; i++ ) {
70 TfLiteTensor* inputGruStateTensor = model.GetInputTensor(i);
71 auto* inputGruState = tflite::GetTensorData<int8_t>(inputGruStateTensor);
72 for (size_t tIndex = 0; tIndex < inputGruStateTensor->bytes; tIndex++) {
73 REQUIRE(inputGruState[tIndex] == arm::app::GetTensorQuantParams(inputGruStateTensor).offset);
74 }
75 }
76
77 REQUIRE(RunInferenceRandom(model, 0));
78}
79
80class TestRNNoiseModel : public arm::app::RNNoiseModel
81{
82public:
83 bool CopyGruStatesTest() {
84 return RNNoiseModel::CopyGruStates();
85 }
86
87 std::vector<std::pair<size_t, size_t>> GetStateMap() {
88 return m_gruStateMap;
89 }
90
91};
92
93template <class T>
94void printArray(size_t dataSz, T data){
95 char strhex[8];
96 std::string strdump;
97
98 for (size_t i = 0; i < dataSz; ++i) {
99 if (0 == i % 8) {
100 printf("%s\n\t", strdump.c_str());
101 strdump.clear();
102 }
103 snprintf(strhex, sizeof(strhex) - 1,
104 "0x%02x, ", data[i]);
105 strdump += std::string(strhex);
106 }
107
108 if (!strdump.empty()) {
109 printf("%s\n", strdump.c_str());
110 }
111}
112
113/* This is true for gcc x86 platform, not guaranteed for other compilers and platforms. */
114TEST_CASE("Test initial GRU out state is 0", "[RNNoise]")
115{
116 TestRNNoiseModel model{};
117 model.Init();
118
119 auto map = model.GetStateMap();
120
121 for(auto& mapping: map) {
122 TfLiteTensor* gruOut = model.GetOutputTensor(mapping.first);
123 auto* outGruState = tflite::GetTensorData<uint8_t>(gruOut);
124
125 printf("gru out state:");
126 printArray(gruOut->bytes, outGruState);
127
128 for (size_t tIndex = 0; tIndex < gruOut->bytes; tIndex++) {
129 REQUIRE(outGruState[tIndex] == 0);
130 }
131 }
132
133}
134
135TEST_CASE("Test GRU state copy", "[RNNoise]")
136{
137 TestRNNoiseModel model{};
138 model.Init();
139 REQUIRE(RunInferenceRandom(model, 0));
140
141 auto map = model.GetStateMap();
142
143 std::vector<std::vector<uint8_t>> oldStates;
144 for(auto& mapping: map) {
145
146 TfLiteTensor* gruOut = model.GetOutputTensor(mapping.first);
147 auto* outGruState = tflite::GetTensorData<uint8_t>(gruOut);
148 /* Save old output state. */
149 std::vector<uint8_t> oldState(gruOut->bytes);
150 memcpy(oldState.data(), outGruState, gruOut->bytes);
151 oldStates.push_back(oldState);
152 }
153
154 model.CopyGruStatesTest();
155 auto statesIter = oldStates.begin();
156 for(auto& mapping: map) {
157 TfLiteTensor* gruInput = model.GetInputTensor(mapping.second);
158 auto* inGruState = tflite::GetTensorData<uint8_t>(gruInput);
159 for (size_t tIndex = 0; tIndex < gruInput->bytes; tIndex++) {
160 REQUIRE((*statesIter)[tIndex] == inGruState[tIndex]);
161 }
162 statesIter++;
163 }
164
165}