blob: d837617999bbf1296cea99d908e6c87a5ef88c69 [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 */
17
18#include <catch.hpp>
19#include <random>
20
21#include "AdModel.hpp"
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010022#include "TestData_ad.hpp"
alexander31ae9f02022-02-10 16:15:54 +000023#include "log_macros.h"
alexander3c798932021-03-26 21:42:19 +000024#include "TensorFlowLiteMicro.hpp"
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010025#include "BufAttributes.hpp"
alexander3c798932021-03-26 21:42:19 +000026
27#ifndef AD_FEATURE_VEC_DATA_SIZE
28#define AD_IN_FEATURE_VEC_DATA_SIZE (1024)
29#endif /* AD_FEATURE_VEC_DATA_SIZE */
30
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010031namespace arm {
32 namespace app {
33 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
34 } /* namespace app */
35} /* namespace arm */
36
37extern uint8_t* GetModelPointer();
38extern size_t GetModelLen();
39
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010040using namespace test;
41
alexander3c798932021-03-26 21:42:19 +000042bool RunInference(arm::app::Model& model, const int8_t vec[])
43{
44 TfLiteTensor *inputTensor = model.GetInputTensor(0);
45 REQUIRE(inputTensor);
46
47 const size_t copySz = inputTensor->bytes < AD_IN_FEATURE_VEC_DATA_SIZE ? inputTensor->bytes : AD_IN_FEATURE_VEC_DATA_SIZE;
48
49 memcpy(inputTensor->data.data, vec, copySz);
50
51 return model.RunInference();
52}
53
54bool RunInferenceRandom(arm::app::Model& model)
55{
56 TfLiteTensor *inputTensor = model.GetInputTensor(0);
57 REQUIRE(inputTensor);
58
59 std::random_device rndDevice;
60 std::mt19937 mersenneGen{rndDevice()};
61 std::uniform_int_distribution<short> dist{-128, 127};
62
63 auto gen = [&dist, &mersenneGen]() {
64 return dist(mersenneGen);
65 };
66
67 std::vector<int8_t> randomInput(inputTensor->bytes);
68 std::generate(std::begin(randomInput), std::end(randomInput), gen);
69
70 REQUIRE(RunInference(model, randomInput.data()));
71 return true;
72}
73
74template <typename T>
75void TestInference(const T *input_goldenFV, const T *output_goldenFV, arm::app::Model& model)
76{
Isabella Gottardi79d41542021-10-20 15:52:32 +010077 REQUIRE(RunInference(model, static_cast<const T*>(input_goldenFV)));
alexander3c798932021-03-26 21:42:19 +000078
79 TfLiteTensor *outputTensor = model.GetOutputTensor(0);
80
81 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000082 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000083 auto tensorData = tflite::GetTensorData<T>(outputTensor);
84 REQUIRE(tensorData);
85
86 for (size_t i = 0; i < outputTensor->bytes; i++)
87 {
Isabella Gottardi79d41542021-10-20 15:52:32 +010088 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i])));
alexander3c798932021-03-26 21:42:19 +000089 }
90}
91
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010092TEST_CASE("Running random inference with TensorFlow Lite Micro and AdModel Int8", "[AD]")
alexander3c798932021-03-26 21:42:19 +000093{
94 arm::app::AdModel model{};
95
96 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010097 REQUIRE(model.Init(arm::app::tensorArena,
98 sizeof(arm::app::tensorArena),
99 GetModelPointer(),
100 GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000101 REQUIRE(model.IsInited());
102
103 REQUIRE(RunInferenceRandom(model));
104}
105
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100106TEST_CASE("Running golden vector inference with TensorFlow Lite Micro and AdModel Int8", "[AD]")
alexander3c798932021-03-26 21:42:19 +0000107{
Richard Burton00553462021-11-10 16:27:14 +0000108 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_IFM_FILES);
109 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100110 auto input_goldenFV = get_ifm_data_array(i);;
111 auto output_goldenFV = get_ofm_data_array(i);
alexander3c798932021-03-26 21:42:19 +0000112
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100113 DYNAMIC_SECTION("Executing inference with re-init")
114 {
115 arm::app::AdModel model{};
alexander3c798932021-03-26 21:42:19 +0000116
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100117 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100118 REQUIRE(model.Init(arm::app::tensorArena,
119 sizeof(arm::app::tensorArena),
120 GetModelPointer(),
121 GetModelLen()));
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100122 REQUIRE(model.IsInited());
123
124 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
125
126 }
127 }
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100128}