Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 1 | /* |
Liam Barry | cca34a9 | 2023-11-03 15:49:34 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021,2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 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 { |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 26 | namespace 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 Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 33 | } /* namespace arm */ |
| 34 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 35 | bool 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 | |
| 46 | void 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 | |
| 58 | bool 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 | |
| 70 | TEST_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 Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 75 | REQUIRE(model.Init(arm::app::tensorArena, |
| 76 | sizeof(arm::app::tensorArena), |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 77 | arm::app::rnn::GetModelPointer(), |
| 78 | arm::app::rnn::GetModelLen())); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 79 | 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 | |
| 94 | class TestRNNoiseModel : public arm::app::RNNoiseModel |
| 95 | { |
| 96 | public: |
| 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 Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 107 | /* This is true for gcc x86 platform, not guaranteed for other compilers and platforms. */ |
| 108 | TEST_CASE("Test initial GRU out state is 0", "[RNNoise]") |
| 109 | { |
| 110 | TestRNNoiseModel model{}; |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 111 | model.Init(arm::app::tensorArena, |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 112 | sizeof(arm::app::tensorArena), |
| 113 | arm::app::rnn::GetModelPointer(), |
| 114 | arm::app::rnn::GetModelLen()); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 115 | |
| 116 | auto map = model.GetStateMap(); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 117 | for(auto& mapping: map) { |
| 118 | TfLiteTensor* gruOut = model.GetOutputTensor(mapping.first); |
| 119 | auto* outGruState = tflite::GetTensorData<uint8_t>(gruOut); |
| 120 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 121 | for (size_t tIndex = 0; tIndex < gruOut->bytes; tIndex++) { |
| 122 | REQUIRE(outGruState[tIndex] == 0); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | |
| 128 | TEST_CASE("Test GRU state copy", "[RNNoise]") |
| 129 | { |
| 130 | TestRNNoiseModel model{}; |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 131 | model.Init(arm::app::tensorArena, |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 132 | sizeof(arm::app::tensorArena), |
| 133 | arm::app::rnn::GetModelPointer(), |
| 134 | arm::app::rnn::GetModelLen()); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 135 | 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 Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 161 | } |