blob: 5f5ad98ade1de2411a0b3b399bc8e6a1b6b3f89c [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 */
17#include "hal.h"
18#include "TensorFlowLiteMicro.hpp"
19#include "Wav2LetterModel.hpp"
20#include "TestData_asr.hpp"
21
22#include <catch.hpp>
23#include <random>
24
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010025namespace test {
alexander3c798932021-03-26 21:42:19 +000026namespace asr {
27
28bool RunInference(arm::app::Model& model, const int8_t vec[], const size_t copySz)
29{
30 TfLiteTensor* inputTensor = model.GetInputTensor(0);
31 REQUIRE(inputTensor);
32
33 memcpy(inputTensor->data.data, vec, copySz);
34
35 return model.RunInference();
36}
37
38bool RunInferenceRandom(arm::app::Model& model)
39{
40 TfLiteTensor* inputTensor = model.GetInputTensor(0);
41 REQUIRE(inputTensor);
42
43 std::random_device rndDevice;
44 std::mt19937 mersenneGen{rndDevice()};
45 std::uniform_int_distribution<short> dist {-128, 127};
46
47 auto gen = [&dist, &mersenneGen](){
48 return dist(mersenneGen);
49 };
50
51 std::vector<int8_t> randomAudio(inputTensor->bytes);
52 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
53
54 REQUIRE(RunInference(model, randomAudio.data(), inputTensor->bytes));
55 return true;
56}
57
Richard Burton0d110592021-08-12 17:26:30 +010058TEST_CASE("Running random inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter]")
alexander3c798932021-03-26 21:42:19 +000059{
60 arm::app::Wav2LetterModel model{};
61
62 REQUIRE_FALSE(model.IsInited());
63 REQUIRE(model.Init());
64 REQUIRE(model.IsInited());
65
66 REQUIRE(RunInferenceRandom(model));
67}
68
69
70template<typename T>
71void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
72{
73 TfLiteTensor* inputTensor = model.GetInputTensor(0);
74 REQUIRE(inputTensor);
75
76 REQUIRE(RunInference(model, input_goldenFV, inputTensor->bytes));
77
78 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
79
80 REQUIRE(outputTensor);
81 REQUIRE(outputTensor->bytes == OFM_DATA_SIZE);
82 auto tensorData = tflite::GetTensorData<T>(outputTensor);
83 REQUIRE(tensorData);
84
85 for (size_t i = 0; i < outputTensor->bytes; i++) {
86 REQUIRE((int)tensorData[i] == (int)((T)output_goldenFV[i]));
87 }
88}
89
Richard Burton0d110592021-08-12 17:26:30 +010090TEST_CASE("Running inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter]")
alexander3c798932021-03-26 21:42:19 +000091{
92 for (uint32_t i = 0 ; i < NUMBER_OF_FM_FILES; ++i) {
93 auto input_goldenFV = get_ifm_data_array(i);;
94 auto output_goldenFV = get_ofm_data_array(i);
95
96 DYNAMIC_SECTION("Executing inference with re-init")
97 {
98 arm::app::Wav2LetterModel model{};
99
100 REQUIRE_FALSE(model.IsInited());
101 REQUIRE(model.Init());
102 REQUIRE(model.IsInited());
103
104 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
105
106 }
107 }
108}
109
110} //namespace
111} //namespace