blob: 928a19c23216b8005eb406cbf11e2feb5bac4a8e [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;
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000046 case LayerType::Constant:
47 outputInfos.push_back(&infos[0]);
48 break;
49 case LayerType::Convolution2d:
50 {
51 inputInfos.push_back(&infos[0]); // input
52 outputInfos.push_back(&infos[1]); // output
53 inputInfos.push_back(&infos[2]); // weights
54
55 auto conv2dDesc = PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor);
56 if(conv2dDesc->m_BiasEnabled)
57 {
58 inputInfos.push_back(&infos[3]); // bias
59 }
60 break;
61 }
Cathal Corbettbd18eab2022-11-15 12:56:16 +000062 case LayerType::Pooling2d:
Cathal Corbettb30e6552022-12-07 11:50:50 +000063 case LayerType::Reshape:
Cathal Corbett3b9acd52022-12-09 12:17:27 +000064 case LayerType::Slice:
Cathal Corbettbd18eab2022-11-15 12:56:16 +000065 // Setup inputs and outputs
66 inputInfos.push_back(&infos[0]);
67 outputInfos.push_back(&infos[1]);
68 break;
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010069 default:
70 break;
71 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +010072
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000073 auto mappings = GetTosaMapping(nullptr, type, inputInfos, outputInfos, descriptor);
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010074 if (mappings->GetName() == "")
75 {
76 // There currently isn't a TOSA mapping for this layer, as the default was returned.
77 return false;
78 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +010079
Matthew Sloyan67fd5262022-12-07 19:28:18 +000080 TosaSerializationHandler handler;
81
82 // Add mappings to main block as the TOSA Reference Model requires the graph to be in one block called main.
83 auto* block = new TosaSerializationBasicBlock("main",
84 mappings->GetOperators(),
85 mappings->GetTensors(),
86 mappings->GetInputs(),
87 mappings->GetOutputs());
88 handler.GetBlocks().emplace_back(block);
89
90 GraphStatus status;
91 TosaReference::IModelRunner runner;
92
93#if !defined(TOSA_REFERENCE_MODEL_OUTPUT)
94 // There currently isn't a way to disable the output from the TOSA Reference Model, but it does have a file pointer
95 // to write debug output to, so set this to /dev/null (if it exists on the system) to hide the output.
96 func_debug_t funcDebug;
97
98 FILE* file = fopen("/dev/null", "w");
99 funcDebug.func_debug_file = (file == nullptr) ? stderr : file;
100
101 runner.setFuncDebug(funcDebug);
102#endif
103
104 // Initialise the model runner with the TosaSerializationHandler, which runs validation on the mapping.
105 status = runner.initialize(handler);
106
107#if !defined(TOSA_REFERENCE_MODEL_OUTPUT)
108 // Reset FuncDebug as they can persist across multiple IModelRunner instances.
109 funcDebug.func_debug_file = stderr;
110 runner.setFuncDebug(funcDebug);
111#endif
112
113 if(status == GraphStatus::TOSA_ERROR || status == GraphStatus::TOSA_UNPREDICTABLE)
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100114 {
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000115 return false;
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100116 }
Matthew Sloyan67fd5262022-12-07 19:28:18 +0000117 else
118 {
119 return true;
120 }
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100121}
122
123} // namespace armnn