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