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