From 90231b8c9f680d323e4b93dcd0820a47925e6d24 Mon Sep 17 00:00:00 2001 From: Mike Kelly Date: Thu, 5 Nov 2020 15:44:56 +0000 Subject: IVGCVSW-5315 Create FuseBatchNorm class Signed-off-by: Teresa Charlin Signed-off-by: Mike Kelly Change-Id: Id0625c58dbeea79874bf986b70d136ed9390bf83 --- .../test/optimizations/FuseBatchNormTests.cpp | 326 +++++++++++++++------ 1 file changed, 232 insertions(+), 94 deletions(-) (limited to 'src/armnn/test/optimizations') diff --git a/src/armnn/test/optimizations/FuseBatchNormTests.cpp b/src/armnn/test/optimizations/FuseBatchNormTests.cpp index 74cb8f96b7..bf47c577a4 100644 --- a/src/armnn/test/optimizations/FuseBatchNormTests.cpp +++ b/src/armnn/test/optimizations/FuseBatchNormTests.cpp @@ -4,17 +4,79 @@ // #include "LayersFwd.hpp" + +#include +#include +#include +#include + #include -BOOST_AUTO_TEST_SUITE(Optimizer) using namespace armnn; -// This unit test needs the reference backend, it's not available if the reference backend is not built -#if defined(ARMNNREF_ENABLED) -BOOST_AUTO_TEST_CASE(Fuse_batchNorm_into_Conv2D_Float32_Test) +BOOST_AUTO_TEST_SUITE(Optimizer) + +namespace +{ + +class Conv2dTest +{ +public: + using ConvDescriptorType = armnn::Convolution2dDescriptor; + using ConvLayerType = armnn::Convolution2dLayer; + + static IConnectableLayer *AddConvolution(INetwork *network, + const Convolution2dDescriptor &descriptor, + const ConstTensor &weights, + const Optional &biases, + const char *name) + { + return network->AddConvolution2dLayer(descriptor, weights, biases, name); + } +}; + +class DepthwiseConv2dTest +{ +public: + using ConvDescriptorType = armnn::DepthwiseConvolution2dDescriptor; + using ConvLayerType = armnn::DepthwiseConvolution2dLayer; + + static IConnectableLayer *AddConvolution(INetwork *network, + const DepthwiseConvolution2dDescriptor &descriptor, + const ConstTensor &weights, + const Optional &biases, + const char *name) + { + return network->AddDepthwiseConvolution2dLayer(descriptor, weights, biases, name); + } +}; + +template +std::vector GetVector(unsigned int size, float initial, float increment) +{ + std::vector typeVector(size, initial); + std::vector vector(size); + + if (size > 1) + { + for (unsigned int i = 0; i < size; ++i) + { + vector[i] = T(initial + (increment * static_cast(i))); + } + } + return vector; +} + +} // namespace + +template > +INetworkPtr CreatNetwork(bool depthwise, bool preventFusing) { // Define layers information - Convolution2dDescriptor convolution2dDescriptor; + ConvDescriptorType convolution2dDescriptor; convolution2dDescriptor.m_BiasEnabled = false; convolution2dDescriptor.m_DataLayout = DataLayout::NHWC; convolution2dDescriptor.m_StrideX = 1; @@ -22,127 +84,181 @@ BOOST_AUTO_TEST_CASE(Fuse_batchNorm_into_Conv2D_Float32_Test) BatchNormalizationDescriptor batchNormDescriptor; batchNormDescriptor.m_DataLayout = DataLayout::NHWC; - const unsigned int inputDimensionSizes[] = {1, 4, 4, 3}; // NHWCin - const unsigned int weightsDimensionSizes[] = {4, 2, 2, 3}; // CoutHWCin - const unsigned int outputDimensionSizes[] = {1, 3, 3, 4}; // NHWCout - const unsigned int outputChannelSize[] = {outputDimensionSizes[3]}; // Cout - - TensorInfo inputInfo (4, inputDimensionSizes, DataType::Float32); - TensorInfo outputInfo(4, outputDimensionSizes, DataType::Float32); - - std::vector weightsVector = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, - 11, 12, 13, 14, 15, 16, 17, 18, 19, 110, 111, 112, - 21, 22, 23, 24, 25, 26, 27, 28, 29, 210, 211, 212, - 31, 32, 33, 34, 35, 36, 37, 38, 39, 310, 311, 312}; - TensorInfo weightsInfo(4, weightsDimensionSizes, DataType::Float32); - ConstTensor weights (weightsInfo, weightsVector); - std::vector biasVector = {3.3f, 3.2f, 3.1f, 3.0f}; - TensorInfo biasInfo(1, outputChannelSize, DataType::Float32); - ConstTensor bias (biasInfo, biasVector); - Optional optionalBias = Optional(bias); + const unsigned int inputDimensionSizes[] = {1, 4, 4, 3}; // NHWCin + unsigned int weightsDimensionSizes[] = {4, 2, 2, 3}; // CoutHWCin + unsigned int outputDimensionSizes[] = {1, 3, 3, 4}; // NHWCout - std::vector betaVector = {0.0f, 0.2f, 0.3f, 0.4f}; - std::vector gammaVector = {0.5f, 0.6f, 0.7f, 0.8f}; - std::vector meanVector = {0.1f, 0.2f, 0.3f, 0.4f}; - std::vector varianceVector = {1.0f, 1.1f, 1.2f, 1.3f}; - ConstTensor beta (TensorInfo(1, outputChannelSize, DataType::Float32), betaVector); - ConstTensor gamma (TensorInfo(1, outputChannelSize, DataType::Float32), gammaVector); - ConstTensor mean (TensorInfo(1, outputChannelSize, DataType::Float32), meanVector); - ConstTensor variance(TensorInfo(1, outputChannelSize, DataType::Float32), varianceVector); + if (depthwise) + { + //M Cin H W + weightsDimensionSizes[0] = 4; + weightsDimensionSizes[1] = 3; + weightsDimensionSizes[2] = 2; + weightsDimensionSizes[3] = 2; + outputDimensionSizes[3] = weightsDimensionSizes[0] * weightsDimensionSizes[1]; + } + const unsigned int outputChannelSize[] = {outputDimensionSizes[3]}; // Cout - auto inputSize = inputDimensionSizes[0]*inputDimensionSizes[1]*inputDimensionSizes[2]*inputDimensionSizes[3]; - auto outputSize = outputDimensionSizes[0]*outputDimensionSizes[1]*outputDimensionSizes[2]*outputDimensionSizes[3]; + TensorInfo inputInfo(4, inputDimensionSizes, ArmnnType); + TensorInfo outputInfo(4, outputDimensionSizes, ArmnnType); - // FIRST NETWORK: Fused + std::vector weightsIntVector = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42}; + std::vector weightsVector(begin(weightsIntVector), end(weightsIntVector)); + TensorInfo weightsInfo(4, weightsDimensionSizes, ArmnnType); + ConstTensor weights(weightsInfo, weightsVector); - // Construct ArmNN network - NetworkId networkIdentifier; + std::vector biasVector = GetVector(outputDimensionSizes[3], 3.3f, 0.1f); + TensorInfo biasInfo(1, outputChannelSize, ArmnnType); + ConstTensor bias(biasInfo, biasVector); + Optional optionalBias = Optional(bias); + + std::vector betaVector = GetVector(outputDimensionSizes[3], 0.0f, 0.2f); + std::vector gammaVector = GetVector(outputDimensionSizes[3], 0.5f, 0.1f); + std::vector meanVector = GetVector(outputDimensionSizes[3], 0.1f, 0.1f); + std::vector varianceVector = GetVector(outputDimensionSizes[3], 1.0f, 0.1f); + + ConstTensor beta (TensorInfo(1, outputChannelSize, ArmnnType), betaVector); + ConstTensor gamma (TensorInfo(1, outputChannelSize, ArmnnType), gammaVector); + ConstTensor mean (TensorInfo(1, outputChannelSize, ArmnnType), meanVector); + ConstTensor variance(TensorInfo(1, outputChannelSize, ArmnnType), varianceVector); + + // Create a network INetworkPtr network = INetwork::Create(); - IConnectableLayer *inputLayer = network->AddInputLayer(0); - IConnectableLayer *convLayer = network->AddConvolution2dLayer(convolution2dDescriptor, - weights, - optionalBias, - "convolution"); - IConnectableLayer *batchNormLayer = network->AddBatchNormalizationLayer(batchNormDescriptor, + + IConnectableLayer* inputLayer = network->AddInputLayer(0); + + IConnectableLayer* convLayer = Conv2dTest::AddConvolution(network.get(), + convolution2dDescriptor, + weights, + optionalBias, + "convolution"); + + IConnectableLayer* batchNormLayer = network->AddBatchNormalizationLayer(batchNormDescriptor, mean, variance, beta, gamma, "batchNorm"); - IConnectableLayer *outputLayer = network->AddOutputLayer(0); - inputLayer ->GetOutputSlot(0).Connect(convLayer ->GetInputSlot(0)); - convLayer ->GetOutputSlot(0).Connect(batchNormLayer->GetInputSlot(0)); - batchNormLayer ->GetOutputSlot(0).Connect(outputLayer ->GetInputSlot(0)); + IConnectableLayer* outputLayer = network->AddOutputLayer(0); + IConnectableLayer* output2Layer = nullptr; + + if (preventFusing) + { + output2Layer = network->AddOutputLayer(1); + } - //Set the tensors in the network. - inputLayer ->GetOutputSlot(0).SetTensorInfo(inputInfo); - convLayer ->GetOutputSlot(0).SetTensorInfo(outputInfo); - batchNormLayer ->GetOutputSlot(0).SetTensorInfo(outputInfo); + // Set layer information + inputLayer ->GetOutputSlot(0).SetTensorInfo(inputInfo); + convLayer ->GetOutputSlot(0).SetTensorInfo(outputInfo); + batchNormLayer->GetOutputSlot(0).SetTensorInfo(outputInfo); + + // Connect layers + inputLayer ->GetOutputSlot(0).Connect(convLayer->GetInputSlot(0)); + convLayer ->GetOutputSlot(0).Connect(batchNormLayer->GetInputSlot(0)); + batchNormLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); + + if (preventFusing) + { + convLayer ->GetOutputSlot(0).Connect(output2Layer->GetInputSlot(0)); + } + + return network; +} + +template > +void FuseBatchNormIntoConvTest(bool depthwise, float tolerance, armnn::Compute backendId) +{ + // FIRST NETWORK: Fused + // Construct ArmNN network + INetworkPtr networkFused = CreatNetwork(depthwise, false); // Create ArmNN runtime - IRuntime::CreationOptions options; // default options - IRuntimePtr run = IRuntime::Create(options); + IRuntimePtr run = IRuntime::Create(IRuntime::CreationOptions()); // default options // Optimise ArmNN network - IOptimizedNetworkPtr optNet = Optimize(*network, {Compute::CpuRef}, run->GetDeviceSpec()); + IOptimizedNetworkPtr optNetFused = Optimize(*networkFused, {backendId}, run->GetDeviceSpec()); - // Load graph into runtime - BOOST_TEST(run->LoadNetwork(networkIdentifier, std::move(optNet)) == Status::Success); + Graph graphFused = PolymorphicDowncast(optNetFused.get())->GetGraph(); + + auto checkFusedConv2d = [ ](const armnn::Layer* const layer) -> bool + { + return IsLayerOfType(layer) && + (layer->GetNameStr() == "fused-batchNorm-into-convolution"); + }; + + BOOST_CHECK(3 == graphFused.GetNumLayers()); + BOOST_TEST(CheckSequence(graphFused.cbegin(), + graphFused.cend(), + &IsLayerOfType, + checkFusedConv2d, + &IsLayerOfType)); + + // Load network into runtime + NetworkId networkIdentifier; + BOOST_TEST(run->LoadNetwork(networkIdentifier, std::move(optNetFused)) == Status::Success); //Creates structures for inputs and outputs. - std::vector inputData(inputSize, 128); - std::vector outputData(outputSize); + std::vector inputDataFused = GetVector(48, 1.0f, 0.1f); + + std::vector outputDataFused(36); - InputTensors inputTensors {{0, ConstTensor(run->GetInputTensorInfo (networkIdentifier, 0), inputData.data())}}; - OutputTensors outputTensors{{0, Tensor(run->GetOutputTensorInfo(networkIdentifier, 0), outputData.data())}}; + if (depthwise) + { + outputDataFused.resize(108); + } + + InputTensors inputTensorsFused { + {0, ConstTensor(run->GetInputTensorInfo (networkIdentifier, 0), inputDataFused.data())}}; + OutputTensors outputTensorsFused{ + {0, Tensor(run->GetOutputTensorInfo(networkIdentifier, 0), outputDataFused.data())}}; // Execute network - run->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors); + run->EnqueueWorkload(networkIdentifier, inputTensorsFused, outputTensorsFused); // SECOND NETWORK: NotFused - // Construct ArmNN network - NetworkId networkIdentifierNotFused; - INetworkPtr networkNotFused = INetwork::Create(); - IConnectableLayer *inputLayerNotFused = networkNotFused->AddInputLayer(0); - IConnectableLayer *convLayerNotFused = networkNotFused->AddConvolution2dLayer(convolution2dDescriptor, - weights, - optionalBias, - "convolution"); - IConnectableLayer *batchNormLayerNotFused = networkNotFused->AddBatchNormalizationLayer(batchNormDescriptor, - mean, - variance, - beta, - gamma, - "batchNorm"); - IConnectableLayer *outputLayerNotFused = networkNotFused->AddOutputLayer(0); - IConnectableLayer *output2LayerNotFused = networkNotFused->AddOutputLayer(1); - - inputLayerNotFused ->GetOutputSlot(0).Connect(convLayerNotFused ->GetInputSlot(0)); - convLayerNotFused ->GetOutputSlot(0).Connect(batchNormLayerNotFused->GetInputSlot(0)); - batchNormLayerNotFused ->GetOutputSlot(0).Connect(outputLayerNotFused ->GetInputSlot(0)); - convLayerNotFused ->GetOutputSlot(0).Connect(output2LayerNotFused ->GetInputSlot(0)); - - //Set the tensors in the network. - inputLayerNotFused ->GetOutputSlot(0).SetTensorInfo(inputInfo); - convLayerNotFused ->GetOutputSlot(0).SetTensorInfo(outputInfo); - batchNormLayerNotFused ->GetOutputSlot(0).SetTensorInfo(outputInfo); + INetworkPtr networkNotFused = CreatNetwork(depthwise, true); // Create ArmNN runtime - IRuntimePtr runNotFused = IRuntime::Create(options); + IRuntimePtr runNotFused = IRuntime::Create(IRuntime::CreationOptions()); // default options // Optimise ArmNN network - IOptimizedNetworkPtr optNetNotFused = Optimize(*networkNotFused, {Compute::CpuRef}, runNotFused->GetDeviceSpec()); + IOptimizedNetworkPtr optNetNotFused = Optimize(*networkNotFused, {backendId}, runNotFused->GetDeviceSpec()); - // Load graph into runtime + Graph graphNotFused = PolymorphicDowncast(optNetNotFused.get())->GetGraph(); + + BOOST_CHECK(5 == graphNotFused.GetNumLayers()); + BOOST_TEST(CheckSequence(graphNotFused.cbegin(), + graphNotFused.cend(), + &IsLayerOfType, + &IsLayerOfType, + &IsLayerOfType, + &IsLayerOfType, + &IsLayerOfType)); + + // Load network into runtime + NetworkId networkIdentifierNotFused; BOOST_TEST(runNotFused->LoadNetwork(networkIdentifierNotFused, std::move(optNetNotFused)) == Status::Success); //Creates structures for inputs and outputs. - std::vector inputDataNotFused(inputSize, 128); - std::vector outputDataNotFused(outputSize); - std::vector outputData2NotFused(outputSize); + std::vector inputDataNotFused = GetVector(48, 1.0f, 0.1f); + std::vector outputDataNotFused(36); + std::vector outputData2NotFused(36); + + if (depthwise) + { + outputDataNotFused.resize(108); + outputData2NotFused.resize(108); + } InputTensors inputTensorsNotFused{ {0, ConstTensor(runNotFused->GetInputTensorInfo(networkIdentifierNotFused, 0), inputDataNotFused.data())}}; OutputTensors outputTensorsNotFused{ @@ -153,11 +269,33 @@ BOOST_AUTO_TEST_CASE(Fuse_batchNorm_into_Conv2D_Float32_Test) runNotFused->EnqueueWorkload(networkIdentifierNotFused, inputTensorsNotFused, outputTensorsNotFused); // Check the output of the fused-convolution matches with the output of the batchNormm in the "NotFused" network - for (unsigned int n = 0; n < outputData.size(); ++n) + for (unsigned int n = 0; n < outputDataFused.size(); ++n) { - BOOST_CHECK_CLOSE(outputData[n], outputDataNotFused[n], 0.001); + BOOST_CHECK_CLOSE(outputDataFused[n], outputDataNotFused[n], T(tolerance)); } } + +// This unit test needs the reference backend, it's not available if the reference backend is not built +#if defined(ARMNNREF_ENABLED) +BOOST_AUTO_TEST_CASE(FuseBatchNormIntoConv2DFloat32Test) +{ + FuseBatchNormIntoConvTest(false, 0.0001f, armnn::Compute::CpuRef); +} + +BOOST_AUTO_TEST_CASE(FuseBatchNormIntoConv2DFloat16Test) +{ + FuseBatchNormIntoConvTest(false, 0.1f, armnn::Compute::CpuRef); +} + +BOOST_AUTO_TEST_CASE(FuseBatchNormIntoDepthwiseConv2DFloat32Test) +{ + FuseBatchNormIntoConvTest(true, 0.0001f,armnn::Compute::CpuRef); +} + +BOOST_AUTO_TEST_CASE(FuseBatchNormIntoDepthwiseConv2DFloat16Test) +{ + FuseBatchNormIntoConvTest(true, 0.1f,armnn::Compute::CpuRef); +} #endif BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.1