1 //
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
13 namespace
14 {
15 template<typename armnn::DataType DataType>
CreatePreluNetwork(const armnn::TensorInfo & inputInfo,const armnn::TensorInfo & alphaInfo,const armnn::TensorInfo & outputInfo)16 INetworkPtr CreatePreluNetwork(const armnn::TensorInfo& inputInfo,
17 const armnn::TensorInfo& alphaInfo,
18 const armnn::TensorInfo& outputInfo)
19 {
20 using namespace armnn;
21
22 INetworkPtr net(INetwork::Create());
23
24 IConnectableLayer* input = net->AddInputLayer(0, "input");
25 IConnectableLayer* alpha = net->AddInputLayer(1, "alpha");
26 IConnectableLayer* prelu = net->AddPreluLayer("Prelu");
27 IConnectableLayer* output = net->AddOutputLayer(0, "output");
28
29 Connect(input, prelu, inputInfo, 0, 0);
30 Connect(alpha, prelu, alphaInfo, 0, 1);
31 Connect(prelu, output, outputInfo, 0, 0);
32
33 return net;
34 }
35
36 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
PreluEndToEnd(const std::vector<BackendId> & backends,const std::vector<T> & inputData,const std::vector<T> & alphaData,const std::vector<T> & expectedOutputData,const float qScale,const int32_t qOffset)37 void PreluEndToEnd(const std::vector<BackendId>& backends,
38 const std::vector<T>& inputData,
39 const std::vector<T>& alphaData,
40 const std::vector<T>& expectedOutputData,
41 const float qScale ,
42 const int32_t qOffset)
43 {
44 using namespace armnn;
45
46 armnn::TensorInfo inputInfo({ 2, 2, 2, 1 }, ArmnnType);
47 armnn::TensorInfo alphaInfo({ 1, 2, 2, 1 }, ArmnnType);
48 armnn::TensorInfo outputInfo({ 2, 2, 2, 1 }, ArmnnType);
49
50 inputInfo.SetQuantizationOffset(qOffset);
51 inputInfo.SetQuantizationScale(qScale);
52 alphaInfo.SetQuantizationOffset(qOffset);
53 alphaInfo.SetQuantizationScale(qScale);
54 outputInfo.SetQuantizationOffset(qOffset);
55 outputInfo.SetQuantizationScale(qScale);
56
57 INetworkPtr net = CreatePreluNetwork<ArmnnType>(inputInfo, alphaInfo, outputInfo);
58
59 BOOST_TEST_CHECKPOINT("Create a network");
60
61 std::map<int, std::vector<T>> inputTensorData = { { 0, inputData }, { 1, alphaData} };
62 std::map<int, std::vector<T>> expectedOutputTensorData = { { 0, expectedOutputData } };
63
64 EndToEndLayerTestImpl<ArmnnType, ArmnnType>(move(net),
65 inputTensorData,
66 expectedOutputTensorData,
67 backends);
68 }
69
70 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
PreluEndToEndPositiveTest(const std::vector<BackendId> & backends,const float qScale=1.0f,const int32_t qOffset=2)71 void PreluEndToEndPositiveTest(const std::vector<BackendId>& backends, const float qScale = 1.0f,
72 const int32_t qOffset = 2)
73 {
74 std::vector<T> inputData{ 1, 2, 3, 4, 5, 6, 7, 8 };
75 std::vector<T> alphaData{ 2, 1, 1, 1 };
76
77 std::vector<T> expectedOutputData{ 2, 2, 3, 4, 5, 6, 7, 8 };
78
79 PreluEndToEnd<ArmnnType>(backends, inputData, alphaData, expectedOutputData, qScale, qOffset);
80 }
81
82 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
PreluEndToEndNegativeTest(const std::vector<BackendId> & backends,const float qScale=1.0f,const int32_t qOffset=0)83 void PreluEndToEndNegativeTest(const std::vector<BackendId>& backends, const float qScale = 1.0f,
84 const int32_t qOffset = 0)
85 {
86 std::vector<T> inputData{ 1, -2, 3, 4, 5, 6, 7, 8 };
87 std::vector<T> alphaData{ 1, 2, 1, 1 };
88
89 std::vector<T> expectedOutputData{ 1, -4, 3, 4, 5, 6, 7, 8 };
90
91 PreluEndToEnd<ArmnnType>(backends, inputData, alphaData, expectedOutputData, qScale, qOffset);
92 }
93
94 } // anonymous namespace