blob: 4991a30dcb6e3bd4881c2b7366afcf2367f29c79 [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 */
17
18#include <catch.hpp>
19#include <random>
20
21#include "AdModel.hpp"
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000022#include "BufAttributes.hpp"
23#include "TensorFlowLiteMicro.hpp"
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010024#include "TestData_ad.hpp"
alexander31ae9f02022-02-10 16:15:54 +000025#include "log_macros.h"
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 {
Liam Barry213a5432022-05-09 17:06:19 +010032namespace app {
33 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
34 namespace ad {
35 extern uint8_t* GetModelPointer();
36 extern size_t GetModelLen();
37 } /* namespace ad */
38} /* namespace app */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010039} /* namespace arm */
40
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010041using namespace test;
42
alexander3c798932021-03-26 21:42:19 +000043bool RunInference(arm::app::Model& model, const int8_t vec[])
44{
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000045 TfLiteTensor* inputTensor = model.GetInputTensor(0);
alexander3c798932021-03-26 21:42:19 +000046 REQUIRE(inputTensor);
47
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000048 const size_t copySz = inputTensor->bytes < AD_IN_FEATURE_VEC_DATA_SIZE
49 ? inputTensor->bytes
50 : AD_IN_FEATURE_VEC_DATA_SIZE;
alexander3c798932021-03-26 21:42:19 +000051
52 memcpy(inputTensor->data.data, vec, copySz);
53
54 return model.RunInference();
55}
56
57bool RunInferenceRandom(arm::app::Model& model)
58{
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000059 TfLiteTensor* inputTensor = model.GetInputTensor(0);
alexander3c798932021-03-26 21:42:19 +000060 REQUIRE(inputTensor);
61
62 std::random_device rndDevice;
63 std::mt19937 mersenneGen{rndDevice()};
64 std::uniform_int_distribution<short> dist{-128, 127};
65
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000066 auto gen = [&dist, &mersenneGen]() { return dist(mersenneGen); };
alexander3c798932021-03-26 21:42:19 +000067
68 std::vector<int8_t> randomInput(inputTensor->bytes);
69 std::generate(std::begin(randomInput), std::end(randomInput), gen);
70
71 REQUIRE(RunInference(model, randomInput.data()));
72 return true;
73}
74
75template <typename T>
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000076void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
alexander3c798932021-03-26 21:42:19 +000077{
Isabella Gottardi79d41542021-10-20 15:52:32 +010078 REQUIRE(RunInference(model, static_cast<const T*>(input_goldenFV)));
alexander3c798932021-03-26 21:42:19 +000079
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000080 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
alexander3c798932021-03-26 21:42:19 +000081
82 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000083 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000084 auto tensorData = tflite::GetTensorData<T>(outputTensor);
85 REQUIRE(tensorData);
86
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000087 for (size_t i = 0; i < outputTensor->bytes; i++) {
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,
Liam Barry213a5432022-05-09 17:06:19 +010098 sizeof(arm::app::tensorArena),
99 arm::app::ad::GetModelPointer(),
100 arm::app::ad::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);
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000109 for (uint32_t i = 0; i < NUMBER_OF_IFM_FILES; ++i) {
110 auto input_goldenFV = GetIfmDataArray(i);
111 ;
112 auto output_goldenFV = GetOfmDataArray(i);
alexander3c798932021-03-26 21:42:19 +0000113
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100114 DYNAMIC_SECTION("Executing inference with re-init")
115 {
116 arm::app::AdModel model{};
alexander3c798932021-03-26 21:42:19 +0000117
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100118 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100119 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +0100120 sizeof(arm::app::tensorArena),
121 arm::app::ad::GetModelPointer(),
122 arm::app::ad::GetModelLen()));
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100123 REQUIRE(model.IsInited());
124
125 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100126 }
127 }
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100128}