blob: 9dc640b9a42a1af66c69a91497982f2f2f6f29f5 [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 */
Richard Burton00553462021-11-10 16:27:14 +000017#include "TensorFlowLiteMicro.hpp"
18#include "RNNoiseModel.hpp"
19#include "TestData_noise_reduction.hpp"
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010020#include "BufAttributes.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 {
26 namespace app {
27 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
28 } /* namespace app */
29} /* namespace arm */
30
31extern uint8_t* GetModelPointer();
32extern size_t GetModelLen();
33
Richard Burton00553462021-11-10 16:27:14 +000034namespace test {
35namespace rnnoise {
36
37 bool RunInference(arm::app::Model& model, const std::vector<std::vector<int8_t>> inData)
38 {
39 for (size_t i = 0; i < model.GetNumInputs(); ++i) {
40 TfLiteTensor* inputTensor = model.GetInputTensor(i);
41 REQUIRE(inputTensor);
42 memcpy(inputTensor->data.data, inData[i].data(), inData[i].size());
43 }
44
45 return model.RunInference();
46 }
47
48 bool RunInferenceRandom(arm::app::Model& model)
49 {
50 std::random_device rndDevice;
51 std::mt19937 mersenneGen{rndDevice()};
52 std::uniform_int_distribution<short> dist {-128, 127};
53
54 auto gen = [&dist, &mersenneGen](){
55 return dist(mersenneGen);
56 };
57
58 std::vector<std::vector<int8_t>> randomInput{NUMBER_OF_IFM_FILES};
59 for (size_t i = 0; i < model.GetNumInputs(); ++i) {
60 TfLiteTensor *inputTensor = model.GetInputTensor(i);
61 REQUIRE(inputTensor);
62 randomInput[i].resize(inputTensor->bytes);
63 std::generate(std::begin(randomInput[i]), std::end(randomInput[i]), gen);
64 }
65
66 REQUIRE(RunInference(model, randomInput));
67 return true;
68 }
69
70 TEST_CASE("Running random inference with Tflu and RNNoise Int8", "[RNNoise]")
71 {
72 arm::app::RNNoiseModel model{};
73
74 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010075 REQUIRE(model.Init(arm::app::tensorArena,
76 sizeof(arm::app::tensorArena),
77 GetModelPointer(),
78 GetModelLen()));
Richard Burton00553462021-11-10 16:27:14 +000079 REQUIRE(model.IsInited());
80
81 REQUIRE(RunInferenceRandom(model));
82 }
83
84 template<typename T>
85 void TestInference(const std::vector<std::vector<T>> input_goldenFV, const std::vector<std::vector<T>> output_goldenFV, arm::app::Model& model)
86 {
87 for (size_t i = 0; i < model.GetNumInputs(); ++i) {
88 TfLiteTensor* inputTensor = model.GetInputTensor(i);
89 REQUIRE(inputTensor);
90 }
91
92 REQUIRE(RunInference(model, input_goldenFV));
93
94 for (size_t i = 0; i < model.GetNumOutputs(); ++i) {
95 TfLiteTensor *outputTensor = model.GetOutputTensor(i);
96
97 REQUIRE(outputTensor);
98 auto tensorData = tflite::GetTensorData<T>(outputTensor);
99 REQUIRE(tensorData);
100
101 for (size_t j = 0; j < outputTensor->bytes; j++) {
102 REQUIRE(static_cast<int>(tensorData[j]) == static_cast<int>((output_goldenFV[i][j])));
103 }
104 }
105 }
106
107 TEST_CASE("Running inference with Tflu and RNNoise Int8", "[RNNoise]")
108 {
109 std::vector<std::vector<int8_t>> goldenInputFV {NUMBER_OF_IFM_FILES};
110 std::vector<std::vector<int8_t>> goldenOutputFV {NUMBER_OF_OFM_FILES};
111
112 std::array<size_t, NUMBER_OF_IFM_FILES> inputSizes = {IFM_0_DATA_SIZE,
113 IFM_1_DATA_SIZE,
114 IFM_2_DATA_SIZE,
115 IFM_3_DATA_SIZE};
116
117 std::array<size_t, NUMBER_OF_OFM_FILES> outputSizes = {OFM_0_DATA_SIZE,
118 OFM_1_DATA_SIZE,
119 OFM_2_DATA_SIZE,
120 OFM_3_DATA_SIZE,
121 OFM_4_DATA_SIZE};
122
123 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
124 goldenInputFV[i].resize(inputSizes[i]);
125 std::memcpy(goldenInputFV[i].data(), get_ifm_data_array(i), inputSizes[i]);
126 }
127 for (uint32_t i = 0 ; i < NUMBER_OF_OFM_FILES; ++i) {
128 goldenOutputFV[i].resize(outputSizes[i]);
129 std::memcpy(goldenOutputFV[i].data(), get_ofm_data_array(i), outputSizes[i]);
130 }
131
132 DYNAMIC_SECTION("Executing inference with re-init")
133 {
134 arm::app::RNNoiseModel model{};
135
136 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100137 REQUIRE(model.Init(arm::app::tensorArena,
138 sizeof(arm::app::tensorArena),
139 GetModelPointer(),
140 GetModelLen()));
Richard Burton00553462021-11-10 16:27:14 +0000141 REQUIRE(model.IsInited());
142
143 TestInference<int8_t>(goldenInputFV, goldenOutputFV, model);
144 }
145 }
146
147} /* namespace rnnoise */
148} /* namespace test */