blob: 9d05a64ce80237be663b908d4208c3d46b5b7666 [file] [log] [blame]
josh minor4a3c6102020-01-06 16:40:46 -06001//
David Monahand7fca092023-01-12 14:53:34 +00002// Copyright © 2020-2021, 2023 Arm Ltd. All rights reserved.
josh minor4a3c6102020-01-06 16:40:46 -06003// SPDX-License-Identifier: MIT
4//
5#pragma once
6
Sadik Armagana097d2a2021-11-24 15:47:28 +00007#include <CommonTestUtils.hpp>
josh minor4a3c6102020-01-06 16:40:46 -06008
9#include <ResolveType.hpp>
10
11#include <armnn/INetwork.hpp>
12
Matthew Sloyan171214c2020-09-09 09:07:37 +010013#include <armnn/utility/NumericCast.hpp>
14
Sadik Armagan1625efc2021-06-10 18:24:34 +010015#include <doctest/doctest.h>
josh minor4a3c6102020-01-06 16:40:46 -060016
17#include <vector>
18
19namespace
20{
21
22template<armnn::DataType ArmnnTypeInput>
23INetworkPtr CreateElementwiseUnaryNetwork(const TensorShape& inputShape,
24 const TensorShape& outputShape,
25 UnaryOperation operation,
26 const float qScale = 1.0f,
27 const int32_t qOffset = 0)
28{
29 using namespace armnn;
30
31 INetworkPtr net(INetwork::Create());
32
33 ElementwiseUnaryDescriptor descriptor(operation);
34 IConnectableLayer* elementwiseUnaryLayer = net->AddElementwiseUnaryLayer(descriptor, "elementwiseUnary");
35
Cathal Corbett5b8093c2021-10-22 11:12:07 +010036 TensorInfo inputTensorInfo(inputShape, ArmnnTypeInput, qScale, qOffset, true);
Matthew Sloyan171214c2020-09-09 09:07:37 +010037 IConnectableLayer* input = net->AddInputLayer(armnn::numeric_cast<LayerBindingId>(0));
josh minor4a3c6102020-01-06 16:40:46 -060038 Connect(input, elementwiseUnaryLayer, inputTensorInfo, 0, 0);
39
40 TensorInfo outputTensorInfo(outputShape, ArmnnTypeInput, qScale, qOffset);
41 IConnectableLayer* output = net->AddOutputLayer(0, "output");
42 Connect(elementwiseUnaryLayer, output, outputTensorInfo, 0, 0);
43
44 return net;
45}
46
47template<armnn::DataType ArmnnInType,
48 typename TInput = armnn::ResolveType<ArmnnInType>>
49void ElementwiseUnarySimpleEndToEnd(const std::vector<BackendId>& backends,
David Monahand7fca092023-01-12 14:53:34 +000050 UnaryOperation operation)
josh minor4a3c6102020-01-06 16:40:46 -060051{
52 using namespace armnn;
53
54 const float qScale = IsQuantizedType<TInput>() ? 0.25f : 1.0f;
55 const int32_t qOffset = IsQuantizedType<TInput>() ? 50 : 0;
56
57 const TensorShape& inputShape = { 2, 2, 2, 2 };
58 const TensorShape& outputShape = { 2, 2, 2, 2 };
59
60 // Builds up the structure of the network
61 INetworkPtr net = CreateElementwiseUnaryNetwork<ArmnnInType>(inputShape, outputShape, operation, qScale, qOffset);
62
Sadik Armagan1625efc2021-06-10 18:24:34 +010063 CHECK(net);
josh minor4a3c6102020-01-06 16:40:46 -060064
David Monahand7fca092023-01-12 14:53:34 +000065 std::vector<float> input;
66 std::vector<float> expectedOutput;
67 switch(operation)
68 {
69 case UnaryOperation::Abs:
70 input = { 1, -1, 1, 1, 5, -5, 5, 5,
71 -3, 3, 3, 3, 4, 4, -4, 4 };
72 expectedOutput = { 1.f, 1.f, 1.f, 1.f, 5.f, 5.f, 5.f, 5.f,
73 3.f, 3.f, 3.f, 3.f, 4.f, 4.f, 4.f, 4.f };
74 break;
75 case UnaryOperation::Rsqrt:
76 input = { 1, 1, 1, 1, 5, 5, 5, 5,
77 3, 3, 3, 3, 4, 4, 4, 4 };
78 expectedOutput = { 1.f, 1.f, 1.f, 1.f, 0.447214f, 0.447214f, 0.447214f, 0.447214f,
79 0.57735f, 0.57735f, 0.57735f, 0.57735f, 0.5f, 0.5f, 0.5f, 0.5f };
80 break;
81 default:
82 input = { 1, -1, 1, 1, 5, -5, 5, 5,
83 -3, 3, 3, 3, 4, 4, -4, 4 };
84 expectedOutput = { 1.f, 1.f, 1.f, 1.f, 5.f, 5.f, 5.f, 5.f,
85 3.f, 3.f, 3.f, 3.f, 4.f, 4.f, 4.f, 4.f };
86 break;
87 }
88
josh minor4a3c6102020-01-06 16:40:46 -060089
90 // quantize data
91 std::vector<TInput> qInputData = armnnUtils::QuantizedVector<TInput>(input, qScale, qOffset);
92 std::vector<TInput> qExpectedOutput = armnnUtils::QuantizedVector<TInput>(expectedOutput, qScale, qOffset);
93
94 std::map<int, std::vector<TInput>> inputTensorData = {{ 0, qInputData }};
95 std::map<int, std::vector<TInput>> expectedOutputData = {{ 0, qExpectedOutput }};
96
Idriss Chaouch98e383e2023-08-28 14:28:31 +010097 EndToEndLayerTestImpl<ArmnnInType, ArmnnInType>(std::move(net), inputTensorData, expectedOutputData, backends);
josh minor4a3c6102020-01-06 16:40:46 -060098}
99
100} // anonymous namespace