blob: 9fd817153df0e70ab835707bb99db578b01cb43d [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 */
Kshitij Sisodia76a15802021-12-24 11:05:11 +000017#include "MicroNetKwsModel.hpp"
alexander3c798932021-03-26 21:42:19 +000018#include "TestData_kws.hpp"
19#include "TensorFlowLiteMicro.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 kws {
29 extern uint8_t* GetModelPointer();
30 extern size_t GetModelLen();
31 } /* namespace kws */
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 kws {
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010037
38bool RunInference(arm::app::Model& model, const int8_t vec[]) {
alexander3c798932021-03-26 21:42:19 +000039 TfLiteTensor* inputTensor = model.GetInputTensor(0);
40 REQUIRE(inputTensor);
41
Richard Burton00553462021-11-10 16:27:14 +000042 const size_t copySz = inputTensor->bytes < IFM_0_DATA_SIZE ?
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010043 inputTensor->bytes :
Richard Burton00553462021-11-10 16:27:14 +000044 IFM_0_DATA_SIZE;
alexander3c798932021-03-26 21:42:19 +000045 memcpy(inputTensor->data.data, vec, copySz);
46
47 return model.RunInference();
48}
49
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010050bool RunInferenceRandom(arm::app::Model& model) {
alexander3c798932021-03-26 21:42:19 +000051 TfLiteTensor* inputTensor = model.GetInputTensor(0);
52 REQUIRE(inputTensor);
53
54 std::random_device rndDevice;
55 std::mt19937 mersenneGen{rndDevice()};
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010056 std::uniform_int_distribution<short> dist{-128, 127};
alexander3c798932021-03-26 21:42:19 +000057
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010058 auto gen = [&dist, &mersenneGen]() {
59 return dist(mersenneGen);
60 };
alexander3c798932021-03-26 21:42:19 +000061
62 std::vector<int8_t> randomAudio(inputTensor->bytes);
63 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
64
65 REQUIRE(RunInference(model, randomAudio.data()));
66 return true;
67}
68
69template<typename T>
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010070void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model) {
alexander3c798932021-03-26 21:42:19 +000071 REQUIRE(RunInference(model, input_goldenFV));
72
73 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
74
75 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000076 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000077 auto tensorData = tflite::GetTensorData<T>(outputTensor);
78 REQUIRE(tensorData);
79
80 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010081 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>((T) output_goldenFV[i]));
alexander3c798932021-03-26 21:42:19 +000082 }
83}
84
Kshitij Sisodia76a15802021-12-24 11:05:11 +000085TEST_CASE("Running random inference with Tflu and MicroNetKwsModel Int8", "[MicroNetKws]") {
86 arm::app::MicroNetKwsModel model{};
alexander3c798932021-03-26 21:42:19 +000087
88 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010089 REQUIRE(model.Init(arm::app::tensorArena,
90 sizeof(arm::app::tensorArena),
91 arm::app::kws::GetModelPointer(),
92 arm::app::kws::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000093 REQUIRE(model.IsInited());
94
95 REQUIRE(RunInferenceRandom(model));
96}
97
Kshitij Sisodia76a15802021-12-24 11:05:11 +000098TEST_CASE("Running inference with Tflu and MicroNetKwsModel Int8", "[MicroNetKws]") {
Richard Burton00553462021-11-10 16:27:14 +000099 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_OFM_FILES);
100 for (uint32_t i = 0; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +0000101 const int8_t* input_goldenFV = get_ifm_data_array(i);
102 const int8_t* output_goldenFV = get_ofm_data_array(i);
103
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100104 DYNAMIC_SECTION("Executing inference with re-init") {
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000105 arm::app::MicroNetKwsModel model{};
alexander3c798932021-03-26 21:42:19 +0000106
107 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100108 REQUIRE(model.Init(arm::app::tensorArena,
109 sizeof(arm::app::tensorArena),
110 arm::app::kws::GetModelPointer(),
111 arm::app::kws::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000112 REQUIRE(model.IsInited());
113
114 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
115
116 }
117 }
118}
119
120} //namespace
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100121} //namespace