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