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