Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "LayerTestResult.hpp" |
| 9 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 10 | #include <QuantizeHelper.hpp> |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 11 | #include <ResolveType.hpp> |
| 12 | |
| 13 | #include <armnn/ArmNN.hpp> |
| 14 | |
| 15 | #include <backendsCommon/IBackendInternal.hpp> |
| 16 | #include <backendsCommon/WorkloadFactory.hpp> |
| 17 | |
| 18 | #include <backendsCommon/test/TensorCopyUtils.hpp> |
| 19 | #include <backendsCommon/test/WorkloadTestUtils.hpp> |
| 20 | |
| 21 | #include <test/TensorHelpers.hpp> |
| 22 | |
| 23 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 24 | LayerTestResult<T, 4> PreluTest( |
| 25 | armnn::IWorkloadFactory& workloadFactory, |
| 26 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 27 | { |
| 28 | armnn::TensorInfo inputTensorInfo ({ 1, 2, 2, 3 }, ArmnnType); |
| 29 | armnn::TensorInfo alphaTensorInfo ({ 1, 1, 1, 3 }, ArmnnType); |
| 30 | armnn::TensorInfo outputTensorInfo({ 1, 2, 2, 3 }, ArmnnType); |
| 31 | |
| 32 | if (armnn::IsQuantizedType<T>()) |
| 33 | { |
| 34 | inputTensorInfo.SetQuantizationScale(0.25f); |
| 35 | inputTensorInfo.SetQuantizationOffset(128); |
| 36 | alphaTensorInfo.SetQuantizationScale(0.25f); |
| 37 | alphaTensorInfo.SetQuantizationOffset(50); |
| 38 | outputTensorInfo.SetQuantizationScale(0.5f); |
| 39 | outputTensorInfo.SetQuantizationOffset(120); |
| 40 | } |
| 41 | |
| 42 | std::vector<float> inputData |
| 43 | { |
| 44 | // Expected quantized values: |
| 45 | // 128, 128, 128, 132, 132, 132, 124, 124, 124, 120, 120, 120 |
| 46 | 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -2.0f, -2.0f, -2.0f |
| 47 | }; |
| 48 | std::vector<float> alphaData |
| 49 | { |
| 50 | // Expected quantized values: |
| 51 | // 50, 54, 58 |
| 52 | 0.0f, 1.0f, 2.0f |
| 53 | }; |
| 54 | std::vector<float> outputExpectedData = |
| 55 | { |
| 56 | // Expected quantized values: |
| 57 | // 20, 120, 120, 122, 122, 122, 120, 118, 116, 120, 116, 112 |
| 58 | 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, -1.0f, -2.0f, 0.0f, -2.0f, -4.0f |
| 59 | }; |
| 60 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 61 | auto input = MakeTensor<T, 4>(inputTensorInfo, |
| 62 | armnnUtils::QuantizedVector<T>(inputData, |
| 63 | inputTensorInfo.GetQuantizationScale(), |
| 64 | inputTensorInfo.GetQuantizationOffset())); |
| 65 | |
| 66 | auto alpha = MakeTensor<T, 4>(alphaTensorInfo, |
| 67 | armnnUtils::QuantizedVector<T>(alphaData, |
| 68 | alphaTensorInfo.GetQuantizationScale(), |
| 69 | alphaTensorInfo.GetQuantizationOffset())); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 70 | |
| 71 | LayerTestResult<T, 4> result(outputTensorInfo); |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 72 | result.outputExpected = |
| 73 | MakeTensor<T, 4>(outputTensorInfo, |
| 74 | armnnUtils::QuantizedVector<T>(outputExpectedData, |
| 75 | outputTensorInfo.GetQuantizationScale(), |
| 76 | outputTensorInfo.GetQuantizationOffset())); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 77 | |
| 78 | std::unique_ptr <armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 79 | std::unique_ptr <armnn::ITensorHandle> alphaHandle = workloadFactory.CreateTensorHandle(alphaTensorInfo); |
| 80 | std::unique_ptr <armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 81 | |
| 82 | armnn::PreluQueueDescriptor descriptor; |
| 83 | armnn::WorkloadInfo info; |
| 84 | AddInputToWorkload (descriptor, info, inputTensorInfo, inputHandle.get()); |
| 85 | AddInputToWorkload (descriptor, info, alphaTensorInfo, alphaHandle.get()); |
| 86 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
| 87 | |
| 88 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreatePrelu(descriptor, info); |
| 89 | |
| 90 | inputHandle->Allocate(); |
| 91 | alphaHandle->Allocate(); |
| 92 | outputHandle->Allocate(); |
| 93 | |
| 94 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 95 | CopyDataToITensorHandle(alphaHandle.get(), &alpha[0][0][0][0]); |
| 96 | |
| 97 | workload->Execute(); |
| 98 | |
| 99 | CopyDataFromITensorHandle(&result.output[0][0][0][0], outputHandle.get()); |
| 100 | |
| 101 | return result; |
| 102 | } |