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