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 | |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 6 | #include "NeonWorkloadFactoryHelper.hpp" |
| 7 | |
Aron Virginas-Tar | 3b278e9 | 2018-10-12 13:00:55 +0100 | [diff] [blame] | 8 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 9 | #include <test/TensorHelpers.hpp> |
Aron Virginas-Tar | 3b278e9 | 2018-10-12 13:00:55 +0100 | [diff] [blame] | 10 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 11 | #include <backendsCommon/CpuTensorHandle.hpp> |
| 12 | #include <backendsCommon/WorkloadFactory.hpp> |
Aron Virginas-Tar | 3b278e9 | 2018-10-12 13:00:55 +0100 | [diff] [blame] | 13 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 14 | #include <neon/NeonTimer.hpp> |
| 15 | #include <neon/NeonWorkloadFactory.hpp> |
Aron Virginas-Tar | 3b278e9 | 2018-10-12 13:00:55 +0100 | [diff] [blame] | 16 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 17 | #include <backendsCommon/test/LayerTests.hpp> |
| 18 | #include <backendsCommon/test/TensorCopyUtils.hpp> |
| 19 | #include <backendsCommon/test/WorkloadTestUtils.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 20 | |
| 21 | #include <boost/test/unit_test.hpp> |
Aron Virginas-Tar | 3b278e9 | 2018-10-12 13:00:55 +0100 | [diff] [blame] | 22 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 23 | #include <cstdlib> |
| 24 | #include <algorithm> |
| 25 | |
| 26 | using namespace armnn; |
| 27 | |
| 28 | BOOST_AUTO_TEST_SUITE(NeonTimerInstrument) |
| 29 | |
| 30 | |
| 31 | BOOST_AUTO_TEST_CASE(NeonTimerGetName) |
| 32 | { |
| 33 | NeonTimer neonTimer; |
| 34 | BOOST_CHECK_EQUAL(neonTimer.GetName(), "NeonKernelTimer"); |
| 35 | } |
| 36 | |
| 37 | BOOST_AUTO_TEST_CASE(NeonTimerMeasure) |
| 38 | { |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 39 | NeonWorkloadFactory workloadFactory = |
| 40 | NeonWorkloadFactoryHelper::GetFactory(NeonWorkloadFactoryHelper::GetMemoryManager()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 41 | |
Conor Kennedy | f341d7f | 2019-05-27 13:20:38 +0100 | [diff] [blame] | 42 | unsigned int inputWidth = 2000u; |
| 43 | unsigned int inputHeight = 2000u; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 44 | unsigned int inputChannels = 1u; |
| 45 | unsigned int inputBatchSize = 1u; |
| 46 | |
| 47 | float upperBound = 1.0f; |
| 48 | float lowerBound = -1.0f; |
| 49 | |
| 50 | size_t inputSize = inputWidth * inputHeight * inputChannels * inputBatchSize; |
| 51 | std::vector<float> inputData(inputSize, 0.f); |
| 52 | std::generate(inputData.begin(), inputData.end(), [](){ |
| 53 | return (static_cast<float>(rand()) / static_cast<float>(RAND_MAX / 3)) + 1.f; }); |
| 54 | |
| 55 | unsigned int outputWidth = inputWidth; |
| 56 | unsigned int outputHeight = inputHeight; |
| 57 | unsigned int outputChannels = inputChannels; |
| 58 | unsigned int outputBatchSize = inputBatchSize; |
| 59 | |
| 60 | armnn::TensorInfo inputTensorInfo({ inputBatchSize, inputChannels, inputHeight, inputWidth }, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 61 | armnn::DataType::Float32); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 62 | |
| 63 | armnn::TensorInfo outputTensorInfo({ outputBatchSize, outputChannels, outputHeight, outputWidth }, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 64 | armnn::DataType::Float32); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | |
| 66 | LayerTestResult<float, 4> result(inputTensorInfo); |
| 67 | |
| 68 | auto input = MakeTensor<float, 4>(inputTensorInfo, inputData); |
| 69 | |
Teresa Charlin | e2a3b3f | 2020-08-17 23:22:11 +0100 | [diff] [blame] | 70 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 71 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 72 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
Teresa Charlin | e2a3b3f | 2020-08-17 23:22:11 +0100 | [diff] [blame] | 73 | ARMNN_NO_DEPRECATE_WARN_END |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 74 | |
| 75 | // Setup bounded ReLu |
| 76 | armnn::ActivationQueueDescriptor descriptor; |
| 77 | armnn::WorkloadInfo workloadInfo; |
| 78 | AddInputToWorkload(descriptor, workloadInfo, inputTensorInfo, inputHandle.get()); |
| 79 | AddOutputToWorkload(descriptor, workloadInfo, outputTensorInfo, outputHandle.get()); |
| 80 | |
| 81 | descriptor.m_Parameters.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 82 | descriptor.m_Parameters.m_A = upperBound; |
| 83 | descriptor.m_Parameters.m_B = lowerBound; |
| 84 | |
| 85 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateActivation(descriptor, workloadInfo); |
| 86 | |
| 87 | inputHandle->Allocate(); |
| 88 | outputHandle->Allocate(); |
| 89 | |
| 90 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 91 | |
| 92 | NeonTimer neonTimer; |
| 93 | // Start the timer. |
| 94 | neonTimer.Start(); |
| 95 | // Execute the workload. |
| 96 | workload->Execute(); |
| 97 | // Stop the timer. |
| 98 | neonTimer.Stop(); |
| 99 | |
| 100 | std::vector<Measurement> measurements = neonTimer.GetMeasurements(); |
| 101 | |
Matthew Bentham | 679efda | 2018-12-10 13:14:37 +0000 | [diff] [blame] | 102 | BOOST_CHECK(measurements.size() <= 2); |
| 103 | if (measurements.size() > 1) |
| 104 | { |
| 105 | BOOST_CHECK_EQUAL(measurements[0].m_Name, "NeonKernelTimer/0: NEFillBorderKernel"); |
| 106 | BOOST_CHECK(measurements[0].m_Value > 0.0); |
| 107 | } |
Nikhil Raj | 13816cd | 2021-01-12 14:29:18 +0000 | [diff] [blame] | 108 | std::ostringstream oss_neon; |
| 109 | std::ostringstream oss_cpu; |
| 110 | oss_neon << "NeonKernelTimer/" << measurements.size()-1 << ": NEActivationLayerKernel"; |
| 111 | oss_cpu << "NeonKernelTimer/" << measurements.size()-1 << ": CpuActivationKernel"; |
| 112 | BOOST_CHECK(measurements[measurements.size()-1].m_Name == oss_neon.str() || |
| 113 | measurements[measurements.size()-1].m_Name == oss_cpu.str()); |
Matthew Bentham | 679efda | 2018-12-10 13:14:37 +0000 | [diff] [blame] | 114 | BOOST_CHECK(measurements[measurements.size()-1].m_Value > 0.0); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | BOOST_AUTO_TEST_SUITE_END() |