blob: 991617c6ce11341e0d924e395917609c6f2d2471 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Kshitij Sisodia2ea46232022-12-19 16:37:33 +00002 * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates
3 * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0
alexander3c798932021-03-26 21:42:19 +00004 *
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 */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010017#include "BufAttributes.hpp"
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000018#include "TensorFlowLiteMicro.hpp"
19#include "TestData_asr.hpp"
20#include "Wav2LetterModel.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()};
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000054 std::uniform_int_distribution<short> dist{-128, 127};
alexander3c798932021-03-26 21:42:19 +000055
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000056 auto gen = [&dist, &mersenneGen]() { return dist(mersenneGen); };
alexander3c798932021-03-26 21:42:19 +000057
58 std::vector<int8_t> randomAudio(inputTensor->bytes);
59 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
60
61 REQUIRE(RunInference(model, randomAudio.data(), inputTensor->bytes));
62 return true;
63}
64
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000065TEST_CASE("Running random inference with TensorFlow Lite Micro and Wav2LetterModel Int8",
66 "[Wav2Letter]")
alexander3c798932021-03-26 21:42:19 +000067{
68 arm::app::Wav2LetterModel model{};
69
70 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010071 REQUIRE(model.Init(arm::app::tensorArena,
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000072 sizeof(arm::app::tensorArena),
73 arm::app::asr::GetModelPointer(),
74 arm::app::asr::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000075 REQUIRE(model.IsInited());
76
77 REQUIRE(RunInferenceRandom(model));
78}
79
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000080template <typename T>
alexander3c798932021-03-26 21:42:19 +000081void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
82{
83 TfLiteTensor* inputTensor = model.GetInputTensor(0);
84 REQUIRE(inputTensor);
85
86 REQUIRE(RunInference(model, input_goldenFV, inputTensor->bytes));
87
88 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
89
90 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000091 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000092 auto tensorData = tflite::GetTensorData<T>(outputTensor);
93 REQUIRE(tensorData);
94
95 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010096 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i])));
alexander3c798932021-03-26 21:42:19 +000097 }
98}
99
Richard Burton0d110592021-08-12 17:26:30 +0100100TEST_CASE("Running inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter]")
alexander3c798932021-03-26 21:42:19 +0000101{
Richard Burton00553462021-11-10 16:27:14 +0000102 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_IFM_FILES);
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000103 for (uint32_t i = 0; i < NUMBER_OF_IFM_FILES; ++i) {
104 auto input_goldenFV = GetIfmDataArray(i);
105 ;
106 auto output_goldenFV = GetOfmDataArray(i);
alexander3c798932021-03-26 21:42:19 +0000107
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,
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000114 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);
alexander3c798932021-03-26 21:42:19 +0000120 }
121 }
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100122}