blob: 439c083673dcf7c13617822d7a4b8f618cab459c [file] [log] [blame]
Narumol Prangnawarat8c7324d2019-05-31 16:42:11 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Sadik Armagana097d2a2021-11-24 15:47:28 +00008#include <CommonTestUtils.hpp>
Narumol Prangnawarat8c7324d2019-05-31 16:42:11 +01009
10#include <armnn/INetwork.hpp>
11#include <ResolveType.hpp>
12
Sadik Armagan1625efc2021-06-10 18:24:34 +010013#include <doctest/doctest.h>
14
Narumol Prangnawarat8c7324d2019-05-31 16:42:11 +010015namespace
16{
17
18template<typename T>
19armnn::INetworkPtr CreateDequantizeNetwork(const armnn::TensorInfo& inputInfo,
20 const armnn::TensorInfo& outputInfo)
21{
22 armnn::INetworkPtr net(armnn::INetwork::Create());
23
24 armnn::IConnectableLayer* inputLayer = net->AddInputLayer(0);
25 armnn::IConnectableLayer* dequantizeLayer = net->AddDequantizeLayer("Dequantize");
26 armnn::IConnectableLayer* outputLayer = net->AddOutputLayer(0, "output");
27 Connect(inputLayer, dequantizeLayer, inputInfo, 0, 0);
28 Connect(dequantizeLayer, outputLayer, outputInfo, 0, 0);
29
30 return net;
31}
32
33template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
34void DequantizeEndToEndLayerTestImpl(const std::vector<BackendId>& backends,
35 const armnn::TensorShape& tensorShape,
36 const std::vector<T>& input,
37 const std::vector<float>& expectedOutput,
38 float scale,
39 int32_t offset)
40{
41 armnn::TensorInfo inputInfo(tensorShape, ArmnnType);
42 armnn::TensorInfo outputInfo(tensorShape, armnn::DataType::Float32);
43
44 inputInfo.SetQuantizationScale(scale);
45 inputInfo.SetQuantizationOffset(offset);
Cathal Corbett5b8093c2021-10-22 11:12:07 +010046 inputInfo.SetConstant(true);
Narumol Prangnawarat8c7324d2019-05-31 16:42:11 +010047
48 // Builds up the structure of the network
49 armnn::INetworkPtr net = CreateDequantizeNetwork<T>(inputInfo, outputInfo);
50
Sadik Armagan1625efc2021-06-10 18:24:34 +010051 CHECK(net);
Narumol Prangnawarat8c7324d2019-05-31 16:42:11 +010052
53 std::map<int, std::vector<T>> inputTensorData = { { 0, input } };
54 std::map<int, std::vector<float>> expectedOutputData = { { 0, expectedOutput } };
55
56 EndToEndLayerTestImpl<ArmnnType, armnn::DataType::Float32>(
57 move(net), inputTensorData, expectedOutputData, backends);
58}
59
60template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
61void DequantizeEndToEndSimple(const std::vector<BackendId>& backends)
62{
63 const armnn::TensorShape tensorShape({ 1, 2, 2, 4 });
64 std::vector<T> inputData = std::vector<T>(
65 {
66 2, 4, 6, 8,
67 10, 12, 14, 16,
68 18, 20, 22, 24,
69 26, 28, 30, 32
70 });
71
72 std::vector<float> expectedOutputData = std::vector<float>(
73 {
74 1.0f, 2.0f, 3.0f, 4.0f,
75 5.0f, 6.0f, 7.0f, 8.0f,
76 9.0f, 10.0f, 11.0f, 12.0f,
77 13.0f, 14.0f, 15.0f, 16.0f
78 });
79 DequantizeEndToEndLayerTestImpl<ArmnnType>(backends, tensorShape, inputData, expectedOutputData, 0.5f, 0);
80};
81
82template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
83void DequantizeEndToEndOffset(const std::vector<BackendId>& backends)
84{
85 const armnn::TensorShape tensorShape({ 1, 2, 2, 4 });
86 std::vector<T> inputData = std::vector<T>(
87 {
88 3, 5, 7, 9,
89 11, 13, 15, 17,
90 19, 21, 23, 25,
91 27, 29, 31, 33
92 });
93
94 std::vector<float> expectedOutputData = std::vector<float>(
95 {
96 1.0f, 2.0f, 3.0f, 4.0f,
97 5.0f, 6.0f, 7.0f, 8.0f,
98 9.0f, 10.0f, 11.0f, 12.0f,
99 13.0f, 14.0f, 15.0f, 16.0f
100 });
101 DequantizeEndToEndLayerTestImpl<ArmnnType>(backends, tensorShape, inputData, expectedOutputData, 0.5f, 1);
102};
103
104} // anonymous namespace