blob: a6f7a03d2f30e2c0a3641e1c2a3d74b3fdc10a16 [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 */
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;
28
29 namespace kws {
30 extern uint8_t *GetModelPointer();
31 extern size_t GetModelLen();
32 } /* namespace kws */
33} /* namespace app */
34} /* namespace arm */
35
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010036using namespace test;
37
alexander3c798932021-03-26 21:42:19 +000038bool RunInference(arm::app::Model& model, const int8_t vec[])
39{
40 TfLiteTensor* inputTensor = model.GetInputTensor(0);
41 REQUIRE(inputTensor);
42
Richard Burton00553462021-11-10 16:27:14 +000043 const size_t copySz = inputTensor->bytes < IFM_0_DATA_SIZE ?
alexander3c798932021-03-26 21:42:19 +000044 inputTensor->bytes :
Richard Burton00553462021-11-10 16:27:14 +000045 IFM_0_DATA_SIZE;
alexander3c798932021-03-26 21:42:19 +000046 memcpy(inputTensor->data.data, vec, copySz);
47
48 return model.RunInference();
49}
50
51bool RunInferenceRandom(arm::app::Model& model)
52{
53 TfLiteTensor* inputTensor = model.GetInputTensor(0);
54 REQUIRE(inputTensor);
55
56 std::random_device rndDevice;
57 std::mt19937 mersenneGen{rndDevice()};
58 std::uniform_int_distribution<short> dist {-128, 127};
59
60 auto gen = [&dist, &mersenneGen](){
61 return dist(mersenneGen);
62 };
63
64 std::vector<int8_t> randomAudio(inputTensor->bytes);
65 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
66
67 REQUIRE(RunInference(model, randomAudio.data()));
68 return true;
69}
70
71template<typename T>
72void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
73{
74 REQUIRE(RunInference(model, input_goldenFV));
75
76 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
77
78 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000079 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000080 auto tensorData = tflite::GetTensorData<T>(outputTensor);
81 REQUIRE(tensorData);
82
83 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010084 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i])));
alexander3c798932021-03-26 21:42:19 +000085 }
86}
87
Kshitij Sisodia76a15802021-12-24 11:05:11 +000088TEST_CASE("Running random inference with TensorFlow Lite Micro and MicroNetKwsModel Int8", "[MicroNetKws]")
alexander3c798932021-03-26 21:42:19 +000089{
Kshitij Sisodia76a15802021-12-24 11:05:11 +000090 arm::app::MicroNetKwsModel model{};
alexander3c798932021-03-26 21:42:19 +000091
92 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010093 REQUIRE(model.Init(arm::app::tensorArena,
94 sizeof(arm::app::tensorArena),
95 arm::app::kws::GetModelPointer(),
96 arm::app::kws::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000097 REQUIRE(model.IsInited());
98
99 REQUIRE(RunInferenceRandom(model));
100}
101
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000102TEST_CASE("Running inference with TensorFlow Lite Micro and MicroNetKwsModel int8", "[MicroNetKws]")
alexander3c798932021-03-26 21:42:19 +0000103{
Richard Burton00553462021-11-10 16:27:14 +0000104 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_OFM_FILES);
105 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +0000106 const int8_t* input_goldenFV = get_ifm_data_array(i);;
107 const int8_t* output_goldenFV = get_ofm_data_array(i);
108
alexander31ae9f02022-02-10 16:15:54 +0000109 DYNAMIC_SECTION("Executing inference with re-init " << i)
alexander3c798932021-03-26 21:42:19 +0000110 {
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000111 arm::app::MicroNetKwsModel model{};
alexander3c798932021-03-26 21:42:19 +0000112
113 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100114 REQUIRE(model.Init(arm::app::tensorArena,
115 sizeof(arm::app::tensorArena),
116 arm::app::kws::GetModelPointer(),
117 arm::app::kws::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000118 REQUIRE(model.IsInited());
119
120 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
121
122 }
123 }
124}