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 "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 | { |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 37 | case LayerType::Input: |
| 38 | case LayerType::Output: |
| 39 | return true; |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 40 | case LayerType::Addition: |
Nikhil Raj | 9a33946 | 2022-12-05 11:24:35 +0000 | [diff] [blame] | 41 | case LayerType::Multiplication: |
| 42 | case LayerType::Subtraction: |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 43 | // Setup inputs and outputs |
| 44 | inputInfos.push_back(&infos[0]); |
| 45 | inputInfos.push_back(&infos[1]); |
| 46 | outputInfos.push_back(&infos[2]); |
| 47 | break; |
Kevin May | 5b58e31 | 2022-12-15 10:15:21 +0000 | [diff] [blame] | 48 | case LayerType::Concat: |
| 49 | for (unsigned int i = 0; i < infos.size() - 1; ++i) |
| 50 | { |
| 51 | inputInfos.push_back(&infos[i]); |
| 52 | } |
| 53 | outputInfos.push_back(&infos.back()); |
| 54 | break; |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 55 | case LayerType::Constant: |
| 56 | outputInfos.push_back(&infos[0]); |
| 57 | break; |
| 58 | case LayerType::Convolution2d: |
| 59 | { |
| 60 | inputInfos.push_back(&infos[0]); // input |
| 61 | outputInfos.push_back(&infos[1]); // output |
| 62 | inputInfos.push_back(&infos[2]); // weights |
| 63 | |
| 64 | auto conv2dDesc = PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor); |
| 65 | if(conv2dDesc->m_BiasEnabled) |
| 66 | { |
| 67 | inputInfos.push_back(&infos[3]); // bias |
| 68 | } |
| 69 | break; |
| 70 | } |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 71 | case LayerType::Pooling2d: |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 72 | case LayerType::Reshape: |
Cathal Corbett | 3b9acd5 | 2022-12-09 12:17:27 +0000 | [diff] [blame] | 73 | case LayerType::Slice: |
Cathal Corbett | 0bb096d | 2022-12-22 13:09:38 +0000 | [diff] [blame^] | 74 | case LayerType::Transpose: |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 75 | inputInfos.push_back(&infos[0]); |
| 76 | outputInfos.push_back(&infos[1]); |
| 77 | break; |
Matthew Sloyan | fc9d5e7 | 2022-12-08 13:38:23 +0000 | [diff] [blame] | 78 | case LayerType::TransposeConvolution2d: |
| 79 | { |
| 80 | inputInfos.push_back(&infos[0]); // input |
| 81 | outputInfos.push_back(&infos[1]); // output |
| 82 | inputInfos.push_back(&infos[2]); // weights |
| 83 | |
| 84 | auto conv2dDesc = PolymorphicDowncast<const TransposeConvolution2dDescriptor*>(&descriptor); |
| 85 | if(conv2dDesc->m_BiasEnabled) |
| 86 | { |
| 87 | inputInfos.push_back(&infos[3]); // bias |
| 88 | } |
| 89 | break; |
| 90 | } |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 91 | default: |
| 92 | break; |
| 93 | } |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 94 | |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 95 | auto mappings = GetTosaMapping(nullptr, type, inputInfos, outputInfos, descriptor); |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 96 | if (mappings->GetName() == "") |
| 97 | { |
| 98 | // There currently isn't a TOSA mapping for this layer, as the default was returned. |
| 99 | return false; |
| 100 | } |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 101 | |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 102 | TosaSerializationHandler handler; |
| 103 | |
| 104 | // Add mappings to main block as the TOSA Reference Model requires the graph to be in one block called main. |
| 105 | auto* block = new TosaSerializationBasicBlock("main", |
| 106 | mappings->GetOperators(), |
| 107 | mappings->GetTensors(), |
| 108 | mappings->GetInputs(), |
| 109 | mappings->GetOutputs()); |
| 110 | handler.GetBlocks().emplace_back(block); |
| 111 | |
| 112 | GraphStatus status; |
| 113 | TosaReference::IModelRunner runner; |
| 114 | |
| 115 | #if !defined(TOSA_REFERENCE_MODEL_OUTPUT) |
| 116 | // There currently isn't a way to disable the output from the TOSA Reference Model, but it does have a file pointer |
| 117 | // to write debug output to, so set this to /dev/null (if it exists on the system) to hide the output. |
| 118 | func_debug_t funcDebug; |
| 119 | |
| 120 | FILE* file = fopen("/dev/null", "w"); |
| 121 | funcDebug.func_debug_file = (file == nullptr) ? stderr : file; |
| 122 | |
| 123 | runner.setFuncDebug(funcDebug); |
| 124 | #endif |
| 125 | |
| 126 | // Initialise the model runner with the TosaSerializationHandler, which runs validation on the mapping. |
| 127 | status = runner.initialize(handler); |
| 128 | |
| 129 | #if !defined(TOSA_REFERENCE_MODEL_OUTPUT) |
| 130 | // Reset FuncDebug as they can persist across multiple IModelRunner instances. |
| 131 | funcDebug.func_debug_file = stderr; |
| 132 | runner.setFuncDebug(funcDebug); |
| 133 | #endif |
| 134 | |
| 135 | if(status == GraphStatus::TOSA_ERROR || status == GraphStatus::TOSA_UNPREDICTABLE) |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 136 | { |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 137 | return false; |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 138 | } |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 139 | else |
| 140 | { |
| 141 | return true; |
| 142 | } |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // namespace armnn |