blob: 4c93735bc8ed302988c0adc123f46eee59c2cde4 [file] [log] [blame]
josh minor4a3c6102020-01-06 16:40:46 -06001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#pragma once
6
7#include "CommonTestUtils.hpp"
8
9#include <ResolveType.hpp>
10
11#include <armnn/INetwork.hpp>
12
13#include <boost/test/unit_test.hpp>
14
15#include <vector>
16
17namespace
18{
19
20template<armnn::DataType ArmnnTypeInput>
21INetworkPtr CreateElementwiseUnaryNetwork(const TensorShape& inputShape,
22 const TensorShape& outputShape,
23 UnaryOperation operation,
24 const float qScale = 1.0f,
25 const int32_t qOffset = 0)
26{
27 using namespace armnn;
28
29 INetworkPtr net(INetwork::Create());
30
31 ElementwiseUnaryDescriptor descriptor(operation);
32 IConnectableLayer* elementwiseUnaryLayer = net->AddElementwiseUnaryLayer(descriptor, "elementwiseUnary");
33
34 TensorInfo inputTensorInfo(inputShape, ArmnnTypeInput, qScale, qOffset);
35 IConnectableLayer* input = net->AddInputLayer(boost::numeric_cast<LayerBindingId>(0));
36 Connect(input, elementwiseUnaryLayer, inputTensorInfo, 0, 0);
37
38 TensorInfo outputTensorInfo(outputShape, ArmnnTypeInput, qScale, qOffset);
39 IConnectableLayer* output = net->AddOutputLayer(0, "output");
40 Connect(elementwiseUnaryLayer, output, outputTensorInfo, 0, 0);
41
42 return net;
43}
44
45template<armnn::DataType ArmnnInType,
46 typename TInput = armnn::ResolveType<ArmnnInType>>
47void ElementwiseUnarySimpleEndToEnd(const std::vector<BackendId>& backends,
48 UnaryOperation operation,
49 const std::vector<float> expectedOutput)
50{
51 using namespace armnn;
52
53 const float qScale = IsQuantizedType<TInput>() ? 0.25f : 1.0f;
54 const int32_t qOffset = IsQuantizedType<TInput>() ? 50 : 0;
55
56 const TensorShape& inputShape = { 2, 2, 2, 2 };
57 const TensorShape& outputShape = { 2, 2, 2, 2 };
58
59 // Builds up the structure of the network
60 INetworkPtr net = CreateElementwiseUnaryNetwork<ArmnnInType>(inputShape, outputShape, operation, qScale, qOffset);
61
62 BOOST_TEST_CHECKPOINT("create a network");
63
64 const std::vector<float> input({ 1, -1, 1, 1, 5, -5, 5, 5,
65 -3, 3, 3, 3, 4, 4, -4, 4 });
66
67 // quantize data
68 std::vector<TInput> qInputData = armnnUtils::QuantizedVector<TInput>(input, qScale, qOffset);
69 std::vector<TInput> qExpectedOutput = armnnUtils::QuantizedVector<TInput>(expectedOutput, qScale, qOffset);
70
71 std::map<int, std::vector<TInput>> inputTensorData = {{ 0, qInputData }};
72 std::map<int, std::vector<TInput>> expectedOutputData = {{ 0, qExpectedOutput }};
73
74 EndToEndLayerTestImpl<ArmnnInType, ArmnnInType>(move(net), inputTensorData, expectedOutputData, backends);
75}
76
77} // anonymous namespace