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