telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #include <boost/test/unit_test.hpp> |
| 7 | #include <boost/cast.hpp> |
| 8 | |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame^] | 9 | #include <backends/WorkloadData.hpp> |
| 10 | #include <Graph.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 11 | |
| 12 | #include <utility> |
| 13 | |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame^] | 14 | #include <backends/CpuTensorHandle.hpp> |
| 15 | #include <backends/cl/ClWorkloadFactory.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 16 | |
| 17 | using namespace armnn; |
| 18 | using namespace std; |
| 19 | |
| 20 | // connects two layers |
| 21 | void Connect(Layer* from, Layer* to, const TensorInfo& tensorInfo, unsigned int fromIndex = 0, unsigned int toIndex = 0) |
| 22 | { |
| 23 | from->GetOutputSlot(fromIndex).Connect(to->GetInputSlot(toIndex)); |
| 24 | from->GetOutputHandler(fromIndex).SetTensorInfo(tensorInfo); |
| 25 | } |
| 26 | |
| 27 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 28 | // The following test are created specifically to test ReleaseConstantData() method in the Layer |
| 29 | // They build very simple graphs including the layer will be checked. |
| 30 | // Checks weights and biases before the method called and after. |
| 31 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | BOOST_AUTO_TEST_SUITE(LayerReleaseConstantDataTest) |
| 34 | |
| 35 | BOOST_AUTO_TEST_CASE(ReleaseBatchNormalizationLayerConstantDataTest) |
| 36 | { |
| 37 | Graph graph; |
| 38 | ClWorkloadFactory factory; |
| 39 | |
| 40 | // create the layer we're testing |
| 41 | BatchNormalizationDescriptor layerDesc; |
| 42 | layerDesc.m_Eps = 0.05f; |
| 43 | BatchNormalizationLayer* const layer = graph.AddLayer<BatchNormalizationLayer>(layerDesc, "layer"); |
| 44 | |
| 45 | armnn::TensorInfo weightInfo({3}, armnn::DataType::Float32); |
| 46 | layer->m_Mean = std::make_unique<ScopedCpuTensorHandle>(weightInfo); |
| 47 | layer->m_Variance = std::make_unique<ScopedCpuTensorHandle>(weightInfo); |
| 48 | layer->m_Beta = std::make_unique<ScopedCpuTensorHandle>(weightInfo); |
| 49 | layer->m_Gamma = std::make_unique<ScopedCpuTensorHandle>(weightInfo); |
| 50 | layer->m_Mean->Allocate(); |
| 51 | layer->m_Variance->Allocate(); |
| 52 | layer->m_Beta->Allocate(); |
| 53 | layer->m_Gamma->Allocate(); |
| 54 | |
| 55 | // create extra layers |
| 56 | Layer* const input = graph.AddLayer<InputLayer>(0, "input"); |
| 57 | Layer* const output = graph.AddLayer<OutputLayer>(0, "output"); |
| 58 | |
| 59 | // connect up |
| 60 | armnn::TensorInfo tensorInfo({2, 3, 1, 1}, armnn::DataType::Float32); |
| 61 | Connect(input, layer, tensorInfo); |
| 62 | Connect(layer, output, tensorInfo); |
| 63 | |
| 64 | // check the constants that they are not NULL |
| 65 | BOOST_CHECK(layer->m_Mean != nullptr); |
| 66 | BOOST_CHECK(layer->m_Variance != nullptr); |
| 67 | BOOST_CHECK(layer->m_Beta != nullptr); |
| 68 | BOOST_CHECK(layer->m_Gamma != nullptr); |
| 69 | |
| 70 | // free up the constants.. |
| 71 | layer->ReleaseConstantData(); |
| 72 | |
| 73 | // check the constants that they are NULL now |
| 74 | BOOST_CHECK(layer->m_Mean == nullptr); |
| 75 | BOOST_CHECK(layer->m_Variance == nullptr); |
| 76 | BOOST_CHECK(layer->m_Beta == nullptr); |
| 77 | BOOST_CHECK(layer->m_Gamma == nullptr); |
| 78 | |
| 79 | } |
| 80 | |
| 81 | |
| 82 | BOOST_AUTO_TEST_CASE(ReleaseConvolution2dLayerConstantDataTest) |
| 83 | { |
| 84 | Graph graph; |
| 85 | ClWorkloadFactory factory; |
| 86 | |
| 87 | // create the layer we're testing |
| 88 | Convolution2dDescriptor layerDesc; |
| 89 | layerDesc.m_PadLeft = 3; |
| 90 | layerDesc.m_PadRight = 3; |
| 91 | layerDesc.m_PadTop = 1; |
| 92 | layerDesc.m_PadBottom = 1; |
| 93 | layerDesc.m_StrideX = 2; |
| 94 | layerDesc.m_StrideY = 4; |
| 95 | layerDesc.m_BiasEnabled = true; |
| 96 | |
| 97 | Convolution2dLayer* const layer = graph.AddLayer<Convolution2dLayer>(layerDesc, "layer"); |
| 98 | |
| 99 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(TensorInfo({2, 3, 5, 3}, |
| 100 | armnn::DataType::Float32)); |
| 101 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle> |
| 102 | (TensorInfo({2}, GetBiasDataType(armnn::DataType::Float32))); |
| 103 | |
| 104 | layer->m_Weight->Allocate(); |
| 105 | layer->m_Bias->Allocate(); |
| 106 | |
| 107 | // create extra layers |
| 108 | Layer* const input = graph.AddLayer<InputLayer>(0, "input"); |
| 109 | Layer* const output = graph.AddLayer<OutputLayer>(0, "output"); |
| 110 | |
| 111 | // connect up |
| 112 | Connect(input, layer, TensorInfo({2, 3, 8, 16}, armnn::DataType::Float32)); |
| 113 | Connect(layer, output, TensorInfo({2, 2, 2, 10}, armnn::DataType::Float32)); |
| 114 | |
| 115 | // check the constants that they are not NULL |
| 116 | BOOST_CHECK(layer->m_Weight != nullptr); |
| 117 | BOOST_CHECK(layer->m_Bias != nullptr); |
| 118 | |
| 119 | // free up the constants.. |
| 120 | layer->ReleaseConstantData(); |
| 121 | |
| 122 | // check the constants that they are NULL now |
| 123 | BOOST_CHECK(layer->m_Weight == nullptr); |
| 124 | BOOST_CHECK(layer->m_Bias == nullptr); |
| 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(ReleaseDepthwiseConvolution2dLayerConstantDataTest) |
| 128 | { |
| 129 | Graph graph; |
| 130 | ClWorkloadFactory factory; |
| 131 | |
| 132 | // create the layer we're testing |
| 133 | DepthwiseConvolution2dDescriptor layerDesc; |
| 134 | layerDesc.m_PadLeft = 3; |
| 135 | layerDesc.m_PadRight = 3; |
| 136 | layerDesc.m_PadTop = 1; |
| 137 | layerDesc.m_PadBottom = 1; |
| 138 | layerDesc.m_StrideX = 2; |
| 139 | layerDesc.m_StrideY = 4; |
| 140 | layerDesc.m_BiasEnabled = true; |
| 141 | |
| 142 | DepthwiseConvolution2dLayer* const layer = graph.AddLayer<DepthwiseConvolution2dLayer>(layerDesc, "layer"); |
| 143 | |
| 144 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(TensorInfo({3, 3, 5, 3}, DataType::Float32)); |
| 145 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(TensorInfo({9}, DataType::Float32)); |
| 146 | layer->m_Weight->Allocate(); |
| 147 | layer->m_Bias->Allocate(); |
| 148 | |
| 149 | // create extra layers |
| 150 | Layer* const input = graph.AddLayer<InputLayer>(0, "input"); |
| 151 | Layer* const output = graph.AddLayer<OutputLayer>(0, "output"); |
| 152 | |
| 153 | // connect up |
| 154 | Connect(input, layer, TensorInfo({2, 3, 8, 16}, armnn::DataType::Float32)); |
| 155 | Connect(layer, output, TensorInfo({2, 9, 2, 10}, armnn::DataType::Float32)); |
| 156 | |
| 157 | // check the constants that they are not NULL |
| 158 | BOOST_CHECK(layer->m_Weight != nullptr); |
| 159 | BOOST_CHECK(layer->m_Bias != nullptr); |
| 160 | |
| 161 | // free up the constants.. |
| 162 | layer->ReleaseConstantData(); |
| 163 | |
| 164 | // check the constants that they are NULL now |
| 165 | BOOST_CHECK(layer->m_Weight == nullptr); |
| 166 | BOOST_CHECK(layer->m_Bias == nullptr); |
| 167 | } |
| 168 | |
| 169 | BOOST_AUTO_TEST_CASE(ReleaseFullyConnectedLayerConstantDataTest) |
| 170 | { |
| 171 | Graph graph; |
| 172 | ClWorkloadFactory factory; |
| 173 | |
| 174 | // create the layer we're testing |
| 175 | FullyConnectedDescriptor layerDesc; |
| 176 | layerDesc.m_BiasEnabled = true; |
| 177 | layerDesc.m_TransposeWeightMatrix = true; |
| 178 | |
| 179 | FullyConnectedLayer* const layer = graph.AddLayer<FullyConnectedLayer>(layerDesc, "layer"); |
| 180 | |
| 181 | float inputsQScale = 1.0f; |
| 182 | float outputQScale = 2.0f; |
| 183 | |
| 184 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(TensorInfo({7, 20}, |
| 185 | DataType::QuantisedAsymm8, inputsQScale, 0)); |
| 186 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(TensorInfo({7}, |
| 187 | GetBiasDataType(DataType::QuantisedAsymm8), inputsQScale)); |
| 188 | layer->m_Weight->Allocate(); |
| 189 | layer->m_Bias->Allocate(); |
| 190 | |
| 191 | // create extra layers |
| 192 | Layer* const input = graph.AddLayer<InputLayer>(0, "input"); |
| 193 | Layer* const output = graph.AddLayer<OutputLayer>(0, "output"); |
| 194 | |
| 195 | // connect up |
| 196 | Connect(input, layer, TensorInfo({3, 1, 4, 5}, DataType::QuantisedAsymm8, inputsQScale)); |
| 197 | Connect(layer, output, TensorInfo({3, 7}, DataType::QuantisedAsymm8, outputQScale)); |
| 198 | |
| 199 | // check the constants that they are not NULL |
| 200 | BOOST_CHECK(layer->m_Weight != nullptr); |
| 201 | BOOST_CHECK(layer->m_Bias != nullptr); |
| 202 | |
| 203 | // free up the constants.. |
| 204 | layer->ReleaseConstantData(); |
| 205 | |
| 206 | // check the constants that they are NULL now |
| 207 | BOOST_CHECK(layer->m_Weight == nullptr); |
| 208 | BOOST_CHECK(layer->m_Bias == nullptr); |
| 209 | } |
| 210 | |
| 211 | BOOST_AUTO_TEST_SUITE_END() |
| 212 | |