blob: 0d0d07a7835ea52a0c751595a6ed84f5b2bc5f07 [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:
41 // Setup inputs and outputs
42 inputInfos.push_back(&infos[0]);
43 inputInfos.push_back(&infos[1]);
44 outputInfos.push_back(&infos[2]);
45 break;
Kevin May5b58e312022-12-15 10:15:21 +000046 case LayerType::Concat:
47 for (unsigned int i = 0; i < infos.size() - 1; ++i)
48 {
49 inputInfos.push_back(&infos[i]);
50 }
51 outputInfos.push_back(&infos.back());
52 break;
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000053 case LayerType::Constant:
54 outputInfos.push_back(&infos[0]);
55 break;
56 case LayerType::Convolution2d:
57 {
58 inputInfos.push_back(&infos[0]); // input
59 outputInfos.push_back(&infos[1]); // output
60 inputInfos.push_back(&infos[2]); // weights
61
62 auto conv2dDesc = PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor);
63 if(conv2dDesc->m_BiasEnabled)
64 {
65 inputInfos.push_back(&infos[3]); // bias
66 }
67 break;
68 }
Cathal Corbettbd18eab2022-11-15 12:56:16 +000069 case LayerType::Pooling2d:
Cathal Corbettb30e6552022-12-07 11:50:50 +000070 case LayerType::Reshape:
Cathal Corbett3b9acd52022-12-09 12:17:27 +000071 case LayerType::Slice:
Cathal Corbettbd18eab2022-11-15 12:56:16 +000072 // Setup inputs and outputs
73 inputInfos.push_back(&infos[0]);
74 outputInfos.push_back(&infos[1]);
75 break;
Matthew Sloyanfc9d5e72022-12-08 13:38:23 +000076 case LayerType::TransposeConvolution2d:
77 {
78 inputInfos.push_back(&infos[0]); // input
79 outputInfos.push_back(&infos[1]); // output
80 inputInfos.push_back(&infos[2]); // weights
81
82 auto conv2dDesc = PolymorphicDowncast<const TransposeConvolution2dDescriptor*>(&descriptor);
83 if(conv2dDesc->m_BiasEnabled)
84 {
85 inputInfos.push_back(&infos[3]); // bias
86 }
87 break;
88 }
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010089 default:
90 break;
91 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +010092
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000093 auto mappings = GetTosaMapping(nullptr, type, inputInfos, outputInfos, descriptor);
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010094 if (mappings->GetName() == "")
95 {
96 // There currently isn't a TOSA mapping for this layer, as the default was returned.
97 return false;
98 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +010099
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000100 TosaSerializationHandler handler;
101
102 // Add mappings to main block as the TOSA Reference Model requires the graph to be in one block called main.
103 auto* block = new TosaSerializationBasicBlock("main",
104 mappings->GetOperators(),
105 mappings->GetTensors(),
106 mappings->GetInputs(),
107 mappings->GetOutputs());
108 handler.GetBlocks().emplace_back(block);
109
110 GraphStatus status;
111 TosaReference::IModelRunner runner;
112
113#if !defined(TOSA_REFERENCE_MODEL_OUTPUT)
114 // There currently isn't a way to disable the output from the TOSA Reference Model, but it does have a file pointer
115 // to write debug output to, so set this to /dev/null (if it exists on the system) to hide the output.
116 func_debug_t funcDebug;
117
118 FILE* file = fopen("/dev/null", "w");
119 funcDebug.func_debug_file = (file == nullptr) ? stderr : file;
120
121 runner.setFuncDebug(funcDebug);
122#endif
123
124 // Initialise the model runner with the TosaSerializationHandler, which runs validation on the mapping.
125 status = runner.initialize(handler);
126
127#if !defined(TOSA_REFERENCE_MODEL_OUTPUT)
128 // Reset FuncDebug as they can persist across multiple IModelRunner instances.
129 funcDebug.func_debug_file = stderr;
130 runner.setFuncDebug(funcDebug);
131#endif
132
133 if(status == GraphStatus::TOSA_ERROR || status == GraphStatus::TOSA_UNPREDICTABLE)
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100134 {
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000135 return false;
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100136 }
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000137 else
138 {
139 return true;
140 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100141}
142
143} // namespace armnn