ArmNN
 21.08
ReduceMultipleAxesTests.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "../GraphUtils.hpp"
7 #include "../TestUtils.hpp"
8 
9 #include <armnn/INetwork.hpp>
10 
11 #include <doctest/doctest.h>
12 
13 using namespace armnn;
14 
15 namespace
16 {
17 #if defined(ARMCOMPUTENEON_ENABLED)||defined(ARMCOMPUTECL_ENABLED)
18 INetworkPtr CreateSimpleReduceNetwork(ReduceDescriptor reduceDescriptor,
19  TensorShape& inputShape,
20  TensorShape& outputShape)
21 {
22  // Create a network
23  INetworkPtr network = INetwork::Create();
24 
25  const std::string layerName("reduce_layer");
26  const TensorInfo inputInfo(inputShape, DataType::Float32);
27  const TensorInfo outputInfo(outputShape, DataType::Float32);
28 
29  IConnectableLayer* const inputLayer = network->AddInputLayer(0);
30  IConnectableLayer* const reduceLayer = network->AddReduceLayer(reduceDescriptor, layerName.c_str());
31  IConnectableLayer* const outputLayer1 = network->AddOutputLayer(0);
32  IConnectableLayer* const outputLayer2 = network->AddOutputLayer(1);
33 
34  inputLayer->GetOutputSlot(0).SetTensorInfo(inputInfo);
35  reduceLayer->GetOutputSlot(0).SetTensorInfo(outputInfo);
36 
37  inputLayer->GetOutputSlot(0).Connect(reduceLayer->GetInputSlot(0));
38  reduceLayer->GetOutputSlot(0).Connect(outputLayer1->GetInputSlot(0));
39  reduceLayer->GetOutputSlot(0).Connect(outputLayer2->GetInputSlot(0));
40 
41  return network;
42 }
43 
44 void ReduceWithMultipleAxesTest(INetworkPtr& network,
45  const TensorShape& outputShape,
46  const std::vector<float>& inputData,
47  const std::vector<float>& expectedOutput,
48  const size_t numOfAxes,
49  Compute backendId)
50 {
51  // Create ArmNN runtime
53 
54  // Optimise ArmNN network
55  IOptimizedNetworkPtr optNet = Optimize(*network, {backendId}, run->GetDeviceSpec());
56 
57  Graph& graph = GetGraphForTesting(optNet.get());
58  if (numOfAxes == 2)
59  {
60  CHECK(graph.GetNumLayers() == 5);
61  CHECK(CheckSequence(graph.cbegin(),
62  graph.cend(),
63  &IsLayerOfType<InputLayer>,
64  &IsLayerOfType<ReduceLayer>,
65  &IsLayerOfType<ReduceLayer>,
66  &IsLayerOfType<OutputLayer>,
67  &IsLayerOfType<OutputLayer>));
68  } else
69  {
70  CHECK(graph.GetNumLayers() == 6);
71  CHECK(CheckSequence(graph.cbegin(),
72  graph.cend(),
73  &IsLayerOfType<InputLayer>,
74  &IsLayerOfType<ReduceLayer>,
75  &IsLayerOfType<ReduceLayer>,
76  &IsLayerOfType<ReduceLayer>,
77  &IsLayerOfType<OutputLayer>,
78  &IsLayerOfType<OutputLayer>));
79  }
80 
81  // Get last layer in new chain, layers name follow 0, 1, 2 pattern
82  std::string layerName = "reduce_layer_" + std::to_string(numOfAxes - 1);
83  Layer* const reduceLayer = GetFirstLayerWithName(graph, layerName);
84  CHECK(reduceLayer);
85  auto reduceTensorInfo = reduceLayer->GetOutputSlot().GetTensorInfo();
86 
87  // Tensorshape and the data type are correct
88  CHECK((reduceTensorInfo.GetShape() == outputShape));
89  CHECK((reduceTensorInfo.GetDataType() == DataType::Float32));
90 
91  // Load network into runtime
92  NetworkId networkIdentifier;
93  run->LoadNetwork(networkIdentifier, std::move(optNet));
94 
95  // Create input and output tensors
96  std::vector<float> outputData(expectedOutput.size());
97  InputTensors inputTensors
98  {
99  {0, armnn::ConstTensor(run->GetInputTensorInfo(networkIdentifier, 0), inputData.data())}
100  };
101  OutputTensors outputTensors
102  {
103  {0, armnn::Tensor(run->GetOutputTensorInfo(networkIdentifier, 0), outputData.data())},
104  {1, armnn::Tensor(run->GetOutputTensorInfo(networkIdentifier, 1), outputData.data())}
105  };
106 
107  // Run inference
108  run->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors);
109 
110  // Checks the results
111  CHECK(outputData == expectedOutput);
112 }
113 
114 void ReduceSumWithTwoAxesKeepDimsTest(Compute backendId)
115 {
116  armnn::ReduceDescriptor reduceDescriptor;
117  reduceDescriptor.m_vAxis = {1, 2};
118  reduceDescriptor.m_KeepDims = true;
120 
121  TensorShape inputShape = {1, 3, 2, 4};
122  TensorShape outputShape = {1, 1, 1, 4};
123 
124  // Construct ArmNN network
125  INetworkPtr network = CreateSimpleReduceNetwork(reduceDescriptor, inputShape, outputShape);
126 
127  // Creates structures for input & output.
128  const std::vector<float> inputData({1.0f, 2.0f, 3.0f, 4.0f,
129  5.0f, 6.0f, 7.0f, 8.0f,
130 
131  10.0f, 20.0f, 30.0f, 40.0f,
132  50.0f, 60.0f, 70.0f, 80.0f,
133 
134  100.0f, 200.0f, 300.0f, 400.0f,
135  500.0f, 600.0f, 700.0f, 800.0f});
136  const std::vector<float> expectedOutput({666.0f, 888.0f, 1110.0f, 1332.0f});
137 
138  ReduceWithMultipleAxesTest(network,
139  outputShape,
140  inputData,
141  expectedOutput,
142  reduceDescriptor.m_vAxis.size(),
143  backendId);
144 }
145 
146 void ReduceSumWithTwoAxesTest(Compute backendId)
147 {
148  armnn::ReduceDescriptor reduceDescriptor;
149  reduceDescriptor.m_vAxis = {1, 2};
150  reduceDescriptor.m_KeepDims = false;
152 
153  TensorShape inputShape = {1, 3, 2, 4};
154  TensorShape outputShape = {1, 4};
155 
156  // Construct ArmNN network
157  INetworkPtr network = CreateSimpleReduceNetwork(reduceDescriptor, inputShape, outputShape);
158 
159  // Creates structures for input & output.
160  const std::vector<float> inputData({1.0f, 2.0f, 3.0f, 4.0f,
161  5.0f, 6.0f, 7.0f, 8.0f,
162 
163  10.0f, 20.0f, 30.0f, 40.0f,
164  50.0f, 60.0f, 70.0f, 80.0f,
165 
166  100.0f, 200.0f, 300.0f, 400.0f,
167  500.0f, 600.0f, 700.0f, 800.0f});
168  const std::vector<float> expectedOutput({666.0f, 888.0f, 1110.0f, 1332.0f});
169 
170  ReduceWithMultipleAxesTest(network,
171  outputShape,
172  inputData,
173  expectedOutput,
174  reduceDescriptor.m_vAxis.size(),
175  backendId);
176 }
177 
178 void ReduceSumWithThreeAxesKeepDimsTest(Compute backendId)
179 {
180  armnn::ReduceDescriptor reduceDescriptor;
181  reduceDescriptor.m_vAxis = {0, 2, 3};
182  reduceDescriptor.m_KeepDims = true;
184 
185  TensorShape inputShape = {2, 2, 2, 2};
186  TensorShape outputShape = {1, 2, 1, 1};
187 
188  // Construct ArmNN network
189  INetworkPtr network = CreateSimpleReduceNetwork(reduceDescriptor, inputShape, outputShape);
190 
191  // Creates structures for input & output.
192  const std::vector<float> inputData({1.0f, 2.0f,
193  3.0f, 4.0f,
194 
195  5.0f, 6.0f,
196  7.0f, 8.0f,
197 
198  10.0f, 20.0f,
199  30.0f, 40.0f,
200 
201  50.0f, 60.0f,
202  70.0f, 80.0f});
203  const std::vector<float> expectedOutput({110.0f, 286.0f});
204 
205  ReduceWithMultipleAxesTest(network,
206  outputShape,
207  inputData,
208  expectedOutput,
209  reduceDescriptor.m_vAxis.size(),
210  backendId);
211 }
212 
213 void ReduceSumWithThreeAxesTest(Compute backendId)
214 {
215  armnn::ReduceDescriptor reduceDescriptor;
216  reduceDescriptor.m_vAxis = {0, 2, 3};
217  reduceDescriptor.m_KeepDims = false;
219 
220  TensorShape inputShape = {2, 2, 2, 2};
221  TensorShape outputShape = {2};
222 
223  // Construct ArmNN network
224  INetworkPtr network = CreateSimpleReduceNetwork(reduceDescriptor, inputShape, outputShape);
225 
226  // Creates structures for input & output.
227  const std::vector<float> inputData({1.0f, 2.0f,
228  3.0f, 4.0f,
229 
230  5.0f, 6.0f,
231  7.0f, 8.0f,
232 
233  10.0f, 20.0f,
234  30.0f, 40.0f,
235 
236  50.0f, 60.0f,
237  70.0f, 80.0f});
238  const std::vector<float> expectedOutput({110.0f, 286.0f});
239 
240  ReduceWithMultipleAxesTest(network,
241  outputShape,
242  inputData,
243  expectedOutput,
244  reduceDescriptor.m_vAxis.size(),
245  backendId);
246 }
247 #endif
248 }
249 
250 #if defined(ARMCOMPUTENEON_ENABLED)
251 TEST_SUITE("Optimizer_ReduceMultipleAxesCpu")
252 {
253 TEST_CASE("ReduceSumWithTwoAxesKeepDimsCpuAccTest")
254 {
255  ReduceSumWithTwoAxesKeepDimsTest(Compute::CpuAcc);
256 }
257 
258 TEST_CASE("ReduceSumWithTwoAxesCpuAccTest")
259 {
260  ReduceSumWithTwoAxesTest(Compute::CpuAcc);
261 }
262 
263 TEST_CASE("ReduceSumWithThreeAxesKeepDimsCpuAccTest")
264 {
265  ReduceSumWithThreeAxesKeepDimsTest(Compute::CpuAcc);
266 }
267 
268 TEST_CASE("ReduceSumWithThreeAxesCpuAccTest")
269 {
270  ReduceSumWithThreeAxesTest(Compute::CpuAcc);
271 }
272 }
273 #endif
274 
275 #if defined(ARMCOMPUTECL_ENABLED)
276 TEST_SUITE("Optimizer_ReduceMultipleAxesGpu")
277 {
278 TEST_CASE("ReduceSumWithTwoAxesKeepDimsGpuAccTest")
279 {
280  ReduceSumWithTwoAxesKeepDimsTest(Compute::GpuAcc);
281 }
282 
283 TEST_CASE("ReduceSumWithTwoAxesGpuAccTest")
284 {
285  ReduceSumWithTwoAxesTest(Compute::GpuAcc);
286 }
287 
288 TEST_CASE("ReduceSumWithThreeAxesKeepDimsGpuAccTest")
289 {
290  ReduceSumWithThreeAxesKeepDimsTest(Compute::GpuAcc);
291 }
292 
293 TEST_CASE("ReduceSumWithThreeAxesGpuAccTest")
294 {
295  ReduceSumWithThreeAxesTest(Compute::GpuAcc);
296 }
297 }
298 #endif
TEST_SUITE("TestConstTensorLayerVisitor")
static IRuntimePtr Create(const CreationOptions &options)
Definition: Runtime.cpp:39
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
armnn::Layer * GetFirstLayerWithName(armnn::Graph &graph, const std::string &name)
Definition: GraphUtils.cpp:22
bool m_KeepDims
if true then output shape has no change.
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:30
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:360
ReduceOperation m_ReduceOperation
Specifies the reduction operation to execute.
Copyright (c) 2021 ARM Limited and Contributors.
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition: Tensor.hpp:319
Compute
The Compute enum is now deprecated and it is now being replaced by BackendId.
Definition: BackendId.hpp:21
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
Definition: Network.cpp:1613
A ReduceDescriptor for the REDUCE operators.
int NetworkId
Definition: IRuntime.hpp:24
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:361
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:173
GPU Execution: OpenCL: ArmCompute.
std::vector< uint32_t > m_vAxis
The indices of the dimensions to reduce.
Graph & GetGraphForTesting(IOptimizedNetwork *optNet)
Definition: TestUtils.cpp:25
CPU Execution: NEON: ArmCompute.
bool CheckSequence(const armnn::Graph::ConstIterator first, const armnn::Graph::ConstIterator last)
Definition: TestUtils.hpp:21
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:318
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:172
virtual int Connect(IInputSlot &destination)=0
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63
static INetworkPtr Create(NetworkOptions networkOptions={})
Definition: Network.cpp:530