blob: ec6fc3b9170712810aebb25d26224d7329296901 [file] [log] [blame]
Francis Murtagh9270d9e2022-08-12 13:54:17 +01001//
David Monahand7fca092023-01-12 14:53:34 +00002// Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
Francis Murtagh9270d9e2022-08-12 13:54:17 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "TosaRefLayerSupport.hpp"
Matthew Sloyan67fd5262022-12-07 19:28:18 +00007
Francis Murtagh9270d9e2022-08-12 13:54:17 +01008#include <tosaCommon/TosaMappings.hpp>
9
10#include <armnn/Types.hpp>
11#include <armnn/utility/IgnoreUnused.hpp>
Matthew Sloyan67fd5262022-12-07 19:28:18 +000012
13#include <graph_status.h>
14#include <model_runner.h>
Francis Murtagh9270d9e2022-08-12 13:54:17 +010015
16#include <vector>
Francis Murtagh9270d9e2022-08-12 13:54:17 +010017
18namespace armnn
19{
20
Francis Murtagh9270d9e2022-08-12 13:54:17 +010021bool 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 Sloyan67fd5262022-12-07 19:28:18 +000030 IgnoreUnused(reasonIfUnsupported);
Francis Murtagh9270d9e2022-08-12 13:54:17 +010031
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010032 std::vector<const TensorInfo*> inputInfos;
33 std::vector<const TensorInfo*> outputInfos;
Francis Murtagh9270d9e2022-08-12 13:54:17 +010034
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010035 switch (type)
36 {
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000037 case LayerType::Input:
38 case LayerType::Output:
39 return true;
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010040 case LayerType::Addition:
Nikhil Raj9a339462022-12-05 11:24:35 +000041 case LayerType::Multiplication:
42 case LayerType::Subtraction:
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010043 // 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 May5b58e312022-12-15 10:15:21 +000048 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 Sloyanc5fe6e72022-11-25 16:10:00 +000055 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 }
David Monahand7fca092023-01-12 14:53:34 +000071 case LayerType::ElementwiseUnary:
Cathal Corbettbd18eab2022-11-15 12:56:16 +000072 case LayerType::Pooling2d:
Cathal Corbettb30e6552022-12-07 11:50:50 +000073 case LayerType::Reshape:
Teresa Charlince655882023-11-21 15:44:13 +000074 case LayerType::Resize:
Cathal Corbett3b9acd52022-12-09 12:17:27 +000075 case LayerType::Slice:
Cathal Corbett0bb096d2022-12-22 13:09:38 +000076 case LayerType::Transpose:
Kevin May1bea6be2023-12-12 11:18:46 +000077 {
Cathal Corbettbd18eab2022-11-15 12:56:16 +000078 inputInfos.push_back(&infos[0]);
79 outputInfos.push_back(&infos[1]);
80 break;
Kevin May1bea6be2023-12-12 11:18:46 +000081 }
82 case LayerType::Splitter:
83 {
84 inputInfos.push_back(&infos[0]);
85 for (unsigned int i = 1; i < infos.size(); ++i)
86 {
87 outputInfos.push_back(&infos[i]);
88 }
89 break;
90 }
Matthew Sloyanfc9d5e72022-12-08 13:38:23 +000091 case LayerType::TransposeConvolution2d:
92 {
93 inputInfos.push_back(&infos[0]); // input
94 outputInfos.push_back(&infos[1]); // output
95 inputInfos.push_back(&infos[2]); // weights
96
97 auto conv2dDesc = PolymorphicDowncast<const TransposeConvolution2dDescriptor*>(&descriptor);
98 if(conv2dDesc->m_BiasEnabled)
99 {
100 inputInfos.push_back(&infos[3]); // bias
101 }
102 break;
103 }
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100104 default:
David Monahand7fca092023-01-12 14:53:34 +0000105 // Default to false for all unsupported layers.
106 return false;
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100107 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100108
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000109 auto mappings = GetTosaMapping(nullptr, type, inputInfos, outputInfos, descriptor);
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100110 if (mappings->GetName() == "")
111 {
112 // There currently isn't a TOSA mapping for this layer, as the default was returned.
113 return false;
114 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100115
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000116 TosaSerializationHandler handler;
117
Narumol Prangnawaratad323af2023-09-29 17:00:38 +0100118 // Add all mappings to main block.
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000119 auto* block = new TosaSerializationBasicBlock("main",
Narumol Prangnawaratad323af2023-09-29 17:00:38 +0100120 "main",
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000121 mappings->GetOperators(),
122 mappings->GetTensors(),
123 mappings->GetInputs(),
124 mappings->GetOutputs());
Narumol Prangnawaratad323af2023-09-29 17:00:38 +0100125
126 std::vector<TosaSerializationBasicBlock*> blocks;
127 blocks.emplace_back(block);
128
129 // Add blocks to the main region.
130 auto* region = new TosaSerializationRegion("main", blocks);
131 handler.GetRegions().emplace_back(region);
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000132
133 GraphStatus status;
134 TosaReference::IModelRunner runner;
135
136#if !defined(TOSA_REFERENCE_MODEL_OUTPUT)
137 // There currently isn't a way to disable the output from the TOSA Reference Model, but it does have a file pointer
138 // to write debug output to, so set this to /dev/null (if it exists on the system) to hide the output.
139 func_debug_t funcDebug;
140
141 FILE* file = fopen("/dev/null", "w");
142 funcDebug.func_debug_file = (file == nullptr) ? stderr : file;
143
144 runner.setFuncDebug(funcDebug);
145#endif
146
147 // Initialise the model runner with the TosaSerializationHandler, which runs validation on the mapping.
148 status = runner.initialize(handler);
149
150#if !defined(TOSA_REFERENCE_MODEL_OUTPUT)
151 // Reset FuncDebug as they can persist across multiple IModelRunner instances.
152 funcDebug.func_debug_file = stderr;
153 runner.setFuncDebug(funcDebug);
154#endif
155
156 if(status == GraphStatus::TOSA_ERROR || status == GraphStatus::TOSA_UNPREDICTABLE)
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100157 {
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000158 return false;
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100159 }
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000160 else
161 {
162 return true;
163 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100164}
165
166} // namespace armnn