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