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; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 54 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 55 | TensorInfo inputTensorInfo( {num, channels, height, width}, DataType::Float32); |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 56 | TensorInfo outputTensorInfo({num, channels, height, width}, DataType::Float32); |
| 57 | TensorInfo tensorInfo({channels}, DataType::Float32); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 59 | auto input = MakeTensor<float, 4>(inputTensorInfo, |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 60 | { |
| 61 | 1.f, 4.f, |
| 62 | 4.f, 2.f, |
| 63 | 1.f, 6.f, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 64 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 65 | 1.f, 1.f, |
| 66 | 4.f, 1.f, |
| 67 | -2.f, 4.f |
| 68 | }); |
| 69 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 70 | // these values are per-channel of the input |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 71 | auto mean = MakeTensor<float, 1>(tensorInfo, { 3.f, -2.f }); |
| 72 | auto variance = MakeTensor<float, 1>(tensorInfo, { 4.f, 9.f }); |
| 73 | auto beta = MakeTensor<float, 1>(tensorInfo, { 3.f, 2.f }); |
| 74 | auto gamma = MakeTensor<float, 1>(tensorInfo, { 2.f, 1.f }); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 75 | |
Teresa Charlin | e2a3b3f | 2020-08-17 23:22:11 +0100 | [diff] [blame] | 76 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 77 | std::unique_ptr<ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 78 | std::unique_ptr<ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
Teresa Charlin | e2a3b3f | 2020-08-17 23:22:11 +0100 | [diff] [blame] | 79 | ARMNN_NO_DEPRECATE_WARN_END |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 80 | |
| 81 | BatchNormalizationQueueDescriptor data; |
| 82 | WorkloadInfo info; |
| 83 | ScopedCpuTensorHandle meanTensor(tensorInfo); |
| 84 | ScopedCpuTensorHandle varianceTensor(tensorInfo); |
| 85 | ScopedCpuTensorHandle betaTensor(tensorInfo); |
| 86 | ScopedCpuTensorHandle gammaTensor(tensorInfo); |
| 87 | |
| 88 | AllocateAndCopyDataToITensorHandle(&meanTensor, &mean[0]); |
| 89 | AllocateAndCopyDataToITensorHandle(&varianceTensor, &variance[0]); |
| 90 | AllocateAndCopyDataToITensorHandle(&betaTensor, &beta[0]); |
| 91 | AllocateAndCopyDataToITensorHandle(&gammaTensor, &gamma[0]); |
| 92 | |
| 93 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 94 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 95 | data.m_Mean = &meanTensor; |
| 96 | data.m_Variance = &varianceTensor; |
| 97 | data.m_Beta = &betaTensor; |
| 98 | data.m_Gamma = &gammaTensor; |
| 99 | data.m_Parameters.m_Eps = 0.0f; |
| 100 | |
| 101 | // for each channel: |
| 102 | // substract mean, divide by standard deviation (with an epsilon to avoid div by 0) |
| 103 | // multiply by gamma and add beta |
| 104 | std::unique_ptr<IWorkload> workload = workloadFactory.CreateBatchNormalization(data, info); |
| 105 | |
| 106 | inputHandle->Allocate(); |
| 107 | outputHandle->Allocate(); |
| 108 | |
| 109 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 110 | |
| 111 | OpenClTimer openClTimer; |
| 112 | |
| 113 | BOOST_CHECK_EQUAL(openClTimer.GetName(), "OpenClKernelTimer"); |
| 114 | |
| 115 | //Start the timer |
| 116 | openClTimer.Start(); |
| 117 | |
| 118 | //Execute the workload |
| 119 | workload->Execute(); |
| 120 | |
| 121 | //Stop the timer |
| 122 | openClTimer.Stop(); |
| 123 | |
| 124 | BOOST_CHECK_EQUAL(openClTimer.GetMeasurements().size(), 1); |
| 125 | |
| 126 | BOOST_CHECK_EQUAL(openClTimer.GetMeasurements().front().m_Name, |
| 127 | "OpenClKernelTimer/0: batchnormalization_layer_nchw GWS[1,3,2]"); |
| 128 | |
| 129 | BOOST_CHECK(openClTimer.GetMeasurements().front().m_Value > 0); |
| 130 | |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_SUITE_END() |
| 134 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 135 | #endif //aarch64 or x86_64 |