Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1 | // |
Finn Williams | 2605b23 | 2020-06-10 15:53:46 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 6 | #include "Deserializer.hpp" |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 7 | |
Matthew Bentham | ff130e2 | 2020-01-17 11:47:42 +0000 | [diff] [blame] | 8 | #include <armnn/Descriptors.hpp> |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 9 | #include <armnn/Exceptions.hpp> |
Matthew Bentham | ff130e2 | 2020-01-17 11:47:42 +0000 | [diff] [blame] | 10 | #include <armnn/TypesUtils.hpp> |
| 11 | #include <armnn/LstmParams.hpp> |
| 12 | #include <armnn/QuantizedLstmParams.hpp> |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 13 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 14 | #include <armnnUtils/Permute.hpp> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 15 | #include <armnnUtils/Transpose.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 16 | #include <armnn/utility/Assert.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 17 | #include <armnn/utility/IgnoreUnused.hpp> |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 18 | #include <armnn/utility/NumericCast.hpp> |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 19 | |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 20 | #include <ParserHelper.hpp> |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 21 | #include <VerificationHelpers.hpp> |
| 22 | |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 23 | #include <fmt/format.h> |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 24 | |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 25 | #include <fstream> |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | #include <limits> |
| 28 | #include <numeric> |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 29 | |
| 30 | using armnn::ParseException; |
| 31 | using namespace armnn; |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 32 | using namespace armnnSerializer; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 33 | |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 34 | namespace armnnDeserializer |
Aron Virginas-Tar | fc413c0 | 2019-02-13 15:41:52 +0000 | [diff] [blame] | 35 | { |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 36 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 37 | IDeserializer::IDeserializer() : pDeserializerImpl(new DeserializerImpl()){} |
| 38 | |
| 39 | IDeserializer::~IDeserializer() = default; |
| 40 | |
| 41 | IDeserializer *IDeserializer::CreateRaw() |
| 42 | { |
| 43 | return new IDeserializer(); |
| 44 | } |
| 45 | |
| 46 | IDeserializerPtr IDeserializer::Create() |
| 47 | { |
| 48 | return IDeserializerPtr(CreateRaw(), &IDeserializer::Destroy); |
| 49 | } |
| 50 | |
| 51 | void IDeserializer::Destroy(IDeserializer *parser) |
| 52 | { |
| 53 | delete parser; |
| 54 | } |
| 55 | |
| 56 | armnn::INetworkPtr IDeserializer::CreateNetworkFromBinary(const std::vector<uint8_t> &binaryContent) |
| 57 | { |
| 58 | return pDeserializerImpl->CreateNetworkFromBinary(binaryContent); |
| 59 | } |
| 60 | |
| 61 | armnn::INetworkPtr IDeserializer::CreateNetworkFromBinary(std::istream &binaryContent) |
| 62 | { |
| 63 | return pDeserializerImpl->CreateNetworkFromBinary(binaryContent); |
| 64 | } |
| 65 | |
| 66 | BindingPointInfo IDeserializer::GetNetworkInputBindingInfo(unsigned int layerId, const std::string &name) const |
| 67 | { |
| 68 | return pDeserializerImpl->GetNetworkInputBindingInfo(layerId, name); |
| 69 | } |
| 70 | |
| 71 | BindingPointInfo IDeserializer::GetNetworkOutputBindingInfo(unsigned int layerId, const std::string &name) const |
| 72 | { |
| 73 | return pDeserializerImpl->GetNetworkOutputBindingInfo(layerId, name); |
| 74 | } |
| 75 | |
Aron Virginas-Tar | fc413c0 | 2019-02-13 15:41:52 +0000 | [diff] [blame] | 76 | namespace |
| 77 | { |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 78 | |
| 79 | const uint32_t VIRTUAL_LAYER_ID = std::numeric_limits<uint32_t>::max(); |
| 80 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 81 | void CheckGraph(const GraphPtr& graph, |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 82 | unsigned int layersIndex, |
| 83 | const CheckLocation& location) |
| 84 | { |
| 85 | if (graph->layers() == nullptr) |
| 86 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 87 | throw ParseException(fmt::format("{0} was called with invalid (null) graph. " |
| 88 | "Possible reason is that the graph is not yet loaded and Unpack(ed). " |
| 89 | "layers:{1} at {2}", |
| 90 | location.m_Function, |
| 91 | layersIndex, |
| 92 | location.FileLine())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 93 | } |
| 94 | else if (layersIndex >= graph->layers()->size()) |
| 95 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 96 | throw ParseException(fmt::format("{0} was called with an invalid layers index. layers:{1} at {2}", |
| 97 | location.m_Function, |
| 98 | layersIndex, |
| 99 | location.FileLine())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 103 | void CheckLayers(const GraphPtr& graph, |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 104 | unsigned int layersIndex, |
| 105 | unsigned int layerIndex, |
| 106 | const CheckLocation& location) |
| 107 | { |
| 108 | if (graph->layers() == nullptr) |
| 109 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 110 | throw ParseException(fmt::format("{0} was called with invalid (null) graph. " |
| 111 | "Possible reason is that the graph is not yet loaded and Unpack(ed). " |
| 112 | "layers:{1} at {2}", |
| 113 | location.m_Function, |
| 114 | layersIndex, |
| 115 | location.FileLine())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 116 | } |
| 117 | else if (layersIndex >= graph->layers()->size()) |
| 118 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 119 | throw ParseException(fmt::format("{0} was called with an invalid layers index. " |
| 120 | "layers:{1} at {2}", |
| 121 | location.m_Function, |
| 122 | layersIndex, |
| 123 | location.FileLine())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 124 | } |
| 125 | else if (layerIndex >= graph->layers()[layersIndex].size() |
| 126 | && layerIndex != VIRTUAL_LAYER_ID) |
| 127 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 128 | throw ParseException(fmt::format("{0} was called with an invalid layer index. " |
| 129 | "layers:{1} layer:{2} at {3}", |
| 130 | location.m_Function, |
| 131 | layersIndex, |
| 132 | layerIndex, |
| 133 | location.FileLine())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 137 | void CheckTensorPtr(TensorRawPtr rawPtr, |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 138 | const CheckLocation& location) |
| 139 | { |
| 140 | if (rawPtr == nullptr) |
| 141 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 142 | throw ParseException(fmt::format("{0} was called with a null tensor pointer. at {1}", |
| 143 | location.m_Function, |
| 144 | location.FileLine())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 148 | void CheckConstTensorPtr(ConstTensorRawPtr rawPtr, |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 149 | const CheckLocation& location) |
| 150 | { |
| 151 | if (rawPtr == nullptr) |
| 152 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 153 | throw ParseException(fmt::format("{0} was called with a null const tensor pointer. at {1}", |
| 154 | location.m_Function, |
| 155 | location.FileLine())); |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Saoirse Stewart | f11bab5 | 2019-02-25 09:22:58 +0000 | [diff] [blame] | 159 | void CheckConstTensorSize(const unsigned int constTensorSize, |
| 160 | const unsigned int tensorSize, |
| 161 | const CheckLocation& location) |
| 162 | { |
| 163 | if (constTensorSize != tensorSize) |
| 164 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 165 | throw ParseException(fmt::format("{0} wrong number of components supplied to tensor. at:{1}", |
| 166 | location.m_Function, |
| 167 | location.FileLine())); |
Saoirse Stewart | f11bab5 | 2019-02-25 09:22:58 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 171 | #define CHECK_TENSOR_PTR(TENSOR_PTR) \ |
| 172 | CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION()) |
| 173 | |
Saoirse Stewart | f11bab5 | 2019-02-25 09:22:58 +0000 | [diff] [blame] | 174 | #define CHECK_CONST_TENSOR_SIZE(CONST_TENSOR_SIZE, TENSOR_SIZE) \ |
| 175 | CheckConstTensorSize(CONST_TENSOR_SIZE, TENSOR_SIZE, CHECK_LOCATION()) |
| 176 | |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 177 | #define CHECK_CONST_TENSOR_PTR(TENSOR_PTR) \ |
| 178 | CheckConstTensorPtr(TENSOR_PTR, CHECK_LOCATION()) |
| 179 | |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 180 | #define CHECK_LAYERS(GRAPH, LAYERS_INDEX, LAYER_INDEX) \ |
| 181 | CheckLayers(GRAPH, LAYERS_INDEX, LAYER_INDEX, CHECK_LOCATION()) |
| 182 | |
| 183 | #define CHECK_GRAPH(GRAPH, LAYERS_INDEX) \ |
| 184 | CheckGraph(GRAPH, LAYERS_INDEX, CHECK_LOCATION()) |
| 185 | } |
| 186 | |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 187 | bool CheckShape(const armnn::TensorShape& actual, const std::vector<uint32_t>& expected) |
| 188 | { |
| 189 | const unsigned int actualSize = actual.GetNumDimensions(); |
| 190 | if (actualSize != expected.size()) |
| 191 | { |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | for (unsigned int i = 0u; i < actualSize; i++) |
| 196 | { |
| 197 | if (actual[i] != static_cast<unsigned int>(expected[i])) |
| 198 | { |
| 199 | return false; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 206 | IDeserializer::DeserializerImpl::DeserializerImpl() |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 207 | : m_Network(nullptr, nullptr), |
| 208 | //May require LayerType_Max to be included |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 209 | m_ParserFunctions(Layer_MAX+1, &IDeserializer::DeserializerImpl::ParseUnsupportedLayer) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 210 | { |
| 211 | // register supported layers |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 212 | m_ParserFunctions[Layer_AbsLayer] = &DeserializerImpl::ParseAbs; |
| 213 | m_ParserFunctions[Layer_ActivationLayer] = &DeserializerImpl::ParseActivation; |
| 214 | m_ParserFunctions[Layer_AdditionLayer] = &DeserializerImpl::ParseAdd; |
| 215 | m_ParserFunctions[Layer_ArgMinMaxLayer] = &DeserializerImpl::ParseArgMinMax; |
| 216 | m_ParserFunctions[Layer_BatchToSpaceNdLayer] = &DeserializerImpl::ParseBatchToSpaceNd; |
| 217 | m_ParserFunctions[Layer_BatchNormalizationLayer] = &DeserializerImpl::ParseBatchNormalization; |
| 218 | m_ParserFunctions[Layer_ComparisonLayer] = &DeserializerImpl::ParseComparison; |
| 219 | m_ParserFunctions[Layer_ConcatLayer] = &DeserializerImpl::ParseConcat; |
| 220 | m_ParserFunctions[Layer_ConstantLayer] = &DeserializerImpl::ParseConstant; |
| 221 | m_ParserFunctions[Layer_Convolution2dLayer] = &DeserializerImpl::ParseConvolution2d; |
| 222 | m_ParserFunctions[Layer_DepthToSpaceLayer] = &DeserializerImpl::ParseDepthToSpace; |
| 223 | m_ParserFunctions[Layer_DepthwiseConvolution2dLayer] = &DeserializerImpl::ParseDepthwiseConvolution2d; |
| 224 | m_ParserFunctions[Layer_DequantizeLayer] = &DeserializerImpl::ParseDequantize; |
| 225 | m_ParserFunctions[Layer_DetectionPostProcessLayer] = &DeserializerImpl::ParseDetectionPostProcess; |
| 226 | m_ParserFunctions[Layer_DivisionLayer] = &DeserializerImpl::ParseDivision; |
| 227 | m_ParserFunctions[Layer_ElementwiseUnaryLayer] = &DeserializerImpl::ParseElementwiseUnary; |
| 228 | m_ParserFunctions[Layer_EqualLayer] = &DeserializerImpl::ParseEqual; |
| 229 | m_ParserFunctions[Layer_FullyConnectedLayer] = &DeserializerImpl::ParseFullyConnected; |
| 230 | m_ParserFunctions[Layer_FillLayer] = &DeserializerImpl::ParseFill; |
| 231 | m_ParserFunctions[Layer_FloorLayer] = &DeserializerImpl::ParseFloor; |
| 232 | m_ParserFunctions[Layer_GatherLayer] = &DeserializerImpl::ParseGather; |
| 233 | m_ParserFunctions[Layer_GreaterLayer] = &DeserializerImpl::ParseGreater; |
| 234 | m_ParserFunctions[Layer_InstanceNormalizationLayer] = &DeserializerImpl::ParseInstanceNormalization; |
| 235 | m_ParserFunctions[Layer_L2NormalizationLayer] = &DeserializerImpl::ParseL2Normalization; |
| 236 | m_ParserFunctions[Layer_LogicalBinaryLayer] = &DeserializerImpl::ParseLogicalBinary; |
| 237 | m_ParserFunctions[Layer_LogSoftmaxLayer] = &DeserializerImpl::ParseLogSoftmax; |
| 238 | m_ParserFunctions[Layer_LstmLayer] = &DeserializerImpl::ParseLstm; |
| 239 | m_ParserFunctions[Layer_MaximumLayer] = &DeserializerImpl::ParseMaximum; |
| 240 | m_ParserFunctions[Layer_MeanLayer] = &DeserializerImpl::ParseMean; |
| 241 | m_ParserFunctions[Layer_MinimumLayer] = &DeserializerImpl::ParseMinimum; |
| 242 | m_ParserFunctions[Layer_MergeLayer] = &DeserializerImpl::ParseMerge; |
| 243 | m_ParserFunctions[Layer_MergerLayer] = &DeserializerImpl::ParseConcat; |
| 244 | m_ParserFunctions[Layer_MultiplicationLayer] = &DeserializerImpl::ParseMultiplication; |
| 245 | m_ParserFunctions[Layer_NormalizationLayer] = &DeserializerImpl::ParseNormalization; |
| 246 | m_ParserFunctions[Layer_PadLayer] = &DeserializerImpl::ParsePad; |
| 247 | m_ParserFunctions[Layer_PermuteLayer] = &DeserializerImpl::ParsePermute; |
| 248 | m_ParserFunctions[Layer_Pooling2dLayer] = &DeserializerImpl::ParsePooling2d; |
| 249 | m_ParserFunctions[Layer_PreluLayer] = &DeserializerImpl::ParsePrelu; |
| 250 | m_ParserFunctions[Layer_QLstmLayer] = &DeserializerImpl::ParseQLstm; |
| 251 | m_ParserFunctions[Layer_QuantizeLayer] = &DeserializerImpl::ParseQuantize; |
| 252 | m_ParserFunctions[Layer_QuantizedLstmLayer] = &DeserializerImpl::ParseQuantizedLstm; |
| 253 | m_ParserFunctions[Layer_RankLayer] = &DeserializerImpl::ParseRank; |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 254 | m_ParserFunctions[Layer_ReduceLayer] = &DeserializerImpl::ParseReduce; |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 255 | m_ParserFunctions[Layer_ReshapeLayer] = &DeserializerImpl::ParseReshape; |
| 256 | m_ParserFunctions[Layer_ResizeBilinearLayer] = &DeserializerImpl::ParseResizeBilinear; |
| 257 | m_ParserFunctions[Layer_ResizeLayer] = &DeserializerImpl::ParseResize; |
| 258 | m_ParserFunctions[Layer_RsqrtLayer] = &DeserializerImpl::ParseRsqrt; |
| 259 | m_ParserFunctions[Layer_SliceLayer] = &DeserializerImpl::ParseSlice; |
| 260 | m_ParserFunctions[Layer_SoftmaxLayer] = &DeserializerImpl::ParseSoftmax; |
| 261 | m_ParserFunctions[Layer_SpaceToBatchNdLayer] = &DeserializerImpl::ParseSpaceToBatchNd; |
| 262 | m_ParserFunctions[Layer_SpaceToDepthLayer] = &DeserializerImpl::ParseSpaceToDepth; |
| 263 | m_ParserFunctions[Layer_SplitterLayer] = &DeserializerImpl::ParseSplitter; |
| 264 | m_ParserFunctions[Layer_StackLayer] = &DeserializerImpl::ParseStack; |
| 265 | m_ParserFunctions[Layer_StandInLayer] = &DeserializerImpl::ParseStandIn; |
| 266 | m_ParserFunctions[Layer_StridedSliceLayer] = &DeserializerImpl::ParseStridedSlice; |
| 267 | m_ParserFunctions[Layer_SubtractionLayer] = &DeserializerImpl::ParseSubtraction; |
| 268 | m_ParserFunctions[Layer_SwitchLayer] = &DeserializerImpl::ParseSwitch; |
| 269 | m_ParserFunctions[Layer_TransposeConvolution2dLayer] = &DeserializerImpl::ParseTransposeConvolution2d; |
| 270 | m_ParserFunctions[Layer_TransposeLayer] = &DeserializerImpl::ParseTranspose; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 273 | LayerBaseRawPtr IDeserializer::DeserializerImpl::GetBaseLayer(const GraphPtr& graphPtr, unsigned int layerIndex) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 274 | { |
| 275 | auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type(); |
| 276 | |
| 277 | switch(layerType) |
| 278 | { |
FinnWilliamsArm | 4ffcc8f | 2019-09-05 14:34:20 +0100 | [diff] [blame] | 279 | case Layer::Layer_AbsLayer: |
| 280 | return graphPtr->layers()->Get(layerIndex)->layer_as_AbsLayer()->base(); |
Mike Kelly | af48401 | 2019-02-20 16:53:11 +0000 | [diff] [blame] | 281 | case Layer::Layer_ActivationLayer: |
| 282 | return graphPtr->layers()->Get(layerIndex)->layer_as_ActivationLayer()->base(); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 283 | case Layer::Layer_AdditionLayer: |
| 284 | return graphPtr->layers()->Get(layerIndex)->layer_as_AdditionLayer()->base(); |
Narumol Prangnawarat | 0cfcf23 | 2019-09-09 17:16:24 +0100 | [diff] [blame] | 285 | case Layer::Layer_ArgMinMaxLayer: |
| 286 | return graphPtr->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer()->base(); |
Nattapat Chaimanowong | 6b4ed98 | 2019-02-26 17:24:13 +0000 | [diff] [blame] | 287 | case Layer::Layer_BatchToSpaceNdLayer: |
| 288 | return graphPtr->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->base(); |
ruoyan01 | 8e7fa23 | 2019-02-28 15:09:07 +0000 | [diff] [blame] | 289 | case Layer::Layer_BatchNormalizationLayer: |
| 290 | return graphPtr->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer()->base(); |
Aron Virginas-Tar | e80ebd1 | 2019-10-17 16:11:54 +0100 | [diff] [blame] | 291 | case Layer::Layer_ComparisonLayer: |
| 292 | return graphPtr->layers()->Get(layerIndex)->layer_as_ComparisonLayer()->base(); |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 293 | case Layer::Layer_ConcatLayer: |
| 294 | return graphPtr->layers()->Get(layerIndex)->layer_as_ConcatLayer()->base(); |
Conor Kennedy | 7627788 | 2019-02-26 08:29:54 +0000 | [diff] [blame] | 295 | case Layer::Layer_ConstantLayer: |
| 296 | return graphPtr->layers()->Get(layerIndex)->layer_as_ConstantLayer()->base(); |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 297 | case Layer::Layer_Convolution2dLayer: |
| 298 | return graphPtr->layers()->Get(layerIndex)->layer_as_Convolution2dLayer()->base(); |
Aron Virginas-Tar | da9d2d3 | 2019-09-20 10:42:02 +0100 | [diff] [blame] | 299 | case Layer::Layer_DepthToSpaceLayer: |
| 300 | return graphPtr->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->base(); |
Aron Virginas-Tar | c04125f | 2019-02-19 16:31:08 +0000 | [diff] [blame] | 301 | case Layer::Layer_DepthwiseConvolution2dLayer: |
| 302 | return graphPtr->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer()->base(); |
Nattapat Chaimanowong | e4294fd | 2019-03-28 09:56:53 +0000 | [diff] [blame] | 303 | case Layer::Layer_DequantizeLayer: |
| 304 | return graphPtr->layers()->Get(layerIndex)->layer_as_DequantizeLayer()->base(); |
Nattapat Chaimanowong | 3e14a9d | 2019-03-18 12:37:06 +0000 | [diff] [blame] | 305 | case Layer::Layer_DetectionPostProcessLayer: |
| 306 | return graphPtr->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer()->base(); |
Éanna Ó Catháin | 5888589 | 2019-02-27 16:16:39 +0000 | [diff] [blame] | 307 | case Layer::Layer_DivisionLayer: |
| 308 | return graphPtr->layers()->Get(layerIndex)->layer_as_DivisionLayer()->base(); |
Nattapat Chaimanowong | 235cea5 | 2019-02-28 16:27:30 +0000 | [diff] [blame] | 309 | case Layer::Layer_EqualLayer: |
| 310 | return graphPtr->layers()->Get(layerIndex)->layer_as_EqualLayer()->base(); |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 311 | case Layer::Layer_ElementwiseUnaryLayer: |
| 312 | return graphPtr->layers()->Get(layerIndex)->layer_as_ElementwiseUnaryLayer()->base(); |
Sadik Armagan | dbb0c0c | 2019-02-21 09:01:41 +0000 | [diff] [blame] | 313 | case Layer::Layer_FullyConnectedLayer: |
| 314 | return graphPtr->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer()->base(); |
Keith Davis | 300ad56 | 2020-06-04 16:34:23 +0100 | [diff] [blame] | 315 | case Layer::Layer_FillLayer: |
| 316 | return graphPtr->layers()->Get(layerIndex)->layer_as_FillLayer()->base(); |
Finn Williams | dd2ba7e | 2019-03-01 11:51:52 +0000 | [diff] [blame] | 317 | case Layer::Layer_FloorLayer: |
| 318 | return graphPtr->layers()->Get(layerIndex)->layer_as_FloorLayer()->base(); |
Saoirse Stewart | a1ed73a | 2019-03-04 13:40:12 +0000 | [diff] [blame] | 319 | case Layer::Layer_GatherLayer: |
| 320 | return graphPtr->layers()->Get(layerIndex)->layer_as_GatherLayer()->base(); |
Conor Kennedy | 79ffdf5 | 2019-03-01 14:24:54 +0000 | [diff] [blame] | 321 | case Layer::Layer_GreaterLayer: |
| 322 | return graphPtr->layers()->Get(layerIndex)->layer_as_GreaterLayer()->base(); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 323 | case Layer::Layer_InputLayer: |
Aron Virginas-Tar | 0fe3245 | 2019-02-28 13:12:47 +0000 | [diff] [blame] | 324 | return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->base(); |
Aron Virginas-Tar | 781ced9 | 2019-10-03 11:15:39 +0100 | [diff] [blame] | 325 | case Layer::Layer_InstanceNormalizationLayer: |
| 326 | return graphPtr->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer()->base(); |
Narumol Prangnawarat | 495701f | 2019-03-07 17:31:34 +0000 | [diff] [blame] | 327 | case Layer::Layer_L2NormalizationLayer: |
| 328 | return graphPtr->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer()->base(); |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 329 | case Layer::Layer_LogicalBinaryLayer: |
| 330 | return graphPtr->layers()->Get(layerIndex)->layer_as_LogicalBinaryLayer()->base(); |
Sadik Armagan | 2625785 | 2019-10-14 13:00:47 +0100 | [diff] [blame] | 331 | case Layer::Layer_LogSoftmaxLayer: |
| 332 | return graphPtr->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->base(); |
Jim Flynn | 11af375 | 2019-03-19 17:22:29 +0000 | [diff] [blame] | 333 | case Layer::Layer_LstmLayer: |
| 334 | return graphPtr->layers()->Get(layerIndex)->layer_as_LstmLayer()->base(); |
Sadik Armagan | ac97c8c | 2019-03-04 17:44:21 +0000 | [diff] [blame] | 335 | case Layer::Layer_MeanLayer: |
| 336 | return graphPtr->layers()->Get(layerIndex)->layer_as_MeanLayer()->base(); |
Aron Virginas-Tar | 0fe3245 | 2019-02-28 13:12:47 +0000 | [diff] [blame] | 337 | case Layer::Layer_MinimumLayer: |
| 338 | return graphPtr->layers()->Get(layerIndex)->layer_as_MinimumLayer()->base(); |
Aron Virginas-Tar | 377351e | 2019-02-27 14:42:31 +0000 | [diff] [blame] | 339 | case Layer::Layer_MaximumLayer: |
| 340 | return graphPtr->layers()->Get(layerIndex)->layer_as_MaximumLayer()->base(); |
Nattapat Chaimanowong | 1f88630 | 2019-04-05 13:37:19 +0100 | [diff] [blame] | 341 | case Layer::Layer_MergeLayer: |
| 342 | return graphPtr->layers()->Get(layerIndex)->layer_as_MergeLayer()->base(); |
Jim Flynn | ac25a1b | 2019-02-28 10:40:49 +0000 | [diff] [blame] | 343 | case Layer::Layer_MergerLayer: |
| 344 | return graphPtr->layers()->Get(layerIndex)->layer_as_MergerLayer()->base(); |
Sadik Armagan | 5f45027 | 2019-02-12 14:31:45 +0000 | [diff] [blame] | 345 | case Layer::Layer_MultiplicationLayer: |
| 346 | return graphPtr->layers()->Get(layerIndex)->layer_as_MultiplicationLayer()->base(); |
Nina Drozd | 5772878 | 2019-02-27 10:53:27 +0000 | [diff] [blame] | 347 | case Layer::Layer_NormalizationLayer: |
| 348 | return graphPtr->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->base(); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 349 | case Layer::Layer_OutputLayer: |
| 350 | return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->base(); |
Nattapat Chaimanowong | ebb0f9c | 2019-03-01 12:14:06 +0000 | [diff] [blame] | 351 | case Layer::Layer_PadLayer: |
| 352 | return graphPtr->layers()->Get(layerIndex)->layer_as_PadLayer()->base(); |
Nattapat Chaimanowong | 30b0020 | 2019-02-20 17:31:34 +0000 | [diff] [blame] | 353 | case Layer::Layer_PermuteLayer: |
| 354 | return graphPtr->layers()->Get(layerIndex)->layer_as_PermuteLayer()->base(); |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 355 | case Layer::Layer_Pooling2dLayer: |
| 356 | return graphPtr->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->base(); |
Ellen Norris-Thompson | 5198247 | 2019-06-19 11:46:21 +0100 | [diff] [blame] | 357 | case Layer::Layer_PreluLayer: |
| 358 | return graphPtr->layers()->Get(layerIndex)->layer_as_PreluLayer()->base(); |
James Conroy | 8d33318 | 2020-05-13 10:27:58 +0100 | [diff] [blame] | 359 | case Layer::Layer_QLstmLayer: |
| 360 | return graphPtr->layers()->Get(layerIndex)->layer_as_QLstmLayer()->base(); |
Derek Lamberti | 87acb27 | 2019-03-27 16:51:31 +0000 | [diff] [blame] | 361 | case Layer::Layer_QuantizeLayer: |
| 362 | return graphPtr->layers()->Get(layerIndex)->layer_as_QuantizeLayer()->base(); |
Jan Eilers | 5b01a89 | 2019-07-23 09:47:43 +0100 | [diff] [blame] | 363 | case Layer::Layer_QuantizedLstmLayer: |
| 364 | return graphPtr->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer()->base(); |
Finn Williams | 2605b23 | 2020-06-10 15:53:46 +0100 | [diff] [blame] | 365 | case Layer::Layer_RankLayer: |
| 366 | return graphPtr->layers()->Get(layerIndex)->layer_as_RankLayer()->base(); |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 367 | case Layer::Layer_ReduceLayer: |
| 368 | return graphPtr->layers()->Get(layerIndex)->layer_as_ReduceLayer()->base(); |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 369 | case Layer::Layer_ReshapeLayer: |
| 370 | return graphPtr->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->base(); |
Nattapat Chaimanowong | 6522cdc | 2019-03-01 16:14:13 +0000 | [diff] [blame] | 371 | case Layer::Layer_ResizeBilinearLayer: |
| 372 | return graphPtr->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->base(); |
FinnWilliamsArm | 6fb339a | 2019-06-28 15:07:10 +0100 | [diff] [blame] | 373 | case Layer::Layer_ResizeLayer: |
| 374 | return graphPtr->layers()->Get(layerIndex)->layer_as_ResizeLayer()->base(); |
Sadik Armagan | 8b42a38 | 2019-03-01 14:24:49 +0000 | [diff] [blame] | 375 | case Layer::Layer_RsqrtLayer: |
| 376 | return graphPtr->layers()->Get(layerIndex)->layer_as_RsqrtLayer()->base(); |
Aron Virginas-Tar | 2fda80b | 2019-09-18 13:36:52 +0100 | [diff] [blame] | 377 | case Layer::Layer_SliceLayer: |
| 378 | return graphPtr->layers()->Get(layerIndex)->layer_as_SliceLayer()->base(); |
Aron Virginas-Tar | fc413c0 | 2019-02-13 15:41:52 +0000 | [diff] [blame] | 379 | case Layer::Layer_SoftmaxLayer: |
| 380 | return graphPtr->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->base(); |
Nattapat Chaimanowong | 4528699 | 2019-02-26 15:53:02 +0000 | [diff] [blame] | 381 | case Layer::Layer_SpaceToBatchNdLayer: |
| 382 | return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->base(); |
Aron Virginas-Tar | aa06714 | 2019-06-11 16:01:44 +0100 | [diff] [blame] | 383 | case Layer::Layer_SpaceToDepthLayer: |
| 384 | return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->base(); |
Jim Flynn | 18ce338 | 2019-03-08 11:08:30 +0000 | [diff] [blame] | 385 | case Layer::Layer_SplitterLayer: |
| 386 | return graphPtr->layers()->Get(layerIndex)->layer_as_SplitterLayer()->base(); |
Matthew Jackson | b5433ee | 2019-07-11 15:54:20 +0100 | [diff] [blame] | 387 | case Layer::Layer_StackLayer: |
| 388 | return graphPtr->layers()->Get(layerIndex)->layer_as_StackLayer()->base(); |
Aron Virginas-Tar | 85121a2 | 2019-10-23 10:41:35 +0100 | [diff] [blame] | 389 | case Layer::Layer_StandInLayer: |
| 390 | return graphPtr->layers()->Get(layerIndex)->layer_as_StandInLayer()->base(); |
Nattapat Chaimanowong | b348521 | 2019-03-04 12:35:39 +0000 | [diff] [blame] | 391 | case Layer::Layer_StridedSliceLayer: |
| 392 | return graphPtr->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->base(); |
Conor Kennedy | da1f975 | 2019-03-01 14:37:12 +0000 | [diff] [blame] | 393 | case Layer::Layer_SubtractionLayer: |
| 394 | return graphPtr->layers()->Get(layerIndex)->layer_as_SubtractionLayer()->base(); |
Sadik Armagan | eff363d | 2019-04-05 15:25:46 +0100 | [diff] [blame] | 395 | case Layer::Layer_SwitchLayer: |
| 396 | return graphPtr->layers()->Get(layerIndex)->layer_as_SwitchLayer()->base(); |
Aron Virginas-Tar | cb54930 | 2019-06-21 13:53:38 +0100 | [diff] [blame] | 397 | case Layer::Layer_TransposeConvolution2dLayer: |
| 398 | return graphPtr->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer()->base(); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 399 | case Layer::Layer_TransposeLayer: |
| 400 | return graphPtr->layers()->Get(layerIndex)->layer_as_TransposeLayer()->base(); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 401 | case Layer::Layer_NONE: |
| 402 | default: |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 403 | throw ParseException(fmt::format("Layer type {} not recognized", layerType)); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 407 | std::string IDeserializer::DeserializerImpl::GetLayerName(const GraphPtr& graph, unsigned int index) |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 408 | { |
| 409 | auto layer = GetBaseLayer(graph, index); |
| 410 | assert(layer); |
| 411 | return layer->layerName()->str(); |
| 412 | } |
| 413 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 414 | int32_t IDeserializer::DeserializerImpl::GetBindingLayerInfo(const GraphPtr& graphPtr, unsigned int layerIndex) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 415 | { |
| 416 | auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type(); |
| 417 | |
| 418 | if (layerType == Layer::Layer_InputLayer) |
| 419 | { |
| 420 | return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->layerBindingId(); |
| 421 | } |
| 422 | else if ( layerType == Layer::Layer_OutputLayer ) |
| 423 | { |
| 424 | return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->layerBindingId(); |
| 425 | } |
| 426 | return 0; |
| 427 | } |
| 428 | |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 429 | armnn::DataLayout ToDataLayout(armnnSerializer::DataLayout dataLayout) |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 430 | { |
| 431 | switch (dataLayout) |
| 432 | { |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 433 | case armnnSerializer::DataLayout::DataLayout_NHWC: |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 434 | return armnn::DataLayout::NHWC; |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 435 | case armnnSerializer::DataLayout::DataLayout_NCHW: |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 436 | default: |
| 437 | return armnn::DataLayout::NCHW; |
| 438 | } |
| 439 | } |
| 440 | |
Mike Kelly | af48401 | 2019-02-20 16:53:11 +0000 | [diff] [blame] | 441 | armnn::ActivationFunction ToActivationFunction(armnnSerializer::ActivationFunction function) |
| 442 | { |
| 443 | switch (function) |
| 444 | { |
| 445 | case armnnSerializer::ActivationFunction_Sigmoid: |
| 446 | return armnn::ActivationFunction::Sigmoid; |
| 447 | case armnnSerializer::ActivationFunction_TanH: |
| 448 | return armnn::ActivationFunction::TanH; |
| 449 | case armnnSerializer::ActivationFunction_Linear: |
| 450 | return armnn::ActivationFunction::Linear; |
| 451 | case armnnSerializer::ActivationFunction_ReLu: |
| 452 | return armnn::ActivationFunction::ReLu; |
| 453 | case armnnSerializer::ActivationFunction_BoundedReLu: |
| 454 | return armnn::ActivationFunction::BoundedReLu; |
| 455 | case armnnSerializer::ActivationFunction_LeakyReLu: |
| 456 | return armnn::ActivationFunction::LeakyReLu; |
| 457 | case armnnSerializer::ActivationFunction_Abs: |
| 458 | return armnn::ActivationFunction::Abs; |
| 459 | case armnnSerializer::ActivationFunction_Sqrt: |
| 460 | return armnn::ActivationFunction::Sqrt; |
| 461 | case armnnSerializer::ActivationFunction_Square: |
| 462 | return armnn::ActivationFunction::Square; |
David Monahan | 3b3c381 | 2020-02-25 09:03:29 +0000 | [diff] [blame] | 463 | case armnnSerializer::ActivationFunction_Elu: |
| 464 | return armnn::ActivationFunction::Elu; |
Colm Donelan | 03fbeaf | 2020-02-26 15:39:23 +0000 | [diff] [blame] | 465 | case armnnSerializer::ActivationFunction_HardSwish: |
| 466 | return armnn::ActivationFunction::HardSwish; |
Mike Kelly | af48401 | 2019-02-20 16:53:11 +0000 | [diff] [blame] | 467 | default: |
| 468 | return armnn::ActivationFunction::Sigmoid; |
| 469 | } |
| 470 | } |
| 471 | |
Narumol Prangnawarat | 0cfcf23 | 2019-09-09 17:16:24 +0100 | [diff] [blame] | 472 | armnn::ArgMinMaxFunction ToArgMinMaxFunction(armnnSerializer::ArgMinMaxFunction function) |
| 473 | { |
| 474 | switch (function) |
| 475 | { |
| 476 | case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Max: |
| 477 | return armnn::ArgMinMaxFunction::Max; |
| 478 | case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Min: |
| 479 | default: |
| 480 | return armnn::ArgMinMaxFunction::Min; |
| 481 | } |
| 482 | } |
| 483 | |
Aron Virginas-Tar | e80ebd1 | 2019-10-17 16:11:54 +0100 | [diff] [blame] | 484 | armnn::ComparisonOperation ToComparisonOperation(armnnSerializer::ComparisonOperation operation) |
| 485 | { |
| 486 | switch (operation) |
| 487 | { |
| 488 | case armnnSerializer::ComparisonOperation::ComparisonOperation_Equal: |
| 489 | return armnn::ComparisonOperation::Equal; |
| 490 | case armnnSerializer::ComparisonOperation::ComparisonOperation_Greater: |
| 491 | return armnn::ComparisonOperation::Greater; |
| 492 | case armnnSerializer::ComparisonOperation::ComparisonOperation_GreaterOrEqual: |
| 493 | return armnn::ComparisonOperation::GreaterOrEqual; |
| 494 | case armnnSerializer::ComparisonOperation::ComparisonOperation_Less: |
| 495 | return armnn::ComparisonOperation::Less; |
| 496 | case armnnSerializer::ComparisonOperation::ComparisonOperation_LessOrEqual: |
| 497 | return armnn::ComparisonOperation::LessOrEqual; |
| 498 | case armnnSerializer::ComparisonOperation::ComparisonOperation_NotEqual: |
| 499 | default: |
| 500 | return armnn::ComparisonOperation::NotEqual; |
| 501 | } |
| 502 | } |
| 503 | |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 504 | armnn::ReduceOperation ToReduceOperation(armnnSerializer::ReduceOperation operation) |
| 505 | { |
| 506 | switch (operation) |
| 507 | { |
| 508 | case armnnSerializer::ReduceOperation::ReduceOperation_Sum: |
| 509 | return armnn::ReduceOperation::Sum; |
| 510 | case armnnSerializer::ReduceOperation::ReduceOperation_Max: |
| 511 | return armnn::ReduceOperation::Max; |
| 512 | case armnnSerializer::ReduceOperation::ReduceOperation_Mean: |
| 513 | return armnn::ReduceOperation::Mean; |
| 514 | case armnnSerializer::ReduceOperation::ReduceOperation_Min: |
| 515 | return armnn::ReduceOperation::Min; |
| 516 | default: |
| 517 | return armnn::ReduceOperation::Sum; |
| 518 | } |
| 519 | } |
| 520 | |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 521 | armnn::LogicalBinaryOperation ToLogicalBinaryOperation(armnnSerializer::LogicalBinaryOperation operation) |
| 522 | { |
| 523 | switch (operation) |
| 524 | { |
| 525 | case armnnSerializer::LogicalBinaryOperation::LogicalBinaryOperation_LogicalAnd: |
| 526 | return armnn::LogicalBinaryOperation::LogicalAnd; |
| 527 | case armnnSerializer::LogicalBinaryOperation::LogicalBinaryOperation_LogicalOr: |
| 528 | return armnn::LogicalBinaryOperation::LogicalOr; |
| 529 | default: |
| 530 | throw armnn::InvalidArgumentException("Logical Binary operation unknown"); |
| 531 | } |
| 532 | } |
| 533 | |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 534 | armnn::UnaryOperation ToUnaryOperation(armnnSerializer::UnaryOperation operation) |
| 535 | { |
| 536 | switch (operation) |
| 537 | { |
| 538 | case armnnSerializer::UnaryOperation::UnaryOperation_Abs: |
| 539 | return armnn::UnaryOperation::Abs; |
| 540 | case armnnSerializer::UnaryOperation::UnaryOperation_Rsqrt: |
| 541 | return armnn::UnaryOperation::Rsqrt; |
| 542 | case armnnSerializer::UnaryOperation::UnaryOperation_Sqrt: |
| 543 | return armnn::UnaryOperation::Sqrt; |
| 544 | case armnnSerializer::UnaryOperation::UnaryOperation_Exp: |
| 545 | return armnn::UnaryOperation::Exp; |
| 546 | case armnnSerializer::UnaryOperation::UnaryOperation_Neg: |
| 547 | return armnn::UnaryOperation::Neg; |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 548 | case armnnSerializer::UnaryOperation::UnaryOperation_LogicalNot: |
| 549 | return armnn::UnaryOperation::LogicalNot; |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 550 | default: |
| 551 | throw armnn::InvalidArgumentException("Unary operation unknown"); |
| 552 | } |
| 553 | } |
| 554 | |
FinnWilliamsArm | 6fb339a | 2019-06-28 15:07:10 +0100 | [diff] [blame] | 555 | armnn::ResizeMethod ToResizeMethod(armnnSerializer::ResizeMethod method) |
| 556 | { |
| 557 | switch (method) |
| 558 | { |
| 559 | case armnnSerializer::ResizeMethod_NearestNeighbor: |
| 560 | return armnn::ResizeMethod::NearestNeighbor; |
| 561 | case armnnSerializer::ResizeMethod_Bilinear: |
Aron Virginas-Tar | 3c9b270 | 2019-10-31 13:45:16 +0000 | [diff] [blame] | 562 | return armnn::ResizeMethod::Bilinear; |
FinnWilliamsArm | 6fb339a | 2019-06-28 15:07:10 +0100 | [diff] [blame] | 563 | default: |
| 564 | return armnn::ResizeMethod::NearestNeighbor; |
| 565 | } |
| 566 | } |
| 567 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 568 | armnn::TensorInfo ToTensorInfo(TensorRawPtr tensorPtr) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 569 | { |
| 570 | armnn::DataType type; |
| 571 | CHECK_TENSOR_PTR(tensorPtr); |
| 572 | |
| 573 | switch (tensorPtr->dataType()) |
| 574 | { |
Francis Murtagh | ddb1d06 | 2020-03-10 13:51:45 +0000 | [diff] [blame] | 575 | case DataType_QAsymmS8: |
| 576 | type = armnn::DataType::QAsymmS8; |
| 577 | break; |
Sadik Armagan | 1a84fe3 | 2020-03-27 15:56:57 +0000 | [diff] [blame] | 578 | case DataType_QSymmS8: |
| 579 | type = armnn::DataType::QSymmS8; |
| 580 | break; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 581 | case DataType_QuantisedAsymm8: |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 582 | case DataType_QAsymmU8: |
| 583 | type = armnn::DataType::QAsymmU8; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 584 | break; |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 585 | case DataType_QSymmS16: |
Nattapat Chaimanowong | cd5ac23 | 2019-03-19 12:26:36 +0000 | [diff] [blame] | 586 | case DataType_QuantisedSymm16: |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 587 | type = armnn::DataType::QSymmS16; |
Nattapat Chaimanowong | cd5ac23 | 2019-03-19 12:26:36 +0000 | [diff] [blame] | 588 | break; |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 589 | case DataType_Signed32: |
| 590 | type = armnn::DataType::Signed32; |
| 591 | break; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 592 | case DataType_Float32: |
| 593 | type = armnn::DataType::Float32; |
| 594 | break; |
| 595 | case DataType_Float16: |
| 596 | type = armnn::DataType::Float16; |
| 597 | break; |
| 598 | case DataType_Boolean: |
| 599 | type = armnn::DataType::Boolean; |
| 600 | break; |
| 601 | default: |
| 602 | { |
| 603 | CheckLocation location = CHECK_LOCATION(); |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 604 | throw ParseException(fmt::format("Unsupported data type {0} = {1}. {2}", |
| 605 | tensorPtr->dataType(), |
| 606 | EnumNameDataType(tensorPtr->dataType()), |
| 607 | location.AsString())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 608 | } |
| 609 | } |
Sadik Armagan | 1a84fe3 | 2020-03-27 15:56:57 +0000 | [diff] [blame] | 610 | |
Colm Donelan | 800b281 | 2021-02-12 12:43:35 +0000 | [diff] [blame] | 611 | float quantizationScale = tensorPtr->quantizationScale(); |
| 612 | int32_t quantizationOffset = tensorPtr->quantizationOffset(); |
| 613 | |
Finn Williams | 2605b23 | 2020-06-10 15:53:46 +0100 | [diff] [blame] | 614 | if (tensorPtr->dimensionality() == static_cast<unsigned int>(Dimensionality::Scalar)) |
| 615 | { |
Colm Donelan | 800b281 | 2021-02-12 12:43:35 +0000 | [diff] [blame] | 616 | return armnn::TensorInfo(TensorShape{armnn::Dimensionality::Scalar}, |
Finn Williams | 2605b23 | 2020-06-10 15:53:46 +0100 | [diff] [blame] | 617 | type, |
| 618 | quantizationScale, |
| 619 | quantizationOffset); |
| 620 | } |
Colm Donelan | 800b281 | 2021-02-12 12:43:35 +0000 | [diff] [blame] | 621 | else if (tensorPtr->dimensionality() == static_cast<unsigned int>(Dimensionality::NotSpecified)) |
| 622 | { |
| 623 | armnn::TensorInfo result(TensorShape{Dimensionality::NotSpecified}, |
| 624 | type, |
| 625 | quantizationScale, |
| 626 | quantizationOffset); |
| 627 | return result; |
| 628 | } |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 629 | |
| 630 | auto dimensions = tensorPtr->dimensions(); |
| 631 | unsigned int size = dimensions->size(); |
| 632 | std::vector<unsigned int> outputDims(dimensions->begin(), dimensions->begin() + size); |
Colm Donelan | 800b281 | 2021-02-12 12:43:35 +0000 | [diff] [blame] | 633 | bool dimensionsSpecificity[armnn::MaxNumOfTensorDimensions]; |
| 634 | std::fill_n(dimensionsSpecificity, armnn::MaxNumOfTensorDimensions, true); |
| 635 | // For backwards compatibility check if the dimensionSpecificity vector is present first. |
| 636 | // The default is to have dimensionSpecificity set to all true's anyway. |
| 637 | if (tensorPtr->dimensionSpecificity() != nullptr) |
| 638 | { |
| 639 | auto dimensionSpecificity = tensorPtr->dimensionSpecificity(); |
| 640 | size = dimensionSpecificity->size(); |
| 641 | for (unsigned int i = 0; i < size; ++i) |
| 642 | { |
| 643 | dimensionsSpecificity[i] = dimensionSpecificity->Get(i); |
| 644 | } |
| 645 | } |
| 646 | // Construct a TensorShape |
| 647 | TensorShape shape(size, outputDims.data(), dimensionsSpecificity); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 648 | |
Sadik Armagan | 1a84fe3 | 2020-03-27 15:56:57 +0000 | [diff] [blame] | 649 | auto quantizationScales = tensorPtr->quantizationScales(); |
Sadik Armagan | 1a84fe3 | 2020-03-27 15:56:57 +0000 | [diff] [blame] | 650 | if (quantizationScales) |
| 651 | { |
| 652 | unsigned int quantizationScalesSize = quantizationScales->size(); |
| 653 | std::vector<float> scales(quantizationScales->begin(), quantizationScales->begin() + quantizationScalesSize); |
| 654 | unsigned int quantizationDim = tensorPtr->quantizationDim(); |
Colm Donelan | 800b281 | 2021-02-12 12:43:35 +0000 | [diff] [blame] | 655 | armnn::TensorInfo result(shape, |
Sadik Armagan | 1a84fe3 | 2020-03-27 15:56:57 +0000 | [diff] [blame] | 656 | type, |
| 657 | scales, |
| 658 | quantizationDim); |
| 659 | return result; |
| 660 | } |
| 661 | |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 662 | // two statements (on purpose) for easier debugging: |
Colm Donelan | 800b281 | 2021-02-12 12:43:35 +0000 | [diff] [blame] | 663 | armnn::TensorInfo result(shape, |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 664 | type, |
| 665 | quantizationScale, |
| 666 | quantizationOffset); |
Colm Donelan | 800b281 | 2021-02-12 12:43:35 +0000 | [diff] [blame] | 667 | |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 668 | return result; |
| 669 | } |
| 670 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 671 | armnn::ConstTensor ToConstTensor(ConstTensorRawPtr constTensorPtr) |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 672 | { |
| 673 | CHECK_CONST_TENSOR_PTR(constTensorPtr); |
| 674 | armnn::TensorInfo tensorInfo = ToTensorInfo(constTensorPtr->info()); |
| 675 | |
| 676 | switch (constTensorPtr->data_type()) |
| 677 | { |
| 678 | case ConstTensorData_ByteData: |
Saoirse Stewart | f11bab5 | 2019-02-25 09:22:58 +0000 | [diff] [blame] | 679 | { |
| 680 | auto byteData = constTensorPtr->data_as_ByteData()->data(); |
| 681 | CHECK_CONST_TENSOR_SIZE(byteData->size(), tensorInfo.GetNumElements()); |
| 682 | return armnn::ConstTensor(tensorInfo, byteData->data()); |
| 683 | } |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 684 | case ConstTensorData_ShortData: |
Saoirse Stewart | f11bab5 | 2019-02-25 09:22:58 +0000 | [diff] [blame] | 685 | { |
| 686 | auto shortData = constTensorPtr->data_as_ShortData()->data(); |
| 687 | CHECK_CONST_TENSOR_SIZE(shortData->size(), tensorInfo.GetNumElements()); |
| 688 | return armnn::ConstTensor(tensorInfo, shortData->data()); |
| 689 | } |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 690 | case ConstTensorData_IntData: |
Saoirse Stewart | f11bab5 | 2019-02-25 09:22:58 +0000 | [diff] [blame] | 691 | { |
| 692 | auto intData = constTensorPtr->data_as_IntData()->data(); |
| 693 | CHECK_CONST_TENSOR_SIZE(intData->size(), tensorInfo.GetNumElements()); |
| 694 | return armnn::ConstTensor(tensorInfo, intData->data()); |
| 695 | } |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 696 | case ConstTensorData_LongData: |
Saoirse Stewart | f11bab5 | 2019-02-25 09:22:58 +0000 | [diff] [blame] | 697 | { |
| 698 | auto longData = constTensorPtr->data_as_LongData()->data(); |
| 699 | CHECK_CONST_TENSOR_SIZE(longData->size(), tensorInfo.GetNumElements()); |
| 700 | return armnn::ConstTensor(tensorInfo, longData->data()); |
| 701 | } |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 702 | default: |
| 703 | { |
| 704 | CheckLocation location = CHECK_LOCATION(); |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 705 | throw ParseException(fmt::format("Unsupported data type {0} = {1}. {2}", |
| 706 | constTensorPtr->data_type(), |
| 707 | EnumNameConstTensorData(constTensorPtr->data_type()), |
| 708 | location.AsString())); |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 713 | TensorRawPtrVector IDeserializer::DeserializerImpl::GetInputs(const GraphPtr& graphPtr, unsigned int layerIndex) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 714 | { |
| 715 | CHECK_LAYERS(graphPtr, 0, layerIndex); |
| 716 | auto layer = GetBaseLayer(graphPtr, layerIndex); |
| 717 | const auto& numInputs = layer->inputSlots()->size(); |
| 718 | |
| 719 | TensorRawPtrVector result(numInputs); |
| 720 | |
| 721 | for (unsigned int i=0; i<numInputs; ++i) |
| 722 | { |
| 723 | auto inputId = CHECKED_NON_NEGATIVE(static_cast<int32_t> |
| 724 | (layer->inputSlots()->Get(i)->connection()->sourceLayerIndex())); |
| 725 | result[i] = GetBaseLayer(graphPtr, inputId)->outputSlots()->Get(0)->tensorInfo(); |
| 726 | } |
| 727 | return result; |
| 728 | } |
| 729 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 730 | TensorRawPtrVector IDeserializer::DeserializerImpl::GetOutputs(const GraphPtr& graphPtr, unsigned int layerIndex) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 731 | { |
| 732 | CHECK_LAYERS(graphPtr, 0, layerIndex); |
| 733 | auto layer = GetBaseLayer(graphPtr, layerIndex); |
| 734 | const auto& numOutputs = layer->outputSlots()->size(); |
| 735 | |
| 736 | TensorRawPtrVector result(numOutputs); |
| 737 | |
| 738 | for (unsigned int i=0; i<numOutputs; ++i) |
| 739 | { |
| 740 | result[i] = layer->outputSlots()->Get(i)->tensorInfo(); |
| 741 | } |
| 742 | return result; |
| 743 | } |
| 744 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 745 | void IDeserializer::DeserializerImpl::ParseUnsupportedLayer(GraphPtr graph, unsigned int layerIndex) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 746 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 747 | CHECK_LAYERS(graph, 0, layerIndex); |
| 748 | const auto layerName = GetBaseLayer(graph, layerIndex)->layerName()->c_str(); |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 749 | throw ParseException(fmt::format("Layer not supported. layerIndex: {0} " |
| 750 | "layerName: {1} / {2}", |
| 751 | layerIndex, |
| 752 | layerName, |
| 753 | CHECK_LOCATION().AsString())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 756 | void IDeserializer::DeserializerImpl::ResetParser() |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 757 | { |
| 758 | m_Network = armnn::INetworkPtr(nullptr, nullptr); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 759 | m_InputBindings.clear(); |
| 760 | m_OutputBindings.clear(); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 763 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 764 | INetworkPtr IDeserializer::DeserializerImpl::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 765 | { |
| 766 | ResetParser(); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 767 | GraphPtr graph = LoadGraphFromBinary(binaryContent.data(), binaryContent.size()); |
| 768 | return CreateNetworkFromGraph(graph); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 771 | armnn::INetworkPtr IDeserializer::DeserializerImpl::CreateNetworkFromBinary(std::istream& binaryContent) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 772 | { |
Derek Lamberti | 2b183fb | 2019-02-18 16:36:57 +0000 | [diff] [blame] | 773 | ResetParser(); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 774 | std::vector<uint8_t> content((std::istreambuf_iterator<char>(binaryContent)), std::istreambuf_iterator<char>()); |
| 775 | GraphPtr graph = LoadGraphFromBinary(content.data(), content.size()); |
| 776 | return CreateNetworkFromGraph(graph); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 779 | GraphPtr IDeserializer::DeserializerImpl::LoadGraphFromBinary(const uint8_t* binaryContent, size_t len) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 780 | { |
| 781 | if (binaryContent == nullptr) |
| 782 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 783 | throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}", |
| 784 | CHECK_LOCATION().AsString())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 785 | } |
| 786 | flatbuffers::Verifier verifier(binaryContent, len); |
| 787 | if (verifier.VerifyBuffer<SerializedGraph>() == false) |
| 788 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 789 | throw ParseException(fmt::format("Buffer doesn't conform to the expected Armnn " |
| 790 | "flatbuffers format. size:{0} {1}", |
| 791 | len, |
| 792 | CHECK_LOCATION().AsString())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 793 | } |
| 794 | return GetSerializedGraph(binaryContent); |
| 795 | } |
| 796 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 797 | INetworkPtr IDeserializer::DeserializerImpl::CreateNetworkFromGraph(GraphPtr graph) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 798 | { |
| 799 | m_Network = INetwork::Create(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 800 | ARMNN_ASSERT(graph != nullptr); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 801 | unsigned int layerIndex = 0; |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 802 | for (AnyLayer const* layer : *graph->layers()) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 803 | { |
| 804 | if (layer->layer_type() != Layer_InputLayer && |
| 805 | layer->layer_type() != Layer_OutputLayer) |
| 806 | { |
| 807 | // lookup and call the parser function |
| 808 | auto& parserFunction = m_ParserFunctions[layer->layer_type()]; |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 809 | (this->*parserFunction)(graph, layerIndex); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 810 | } |
| 811 | ++layerIndex; |
| 812 | } |
| 813 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 814 | SetupInputLayers(graph); |
| 815 | SetupOutputLayers(graph); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 816 | |
| 817 | // establish the connections from the layer outputs to the inputs of the subsequent layers |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 818 | for (auto&& graphIt : m_GraphConnections) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 819 | { |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 820 | Connections& connections = graphIt.second; |
| 821 | for (auto&& outputIt : connections.outputSlots) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 822 | { |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 823 | const unsigned int outputSlotIndex = outputIt.first; |
| 824 | IOutputSlot* outputSlot = outputIt.second; |
| 825 | if (connections.inputSlots.find(outputSlotIndex) != connections.inputSlots.end()) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 826 | { |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 827 | for (IInputSlot* inputSlot : connections.inputSlots[outputSlotIndex]) |
Nattapat Chaimanowong | d469faf | 2019-03-04 17:10:40 +0000 | [diff] [blame] | 828 | { |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 829 | outputSlot->Connect(*inputSlot); |
Nattapat Chaimanowong | d469faf | 2019-03-04 17:10:40 +0000 | [diff] [blame] | 830 | } |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | return std::move(m_Network); |
| 836 | } |
| 837 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 838 | BindingPointInfo IDeserializer::DeserializerImpl::GetNetworkInputBindingInfo(unsigned int layerIndex, |
Saoirse Stewart | a1ed73a | 2019-03-04 13:40:12 +0000 | [diff] [blame] | 839 | const std::string& name) const |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 840 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 841 | IgnoreUnused(layerIndex); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 842 | for (auto inputBinding : m_InputBindings) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 843 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 844 | if (inputBinding.first == name) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 845 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 846 | return inputBinding.second; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 847 | } |
| 848 | } |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 849 | throw ParseException(fmt::format("No input binding found for layer:{0} / {1}", |
| 850 | name, |
| 851 | CHECK_LOCATION().AsString())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 854 | BindingPointInfo IDeserializer::DeserializerImpl::GetNetworkOutputBindingInfo(unsigned int layerIndex, |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 855 | const std::string& name) const |
| 856 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 857 | IgnoreUnused(layerIndex); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 858 | for (auto outputBinding : m_OutputBindings) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 859 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 860 | if (outputBinding.first == name) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 861 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 862 | return outputBinding.second; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 863 | } |
| 864 | } |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 865 | throw ParseException(fmt::format("No output binding found for layer:{0} / {1}", |
| 866 | name, |
| 867 | CHECK_LOCATION().AsString())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 870 | unsigned int IDeserializer::DeserializerImpl::GetInputLayerInVector(GraphPtr graph, int targetId) |
Tee Jung | aa920c5 | 2019-11-05 10:48:25 +0000 | [diff] [blame] | 871 | { |
| 872 | for (unsigned int i = 0; i < graph->layers()->size(); i++) |
| 873 | { |
| 874 | auto layer = graph->layers()->Get(i); |
| 875 | if (layer->layer_type() == Layer::Layer_InputLayer) |
| 876 | { |
| 877 | auto layerBindingId = layer->layer_as_InputLayer()->base()->layerBindingId(); |
| 878 | if (layerBindingId == targetId) |
| 879 | { |
| 880 | return i; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | throw ParseException("Input layer with given layerBindingId not found"); |
| 885 | } |
| 886 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 887 | unsigned int IDeserializer::DeserializerImpl::GetOutputLayerInVector(GraphPtr graph, int targetId) |
Tee Jung | aa920c5 | 2019-11-05 10:48:25 +0000 | [diff] [blame] | 888 | { |
| 889 | for (unsigned int i = 0; i < graph->layers()->size(); i++) |
| 890 | { |
| 891 | auto layer = graph->layers()->Get(i); |
| 892 | if (layer->layer_type() == Layer::Layer_OutputLayer) |
| 893 | { |
| 894 | auto layerBindingId = layer->layer_as_OutputLayer()->base()->layerBindingId(); |
| 895 | if (layerBindingId == targetId) |
| 896 | { |
| 897 | return i; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | throw ParseException("Output layer with given layerBindingId not found"); |
| 902 | } |
| 903 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 904 | unsigned int IDeserializer::DeserializerImpl::GetLayerIndexInVector(GraphPtr graph, unsigned int targetIndex) |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 905 | { |
| 906 | for (unsigned int i = 0; i < graph->layers()->size(); i++) |
| 907 | { |
| 908 | LayerBaseRawPtr layer = GetBaseLayer(graph, i); |
| 909 | if (layer->index() == targetIndex) |
| 910 | { |
| 911 | return i; |
| 912 | } |
| 913 | } |
| 914 | throw ParseException("Layer with given index not found"); |
| 915 | } |
| 916 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 917 | IDeserializer::DeserializerImpl::FeatureVersions IDeserializer::DeserializerImpl::GetFeatureVersions(GraphPtr graph) |
Tee Jung | aa920c5 | 2019-11-05 10:48:25 +0000 | [diff] [blame] | 918 | { |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 919 | IDeserializer::DeserializerImpl::FeatureVersions versions; |
Tee Jung | aa920c5 | 2019-11-05 10:48:25 +0000 | [diff] [blame] | 920 | |
| 921 | if (graph->featureVersions()) |
| 922 | { |
| 923 | versions.m_BindingIdScheme = graph->featureVersions()->bindingIdsScheme(); |
| 924 | } |
| 925 | |
| 926 | return versions; |
| 927 | } |
| 928 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 929 | void IDeserializer::DeserializerImpl::SetupInputLayers(GraphPtr graph) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 930 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 931 | CHECK_GRAPH(graph, 0); |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 932 | const unsigned int numInputs = graph->inputIds()->size(); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 933 | m_InputBindings.clear(); |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 934 | m_InputBindings.reserve(numInputs); |
| 935 | |
| 936 | for (unsigned int i = 0; i < numInputs; i++) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 937 | { |
Tee Jung | aa920c5 | 2019-11-05 10:48:25 +0000 | [diff] [blame] | 938 | unsigned int inputLayerIndex = 0xFFFFFFFF; |
| 939 | if (GetFeatureVersions(graph).m_BindingIdScheme == 0) |
| 940 | { |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 941 | const unsigned int inputId = armnn::numeric_cast<unsigned int>(graph->inputIds()->Get(i)); |
Tee Jung | aa920c5 | 2019-11-05 10:48:25 +0000 | [diff] [blame] | 942 | inputLayerIndex = GetLayerIndexInVector(graph, inputId); |
| 943 | } |
| 944 | else |
| 945 | { |
| 946 | const int inputId = graph->inputIds()->Get(i); |
| 947 | inputLayerIndex = GetInputLayerInVector(graph, inputId); |
| 948 | } |
| 949 | |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 950 | LayerBaseRawPtr baseLayer = GetBaseLayer(graph, inputLayerIndex); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 951 | |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 952 | // GetBindingLayerInfo expect the index to be index in the vector not index property on each layer base |
| 953 | LayerBindingId bindingId = GetBindingLayerInfo(graph, inputLayerIndex); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 954 | ARMNN_ASSERT_MSG(baseLayer->layerName()->c_str(), "Input has no name."); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 955 | |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 956 | IConnectableLayer* inputLayer = |
| 957 | m_Network->AddInputLayer(bindingId, baseLayer->layerName()->c_str()); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 958 | |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 959 | const armnn::TensorInfo& tensorInfo = ToTensorInfo(baseLayer->outputSlots()->Get(0)->tensorInfo()); |
| 960 | inputLayer->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 961 | RegisterOutputSlots(graph, inputLayerIndex, inputLayer); |
| 962 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 963 | BindingPointInfo bindingInfo = {bindingId, tensorInfo}; |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 964 | m_InputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo)); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 968 | void IDeserializer::DeserializerImpl::SetupOutputLayers(GraphPtr graph) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 969 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 970 | CHECK_GRAPH(graph, 0); |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 971 | const unsigned int numOutputs = graph->outputIds()->size(); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 972 | m_OutputBindings.clear(); |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 973 | m_OutputBindings.reserve(numOutputs); |
| 974 | |
| 975 | for (unsigned int i = 0; i < numOutputs; i++) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 976 | { |
Tee Jung | aa920c5 | 2019-11-05 10:48:25 +0000 | [diff] [blame] | 977 | unsigned int outputLayerIndex = 0xFFFFFFFF; |
| 978 | if (GetFeatureVersions(graph).m_BindingIdScheme == 0) |
| 979 | { |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 980 | const unsigned int outputId = armnn::numeric_cast<unsigned int>(graph->outputIds()->Get(i)); |
Tee Jung | aa920c5 | 2019-11-05 10:48:25 +0000 | [diff] [blame] | 981 | outputLayerIndex = GetLayerIndexInVector(graph, outputId); |
| 982 | } |
| 983 | else |
| 984 | { |
| 985 | const int outputId = graph->outputIds()->Get(i); |
| 986 | outputLayerIndex = GetOutputLayerInVector(graph, outputId); |
| 987 | } |
| 988 | |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 989 | LayerBaseRawPtr baseLayer = GetBaseLayer(graph, outputLayerIndex); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 990 | |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 991 | // GetBindingLayerInfo expect the index to be index in the vector not index property on each layer base |
| 992 | LayerBindingId bindingId = GetBindingLayerInfo(graph, outputLayerIndex); |
Josh Minor | 4c10dfc | 2020-06-17 13:56:20 -0500 | [diff] [blame] | 993 | ARMNN_ASSERT_MSG(baseLayer->layerName()->c_str(), "Output has no name."); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 994 | |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 995 | IConnectableLayer* outputLayer = |
| 996 | m_Network->AddOutputLayer(bindingId, baseLayer->layerName()->c_str()); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 997 | |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 998 | RegisterInputSlots(graph, outputLayerIndex, outputLayer); |
| 999 | |
| 1000 | unsigned int sourceLayerIndex = |
| 1001 | GetLayerIndexInVector(graph, baseLayer->inputSlots()->Get(0)->connection()->sourceLayerIndex()); |
| 1002 | LayerBaseRawPtr sourceBaseLayer = GetBaseLayer(graph, sourceLayerIndex); |
Josh Minor | 4c10dfc | 2020-06-17 13:56:20 -0500 | [diff] [blame] | 1003 | const armnn::TensorInfo& tensorInfo = ToTensorInfo(sourceBaseLayer->outputSlots()->Get(i)->tensorInfo()); |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1004 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1005 | BindingPointInfo bindingInfo = {bindingId, tensorInfo}; |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1006 | m_OutputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo)); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1010 | void IDeserializer::DeserializerImpl::RegisterOutputSlots(GraphPtr graph, |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1011 | uint32_t layerIndex, |
| 1012 | IConnectableLayer* layer) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1013 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1014 | CHECK_LAYERS(graph, 0, layerIndex); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1015 | ARMNN_ASSERT(layer != nullptr); |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1016 | LayerBaseRawPtr baseLayer = GetBaseLayer(graph, layerIndex); |
| 1017 | if (baseLayer->outputSlots()->size() != layer->GetNumOutputSlots()) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1018 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 1019 | throw ParseException(fmt::format("The number of outputslots ({0}) does not match the number expected ({1})" |
| 1020 | " for layer index: {2} {3}", |
| 1021 | baseLayer->outputSlots()->size(), |
| 1022 | layer->GetNumOutputSlots(), |
| 1023 | layerIndex, |
| 1024 | CHECK_LOCATION().AsString())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1027 | for (unsigned int i = 0; i < layer->GetNumOutputSlots(); ++i) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1028 | { |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1029 | const unsigned int slotIndex = baseLayer->outputSlots()->Get(i)->index(); |
| 1030 | armnn::IOutputSlot* outputSlot = &(layer->GetOutputSlot(slotIndex)); |
| 1031 | // layerIndex is not necessarily the same as baseLayer->index(). The latter is needed here |
| 1032 | RegisterOutputSlotOfConnection(baseLayer->index(), slotIndex, outputSlot); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1036 | void IDeserializer::DeserializerImpl::RegisterInputSlots(GraphPtr graph, |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1037 | uint32_t layerIndex, |
| 1038 | armnn::IConnectableLayer* layer) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1039 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1040 | CHECK_LAYERS(graph, 0, layerIndex); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1041 | ARMNN_ASSERT(layer != nullptr); |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1042 | LayerBaseRawPtr baseLayer = GetBaseLayer(graph, layerIndex); |
| 1043 | if (baseLayer->inputSlots()->size() != layer->GetNumInputSlots()) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1044 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 1045 | throw ParseException(fmt::format("The number of inputslots ({0}) does not match the number expected ({1})" |
| 1046 | " for layer index:{2} {3}", |
| 1047 | baseLayer->inputSlots()->size(), |
| 1048 | layer->GetNumInputSlots(), |
| 1049 | layerIndex, |
| 1050 | CHECK_LOCATION().AsString())); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1053 | for (unsigned int i = 0; i < layer->GetNumInputSlots(); ++i) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1054 | { |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1055 | auto fbInputSlot = baseLayer->inputSlots()->Get(i); |
| 1056 | auto fbConnection = fbInputSlot->connection(); |
| 1057 | armnn::IInputSlot* inputSlot = &(layer->GetInputSlot(fbInputSlot->index())); |
| 1058 | RegisterInputSlotOfConnection(fbConnection->sourceLayerIndex(), fbConnection->outputSlotIndex(), inputSlot); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1059 | } |
| 1060 | } |
| 1061 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1062 | void IDeserializer::DeserializerImpl::RegisterInputSlotOfConnection(uint32_t sourceLayerIndex, |
Nattapat Chaimanowong | d469faf | 2019-03-04 17:10:40 +0000 | [diff] [blame] | 1063 | uint32_t outputSlotIndex, |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1064 | armnn::IInputSlot* inputSlot) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1065 | { |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1066 | if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end()) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1067 | { |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1068 | m_GraphConnections[sourceLayerIndex] = Connections(); |
| 1069 | } |
| 1070 | |
| 1071 | Connections& connections = m_GraphConnections[sourceLayerIndex]; |
| 1072 | if (connections.inputSlots.find(outputSlotIndex) == connections.inputSlots.end()) |
| 1073 | { |
| 1074 | connections.inputSlots[outputSlotIndex] = {inputSlot}; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1075 | } |
Nattapat Chaimanowong | d469faf | 2019-03-04 17:10:40 +0000 | [diff] [blame] | 1076 | else |
| 1077 | { |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1078 | connections.inputSlots[outputSlotIndex].push_back(inputSlot); |
Nattapat Chaimanowong | d469faf | 2019-03-04 17:10:40 +0000 | [diff] [blame] | 1079 | } |
| 1080 | } |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1081 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1082 | void IDeserializer::DeserializerImpl::RegisterOutputSlotOfConnection(uint32_t sourceLayerIndex, |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1083 | uint32_t outputSlotIndex, |
| 1084 | armnn::IOutputSlot* outputSlot) |
Nattapat Chaimanowong | d469faf | 2019-03-04 17:10:40 +0000 | [diff] [blame] | 1085 | { |
Nattapat Chaimanowong | af000a9 | 2019-05-16 16:32:35 +0100 | [diff] [blame] | 1086 | if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end()) |
| 1087 | { |
| 1088 | m_GraphConnections[sourceLayerIndex] = Connections(); |
| 1089 | } |
| 1090 | |
| 1091 | Connections& connections = m_GraphConnections[sourceLayerIndex]; |
| 1092 | if (connections.outputSlots.find(outputSlotIndex) != connections.outputSlots.end()) |
| 1093 | { |
| 1094 | throw ParseException("Same output slot index processed twice"); |
| 1095 | } |
| 1096 | |
| 1097 | connections.outputSlots[outputSlotIndex] = outputSlot; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1100 | void IDeserializer::DeserializerImpl::ParseAbs(GraphPtr graph, unsigned int layerIndex) |
FinnWilliamsArm | 4ffcc8f | 2019-09-05 14:34:20 +0100 | [diff] [blame] | 1101 | { |
| 1102 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1103 | auto inputs = GetInputs(graph, layerIndex); |
| 1104 | CHECK_LOCATION(); |
| 1105 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1106 | |
| 1107 | auto outputs = GetOutputs(graph, layerIndex); |
| 1108 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1109 | |
| 1110 | auto layerName = GetLayerName(graph, layerIndex); |
| 1111 | |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 1112 | armnn::ElementwiseUnaryDescriptor descriptor(armnn::UnaryOperation::Abs); |
| 1113 | IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str()); |
FinnWilliamsArm | 4ffcc8f | 2019-09-05 14:34:20 +0100 | [diff] [blame] | 1114 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1115 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1116 | |
| 1117 | RegisterInputSlots(graph, layerIndex, layer); |
| 1118 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1119 | } |
| 1120 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1121 | void IDeserializer::DeserializerImpl::ParseActivation(GraphPtr graph, unsigned int layerIndex) |
Mike Kelly | af48401 | 2019-02-20 16:53:11 +0000 | [diff] [blame] | 1122 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1123 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1124 | auto inputs = GetInputs(graph, layerIndex); |
Mike Kelly | af48401 | 2019-02-20 16:53:11 +0000 | [diff] [blame] | 1125 | CHECK_LOCATION(); |
| 1126 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1127 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1128 | auto outputs = GetOutputs(graph, layerIndex); |
Mike Kelly | af48401 | 2019-02-20 16:53:11 +0000 | [diff] [blame] | 1129 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1130 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1131 | auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ActivationLayer(); |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 1132 | auto layerName = GetLayerName(graph, layerIndex); |
Mike Kelly | af48401 | 2019-02-20 16:53:11 +0000 | [diff] [blame] | 1133 | auto serializerDescriptor = serializerLayer->descriptor(); |
| 1134 | |
| 1135 | armnn::ActivationDescriptor descriptor; |
Tee Jung | 86bc3d8 | 2019-10-01 11:25:56 +0900 | [diff] [blame] | 1136 | descriptor.m_Function = ToActivationFunction(serializerDescriptor->activationFunction()); |
Mike Kelly | af48401 | 2019-02-20 16:53:11 +0000 | [diff] [blame] | 1137 | descriptor.m_A = serializerDescriptor->a(); |
| 1138 | descriptor.m_B = serializerDescriptor->b(); |
| 1139 | |
| 1140 | IConnectableLayer* layer = m_Network->AddActivationLayer(descriptor, |
| 1141 | layerName.c_str()); |
| 1142 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1143 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1144 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1145 | RegisterInputSlots(graph, layerIndex, layer); |
| 1146 | RegisterOutputSlots(graph, layerIndex, layer); |
Mike Kelly | af48401 | 2019-02-20 16:53:11 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1149 | void IDeserializer::DeserializerImpl::ParseAdd(GraphPtr graph, unsigned int layerIndex) |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1150 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1151 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1152 | auto inputs = GetInputs(graph, layerIndex); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1153 | CHECK_LOCATION(); |
| 1154 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1155 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1156 | auto outputs = GetOutputs(graph, layerIndex); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1157 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1158 | |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 1159 | auto layerName = GetLayerName(graph, layerIndex); |
| 1160 | IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str()); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1161 | |
| 1162 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1163 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1164 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1165 | RegisterInputSlots(graph, layerIndex, layer); |
| 1166 | RegisterOutputSlots(graph, layerIndex, layer); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1169 | void IDeserializer::DeserializerImpl::ParseArgMinMax(GraphPtr graph, unsigned int layerIndex) |
Narumol Prangnawarat | 0cfcf23 | 2019-09-09 17:16:24 +0100 | [diff] [blame] | 1170 | { |
| 1171 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1172 | auto inputs = GetInputs(graph, layerIndex); |
| 1173 | CHECK_LOCATION(); |
| 1174 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1175 | |
| 1176 | auto outputs = GetOutputs(graph, layerIndex); |
| 1177 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1178 | |
| 1179 | auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer(); |
| 1180 | auto serializerDescriptor = serializerLayer->descriptor(); |
| 1181 | |
| 1182 | armnn::ArgMinMaxDescriptor descriptor; |
Tee Jung | 86bc3d8 | 2019-10-01 11:25:56 +0900 | [diff] [blame] | 1183 | descriptor.m_Function = ToArgMinMaxFunction(serializerDescriptor->argMinMaxFunction()); |
Narumol Prangnawarat | 0cfcf23 | 2019-09-09 17:16:24 +0100 | [diff] [blame] | 1184 | descriptor.m_Axis = serializerDescriptor->axis(); |
| 1185 | auto layerName = GetLayerName(graph, layerIndex); |
| 1186 | IConnectableLayer* layer = m_Network->AddArgMinMaxLayer(descriptor, layerName.c_str()); |
| 1187 | |
| 1188 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1189 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1190 | |
| 1191 | RegisterInputSlots(graph, layerIndex, layer); |
| 1192 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1193 | } |
| 1194 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1195 | void IDeserializer::DeserializerImpl::ParseBatchToSpaceNd(GraphPtr graph, unsigned int layerIndex) |
Nattapat Chaimanowong | 6b4ed98 | 2019-02-26 17:24:13 +0000 | [diff] [blame] | 1196 | { |
| 1197 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1198 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1199 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Nattapat Chaimanowong | 6b4ed98 | 2019-02-26 17:24:13 +0000 | [diff] [blame] | 1200 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1201 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1202 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Nattapat Chaimanowong | 6b4ed98 | 2019-02-26 17:24:13 +0000 | [diff] [blame] | 1203 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1204 | |
| 1205 | auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->descriptor(); |
| 1206 | auto flatBufferCrops = flatBufferDescriptor->crops(); |
| 1207 | auto flatBufferBlockShape = flatBufferDescriptor->blockShape(); |
| 1208 | |
| 1209 | if (flatBufferCrops->Length() % 2 != 0) |
| 1210 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 1211 | throw ParseException(fmt::format("The size of crops must be divisible by 2 {}", CHECK_LOCATION().AsString())); |
Nattapat Chaimanowong | 6b4ed98 | 2019-02-26 17:24:13 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | std::vector<std::pair<unsigned int, unsigned int>> crops; |
| 1215 | crops.reserve(flatBufferCrops->Length() / 2); |
| 1216 | for (unsigned int i = 0; i < flatBufferCrops->Length() - 1; i += 2) |
| 1217 | { |
| 1218 | crops.emplace_back(flatBufferCrops->Get(i), flatBufferCrops->Get(i+1)); |
| 1219 | } |
| 1220 | |
| 1221 | armnn::BatchToSpaceNdDescriptor descriptor; |
| 1222 | descriptor.m_DataLayout = ToDataLayout(flatBufferDescriptor->dataLayout()); |
| 1223 | descriptor.m_BlockShape = |
| 1224 | std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end()); |
| 1225 | descriptor.m_Crops = crops; |
| 1226 | |
| 1227 | auto layerName = GetLayerName(graph, layerIndex); |
| 1228 | IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(descriptor, layerName.c_str()); |
| 1229 | |
| 1230 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1231 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1232 | |
| 1233 | RegisterInputSlots(graph, layerIndex, layer); |
| 1234 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1235 | } |
| 1236 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1237 | void IDeserializer::DeserializerImpl::ParseBatchNormalization(GraphPtr graph, unsigned int layerIndex) |
ruoyan01 | 8e7fa23 | 2019-02-28 15:09:07 +0000 | [diff] [blame] | 1238 | { |
| 1239 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1240 | |
| 1241 | auto inputs = GetInputs(graph, layerIndex); |
| 1242 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1243 | |
| 1244 | auto outputs = GetOutputs(graph, layerIndex); |
| 1245 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1246 | auto outputInfo = ToTensorInfo(outputs[0]); |
| 1247 | |
ruoyan01 | 5c7ab05 | 2019-03-04 14:48:02 +0000 | [diff] [blame] | 1248 | auto layerName = GetLayerName(graph, layerIndex); |
ruoyan01 | 8e7fa23 | 2019-02-28 15:09:07 +0000 | [diff] [blame] | 1249 | |
| 1250 | auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer(); |
| 1251 | auto serializerDescriptor = serializerLayer->descriptor(); |
| 1252 | |
| 1253 | armnn::BatchNormalizationDescriptor descriptor; |
| 1254 | descriptor.m_Eps = serializerDescriptor->eps(); |
| 1255 | descriptor.m_DataLayout = ToDataLayout(serializerDescriptor->dataLayout()); |
| 1256 | |
| 1257 | armnn::ConstTensor mean = ToConstTensor(serializerLayer->mean()); |
| 1258 | armnn::ConstTensor variance = ToConstTensor(serializerLayer->variance()); |
| 1259 | armnn::ConstTensor beta = ToConstTensor(serializerLayer->beta()); |
| 1260 | armnn::ConstTensor gamma = ToConstTensor(serializerLayer->gamma()); |
| 1261 | |
| 1262 | IConnectableLayer* layer = m_Network->AddBatchNormalizationLayer(descriptor, |
| 1263 | mean, |
| 1264 | variance, |
| 1265 | beta, |
| 1266 | gamma, |
| 1267 | layerName.c_str()); |
| 1268 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 1269 | |
| 1270 | RegisterInputSlots(graph, layerIndex, layer); |
| 1271 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1272 | } |
| 1273 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1274 | void IDeserializer::DeserializerImpl::ParseConstant(GraphPtr graph, unsigned int layerIndex) |
Conor Kennedy | 7627788 | 2019-02-26 08:29:54 +0000 | [diff] [blame] | 1275 | { |
| 1276 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1277 | CHECK_LOCATION(); |
| 1278 | |
| 1279 | auto outputs = GetOutputs(graph, layerIndex); |
| 1280 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1281 | |
| 1282 | auto layerName = GetLayerName(graph, layerIndex); |
| 1283 | |
| 1284 | auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ConstantLayer(); |
| 1285 | auto serializerInput = serializerLayer->input(); |
| 1286 | |
| 1287 | armnn::ConstTensor input = ToConstTensor(serializerInput); |
| 1288 | |
| 1289 | IConnectableLayer* layer = m_Network->AddConstantLayer(input, layerName.c_str()); |
| 1290 | |
| 1291 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1292 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1293 | |
| 1294 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1295 | } |
| 1296 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1297 | void IDeserializer::DeserializerImpl::ParseConvolution2d(GraphPtr graph, unsigned int layerIndex) |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 1298 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1299 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1300 | auto inputs = GetInputs(graph, layerIndex); |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 1301 | CHECK_LOCATION(); |
| 1302 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1303 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1304 | auto outputs = GetOutputs(graph, layerIndex); |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 1305 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1306 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1307 | auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_Convolution2dLayer(); |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 1308 | auto layerName = GetLayerName(graph, layerIndex); |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 1309 | auto serializerDescriptor = serializerLayer->descriptor(); |
| 1310 | |
| 1311 | armnn::Convolution2dDescriptor descriptor; |
| 1312 | descriptor.m_PadLeft = serializerDescriptor->padLeft(); |
| 1313 | descriptor.m_PadRight = serializerDescriptor->padRight(); |
| 1314 | descriptor.m_PadTop = serializerDescriptor->padTop(); |
| 1315 | descriptor.m_PadBottom = serializerDescriptor->padBottom(); |
| 1316 | descriptor.m_StrideX = serializerDescriptor->strideX(); |
| 1317 | descriptor.m_StrideY = serializerDescriptor->strideY();; |
Matthew Bentham | acad04e | 2019-05-13 10:02:45 +0100 | [diff] [blame] | 1318 | descriptor.m_DilationX = serializerDescriptor->dilationX(); |
| 1319 | descriptor.m_DilationY = serializerDescriptor->dilationY();; |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 1320 | descriptor.m_BiasEnabled = serializerDescriptor->biasEnabled();; |
| 1321 | descriptor.m_DataLayout = ToDataLayout(serializerDescriptor->dataLayout()); |
| 1322 | |
| 1323 | armnn::ConstTensor weights = ToConstTensor(serializerLayer->weights()); |
| 1324 | armnn::ConstTensor biases; |
| 1325 | |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1326 | armnn::Optional<armnn::ConstTensor> optionalBiases = armnn::EmptyOptional(); |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 1327 | if (descriptor.m_BiasEnabled) |
| 1328 | { |
| 1329 | biases = ToConstTensor(serializerLayer->biases()); |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1330 | optionalBiases = armnn::Optional<armnn::ConstTensor>(biases); |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 1331 | } |
| 1332 | IConnectableLayer* layer = m_Network->AddConvolution2dLayer(descriptor, |
| 1333 | weights, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1334 | optionalBiases, |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 1335 | layerName.c_str()); |
| 1336 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1337 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1338 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1339 | RegisterInputSlots(graph, layerIndex, layer); |
| 1340 | RegisterOutputSlots(graph, layerIndex, layer); |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1343 | void IDeserializer::DeserializerImpl::ParseDepthToSpace(GraphPtr graph, unsigned int layerIndex) |
Aron Virginas-Tar | da9d2d3 | 2019-09-20 10:42:02 +0100 | [diff] [blame] | 1344 | { |
| 1345 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1346 | |
| 1347 | auto inputs = GetInputs(graph, layerIndex); |
| 1348 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1349 | |
| 1350 | auto outputs = GetOutputs(graph, layerIndex); |
| 1351 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1352 | |
| 1353 | auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->descriptor(); |
| 1354 | |
| 1355 | armnn::DepthToSpaceDescriptor descriptor; |
| 1356 | descriptor.m_BlockSize = fbDescriptor->blockSize(); |
| 1357 | descriptor.m_DataLayout = ToDataLayout(fbDescriptor->dataLayout()); |
| 1358 | |
| 1359 | auto layerName = GetLayerName(graph, layerIndex); |
| 1360 | IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str()); |
| 1361 | |
| 1362 | armnn::TensorInfo outputInfo = ToTensorInfo(outputs[0]); |
| 1363 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 1364 | |
| 1365 | RegisterInputSlots(graph, layerIndex, layer); |
| 1366 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1367 | } |
| 1368 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1369 | void IDeserializer::DeserializerImpl::ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex) |
Aron Virginas-Tar | c04125f | 2019-02-19 16:31:08 +0000 | [diff] [blame] | 1370 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1371 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1372 | auto inputs = GetInputs(graph, layerIndex); |
Aron Virginas-Tar | c04125f | 2019-02-19 16:31:08 +0000 | [diff] [blame] | 1373 | CHECK_LOCATION(); |
| 1374 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1375 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1376 | auto outputs = GetOutputs(graph, layerIndex); |
Aron Virginas-Tar | c04125f | 2019-02-19 16:31:08 +0000 | [diff] [blame] | 1377 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1378 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1379 | auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer(); |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 1380 | auto layerName = GetLayerName(graph, layerIndex); |
Aron Virginas-Tar | c04125f | 2019-02-19 16:31:08 +0000 | [diff] [blame] | 1381 | auto serializerDescriptor = serializerLayer->descriptor(); |
| 1382 | |
| 1383 | armnn::DepthwiseConvolution2dDescriptor descriptor; |
| 1384 | descriptor.m_PadLeft = serializerDescriptor->padLeft(); |
| 1385 | descriptor.m_PadRight = serializerDescriptor->padRight(); |
| 1386 | descriptor.m_PadTop = serializerDescriptor->padTop(); |
| 1387 | descriptor.m_PadBottom = serializerDescriptor->padBottom(); |
| 1388 | descriptor.m_StrideX = serializerDescriptor->strideX(); |
Aron Virginas-Tar | 5e1b0cf | 2019-06-21 14:20:11 +0100 | [diff] [blame] | 1389 | descriptor.m_StrideY = serializerDescriptor->strideY(); |
| 1390 | descriptor.m_DilationX = serializerDescriptor->dilationX(); |
| 1391 | descriptor.m_DilationY = serializerDescriptor->dilationY(); |
Aron Virginas-Tar | c04125f | 2019-02-19 16:31:08 +0000 | [diff] [blame] | 1392 | descriptor.m_BiasEnabled = serializerDescriptor->biasEnabled();; |
| 1393 | descriptor.m_DataLayout = ToDataLayout(serializerDescriptor->dataLayout()); |
| 1394 | |
| 1395 | armnn::ConstTensor weights = ToConstTensor(serializerLayer->weights()); |
| 1396 | armnn::ConstTensor biases; |
| 1397 | |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1398 | armnn::Optional<armnn::ConstTensor> optionalBiases = armnn::EmptyOptional(); |
Aron Virginas-Tar | c04125f | 2019-02-19 16:31:08 +0000 | [diff] [blame] | 1399 | if (descriptor.m_BiasEnabled) |
| 1400 | { |
| 1401 | biases = ToConstTensor(serializerLayer->biases()); |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1402 | optionalBiases = armnn::Optional<armnn::ConstTensor>(biases); |
Aron Virginas-Tar | c04125f | 2019-02-19 16:31:08 +0000 | [diff] [blame] | 1403 | } |
| 1404 | IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor, |
| 1405 | weights, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1406 | optionalBiases, |
Aron Virginas-Tar | c04125f | 2019-02-19 16:31:08 +0000 | [diff] [blame] | 1407 | layerName.c_str()); |
| 1408 | |
| 1409 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1410 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1411 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1412 | RegisterInputSlots(graph, layerIndex, layer); |
| 1413 | RegisterOutputSlots(graph, layerIndex, layer); |
Aron Virginas-Tar | c04125f | 2019-02-19 16:31:08 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1416 | void IDeserializer::DeserializerImpl::ParseDetectionPostProcess(GraphPtr graph, unsigned int layerIndex) |
Nattapat Chaimanowong | 3e14a9d | 2019-03-18 12:37:06 +0000 | [diff] [blame] | 1417 | { |
| 1418 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1419 | auto inputs = GetInputs(graph, layerIndex); |
| 1420 | CHECK_LOCATION(); |
| 1421 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1422 | |
| 1423 | auto outputs = GetOutputs(graph, layerIndex); |
| 1424 | CHECK_VALID_SIZE(outputs.size(), 4); |
| 1425 | |
| 1426 | auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer(); |
| 1427 | auto layerName = GetLayerName(graph, layerIndex); |
| 1428 | auto flatBufferDescriptor = flatBufferLayer->descriptor(); |
| 1429 | |
| 1430 | armnn::DetectionPostProcessDescriptor descriptor; |
| 1431 | descriptor.m_MaxDetections = flatBufferDescriptor->maxDetections(); |
| 1432 | descriptor.m_MaxClassesPerDetection = flatBufferDescriptor->maxClassesPerDetection(); |
| 1433 | descriptor.m_DetectionsPerClass = flatBufferDescriptor->detectionsPerClass(); |
| 1434 | descriptor.m_NmsScoreThreshold = flatBufferDescriptor->nmsScoreThreshold(); |
| 1435 | descriptor.m_NmsIouThreshold = flatBufferDescriptor->nmsIouThreshold(); |
| 1436 | descriptor.m_NumClasses = flatBufferDescriptor->numClasses(); |
| 1437 | descriptor.m_UseRegularNms = flatBufferDescriptor->useRegularNms(); |
| 1438 | descriptor.m_ScaleX = flatBufferDescriptor->scaleX(); |
| 1439 | descriptor.m_ScaleY = flatBufferDescriptor->scaleY(); |
| 1440 | descriptor.m_ScaleW = flatBufferDescriptor->scaleW(); |
| 1441 | descriptor.m_ScaleH = flatBufferDescriptor->scaleH(); |
| 1442 | |
| 1443 | armnn::ConstTensor anchors = ToConstTensor(flatBufferLayer->anchors()); |
| 1444 | |
| 1445 | IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(descriptor, |
| 1446 | anchors, |
| 1447 | layerName.c_str()); |
| 1448 | |
| 1449 | for (unsigned int i = 0; i < 4; i++) |
| 1450 | { |
| 1451 | layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i])); |
| 1452 | } |
| 1453 | |
| 1454 | RegisterInputSlots(graph, layerIndex, layer); |
| 1455 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1456 | } |
| 1457 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1458 | void IDeserializer::DeserializerImpl::ParseDivision(GraphPtr graph, unsigned int layerIndex) |
Éanna Ó Catháin | 5888589 | 2019-02-27 16:16:39 +0000 | [diff] [blame] | 1459 | { |
| 1460 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1461 | auto inputs = GetInputs(graph, layerIndex); |
| 1462 | CHECK_LOCATION(); |
| 1463 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1464 | |
| 1465 | auto outputs = GetOutputs(graph, layerIndex); |
| 1466 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1467 | |
| 1468 | auto layerName = GetLayerName(graph, layerIndex); |
| 1469 | IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str()); |
| 1470 | |
| 1471 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1472 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1473 | |
| 1474 | RegisterInputSlots(graph, layerIndex, layer); |
| 1475 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1476 | } |
| 1477 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1478 | void IDeserializer::DeserializerImpl::ParseEqual(GraphPtr graph, unsigned int layerIndex) |
Nattapat Chaimanowong | 235cea5 | 2019-02-28 16:27:30 +0000 | [diff] [blame] | 1479 | { |
| 1480 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1481 | auto inputs = GetInputs(graph, layerIndex); |
| 1482 | CHECK_LOCATION(); |
| 1483 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1484 | |
| 1485 | auto outputs = GetOutputs(graph, layerIndex); |
| 1486 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1487 | |
| 1488 | auto layerName = GetLayerName(graph, layerIndex); |
Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 1489 | armnn::ComparisonDescriptor descriptor(armnn::ComparisonOperation::Equal); |
| 1490 | IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str()); |
Nattapat Chaimanowong | 235cea5 | 2019-02-28 16:27:30 +0000 | [diff] [blame] | 1491 | |
| 1492 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1493 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1494 | |
| 1495 | RegisterInputSlots(graph, layerIndex, layer); |
| 1496 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1497 | } |
| 1498 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1499 | void IDeserializer::DeserializerImpl::ParseFill(GraphPtr graph, unsigned int layerIndex) |
Keith Davis | 300ad56 | 2020-06-04 16:34:23 +0100 | [diff] [blame] | 1500 | { |
| 1501 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1502 | auto inputs = GetInputs(graph, layerIndex); |
| 1503 | CHECK_LOCATION(); |
| 1504 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1505 | |
| 1506 | auto outputs = GetOutputs(graph, layerIndex); |
| 1507 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1508 | |
| 1509 | auto layerName = GetLayerName(graph, layerIndex); |
| 1510 | armnn::FillDescriptor descriptor(1.0f); |
| 1511 | IConnectableLayer* layer = m_Network->AddFillLayer(descriptor, layerName.c_str()); |
| 1512 | |
| 1513 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1514 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1515 | |
| 1516 | RegisterInputSlots(graph, layerIndex, layer); |
| 1517 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1518 | } |
| 1519 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1520 | void IDeserializer::DeserializerImpl::ParseGreater(GraphPtr graph, unsigned int layerIndex) |
Conor Kennedy | 79ffdf5 | 2019-03-01 14:24:54 +0000 | [diff] [blame] | 1521 | { |
| 1522 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1523 | auto inputs = GetInputs(graph, layerIndex); |
| 1524 | CHECK_LOCATION(); |
| 1525 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1526 | |
| 1527 | auto outputs = GetOutputs(graph, layerIndex); |
| 1528 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1529 | |
| 1530 | auto layerName = GetLayerName(graph, layerIndex); |
Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 1531 | armnn::ComparisonDescriptor descriptor(armnn::ComparisonOperation::Greater); |
| 1532 | IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str()); |
Conor Kennedy | 79ffdf5 | 2019-03-01 14:24:54 +0000 | [diff] [blame] | 1533 | |
| 1534 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1535 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1536 | |
| 1537 | RegisterInputSlots(graph, layerIndex, layer); |
| 1538 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1539 | } |
| 1540 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1541 | void IDeserializer::DeserializerImpl::ParseInstanceNormalization(GraphPtr graph, unsigned int layerIndex) |
Aron Virginas-Tar | 781ced9 | 2019-10-03 11:15:39 +0100 | [diff] [blame] | 1542 | { |
| 1543 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1544 | |
| 1545 | auto inputs = GetInputs(graph, layerIndex); |
| 1546 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1547 | |
| 1548 | auto outputs = GetOutputs(graph, layerIndex); |
| 1549 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1550 | |
| 1551 | auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer(); |
| 1552 | auto fbDescriptor = fbLayer->descriptor(); |
| 1553 | |
| 1554 | armnn::InstanceNormalizationDescriptor descriptor; |
| 1555 | descriptor.m_Gamma = fbDescriptor->gamma(); |
| 1556 | descriptor.m_Beta = fbDescriptor->beta(); |
| 1557 | descriptor.m_Eps = fbDescriptor->eps(); |
| 1558 | descriptor.m_DataLayout = ToDataLayout(fbDescriptor->dataLayout()); |
| 1559 | |
| 1560 | const std::string layerName = GetLayerName(graph, layerIndex); |
| 1561 | const armnn::TensorInfo outputInfo = ToTensorInfo(outputs[0]); |
| 1562 | |
| 1563 | IConnectableLayer* layer = m_Network->AddInstanceNormalizationLayer(descriptor, layerName.c_str()); |
| 1564 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 1565 | |
| 1566 | RegisterInputSlots(graph, layerIndex, layer); |
| 1567 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1568 | } |
| 1569 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1570 | void IDeserializer::DeserializerImpl::ParseL2Normalization(GraphPtr graph, unsigned int layerIndex) |
Narumol Prangnawarat | 495701f | 2019-03-07 17:31:34 +0000 | [diff] [blame] | 1571 | { |
| 1572 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1573 | |
| 1574 | auto inputs = GetInputs(graph, layerIndex); |
| 1575 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1576 | |
| 1577 | auto outputs = GetOutputs(graph, layerIndex); |
| 1578 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1579 | auto outputInfo = ToTensorInfo(outputs[0]); |
| 1580 | |
| 1581 | auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer(); |
| 1582 | auto flatBufferDescriptor = flatBufferLayer->descriptor(); |
| 1583 | |
| 1584 | auto layerName = GetLayerName(graph, layerIndex); |
| 1585 | armnn::L2NormalizationDescriptor descriptor; |
| 1586 | descriptor.m_DataLayout = ToDataLayout(flatBufferDescriptor->dataLayout()); |
Ferran Balaguer | 0dcffec | 2019-06-18 16:25:06 +0100 | [diff] [blame] | 1587 | descriptor.m_Eps = flatBufferDescriptor->eps(); |
Narumol Prangnawarat | 495701f | 2019-03-07 17:31:34 +0000 | [diff] [blame] | 1588 | |
| 1589 | IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(descriptor, layerName.c_str()); |
| 1590 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 1591 | |
| 1592 | RegisterInputSlots(graph, layerIndex, layer); |
| 1593 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1594 | } |
| 1595 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1596 | void IDeserializer::DeserializerImpl::ParseLogicalBinary(GraphPtr graph, unsigned int layerIndex) |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 1597 | { |
| 1598 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1599 | CHECK_LOCATION(); |
| 1600 | |
| 1601 | auto inputs = GetInputs(graph, layerIndex); |
| 1602 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1603 | |
| 1604 | auto outputs = GetOutputs(graph, layerIndex); |
| 1605 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1606 | |
| 1607 | auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_LogicalBinaryLayer(); |
| 1608 | auto fbDescriptor = fbLayer->descriptor(); |
| 1609 | |
| 1610 | armnn::LogicalBinaryDescriptor descriptor; |
| 1611 | descriptor.m_Operation = ToLogicalBinaryOperation(fbDescriptor->operation()); |
| 1612 | |
| 1613 | const std::string& layerName = GetLayerName(graph, layerIndex); |
| 1614 | IConnectableLayer* layer = m_Network->AddLogicalBinaryLayer(descriptor, layerName.c_str()); |
| 1615 | |
| 1616 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1617 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1618 | |
| 1619 | RegisterInputSlots(graph, layerIndex, layer); |
| 1620 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1621 | } |
| 1622 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1623 | void IDeserializer::DeserializerImpl::ParseLogSoftmax(GraphPtr graph, unsigned int layerIndex) |
Sadik Armagan | 2625785 | 2019-10-14 13:00:47 +0100 | [diff] [blame] | 1624 | { |
| 1625 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1626 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1627 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Sadik Armagan | 2625785 | 2019-10-14 13:00:47 +0100 | [diff] [blame] | 1628 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1629 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1630 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Sadik Armagan | 2625785 | 2019-10-14 13:00:47 +0100 | [diff] [blame] | 1631 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1632 | |
| 1633 | armnn::LogSoftmaxDescriptor descriptor; |
| 1634 | descriptor.m_Beta = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->beta(); |
| 1635 | descriptor.m_Axis = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->axis(); |
| 1636 | auto layerName = GetLayerName(graph, layerIndex); |
| 1637 | |
| 1638 | IConnectableLayer* layer = m_Network->AddLogSoftmaxLayer(descriptor, layerName.c_str()); |
| 1639 | |
| 1640 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1641 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1642 | |
| 1643 | RegisterInputSlots(graph, layerIndex, layer); |
| 1644 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1645 | } |
| 1646 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1647 | void IDeserializer::DeserializerImpl::ParseMinimum(GraphPtr graph, unsigned int layerIndex) |
Aron Virginas-Tar | 0fe3245 | 2019-02-28 13:12:47 +0000 | [diff] [blame] | 1648 | { |
| 1649 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1650 | auto inputs = GetInputs(graph, layerIndex); |
| 1651 | CHECK_LOCATION(); |
| 1652 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1653 | |
| 1654 | auto outputs = GetOutputs(graph, layerIndex); |
| 1655 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1656 | |
| 1657 | auto layerName = GetLayerName(graph, layerIndex); |
| 1658 | IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str()); |
| 1659 | |
| 1660 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1661 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1662 | |
| 1663 | RegisterInputSlots(graph, layerIndex, layer); |
| 1664 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1665 | } |
| 1666 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1667 | void IDeserializer::DeserializerImpl::ParseMaximum(GraphPtr graph, unsigned int layerIndex) |
Aron Virginas-Tar | 377351e | 2019-02-27 14:42:31 +0000 | [diff] [blame] | 1668 | { |
| 1669 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1670 | auto inputs = GetInputs(graph, layerIndex); |
| 1671 | CHECK_LOCATION(); |
| 1672 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1673 | |
| 1674 | auto outputs = GetOutputs(graph, layerIndex); |
| 1675 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1676 | |
| 1677 | auto layerName = GetLayerName(graph, layerIndex); |
| 1678 | IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str()); |
| 1679 | |
| 1680 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1681 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1682 | |
| 1683 | RegisterInputSlots(graph, layerIndex, layer); |
| 1684 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1685 | } |
| 1686 | |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 1687 | const armnnSerializer::OriginsDescriptor* GetOriginsDescriptor(const armnnSerializer::SerializedGraph* graph, |
| 1688 | unsigned int layerIndex) |
| 1689 | { |
| 1690 | auto layerType = graph->layers()->Get(layerIndex)->layer_type(); |
| 1691 | |
| 1692 | switch (layerType) |
| 1693 | { |
| 1694 | case Layer::Layer_ConcatLayer: |
| 1695 | return graph->layers()->Get(layerIndex)->layer_as_ConcatLayer()->descriptor(); |
| 1696 | case Layer::Layer_MergerLayer: |
| 1697 | return graph->layers()->Get(layerIndex)->layer_as_MergerLayer()->descriptor(); |
| 1698 | default: |
| 1699 | throw armnn::Exception("unknown layer type, should be concat or merger"); |
| 1700 | } |
| 1701 | } |
| 1702 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1703 | void IDeserializer::DeserializerImpl::ParseComparison(GraphPtr graph, unsigned int layerIndex) |
Aron Virginas-Tar | e80ebd1 | 2019-10-17 16:11:54 +0100 | [diff] [blame] | 1704 | { |
| 1705 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1706 | CHECK_LOCATION(); |
| 1707 | |
| 1708 | auto inputs = GetInputs(graph, layerIndex); |
| 1709 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1710 | |
| 1711 | auto outputs = GetOutputs(graph, layerIndex); |
| 1712 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1713 | |
| 1714 | auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ComparisonLayer(); |
| 1715 | auto fbDescriptor = fbLayer->descriptor(); |
| 1716 | |
| 1717 | armnn::ComparisonDescriptor descriptor; |
| 1718 | descriptor.m_Operation = ToComparisonOperation(fbDescriptor->operation()); |
| 1719 | |
| 1720 | const std::string& layerName = GetLayerName(graph, layerIndex); |
| 1721 | IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str()); |
| 1722 | |
| 1723 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1724 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1725 | |
| 1726 | RegisterInputSlots(graph, layerIndex, layer); |
| 1727 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1728 | } |
| 1729 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1730 | void IDeserializer::DeserializerImpl::ParseElementwiseUnary(GraphPtr graph, unsigned int layerIndex) |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 1731 | { |
| 1732 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1733 | CHECK_LOCATION(); |
| 1734 | |
| 1735 | auto inputs = GetInputs(graph, layerIndex); |
| 1736 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1737 | |
| 1738 | auto outputs = GetOutputs(graph, layerIndex); |
| 1739 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1740 | |
| 1741 | auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ElementwiseUnaryLayer(); |
| 1742 | auto fbDescriptor = fbLayer->descriptor(); |
| 1743 | |
| 1744 | armnn::ElementwiseUnaryDescriptor descriptor; |
| 1745 | descriptor.m_Operation = ToUnaryOperation(fbDescriptor->operation()); |
| 1746 | |
| 1747 | const std::string& layerName = GetLayerName(graph, layerIndex); |
| 1748 | IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str()); |
| 1749 | |
| 1750 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1751 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1752 | |
| 1753 | RegisterInputSlots(graph, layerIndex, layer); |
| 1754 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1755 | } |
| 1756 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1757 | void IDeserializer::DeserializerImpl::ParseConcat(GraphPtr graph, unsigned int layerIndex) |
Jim Flynn | ac25a1b | 2019-02-28 10:40:49 +0000 | [diff] [blame] | 1758 | { |
| 1759 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1760 | CHECK_LOCATION(); |
| 1761 | |
| 1762 | auto outputs = GetOutputs(graph, layerIndex); |
| 1763 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1764 | |
Jim Flynn | ac25a1b | 2019-02-28 10:40:49 +0000 | [diff] [blame] | 1765 | auto layerName = GetLayerName(graph, layerIndex); |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 1766 | auto originsDescriptor = GetOriginsDescriptor(graph, layerIndex); |
| 1767 | unsigned int numViews = originsDescriptor->numViews(); |
| 1768 | unsigned int numDimensions = originsDescriptor->numDimensions(); |
Jim Flynn | ac25a1b | 2019-02-28 10:40:49 +0000 | [diff] [blame] | 1769 | |
| 1770 | // can now check the number of inputs == number of views |
| 1771 | auto inputs = GetInputs(graph, layerIndex); |
| 1772 | CHECK_VALID_SIZE(inputs.size(), numViews); |
| 1773 | |
| 1774 | armnn::OriginsDescriptor descriptor(numViews, numDimensions); |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 1775 | auto originsPtr = originsDescriptor->viewOrigins(); |
Jim Flynn | ac25a1b | 2019-02-28 10:40:49 +0000 | [diff] [blame] | 1776 | for (unsigned int v = 0; v < numViews; ++v) |
| 1777 | { |
| 1778 | auto originPtr = originsPtr->Get(v); |
| 1779 | for (unsigned int d = 0; d < numDimensions; ++d) |
| 1780 | { |
| 1781 | uint32_t value = originPtr->data()->Get(d); |
| 1782 | descriptor.SetViewOriginCoord(v, d, value); |
| 1783 | } |
| 1784 | } |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 1785 | descriptor.SetConcatAxis(originsDescriptor->concatAxis()); |
Jim Flynn | ac25a1b | 2019-02-28 10:40:49 +0000 | [diff] [blame] | 1786 | |
Jim Flynn | 906f946 | 2019-05-10 13:55:21 +0100 | [diff] [blame] | 1787 | IConnectableLayer* layer = m_Network->AddConcatLayer(descriptor, layerName.c_str()); |
Jim Flynn | ac25a1b | 2019-02-28 10:40:49 +0000 | [diff] [blame] | 1788 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1789 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1790 | |
| 1791 | RegisterInputSlots(graph, layerIndex, layer); |
| 1792 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1793 | } |
| 1794 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1795 | void IDeserializer::DeserializerImpl::ParseMultiplication(GraphPtr graph, unsigned int layerIndex) |
Sadik Armagan | 5f45027 | 2019-02-12 14:31:45 +0000 | [diff] [blame] | 1796 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1797 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1798 | auto inputs = GetInputs(graph, layerIndex); |
Sadik Armagan | 5f45027 | 2019-02-12 14:31:45 +0000 | [diff] [blame] | 1799 | CHECK_LOCATION(); |
| 1800 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1801 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1802 | auto outputs = GetOutputs(graph, layerIndex); |
Sadik Armagan | 5f45027 | 2019-02-12 14:31:45 +0000 | [diff] [blame] | 1803 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1804 | |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 1805 | auto layerName = GetLayerName(graph, layerIndex); |
| 1806 | IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str()); |
Sadik Armagan | 5f45027 | 2019-02-12 14:31:45 +0000 | [diff] [blame] | 1807 | |
| 1808 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1809 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1810 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1811 | RegisterInputSlots(graph, layerIndex, layer); |
| 1812 | RegisterOutputSlots(graph, layerIndex, layer); |
Sadik Armagan | 5f45027 | 2019-02-12 14:31:45 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1815 | void IDeserializer::DeserializerImpl::ParseFloor(GraphPtr graph, unsigned int layerIndex) |
Finn Williams | dd2ba7e | 2019-03-01 11:51:52 +0000 | [diff] [blame] | 1816 | { |
| 1817 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1818 | CHECK_LOCATION(); |
| 1819 | |
| 1820 | auto inputs = GetInputs(graph, layerIndex); |
| 1821 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1822 | |
| 1823 | auto outputs = GetOutputs(graph, layerIndex); |
| 1824 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1825 | |
| 1826 | auto layerName = GetLayerName(graph, layerIndex); |
| 1827 | |
| 1828 | armnn::IConnectableLayer* layer; |
| 1829 | |
Nattapat Chaimanowong | c192f35 | 2019-03-05 17:35:28 +0000 | [diff] [blame] | 1830 | layer = m_Network->AddFloorLayer(layerName.c_str()); |
Finn Williams | dd2ba7e | 2019-03-01 11:51:52 +0000 | [diff] [blame] | 1831 | |
| 1832 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1833 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1834 | |
| 1835 | RegisterInputSlots(graph, layerIndex, layer); |
| 1836 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1837 | } |
| 1838 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1839 | void IDeserializer::DeserializerImpl::ParseFullyConnected(GraphPtr graph, unsigned int layerIndex) |
Sadik Armagan | dbb0c0c | 2019-02-21 09:01:41 +0000 | [diff] [blame] | 1840 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1841 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1842 | auto inputs = GetInputs(graph, layerIndex); |
Sadik Armagan | dbb0c0c | 2019-02-21 09:01:41 +0000 | [diff] [blame] | 1843 | CHECK_LOCATION(); |
| 1844 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1845 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1846 | auto outputs = GetOutputs(graph, layerIndex); |
Sadik Armagan | dbb0c0c | 2019-02-21 09:01:41 +0000 | [diff] [blame] | 1847 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1848 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1849 | auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer(); |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 1850 | auto layerName = GetLayerName(graph, layerIndex); |
Sadik Armagan | dbb0c0c | 2019-02-21 09:01:41 +0000 | [diff] [blame] | 1851 | auto flatBufferDescriptor = flatBufferLayer->descriptor(); |
| 1852 | |
| 1853 | armnn::FullyConnectedDescriptor fullyConnectedDescriptor; |
| 1854 | fullyConnectedDescriptor.m_BiasEnabled = flatBufferDescriptor->biasEnabled(); |
| 1855 | fullyConnectedDescriptor.m_TransposeWeightMatrix = flatBufferDescriptor->transposeWeightsMatrix(); |
| 1856 | |
| 1857 | armnn::ConstTensor weightsTensor = ToConstTensor(flatBufferLayer->weights()); |
| 1858 | |
| 1859 | armnn::IConnectableLayer* layer; |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1860 | armnn::Optional<armnn::ConstTensor> optionalBiases = armnn::EmptyOptional(); |
Sadik Armagan | dbb0c0c | 2019-02-21 09:01:41 +0000 | [diff] [blame] | 1861 | if (flatBufferDescriptor->biasEnabled()) |
| 1862 | { |
| 1863 | armnn::ConstTensor biasTensorData = ToConstTensor(flatBufferLayer->biases()); |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1864 | optionalBiases = armnn::Optional<armnn::ConstTensor>(biasTensorData); |
Sadik Armagan | dbb0c0c | 2019-02-21 09:01:41 +0000 | [diff] [blame] | 1865 | } |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1866 | layer = m_Network->AddFullyConnectedLayer(fullyConnectedDescriptor, |
| 1867 | weightsTensor, |
| 1868 | optionalBiases, |
| 1869 | layerName.c_str()); |
Sadik Armagan | dbb0c0c | 2019-02-21 09:01:41 +0000 | [diff] [blame] | 1870 | |
| 1871 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1872 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1873 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1874 | RegisterInputSlots(graph, layerIndex, layer); |
| 1875 | RegisterOutputSlots(graph, layerIndex, layer); |
Sadik Armagan | dbb0c0c | 2019-02-21 09:01:41 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1878 | void IDeserializer::DeserializerImpl::ParsePad(GraphPtr graph, unsigned int layerIndex) |
Nattapat Chaimanowong | ebb0f9c | 2019-03-01 12:14:06 +0000 | [diff] [blame] | 1879 | { |
| 1880 | CHECK_LAYERS(graph, 0, layerIndex); |
| 1881 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1882 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Nattapat Chaimanowong | ebb0f9c | 2019-03-01 12:14:06 +0000 | [diff] [blame] | 1883 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1884 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1885 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Nattapat Chaimanowong | ebb0f9c | 2019-03-01 12:14:06 +0000 | [diff] [blame] | 1886 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1887 | |
| 1888 | auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_PadLayer()->descriptor(); |
| 1889 | auto flatBufferPadList = flatBufferDescriptor->padList(); |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 1890 | float padValue = flatBufferDescriptor->padValue(); |
Nattapat Chaimanowong | ebb0f9c | 2019-03-01 12:14:06 +0000 | [diff] [blame] | 1891 | |
| 1892 | if (flatBufferPadList->Length() % 2 != 0) |
| 1893 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 1894 | throw ParseException(fmt::format("The size of the pad list must be divisible by 2 {}", |
| 1895 | CHECK_LOCATION().AsString())); |
Nattapat Chaimanowong | ebb0f9c | 2019-03-01 12:14:06 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | std::vector<std::pair<unsigned int, unsigned int>> padList; |
| 1899 | padList.reserve(flatBufferPadList->Length() / 2); |
| 1900 | for (unsigned int i = 0; i < flatBufferPadList->Length() - 1; i += 2) |
| 1901 | { |
| 1902 | padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1)); |
| 1903 | } |
| 1904 | |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 1905 | armnn::PadDescriptor descriptor(padList, padValue); |
Nattapat Chaimanowong | ebb0f9c | 2019-03-01 12:14:06 +0000 | [diff] [blame] | 1906 | |
| 1907 | auto layerName = GetLayerName(graph, layerIndex); |
| 1908 | IConnectableLayer* layer = m_Network->AddPadLayer(descriptor, layerName.c_str()); |
| 1909 | |
| 1910 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1911 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1912 | |
| 1913 | RegisterInputSlots(graph, layerIndex, layer); |
| 1914 | RegisterOutputSlots(graph, layerIndex, layer); |
| 1915 | } |
| 1916 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1917 | void IDeserializer::DeserializerImpl::ParsePermute(GraphPtr graph, unsigned int layerIndex) |
Nattapat Chaimanowong | 30b0020 | 2019-02-20 17:31:34 +0000 | [diff] [blame] | 1918 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1919 | CHECK_LAYERS(graph, 0, layerIndex); |
Nattapat Chaimanowong | 30b0020 | 2019-02-20 17:31:34 +0000 | [diff] [blame] | 1920 | |
| 1921 | auto dimsMapping = |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1922 | graph->layers()->Get(layerIndex)->layer_as_PermuteLayer()->descriptor()->dimMappings(); |
Nattapat Chaimanowong | 30b0020 | 2019-02-20 17:31:34 +0000 | [diff] [blame] | 1923 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1924 | auto inputs = GetInputs(graph, layerIndex); |
Nattapat Chaimanowong | 30b0020 | 2019-02-20 17:31:34 +0000 | [diff] [blame] | 1925 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1926 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1927 | auto outputs = GetOutputs(graph, layerIndex); |
Nattapat Chaimanowong | 30b0020 | 2019-02-20 17:31:34 +0000 | [diff] [blame] | 1928 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1929 | auto outputInfo = ToTensorInfo(outputs[0]); |
| 1930 | |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 1931 | auto layerName = GetLayerName(graph, layerIndex); |
Nattapat Chaimanowong | 30b0020 | 2019-02-20 17:31:34 +0000 | [diff] [blame] | 1932 | const armnn::PermuteDescriptor descriptor(armnn::PermutationVector(dimsMapping->data(), dimsMapping->Length())); |
| 1933 | |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 1934 | IConnectableLayer* layer = m_Network->AddPermuteLayer(descriptor, layerName.c_str()); |
Nattapat Chaimanowong | 30b0020 | 2019-02-20 17:31:34 +0000 | [diff] [blame] | 1935 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 1936 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 1937 | RegisterInputSlots(graph, layerIndex, layer); |
| 1938 | RegisterOutputSlots(graph, layerIndex, layer); |
Nattapat Chaimanowong | 30b0020 | 2019-02-20 17:31:34 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 1941 | armnn::Pooling2dDescriptor IDeserializer::DeserializerImpl::GetPoolingDescriptor(PoolingDescriptor pooling2dDesc, |
Nattapat Chaimanowong | 30b0020 | 2019-02-20 17:31:34 +0000 | [diff] [blame] | 1942 | unsigned int layerIndex) |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 1943 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 1944 | IgnoreUnused(layerIndex); |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 1945 | armnn::Pooling2dDescriptor desc; |
| 1946 | |
| 1947 | switch (pooling2dDesc->poolType()) |
| 1948 | { |
| 1949 | case PoolingAlgorithm_Average: |
| 1950 | { |
| 1951 | desc.m_PoolType = armnn::PoolingAlgorithm::Average; |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 1952 | break; |
| 1953 | } |
| 1954 | case PoolingAlgorithm_Max: |
| 1955 | { |
| 1956 | desc.m_PoolType = armnn::PoolingAlgorithm::Max; |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 1957 | break; |
| 1958 | } |
| 1959 | default: |
| 1960 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1961 | ARMNN_ASSERT_MSG(false, "Unsupported pooling algorithm"); |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | switch (pooling2dDesc->outputShapeRounding()) |
| 1966 | { |
| 1967 | case OutputShapeRounding_Floor: |
| 1968 | { |
| 1969 | desc.m_OutputShapeRounding = armnn::OutputShapeRounding::Floor; |
| 1970 | break; |
| 1971 | } |
| 1972 | case OutputShapeRounding_Ceiling: |
| 1973 | { |
| 1974 | desc.m_OutputShapeRounding = armnn::OutputShapeRounding::Ceiling; |
| 1975 | break; |
| 1976 | } |
| 1977 | default: |
| 1978 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1979 | ARMNN_ASSERT_MSG(false, "Unsupported output shape rounding"); |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | switch (pooling2dDesc->paddingMethod()) |
| 1984 | { |
| 1985 | case PaddingMethod_Exclude: |
| 1986 | { |
| 1987 | desc.m_PaddingMethod = armnn::PaddingMethod::Exclude; |
| 1988 | break; |
| 1989 | } |
| 1990 | case PaddingMethod_IgnoreValue: |
| 1991 | { |
| 1992 | desc.m_PaddingMethod = armnn::PaddingMethod::IgnoreValue; |
| 1993 | break; |
| 1994 | } |
| 1995 | default: |
| 1996 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1997 | ARMNN_ASSERT_MSG(false, "Unsupported padding method"); |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | switch (pooling2dDesc->dataLayout()) |
| 2002 | { |
| 2003 | case DataLayout_NCHW: |
| 2004 | { |
| 2005 | desc.m_DataLayout = armnn::DataLayout::NCHW; |
| 2006 | break; |
| 2007 | } |
| 2008 | case DataLayout_NHWC: |
| 2009 | { |
| 2010 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 2011 | break; |
| 2012 | } |
| 2013 | default: |
| 2014 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2015 | ARMNN_ASSERT_MSG(false, "Unsupported data layout"); |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | desc.m_PadRight = pooling2dDesc->padRight(); |
| 2020 | desc.m_PadLeft = pooling2dDesc->padLeft(); |
| 2021 | desc.m_PadBottom = pooling2dDesc->padBottom(); |
| 2022 | desc.m_PadTop = pooling2dDesc->padTop(); |
| 2023 | desc.m_StrideX = pooling2dDesc->strideX(); |
| 2024 | desc.m_StrideY = pooling2dDesc->strideY(); |
| 2025 | desc.m_PoolWidth = pooling2dDesc->poolWidth(); |
| 2026 | desc.m_PoolHeight = pooling2dDesc->poolHeight(); |
| 2027 | |
| 2028 | return desc; |
| 2029 | } |
| 2030 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2031 | |
| 2032 | |
| 2033 | void IDeserializer::DeserializerImpl::ParsePooling2d(GraphPtr graph, unsigned int layerIndex) |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 2034 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 2035 | CHECK_LAYERS(graph, 0, layerIndex); |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 2036 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 2037 | auto pooling2dDes = graph->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->descriptor(); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 2038 | auto inputs = GetInputs(graph, layerIndex); |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 2039 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2040 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 2041 | auto outputs = GetOutputs(graph, layerIndex); |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 2042 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2043 | auto outputInfo = ToTensorInfo(outputs[0]); |
| 2044 | |
| 2045 | auto pooling2dDescriptor = GetPoolingDescriptor(pooling2dDes, layerIndex); |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 2046 | auto layerName = GetLayerName(graph, layerIndex); |
| 2047 | IConnectableLayer* layer = m_Network->AddPooling2dLayer(pooling2dDescriptor, layerName.c_str()); |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 2048 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 2049 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 2050 | RegisterInputSlots(graph, layerIndex, layer); |
| 2051 | RegisterOutputSlots(graph, layerIndex, layer); |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2054 | void IDeserializer::DeserializerImpl::ParseQuantize(GraphPtr graph, unsigned int layerIndex) |
Derek Lamberti | 87acb27 | 2019-03-27 16:51:31 +0000 | [diff] [blame] | 2055 | { |
| 2056 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2057 | |
| 2058 | auto inputs = GetInputs(graph, layerIndex); |
| 2059 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2060 | |
| 2061 | auto outputs = GetOutputs(graph, layerIndex); |
| 2062 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2063 | auto outputInfo = ToTensorInfo(outputs[0]); |
| 2064 | |
| 2065 | auto layerName = GetLayerName(graph, layerIndex); |
| 2066 | IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str()); |
| 2067 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 2068 | |
| 2069 | RegisterInputSlots(graph, layerIndex, layer); |
| 2070 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2071 | } |
| 2072 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2073 | armnn::TensorInfo IDeserializer::DeserializerImpl::OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo, |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 2074 | const std::vector<uint32_t>& targetDimsIn) |
| 2075 | { |
| 2076 | std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end()); |
| 2077 | const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1); |
| 2078 | |
| 2079 | if (stretchDim != targetDimsIn.end()) |
| 2080 | { |
| 2081 | if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end()) |
| 2082 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 2083 | throw ParseException(fmt::format("At most one component of shape can be -1 {}", |
| 2084 | CHECK_LOCATION().AsString())); |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | auto targetNumElements = |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 2088 | armnn::numeric_cast<unsigned int>( |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 2089 | std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>())); |
| 2090 | |
| 2091 | auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim)); |
| 2092 | outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements; |
| 2093 | } |
| 2094 | |
| 2095 | TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data()); |
| 2096 | |
| 2097 | armnn::TensorInfo reshapeInfo = inputTensorInfo; |
| 2098 | reshapeInfo.SetShape(outputShape); |
| 2099 | |
| 2100 | return reshapeInfo; |
| 2101 | } |
| 2102 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2103 | void IDeserializer::DeserializerImpl::ParseRank(GraphPtr graph, unsigned int layerIndex) |
Finn Williams | 2605b23 | 2020-06-10 15:53:46 +0100 | [diff] [blame] | 2104 | { |
| 2105 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2106 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2107 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Finn Williams | 2605b23 | 2020-06-10 15:53:46 +0100 | [diff] [blame] | 2108 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2109 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2110 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Finn Williams | 2605b23 | 2020-06-10 15:53:46 +0100 | [diff] [blame] | 2111 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2112 | |
| 2113 | auto layerName = GetLayerName(graph, layerIndex); |
| 2114 | IConnectableLayer* layer = m_Network->AddRankLayer( layerName.c_str()); |
| 2115 | |
| 2116 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2117 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2118 | |
| 2119 | RegisterInputSlots(graph, layerIndex, layer); |
| 2120 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2121 | } |
| 2122 | |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 2123 | void IDeserializer::DeserializerImpl::ParseReduce(GraphPtr graph, unsigned int layerIndex) |
| 2124 | { |
| 2125 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2126 | CHECK_LOCATION(); |
| 2127 | |
| 2128 | auto inputs = GetInputs(graph, layerIndex); |
| 2129 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2130 | |
| 2131 | auto outputs = GetOutputs(graph, layerIndex); |
| 2132 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2133 | |
| 2134 | auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ReduceLayer(); |
| 2135 | auto fbDescriptor = fbLayer->descriptor(); |
| 2136 | auto flatBufferAxis = fbDescriptor->axis(); |
| 2137 | |
| 2138 | armnn::ReduceDescriptor descriptor; |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 2139 | descriptor.m_KeepDims = fbDescriptor->keepDims(); |
| 2140 | descriptor.m_vAxis = std::vector<unsigned int>(flatBufferAxis->begin(), flatBufferAxis->end()); |
| 2141 | descriptor.m_ReduceOperation = ToReduceOperation(fbDescriptor->reduceOperation()); |
| 2142 | |
| 2143 | const std::string& layerName = GetLayerName(graph, layerIndex); |
| 2144 | IConnectableLayer* layer = m_Network->AddReduceLayer(descriptor, layerName.c_str()); |
| 2145 | |
| 2146 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2147 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2148 | |
| 2149 | RegisterInputSlots(graph, layerIndex, layer); |
| 2150 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2151 | } |
| 2152 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2153 | void IDeserializer::DeserializerImpl::ParseReshape(GraphPtr graph, unsigned int layerIndex) |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 2154 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 2155 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2156 | auto inputs = GetInputs(graph, layerIndex); |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 2157 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 2158 | auto outputs = GetOutputs(graph, layerIndex); |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 2159 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2160 | |
| 2161 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2162 | armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]); |
| 2163 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 2164 | const auto targetDims = graph->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->descriptor()->targetShape(); |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 2165 | std::vector<uint32_t> outputDims(targetDims->begin(), targetDims->begin() + targetDims->size()); |
| 2166 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2167 | armnn::TensorInfo reshapeOutputTensorInfo = DeserializerImpl::OutputShapeOfReshape(inputTensorInfo, outputDims); |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 2168 | const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape(); |
| 2169 | |
| 2170 | const std::vector<uint32_t> expectedDims(outputs[0]->dimensions()->begin(), |
| 2171 | outputs[0]->dimensions()->begin() + outputs[0]->dimensions()->size()); |
| 2172 | |
| 2173 | if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, expectedDims)) |
| 2174 | { |
| 2175 | std::stringstream ss; |
| 2176 | ss << "New shape defined in reshape parameters " |
| 2177 | << reshapeOutputTensorShape |
| 2178 | << " does not equal output shape " |
| 2179 | << actualOutputTensorInfo.GetShape() |
| 2180 | << ": " |
| 2181 | << CHECK_LOCATION().AsString(); |
| 2182 | throw ParseException(ss.str()); |
| 2183 | } |
| 2184 | |
| 2185 | armnn::ReshapeDescriptor reshapeDesc; |
| 2186 | reshapeDesc.m_TargetShape = reshapeOutputTensorShape; |
| 2187 | |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 2188 | auto layerName = GetLayerName(graph, layerIndex); |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 2189 | IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str()); |
| 2190 | layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo); |
| 2191 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 2192 | RegisterInputSlots(graph, layerIndex, layer); |
| 2193 | RegisterOutputSlots(graph, layerIndex, layer); |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2196 | void IDeserializer::DeserializerImpl::ParseResize(GraphPtr graph, unsigned int layerIndex) |
FinnWilliamsArm | 6fb339a | 2019-06-28 15:07:10 +0100 | [diff] [blame] | 2197 | { |
| 2198 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2199 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2200 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
FinnWilliamsArm | 6fb339a | 2019-06-28 15:07:10 +0100 | [diff] [blame] | 2201 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2202 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2203 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
FinnWilliamsArm | 6fb339a | 2019-06-28 15:07:10 +0100 | [diff] [blame] | 2204 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2205 | |
| 2206 | auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeLayer()->descriptor(); |
| 2207 | |
| 2208 | armnn::ResizeDescriptor descriptor; |
| 2209 | descriptor.m_TargetWidth = flatBufferDescriptor->targetWidth(); |
| 2210 | descriptor.m_TargetHeight = flatBufferDescriptor->targetHeight(); |
| 2211 | descriptor.m_Method = ToResizeMethod(flatBufferDescriptor->method()); |
| 2212 | descriptor.m_DataLayout = ToDataLayout(flatBufferDescriptor->dataLayout()); |
David Monahan | 4a0c9b9 | 2020-05-30 09:48:39 +0100 | [diff] [blame] | 2213 | descriptor.m_AlignCorners = flatBufferDescriptor->alignCorners(); |
| 2214 | descriptor.m_HalfPixelCenters = flatBufferDescriptor->halfPixelCenters(); |
FinnWilliamsArm | 6fb339a | 2019-06-28 15:07:10 +0100 | [diff] [blame] | 2215 | |
| 2216 | auto layerName = GetLayerName(graph, layerIndex); |
| 2217 | IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str()); |
| 2218 | |
| 2219 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2220 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2221 | |
| 2222 | RegisterInputSlots(graph, layerIndex, layer); |
| 2223 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2224 | } |
| 2225 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2226 | void IDeserializer::DeserializerImpl::ParseResizeBilinear(GraphPtr graph, unsigned int layerIndex) |
Nattapat Chaimanowong | 6522cdc | 2019-03-01 16:14:13 +0000 | [diff] [blame] | 2227 | { |
| 2228 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2229 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2230 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Nattapat Chaimanowong | 6522cdc | 2019-03-01 16:14:13 +0000 | [diff] [blame] | 2231 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2232 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2233 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Nattapat Chaimanowong | 6522cdc | 2019-03-01 16:14:13 +0000 | [diff] [blame] | 2234 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2235 | |
| 2236 | auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->descriptor(); |
| 2237 | |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 2238 | armnn::ResizeDescriptor descriptor; |
| 2239 | descriptor.m_TargetWidth = flatBufferDescriptor->targetWidth(); |
Nattapat Chaimanowong | 6522cdc | 2019-03-01 16:14:13 +0000 | [diff] [blame] | 2240 | descriptor.m_TargetHeight = flatBufferDescriptor->targetHeight(); |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 2241 | descriptor.m_Method = armnn::ResizeMethod::Bilinear; |
| 2242 | descriptor.m_DataLayout = ToDataLayout(flatBufferDescriptor->dataLayout()); |
David Monahan | 4a0c9b9 | 2020-05-30 09:48:39 +0100 | [diff] [blame] | 2243 | descriptor.m_AlignCorners = flatBufferDescriptor->alignCorners(); |
| 2244 | descriptor.m_HalfPixelCenters = flatBufferDescriptor->halfPixelCenters(); |
Nattapat Chaimanowong | 6522cdc | 2019-03-01 16:14:13 +0000 | [diff] [blame] | 2245 | |
| 2246 | auto layerName = GetLayerName(graph, layerIndex); |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 2247 | IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str()); |
Nattapat Chaimanowong | 6522cdc | 2019-03-01 16:14:13 +0000 | [diff] [blame] | 2248 | |
| 2249 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2250 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2251 | |
| 2252 | RegisterInputSlots(graph, layerIndex, layer); |
| 2253 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2254 | } |
| 2255 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2256 | void IDeserializer::DeserializerImpl::ParseSoftmax(GraphPtr graph, unsigned int layerIndex) |
Aron Virginas-Tar | fc413c0 | 2019-02-13 15:41:52 +0000 | [diff] [blame] | 2257 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 2258 | CHECK_LAYERS(graph, 0, layerIndex); |
Aron Virginas-Tar | fc413c0 | 2019-02-13 15:41:52 +0000 | [diff] [blame] | 2259 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2260 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Aron Virginas-Tar | fc413c0 | 2019-02-13 15:41:52 +0000 | [diff] [blame] | 2261 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2262 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2263 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Aron Virginas-Tar | fc413c0 | 2019-02-13 15:41:52 +0000 | [diff] [blame] | 2264 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2265 | |
| 2266 | armnn::SoftmaxDescriptor descriptor; |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 2267 | descriptor.m_Beta = graph->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->descriptor()->beta(); |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 2268 | auto layerName = GetLayerName(graph, layerIndex); |
Aron Virginas-Tar | fc413c0 | 2019-02-13 15:41:52 +0000 | [diff] [blame] | 2269 | |
Aron Virginas-Tar | fc413c0 | 2019-02-13 15:41:52 +0000 | [diff] [blame] | 2270 | IConnectableLayer* layer = m_Network->AddSoftmaxLayer(descriptor, layerName.c_str()); |
| 2271 | |
| 2272 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2273 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2274 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 2275 | RegisterInputSlots(graph, layerIndex, layer); |
| 2276 | RegisterOutputSlots(graph, layerIndex, layer); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 2277 | } |
Aron Virginas-Tar | fc413c0 | 2019-02-13 15:41:52 +0000 | [diff] [blame] | 2278 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2279 | void IDeserializer::DeserializerImpl::ParseSpaceToBatchNd(GraphPtr graph, unsigned int layerIndex) |
Nattapat Chaimanowong | 4528699 | 2019-02-26 15:53:02 +0000 | [diff] [blame] | 2280 | { |
| 2281 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2282 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2283 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Nattapat Chaimanowong | 4528699 | 2019-02-26 15:53:02 +0000 | [diff] [blame] | 2284 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2285 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2286 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Nattapat Chaimanowong | 4528699 | 2019-02-26 15:53:02 +0000 | [diff] [blame] | 2287 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2288 | |
| 2289 | auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->descriptor(); |
| 2290 | auto flatBufferPadList = flatBufferDescriptor->padList(); |
| 2291 | auto flatBufferBlockShape = flatBufferDescriptor->blockShape(); |
| 2292 | |
| 2293 | if (flatBufferPadList->Length() % 2 != 0) |
| 2294 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 2295 | throw ParseException(fmt::format("The size of the pad list must be divisible by 2 {}", |
| 2296 | CHECK_LOCATION().AsString())); |
Nattapat Chaimanowong | 4528699 | 2019-02-26 15:53:02 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | std::vector<std::pair<unsigned int, unsigned int>> padList; |
| 2300 | padList.reserve(flatBufferPadList->Length() / 2); |
| 2301 | for (unsigned int i = 0; i < flatBufferPadList->Length() - 1; i += 2) |
| 2302 | { |
| 2303 | padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1)); |
| 2304 | } |
| 2305 | |
| 2306 | armnn::SpaceToBatchNdDescriptor descriptor; |
| 2307 | descriptor.m_DataLayout = ToDataLayout(flatBufferDescriptor->dataLayout()); |
| 2308 | descriptor.m_BlockShape = |
| 2309 | std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end()); |
| 2310 | descriptor.m_PadList = padList; |
| 2311 | |
| 2312 | auto layerName = GetLayerName(graph, layerIndex); |
| 2313 | IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(descriptor, layerName.c_str()); |
| 2314 | |
| 2315 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2316 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2317 | |
| 2318 | RegisterInputSlots(graph, layerIndex, layer); |
| 2319 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2320 | } |
| 2321 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2322 | void IDeserializer::DeserializerImpl::ParseSpaceToDepth(GraphPtr graph, unsigned int layerIndex) |
Aron Virginas-Tar | aa06714 | 2019-06-11 16:01:44 +0100 | [diff] [blame] | 2323 | { |
| 2324 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2325 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2326 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Aron Virginas-Tar | aa06714 | 2019-06-11 16:01:44 +0100 | [diff] [blame] | 2327 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2328 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2329 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Aron Virginas-Tar | aa06714 | 2019-06-11 16:01:44 +0100 | [diff] [blame] | 2330 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2331 | |
| 2332 | auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->descriptor(); |
| 2333 | |
| 2334 | armnn::SpaceToDepthDescriptor descriptor; |
| 2335 | descriptor.m_BlockSize = flatBufferDescriptor->blockSize(); |
| 2336 | descriptor.m_DataLayout = ToDataLayout(flatBufferDescriptor->dataLayout()); |
| 2337 | |
| 2338 | auto layerName = GetLayerName(graph, layerIndex); |
| 2339 | IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str()); |
| 2340 | |
| 2341 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2342 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2343 | |
| 2344 | RegisterInputSlots(graph, layerIndex, layer); |
| 2345 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2346 | } |
| 2347 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2348 | armnn::NormalizationDescriptor IDeserializer::DeserializerImpl::GetNormalizationDescriptor( |
| 2349 | NormalizationDescriptorPtr normalizationDescriptor, |
Nina Drozd | 5772878 | 2019-02-27 10:53:27 +0000 | [diff] [blame] | 2350 | unsigned int layerIndex) |
| 2351 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 2352 | IgnoreUnused(layerIndex); |
Nina Drozd | 5772878 | 2019-02-27 10:53:27 +0000 | [diff] [blame] | 2353 | armnn::NormalizationDescriptor desc; |
| 2354 | |
| 2355 | switch (normalizationDescriptor->normChannelType()) |
| 2356 | { |
| 2357 | case NormalizationAlgorithmChannel_Across: |
| 2358 | { |
| 2359 | desc.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across; |
| 2360 | break; |
| 2361 | } |
| 2362 | case NormalizationAlgorithmChannel_Within: |
| 2363 | { |
| 2364 | desc.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Within; |
| 2365 | break; |
| 2366 | } |
| 2367 | default: |
| 2368 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2369 | ARMNN_ASSERT_MSG(false, "Unsupported normalization channel type"); |
Nina Drozd | 5772878 | 2019-02-27 10:53:27 +0000 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | |
| 2373 | switch (normalizationDescriptor->normMethodType()) |
| 2374 | { |
| 2375 | case NormalizationAlgorithmMethod_LocalBrightness: |
| 2376 | { |
| 2377 | desc.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness; |
| 2378 | break; |
| 2379 | } |
| 2380 | case NormalizationAlgorithmMethod_LocalContrast: |
| 2381 | { |
| 2382 | desc.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalContrast; |
| 2383 | break; |
| 2384 | } |
| 2385 | default: |
| 2386 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2387 | ARMNN_ASSERT_MSG(false, "Unsupported normalization method type"); |
Nina Drozd | 5772878 | 2019-02-27 10:53:27 +0000 | [diff] [blame] | 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | switch (normalizationDescriptor->dataLayout()) |
| 2392 | { |
| 2393 | case DataLayout_NCHW: |
| 2394 | { |
| 2395 | desc.m_DataLayout = armnn::DataLayout::NCHW; |
| 2396 | break; |
| 2397 | } |
| 2398 | case DataLayout_NHWC: |
| 2399 | { |
| 2400 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 2401 | break; |
| 2402 | } |
| 2403 | default: |
| 2404 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2405 | ARMNN_ASSERT_MSG(false, "Unsupported data layout"); |
Nina Drozd | 5772878 | 2019-02-27 10:53:27 +0000 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | desc.m_Alpha = normalizationDescriptor->alpha(); |
| 2410 | desc.m_Beta = normalizationDescriptor->beta(); |
| 2411 | desc.m_K = normalizationDescriptor->k(); |
| 2412 | desc.m_NormSize = normalizationDescriptor->normSize(); |
| 2413 | |
| 2414 | return desc; |
| 2415 | } |
| 2416 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2417 | void IDeserializer::DeserializerImpl::ParseNormalization(GraphPtr graph, unsigned int layerIndex) |
Nina Drozd | 5772878 | 2019-02-27 10:53:27 +0000 | [diff] [blame] | 2418 | { |
| 2419 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2420 | |
| 2421 | auto normalizationDes = graph->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->descriptor(); |
| 2422 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2423 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Nina Drozd | 5772878 | 2019-02-27 10:53:27 +0000 | [diff] [blame] | 2424 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2425 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2426 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Nina Drozd | 5772878 | 2019-02-27 10:53:27 +0000 | [diff] [blame] | 2427 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2428 | |
| 2429 | auto outputInfo = ToTensorInfo(outputs[0]); |
| 2430 | |
| 2431 | auto normalizationDescriptor = GetNormalizationDescriptor(normalizationDes, layerIndex); |
| 2432 | auto layerName = GetLayerName(graph, layerIndex); |
| 2433 | |
| 2434 | IConnectableLayer* layer = m_Network->AddNormalizationLayer(normalizationDescriptor, layerName.c_str()); |
| 2435 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 2436 | |
| 2437 | RegisterInputSlots(graph, layerIndex, layer); |
| 2438 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2439 | } |
| 2440 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2441 | void IDeserializer::DeserializerImpl::ParseRsqrt(GraphPtr graph, unsigned int layerIndex) |
Sadik Armagan | 8b42a38 | 2019-03-01 14:24:49 +0000 | [diff] [blame] | 2442 | { |
| 2443 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2444 | auto inputs = GetInputs(graph, layerIndex); |
| 2445 | CHECK_LOCATION(); |
| 2446 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2447 | |
| 2448 | auto outputs = GetOutputs(graph, layerIndex); |
| 2449 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2450 | |
| 2451 | auto layerName = GetLayerName(graph, layerIndex); |
Sadik Armagan | 8b42a38 | 2019-03-01 14:24:49 +0000 | [diff] [blame] | 2452 | |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 2453 | armnn::ElementwiseUnaryDescriptor descriptor(armnn::UnaryOperation::Rsqrt); |
| 2454 | IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str()); |
Sadik Armagan | 8b42a38 | 2019-03-01 14:24:49 +0000 | [diff] [blame] | 2455 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2456 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2457 | |
| 2458 | RegisterInputSlots(graph, layerIndex, layer); |
| 2459 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2460 | } |
| 2461 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2462 | void IDeserializer::DeserializerImpl::ParseSlice(GraphPtr graph, unsigned int layerIndex) |
Aron Virginas-Tar | 2fda80b | 2019-09-18 13:36:52 +0100 | [diff] [blame] | 2463 | { |
| 2464 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2465 | |
| 2466 | auto inputs = GetInputs(graph, layerIndex); |
| 2467 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2468 | |
| 2469 | auto outputs = GetOutputs(graph, layerIndex); |
| 2470 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2471 | |
| 2472 | auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_SliceLayer()->descriptor(); |
| 2473 | |
| 2474 | auto fbBegin = fbDescriptor->begin(); |
| 2475 | auto fbSize = fbDescriptor->size(); |
| 2476 | |
| 2477 | if (fbBegin->Length() != fbSize->Length()) |
| 2478 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 2479 | throw ParseException(fmt::format("Begin and size descriptors must have the same length {}", |
| 2480 | CHECK_LOCATION().AsString())); |
Aron Virginas-Tar | 2fda80b | 2019-09-18 13:36:52 +0100 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | armnn::SliceDescriptor descriptor; |
| 2484 | descriptor.m_Begin.insert(descriptor.m_Begin.end(), fbBegin->begin(), fbBegin->end()); |
| 2485 | descriptor.m_Size.insert(descriptor.m_Size.end(), fbSize->begin(), fbSize->end()); |
| 2486 | |
| 2487 | auto layerName = GetLayerName(graph, layerIndex); |
| 2488 | IConnectableLayer* layer = m_Network->AddSliceLayer(descriptor, layerName.c_str()); |
| 2489 | |
| 2490 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2491 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2492 | |
| 2493 | RegisterInputSlots(graph, layerIndex, layer); |
| 2494 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2495 | } |
| 2496 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2497 | void IDeserializer::DeserializerImpl::ParseStridedSlice(GraphPtr graph, unsigned int layerIndex) |
Nattapat Chaimanowong | b348521 | 2019-03-04 12:35:39 +0000 | [diff] [blame] | 2498 | { |
| 2499 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2500 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2501 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Nattapat Chaimanowong | b348521 | 2019-03-04 12:35:39 +0000 | [diff] [blame] | 2502 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2503 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2504 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Nattapat Chaimanowong | b348521 | 2019-03-04 12:35:39 +0000 | [diff] [blame] | 2505 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2506 | |
| 2507 | auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->descriptor(); |
| 2508 | |
| 2509 | auto flatBufferBegin = flatBufferDescriptor->begin(); |
| 2510 | auto flatBufferEnd = flatBufferDescriptor->end(); |
| 2511 | auto flatBufferStride = flatBufferDescriptor->stride(); |
| 2512 | |
| 2513 | if (!(flatBufferBegin->Length() == flatBufferEnd->Length() && |
| 2514 | flatBufferBegin->Length() == flatBufferStride->Length())) |
| 2515 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 2516 | throw ParseException(fmt::format("The size of the begin, end, and stride must be equal {}", |
| 2517 | CHECK_LOCATION().AsString())); |
Nattapat Chaimanowong | b348521 | 2019-03-04 12:35:39 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | std::vector<int> begin(flatBufferBegin->begin(), flatBufferBegin->end()); |
| 2521 | std::vector<int> end(flatBufferEnd->begin(), flatBufferEnd->end()); |
| 2522 | std::vector<int> stride(flatBufferStride->begin(), flatBufferStride->end()); |
| 2523 | |
| 2524 | armnn::StridedSliceDescriptor descriptor(begin, end, stride); |
| 2525 | descriptor.m_BeginMask = flatBufferDescriptor->beginMask(); |
| 2526 | descriptor.m_EndMask = flatBufferDescriptor->endMask(); |
| 2527 | descriptor.m_ShrinkAxisMask = flatBufferDescriptor->shrinkAxisMask(); |
| 2528 | descriptor.m_EllipsisMask = flatBufferDescriptor->ellipsisMask(); |
| 2529 | descriptor.m_NewAxisMask = flatBufferDescriptor->newAxisMask(); |
| 2530 | descriptor.m_DataLayout = ToDataLayout(flatBufferDescriptor->dataLayout()); |
| 2531 | |
| 2532 | auto layerName = GetLayerName(graph, layerIndex); |
| 2533 | IConnectableLayer* layer = m_Network->AddStridedSliceLayer(descriptor, layerName.c_str()); |
| 2534 | |
| 2535 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2536 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2537 | |
| 2538 | RegisterInputSlots(graph, layerIndex, layer); |
| 2539 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2540 | } |
| 2541 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2542 | void IDeserializer::DeserializerImpl::ParseSubtraction(GraphPtr graph, unsigned int layerIndex) |
Conor Kennedy | da1f975 | 2019-03-01 14:37:12 +0000 | [diff] [blame] | 2543 | { |
| 2544 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2545 | auto inputs = GetInputs(graph, layerIndex); |
| 2546 | CHECK_LOCATION(); |
| 2547 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2548 | |
| 2549 | auto outputs = GetOutputs(graph, layerIndex); |
| 2550 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2551 | |
| 2552 | auto layerName = GetLayerName(graph, layerIndex); |
| 2553 | IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str()); |
| 2554 | |
| 2555 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2556 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2557 | |
| 2558 | RegisterInputSlots(graph, layerIndex, layer); |
| 2559 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2560 | } |
| 2561 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2562 | void IDeserializer::DeserializerImpl::ParseGather(GraphPtr graph, unsigned int layerIndex) |
Saoirse Stewart | a1ed73a | 2019-03-04 13:40:12 +0000 | [diff] [blame] | 2563 | { |
| 2564 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2565 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2566 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Saoirse Stewart | a1ed73a | 2019-03-04 13:40:12 +0000 | [diff] [blame] | 2567 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2568 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2569 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Saoirse Stewart | a1ed73a | 2019-03-04 13:40:12 +0000 | [diff] [blame] | 2570 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2571 | |
Teresa Charlin | 5266473 | 2020-06-29 16:27:03 +0100 | [diff] [blame] | 2572 | armnn::GatherDescriptor descriptor; |
| 2573 | descriptor.m_Axis = graph->layers()->Get(layerIndex)->layer_as_GatherLayer()->descriptor()->axis(); |
| 2574 | |
Saoirse Stewart | a1ed73a | 2019-03-04 13:40:12 +0000 | [diff] [blame] | 2575 | auto layerName = GetLayerName(graph, layerIndex); |
Teresa Charlin | 5266473 | 2020-06-29 16:27:03 +0100 | [diff] [blame] | 2576 | IConnectableLayer* layer = m_Network->AddGatherLayer(descriptor, layerName.c_str()); |
Saoirse Stewart | a1ed73a | 2019-03-04 13:40:12 +0000 | [diff] [blame] | 2577 | |
| 2578 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
Saoirse Stewart | a1ed73a | 2019-03-04 13:40:12 +0000 | [diff] [blame] | 2579 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2580 | |
| 2581 | RegisterInputSlots(graph, layerIndex, layer); |
| 2582 | RegisterOutputSlots(graph, layerIndex, layer); |
Saoirse Stewart | a1ed73a | 2019-03-04 13:40:12 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2585 | void IDeserializer::DeserializerImpl::ParseMean(GraphPtr graph, unsigned int layerIndex) |
Sadik Armagan | ac97c8c | 2019-03-04 17:44:21 +0000 | [diff] [blame] | 2586 | { |
| 2587 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2588 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2589 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Sadik Armagan | ac97c8c | 2019-03-04 17:44:21 +0000 | [diff] [blame] | 2590 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2591 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2592 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Sadik Armagan | ac97c8c | 2019-03-04 17:44:21 +0000 | [diff] [blame] | 2593 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2594 | |
| 2595 | auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_MeanLayer()->descriptor(); |
| 2596 | auto flatBufferAxis = flatBufferDescriptor->axis(); |
| 2597 | auto flatBufferKeepDims = flatBufferDescriptor->keepDims(); |
| 2598 | |
| 2599 | armnn::MeanDescriptor descriptor; |
| 2600 | descriptor.m_Axis = std::vector<unsigned int>(flatBufferAxis->begin(), flatBufferAxis->end()); |
| 2601 | descriptor.m_KeepDims = flatBufferKeepDims; |
| 2602 | |
| 2603 | auto layerName = GetLayerName(graph, layerIndex); |
| 2604 | IConnectableLayer* layer = m_Network->AddMeanLayer(descriptor, layerName.c_str()); |
| 2605 | |
| 2606 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2607 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2608 | |
| 2609 | RegisterInputSlots(graph, layerIndex, layer); |
| 2610 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2611 | } |
| 2612 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2613 | void IDeserializer::DeserializerImpl::ParseSplitter(GraphPtr graph, unsigned int layerIndex) |
Jim Flynn | 18ce338 | 2019-03-08 11:08:30 +0000 | [diff] [blame] | 2614 | { |
| 2615 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2616 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2617 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Jim Flynn | 18ce338 | 2019-03-08 11:08:30 +0000 | [diff] [blame] | 2618 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2619 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2620 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Jim Flynn | 18ce338 | 2019-03-08 11:08:30 +0000 | [diff] [blame] | 2621 | |
| 2622 | auto flatBufferViewsDescriptor = graph->layers()->Get(layerIndex)->layer_as_SplitterLayer()->descriptor(); |
| 2623 | auto flatBufferViewSizes = flatBufferViewsDescriptor->viewSizes(); |
| 2624 | auto flatBufferOriginsDescriptor = flatBufferViewsDescriptor->origins(); |
| 2625 | auto flatBufferViewOrigins = flatBufferOriginsDescriptor->viewOrigins(); |
| 2626 | uint32_t numViews = flatBufferOriginsDescriptor->numViews(); |
| 2627 | uint32_t numDimensions = flatBufferOriginsDescriptor->numDimensions(); |
| 2628 | |
| 2629 | // Check numViews and numDimensions corresponds to the ones already serialized ... |
| 2630 | // numViews == flatBufferViewSizes.size(); |
| 2631 | // foreach: numDimensions == flatBufferViewSizes[x].size(); |
| 2632 | |
| 2633 | armnn::ViewsDescriptor viewsDescriptor(numViews, numDimensions); |
| 2634 | for(unsigned int vIdx = 0; vIdx < numViews; ++vIdx) |
| 2635 | { |
| 2636 | for (unsigned int dIdx = 0; dIdx < numDimensions; ++dIdx) |
| 2637 | { |
| 2638 | viewsDescriptor.SetViewSize(vIdx, dIdx, flatBufferViewSizes->Get(vIdx)->data()->Get(dIdx)); |
| 2639 | viewsDescriptor.SetViewOriginCoord(vIdx, dIdx, flatBufferViewOrigins->Get(vIdx)->data()->Get(dIdx)); |
| 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | auto layerName = GetLayerName(graph, layerIndex); |
| 2644 | IConnectableLayer* layer = m_Network->AddSplitterLayer(viewsDescriptor, layerName.c_str()); |
| 2645 | |
| 2646 | // I could have as many outputs as views ... |
| 2647 | for(unsigned int vIdx = 0; vIdx < numViews; ++vIdx) |
| 2648 | { |
| 2649 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[vIdx]); |
| 2650 | layer->GetOutputSlot(vIdx).SetTensorInfo(outputTensorInfo); |
| 2651 | } |
| 2652 | |
| 2653 | RegisterInputSlots(graph, layerIndex, layer); |
| 2654 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2655 | } |
| 2656 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2657 | armnn::LstmDescriptor IDeserializer::DeserializerImpl::GetLstmDescriptor(LstmDescriptorPtr lstmDescriptor) |
Jim Flynn | 11af375 | 2019-03-19 17:22:29 +0000 | [diff] [blame] | 2658 | { |
| 2659 | armnn::LstmDescriptor desc; |
| 2660 | |
| 2661 | desc.m_ActivationFunc = lstmDescriptor->activationFunc(); |
| 2662 | desc.m_ClippingThresCell = lstmDescriptor->clippingThresCell(); |
| 2663 | desc.m_ClippingThresProj = lstmDescriptor->clippingThresProj(); |
| 2664 | desc.m_CifgEnabled = lstmDescriptor->cifgEnabled(); |
| 2665 | desc.m_PeepholeEnabled = lstmDescriptor->peepholeEnabled(); |
| 2666 | desc.m_ProjectionEnabled = lstmDescriptor->projectionEnabled(); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 2667 | desc.m_LayerNormEnabled = lstmDescriptor->layerNormEnabled(); |
Jim Flynn | 11af375 | 2019-03-19 17:22:29 +0000 | [diff] [blame] | 2668 | |
| 2669 | return desc; |
| 2670 | } |
| 2671 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2672 | void IDeserializer::DeserializerImpl::ParseLstm(GraphPtr graph, unsigned int layerIndex) |
Jim Flynn | 11af375 | 2019-03-19 17:22:29 +0000 | [diff] [blame] | 2673 | { |
| 2674 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2675 | |
| 2676 | auto inputs = GetInputs(graph, layerIndex); |
| 2677 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 2678 | |
| 2679 | auto outputs = GetOutputs(graph, layerIndex); |
| 2680 | CHECK_VALID_SIZE(outputs.size(), 4); |
| 2681 | |
| 2682 | auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_LstmLayer(); |
| 2683 | auto layerName = GetLayerName(graph, layerIndex); |
| 2684 | auto flatBufferDescriptor = flatBufferLayer->descriptor(); |
| 2685 | auto flatBufferInputParams = flatBufferLayer->inputParams(); |
| 2686 | |
| 2687 | auto lstmDescriptor = GetLstmDescriptor(flatBufferDescriptor); |
| 2688 | |
| 2689 | armnn::LstmInputParams lstmInputParams; |
| 2690 | |
| 2691 | armnn::ConstTensor inputToForgetWeights = ToConstTensor(flatBufferInputParams->inputToForgetWeights()); |
| 2692 | armnn::ConstTensor inputToCellWeights = ToConstTensor(flatBufferInputParams->inputToCellWeights()); |
| 2693 | armnn::ConstTensor inputToOutputWeights = ToConstTensor(flatBufferInputParams->inputToOutputWeights()); |
| 2694 | armnn::ConstTensor recurrentToForgetWeights = ToConstTensor(flatBufferInputParams->recurrentToForgetWeights()); |
| 2695 | armnn::ConstTensor recurrentToCellWeights = ToConstTensor(flatBufferInputParams->recurrentToCellWeights()); |
| 2696 | armnn::ConstTensor recurrentToOutputWeights = ToConstTensor(flatBufferInputParams->recurrentToOutputWeights()); |
| 2697 | armnn::ConstTensor forgetGateBias = ToConstTensor(flatBufferInputParams->forgetGateBias()); |
| 2698 | armnn::ConstTensor cellBias = ToConstTensor(flatBufferInputParams->cellBias()); |
| 2699 | armnn::ConstTensor outputGateBias = ToConstTensor(flatBufferInputParams->outputGateBias()); |
| 2700 | |
| 2701 | lstmInputParams.m_InputToForgetWeights = &inputToForgetWeights; |
| 2702 | lstmInputParams.m_InputToCellWeights = &inputToCellWeights; |
| 2703 | lstmInputParams.m_InputToOutputWeights = &inputToOutputWeights; |
| 2704 | lstmInputParams.m_RecurrentToForgetWeights = &recurrentToForgetWeights; |
| 2705 | lstmInputParams.m_RecurrentToCellWeights = &recurrentToCellWeights; |
| 2706 | lstmInputParams.m_RecurrentToOutputWeights = &recurrentToOutputWeights; |
| 2707 | lstmInputParams.m_ForgetGateBias = &forgetGateBias; |
| 2708 | lstmInputParams.m_CellBias = &cellBias; |
| 2709 | lstmInputParams.m_OutputGateBias = &outputGateBias; |
| 2710 | |
| 2711 | armnn::ConstTensor inputToInputWeights; |
| 2712 | armnn::ConstTensor recurrentToInputWeights; |
| 2713 | armnn::ConstTensor cellToInputWeights; |
| 2714 | armnn::ConstTensor inputGateBias; |
| 2715 | if (!lstmDescriptor.m_CifgEnabled) |
| 2716 | { |
| 2717 | inputToInputWeights = ToConstTensor(flatBufferInputParams->inputToInputWeights()); |
| 2718 | recurrentToInputWeights = ToConstTensor(flatBufferInputParams->recurrentToInputWeights()); |
| 2719 | cellToInputWeights = ToConstTensor(flatBufferInputParams->cellToInputWeights()); |
| 2720 | inputGateBias = ToConstTensor(flatBufferInputParams->inputGateBias()); |
| 2721 | |
| 2722 | lstmInputParams.m_InputToInputWeights = &inputToInputWeights; |
| 2723 | lstmInputParams.m_RecurrentToInputWeights = &recurrentToInputWeights; |
| 2724 | lstmInputParams.m_CellToInputWeights = &cellToInputWeights; |
| 2725 | lstmInputParams.m_InputGateBias = &inputGateBias; |
| 2726 | } |
| 2727 | |
| 2728 | armnn::ConstTensor projectionWeights; |
| 2729 | armnn::ConstTensor projectionBias; |
| 2730 | if (lstmDescriptor.m_ProjectionEnabled) |
| 2731 | { |
| 2732 | projectionWeights = ToConstTensor(flatBufferInputParams->projectionWeights()); |
| 2733 | projectionBias = ToConstTensor(flatBufferInputParams->projectionBias()); |
| 2734 | |
| 2735 | lstmInputParams.m_ProjectionWeights = &projectionWeights; |
| 2736 | lstmInputParams.m_ProjectionBias = &projectionBias; |
| 2737 | } |
| 2738 | |
| 2739 | armnn::ConstTensor cellToForgetWeights; |
| 2740 | armnn::ConstTensor cellToOutputWeights; |
| 2741 | if (lstmDescriptor.m_PeepholeEnabled) |
| 2742 | { |
| 2743 | cellToForgetWeights = ToConstTensor(flatBufferInputParams->cellToForgetWeights()); |
| 2744 | cellToOutputWeights = ToConstTensor(flatBufferInputParams->cellToOutputWeights()); |
| 2745 | |
| 2746 | lstmInputParams.m_CellToForgetWeights = &cellToForgetWeights; |
| 2747 | lstmInputParams.m_CellToOutputWeights = &cellToOutputWeights; |
| 2748 | } |
| 2749 | |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 2750 | armnn::ConstTensor inputLayerNormWeights; |
| 2751 | armnn::ConstTensor forgetLayerNormWeights; |
| 2752 | armnn::ConstTensor cellLayerNormWeights; |
| 2753 | armnn::ConstTensor outputLayerNormWeights; |
| 2754 | if (lstmDescriptor.m_LayerNormEnabled) |
| 2755 | { |
| 2756 | if (!lstmDescriptor.m_CifgEnabled) |
| 2757 | { |
| 2758 | inputLayerNormWeights = ToConstTensor(flatBufferInputParams->inputLayerNormWeights()); |
| 2759 | lstmInputParams.m_InputLayerNormWeights = &inputLayerNormWeights; |
| 2760 | } |
| 2761 | forgetLayerNormWeights = ToConstTensor(flatBufferInputParams->forgetLayerNormWeights()); |
| 2762 | cellLayerNormWeights = ToConstTensor(flatBufferInputParams->cellLayerNormWeights()); |
| 2763 | outputLayerNormWeights = ToConstTensor(flatBufferInputParams->outputLayerNormWeights()); |
| 2764 | |
| 2765 | lstmInputParams.m_ForgetLayerNormWeights = &forgetLayerNormWeights; |
| 2766 | lstmInputParams.m_CellLayerNormWeights = &cellLayerNormWeights; |
| 2767 | lstmInputParams.m_OutputLayerNormWeights = &outputLayerNormWeights; |
| 2768 | } |
| 2769 | |
Jim Flynn | 11af375 | 2019-03-19 17:22:29 +0000 | [diff] [blame] | 2770 | IConnectableLayer* layer = m_Network->AddLstmLayer(lstmDescriptor, lstmInputParams, layerName.c_str()); |
| 2771 | |
| 2772 | armnn::TensorInfo outputTensorInfo1 = ToTensorInfo(outputs[0]); |
| 2773 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo1); |
| 2774 | |
| 2775 | armnn::TensorInfo outputTensorInfo2 = ToTensorInfo(outputs[1]); |
| 2776 | layer->GetOutputSlot(1).SetTensorInfo(outputTensorInfo2); |
| 2777 | |
| 2778 | armnn::TensorInfo outputTensorInfo3 = ToTensorInfo(outputs[2]); |
| 2779 | layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo3); |
| 2780 | |
| 2781 | armnn::TensorInfo outputTensorInfo4 = ToTensorInfo(outputs[3]); |
| 2782 | layer->GetOutputSlot(3).SetTensorInfo(outputTensorInfo4); |
| 2783 | |
| 2784 | RegisterInputSlots(graph, layerIndex, layer); |
| 2785 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2786 | } |
| 2787 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2788 | armnn::QLstmDescriptor IDeserializer::DeserializerImpl::GetQLstmDescriptor(QLstmDescriptorPtr qLstmDescriptor) |
James Conroy | 8d33318 | 2020-05-13 10:27:58 +0100 | [diff] [blame] | 2789 | { |
| 2790 | armnn::QLstmDescriptor desc; |
| 2791 | |
| 2792 | desc.m_CifgEnabled = qLstmDescriptor->cifgEnabled(); |
| 2793 | desc.m_PeepholeEnabled = qLstmDescriptor->peepholeEnabled(); |
| 2794 | desc.m_ProjectionEnabled = qLstmDescriptor->projectionEnabled(); |
| 2795 | desc.m_LayerNormEnabled = qLstmDescriptor->layerNormEnabled(); |
| 2796 | |
| 2797 | desc.m_CellClip = qLstmDescriptor->cellClip(); |
| 2798 | desc.m_ProjectionClip = qLstmDescriptor->projectionClip(); |
| 2799 | |
| 2800 | desc.m_InputIntermediateScale = qLstmDescriptor->inputIntermediateScale(); |
| 2801 | desc.m_ForgetIntermediateScale = qLstmDescriptor->forgetIntermediateScale(); |
| 2802 | desc.m_CellIntermediateScale = qLstmDescriptor->cellIntermediateScale(); |
| 2803 | desc.m_OutputIntermediateScale = qLstmDescriptor->outputIntermediateScale(); |
| 2804 | |
| 2805 | desc.m_HiddenStateScale = qLstmDescriptor->hiddenStateScale(); |
| 2806 | desc.m_HiddenStateZeroPoint = qLstmDescriptor->hiddenStateZeroPoint(); |
| 2807 | |
| 2808 | return desc; |
| 2809 | } |
| 2810 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2811 | void IDeserializer::DeserializerImpl::ParseQLstm(GraphPtr graph, unsigned int layerIndex) |
James Conroy | 8d33318 | 2020-05-13 10:27:58 +0100 | [diff] [blame] | 2812 | { |
| 2813 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2814 | |
| 2815 | auto inputs = GetInputs(graph, layerIndex); |
| 2816 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 2817 | |
| 2818 | auto outputs = GetOutputs(graph, layerIndex); |
| 2819 | CHECK_VALID_SIZE(outputs.size(), 3); |
| 2820 | |
| 2821 | auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QLstmLayer(); |
| 2822 | auto layerName = GetLayerName(graph, layerIndex); |
| 2823 | auto flatBufferDescriptor = flatBufferLayer->descriptor(); |
| 2824 | auto flatBufferInputParams = flatBufferLayer->inputParams(); |
| 2825 | |
| 2826 | auto qLstmDescriptor = GetQLstmDescriptor(flatBufferDescriptor); |
| 2827 | armnn::LstmInputParams qLstmInputParams; |
| 2828 | |
| 2829 | // Mandatory params |
| 2830 | armnn::ConstTensor inputToForgetWeights = ToConstTensor(flatBufferInputParams->inputToForgetWeights()); |
| 2831 | armnn::ConstTensor inputToCellWeights = ToConstTensor(flatBufferInputParams->inputToCellWeights()); |
| 2832 | armnn::ConstTensor inputToOutputWeights = ToConstTensor(flatBufferInputParams->inputToOutputWeights()); |
| 2833 | armnn::ConstTensor recurrentToForgetWeights = ToConstTensor(flatBufferInputParams->recurrentToForgetWeights()); |
| 2834 | armnn::ConstTensor recurrentToCellWeights = ToConstTensor(flatBufferInputParams->recurrentToCellWeights()); |
| 2835 | armnn::ConstTensor recurrentToOutputWeights = ToConstTensor(flatBufferInputParams->recurrentToOutputWeights()); |
| 2836 | armnn::ConstTensor forgetGateBias = ToConstTensor(flatBufferInputParams->forgetGateBias()); |
| 2837 | armnn::ConstTensor cellBias = ToConstTensor(flatBufferInputParams->cellBias()); |
| 2838 | armnn::ConstTensor outputGateBias = ToConstTensor(flatBufferInputParams->outputGateBias()); |
| 2839 | |
| 2840 | qLstmInputParams.m_InputToForgetWeights = &inputToForgetWeights; |
| 2841 | qLstmInputParams.m_InputToCellWeights = &inputToCellWeights; |
| 2842 | qLstmInputParams.m_InputToOutputWeights = &inputToOutputWeights; |
| 2843 | qLstmInputParams.m_RecurrentToForgetWeights = &recurrentToForgetWeights; |
| 2844 | qLstmInputParams.m_RecurrentToCellWeights = &recurrentToCellWeights; |
| 2845 | qLstmInputParams.m_RecurrentToOutputWeights = &recurrentToOutputWeights; |
| 2846 | qLstmInputParams.m_ForgetGateBias = &forgetGateBias; |
| 2847 | qLstmInputParams.m_CellBias = &cellBias; |
| 2848 | qLstmInputParams.m_OutputGateBias = &outputGateBias; |
| 2849 | |
| 2850 | // Optional CIFG params |
| 2851 | armnn::ConstTensor inputToInputWeights; |
| 2852 | armnn::ConstTensor recurrentToInputWeights; |
| 2853 | armnn::ConstTensor inputGateBias; |
| 2854 | |
| 2855 | if (!qLstmDescriptor.m_CifgEnabled) |
| 2856 | { |
| 2857 | inputToInputWeights = ToConstTensor(flatBufferInputParams->inputToInputWeights()); |
| 2858 | recurrentToInputWeights = ToConstTensor(flatBufferInputParams->recurrentToInputWeights()); |
| 2859 | inputGateBias = ToConstTensor(flatBufferInputParams->inputGateBias()); |
| 2860 | |
| 2861 | qLstmInputParams.m_InputToInputWeights = &inputToInputWeights; |
| 2862 | qLstmInputParams.m_RecurrentToInputWeights = &recurrentToInputWeights; |
| 2863 | qLstmInputParams.m_InputGateBias = &inputGateBias; |
| 2864 | } |
| 2865 | |
| 2866 | // Optional projection params |
| 2867 | armnn::ConstTensor projectionWeights; |
| 2868 | armnn::ConstTensor projectionBias; |
| 2869 | |
| 2870 | if (qLstmDescriptor.m_ProjectionEnabled) |
| 2871 | { |
| 2872 | projectionWeights = ToConstTensor(flatBufferInputParams->projectionWeights()); |
| 2873 | projectionBias = ToConstTensor(flatBufferInputParams->projectionBias()); |
| 2874 | |
| 2875 | qLstmInputParams.m_ProjectionWeights = &projectionWeights; |
| 2876 | qLstmInputParams.m_ProjectionBias = &projectionBias; |
| 2877 | } |
| 2878 | |
| 2879 | // Optional peephole params |
| 2880 | armnn::ConstTensor cellToInputWeights; |
| 2881 | armnn::ConstTensor cellToForgetWeights; |
| 2882 | armnn::ConstTensor cellToOutputWeights; |
| 2883 | |
| 2884 | if (qLstmDescriptor.m_PeepholeEnabled) |
| 2885 | { |
| 2886 | if (!qLstmDescriptor.m_CifgEnabled) |
| 2887 | { |
| 2888 | cellToInputWeights = ToConstTensor(flatBufferInputParams->cellToInputWeights()); |
| 2889 | qLstmInputParams.m_CellToInputWeights = &cellToInputWeights; |
| 2890 | } |
| 2891 | |
| 2892 | cellToForgetWeights = ToConstTensor(flatBufferInputParams->cellToForgetWeights()); |
| 2893 | cellToOutputWeights = ToConstTensor(flatBufferInputParams->cellToOutputWeights()); |
| 2894 | |
| 2895 | qLstmInputParams.m_CellToForgetWeights = &cellToForgetWeights; |
| 2896 | qLstmInputParams.m_CellToOutputWeights = &cellToOutputWeights; |
| 2897 | } |
| 2898 | |
| 2899 | // Optional layer norm params |
| 2900 | armnn::ConstTensor inputLayerNormWeights; |
| 2901 | armnn::ConstTensor forgetLayerNormWeights; |
| 2902 | armnn::ConstTensor cellLayerNormWeights; |
| 2903 | armnn::ConstTensor outputLayerNormWeights; |
| 2904 | |
| 2905 | if (qLstmDescriptor.m_LayerNormEnabled) |
| 2906 | { |
| 2907 | if (!qLstmDescriptor.m_CifgEnabled) |
| 2908 | { |
| 2909 | inputLayerNormWeights = ToConstTensor(flatBufferInputParams->inputLayerNormWeights()); |
| 2910 | qLstmInputParams.m_InputLayerNormWeights = &inputLayerNormWeights; |
| 2911 | } |
| 2912 | |
| 2913 | forgetLayerNormWeights = ToConstTensor(flatBufferInputParams->forgetLayerNormWeights()); |
| 2914 | cellLayerNormWeights = ToConstTensor(flatBufferInputParams->cellLayerNormWeights()); |
| 2915 | outputLayerNormWeights = ToConstTensor(flatBufferInputParams->outputLayerNormWeights()); |
| 2916 | |
| 2917 | qLstmInputParams.m_ForgetLayerNormWeights = &forgetLayerNormWeights; |
| 2918 | qLstmInputParams.m_CellLayerNormWeights = &cellLayerNormWeights; |
| 2919 | qLstmInputParams.m_OutputLayerNormWeights = &outputLayerNormWeights; |
| 2920 | } |
| 2921 | |
| 2922 | IConnectableLayer* layer = m_Network->AddQLstmLayer(qLstmDescriptor, qLstmInputParams, layerName.c_str()); |
| 2923 | |
| 2924 | armnn::TensorInfo outputStateOutInfo = ToTensorInfo(outputs[0]); |
| 2925 | layer->GetOutputSlot(0).SetTensorInfo(outputStateOutInfo); |
| 2926 | |
| 2927 | armnn::TensorInfo cellStateOutInfo = ToTensorInfo(outputs[1]); |
| 2928 | layer->GetOutputSlot(1).SetTensorInfo(cellStateOutInfo); |
| 2929 | |
| 2930 | armnn::TensorInfo outputInfo = ToTensorInfo(outputs[2]); |
| 2931 | layer->GetOutputSlot(2).SetTensorInfo(outputInfo); |
| 2932 | |
| 2933 | RegisterInputSlots(graph, layerIndex, layer); |
| 2934 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2935 | } |
| 2936 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2937 | void IDeserializer::DeserializerImpl::ParseQuantizedLstm(GraphPtr graph, unsigned int layerIndex) |
Jan Eilers | 5b01a89 | 2019-07-23 09:47:43 +0100 | [diff] [blame] | 2938 | { |
| 2939 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2940 | |
| 2941 | auto inputs = GetInputs(graph, layerIndex); |
| 2942 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 2943 | |
| 2944 | auto outputs = GetOutputs(graph, layerIndex); |
| 2945 | CHECK_VALID_SIZE(outputs.size(), 2); |
| 2946 | |
| 2947 | auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer(); |
| 2948 | auto layerName = GetLayerName(graph, layerIndex); |
| 2949 | auto flatBufferInputParams = flatBufferLayer->inputParams(); |
| 2950 | |
| 2951 | armnn::QuantizedLstmInputParams lstmInputParams; |
| 2952 | |
| 2953 | armnn::ConstTensor inputToInputWeights = ToConstTensor(flatBufferInputParams->inputToInputWeights()); |
| 2954 | armnn::ConstTensor inputToForgetWeights = ToConstTensor(flatBufferInputParams->inputToForgetWeights()); |
| 2955 | armnn::ConstTensor inputToCellWeights = ToConstTensor(flatBufferInputParams->inputToCellWeights()); |
| 2956 | armnn::ConstTensor inputToOutputWeights = ToConstTensor(flatBufferInputParams->inputToOutputWeights()); |
| 2957 | armnn::ConstTensor recurrentToInputWeights = ToConstTensor(flatBufferInputParams->recurrentToInputWeights()); |
| 2958 | armnn::ConstTensor recurrentToForgetWeights = ToConstTensor(flatBufferInputParams->recurrentToForgetWeights()); |
| 2959 | armnn::ConstTensor recurrentToCellWeights = ToConstTensor(flatBufferInputParams->recurrentToCellWeights()); |
| 2960 | armnn::ConstTensor recurrentToOutputWeights = ToConstTensor(flatBufferInputParams->recurrentToOutputWeights()); |
| 2961 | armnn::ConstTensor inputGateBias = ToConstTensor(flatBufferInputParams->inputGateBias()); |
| 2962 | armnn::ConstTensor forgetGateBias = ToConstTensor(flatBufferInputParams->forgetGateBias()); |
| 2963 | armnn::ConstTensor cellBias = ToConstTensor(flatBufferInputParams->cellBias()); |
| 2964 | armnn::ConstTensor outputGateBias = ToConstTensor(flatBufferInputParams->outputGateBias()); |
| 2965 | |
| 2966 | lstmInputParams.m_InputToInputWeights = &inputToInputWeights; |
| 2967 | lstmInputParams.m_InputToForgetWeights = &inputToForgetWeights; |
| 2968 | lstmInputParams.m_InputToCellWeights = &inputToCellWeights; |
| 2969 | lstmInputParams.m_InputToOutputWeights = &inputToOutputWeights; |
| 2970 | lstmInputParams.m_RecurrentToInputWeights = &recurrentToInputWeights; |
| 2971 | lstmInputParams.m_RecurrentToForgetWeights = &recurrentToForgetWeights; |
| 2972 | lstmInputParams.m_RecurrentToCellWeights = &recurrentToCellWeights; |
| 2973 | lstmInputParams.m_RecurrentToOutputWeights = &recurrentToOutputWeights; |
| 2974 | lstmInputParams.m_InputGateBias = &inputGateBias; |
| 2975 | lstmInputParams.m_ForgetGateBias = &forgetGateBias; |
| 2976 | lstmInputParams.m_CellBias = &cellBias; |
| 2977 | lstmInputParams.m_OutputGateBias = &outputGateBias; |
| 2978 | |
| 2979 | IConnectableLayer* layer = m_Network->AddQuantizedLstmLayer(lstmInputParams, layerName.c_str()); |
| 2980 | |
| 2981 | armnn::TensorInfo outputTensorInfo1 = ToTensorInfo(outputs[0]); |
| 2982 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo1); |
| 2983 | |
| 2984 | armnn::TensorInfo outputTensorInfo2 = ToTensorInfo(outputs[1]); |
| 2985 | layer->GetOutputSlot(1).SetTensorInfo(outputTensorInfo2); |
| 2986 | |
| 2987 | RegisterInputSlots(graph, layerIndex, layer); |
| 2988 | RegisterOutputSlots(graph, layerIndex, layer); |
| 2989 | } |
| 2990 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2991 | void IDeserializer::DeserializerImpl::ParseDequantize(GraphPtr graph, unsigned int layerIndex) |
Nattapat Chaimanowong | e4294fd | 2019-03-28 09:56:53 +0000 | [diff] [blame] | 2992 | { |
| 2993 | CHECK_LAYERS(graph, 0, layerIndex); |
| 2994 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2995 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Nattapat Chaimanowong | e4294fd | 2019-03-28 09:56:53 +0000 | [diff] [blame] | 2996 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2997 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 2998 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Nattapat Chaimanowong | e4294fd | 2019-03-28 09:56:53 +0000 | [diff] [blame] | 2999 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3000 | |
| 3001 | const std::string layerName = GetLayerName(graph, layerIndex); |
| 3002 | IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str()); |
| 3003 | |
| 3004 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 3005 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 3006 | |
| 3007 | RegisterInputSlots(graph, layerIndex, layer); |
| 3008 | RegisterOutputSlots(graph, layerIndex, layer); |
| 3009 | } |
| 3010 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 3011 | void IDeserializer::DeserializerImpl::ParseMerge(GraphPtr graph, unsigned int layerIndex) |
Nattapat Chaimanowong | 1f88630 | 2019-04-05 13:37:19 +0100 | [diff] [blame] | 3012 | { |
| 3013 | CHECK_LAYERS(graph, 0, layerIndex); |
| 3014 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 3015 | TensorRawPtrVector inputs = GetInputs(graph, layerIndex); |
Nattapat Chaimanowong | 1f88630 | 2019-04-05 13:37:19 +0100 | [diff] [blame] | 3016 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 3017 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 3018 | TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); |
Nattapat Chaimanowong | 1f88630 | 2019-04-05 13:37:19 +0100 | [diff] [blame] | 3019 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3020 | |
| 3021 | const std::string layerName = GetLayerName(graph, layerIndex); |
| 3022 | IConnectableLayer* layer = m_Network->AddMergeLayer(layerName.c_str()); |
| 3023 | |
| 3024 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 3025 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 3026 | |
| 3027 | RegisterInputSlots(graph, layerIndex, layer); |
| 3028 | RegisterOutputSlots(graph, layerIndex, layer); |
| 3029 | } |
| 3030 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 3031 | void IDeserializer::DeserializerImpl::ParseSwitch(GraphPtr graph, unsigned int layerIndex) |
Sadik Armagan | eff363d | 2019-04-05 15:25:46 +0100 | [diff] [blame] | 3032 | { |
| 3033 | CHECK_LAYERS(graph, 0, layerIndex); |
| 3034 | auto inputs = GetInputs(graph, layerIndex); |
| 3035 | CHECK_LOCATION(); |
| 3036 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 3037 | |
| 3038 | auto outputs = GetOutputs(graph, layerIndex); |
| 3039 | CHECK_VALID_SIZE(outputs.size(), 2); |
| 3040 | |
| 3041 | auto layerName = GetLayerName(graph, layerIndex); |
| 3042 | IConnectableLayer* layer = m_Network->AddSwitchLayer(layerName.c_str()); |
| 3043 | |
| 3044 | armnn::TensorInfo output0TensorInfo = ToTensorInfo(outputs[0]); |
| 3045 | layer->GetOutputSlot(0).SetTensorInfo(output0TensorInfo); |
| 3046 | |
| 3047 | armnn::TensorInfo output1TensorInfo = ToTensorInfo(outputs[1]); |
| 3048 | layer->GetOutputSlot(1).SetTensorInfo(output1TensorInfo); |
| 3049 | |
| 3050 | RegisterInputSlots(graph, layerIndex, layer); |
| 3051 | RegisterOutputSlots(graph, layerIndex, layer); |
| 3052 | } |
| 3053 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 3054 | void IDeserializer::DeserializerImpl::ParsePrelu(GraphPtr graph, unsigned int layerIndex) |
Ellen Norris-Thompson | 5198247 | 2019-06-19 11:46:21 +0100 | [diff] [blame] | 3055 | { |
| 3056 | CHECK_LAYERS(graph, 0, layerIndex); |
| 3057 | auto inputs = GetInputs(graph, layerIndex); |
| 3058 | CHECK_LOCATION(); |
| 3059 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 3060 | |
| 3061 | auto outputs = GetOutputs(graph, layerIndex); |
| 3062 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3063 | |
| 3064 | auto layerName = GetLayerName(graph, layerIndex); |
| 3065 | IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str()); |
| 3066 | |
| 3067 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 3068 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 3069 | |
| 3070 | RegisterInputSlots(graph, layerIndex, layer); |
| 3071 | RegisterOutputSlots(graph, layerIndex, layer); |
| 3072 | } |
| 3073 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 3074 | void IDeserializer::DeserializerImpl::ParseTranspose(GraphPtr graph, unsigned int layerIndex) |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 3075 | { |
| 3076 | CHECK_LAYERS(graph, 0, layerIndex); |
| 3077 | |
| 3078 | auto dimsMapping = graph->layers()->Get(layerIndex)->layer_as_TransposeLayer()->descriptor()->dimMappings(); |
| 3079 | |
| 3080 | auto inputs = GetInputs(graph, layerIndex); |
| 3081 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 3082 | |
| 3083 | auto outputs = GetOutputs(graph, layerIndex); |
| 3084 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3085 | auto outputInfo = ToTensorInfo(outputs[0]); |
| 3086 | |
| 3087 | auto layerName = GetLayerName(graph, layerIndex); |
| 3088 | const armnn::TransposeDescriptor descriptor(armnn::PermutationVector(dimsMapping->data(), dimsMapping->Length())); |
| 3089 | |
| 3090 | IConnectableLayer* layer = m_Network->AddTransposeLayer(descriptor, layerName.c_str()); |
| 3091 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 3092 | |
| 3093 | RegisterInputSlots(graph, layerIndex, layer); |
| 3094 | RegisterOutputSlots(graph, layerIndex, layer); |
| 3095 | } |
| 3096 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 3097 | void IDeserializer::DeserializerImpl::ParseTransposeConvolution2d(GraphPtr graph, unsigned int layerIndex) |
Aron Virginas-Tar | cb54930 | 2019-06-21 13:53:38 +0100 | [diff] [blame] | 3098 | { |
| 3099 | CHECK_LAYERS(graph, 0, layerIndex); |
| 3100 | |
| 3101 | auto inputs = GetInputs(graph, layerIndex); |
| 3102 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 3103 | |
| 3104 | auto outputs = GetOutputs(graph, layerIndex); |
| 3105 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3106 | |
| 3107 | auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer(); |
| 3108 | auto layerName = GetLayerName(graph, layerIndex); |
| 3109 | auto serializerDescriptor = serializerLayer->descriptor(); |
| 3110 | |
| 3111 | armnn::TransposeConvolution2dDescriptor descriptor; |
| 3112 | descriptor.m_PadLeft = serializerDescriptor->padLeft(); |
| 3113 | descriptor.m_PadRight = serializerDescriptor->padRight(); |
| 3114 | descriptor.m_PadTop = serializerDescriptor->padTop(); |
| 3115 | descriptor.m_PadBottom = serializerDescriptor->padBottom(); |
| 3116 | descriptor.m_StrideX = serializerDescriptor->strideX(); |
| 3117 | descriptor.m_StrideY = serializerDescriptor->strideY();; |
| 3118 | descriptor.m_BiasEnabled = serializerDescriptor->biasEnabled();; |
| 3119 | descriptor.m_DataLayout = ToDataLayout(serializerDescriptor->dataLayout()); |
| 3120 | |
| 3121 | // weights & biases |
| 3122 | armnn::ConstTensor weights = ToConstTensor(serializerLayer->weights()); |
| 3123 | armnn::Optional<armnn::ConstTensor> optionalBiases; |
| 3124 | if (descriptor.m_BiasEnabled) |
| 3125 | { |
| 3126 | armnn::ConstTensor biases = ToConstTensor(serializerLayer->biases()); |
| 3127 | optionalBiases = armnn::MakeOptional<armnn::ConstTensor>(biases); |
| 3128 | } |
| 3129 | |
| 3130 | IConnectableLayer* layer = m_Network->AddTransposeConvolution2dLayer(descriptor, |
| 3131 | weights, |
| 3132 | optionalBiases, |
| 3133 | layerName.c_str()); |
| 3134 | |
| 3135 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 3136 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 3137 | |
| 3138 | RegisterInputSlots(graph, layerIndex, layer); |
| 3139 | RegisterOutputSlots(graph, layerIndex, layer); |
| 3140 | } |
| 3141 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 3142 | void IDeserializer::DeserializerImpl::ParseStack(GraphPtr graph, unsigned int layerIndex) |
Matthew Jackson | b5433ee | 2019-07-11 15:54:20 +0100 | [diff] [blame] | 3143 | { |
| 3144 | CHECK_LAYERS(graph, 0, layerIndex); |
| 3145 | auto inputs = GetInputs(graph, layerIndex); |
| 3146 | |
| 3147 | auto outputs = GetOutputs(graph, layerIndex); |
| 3148 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3149 | |
| 3150 | auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StackLayer()->descriptor(); |
| 3151 | unsigned int axis = flatBufferDescriptor->axis(); |
| 3152 | unsigned int numInputs = flatBufferDescriptor->numInputs(); |
| 3153 | CHECK_VALID_SIZE(inputs.size(), numInputs); |
| 3154 | |
| 3155 | auto flatBufferInputShape = flatBufferDescriptor->inputShape(); |
| 3156 | std::vector<uint32_t> vectorInputShape(flatBufferInputShape->begin(), |
| 3157 | flatBufferInputShape->begin() + flatBufferInputShape->size()); |
| 3158 | |
| 3159 | TensorShape inputShape(static_cast<unsigned int>(vectorInputShape.size()), vectorInputShape.data()); |
| 3160 | armnn::StackDescriptor descriptor(axis, numInputs, inputShape); |
| 3161 | |
| 3162 | for (unsigned int i=0; i<inputs.size(); ++i) |
| 3163 | { |
Matthew Bentham | 75ae2b0 | 2019-09-19 12:04:13 +0100 | [diff] [blame] | 3164 | armnn::TensorShape inputShape = ToTensorInfo(inputs[i]).GetShape(); |
Matthew Jackson | b5433ee | 2019-07-11 15:54:20 +0100 | [diff] [blame] | 3165 | if (descriptor.m_InputShape != inputShape) |
| 3166 | { |
| 3167 | std::stringstream ss; |
| 3168 | ss << "Shape of input " |
| 3169 | << i |
| 3170 | << " " |
| 3171 | << inputShape |
| 3172 | << " does not equal defined input shape " |
| 3173 | << descriptor.m_InputShape |
| 3174 | << ": " |
| 3175 | << CHECK_LOCATION().AsString(); |
| 3176 | throw ParseException(ss.str()); |
| 3177 | } |
| 3178 | } |
| 3179 | |
| 3180 | auto layerName = GetLayerName(graph, layerIndex); |
| 3181 | IConnectableLayer* layer = m_Network->AddStackLayer(descriptor, layerName.c_str()); |
| 3182 | |
| 3183 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 3184 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 3185 | |
| 3186 | RegisterInputSlots(graph, layerIndex, layer); |
| 3187 | RegisterOutputSlots(graph, layerIndex, layer); |
| 3188 | } |
| 3189 | |
Finn Williams | 85d3671 | 2021-01-26 22:30:06 +0000 | [diff] [blame] | 3190 | void IDeserializer::DeserializerImpl::ParseStandIn(GraphPtr graph, unsigned int layerIndex) |
Aron Virginas-Tar | 85121a2 | 2019-10-23 10:41:35 +0100 | [diff] [blame] | 3191 | { |
| 3192 | CHECK_LAYERS(graph, 0, layerIndex); |
| 3193 | |
| 3194 | auto inputs = GetInputs(graph, layerIndex); |
| 3195 | auto outputs = GetOutputs(graph, layerIndex); |
| 3196 | |
| 3197 | auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_StandInLayer(); |
| 3198 | auto fbDescriptor = fbLayer->descriptor(); |
| 3199 | |
| 3200 | armnn::StandInDescriptor descriptor; |
| 3201 | descriptor.m_NumInputs = fbDescriptor->numInputs(); |
| 3202 | descriptor.m_NumOutputs = fbDescriptor->numOutputs(); |
| 3203 | |
| 3204 | CHECK_VALID_SIZE(inputs.size(), descriptor.m_NumInputs); |
| 3205 | CHECK_VALID_SIZE(outputs.size(), descriptor.m_NumOutputs); |
| 3206 | |
| 3207 | const std::string layerName = GetLayerName(graph, layerIndex); |
| 3208 | armnn::IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str()); |
| 3209 | |
| 3210 | for (unsigned int i = 0u; i < descriptor.m_NumOutputs; ++i) |
| 3211 | { |
| 3212 | armnn::TensorInfo outputInfo = ToTensorInfo(outputs[i]); |
| 3213 | layer->GetOutputSlot(i).SetTensorInfo(outputInfo); |
| 3214 | } |
| 3215 | |
| 3216 | RegisterInputSlots(graph, layerIndex, layer); |
| 3217 | RegisterOutputSlots(graph, layerIndex, layer); |
| 3218 | } |
| 3219 | |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 3220 | } // namespace armnnDeserializer |