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