Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 1 | // |
Kevin May | 1bea6be | 2023-12-12 11:18:46 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "TosaRefPreCompiledWorkload.hpp" |
| 7 | |
| 8 | namespace armnn |
| 9 | { |
| 10 | |
| 11 | TosaRefPreCompiledWorkload::TosaRefPreCompiledWorkload(const PreCompiledQueueDescriptor& descriptor, |
| 12 | const WorkloadInfo& info) |
| 13 | : BaseWorkload<PreCompiledQueueDescriptor>(descriptor, info) |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 14 | , m_workloadInfo(info) |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 15 | { |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 16 | // Check that the workload is holding a pointer to a valid pre-compiled object |
| 17 | if (m_Data.m_PreCompiledObject == nullptr) |
| 18 | { |
| 19 | throw InvalidArgumentException( |
| 20 | "TosaRefPreCompiledWorkload requires a valid pre-compiled object (TosaSerializationHandler)."); |
| 21 | } |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 22 | } |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 23 | void TosaRefPreCompiledWorkload::Execute() const |
| 24 | { |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 25 | tosa::TosaSerializationHandler* handler = static_cast<tosa::TosaSerializationHandler*>(m_Data.m_PreCompiledObject); |
| 26 | |
Narumol Prangnawarat | ad323af | 2023-09-29 17:00:38 +0100 | [diff] [blame] | 27 | std::vector<std::string> inputNames = handler->GetMainRegion()->GetBlocks()[0]->GetInputs(); |
| 28 | std::vector<std::string> outputNames = handler->GetMainRegion()->GetBlocks()[0]->GetOutputs(); |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 29 | |
| 30 | TosaReference::IModelRunner runner; |
| 31 | GraphStatus status; |
| 32 | |
| 33 | // Initialise the model runner with the TosaSerializationHandler |
| 34 | status = runner.initialize(*handler); |
| 35 | if(status != GraphStatus::TOSA_VALID) |
| 36 | { |
| 37 | throw armnn::Exception("An error has occurred while initialising the TOSA Reference Model."); |
| 38 | } |
| 39 | |
| 40 | // Set the inputs |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 41 | for (uint32_t inputSlotIdx = 0; inputSlotIdx < inputNames.size(); ++inputSlotIdx) |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 42 | { |
| 43 | DataType dataType = m_workloadInfo.m_InputTensorInfos[inputSlotIdx].GetDataType(); |
| 44 | switch (dataType) |
| 45 | { |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 46 | case DataType::Float16: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 47 | SetInput<half_float::half>(runner, inputNames[inputSlotIdx], inputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 48 | break; |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 49 | case DataType::Float32: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 50 | SetInput<float>(runner, inputNames[inputSlotIdx], inputSlotIdx); |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 51 | break; |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 52 | case DataType::QAsymmU8: |
Teresa Charlin | ce65588 | 2023-11-21 15:44:13 +0000 | [diff] [blame] | 53 | SetInput<uint8_t, int32_t>(runner, inputNames[inputSlotIdx], inputSlotIdx); |
| 54 | break; |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 55 | case DataType::QAsymmS8: |
| 56 | case DataType::QSymmS8: |
Teresa Charlin | ce65588 | 2023-11-21 15:44:13 +0000 | [diff] [blame] | 57 | SetInput<int8_t, int32_t>(runner, inputNames[inputSlotIdx], inputSlotIdx); |
| 58 | break; |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 59 | case DataType::QSymmS16: |
Teresa Charlin | ce65588 | 2023-11-21 15:44:13 +0000 | [diff] [blame] | 60 | SetInput<int16_t, int32_t>(runner, inputNames[inputSlotIdx], inputSlotIdx); |
| 61 | break; |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 62 | case DataType::Signed32: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 63 | SetInput<int32_t>(runner, inputNames[inputSlotIdx], inputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 64 | break; |
| 65 | case DataType::Signed64: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 66 | SetInput<int64_t>(runner, inputNames[inputSlotIdx], inputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 67 | break; |
| 68 | case DataType::Boolean: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 69 | SetInput<unsigned char>(runner, inputNames[inputSlotIdx], inputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 70 | break; |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 71 | default: |
| 72 | throw armnn::Exception("Input data type is unsupported in TOSA Reference Backend."); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Run the TOSA Reference Model |
| 77 | status = runner.run(); |
| 78 | if(status != GraphStatus::TOSA_VALID) |
| 79 | { |
| 80 | throw armnn::Exception("An error has occurred while running the TOSA Reference Model."); |
| 81 | } |
| 82 | |
| 83 | // Gets the outputs |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 84 | for (uint32_t outputSlotIdx = 0; outputSlotIdx < outputNames.size(); ++outputSlotIdx) |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 85 | { |
| 86 | DataType dataType = m_workloadInfo.m_OutputTensorInfos[outputSlotIdx].GetDataType(); |
| 87 | switch (dataType) |
| 88 | { |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 89 | case DataType::Float16: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 90 | GetOutput<half_float::half>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 91 | break; |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 92 | case DataType::Float32: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 93 | GetOutput<float>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 94 | break; |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 95 | case DataType::QAsymmU8: |
Teresa Charlin | ce65588 | 2023-11-21 15:44:13 +0000 | [diff] [blame] | 96 | GetOutput<uint8_t, int32_t>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
| 97 | break; |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 98 | case DataType::QAsymmS8: |
| 99 | case DataType::QSymmS8: |
Teresa Charlin | ce65588 | 2023-11-21 15:44:13 +0000 | [diff] [blame] | 100 | GetOutput<int8_t, int32_t>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
| 101 | break; |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 102 | case DataType::QSymmS16: |
Teresa Charlin | ce65588 | 2023-11-21 15:44:13 +0000 | [diff] [blame] | 103 | GetOutput<int16_t, int32_t>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
| 104 | break; |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 105 | case DataType::Signed32: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 106 | GetOutput<int32_t>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 107 | break; |
| 108 | case DataType::Signed64: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 109 | GetOutput<int64_t>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 110 | break; |
| 111 | case DataType::Boolean: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 112 | GetOutput<unsigned char>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 113 | break; |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 114 | default: |
| 115 | throw armnn::Exception("Output data type is unsupported in TOSA Reference Backend."); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | template <typename T> |
| 121 | void TosaRefPreCompiledWorkload::SetInput(TosaReference::IModelRunner& runner, |
| 122 | std::string inputName, |
| 123 | uint32_t inputIndex) const |
| 124 | { |
Teresa Charlin | ce65588 | 2023-11-21 15:44:13 +0000 | [diff] [blame] | 125 | SetInput<T, T>(runner, inputName, inputIndex); |
| 126 | } |
| 127 | |
| 128 | template <typename T, typename Trunner> |
| 129 | void TosaRefPreCompiledWorkload::SetInput(TosaReference::IModelRunner& runner, |
| 130 | std::string inputName, |
| 131 | uint32_t inputIndex) const |
| 132 | { |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 133 | std::vector<T> inputData(m_Data.m_Inputs[inputIndex]->GetShape().GetNumElements()); |
Teresa Charlin | ce65588 | 2023-11-21 15:44:13 +0000 | [diff] [blame] | 134 | std::vector<Trunner> inputDataRunner(m_Data.m_Inputs[inputIndex]->GetShape().GetNumElements()); |
| 135 | |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 136 | m_Data.m_Inputs[inputIndex]->CopyOutTo(inputData.data()); |
| 137 | |
Teresa Charlin | ce65588 | 2023-11-21 15:44:13 +0000 | [diff] [blame] | 138 | std::transform(inputData.begin(), inputData.end(), |
| 139 | inputDataRunner.begin(), [](T x) { return static_cast<Trunner>(x);}); |
| 140 | |
| 141 | runner.setInput<Trunner>(inputName, inputDataRunner); |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | template <typename T> |
| 145 | void TosaRefPreCompiledWorkload::GetOutput(TosaReference::IModelRunner& runner, |
| 146 | std::string outputName, |
| 147 | uint32_t outputIndex) const |
| 148 | { |
Teresa Charlin | ce65588 | 2023-11-21 15:44:13 +0000 | [diff] [blame] | 149 | GetOutput<T, T>(runner, outputName, outputIndex); |
| 150 | } |
| 151 | |
| 152 | template <typename T, typename Trunner> |
| 153 | void TosaRefPreCompiledWorkload::GetOutput(TosaReference::IModelRunner& runner, |
| 154 | std::string outputName, |
| 155 | uint32_t outputIndex) const |
| 156 | { |
| 157 | std::vector<Trunner> actualOutputsRunner = runner.getOutput<Trunner>(outputName); |
| 158 | std::vector<T> actualOutputs (actualOutputsRunner.size()); |
| 159 | |
| 160 | std::transform(actualOutputsRunner.begin(), actualOutputsRunner.end(), |
| 161 | actualOutputs.begin(), [](Trunner x) { return static_cast<T>(x);}); |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 162 | |
| 163 | m_Data.m_Outputs[outputIndex]->CopyInFrom(actualOutputs.data()); |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | bool TosaRefPreCompiledWorkloadValidate(std::string*) |
| 167 | { |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | } //namespace armnn |