diff options
Diffstat (limited to 'src/armnnDeserializer')
-rw-r--r-- | src/armnnDeserializer/Deserializer.cpp | 152 | ||||
-rw-r--r-- | src/armnnDeserializer/test/DeserializeConstant.cpp | 5 | ||||
-rw-r--r-- | src/armnnDeserializer/test/DeserializeDepthwiseConv2d.cpp | 280 | ||||
-rw-r--r-- | src/armnnDeserializer/test/DeserializeGather.cpp | 5 | ||||
-rw-r--r-- | src/armnnDeserializer/test/DeserializeGatherNd.cpp | 5 |
5 files changed, 388 insertions, 59 deletions
diff --git a/src/armnnDeserializer/Deserializer.cpp b/src/armnnDeserializer/Deserializer.cpp index 93fa99dcc3..704b6c35c1 100644 --- a/src/armnnDeserializer/Deserializer.cpp +++ b/src/armnnDeserializer/Deserializer.cpp @@ -1372,11 +1372,48 @@ void IDeserializer::DeserializerImpl::ParseConstant(GraphPtr graph, unsigned int auto serializerInput = serializerLayer->input(); armnn::ConstTensor input = ToConstTensor(serializerInput); + IConnectableLayer* layer; - IConnectableLayer* layer = m_Network->AddConstantLayer(input, layerName.c_str()); + // Required for when Constant Layer is used as an inputs to DepthwiseConvolution2d Layer. + // Running a model that was created before weights layout scheme version was added to our flatbuffers + // file ensuring older models can still be read and executed. featureVersion weights layout scheme 1 + // indicates a change in the depthwise weights layout within ArmNN from [M,I,H,W] --> [1,H,W,I*M] + if (this->GetFeatureVersions(graph).m_WeightsLayoutScheme <= 0) + { + // Permute weights [ H, W, M, I ] --> [ 1, H, W, I*M ] + // Step1: [ M, I, H, W ] --> [ H, W, I, M] + PermutationVector permutationVector = { 3, 2, 0, 1 }; + armnn::TensorInfo weightsInfo = input.GetInfo(); + std::unique_ptr<unsigned char[]> permuteBuffer(new unsigned char[weightsInfo.GetNumBytes()]); + weightsInfo = armnnUtils::Permuted(weightsInfo, permutationVector); + armnnUtils::Permute(weightsInfo.GetShape(), permutationVector, + input.GetMemoryArea(), permuteBuffer.get(), + GetDataTypeSize(weightsInfo.GetDataType())); - armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); - layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + // Step2: Reshape [ H, W, I, M] --> [ 1, H, W, I*M ] + auto weightsShape = weightsInfo.GetShape(); + weightsInfo.SetShape({1, + weightsShape[0], + weightsShape[1], + weightsShape[2]*weightsShape[3]}); + + armnn::ConstTensor weightsPermuted(weightsInfo, permuteBuffer.get()); + + layer = m_Network->AddConstantLayer(weightsPermuted, layerName.c_str()); + + layer->GetOutputSlot(0).SetTensorInfo(weightsPermuted.GetInfo()); + + RegisterOutputSlots(graph, layerIndex, layer); + + return; + } + else + { + layer = m_Network->AddConstantLayer(input, layerName.c_str()); + + armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); + layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + } RegisterOutputSlots(graph, layerIndex, layer); } @@ -1499,7 +1536,6 @@ void IDeserializer::DeserializerImpl::ParseDepthwiseConvolution2d(GraphPtr graph CHECK_LAYERS(graph, 0, layerIndex); auto inputs = GetInputs(graph, layerIndex); CHECK_LOCATION(); - CHECK_VALID_SIZE(inputs.size(), 1); auto outputs = GetOutputs(graph, layerIndex); CHECK_VALID_SIZE(outputs.size(), 1); @@ -1509,67 +1545,89 @@ void IDeserializer::DeserializerImpl::ParseDepthwiseConvolution2d(GraphPtr graph auto serializerDescriptor = serializerLayer->descriptor(); armnn::DepthwiseConvolution2dDescriptor descriptor; - descriptor.m_PadLeft = serializerDescriptor->padLeft(); - descriptor.m_PadRight = serializerDescriptor->padRight(); - descriptor.m_PadTop = serializerDescriptor->padTop(); - descriptor.m_PadBottom = serializerDescriptor->padBottom(); - descriptor.m_StrideX = serializerDescriptor->strideX(); - descriptor.m_StrideY = serializerDescriptor->strideY(); - descriptor.m_DilationX = serializerDescriptor->dilationX(); - descriptor.m_DilationY = serializerDescriptor->dilationY(); - descriptor.m_BiasEnabled = serializerDescriptor->biasEnabled();; - descriptor.m_DataLayout = ToDataLayout(serializerDescriptor->dataLayout()); + descriptor.m_PadLeft = serializerDescriptor->padLeft(); + descriptor.m_PadRight = serializerDescriptor->padRight(); + descriptor.m_PadTop = serializerDescriptor->padTop(); + descriptor.m_PadBottom = serializerDescriptor->padBottom(); + descriptor.m_StrideX = serializerDescriptor->strideX(); + descriptor.m_StrideY = serializerDescriptor->strideY(); + descriptor.m_DilationX = serializerDescriptor->dilationX(); + descriptor.m_DilationY = serializerDescriptor->dilationY(); + descriptor.m_BiasEnabled = serializerDescriptor->biasEnabled(); + descriptor.m_DataLayout = ToDataLayout(serializerDescriptor->dataLayout()); IConnectableLayer* layer; + std::vector<unsigned int> ignoreSlots {}; - armnn::Optional<armnn::ConstTensor> optionalBiases = armnn::EmptyOptional(); - if (descriptor.m_BiasEnabled) - { - armnn::ConstTensor biases = ToConstTensor(serializerLayer->biases()); - optionalBiases = armnn::Optional<armnn::ConstTensor>(biases); - } - - armnn::ConstTensor weights = ToConstTensor(serializerLayer->weights()); - // The data layout for weights in ArmNN used to be [M,I,H,W] but now it's changed to [1,H,W,I*M] - // When reading older flatbuffer files we need to add a permutation to get to the new layout. - if (this->GetFeatureVersions(graph).m_WeightsLayoutScheme <= 0) + // Weights and biases used to be always constant and were stored as members of the layer. This has changed and + // they are now passed as inputs. If they are constant then they will be stored in a ConstantLayer. + if (this->GetFeatureVersions(graph).m_ConstTensorsAsInputs <= 0) { - // Permute weights [ H, W, M, I ] --> [ 1, H, W, I*M ] - // Step1: [ M, I, H, W ] --> [ H, W, I, M] - PermutationVector permutationVector = { 3, 2, 0, 1 }; - armnn::TensorInfo weightsInfo = weights.GetInfo(); - std::unique_ptr<unsigned char[]> permuteBuffer(new unsigned char[weightsInfo.GetNumBytes()]); - weightsInfo = armnnUtils::Permuted(weightsInfo, permutationVector); - armnnUtils::Permute(weightsInfo.GetShape(), permutationVector, - weights.GetMemoryArea(), permuteBuffer.get(), - GetDataTypeSize(weightsInfo.GetDataType())); - - // Step2: Reshape [ H, W, I, M] --> [ 1, H, W, I*M ] - auto weightsShape = weightsInfo.GetShape(); - weightsInfo.SetShape({1, - weightsShape[0], - weightsShape[1], - weightsShape[2]*weightsShape[3]}); + CHECK_VALID_SIZE(inputs.size(), 1); - armnn::ConstTensor weightsPermuted(weightsInfo, permuteBuffer.get()); + // If the model stores weights and biases as members of the layer we have to read them from there + // but add them to their own ConstantLayer for compatibility + armnn::ConstTensor weights = ToConstTensor(serializerLayer->weights()); + ignoreSlots.emplace_back(1u); layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor, - weightsPermuted, - optionalBiases, layerName.c_str()); + + armnn::Optional<armnn::ConstTensor> optionalBiases = armnn::EmptyOptional(); + if (descriptor.m_BiasEnabled) + { + armnn::ConstTensor biases = ToConstTensor(serializerLayer->biases()); + ignoreSlots.emplace_back(2u); + + auto biasLayer = m_Network->AddConstantLayer(biases); + biasLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(2u)); + biasLayer->GetOutputSlot(0).SetTensorInfo(biases.GetInfo()); + } + + if (this->GetFeatureVersions(graph).m_WeightsLayoutScheme <= 0) + { + // Permute weights [ H, W, M, I ] --> [ 1, H, W, I*M ] + // Step1: [ M, I, H, W ] --> [ H, W, I, M] + PermutationVector permutationVector = { 3, 2, 0, 1 }; + armnn::TensorInfo weightsInfo = weights.GetInfo(); + std::unique_ptr<unsigned char[]> permuteBuffer(new unsigned char[weightsInfo.GetNumBytes()]); + weightsInfo = armnnUtils::Permuted(weightsInfo, permutationVector); + armnnUtils::Permute(weightsInfo.GetShape(), permutationVector, + weights.GetMemoryArea(), permuteBuffer.get(), + GetDataTypeSize(weightsInfo.GetDataType())); + + // Step2: Reshape [ H, W, I, M] --> [ 1, H, W, I*M ] + auto weightsShape = weightsInfo.GetShape(); + weightsInfo.SetShape({1, + weightsShape[0], + weightsShape[1], + weightsShape[2]*weightsShape[3]}); + + armnn::ConstTensor weightsPermuted(weightsInfo, permuteBuffer.get()); + + auto weightsLayer = m_Network->AddConstantLayer(weightsPermuted); + weightsLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1u)); + weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsPermuted.GetInfo()); + } + else + { + auto weightsLayer = m_Network->AddConstantLayer(weights); + weightsLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1u)); + weightsLayer->GetOutputSlot(0).SetTensorInfo(weights.GetInfo()); + } } else { layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor, - weights, - optionalBiases, layerName.c_str()); + uint32_t numInputs = descriptor.GetNumInputs(); + CHECK_VALID_SIZE(inputs.size(), numInputs); } armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); - RegisterInputSlots(graph, layerIndex, layer); + RegisterInputSlots(graph, layerIndex, layer, ignoreSlots); RegisterOutputSlots(graph, layerIndex, layer); } diff --git a/src/armnnDeserializer/test/DeserializeConstant.cpp b/src/armnnDeserializer/test/DeserializeConstant.cpp index 682e8a157d..b5a2151268 100644 --- a/src/armnnDeserializer/test/DeserializeConstant.cpp +++ b/src/armnnDeserializer/test/DeserializeConstant.cpp @@ -121,7 +121,10 @@ struct ConstantAddFixture : public ParserFlatbuffersSerializeFixture }, }], }}}, - }] + }], + featureVersions: { + weightsLayoutScheme: 1, + } } )"; SetupSingleInputSingleOutput("InputLayer1", "OutputLayer"); diff --git a/src/armnnDeserializer/test/DeserializeDepthwiseConv2d.cpp b/src/armnnDeserializer/test/DeserializeDepthwiseConv2d.cpp index 1920c1cc79..d5f3c6396f 100644 --- a/src/armnnDeserializer/test/DeserializeDepthwiseConv2d.cpp +++ b/src/armnnDeserializer/test/DeserializeDepthwiseConv2d.cpp @@ -13,9 +13,9 @@ TEST_SUITE("Deserializer_DepthwiseConv2d") { -struct DepthwiseConv2dFlatbufferVersion1Fixture : public ParserFlatbuffersSerializeFixture +struct DepthwiseConv2dFlatbufferVersion1FixtureOld : public ParserFlatbuffersSerializeFixture { - explicit DepthwiseConv2dFlatbufferVersion1Fixture() + explicit DepthwiseConv2dFlatbufferVersion1FixtureOld() { m_JsonString = R"( { @@ -214,20 +214,282 @@ struct DepthwiseConv2dFlatbufferVersion1Fixture : public ParserFlatbuffersSerial } }; +struct DepthwiseConv2dFlatbufferVersion1Fixture : public ParserFlatbuffersSerializeFixture +{ + explicit DepthwiseConv2dFlatbufferVersion1Fixture() + { + m_JsonString = R"( + { + "layers": [ + { + "layer_type": "InputLayer", + "layer": { + "base": { + "base": { + "index": 0, + "layerName": "InputLayer", + "layerType": "Input", + "inputSlots": [ + ], + "outputSlots": [ + { + "index": 0, + "tensorInfo": { + "dimensions": [ + 1, + 3, + 3, + 3 + ], + "dataType": "QAsymmS8", + "quantizationScale": 1.0, + "quantizationOffset": 0, + "quantizationDim": 0, + "dimensionality": 1, + "dimensionSpecificity": [ + true, + true, + true, + true + ] + } + } + ] + }, + "layerBindingId": 0 + } + } + }, + { + "layer_type": "DepthwiseConvolution2dLayer", + "layer": { + "base": { + "index": 1, + "layerName": "depthwiseConvolution2dWithPerAxis", + "layerType": "DepthwiseConvolution2d", + "inputSlots": [ + { + "index": 0, + "connection": { + "sourceLayerIndex": 0, + "outputSlotIndex": 0 + } + }, + { + "index": 1, + "connection": { + "sourceLayerIndex": 2, + "outputSlotIndex": 0 + } + } + ], + "outputSlots": [ + { + "index": 0, + "tensorInfo": { + "dimensions": [ + 1, + 3, + 3, + 3 + ], + "dataType": "QAsymmS8", + "quantizationScale": 1.0, + "quantizationOffset": 0, + "quantizationDim": 0, + "dimensionality": 1, + "dimensionSpecificity": [ + true, + true, + true, + true + ] + } + } + ] + }, + "descriptor": { + "padLeft": 1, + "padRight": 1, + "padTop": 1, + "padBottom": 1, + "strideX": 1, + "strideY": 1, + "dilationX": 1, + "dilationY": 1, + "biasEnabled": false, + "dataLayout": "NHWC" + } + } + }, + { + "layer_type": "ConstantLayer", + "layer": { + "base": { + "index": 2, + "layerName": "Weights", + "layerType": "Constant", + "inputSlots": [ + ], + "outputSlots": [ + { + "index": 0, + "tensorInfo": { + "dimensions": [ + 1, + 3, + 3, + 3 + ], + "dataType": "QSymmS8", + "quantizationScale": 0.25, + "quantizationOffset": 0, + "quantizationDim": 0, + "dimensionality": 1, + "dimensionSpecificity": [ + true, + true, + true, + true + ], + quantizationScales: [ + 0.25, + 0.2, + 0.1 + ], + "isConstant": true, + } + } + ] + }, + "input": { + "info": { + "dimensions": [ + 1, + 3, + 3, + 3 + ], + "dataType": "QSymmS8", + "quantizationScale": 0.25, + "quantizationOffset": 0, + "quantizationDim": 0, + "dimensionality": 1, + "dimensionSpecificity": [ + true, + true, + true, + true + ], + quantizationScales: [ + 0.25, + 0.2, + 0.1 + ] + }, + "data_type": "ByteData", + "data": { + "data": [ + 4, + 20, + 0, + 8, + 20, + 30, + 4, + 0, + 10, + 12, + 0, + 40, + 0, + 5, + 30, + 16, + 10, + 40, + 12, + 0, + 30, + 16, + 20, + 0, + 12, + 20, + 20 + ] + } + } + } + }, + { + "layer_type": "OutputLayer", + "layer": { + "base": { + "base": { + "index": 3, + "layerName": "OutputLayer", + "layerType": "Output", + "inputSlots": [ + { + "index": 0, + "connection": { + "sourceLayerIndex": 1, + "outputSlotIndex": 0 + } + } + ], + "outputSlots": [ + ] + }, + "layerBindingId": 0 + } + } + } + ], + "inputIds": [ + 0 + ], + "outputIds": [ + 0 + ], + "featureVersions": { + "bindingIdsScheme": 1, + "constantTensorsAsInputs": 1 + } + } + )"; + Setup(); + } +}; + // This test uses a model that was created before weights layout scheme version was added to our flatbuffers // file. It ensures older models can still be read and executed // featureVersion weights layout scheme 1 indicates a change in the depthwise weights layout within // armm from [M,I,H,W] --> [1,H,W,I*M] -TEST_CASE_FIXTURE(DepthwiseConv2dFlatbufferVersion1Fixture, "DepthwiseConv2d_FlatbufferVersion1") +TEST_CASE_FIXTURE(DepthwiseConv2dFlatbufferVersion1FixtureOld, "DepthwiseConv2d_FlatbufferVersion1Old") +{ + RunTest<4, armnn::DataType::QAsymmS8>( + 0, + { 3,2,0,0,4,3,0,1,2, + 0,1,3,0,4,2,2,2,3, + 2,4,3,2,0,4,3,4,0}, + { 15,60,10,11,37,20, 0,18,17, + 20,65,28,28,74,26,12,20,18, + 25,36,12,37,42,25,29,14, 9}); +} + +TEST_CASE_FIXTURE(DepthwiseConv2dFlatbufferVersion1Fixture, + "DepthwiseConv2d_FlatbufferVersion1_WeightsAndBiasesAsConstantLayers") { RunTest<4, armnn::DataType::QAsymmS8>( 0, - { 3,2,0,0,4,3,0,1,2, - 0,1,3,0,4,2,2,2,3, - 2,4,3,2,0,4,3,4,0}, - { 15,60,10,11,37,20, 0,18,17, - 20,65,28,28,74,26,12,20,18, - 25,36,12,37,42,25,29,14, 9}); + {{"InputLayer", { 3,2,0,0,4,3,0,1,2, + 0,1,3,0,4,2,2,2,3, + 2,4,3,2,0,4,3,4,0}}}, + {{"OutputLayer", { 15,60,10,11,37,20, 0,18,17, + 20,65,28,28,74,26,12,20,18, + 25,36,12,37,42,25,29,14, 9}}}); } }
\ No newline at end of file diff --git a/src/armnnDeserializer/test/DeserializeGather.cpp b/src/armnnDeserializer/test/DeserializeGather.cpp index 47919c4481..0d12d71c9d 100644 --- a/src/armnnDeserializer/test/DeserializeGather.cpp +++ b/src/armnnDeserializer/test/DeserializeGather.cpp @@ -119,7 +119,10 @@ struct GatherFixture : public ParserFlatbuffersSerializeFixture }, }], }}}, - }] + }], + featureVersions: { + weightsLayoutScheme: 1, + } } )"; Setup(); diff --git a/src/armnnDeserializer/test/DeserializeGatherNd.cpp b/src/armnnDeserializer/test/DeserializeGatherNd.cpp index 684a42ca07..f0341e24ee 100644 --- a/src/armnnDeserializer/test/DeserializeGatherNd.cpp +++ b/src/armnnDeserializer/test/DeserializeGatherNd.cpp @@ -115,7 +115,10 @@ struct GatherNdFixture : public ParserFlatbuffersSerializeFixture }, }], }}}, - }] + }], + featureVersions: { + weightsLayoutScheme: 1, + } } )"; Setup(); |