blob: 27c6f9668cfdf8820a57d0c63e517012bbd6fda0 [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 {
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
Richard Burton00553462021-11-10 16:27:14 +000042 const size_t copySz = inputTensor->bytes < IFM_0_DATA_SIZE ?
alexander3c798932021-03-26 21:42:19 +000043 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
50bool RunInferenceRandom(arm::app::Model& model)
51{
52 TfLiteTensor* inputTensor = model.GetInputTensor(0);
53 REQUIRE(inputTensor);
54
55 std::random_device rndDevice;
56 std::mt19937 mersenneGen{rndDevice()};
57 std::uniform_int_distribution<short> dist {-128, 127};
58
59 auto gen = [&dist, &mersenneGen](){
60 return dist(mersenneGen);
61 };
62
63 std::vector<int8_t> randomAudio(inputTensor->bytes);
64 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
65
66 REQUIRE(RunInference(model, randomAudio.data()));
67 return true;
68}
69
70template<typename T>
71void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
72{
73 REQUIRE(RunInference(model, input_goldenFV));
74
75 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
76
77 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000078 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000079 auto tensorData = tflite::GetTensorData<T>(outputTensor);
80 REQUIRE(tensorData);
81
82 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010083 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i])));
alexander3c798932021-03-26 21:42:19 +000084 }
85}
86
Kshitij Sisodia76a15802021-12-24 11:05:11 +000087TEST_CASE("Running random inference with TensorFlow Lite Micro and MicroNetKwsModel Int8", "[MicroNetKws]")
alexander3c798932021-03-26 21:42:19 +000088{
Kshitij Sisodia76a15802021-12-24 11:05:11 +000089 arm::app::MicroNetKwsModel model{};
alexander3c798932021-03-26 21:42:19 +000090
91 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010092 REQUIRE(model.Init(arm::app::tensorArena,
93 sizeof(arm::app::tensorArena),
94 arm::app::kws::GetModelPointer(),
95 arm::app::kws::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000096 REQUIRE(model.IsInited());
97
98 REQUIRE(RunInferenceRandom(model));
99}
100
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000101TEST_CASE("Running inference with TensorFlow Lite Micro and MicroNetKwsModel int8", "[MicroNetKws]")
alexander3c798932021-03-26 21:42:19 +0000102{
Richard Burton00553462021-11-10 16:27:14 +0000103 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_OFM_FILES);
104 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +0000105 const int8_t* input_goldenFV = get_ifm_data_array(i);;
106 const int8_t* output_goldenFV = get_ofm_data_array(i);
107
alexander31ae9f02022-02-10 16:15:54 +0000108 DYNAMIC_SECTION("Executing inference with re-init " << i)
alexander3c798932021-03-26 21:42:19 +0000109 {
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000110 arm::app::MicroNetKwsModel model{};
alexander3c798932021-03-26 21:42:19 +0000111
112 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100113 REQUIRE(model.Init(arm::app::tensorArena,
114 sizeof(arm::app::tensorArena),
115 arm::app::kws::GetModelPointer(),
116 arm::app::kws::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000117 REQUIRE(model.IsInited());
118
119 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
120
121 }
122 }
123}