ArmNN
 21.08
GatherEndToEndTestImpl.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "CommonTestUtils.hpp"
9 
10 #include <armnn/INetwork.hpp>
11 #include <ResolveType.hpp>
12 
13 #include <doctest/doctest.h>
14 
15 namespace{
16 
17 armnn::INetworkPtr CreateGatherNetwork(const armnn::TensorInfo& paramsInfo,
18  const armnn::TensorInfo& indicesInfo,
19  const armnn::TensorInfo& outputInfo,
20  const std::vector<int32_t>& indicesData)
21 {
23 
24  armnn::GatherDescriptor descriptor;
25  armnn::IConnectableLayer* paramsLayer = net->AddInputLayer(0);
26  armnn::IConnectableLayer* indicesLayer = net->AddConstantLayer(armnn::ConstTensor(indicesInfo, indicesData));
27  armnn::IConnectableLayer* gatherLayer = net->AddGatherLayer(descriptor, "gather");
28  armnn::IConnectableLayer* outputLayer = net->AddOutputLayer(0, "output");
29  Connect(paramsLayer, gatherLayer, paramsInfo, 0, 0);
30  Connect(indicesLayer, gatherLayer, indicesInfo, 0, 1);
31  Connect(gatherLayer, outputLayer, outputInfo, 0, 0);
32 
33  return net;
34 }
35 
36 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
37 void GatherEndToEnd(const std::vector<BackendId>& backends)
38 {
39  armnn::TensorInfo paramsInfo({ 8 }, ArmnnType);
40  armnn::TensorInfo indicesInfo({ 3 }, armnn::DataType::Signed32);
41  armnn::TensorInfo outputInfo({ 3 }, ArmnnType);
42 
43  paramsInfo.SetQuantizationScale(1.0f);
44  paramsInfo.SetQuantizationOffset(0);
45  outputInfo.SetQuantizationScale(1.0f);
46  outputInfo.SetQuantizationOffset(0);
47 
48  // Creates structures for input & output.
49  std::vector<T> paramsData{
50  1, 2, 3, 4, 5, 6, 7, 8
51  };
52 
53  std::vector<int32_t> indicesData{
54  7, 6, 5
55  };
56 
57  std::vector<T> expectedOutput{
58  8, 7, 6
59  };
60 
61  // Builds up the structure of the network
62  armnn::INetworkPtr net = CreateGatherNetwork(paramsInfo, indicesInfo, outputInfo, indicesData);
63 
64  CHECK(net);
65 
66  std::map<int, std::vector<T>> inputTensorData = {{ 0, paramsData }};
67  std::map<int, std::vector<T>> expectedOutputData = {{ 0, expectedOutput }};
68 
69  EndToEndLayerTestImpl<ArmnnType, ArmnnType>(move(net), inputTensorData, expectedOutputData, backends);
70 }
71 
72 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
73 void GatherMultiDimEndToEnd(const std::vector<BackendId>& backends)
74 {
75  armnn::TensorInfo paramsInfo({ 3, 2, 3}, ArmnnType);
76  armnn::TensorInfo indicesInfo({ 2, 3 }, armnn::DataType::Signed32);
77  armnn::TensorInfo outputInfo({ 2, 3, 2, 3 }, ArmnnType);
78 
79  paramsInfo.SetQuantizationScale(1.0f);
80  paramsInfo.SetQuantizationOffset(0);
81  outputInfo.SetQuantizationScale(1.0f);
82  outputInfo.SetQuantizationOffset(0);
83 
84  // Creates structures for input & output.
85  std::vector<T> paramsData{
86  1, 2, 3,
87  4, 5, 6,
88 
89  7, 8, 9,
90  10, 11, 12,
91 
92  13, 14, 15,
93  16, 17, 18
94  };
95 
96  std::vector<int32_t> indicesData{
97  1, 2, 1,
98  2, 1, 0
99  };
100 
101  std::vector<T> expectedOutput{
102  7, 8, 9,
103  10, 11, 12,
104  13, 14, 15,
105  16, 17, 18,
106  7, 8, 9,
107  10, 11, 12,
108 
109  13, 14, 15,
110  16, 17, 18,
111  7, 8, 9,
112  10, 11, 12,
113  1, 2, 3,
114  4, 5, 6
115  };
116 
117  // Builds up the structure of the network
118  armnn::INetworkPtr net = CreateGatherNetwork(paramsInfo, indicesInfo, outputInfo, indicesData);
119 
120  std::map<int, std::vector<T>> inputTensorData = {{ 0, paramsData }};
121  std::map<int, std::vector<T>> expectedOutputData = {{ 0, expectedOutput }};
122 
123  EndToEndLayerTestImpl<ArmnnType, ArmnnType>(move(net), inputTensorData, expectedOutputData, backends);
124 }
125 
126 } // anonymous namespace
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
A GatherDescriptor for the GatherLayer.
void SetQuantizationScale(float scale)
Definition: Tensor.cpp:475
void SetQuantizationOffset(int32_t offset)
Definition: Tensor.cpp:491
void Connect(armnn::IConnectableLayer *from, armnn::IConnectableLayer *to, const armnn::TensorInfo &tensorInfo, unsigned int fromIndex, unsigned int toIndex)
Definition: TestUtils.cpp:12
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:172
static INetworkPtr Create(NetworkOptions networkOptions={})
Definition: Network.cpp:530