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 | |
| 70 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 71 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 72 | |
| 73 | // Setup bounded ReLu |
| 74 | armnn::ActivationQueueDescriptor descriptor; |
| 75 | armnn::WorkloadInfo workloadInfo; |
| 76 | AddInputToWorkload(descriptor, workloadInfo, inputTensorInfo, inputHandle.get()); |
| 77 | AddOutputToWorkload(descriptor, workloadInfo, outputTensorInfo, outputHandle.get()); |
| 78 | |
| 79 | descriptor.m_Parameters.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 80 | descriptor.m_Parameters.m_A = upperBound; |
| 81 | descriptor.m_Parameters.m_B = lowerBound; |
| 82 | |
| 83 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateActivation(descriptor, workloadInfo); |
| 84 | |
| 85 | inputHandle->Allocate(); |
| 86 | outputHandle->Allocate(); |
| 87 | |
| 88 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 89 | |
| 90 | NeonTimer neonTimer; |
| 91 | // Start the timer. |
| 92 | neonTimer.Start(); |
| 93 | // Execute the workload. |
| 94 | workload->Execute(); |
| 95 | // Stop the timer. |
| 96 | neonTimer.Stop(); |
| 97 | |
| 98 | std::vector<Measurement> measurements = neonTimer.GetMeasurements(); |
| 99 | |
Matthew Bentham | 679efda | 2018-12-10 13:14:37 +0000 | [diff] [blame] | 100 | BOOST_CHECK(measurements.size() <= 2); |
| 101 | if (measurements.size() > 1) |
| 102 | { |
| 103 | BOOST_CHECK_EQUAL(measurements[0].m_Name, "NeonKernelTimer/0: NEFillBorderKernel"); |
| 104 | BOOST_CHECK(measurements[0].m_Value > 0.0); |
| 105 | } |
Matthew Bentham | f8d6301 | 2018-12-10 17:05:25 +0000 | [diff] [blame] | 106 | std::ostringstream oss; |
| 107 | oss << "NeonKernelTimer/" << measurements.size()-1 << ": NEActivationLayerKernel"; |
| 108 | BOOST_CHECK_EQUAL(measurements[measurements.size()-1].m_Name, oss.str()); |
Matthew Bentham | 679efda | 2018-12-10 13:14:37 +0000 | [diff] [blame] | 109 | BOOST_CHECK(measurements[measurements.size()-1].m_Value > 0.0); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | BOOST_AUTO_TEST_SUITE_END() |