blob: 6113b5861a32a8a73a60bc6390cdcce1acc6f140 [file] [log] [blame]
Francis Murtagh9270d9e2022-08-12 13:54:17 +01001//
2// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3// 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 }
Cathal Corbettbd18eab2022-11-15 12:56:16 +000071 case LayerType::Pooling2d:
Cathal Corbettb30e6552022-12-07 11:50:50 +000072 case LayerType::Reshape:
Cathal Corbett3b9acd52022-12-09 12:17:27 +000073 case LayerType::Slice:
Cathal Corbett0bb096d2022-12-22 13:09:38 +000074 case LayerType::Transpose:
Cathal Corbettbd18eab2022-11-15 12:56:16 +000075 inputInfos.push_back(&infos[0]);
76 outputInfos.push_back(&infos[1]);
77 break;
Matthew Sloyanfc9d5e72022-12-08 13:38:23 +000078 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 Sloyan164bf4f2022-10-28 18:02:17 +010091 default:
92 break;
93 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +010094
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000095 auto mappings = GetTosaMapping(nullptr, type, inputInfos, outputInfos, descriptor);
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010096 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 Murtagh9270d9e2022-08-12 13:54:17 +0100101
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000102 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 Murtagh9270d9e2022-08-12 13:54:17 +0100136 {
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000137 return false;
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100138 }
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000139 else
140 {
141 return true;
142 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100143}
144
145} // namespace armnn