blob: e1c349feb4bd097cb0970f4cbf549a79a49ddd4a [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:
Cathal Corbett3b9acd52022-12-09 12:17:27 +000074 case LayerType::Slice:
Cathal Corbett0bb096d2022-12-22 13:09:38 +000075 case LayerType::Transpose:
Cathal Corbettbd18eab2022-11-15 12:56:16 +000076 inputInfos.push_back(&infos[0]);
77 outputInfos.push_back(&infos[1]);
78 break;
Matthew Sloyanfc9d5e72022-12-08 13:38:23 +000079 case LayerType::TransposeConvolution2d:
80 {
81 inputInfos.push_back(&infos[0]); // input
82 outputInfos.push_back(&infos[1]); // output
83 inputInfos.push_back(&infos[2]); // weights
84
85 auto conv2dDesc = PolymorphicDowncast<const TransposeConvolution2dDescriptor*>(&descriptor);
86 if(conv2dDesc->m_BiasEnabled)
87 {
88 inputInfos.push_back(&infos[3]); // bias
89 }
90 break;
91 }
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010092 default:
David Monahand7fca092023-01-12 14:53:34 +000093 // Default to false for all unsupported layers.
94 return false;
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010095 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +010096
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000097 auto mappings = GetTosaMapping(nullptr, type, inputInfos, outputInfos, descriptor);
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010098 if (mappings->GetName() == "")
99 {
100 // There currently isn't a TOSA mapping for this layer, as the default was returned.
101 return false;
102 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100103
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000104 TosaSerializationHandler handler;
105
Narumol Prangnawaratad323af2023-09-29 17:00:38 +0100106 // Add all mappings to main block.
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000107 auto* block = new TosaSerializationBasicBlock("main",
Narumol Prangnawaratad323af2023-09-29 17:00:38 +0100108 "main",
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000109 mappings->GetOperators(),
110 mappings->GetTensors(),
111 mappings->GetInputs(),
112 mappings->GetOutputs());
Narumol Prangnawaratad323af2023-09-29 17:00:38 +0100113
114 std::vector<TosaSerializationBasicBlock*> blocks;
115 blocks.emplace_back(block);
116
117 // Add blocks to the main region.
118 auto* region = new TosaSerializationRegion("main", blocks);
119 handler.GetRegions().emplace_back(region);
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000120
121 GraphStatus status;
122 TosaReference::IModelRunner runner;
123
124#if !defined(TOSA_REFERENCE_MODEL_OUTPUT)
125 // There currently isn't a way to disable the output from the TOSA Reference Model, but it does have a file pointer
126 // to write debug output to, so set this to /dev/null (if it exists on the system) to hide the output.
127 func_debug_t funcDebug;
128
129 FILE* file = fopen("/dev/null", "w");
130 funcDebug.func_debug_file = (file == nullptr) ? stderr : file;
131
132 runner.setFuncDebug(funcDebug);
133#endif
134
135 // Initialise the model runner with the TosaSerializationHandler, which runs validation on the mapping.
136 status = runner.initialize(handler);
137
138#if !defined(TOSA_REFERENCE_MODEL_OUTPUT)
139 // Reset FuncDebug as they can persist across multiple IModelRunner instances.
140 funcDebug.func_debug_file = stderr;
141 runner.setFuncDebug(funcDebug);
142#endif
143
144 if(status == GraphStatus::TOSA_ERROR || status == GraphStatus::TOSA_UNPREDICTABLE)
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100145 {
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000146 return false;
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100147 }
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000148 else
149 {
150 return true;
151 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100152}
153
154} // namespace armnn