Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 1 | // |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2024 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 "TosaRefLayerSupport.hpp" |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 7 | |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 8 | #include <tosaCommon/TosaMappings.hpp> |
| 9 | |
| 10 | #include <armnn/Types.hpp> |
| 11 | #include <armnn/utility/IgnoreUnused.hpp> |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 12 | |
| 13 | #include <graph_status.h> |
| 14 | #include <model_runner.h> |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 15 | |
| 16 | #include <vector> |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 17 | |
| 18 | namespace armnn |
| 19 | { |
| 20 | |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 21 | bool TosaRefLayerSupport::IsLayerSupported(const LayerType& type, |
| 22 | const std::vector<TensorInfo>& infos, |
| 23 | const BaseDescriptor& descriptor, |
| 24 | const Optional<LstmInputParamsInfo>& lstmParamsInfo, |
| 25 | const Optional<QuantizedLstmInputParamsInfo>& quantizedLstmInputParamsInfo, |
| 26 | Optional<std::string&> reasonIfUnsupported) const |
| 27 | { |
| 28 | IgnoreUnused(lstmParamsInfo); |
| 29 | IgnoreUnused(quantizedLstmInputParamsInfo); |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 30 | IgnoreUnused(reasonIfUnsupported); |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 31 | |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 32 | std::vector<const TensorInfo*> inputInfos; |
| 33 | std::vector<const TensorInfo*> outputInfos; |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 34 | |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 35 | switch (type) |
| 36 | { |
Tracy Narine | 10403ec | 2023-11-28 11:55:08 +0000 | [diff] [blame] | 37 | case LayerType::Activation: |
| 38 | inputInfos.push_back(&infos[0]); |
| 39 | outputInfos.push_back(&infos[1]); |
| 40 | break; |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 41 | case LayerType::Input: |
| 42 | case LayerType::Output: |
| 43 | return true; |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 44 | case LayerType::Addition: |
Nikhil Raj | 9a33946 | 2022-12-05 11:24:35 +0000 | [diff] [blame] | 45 | case LayerType::Multiplication: |
| 46 | case LayerType::Subtraction: |
Tianle Cheng | 7790dc6 | 2023-12-12 13:52:22 +0000 | [diff] [blame] | 47 | case LayerType::ElementwiseBinary: |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 48 | // Setup inputs and outputs |
| 49 | inputInfos.push_back(&infos[0]); |
| 50 | inputInfos.push_back(&infos[1]); |
| 51 | outputInfos.push_back(&infos[2]); |
| 52 | break; |
Kevin May | 5b58e31 | 2022-12-15 10:15:21 +0000 | [diff] [blame] | 53 | case LayerType::Concat: |
| 54 | for (unsigned int i = 0; i < infos.size() - 1; ++i) |
| 55 | { |
| 56 | inputInfos.push_back(&infos[i]); |
| 57 | } |
| 58 | outputInfos.push_back(&infos.back()); |
| 59 | break; |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 60 | case LayerType::Constant: |
| 61 | outputInfos.push_back(&infos[0]); |
| 62 | break; |
| 63 | case LayerType::Convolution2d: |
| 64 | { |
| 65 | inputInfos.push_back(&infos[0]); // input |
| 66 | outputInfos.push_back(&infos[1]); // output |
| 67 | inputInfos.push_back(&infos[2]); // weights |
| 68 | |
| 69 | auto conv2dDesc = PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor); |
| 70 | if(conv2dDesc->m_BiasEnabled) |
| 71 | { |
| 72 | inputInfos.push_back(&infos[3]); // bias |
| 73 | } |
| 74 | break; |
| 75 | } |
David Monahan | d7fca09 | 2023-01-12 14:53:34 +0000 | [diff] [blame] | 76 | case LayerType::ElementwiseUnary: |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 77 | case LayerType::Pooling2d: |
Teresa Charlin | ca5a23a | 2023-12-15 14:20:47 +0000 | [diff] [blame] | 78 | case LayerType::Quantize: |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 79 | case LayerType::Reshape: |
Teresa Charlin | ce65588 | 2023-11-21 15:44:13 +0000 | [diff] [blame] | 80 | case LayerType::Resize: |
Cathal Corbett | 3b9acd5 | 2022-12-09 12:17:27 +0000 | [diff] [blame] | 81 | case LayerType::Slice: |
Cathal Corbett | 0bb096d | 2022-12-22 13:09:38 +0000 | [diff] [blame] | 82 | case LayerType::Transpose: |
Kevin May | 1bea6be | 2023-12-12 11:18:46 +0000 | [diff] [blame] | 83 | { |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 84 | inputInfos.push_back(&infos[0]); |
| 85 | outputInfos.push_back(&infos[1]); |
| 86 | break; |
Kevin May | 1bea6be | 2023-12-12 11:18:46 +0000 | [diff] [blame] | 87 | } |
| 88 | case LayerType::Splitter: |
| 89 | { |
| 90 | inputInfos.push_back(&infos[0]); |
| 91 | for (unsigned int i = 1; i < infos.size(); ++i) |
| 92 | { |
| 93 | outputInfos.push_back(&infos[i]); |
| 94 | } |
| 95 | break; |
| 96 | } |
Matthew Sloyan | fc9d5e7 | 2022-12-08 13:38:23 +0000 | [diff] [blame] | 97 | case LayerType::TransposeConvolution2d: |
| 98 | { |
| 99 | inputInfos.push_back(&infos[0]); // input |
| 100 | outputInfos.push_back(&infos[1]); // output |
| 101 | inputInfos.push_back(&infos[2]); // weights |
| 102 | |
| 103 | auto conv2dDesc = PolymorphicDowncast<const TransposeConvolution2dDescriptor*>(&descriptor); |
| 104 | if(conv2dDesc->m_BiasEnabled) |
| 105 | { |
| 106 | inputInfos.push_back(&infos[3]); // bias |
| 107 | } |
| 108 | break; |
| 109 | } |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 110 | default: |
David Monahan | d7fca09 | 2023-01-12 14:53:34 +0000 | [diff] [blame] | 111 | // Default to false for all unsupported layers. |
| 112 | return false; |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 113 | } |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 114 | |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 115 | auto mappings = GetTosaMapping(nullptr, type, inputInfos, outputInfos, descriptor); |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 116 | if (mappings->GetName() == "") |
| 117 | { |
| 118 | // There currently isn't a TOSA mapping for this layer, as the default was returned. |
| 119 | return false; |
| 120 | } |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 121 | |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 122 | TosaSerializationHandler handler; |
| 123 | |
Narumol Prangnawarat | ad323af | 2023-09-29 17:00:38 +0100 | [diff] [blame] | 124 | // Add all mappings to main block. |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 125 | auto* block = new TosaSerializationBasicBlock("main", |
Narumol Prangnawarat | ad323af | 2023-09-29 17:00:38 +0100 | [diff] [blame] | 126 | "main", |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 127 | mappings->GetOperators(), |
| 128 | mappings->GetTensors(), |
| 129 | mappings->GetInputs(), |
| 130 | mappings->GetOutputs()); |
Narumol Prangnawarat | ad323af | 2023-09-29 17:00:38 +0100 | [diff] [blame] | 131 | |
| 132 | std::vector<TosaSerializationBasicBlock*> blocks; |
| 133 | blocks.emplace_back(block); |
| 134 | |
| 135 | // Add blocks to the main region. |
| 136 | auto* region = new TosaSerializationRegion("main", blocks); |
| 137 | handler.GetRegions().emplace_back(region); |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 138 | |
| 139 | GraphStatus status; |
| 140 | TosaReference::IModelRunner runner; |
| 141 | |
| 142 | #if !defined(TOSA_REFERENCE_MODEL_OUTPUT) |
| 143 | // There currently isn't a way to disable the output from the TOSA Reference Model, but it does have a file pointer |
| 144 | // to write debug output to, so set this to /dev/null (if it exists on the system) to hide the output. |
| 145 | func_debug_t funcDebug; |
| 146 | |
| 147 | FILE* file = fopen("/dev/null", "w"); |
| 148 | funcDebug.func_debug_file = (file == nullptr) ? stderr : file; |
| 149 | |
| 150 | runner.setFuncDebug(funcDebug); |
| 151 | #endif |
| 152 | |
| 153 | // Initialise the model runner with the TosaSerializationHandler, which runs validation on the mapping. |
| 154 | status = runner.initialize(handler); |
| 155 | |
| 156 | #if !defined(TOSA_REFERENCE_MODEL_OUTPUT) |
| 157 | // Reset FuncDebug as they can persist across multiple IModelRunner instances. |
| 158 | funcDebug.func_debug_file = stderr; |
| 159 | runner.setFuncDebug(funcDebug); |
| 160 | #endif |
| 161 | |
| 162 | if(status == GraphStatus::TOSA_ERROR || status == GraphStatus::TOSA_UNPREDICTABLE) |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 163 | { |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 164 | return false; |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 165 | } |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 166 | else |
| 167 | { |
| 168 | return true; |
| 169 | } |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | } // namespace armnn |