blob: 72dcadc8d1bd21512ee1d17e7f4bd3f300c4eabd [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com>
alexander3c798932021-03-26 21:42:19 +00003 * 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 {
Liam Barry213a5432022-05-09 17:06:19 +010026namespace app {
27 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
28 namespace asr {
29 extern uint8_t* GetModelPointer();
30 extern size_t GetModelLen();
31 } /* namespace asr */
32} /* namespace app */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010033} /* namespace arm */
34
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010035namespace test {
alexander3c798932021-03-26 21:42:19 +000036namespace asr {
37
38bool RunInference(arm::app::Model& model, const int8_t vec[], const size_t copySz)
39{
40 TfLiteTensor* inputTensor = model.GetInputTensor(0);
41 REQUIRE(inputTensor);
42
43 memcpy(inputTensor->data.data, vec, copySz);
44
45 return model.RunInference();
46}
47
48bool RunInferenceRandom(arm::app::Model& model)
49{
50 TfLiteTensor* inputTensor = model.GetInputTensor(0);
51 REQUIRE(inputTensor);
52
53 std::random_device rndDevice;
54 std::mt19937 mersenneGen{rndDevice()};
55 std::uniform_int_distribution<short> dist {-128, 127};
56
57 auto gen = [&dist, &mersenneGen](){
58 return dist(mersenneGen);
59 };
60
61 std::vector<int8_t> randomAudio(inputTensor->bytes);
62 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
63
64 REQUIRE(RunInference(model, randomAudio.data(), inputTensor->bytes));
65 return true;
66}
67
Richard Burton0d110592021-08-12 17:26:30 +010068TEST_CASE("Running random inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter]")
alexander3c798932021-03-26 21:42:19 +000069{
70 arm::app::Wav2LetterModel model{};
71
72 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010073 REQUIRE(model.Init(arm::app::tensorArena,
74 sizeof(arm::app::tensorArena),
75 arm::app::asr::GetModelPointer(),
76 arm::app::asr::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000077 REQUIRE(model.IsInited());
78
79 REQUIRE(RunInferenceRandom(model));
80}
81
82
83template<typename T>
84void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
85{
86 TfLiteTensor* inputTensor = model.GetInputTensor(0);
87 REQUIRE(inputTensor);
88
89 REQUIRE(RunInference(model, input_goldenFV, inputTensor->bytes));
90
91 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
92
93 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000094 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000095 auto tensorData = tflite::GetTensorData<T>(outputTensor);
96 REQUIRE(tensorData);
97
98 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010099 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i])));
alexander3c798932021-03-26 21:42:19 +0000100 }
101}
102
Richard Burton0d110592021-08-12 17:26:30 +0100103TEST_CASE("Running inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter]")
alexander3c798932021-03-26 21:42:19 +0000104{
Richard Burton00553462021-11-10 16:27:14 +0000105 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_OFM_FILES);
106 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +0000107 auto input_goldenFV = get_ifm_data_array(i);;
108 auto output_goldenFV = get_ofm_data_array(i);
109
110 DYNAMIC_SECTION("Executing inference with re-init")
111 {
112 arm::app::Wav2LetterModel model{};
113
114 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100115 REQUIRE(model.Init(arm::app::tensorArena,
116 sizeof(arm::app::tensorArena),
117 arm::app::asr::GetModelPointer(),
118 arm::app::asr::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000119 REQUIRE(model.IsInited());
120
121 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
122
123 }
124 }
125}
126
127} //namespace
128} //namespace