blob: ace6684e57cbe07a72bc2d404ab986e5cb555f8f [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 {
26namespace app {
27 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010028 namespace kws {
Liam Barry213a5432022-05-09 17:06:19 +010029 extern uint8_t* GetModelPointer();
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010030 extern size_t GetModelLen();
31 } /* namespace kws */
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[])
38{
39 TfLiteTensor* inputTensor = model.GetInputTensor(0);
40 REQUIRE(inputTensor);
41
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000042 const size_t copySz =
43 inputTensor->bytes < IFM_0_DATA_SIZE ? inputTensor->bytes : IFM_0_DATA_SIZE;
alexander3c798932021-03-26 21:42:19 +000044 memcpy(inputTensor->data.data, vec, copySz);
45
46 return model.RunInference();
47}
48
49bool RunInferenceRandom(arm::app::Model& model)
50{
51 TfLiteTensor* inputTensor = model.GetInputTensor(0);
52 REQUIRE(inputTensor);
53
54 std::random_device rndDevice;
55 std::mt19937 mersenneGen{rndDevice()};
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000056 std::uniform_int_distribution<short> dist{-128, 127};
alexander3c798932021-03-26 21:42:19 +000057
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000058 auto gen = [&dist, &mersenneGen]() { return dist(mersenneGen); };
alexander3c798932021-03-26 21:42:19 +000059
60 std::vector<int8_t> randomAudio(inputTensor->bytes);
61 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
62
63 REQUIRE(RunInference(model, randomAudio.data()));
64 return true;
65}
66
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000067template <typename T>
alexander3c798932021-03-26 21:42:19 +000068void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
69{
70 REQUIRE(RunInference(model, input_goldenFV));
71
72 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
73
74 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000075 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000076 auto tensorData = tflite::GetTensorData<T>(outputTensor);
77 REQUIRE(tensorData);
78
79 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010080 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i])));
alexander3c798932021-03-26 21:42:19 +000081 }
82}
83
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000084TEST_CASE("Running random inference with TensorFlow Lite Micro and MicroNetKwsModel Int8",
85 "[MicroNetKws]")
alexander3c798932021-03-26 21:42:19 +000086{
Kshitij Sisodia76a15802021-12-24 11:05:11 +000087 arm::app::MicroNetKwsModel model{};
alexander3c798932021-03-26 21:42:19 +000088
89 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010090 REQUIRE(model.Init(arm::app::tensorArena,
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000091 sizeof(arm::app::tensorArena),
92 arm::app::kws::GetModelPointer(),
93 arm::app::kws::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000094 REQUIRE(model.IsInited());
95
96 REQUIRE(RunInferenceRandom(model));
97}
98
Kshitij Sisodia76a15802021-12-24 11:05:11 +000099TEST_CASE("Running inference with TensorFlow Lite Micro and MicroNetKwsModel int8", "[MicroNetKws]")
alexander3c798932021-03-26 21:42:19 +0000100{
Richard Burton00553462021-11-10 16:27:14 +0000101 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_OFM_FILES);
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000102 for (uint32_t i = 0; i < NUMBER_OF_IFM_FILES; ++i) {
103 const int8_t* input_goldenFV = GetIfmDataArray(i);
104 ;
105 const int8_t* output_goldenFV = GetOfmDataArray(i);
alexander3c798932021-03-26 21:42:19 +0000106
alexander31ae9f02022-02-10 16:15:54 +0000107 DYNAMIC_SECTION("Executing inference with re-init " << i)
alexander3c798932021-03-26 21:42:19 +0000108 {
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000109 arm::app::MicroNetKwsModel model{};
alexander3c798932021-03-26 21:42:19 +0000110
111 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100112 REQUIRE(model.Init(arm::app::tensorArena,
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000113 sizeof(arm::app::tensorArena),
114 arm::app::kws::GetModelPointer(),
115 arm::app::kws::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000116 REQUIRE(model.IsInited());
117
118 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
alexander3c798932021-03-26 21:42:19 +0000119 }
120 }
121}