alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates |
| 3 | * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0 |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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" |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 22 | #include "BufAttributes.hpp" |
| 23 | #include "TensorFlowLiteMicro.hpp" |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 24 | #include "TestData_ad.hpp" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 25 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 26 | |
| 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 Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 31 | namespace arm { |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 32 | namespace 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 Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 39 | } /* namespace arm */ |
| 40 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 41 | using namespace test; |
| 42 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 43 | bool RunInference(arm::app::Model& model, const int8_t vec[]) |
| 44 | { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 45 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 46 | REQUIRE(inputTensor); |
| 47 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 48 | const size_t copySz = inputTensor->bytes < AD_IN_FEATURE_VEC_DATA_SIZE |
| 49 | ? inputTensor->bytes |
| 50 | : AD_IN_FEATURE_VEC_DATA_SIZE; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 51 | |
| 52 | memcpy(inputTensor->data.data, vec, copySz); |
| 53 | |
| 54 | return model.RunInference(); |
| 55 | } |
| 56 | |
| 57 | bool RunInferenceRandom(arm::app::Model& model) |
| 58 | { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 59 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 60 | REQUIRE(inputTensor); |
| 61 | |
| 62 | std::random_device rndDevice; |
| 63 | std::mt19937 mersenneGen{rndDevice()}; |
| 64 | std::uniform_int_distribution<short> dist{-128, 127}; |
| 65 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 66 | auto gen = [&dist, &mersenneGen]() { return dist(mersenneGen); }; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | |
| 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 | |
| 75 | template <typename T> |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 76 | void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 77 | { |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 78 | REQUIRE(RunInference(model, static_cast<const T*>(input_goldenFV))); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 79 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 80 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 81 | |
| 82 | REQUIRE(outputTensor); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 83 | REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 84 | auto tensorData = tflite::GetTensorData<T>(outputTensor); |
| 85 | REQUIRE(tensorData); |
| 86 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 87 | for (size_t i = 0; i < outputTensor->bytes; i++) { |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 88 | REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i]))); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 92 | TEST_CASE("Running random inference with TensorFlow Lite Micro and AdModel Int8", "[AD]") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 93 | { |
| 94 | arm::app::AdModel model{}; |
| 95 | |
| 96 | REQUIRE_FALSE(model.IsInited()); |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 97 | REQUIRE(model.Init(arm::app::tensorArena, |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 98 | sizeof(arm::app::tensorArena), |
| 99 | arm::app::ad::GetModelPointer(), |
| 100 | arm::app::ad::GetModelLen())); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 101 | REQUIRE(model.IsInited()); |
| 102 | |
| 103 | REQUIRE(RunInferenceRandom(model)); |
| 104 | } |
| 105 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 106 | TEST_CASE("Running golden vector inference with TensorFlow Lite Micro and AdModel Int8", "[AD]") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 107 | { |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 108 | REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_IFM_FILES); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 109 | for (uint32_t i = 0; i < NUMBER_OF_IFM_FILES; ++i) { |
| 110 | auto input_goldenFV = GetIfmDataArray(i); |
| 111 | ; |
| 112 | auto output_goldenFV = GetOfmDataArray(i); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 113 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 114 | DYNAMIC_SECTION("Executing inference with re-init") |
| 115 | { |
| 116 | arm::app::AdModel model{}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 117 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 118 | REQUIRE_FALSE(model.IsInited()); |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 119 | REQUIRE(model.Init(arm::app::tensorArena, |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 120 | sizeof(arm::app::tensorArena), |
| 121 | arm::app::ad::GetModelPointer(), |
| 122 | arm::app::ad::GetModelLen())); |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 123 | REQUIRE(model.IsInited()); |
| 124 | |
| 125 | TestInference<int8_t>(input_goldenFV, output_goldenFV, model); |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 128 | } |