blob: e11553dd3804f96d6b0643caa3f640f1f26e529e [file] [log] [blame]
Nikhil Raj747f5862019-07-19 15:15:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#pragma once
6
7#include <ResolveType.hpp>
8
9#include <armnn/INetwork.hpp>
10
11#include <backendsCommon/test/CommonTestUtils.hpp>
12
Sadik Armagan1625efc2021-06-10 18:24:34 +010013#include <doctest/doctest.h>
14
Nikhil Raj747f5862019-07-19 15:15:23 +010015namespace
16{
17template<typename armnn::DataType DataType>
18INetworkPtr CreatePreluNetwork(const armnn::TensorInfo& inputInfo,
19 const armnn::TensorInfo& alphaInfo,
20 const armnn::TensorInfo& outputInfo)
21{
22 using namespace armnn;
23
24 INetworkPtr net(INetwork::Create());
25
26 IConnectableLayer* input = net->AddInputLayer(0, "input");
27 IConnectableLayer* alpha = net->AddInputLayer(1, "alpha");
28 IConnectableLayer* prelu = net->AddPreluLayer("Prelu");
29 IConnectableLayer* output = net->AddOutputLayer(0, "output");
30
31 Connect(input, prelu, inputInfo, 0, 0);
32 Connect(alpha, prelu, alphaInfo, 0, 1);
33 Connect(prelu, output, outputInfo, 0, 0);
34
35 return net;
36}
37
38template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
39void PreluEndToEnd(const std::vector<BackendId>& backends,
40 const std::vector<T>& inputData,
41 const std::vector<T>& alphaData,
42 const std::vector<T>& expectedOutputData,
43 const float qScale ,
44 const int32_t qOffset)
45{
46 using namespace armnn;
47
48 armnn::TensorInfo inputInfo({ 2, 2, 2, 1 }, ArmnnType);
49 armnn::TensorInfo alphaInfo({ 1, 2, 2, 1 }, ArmnnType);
50 armnn::TensorInfo outputInfo({ 2, 2, 2, 1 }, ArmnnType);
51
52 inputInfo.SetQuantizationOffset(qOffset);
53 inputInfo.SetQuantizationScale(qScale);
54 alphaInfo.SetQuantizationOffset(qOffset);
55 alphaInfo.SetQuantizationScale(qScale);
56 outputInfo.SetQuantizationOffset(qOffset);
57 outputInfo.SetQuantizationScale(qScale);
58
59 INetworkPtr net = CreatePreluNetwork<ArmnnType>(inputInfo, alphaInfo, outputInfo);
60
Sadik Armagan1625efc2021-06-10 18:24:34 +010061 CHECK(net);
Nikhil Raj747f5862019-07-19 15:15:23 +010062
63 std::map<int, std::vector<T>> inputTensorData = { { 0, inputData }, { 1, alphaData} };
64 std::map<int, std::vector<T>> expectedOutputTensorData = { { 0, expectedOutputData } };
65
66 EndToEndLayerTestImpl<ArmnnType, ArmnnType>(move(net),
67 inputTensorData,
68 expectedOutputTensorData,
69 backends);
70}
71
72template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
73void PreluEndToEndPositiveTest(const std::vector<BackendId>& backends, const float qScale = 1.0f,
74 const int32_t qOffset = 2)
75{
76 std::vector<T> inputData{ 1, 2, 3, 4, 5, 6, 7, 8 };
77 std::vector<T> alphaData{ 2, 1, 1, 1 };
78
79 std::vector<T> expectedOutputData{ 2, 2, 3, 4, 5, 6, 7, 8 };
80
81 PreluEndToEnd<ArmnnType>(backends, inputData, alphaData, expectedOutputData, qScale, qOffset);
82}
83
84template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
85void PreluEndToEndNegativeTest(const std::vector<BackendId>& backends, const float qScale = 1.0f,
86 const int32_t qOffset = 0)
87{
88 std::vector<T> inputData{ 1, -2, 3, 4, 5, 6, 7, 8 };
89 std::vector<T> alphaData{ 1, 2, 1, 1 };
90
91 std::vector<T> expectedOutputData{ 1, -4, 3, 4, 5, 6, 7, 8 };
92
93 PreluEndToEnd<ArmnnType>(backends, inputData, alphaData, expectedOutputData, qScale, qOffset);
94}
95
96} // anonymous namespace