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