blob: ee63c2ffe34e12261db5f8c61b80aca098450006 [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
25namespace arm {
26namespace app {
27namespace asr {
28
29bool RunInference(arm::app::Model& model, const int8_t vec[], const size_t copySz)
30{
31 TfLiteTensor* inputTensor = model.GetInputTensor(0);
32 REQUIRE(inputTensor);
33
34 memcpy(inputTensor->data.data, vec, copySz);
35
36 return model.RunInference();
37}
38
39bool RunInferenceRandom(arm::app::Model& model)
40{
41 TfLiteTensor* inputTensor = model.GetInputTensor(0);
42 REQUIRE(inputTensor);
43
44 std::random_device rndDevice;
45 std::mt19937 mersenneGen{rndDevice()};
46 std::uniform_int_distribution<short> dist {-128, 127};
47
48 auto gen = [&dist, &mersenneGen](){
49 return dist(mersenneGen);
50 };
51
52 std::vector<int8_t> randomAudio(inputTensor->bytes);
53 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
54
55 REQUIRE(RunInference(model, randomAudio.data(), inputTensor->bytes));
56 return true;
57}
58
59/* Skip this test, Wav2LetterModel if not Vela optimized but only from ML-zoo will fail. */
60TEST_CASE("Running random inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter][.]")
61{
62 arm::app::Wav2LetterModel model{};
63
64 REQUIRE_FALSE(model.IsInited());
65 REQUIRE(model.Init());
66 REQUIRE(model.IsInited());
67
68 REQUIRE(RunInferenceRandom(model));
69}
70
71
72template<typename T>
73void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
74{
75 TfLiteTensor* inputTensor = model.GetInputTensor(0);
76 REQUIRE(inputTensor);
77
78 REQUIRE(RunInference(model, input_goldenFV, inputTensor->bytes));
79
80 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
81
82 REQUIRE(outputTensor);
83 REQUIRE(outputTensor->bytes == OFM_DATA_SIZE);
84 auto tensorData = tflite::GetTensorData<T>(outputTensor);
85 REQUIRE(tensorData);
86
87 for (size_t i = 0; i < outputTensor->bytes; i++) {
88 REQUIRE((int)tensorData[i] == (int)((T)output_goldenFV[i]));
89 }
90}
91
92TEST_CASE("Running inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter][.]")
93{
94 for (uint32_t i = 0 ; i < NUMBER_OF_FM_FILES; ++i) {
95 auto input_goldenFV = get_ifm_data_array(i);;
96 auto output_goldenFV = get_ofm_data_array(i);
97
98 DYNAMIC_SECTION("Executing inference with re-init")
99 {
100 arm::app::Wav2LetterModel model{};
101
102 REQUIRE_FALSE(model.IsInited());
103 REQUIRE(model.Init());
104 REQUIRE(model.IsInited());
105
106 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
107
108 }
109 }
110}
111
112} //namespace
113} //namespace
114} //namespace