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