ArmNN
 24.02
TosaRefLayerSupport.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022-2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
9 
10 #include <armnn/Types.hpp>
12 
13 #include <graph_status.h>
14 #include <model_runner.h>
15 
16 #include <vector>
17 
18 namespace armnn
19 {
20 
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);
30  IgnoreUnused(reasonIfUnsupported);
31 
32  std::vector<const TensorInfo*> inputInfos;
33  std::vector<const TensorInfo*> outputInfos;
34 
35  switch (type)
36  {
38  inputInfos.push_back(&infos[0]);
39  outputInfos.push_back(&infos[1]);
40  break;
41  case LayerType::Input:
42  case LayerType::Output:
43  return true;
48  // Setup inputs and outputs
49  inputInfos.push_back(&infos[0]);
50  inputInfos.push_back(&infos[1]);
51  outputInfos.push_back(&infos[2]);
52  break;
53  case LayerType::Concat:
54  for (unsigned int i = 0; i < infos.size() - 1; ++i)
55  {
56  inputInfos.push_back(&infos[i]);
57  }
58  outputInfos.push_back(&infos.back());
59  break;
61  outputInfos.push_back(&infos[0]);
62  break;
64  {
65  inputInfos.push_back(&infos[0]); // input
66  outputInfos.push_back(&infos[1]); // output
67  inputInfos.push_back(&infos[2]); // weights
68 
69  auto conv2dDesc = PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor);
70  if(conv2dDesc->m_BiasEnabled)
71  {
72  inputInfos.push_back(&infos[3]); // bias
73  }
74  break;
75  }
79  case LayerType::Reshape:
80  case LayerType::Resize:
81  case LayerType::Slice:
83  {
84  inputInfos.push_back(&infos[0]);
85  outputInfos.push_back(&infos[1]);
86  break;
87  }
89  {
90  inputInfos.push_back(&infos[0]);
91  for (unsigned int i = 1; i < infos.size(); ++i)
92  {
93  outputInfos.push_back(&infos[i]);
94  }
95  break;
96  }
98  {
99  inputInfos.push_back(&infos[0]); // input
100  outputInfos.push_back(&infos[1]); // output
101  inputInfos.push_back(&infos[2]); // weights
102 
103  auto conv2dDesc = PolymorphicDowncast<const TransposeConvolution2dDescriptor*>(&descriptor);
104  if(conv2dDesc->m_BiasEnabled)
105  {
106  inputInfos.push_back(&infos[3]); // bias
107  }
108  break;
109  }
110  default:
111  // Default to false for all unsupported layers.
112  return false;
113  }
114 
115  auto mappings = GetTosaMapping(nullptr, type, inputInfos, outputInfos, descriptor);
116  if (mappings->GetName() == "")
117  {
118  // There currently isn't a TOSA mapping for this layer, as the default was returned.
119  return false;
120  }
121 
122  TosaSerializationHandler handler;
123 
124  // Add all mappings to main block.
125  auto* block = new TosaSerializationBasicBlock("main",
126  "main",
127  mappings->GetOperators(),
128  mappings->GetTensors(),
129  mappings->GetInputs(),
130  mappings->GetOutputs());
131 
132  std::vector<TosaSerializationBasicBlock*> blocks;
133  blocks.emplace_back(block);
134 
135  // Add blocks to the main region.
136  auto* region = new TosaSerializationRegion("main", blocks);
137  handler.GetRegions().emplace_back(region);
138 
139  GraphStatus status;
140  TosaReference::IModelRunner runner;
141 
142 #if !defined(TOSA_REFERENCE_MODEL_OUTPUT)
143  // There currently isn't a way to disable the output from the TOSA Reference Model, but it does have a file pointer
144  // to write debug output to, so set this to /dev/null (if it exists on the system) to hide the output.
145  func_debug_t funcDebug;
146 
147  FILE* file = fopen("/dev/null", "w");
148  funcDebug.func_debug_file = (file == nullptr) ? stderr : file;
149 
150  runner.setFuncDebug(funcDebug);
151 #endif
152 
153  // Initialise the model runner with the TosaSerializationHandler, which runs validation on the mapping.
154  status = runner.initialize(handler);
155 
156 #if !defined(TOSA_REFERENCE_MODEL_OUTPUT)
157  // Reset FuncDebug as they can persist across multiple IModelRunner instances.
158  funcDebug.func_debug_file = stderr;
159  runner.setFuncDebug(funcDebug);
160 #endif
161 
162  if(status == GraphStatus::TOSA_ERROR || status == GraphStatus::TOSA_UNPREDICTABLE)
163  {
164  return false;
165  }
166  else
167  {
168  return true;
169  }
170 }
171 
172 } // namespace armnn
armnn::LayerType::Splitter
@ Splitter
armnn::Optional
Definition: Optional.hpp:270
armnn::LayerType::Transpose
@ Transpose
IgnoreUnused.hpp
armnn::LayerType::ElementwiseUnary
@ ElementwiseUnary
armnn::LayerType::ElementwiseBinary
@ ElementwiseBinary
armnn::LayerType::Slice
@ Slice
armnn::LayerType::Subtraction
@ Subtraction
armnn::LayerType::Concat
@ Concat
armnn::LayerType::TransposeConvolution2d
@ TransposeConvolution2d
armnn::LayerType::Quantize
@ Quantize
armnn::LayerType::Multiplication
@ Multiplication
armnn::LayerType::Addition
@ Addition
armnn::BaseDescriptor
Base class for all descriptors.
Definition: Descriptors.hpp:22
GetTosaMapping
TosaSerializationBasicBlock * GetTosaMapping(const Layer *layer, const LayerType type, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const BaseDescriptor &descriptor)
Definition: TosaMappings.cpp:18
armnn::LayerType::Pooling2d
@ Pooling2d
armnn::TosaRefLayerSupport::IsLayerSupported
bool IsLayerSupported(const LayerType &type, const std::vector< TensorInfo > &infos, const BaseDescriptor &descriptor, const Optional< LstmInputParamsInfo > &lstmParamsInfo, const Optional< QuantizedLstmInputParamsInfo > &, Optional< std::string & > reasonIfUnsupported) const override
Default implementation of the ILayerSupport interface, Backends should implement this as a switch sta...
Definition: TosaRefLayerSupport.cpp:21
armnn::LayerType::Reshape
@ Reshape
armnn::IgnoreUnused
void IgnoreUnused(Ts &&...)
Definition: IgnoreUnused.hpp:14
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
Types.hpp
armnn::LayerType::Input
@ Input
TosaRefLayerSupport.hpp
armnn::LayerType::Resize
@ Resize
armnn::LayerType::Convolution2d
@ Convolution2d
armnn::LayerType::Activation
@ Activation
TosaMappings.hpp
armnn::LayerType
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition: Types.hpp:491
armnn::LayerType::Output
@ Output
armnn::LayerType::Constant
@ Constant