Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 1 | // |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 2 | // Copyright © 2021, 2023 Arm Ltd and Contributors. All rights reserved. |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "TestUtils.hpp" |
| 9 | |
| 10 | #include <armnn_delegate.hpp> |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 11 | #include <DelegateTestInterpreter.hpp> |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 12 | |
| 13 | #include <flatbuffers/flatbuffers.h> |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 14 | #include <tensorflow/lite/kernels/register.h> |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 15 | #include <tensorflow/lite/version.h> |
| 16 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 17 | #include <schema_generated.h> |
| 18 | |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 19 | #include <doctest/doctest.h> |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | std::vector<char> CreateCastTfLiteModel(tflite::TensorType inputTensorType, |
| 24 | tflite::TensorType outputTensorType, |
| 25 | const std::vector <int32_t>& tensorShape, |
| 26 | float quantScale = 1.0f, |
| 27 | int quantOffset = 0) |
| 28 | { |
| 29 | using namespace tflite; |
| 30 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 31 | |
| 32 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 33 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 34 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 35 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 36 | |
| 37 | auto quantizationParameters = |
| 38 | CreateQuantizationParameters(flatBufferBuilder, |
| 39 | 0, |
| 40 | 0, |
| 41 | flatBufferBuilder.CreateVector<float>({quantScale}), |
| 42 | flatBufferBuilder.CreateVector<int64_t>({quantOffset})); |
| 43 | |
| 44 | std::array<flatbuffers::Offset<Tensor>, 2> tensors; |
| 45 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 46 | flatBufferBuilder.CreateVector<int32_t>(tensorShape.data(), |
| 47 | tensorShape.size()), |
| 48 | inputTensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 49 | 1, |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 50 | flatBufferBuilder.CreateString("input"), |
| 51 | quantizationParameters); |
| 52 | tensors[1] = CreateTensor(flatBufferBuilder, |
| 53 | flatBufferBuilder.CreateVector<int32_t>(tensorShape.data(), |
| 54 | tensorShape.size()), |
| 55 | outputTensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 56 | 2, |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 57 | flatBufferBuilder.CreateString("output"), |
| 58 | quantizationParameters); |
| 59 | |
| 60 | const std::vector<int32_t> operatorInputs({0}); |
| 61 | const std::vector<int32_t> operatorOutputs({1}); |
| 62 | |
| 63 | flatbuffers::Offset<Operator> castOperator = |
| 64 | CreateOperator(flatBufferBuilder, |
| 65 | 0, |
| 66 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 67 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 68 | BuiltinOptions_CastOptions, |
| 69 | CreateCastOptions(flatBufferBuilder).Union()); |
| 70 | |
| 71 | flatbuffers::Offset<flatbuffers::String> modelDescription = |
| 72 | flatBufferBuilder.CreateString("ArmnnDelegate: CAST Operator Model"); |
| 73 | flatbuffers::Offset<OperatorCode> operatorCode = |
| 74 | CreateOperatorCode(flatBufferBuilder, tflite::BuiltinOperator_CAST); |
| 75 | |
| 76 | const std::vector<int32_t> subgraphInputs({0}); |
| 77 | const std::vector<int32_t> subgraphOutputs({1}); |
| 78 | flatbuffers::Offset<SubGraph> subgraph = |
| 79 | CreateSubGraph(flatBufferBuilder, |
| 80 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 81 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 82 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
| 83 | flatBufferBuilder.CreateVector(&castOperator, 1)); |
| 84 | |
| 85 | flatbuffers::Offset<Model> flatbufferModel = |
| 86 | CreateModel(flatBufferBuilder, |
| 87 | TFLITE_SCHEMA_VERSION, |
| 88 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 89 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 90 | modelDescription, |
| 91 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 92 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 93 | flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER); |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 94 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 95 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 96 | } |
| 97 | |
| 98 | template<typename T, typename K> |
| 99 | void CastTest(tflite::TensorType inputTensorType, |
| 100 | tflite::TensorType outputTensorType, |
| 101 | std::vector<armnn::BackendId>& backends, |
| 102 | std::vector<int32_t>& shape, |
| 103 | std::vector<T>& inputValues, |
| 104 | std::vector<K>& expectedOutputValues, |
| 105 | float quantScale = 1.0f, |
| 106 | int quantOffset = 0) |
| 107 | { |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 108 | using namespace delegateTestInterpreter; |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 109 | std::vector<char> modelBuffer = CreateCastTfLiteModel(inputTensorType, |
| 110 | outputTensorType, |
| 111 | shape, |
| 112 | quantScale, |
| 113 | quantOffset); |
| 114 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 115 | // Setup interpreter with just TFLite Runtime. |
| 116 | auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer); |
| 117 | CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk); |
| 118 | CHECK(tfLiteInterpreter.FillInputTensor<T>(inputValues, 0) == kTfLiteOk); |
| 119 | CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk); |
| 120 | std::vector<K> tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<K>(0); |
| 121 | std::vector<int32_t> tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(0); |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 122 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 123 | // Setup interpreter with Arm NN Delegate applied. |
| 124 | auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, backends); |
| 125 | CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk); |
| 126 | CHECK(armnnInterpreter.FillInputTensor<T>(inputValues, 0) == kTfLiteOk); |
| 127 | CHECK(armnnInterpreter.Invoke() == kTfLiteOk); |
| 128 | std::vector<K> armnnOutputValues = armnnInterpreter.GetOutputResult<K>(0); |
| 129 | std::vector<int32_t> armnnOutputShape = armnnInterpreter.GetOutputShape(0); |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 130 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 131 | armnnDelegate::CompareOutputData<K>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues); |
| 132 | armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, shape); |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 133 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 134 | tfLiteInterpreter.Cleanup(); |
| 135 | armnnInterpreter.Cleanup(); |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | } // anonymous namespace |