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