blob: 5d30211bb4e2822e9ee2d6aeea4e16e8f880055d [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +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 */
alexander3c798932021-03-26 21:42:19 +000017#include "TensorFlowLiteMicro.hpp"
18#include "Wav2LetterModel.hpp"
19#include "TestData_asr.hpp"
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010020#include "BufAttributes.hpp"
alexander3c798932021-03-26 21:42:19 +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
29 namespace asr {
30 extern uint8_t* GetModelPointer();
31 extern size_t GetModelLen();
32 }
33 } /* namespace app */
34} /* namespace arm */
35
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010036namespace test {
alexander3c798932021-03-26 21:42:19 +000037namespace asr {
38
39bool RunInference(arm::app::Model& model, const int8_t vec[], const size_t copySz)
40{
41 TfLiteTensor* inputTensor = model.GetInputTensor(0);
42 REQUIRE(inputTensor);
43
44 memcpy(inputTensor->data.data, vec, copySz);
45
46 return model.RunInference();
47}
48
49bool RunInferenceRandom(arm::app::Model& model)
50{
51 TfLiteTensor* inputTensor = model.GetInputTensor(0);
52 REQUIRE(inputTensor);
53
54 std::random_device rndDevice;
55 std::mt19937 mersenneGen{rndDevice()};
56 std::uniform_int_distribution<short> dist {-128, 127};
57
58 auto gen = [&dist, &mersenneGen](){
59 return dist(mersenneGen);
60 };
61
62 std::vector<int8_t> randomAudio(inputTensor->bytes);
63 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
64
65 REQUIRE(RunInference(model, randomAudio.data(), inputTensor->bytes));
66 return true;
67}
68
Richard Burton0d110592021-08-12 17:26:30 +010069TEST_CASE("Running random inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter]")
alexander3c798932021-03-26 21:42:19 +000070{
71 arm::app::Wav2LetterModel model{};
72
73 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010074 REQUIRE(model.Init(arm::app::tensorArena,
75 sizeof(arm::app::tensorArena),
76 arm::app::asr::GetModelPointer(),
77 arm::app::asr::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000078 REQUIRE(model.IsInited());
79
80 REQUIRE(RunInferenceRandom(model));
81}
82
83
84template<typename T>
85void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
86{
87 TfLiteTensor* inputTensor = model.GetInputTensor(0);
88 REQUIRE(inputTensor);
89
90 REQUIRE(RunInference(model, input_goldenFV, inputTensor->bytes));
91
92 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
93
94 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000095 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000096 auto tensorData = tflite::GetTensorData<T>(outputTensor);
97 REQUIRE(tensorData);
98
99 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +0100100 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i])));
alexander3c798932021-03-26 21:42:19 +0000101 }
102}
103
Richard Burton0d110592021-08-12 17:26:30 +0100104TEST_CASE("Running inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter]")
alexander3c798932021-03-26 21:42:19 +0000105{
Richard Burton00553462021-11-10 16:27:14 +0000106 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_OFM_FILES);
107 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +0000108 auto input_goldenFV = get_ifm_data_array(i);;
109 auto output_goldenFV = get_ofm_data_array(i);
110
111 DYNAMIC_SECTION("Executing inference with re-init")
112 {
113 arm::app::Wav2LetterModel model{};
114
115 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100116 REQUIRE(model.Init(arm::app::tensorArena,
117 sizeof(arm::app::tensorArena),
118 arm::app::asr::GetModelPointer(),
119 arm::app::asr::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000120 REQUIRE(model.IsInited());
121
122 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
123
124 }
125 }
126}
127
128} //namespace
129} //namespace