blob: 643f805d87fe801b87ea8de1390bd0a6020fd318 [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 {
26namespace 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 */
33} /* namespace arm */
34
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010035using namespace test;
36
alexander3c798932021-03-26 21:42:19 +000037bool RunInference(arm::app::Model& model, const int8_t vec[], const size_t copySz)
38{
39 TfLiteTensor* inputTensor = model.GetInputTensor(0);
40 REQUIRE(inputTensor);
41
42 memcpy(inputTensor->data.data, vec, copySz);
43
44 return model.RunInference();
45}
46
47bool RunInferenceRandom(arm::app::Model& model)
48{
49 TfLiteTensor* inputTensor = model.GetInputTensor(0);
50 REQUIRE(inputTensor);
51
52 std::random_device rndDevice;
53 std::mt19937 mersenneGen{rndDevice()};
54 std::uniform_int_distribution<short> dist {-128, 127};
55
56 auto gen = [&dist, &mersenneGen](){
57 return dist(mersenneGen);
58 };
59
60 std::vector<int8_t> randomAudio(inputTensor->bytes);
61 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
62
63 REQUIRE(RunInference(model, randomAudio.data(), inputTensor->bytes));
64 return true;
65}
66
Richard Burton0d110592021-08-12 17:26:30 +010067TEST_CASE("Running random inference with TensorFlow Lite Micro and Wav2LetterModel Int8", "[Wav2Letter]")
alexander3c798932021-03-26 21:42:19 +000068{
69 arm::app::Wav2LetterModel model{};
70
71 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010072 REQUIRE(model.Init(arm::app::tensorArena,
73 sizeof(arm::app::tensorArena),
74 arm::app::asr::GetModelPointer(),
75 arm::app::asr::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000076 REQUIRE(model.IsInited());
77
78 REQUIRE(RunInferenceRandom(model));
79}
80
81template<typename T>
82void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
83{
84 TfLiteTensor* inputTensor = model.GetInputTensor(0);
85 REQUIRE(inputTensor);
86
87 REQUIRE(RunInference(model, input_goldenFV, inputTensor->bytes));
88
89 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
90
91 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000092 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000093 auto tensorData = tflite::GetTensorData<T>(outputTensor);
94 REQUIRE(tensorData);
95
96 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010097 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i])));
alexander3c798932021-03-26 21:42:19 +000098 }
99}
100
Richard Burton0d110592021-08-12 17:26:30 +0100101TEST_CASE("Running inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter]")
alexander3c798932021-03-26 21:42:19 +0000102{
Richard Burton00553462021-11-10 16:27:14 +0000103 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_IFM_FILES);
104 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +0000105 auto input_goldenFV = get_ifm_data_array(i);;
106 auto output_goldenFV = get_ofm_data_array(i);
107
108 DYNAMIC_SECTION("Executing inference with re-init")
109 {
110 arm::app::Wav2LetterModel model{};
111
112 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100113 REQUIRE(model.Init(arm::app::tensorArena,
114 sizeof(arm::app::tensorArena),
115 arm::app::asr::GetModelPointer(),
116 arm::app::asr::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000117 REQUIRE(model.IsInited());
118
119 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
120
121 }
122 }
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100123}