Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "Serializer.hpp" |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 7 | |
| 8 | #include "SerializerUtils.hpp" |
| 9 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 10 | #include <armnn/ArmNN.hpp> |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 11 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 12 | #include <iostream> |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 13 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 14 | #include <Schema_generated.h> |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 15 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 16 | #include <flatbuffers/util.h> |
| 17 | |
| 18 | using namespace armnn; |
| 19 | namespace fb = flatbuffers; |
| 20 | namespace serializer = armnn::armnnSerializer; |
| 21 | |
| 22 | namespace armnnSerializer |
| 23 | { |
| 24 | |
Saoirse Stewart | cb8a321 | 2019-02-14 15:46:10 +0000 | [diff] [blame] | 25 | uint32_t SerializerVisitor::GetSerializedId(unsigned int guid) |
| 26 | { |
| 27 | std::pair<unsigned int, uint32_t> guidPair(guid, m_layerId); |
| 28 | |
| 29 | if (m_guidMap.empty()) |
| 30 | { |
| 31 | m_guidMap.insert(guidPair); |
| 32 | } |
| 33 | else if (m_guidMap.find(guid) == m_guidMap.end()) |
| 34 | { |
| 35 | guidPair.second = ++m_layerId; |
| 36 | m_guidMap.insert(guidPair); |
| 37 | return m_layerId; |
| 38 | } |
Saoirse Stewart | 3021104 | 2019-02-18 17:19:16 +0000 | [diff] [blame] | 39 | return m_guidMap[guid]; |
Saoirse Stewart | cb8a321 | 2019-02-14 15:46:10 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 42 | // Build FlatBuffer for Input Layer |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 43 | void SerializerVisitor::VisitInputLayer(const IConnectableLayer* layer, LayerBindingId id, const char* name) |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 44 | { |
| 45 | // Create FlatBuffer BaseLayer |
| 46 | auto flatBufferInputBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Input); |
| 47 | |
| 48 | // Create FlatBuffer BindableBaseLayer |
| 49 | auto flatBufferInputBindableBaseLayer = serializer::CreateBindableLayerBase(m_flatBufferBuilder, |
| 50 | flatBufferInputBaseLayer, |
| 51 | id); |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 52 | // Push layer Guid to outputIds. |
Saoirse Stewart | cb8a321 | 2019-02-14 15:46:10 +0000 | [diff] [blame] | 53 | m_inputIds.push_back(GetSerializedId(layer->GetGuid())); |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 54 | |
| 55 | // Create the FlatBuffer InputLayer |
| 56 | auto flatBufferInputLayer = serializer::CreateInputLayer(m_flatBufferBuilder, flatBufferInputBindableBaseLayer); |
| 57 | |
| 58 | // Add the AnyLayer to the FlatBufferLayers |
| 59 | CreateAnyLayer(flatBufferInputLayer.o, serializer::Layer::Layer_InputLayer); |
| 60 | } |
| 61 | |
| 62 | // Build FlatBuffer for Output Layer |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 63 | void SerializerVisitor::VisitOutputLayer(const IConnectableLayer* layer, LayerBindingId id, const char* name) |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 64 | { |
| 65 | // Create FlatBuffer BaseLayer |
| 66 | auto flatBufferOutputBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Output); |
| 67 | |
| 68 | // Create FlatBuffer BindableBaseLayer |
| 69 | auto flatBufferOutputBindableBaseLayer = serializer::CreateBindableLayerBase(m_flatBufferBuilder, |
| 70 | flatBufferOutputBaseLayer, |
| 71 | id); |
| 72 | // Push layer Guid to outputIds. |
Saoirse Stewart | cb8a321 | 2019-02-14 15:46:10 +0000 | [diff] [blame] | 73 | m_outputIds.push_back(GetSerializedId(layer->GetGuid())); |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 74 | |
| 75 | // Create the FlatBuffer OutputLayer |
| 76 | auto flatBufferOutputLayer = serializer::CreateOutputLayer(m_flatBufferBuilder, flatBufferOutputBindableBaseLayer); |
| 77 | // Add the AnyLayer to the FlatBufferLayers |
| 78 | CreateAnyLayer(flatBufferOutputLayer.o, serializer::Layer::Layer_OutputLayer); |
| 79 | } |
| 80 | |
| 81 | // Build FlatBuffer for Addition Layer |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 82 | void SerializerVisitor::VisitAdditionLayer(const IConnectableLayer* layer, const char* name) |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 83 | { |
| 84 | // Create FlatBuffer BaseLayer |
| 85 | auto flatBufferAdditionBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Addition); |
| 86 | |
| 87 | // Create the FlatBuffer AdditionLayer |
| 88 | auto flatBufferAdditionLayer = serializer::CreateAdditionLayer(m_flatBufferBuilder, flatBufferAdditionBaseLayer); |
| 89 | |
| 90 | // Add the AnyLayer to the FlatBufferLayers |
| 91 | CreateAnyLayer(flatBufferAdditionLayer.o, serializer::Layer::Layer_AdditionLayer); |
| 92 | } |
| 93 | |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 94 | // Build FlatBuffer for Convolution2dLayer |
| 95 | void SerializerVisitor::VisitConvolution2dLayer(const IConnectableLayer* layer, |
| 96 | const Convolution2dDescriptor& descriptor, |
| 97 | const ConstTensor& weights, |
| 98 | const Optional<ConstTensor>& biases, |
| 99 | const char* name) |
| 100 | { |
| 101 | // Create FlatBuffer BaseLayer |
| 102 | auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Convolution2d); |
| 103 | |
| 104 | auto flatBufferDescriptor = CreateConvolution2dDescriptor(m_flatBufferBuilder, |
| 105 | descriptor.m_PadLeft, |
| 106 | descriptor.m_PadRight, |
| 107 | descriptor.m_PadTop, |
| 108 | descriptor.m_PadBottom, |
| 109 | descriptor.m_StrideX, |
| 110 | descriptor.m_StrideY, |
| 111 | descriptor.m_BiasEnabled, |
| 112 | GetFlatBufferDataLayout(descriptor.m_DataLayout)); |
| 113 | auto flatBufferWeightsConstTensorInfo = CreateConstTensorInfo(weights); |
| 114 | flatbuffers::Offset<serializer::ConstTensor> flatBufferBiasesConstTensorInfo; |
| 115 | |
| 116 | if (biases.has_value()) |
| 117 | { |
| 118 | flatBufferBiasesConstTensorInfo = CreateConstTensorInfo(biases.value()); |
| 119 | } |
| 120 | |
| 121 | // Create the FlatBuffer Convolution2dLayer |
| 122 | auto flatBufferLayer = CreateConvolution2dLayer(m_flatBufferBuilder, |
| 123 | flatBufferBaseLayer, |
| 124 | flatBufferDescriptor, |
| 125 | flatBufferWeightsConstTensorInfo, |
| 126 | flatBufferBiasesConstTensorInfo); |
| 127 | |
| 128 | // Add the AnyLayer to the FlatBufferLayers |
| 129 | CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_Convolution2dLayer); |
| 130 | } |
| 131 | |
Aron Virginas-Tar | c04125f | 2019-02-19 16:31:08 +0000 | [diff] [blame^] | 132 | void SerializerVisitor::VisitDepthwiseConvolution2dLayer(const IConnectableLayer* layer, |
| 133 | const DepthwiseConvolution2dDescriptor& descriptor, |
| 134 | const ConstTensor& weights, |
| 135 | const Optional<ConstTensor>& biases, |
| 136 | const char* name) |
| 137 | { |
| 138 | auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_DepthwiseConvolution2d); |
| 139 | auto fbDescriptor = CreateDepthwiseConvolution2dDescriptor(m_flatBufferBuilder, |
| 140 | descriptor.m_PadLeft, |
| 141 | descriptor.m_PadRight, |
| 142 | descriptor.m_PadTop, |
| 143 | descriptor.m_PadBottom, |
| 144 | descriptor.m_StrideX, |
| 145 | descriptor.m_StrideY, |
| 146 | descriptor.m_BiasEnabled, |
| 147 | GetFlatBufferDataLayout(descriptor.m_DataLayout)); |
| 148 | |
| 149 | flatbuffers::Offset<serializer::ConstTensor> fbWeightsConstTensorInfo = CreateConstTensorInfo(weights); |
| 150 | flatbuffers::Offset<serializer::ConstTensor> fbBiasesConstTensorInfo; |
| 151 | if (biases.has_value()) |
| 152 | { |
| 153 | fbBiasesConstTensorInfo = CreateConstTensorInfo(biases.value()); |
| 154 | } |
| 155 | |
| 156 | auto flatBufferLayer = CreateDepthwiseConvolution2dLayer(m_flatBufferBuilder, |
| 157 | fbBaseLayer, |
| 158 | fbDescriptor, |
| 159 | fbWeightsConstTensorInfo, |
| 160 | fbBiasesConstTensorInfo); |
| 161 | |
| 162 | CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_DepthwiseConvolution2dLayer); |
| 163 | } |
| 164 | |
Sadik Armagan | 5f45027 | 2019-02-12 14:31:45 +0000 | [diff] [blame] | 165 | // Build FlatBuffer for Multiplication Layer |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 166 | void SerializerVisitor::VisitMultiplicationLayer(const IConnectableLayer* layer, const char* name) |
Sadik Armagan | 5f45027 | 2019-02-12 14:31:45 +0000 | [diff] [blame] | 167 | { |
| 168 | // Create FlatBuffer BaseLayer |
| 169 | auto flatBufferMultiplicationBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Multiplication); |
| 170 | |
| 171 | // Create the FlatBuffer MultiplicationLayer |
| 172 | auto flatBufferMultiplicationLayer = |
| 173 | serializer::CreateMultiplicationLayer(m_flatBufferBuilder, flatBufferMultiplicationBaseLayer); |
| 174 | |
| 175 | // Add the AnyLayer to the FlatBufferLayers |
| 176 | CreateAnyLayer(flatBufferMultiplicationLayer.o, serializer::Layer::Layer_MultiplicationLayer); |
| 177 | } |
| 178 | |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 179 | // Build FlatBuffer for Reshape Layer |
| 180 | void SerializerVisitor::VisitReshapeLayer(const IConnectableLayer* layer, |
| 181 | const armnn::ReshapeDescriptor& reshapeDescriptor, |
| 182 | const char* name) |
| 183 | { |
| 184 | // Create FlatBuffer BaseLayer |
| 185 | auto flatBufferReshapeBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Reshape); |
| 186 | |
| 187 | std::vector<unsigned int> targetShape; |
| 188 | for (unsigned int i =0; i < reshapeDescriptor.m_TargetShape.GetNumDimensions(); i++) |
| 189 | { |
| 190 | targetShape.push_back(reshapeDescriptor.m_TargetShape[i]); |
| 191 | } |
| 192 | |
| 193 | auto flatBufferReshapeDesc = serializer::CreateReshapeDescriptor(m_flatBufferBuilder, |
| 194 | m_flatBufferBuilder.CreateVector(targetShape)); |
| 195 | |
| 196 | // Create the FlatBuffer ReshapeLayer |
| 197 | auto flatBufferReshapeLayer = serializer::CreateReshapeLayer(m_flatBufferBuilder, flatBufferReshapeBaseLayer, |
| 198 | flatBufferReshapeDesc); |
| 199 | |
| 200 | // Add the AnyLayer to the FlatBufferLayers |
| 201 | CreateAnyLayer(flatBufferReshapeLayer.o, serializer::Layer::Layer_ReshapeLayer); |
| 202 | } |
| 203 | |
Aron Virginas-Tar | fc413c0 | 2019-02-13 15:41:52 +0000 | [diff] [blame] | 204 | // Build FlatBuffer for Softmax Layer |
| 205 | void SerializerVisitor::VisitSoftmaxLayer(const IConnectableLayer* layer, |
| 206 | const SoftmaxDescriptor& softmaxDescriptor, |
| 207 | const char* name) |
| 208 | { |
| 209 | // Create FlatBuffer BaseLayer |
| 210 | auto flatBufferSoftmaxBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Softmax); |
| 211 | |
| 212 | // Create the FlatBuffer SoftmaxDescriptor |
| 213 | auto flatBufferSoftmaxDesc = |
| 214 | serializer::CreateSoftmaxDescriptor(m_flatBufferBuilder, softmaxDescriptor.m_Beta); |
| 215 | |
| 216 | // Create the FlatBuffer SoftmaxLayer |
| 217 | auto flatBufferSoftmaxLayer = |
| 218 | serializer::CreateSoftmaxLayer(m_flatBufferBuilder, |
| 219 | flatBufferSoftmaxBaseLayer, |
| 220 | flatBufferSoftmaxDesc); |
| 221 | |
| 222 | CreateAnyLayer(flatBufferSoftmaxLayer.o, serializer::Layer::Layer_SoftmaxLayer); |
| 223 | } |
| 224 | |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 225 | void SerializerVisitor::VisitPooling2dLayer(const IConnectableLayer* layer, |
| 226 | const Pooling2dDescriptor& pooling2dDescriptor, |
| 227 | const char* name) |
| 228 | { |
| 229 | auto fbPooling2dBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Pooling2d); |
| 230 | auto fbPooling2dDescriptor = serializer::CreatePooling2dDescriptor( |
| 231 | m_flatBufferBuilder, |
| 232 | GetFlatBufferPoolingAlgorithm(pooling2dDescriptor.m_PoolType), |
| 233 | pooling2dDescriptor.m_PadLeft, |
| 234 | pooling2dDescriptor.m_PadRight, |
| 235 | pooling2dDescriptor.m_PadTop, |
| 236 | pooling2dDescriptor.m_PadBottom, |
| 237 | pooling2dDescriptor.m_PoolWidth, |
| 238 | pooling2dDescriptor.m_PoolHeight, |
| 239 | pooling2dDescriptor.m_StrideX, |
| 240 | pooling2dDescriptor.m_StrideY, |
| 241 | GetFlatBufferOutputShapeRounding(pooling2dDescriptor.m_OutputShapeRounding), |
| 242 | GetFlatBufferPaddingMethod(pooling2dDescriptor.m_PaddingMethod), |
| 243 | GetFlatBufferDataLayout(pooling2dDescriptor.m_DataLayout)); |
| 244 | |
| 245 | auto fbPooling2dLayer = serializer::CreatePooling2dLayer(m_flatBufferBuilder, |
| 246 | fbPooling2dBaseLayer, |
| 247 | fbPooling2dDescriptor); |
| 248 | |
| 249 | CreateAnyLayer(fbPooling2dLayer.o, serializer::Layer::Layer_Pooling2dLayer); |
| 250 | } |
| 251 | |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 252 | fb::Offset<serializer::LayerBase> SerializerVisitor::CreateLayerBase(const IConnectableLayer* layer, |
| 253 | const serializer::LayerType layerType) |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 254 | { |
| 255 | std::vector<fb::Offset<serializer::InputSlot>> inputSlots = CreateInputSlots(layer); |
| 256 | std::vector<fb::Offset<serializer::OutputSlot>> outputSlots = CreateOutputSlots(layer); |
| 257 | |
| 258 | return serializer::CreateLayerBase(m_flatBufferBuilder, |
Saoirse Stewart | cb8a321 | 2019-02-14 15:46:10 +0000 | [diff] [blame] | 259 | GetSerializedId(layer->GetGuid()), |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 260 | m_flatBufferBuilder.CreateString(layer->GetName()), |
| 261 | layerType, |
| 262 | m_flatBufferBuilder.CreateVector(inputSlots), |
| 263 | m_flatBufferBuilder.CreateVector(outputSlots)); |
| 264 | } |
| 265 | |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 266 | void SerializerVisitor::CreateAnyLayer(const flatbuffers::Offset<void>& layer, const serializer::Layer serializerLayer) |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 267 | { |
| 268 | auto anyLayer = armnn::armnnSerializer::CreateAnyLayer(m_flatBufferBuilder, |
| 269 | serializerLayer, |
| 270 | layer); |
| 271 | m_serializedLayers.push_back(anyLayer); |
| 272 | } |
| 273 | |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 274 | template <typename T> |
| 275 | flatbuffers::Offset<flatbuffers::Vector<T>> SerializerVisitor::CreateDataVector(const void* memory, unsigned int size) |
| 276 | { |
| 277 | const T* buffer = reinterpret_cast<const T*>(memory); |
| 278 | std::vector<T> vector(buffer, buffer + (size / sizeof(T))); |
| 279 | auto fbVector = m_flatBufferBuilder.CreateVector(vector); |
| 280 | return fbVector; |
| 281 | } |
| 282 | |
| 283 | flatbuffers::Offset<serializer::ConstTensor> SerializerVisitor::CreateConstTensorInfo(const ConstTensor& constTensor) |
| 284 | { |
| 285 | TensorInfo tensorInfo = constTensor.GetInfo(); |
| 286 | |
| 287 | // Get the dimensions |
| 288 | std::vector<unsigned int> shape; |
| 289 | |
| 290 | for(unsigned int dim = 0; dim < tensorInfo.GetShape().GetNumDimensions(); ++dim) |
| 291 | { |
| 292 | shape.push_back(tensorInfo.GetShape()[dim]); |
| 293 | } |
| 294 | |
| 295 | // Create FlatBuffer TensorInfo |
| 296 | auto flatBufferTensorInfo = serializer::CreateTensorInfo(m_flatBufferBuilder, |
| 297 | m_flatBufferBuilder.CreateVector(shape), |
| 298 | GetFlatBufferDataType(tensorInfo.GetDataType()), |
| 299 | tensorInfo.GetQuantizationScale(), |
| 300 | tensorInfo.GetQuantizationOffset()); |
| 301 | flatbuffers::Offset<void> fbPayload; |
| 302 | |
| 303 | switch (tensorInfo.GetDataType()) |
| 304 | { |
| 305 | case DataType::Float32: |
| 306 | case DataType::Signed32: |
| 307 | { |
| 308 | auto fbVector = CreateDataVector<int32_t>(constTensor.GetMemoryArea(), constTensor.GetNumBytes()); |
| 309 | flatbuffers::Offset<serializer::IntData> flatBuffersData = serializer::CreateIntData( |
| 310 | m_flatBufferBuilder, |
| 311 | fbVector); |
| 312 | fbPayload = flatBuffersData.o; |
| 313 | break; |
| 314 | } |
| 315 | case DataType::Float16: |
| 316 | { |
| 317 | auto fbVector = CreateDataVector<int16_t>(constTensor.GetMemoryArea(), constTensor.GetNumBytes()); |
| 318 | flatbuffers::Offset<serializer::ShortData> flatBuffersData = serializer::CreateShortData( |
| 319 | m_flatBufferBuilder, |
| 320 | fbVector); |
| 321 | fbPayload = flatBuffersData.o; |
| 322 | break; |
| 323 | } |
| 324 | case DataType::QuantisedAsymm8: |
| 325 | case DataType::Boolean: |
| 326 | default: |
| 327 | { |
| 328 | auto fbVector = CreateDataVector<int8_t>(constTensor.GetMemoryArea(), constTensor.GetNumBytes()); |
| 329 | flatbuffers::Offset<serializer::ByteData> flatBuffersData = serializer::CreateByteData( |
| 330 | m_flatBufferBuilder, |
| 331 | fbVector); |
| 332 | fbPayload = flatBuffersData.o; |
| 333 | } |
| 334 | } |
| 335 | flatbuffers::Offset<serializer::ConstTensor> flatBufferConstTensor = serializer::CreateConstTensor( |
| 336 | m_flatBufferBuilder, |
| 337 | flatBufferTensorInfo, |
| 338 | GetFlatBufferConstTensorData(tensorInfo.GetDataType()), |
| 339 | fbPayload); |
| 340 | return flatBufferConstTensor; |
| 341 | } |
| 342 | |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 343 | std::vector<fb::Offset<serializer::InputSlot>> SerializerVisitor::CreateInputSlots(const IConnectableLayer* layer) |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 344 | { |
Mike Kelly | a0766c3 | 2019-02-19 17:22:07 +0000 | [diff] [blame] | 345 | std::vector<fb::Offset<serializer::InputSlot>> inputSlots; |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 346 | |
| 347 | // Get the InputSlots |
| 348 | for (unsigned int slotIndex = 0; slotIndex<layer->GetNumInputSlots(); ++slotIndex) |
| 349 | { |
| 350 | const IInputSlot& inputSlot = layer->GetInputSlot(slotIndex); |
| 351 | |
| 352 | // Get the Connection for the InputSlot |
| 353 | const IOutputSlot* connection = inputSlot.GetConnection(); |
| 354 | |
| 355 | // Create FlatBuffer Connection |
Saoirse Stewart | cb8a321 | 2019-02-14 15:46:10 +0000 | [diff] [blame] | 356 | serializer::Connection conn(GetSerializedId(inputSlot.GetConnection()->GetOwningLayerGuid()), |
| 357 | connection->CalculateIndexOnOwner()); |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 358 | // Create FlatBuffer InputSlot |
| 359 | inputSlots.push_back(serializer::CreateInputSlot(m_flatBufferBuilder, slotIndex, &conn)); |
| 360 | } |
| 361 | return inputSlots; |
| 362 | } |
| 363 | |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 364 | std::vector<fb::Offset<serializer::OutputSlot>> SerializerVisitor::CreateOutputSlots(const IConnectableLayer* layer) |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 365 | { |
| 366 | std::vector<fb::Offset<serializer::OutputSlot>> outputSlots; |
| 367 | |
| 368 | // Get the OutputSlots |
| 369 | for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex) |
| 370 | { |
| 371 | const IOutputSlot& outputSlot = layer->GetOutputSlot(slotIndex); |
| 372 | const TensorInfo& tensorInfo = outputSlot.GetTensorInfo(); |
| 373 | |
| 374 | // Get the dimensions |
| 375 | std::vector<unsigned int> shape; |
| 376 | for(unsigned int dim = 0; dim < tensorInfo.GetShape().GetNumDimensions(); ++dim) |
| 377 | { |
| 378 | shape.push_back(tensorInfo.GetShape()[dim]); |
| 379 | } |
| 380 | |
| 381 | // Create FlatBuffer TensorInfo |
| 382 | auto flatBufferTensorInfo = serializer::CreateTensorInfo(m_flatBufferBuilder, |
| 383 | m_flatBufferBuilder.CreateVector(shape), |
| 384 | GetFlatBufferDataType(tensorInfo.GetDataType()), |
| 385 | tensorInfo.GetQuantizationScale(), |
| 386 | tensorInfo.GetQuantizationOffset()); |
| 387 | |
| 388 | // Create FlatBuffer Outputslot |
| 389 | outputSlots.push_back(serializer::CreateOutputSlot(m_flatBufferBuilder, |
| 390 | slotIndex, |
| 391 | flatBufferTensorInfo)); |
| 392 | } |
| 393 | return outputSlots; |
| 394 | } |
| 395 | |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 396 | |
| 397 | ISerializer* ISerializer::CreateRaw() |
| 398 | { |
| 399 | return new Serializer(); |
| 400 | } |
| 401 | |
| 402 | ISerializerPtr ISerializer::Create() |
| 403 | { |
| 404 | return ISerializerPtr(CreateRaw(), &ISerializer::Destroy); |
| 405 | } |
| 406 | |
| 407 | void ISerializer::Destroy(ISerializer* serializer) |
| 408 | { |
| 409 | delete serializer; |
| 410 | } |
| 411 | |
| 412 | void Serializer::Serialize(const INetwork& inNetwork) |
| 413 | { |
| 414 | // Iterate through to network |
| 415 | inNetwork.Accept(m_SerializerVisitor); |
| 416 | flatbuffers::FlatBufferBuilder& fbBuilder = m_SerializerVisitor.GetFlatBufferBuilder(); |
| 417 | |
| 418 | // Create FlatBuffer SerializedGraph |
| 419 | auto serializedGraph = serializer::CreateSerializedGraph( |
| 420 | fbBuilder, |
| 421 | fbBuilder.CreateVector(m_SerializerVisitor.GetSerializedLayers()), |
| 422 | fbBuilder.CreateVector(m_SerializerVisitor.GetInputIds()), |
| 423 | fbBuilder.CreateVector(m_SerializerVisitor.GetOutputIds())); |
| 424 | |
| 425 | // Serialize the graph |
| 426 | fbBuilder.Finish(serializedGraph); |
| 427 | } |
| 428 | |
| 429 | bool Serializer::SaveSerializedToStream(std::ostream& stream) |
| 430 | { |
| 431 | flatbuffers::FlatBufferBuilder& fbBuilder = m_SerializerVisitor.GetFlatBufferBuilder(); |
| 432 | |
Nattapat Chaimanowong | 7b53b69 | 2019-02-12 14:38:31 +0000 | [diff] [blame] | 433 | auto bytesToWrite = boost::numeric_cast<std::streamsize>(fbBuilder.GetSize()); |
| 434 | stream.write(reinterpret_cast<const char*>(fbBuilder.GetBufferPointer()), bytesToWrite); |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 435 | return !stream.bad(); |
| 436 | } |
| 437 | |
Matteo Martincigh | ec33391 | 2019-02-13 15:12:39 +0000 | [diff] [blame] | 438 | } // namespace armnnSerializer |