ArmNN
 22.05
OptimizeConsecutiveReshapesTests.cpp File Reference
#include <TestUtils.hpp>
#include <Optimizer.hpp>
#include <doctest/doctest.h>

Go to the source code of this file.

Functions

 TEST_SUITE ("Optimizer")
 

Function Documentation

◆ TEST_SUITE()

TEST_SUITE ( "Optimizer"  )

Definition at line 12 of file OptimizeConsecutiveReshapesTests.cpp.

References Graph::AddLayer(), Graph::cbegin(), Graph::cend(), CheckSequence(), armnn::Float32, Layer::GetOutputHandler(), Graph::InsertNewLayer(), armnn::MakeOptimizations(), Optimizer::Pass(), and OutputHandler::SetTensorInfo().

13 {
14 using namespace armnn::optimizations;
15 
16 TEST_CASE("OptimizeConsecutiveReshapesTest")
17 {
18  armnn::Graph graph;
19 
20  const armnn::TensorInfo info0({ 1, 2, 3, 5 }, armnn::DataType::Float32);
21 
22  auto output = graph.AddLayer<armnn::OutputLayer>(0, "output");
23  auto input = graph.InsertNewLayer<armnn::InputLayer>(output->GetInputSlot(0), 0, "input");
24 
25  input->GetOutputHandler().SetTensorInfo(info0);
26 
27  {
28  // Inserts two reshapes.
29  const armnn::TensorInfo info1({ 1, 30, 1, 1 }, armnn::DataType::Float32);
30  const armnn::TensorInfo info2({ 1, 2, 1, 15 }, armnn::DataType::Float32);
31 
32  std::string reshape1Name = "reshape1";
33  std::string reshape2Name = "reshape2";
34 
35  auto reshape1 = graph.InsertNewLayer<armnn::ReshapeLayer>(
36  output->GetInputSlot(0), armnn::ReshapeDescriptor{ info1.GetShape() }, reshape1Name.c_str());
37  auto reshape2 = graph.InsertNewLayer<armnn::ReshapeLayer>(
38  output->GetInputSlot(0), armnn::ReshapeDescriptor{ info2.GetShape() }, reshape2Name.c_str());
39 
40  reshape1->GetOutputHandler().SetTensorInfo(info1);
41  reshape2->GetOutputHandler().SetTensorInfo(info2);
42 
43  CHECK(CheckSequence(graph.cbegin(), graph.cend(), &IsLayerOfType<armnn::InputLayer>,
44  &IsLayerOfType<armnn::ReshapeLayer>, &IsLayerOfType<armnn::ReshapeLayer>,
45  &IsLayerOfType<armnn::OutputLayer>));
46 
48 
49  auto checkReshape = [&info2](const armnn::Layer* const layer) -> bool {
50  const auto reshapeLayer = static_cast<const armnn::ReshapeLayer*>(layer);
51  return IsLayerOfType<armnn::ReshapeLayer>(layer) &&
52  (reshapeLayer->GetParameters().m_TargetShape == info2.GetShape()) &&
53  (reshapeLayer->GetOutputHandler().GetTensorInfo().GetShape() == info2.GetShape());
54  };
55 
56  // The two reshapes are replaced by a single equivalent reshape.
57  CHECK(CheckSequence(graph.cbegin(), graph.cend(), &IsLayerOfType<armnn::InputLayer>, checkReshape,
58  &IsLayerOfType<armnn::OutputLayer>));
59 
60  // Check the new reshape layer has the other two reshapes as related layers
61  std::list<std::string> testRelatedLayers = { reshape2Name, reshape1Name };
62 
63  CHECK(CheckRelatedLayers<armnn::ReshapeLayer>(graph, testRelatedLayers));
64  }
65 
66  {
67  // Inserts a reshape to the input shape.
68  auto reshapeToIn = graph.InsertNewLayer<armnn::ReshapeLayer>(
69  output->GetInputSlot(0), armnn::ReshapeDescriptor{ info0.GetShape() }, "reshapeToIn");
70 
71  reshapeToIn->GetOutputHandler().SetTensorInfo(info0);
72 
74 
75  // The two reshapes are removed.
76  CHECK(CheckSequence(graph.cbegin(), graph.cend(), &IsLayerOfType<armnn::InputLayer>,
77  &IsLayerOfType<armnn::OutputLayer>));
78  }
79 }
80 
81 }
Optimizer::Optimizations MakeOptimizations(Args &&... args)
Definition: Optimizer.hpp:43
bool CheckSequence(const armnn::Graph::ConstIterator first, const armnn::Graph::ConstIterator last)
Definition: TestUtils.hpp:21
A ReshapeDescriptor for the ReshapeLayer.
LayerT * AddLayer(Args &&... args)
Adds a new layer, of type LayerType, to the graph constructed with the arguments passed.
Definition: Graph.hpp:425
ConstIterator cbegin() const
Returns const iterator pointing to the beginning of the list. Lowercase for range-based for loops...
Definition: Graph.hpp:179
static void Pass(Graph &graph, const Optimizations &optimizations)
Definition: Optimizer.cpp:16
This layer represents a reshape operation.
OptimizeForConnection< ReshapeLayer, ReshapeLayer, OptimizeConsecutiveReshapesImpl > OptimizeConsecutiveReshapes
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: OutputLayer.hpp:13
void SetTensorInfo(const TensorInfo &tensorInfo)
Sets the TensorInfo used by this output handler.
const OutputHandler & GetOutputHandler(unsigned int i=0) const
Definition: Layer.hpp:230
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: InputLayer.hpp:13
ConstIterator cend() const
Returns const iterator pointing to the end of the list. Lowercase for range-based for loops...
Definition: Graph.hpp:181
LayerT * InsertNewLayer(InputSlot &insertBefore, Args &&... args)
Inserts a new layer between the output slot currently connected to insertBefore and insertBefore itse...
Definition: Graph.hpp:440