Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2022 Arm Ltd and Contributors. All rights reserved. |
| 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: |
| 53 | case DataType::QAsymmS8: |
| 54 | case DataType::QSymmS8: |
| 55 | case DataType::QSymmS16: |
| 56 | case DataType::Signed32: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 57 | SetInput<int32_t>(runner, inputNames[inputSlotIdx], inputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 58 | break; |
| 59 | case DataType::Signed64: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 60 | SetInput<int64_t>(runner, inputNames[inputSlotIdx], inputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 61 | break; |
| 62 | case DataType::Boolean: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 63 | SetInput<unsigned char>(runner, inputNames[inputSlotIdx], inputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 64 | break; |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 65 | default: |
| 66 | throw armnn::Exception("Input data type is unsupported in TOSA Reference Backend."); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Run the TOSA Reference Model |
| 71 | status = runner.run(); |
| 72 | if(status != GraphStatus::TOSA_VALID) |
| 73 | { |
| 74 | throw armnn::Exception("An error has occurred while running the TOSA Reference Model."); |
| 75 | } |
| 76 | |
| 77 | // Gets the outputs |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 78 | for (uint32_t outputSlotIdx = 0; outputSlotIdx < outputNames.size(); ++outputSlotIdx) |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 79 | { |
| 80 | DataType dataType = m_workloadInfo.m_OutputTensorInfos[outputSlotIdx].GetDataType(); |
| 81 | switch (dataType) |
| 82 | { |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 83 | case DataType::Float16: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 84 | GetOutput<half_float::half>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 85 | break; |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 86 | case DataType::Float32: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 87 | GetOutput<float>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 88 | break; |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 89 | case DataType::QAsymmU8: |
| 90 | case DataType::QAsymmS8: |
| 91 | case DataType::QSymmS8: |
| 92 | case DataType::QSymmS16: |
| 93 | case DataType::Signed32: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 94 | GetOutput<int32_t>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 95 | break; |
| 96 | case DataType::Signed64: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 97 | GetOutput<int64_t>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 98 | break; |
| 99 | case DataType::Boolean: |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 100 | GetOutput<unsigned char>(runner, outputNames[outputSlotIdx], outputSlotIdx); |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 101 | break; |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 102 | default: |
| 103 | throw armnn::Exception("Output data type is unsupported in TOSA Reference Backend."); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | template <typename T> |
| 109 | void TosaRefPreCompiledWorkload::SetInput(TosaReference::IModelRunner& runner, |
| 110 | std::string inputName, |
| 111 | uint32_t inputIndex) const |
| 112 | { |
| 113 | std::vector<T> inputData(m_Data.m_Inputs[inputIndex]->GetShape().GetNumElements()); |
| 114 | m_Data.m_Inputs[inputIndex]->CopyOutTo(inputData.data()); |
| 115 | |
| 116 | runner.setInput<T>(inputName, inputData); |
| 117 | } |
| 118 | |
| 119 | template <typename T> |
| 120 | void TosaRefPreCompiledWorkload::GetOutput(TosaReference::IModelRunner& runner, |
| 121 | std::string outputName, |
| 122 | uint32_t outputIndex) const |
| 123 | { |
| 124 | std::vector<T> actualOutputs = runner.getOutput<T>(outputName); |
| 125 | |
| 126 | m_Data.m_Outputs[outputIndex]->CopyInFrom(actualOutputs.data()); |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | bool TosaRefPreCompiledWorkloadValidate(std::string*) |
| 130 | { |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | } //namespace armnn |