blob: ad785e80f060c1b1a971cbfcfda5487f76596f2c [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"
alexander3c798932021-03-26 21:42:19 +000023#include "hal.h"
24#include "TensorFlowLiteMicro.hpp"
25
26#ifndef AD_FEATURE_VEC_DATA_SIZE
27#define AD_IN_FEATURE_VEC_DATA_SIZE (1024)
28#endif /* AD_FEATURE_VEC_DATA_SIZE */
29
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010030using namespace test;
31
alexander3c798932021-03-26 21:42:19 +000032bool RunInference(arm::app::Model& model, const int8_t vec[])
33{
34 TfLiteTensor *inputTensor = model.GetInputTensor(0);
35 REQUIRE(inputTensor);
36
37 const size_t copySz = inputTensor->bytes < AD_IN_FEATURE_VEC_DATA_SIZE ? inputTensor->bytes : AD_IN_FEATURE_VEC_DATA_SIZE;
38
39 memcpy(inputTensor->data.data, vec, copySz);
40
41 return model.RunInference();
42}
43
44bool RunInferenceRandom(arm::app::Model& model)
45{
46 TfLiteTensor *inputTensor = model.GetInputTensor(0);
47 REQUIRE(inputTensor);
48
49 std::random_device rndDevice;
50 std::mt19937 mersenneGen{rndDevice()};
51 std::uniform_int_distribution<short> dist{-128, 127};
52
53 auto gen = [&dist, &mersenneGen]() {
54 return dist(mersenneGen);
55 };
56
57 std::vector<int8_t> randomInput(inputTensor->bytes);
58 std::generate(std::begin(randomInput), std::end(randomInput), gen);
59
60 REQUIRE(RunInference(model, randomInput.data()));
61 return true;
62}
63
64template <typename T>
65void TestInference(const T *input_goldenFV, const T *output_goldenFV, arm::app::Model& model)
66{
Isabella Gottardi79d41542021-10-20 15:52:32 +010067 REQUIRE(RunInference(model, static_cast<const T*>(input_goldenFV)));
alexander3c798932021-03-26 21:42:19 +000068
69 TfLiteTensor *outputTensor = model.GetOutputTensor(0);
70
71 REQUIRE(outputTensor);
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010072 REQUIRE(outputTensor->bytes == OFM_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000073 auto tensorData = tflite::GetTensorData<T>(outputTensor);
74 REQUIRE(tensorData);
75
76 for (size_t i = 0; i < outputTensor->bytes; i++)
77 {
Isabella Gottardi79d41542021-10-20 15:52:32 +010078 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i])));
alexander3c798932021-03-26 21:42:19 +000079 }
80}
81
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010082TEST_CASE("Running random inference with TensorFlow Lite Micro and AdModel Int8", "[AD]")
alexander3c798932021-03-26 21:42:19 +000083{
84 arm::app::AdModel model{};
85
86 REQUIRE_FALSE(model.IsInited());
87 REQUIRE(model.Init());
88 REQUIRE(model.IsInited());
89
90 REQUIRE(RunInferenceRandom(model));
91}
92
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010093TEST_CASE("Running golden vector inference with TensorFlow Lite Micro and AdModel Int8", "[AD]")
alexander3c798932021-03-26 21:42:19 +000094{
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010095 for (uint32_t i = 0 ; i < NUMBER_OF_FM_FILES; ++i) {
96 auto input_goldenFV = get_ifm_data_array(i);;
97 auto output_goldenFV = get_ofm_data_array(i);
alexander3c798932021-03-26 21:42:19 +000098
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010099 DYNAMIC_SECTION("Executing inference with re-init")
100 {
101 arm::app::AdModel model{};
alexander3c798932021-03-26 21:42:19 +0000102
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100103 REQUIRE_FALSE(model.IsInited());
104 REQUIRE(model.Init());
105 REQUIRE(model.IsInited());
106
107 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
108
109 }
110 }
alexander3c798932021-03-26 21:42:19 +0000111}