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