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 | #if (defined(__aarch64__)) || (defined(__x86_64__)) // disable test failing on FireFly/Armv7 |
| 7 | |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 8 | #include "ClWorkloadFactoryHelper.hpp" |
| 9 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 10 | #include <test/TensorHelpers.hpp> |
Aron Virginas-Tar | 3b278e9 | 2018-10-12 13:00:55 +0100 | [diff] [blame] | 11 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 12 | #include <backendsCommon/CpuTensorHandle.hpp> |
| 13 | #include <backendsCommon/WorkloadFactory.hpp> |
Aron Virginas-Tar | 3b278e9 | 2018-10-12 13:00:55 +0100 | [diff] [blame] | 14 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 15 | #include <cl/ClContextControl.hpp> |
| 16 | #include <cl/ClWorkloadFactory.hpp> |
| 17 | #include <cl/OpenClTimer.hpp> |
Aron Virginas-Tar | 3b278e9 | 2018-10-12 13:00:55 +0100 | [diff] [blame] | 18 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 19 | #include <backendsCommon/test/TensorCopyUtils.hpp> |
| 20 | #include <backendsCommon/test/WorkloadTestUtils.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 21 | |
Aron Virginas-Tar | 3b278e9 | 2018-10-12 13:00:55 +0100 | [diff] [blame] | 22 | #include <arm_compute/runtime/CL/CLScheduler.h> |
| 23 | |
| 24 | #include <boost/format.hpp> |
| 25 | #include <boost/test/unit_test.hpp> |
| 26 | |
| 27 | #include <iostream> |
| 28 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 29 | using namespace armnn; |
| 30 | |
| 31 | struct OpenClFixture |
| 32 | { |
| 33 | // Initialising ClContextControl to ensure OpenCL is loaded correctly for each test case. |
| 34 | // NOTE: Profiling needs to be enabled in ClContextControl to be able to obtain execution |
| 35 | // times from OpenClTimer. |
| 36 | OpenClFixture() : m_ClContextControl(nullptr, true) {} |
| 37 | ~OpenClFixture() {} |
| 38 | |
| 39 | ClContextControl m_ClContextControl; |
| 40 | }; |
| 41 | |
| 42 | BOOST_FIXTURE_TEST_SUITE(OpenClTimerBatchNorm, OpenClFixture) |
| 43 | using FactoryType = ClWorkloadFactory; |
| 44 | |
| 45 | BOOST_AUTO_TEST_CASE(OpenClTimerBatchNorm) |
| 46 | { |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 47 | auto memoryManager = ClWorkloadFactoryHelper::GetMemoryManager(); |
| 48 | ClWorkloadFactory workloadFactory = ClWorkloadFactoryHelper::GetFactory(memoryManager); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 49 | |
| 50 | const unsigned int width = 2; |
| 51 | const unsigned int height = 3; |
| 52 | const unsigned int channels = 2; |
| 53 | const unsigned int num = 1; |
| 54 | int32_t qOffset = 0; |
| 55 | float qScale = 0.f; |
| 56 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 57 | TensorInfo inputTensorInfo({num, channels, height, width}, DataType::Float32); |
| 58 | TensorInfo outputTensorInfo({num, channels, height, width}, DataType::Float32); |
| 59 | TensorInfo tensorInfo({channels}, DataType::Float32); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 60 | |
| 61 | // Set quantization parameters if the requested type is a quantized type. |
| 62 | if(IsQuantizedType<float>()) |
| 63 | { |
| 64 | inputTensorInfo.SetQuantizationScale(qScale); |
| 65 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 66 | outputTensorInfo.SetQuantizationScale(qScale); |
| 67 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 68 | tensorInfo.SetQuantizationScale(qScale); |
| 69 | tensorInfo.SetQuantizationOffset(qOffset); |
| 70 | } |
| 71 | |
| 72 | auto input = MakeTensor<float, 4>(inputTensorInfo, |
| 73 | QuantizedVector<float>(qScale, qOffset, |
| 74 | { |
| 75 | 1.f, 4.f, |
| 76 | 4.f, 2.f, |
| 77 | 1.f, 6.f, |
| 78 | |
| 79 | 1.f, 1.f, |
| 80 | 4.f, 1.f, |
| 81 | -2.f, 4.f |
| 82 | })); |
| 83 | // these values are per-channel of the input |
| 84 | auto mean = MakeTensor<float, 1>(tensorInfo, QuantizedVector<float>(qScale, qOffset, {3, -2})); |
| 85 | auto variance = MakeTensor<float, 1>(tensorInfo, QuantizedVector<float>(qScale, qOffset, {4, 9})); |
| 86 | auto beta = MakeTensor<float, 1>(tensorInfo, QuantizedVector<float>(qScale, qOffset, {3, 2})); |
| 87 | auto gamma = MakeTensor<float, 1>(tensorInfo, QuantizedVector<float>(qScale, qOffset, {2, 1})); |
| 88 | |
| 89 | std::unique_ptr<ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 90 | std::unique_ptr<ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 91 | |
| 92 | BatchNormalizationQueueDescriptor data; |
| 93 | WorkloadInfo info; |
| 94 | ScopedCpuTensorHandle meanTensor(tensorInfo); |
| 95 | ScopedCpuTensorHandle varianceTensor(tensorInfo); |
| 96 | ScopedCpuTensorHandle betaTensor(tensorInfo); |
| 97 | ScopedCpuTensorHandle gammaTensor(tensorInfo); |
| 98 | |
| 99 | AllocateAndCopyDataToITensorHandle(&meanTensor, &mean[0]); |
| 100 | AllocateAndCopyDataToITensorHandle(&varianceTensor, &variance[0]); |
| 101 | AllocateAndCopyDataToITensorHandle(&betaTensor, &beta[0]); |
| 102 | AllocateAndCopyDataToITensorHandle(&gammaTensor, &gamma[0]); |
| 103 | |
| 104 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 105 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 106 | data.m_Mean = &meanTensor; |
| 107 | data.m_Variance = &varianceTensor; |
| 108 | data.m_Beta = &betaTensor; |
| 109 | data.m_Gamma = &gammaTensor; |
| 110 | data.m_Parameters.m_Eps = 0.0f; |
| 111 | |
| 112 | // for each channel: |
| 113 | // substract mean, divide by standard deviation (with an epsilon to avoid div by 0) |
| 114 | // multiply by gamma and add beta |
| 115 | std::unique_ptr<IWorkload> workload = workloadFactory.CreateBatchNormalization(data, info); |
| 116 | |
| 117 | inputHandle->Allocate(); |
| 118 | outputHandle->Allocate(); |
| 119 | |
| 120 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 121 | |
| 122 | OpenClTimer openClTimer; |
| 123 | |
| 124 | BOOST_CHECK_EQUAL(openClTimer.GetName(), "OpenClKernelTimer"); |
| 125 | |
| 126 | //Start the timer |
| 127 | openClTimer.Start(); |
| 128 | |
| 129 | //Execute the workload |
| 130 | workload->Execute(); |
| 131 | |
| 132 | //Stop the timer |
| 133 | openClTimer.Stop(); |
| 134 | |
| 135 | BOOST_CHECK_EQUAL(openClTimer.GetMeasurements().size(), 1); |
| 136 | |
| 137 | BOOST_CHECK_EQUAL(openClTimer.GetMeasurements().front().m_Name, |
| 138 | "OpenClKernelTimer/0: batchnormalization_layer_nchw GWS[1,3,2]"); |
| 139 | |
| 140 | BOOST_CHECK(openClTimer.GetMeasurements().front().m_Value > 0); |
| 141 | |
| 142 | } |
| 143 | |
| 144 | BOOST_AUTO_TEST_SUITE_END() |
| 145 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 146 | #endif //aarch64 or x86_64 |