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" |
| 22 | #include "AdGoldenInput.hpp" |
| 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 | |
| 30 | bool RunInference(arm::app::Model& model, const int8_t vec[]) |
| 31 | { |
| 32 | TfLiteTensor *inputTensor = model.GetInputTensor(0); |
| 33 | REQUIRE(inputTensor); |
| 34 | |
| 35 | const size_t copySz = inputTensor->bytes < AD_IN_FEATURE_VEC_DATA_SIZE ? inputTensor->bytes : AD_IN_FEATURE_VEC_DATA_SIZE; |
| 36 | |
| 37 | memcpy(inputTensor->data.data, vec, copySz); |
| 38 | |
| 39 | return model.RunInference(); |
| 40 | } |
| 41 | |
| 42 | bool RunInferenceRandom(arm::app::Model& model) |
| 43 | { |
| 44 | TfLiteTensor *inputTensor = model.GetInputTensor(0); |
| 45 | REQUIRE(inputTensor); |
| 46 | |
| 47 | std::random_device rndDevice; |
| 48 | std::mt19937 mersenneGen{rndDevice()}; |
| 49 | std::uniform_int_distribution<short> dist{-128, 127}; |
| 50 | |
| 51 | auto gen = [&dist, &mersenneGen]() { |
| 52 | return dist(mersenneGen); |
| 53 | }; |
| 54 | |
| 55 | std::vector<int8_t> randomInput(inputTensor->bytes); |
| 56 | std::generate(std::begin(randomInput), std::end(randomInput), gen); |
| 57 | |
| 58 | REQUIRE(RunInference(model, randomInput.data())); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | template <typename T> |
| 63 | void TestInference(const T *input_goldenFV, const T *output_goldenFV, arm::app::Model& model) |
| 64 | { |
| 65 | REQUIRE(RunInference(model, (int8_t*)input_goldenFV)); |
| 66 | |
| 67 | TfLiteTensor *outputTensor = model.GetOutputTensor(0); |
| 68 | |
| 69 | REQUIRE(outputTensor); |
| 70 | REQUIRE(outputTensor->bytes == AD_OUT_FEATURE_VEC_DATA_SIZE); |
| 71 | auto tensorData = tflite::GetTensorData<T>(outputTensor); |
| 72 | REQUIRE(tensorData); |
| 73 | |
| 74 | for (size_t i = 0; i < outputTensor->bytes; i++) |
| 75 | { |
| 76 | REQUIRE((int)tensorData[i] == (int)((T)output_goldenFV[i])); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | TEST_CASE("Running random inference with TensorFlow Lite Micro and AdModel Int8", "[AD][.]") |
| 81 | { |
| 82 | arm::app::AdModel model{}; |
| 83 | |
| 84 | REQUIRE_FALSE(model.IsInited()); |
| 85 | REQUIRE(model.Init()); |
| 86 | REQUIRE(model.IsInited()); |
| 87 | |
| 88 | REQUIRE(RunInferenceRandom(model)); |
| 89 | } |
| 90 | |
| 91 | TEST_CASE("Running golden vector inference with TensorFlow Lite Micro and AdModel Int8", "[AD][.]") |
| 92 | { |
| 93 | arm::app::AdModel model{}; |
| 94 | |
| 95 | REQUIRE_FALSE(model.IsInited()); |
| 96 | REQUIRE(model.Init()); |
| 97 | REQUIRE(model.IsInited()); |
| 98 | |
| 99 | TestInference(ad_golden_input, ad_golden_out, model); |
| 100 | } |