blob: 4cfd7842ca2657fdaefde7b085025311b940e18d [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 "MicroNetKwsModel.hpp"
19#include "TensorFlowLiteMicro.hpp"
20#include "TestData_kws.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
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000038 bool RunInference(arm::app::Model& model, const int8_t vec[])
39 {
40 TfLiteTensor* inputTensor = model.GetInputTensor(0);
41 REQUIRE(inputTensor);
alexander3c798932021-03-26 21:42:19 +000042
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000043 const size_t copySz =
44 inputTensor->bytes < IFM_0_DATA_SIZE ? inputTensor->bytes : IFM_0_DATA_SIZE;
45 memcpy(inputTensor->data.data, vec, copySz);
alexander3c798932021-03-26 21:42:19 +000046
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000047 return model.RunInference();
alexander3c798932021-03-26 21:42:19 +000048 }
alexander3c798932021-03-26 21:42:19 +000049
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000050 bool RunInferenceRandom(arm::app::Model& model)
51 {
52 TfLiteTensor* inputTensor = model.GetInputTensor(0);
53 REQUIRE(inputTensor);
alexander3c798932021-03-26 21:42:19 +000054
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000055 std::random_device rndDevice;
56 std::mt19937 mersenneGen{rndDevice()};
57 std::uniform_int_distribution<short> dist{-128, 127};
alexander3c798932021-03-26 21:42:19 +000058
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000059 auto gen = [&dist, &mersenneGen]() { return dist(mersenneGen); };
alexander3c798932021-03-26 21:42:19 +000060
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000061 std::vector<int8_t> randomAudio(inputTensor->bytes);
62 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
alexander3c798932021-03-26 21:42:19 +000063
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000064 REQUIRE(RunInference(model, randomAudio.data()));
65 return true;
66 }
alexander3c798932021-03-26 21:42:19 +000067
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000068 template <typename T>
69 void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
70 {
71 REQUIRE(RunInference(model, input_goldenFV));
alexander3c798932021-03-26 21:42:19 +000072
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000073 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
alexander3c798932021-03-26 21:42:19 +000074
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000075 REQUIRE(outputTensor);
76 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
77 auto tensorData = tflite::GetTensorData<T>(outputTensor);
78 REQUIRE(tensorData);
79
80 for (size_t i = 0; i < outputTensor->bytes; i++) {
81 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>((T)output_goldenFV[i]));
alexander3c798932021-03-26 21:42:19 +000082 }
83 }
alexander3c798932021-03-26 21:42:19 +000084
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000085 TEST_CASE("Running random inference with Tflu and MicroNetKwsModel Int8", "[MicroNetKws]")
86 {
87 arm::app::MicroNetKwsModel model{};
88
89 REQUIRE_FALSE(model.IsInited());
90 REQUIRE(model.Init(arm::app::tensorArena,
91 sizeof(arm::app::tensorArena),
92 arm::app::kws::GetModelPointer(),
93 arm::app::kws::GetModelLen()));
94 REQUIRE(model.IsInited());
95
96 REQUIRE(RunInferenceRandom(model));
97 }
98
99 TEST_CASE("Running inference with Tflu and MicroNetKwsModel Int8", "[MicroNetKws]")
100 {
101 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_OFM_FILES);
102 for (uint32_t i = 0; i < NUMBER_OF_IFM_FILES; ++i) {
103 const int8_t* input_goldenFV = GetIfmDataArray(i);
104 const int8_t* output_goldenFV = GetOfmDataArray(i);
105
106 DYNAMIC_SECTION("Executing inference with re-init")
107 {
108 arm::app::MicroNetKwsModel model{};
109
110 REQUIRE_FALSE(model.IsInited());
111 REQUIRE(model.Init(arm::app::tensorArena,
112 sizeof(arm::app::tensorArena),
113 arm::app::kws::GetModelPointer(),
114 arm::app::kws::GetModelLen()));
115 REQUIRE(model.IsInited());
116
117 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
118 }
119 }
120 }
121
122} // namespace kws
123} // namespace test