blob: 17ce9ac2bac8ac1121a27e1b4805fbc8e38373cf [file] [log] [blame]
Richard Burton00553462021-11-10 16:27:14 +00001/*
Kshitij Sisodia2ea46232022-12-19 16:37:33 +00002 * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates
3 * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0
Richard Burton00553462021-11-10 16:27:14 +00004 *
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 */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010017#include "BufAttributes.hpp"
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000018#include "RNNoiseModel.hpp"
19#include "TensorFlowLiteMicro.hpp"
20#include "TestData_noise_reduction.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 +000035namespace test {
Liam Barry213a5432022-05-09 17:06:19 +010036namespace noise_reduction {
Richard Burton00553462021-11-10 16:27:14 +000037
38 bool RunInference(arm::app::Model& model, const std::vector<std::vector<int8_t>> inData)
39 {
40 for (size_t i = 0; i < model.GetNumInputs(); ++i) {
41 TfLiteTensor* inputTensor = model.GetInputTensor(i);
42 REQUIRE(inputTensor);
43 memcpy(inputTensor->data.data, inData[i].data(), inData[i].size());
44 }
45
46 return model.RunInference();
47 }
48
49 bool RunInferenceRandom(arm::app::Model& model)
50 {
51 std::random_device rndDevice;
52 std::mt19937 mersenneGen{rndDevice()};
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000053 std::uniform_int_distribution<short> dist{-128, 127};
Richard Burton00553462021-11-10 16:27:14 +000054
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000055 auto gen = [&dist, &mersenneGen]() { return dist(mersenneGen); };
Richard Burton00553462021-11-10 16:27:14 +000056
57 std::vector<std::vector<int8_t>> randomInput{NUMBER_OF_IFM_FILES};
58 for (size_t i = 0; i < model.GetNumInputs(); ++i) {
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000059 TfLiteTensor* inputTensor = model.GetInputTensor(i);
Richard Burton00553462021-11-10 16:27:14 +000060 REQUIRE(inputTensor);
61 randomInput[i].resize(inputTensor->bytes);
62 std::generate(std::begin(randomInput[i]), std::end(randomInput[i]), gen);
63 }
64
65 REQUIRE(RunInference(model, randomInput));
66 return true;
67 }
68
69 TEST_CASE("Running random inference with Tflu and RNNoise Int8", "[RNNoise]")
70 {
71 arm::app::RNNoiseModel model{};
72
73 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010074 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +010075 sizeof(arm::app::tensorArena),
76 arm::app::rnn::GetModelPointer(),
77 arm::app::rnn::GetModelLen()));
Richard Burton00553462021-11-10 16:27:14 +000078 REQUIRE(model.IsInited());
79
80 REQUIRE(RunInferenceRandom(model));
81 }
82
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000083 template <typename T>
84 void TestInference(const std::vector<std::vector<T>> input_goldenFV,
85 const std::vector<std::vector<T>> output_goldenFV,
86 arm::app::Model& model)
Richard Burton00553462021-11-10 16:27:14 +000087 {
88 for (size_t i = 0; i < model.GetNumInputs(); ++i) {
89 TfLiteTensor* inputTensor = model.GetInputTensor(i);
90 REQUIRE(inputTensor);
91 }
92
93 REQUIRE(RunInference(model, input_goldenFV));
94
95 for (size_t i = 0; i < model.GetNumOutputs(); ++i) {
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000096 TfLiteTensor* outputTensor = model.GetOutputTensor(i);
Richard Burton00553462021-11-10 16:27:14 +000097
98 REQUIRE(outputTensor);
99 auto tensorData = tflite::GetTensorData<T>(outputTensor);
100 REQUIRE(tensorData);
101
102 for (size_t j = 0; j < outputTensor->bytes; j++) {
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000103 REQUIRE(static_cast<int>(tensorData[j]) ==
104 static_cast<int>((output_goldenFV[i][j])));
Richard Burton00553462021-11-10 16:27:14 +0000105 }
106 }
107 }
108
109 TEST_CASE("Running inference with Tflu and RNNoise Int8", "[RNNoise]")
110 {
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000111 std::vector<std::vector<int8_t>> goldenInputFV{NUMBER_OF_IFM_FILES};
112 std::vector<std::vector<int8_t>> goldenOutputFV{NUMBER_OF_OFM_FILES};
Richard Burton00553462021-11-10 16:27:14 +0000113
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000114 std::array<size_t, NUMBER_OF_IFM_FILES> inputSizes = {
115 IFM_0_DATA_SIZE, IFM_1_DATA_SIZE, IFM_2_DATA_SIZE, IFM_3_DATA_SIZE};
Richard Burton00553462021-11-10 16:27:14 +0000116
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000117 std::array<size_t, NUMBER_OF_OFM_FILES> outputSizes = {
118 OFM_0_DATA_SIZE, OFM_1_DATA_SIZE, OFM_2_DATA_SIZE, OFM_3_DATA_SIZE, OFM_4_DATA_SIZE};
Richard Burton00553462021-11-10 16:27:14 +0000119
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000120 for (uint32_t i = 0; i < NUMBER_OF_IFM_FILES; ++i) {
Richard Burton00553462021-11-10 16:27:14 +0000121 goldenInputFV[i].resize(inputSizes[i]);
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000122 std::memcpy(goldenInputFV[i].data(), GetIfmDataArray(i), inputSizes[i]);
Richard Burton00553462021-11-10 16:27:14 +0000123 }
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000124 for (uint32_t i = 0; i < NUMBER_OF_OFM_FILES; ++i) {
Richard Burton00553462021-11-10 16:27:14 +0000125 goldenOutputFV[i].resize(outputSizes[i]);
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000126 std::memcpy(goldenOutputFV[i].data(), GetOfmDataArray(i), outputSizes[i]);
Richard Burton00553462021-11-10 16:27:14 +0000127 }
128
129 DYNAMIC_SECTION("Executing inference with re-init")
130 {
131 arm::app::RNNoiseModel model{};
132
133 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100134 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +0100135 sizeof(arm::app::tensorArena),
136 arm::app::rnn::GetModelPointer(),
137 arm::app::rnn::GetModelLen()));
Richard Burton00553462021-11-10 16:27:14 +0000138 REQUIRE(model.IsInited());
139
140 TestInference<int8_t>(goldenInputFV, goldenOutputFV, model);
141 }
142 }
143
Liam Barry213a5432022-05-09 17:06:19 +0100144} /* namespace noise_reduction */
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000145} /* namespace test */