alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 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 Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame^] | 22 | #include "TestData_ad.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 23 | #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 Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame^] | 30 | using namespace test; |
| 31 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 32 | bool 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 | |
| 44 | bool 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 | |
| 64 | template <typename T> |
| 65 | void TestInference(const T *input_goldenFV, const T *output_goldenFV, arm::app::Model& model) |
| 66 | { |
| 67 | REQUIRE(RunInference(model, (int8_t*)input_goldenFV)); |
| 68 | |
| 69 | TfLiteTensor *outputTensor = model.GetOutputTensor(0); |
| 70 | |
| 71 | REQUIRE(outputTensor); |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame^] | 72 | REQUIRE(outputTensor->bytes == OFM_DATA_SIZE); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 73 | auto tensorData = tflite::GetTensorData<T>(outputTensor); |
| 74 | REQUIRE(tensorData); |
| 75 | |
| 76 | for (size_t i = 0; i < outputTensor->bytes; i++) |
| 77 | { |
| 78 | REQUIRE((int)tensorData[i] == (int)((T)output_goldenFV[i])); |
| 79 | } |
| 80 | } |
| 81 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame^] | 82 | TEST_CASE("Running random inference with TensorFlow Lite Micro and AdModel Int8", "[AD]") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 83 | { |
| 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 Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame^] | 93 | 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] | 94 | { |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame^] | 95 | 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); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 98 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame^] | 99 | DYNAMIC_SECTION("Executing inference with re-init") |
| 100 | { |
| 101 | arm::app::AdModel model{}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 102 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame^] | 103 | 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 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 111 | } |