ArmNN
 21.02
OptimizeConsecutiveReshapesTests.cpp File Reference
#include "../TestUtils.hpp"
#include <Optimizer.hpp>
#include <boost/test/unit_test.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (OptimizeConsecutiveReshapesTest)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE()

BOOST_AUTO_TEST_CASE ( OptimizeConsecutiveReshapesTest  )

Definition at line 15 of file OptimizeConsecutiveReshapesTests.cpp.

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

16 {
17  armnn::Graph graph;
18 
19  const armnn::TensorInfo info0({ 1, 2, 3, 5 }, armnn::DataType::Float32);
20 
21  auto output = graph.AddLayer<armnn::OutputLayer>(0, "output");
22  auto input = graph.InsertNewLayer<armnn::InputLayer>(output->GetInputSlot(0), 0, "input");
23 
24  input->GetOutputHandler().SetTensorInfo(info0);
25 
26  {
27  // Inserts two reshapes.
28  const armnn::TensorInfo info1({ 1, 30, 1, 1 }, armnn::DataType::Float32);
29  const armnn::TensorInfo info2({ 1, 2, 1, 15 }, armnn::DataType::Float32);
30 
31  std::string reshape1Name = "reshape1";
32  std::string reshape2Name = "reshape2";
33 
34  auto reshape1 = graph.InsertNewLayer<armnn::ReshapeLayer>(
35  output->GetInputSlot(0), armnn::ReshapeDescriptor{ info1.GetShape() }, reshape1Name.c_str());
36  auto reshape2 = graph.InsertNewLayer<armnn::ReshapeLayer>(
37  output->GetInputSlot(0), armnn::ReshapeDescriptor{ info2.GetShape() }, reshape2Name.c_str());
38 
39  reshape1->GetOutputHandler().SetTensorInfo(info1);
40  reshape2->GetOutputHandler().SetTensorInfo(info2);
41 
42  BOOST_TEST(CheckSequence(graph.cbegin(), graph.cend(), &IsLayerOfType<armnn::InputLayer>,
43  &IsLayerOfType<armnn::ReshapeLayer>, &IsLayerOfType<armnn::ReshapeLayer>,
44  &IsLayerOfType<armnn::OutputLayer>));
45 
47 
48  auto checkReshape = [&info2](const armnn::Layer* const layer) -> bool {
49  const auto reshapeLayer = static_cast<const armnn::ReshapeLayer*>(layer);
50  return IsLayerOfType<armnn::ReshapeLayer>(layer) &&
51  (reshapeLayer->GetParameters().m_TargetShape == info2.GetShape()) &&
52  (reshapeLayer->GetOutputHandler().GetTensorInfo().GetShape() == info2.GetShape());
53  };
54 
55  // The two reshapes are replaced by a single equivalent reshape.
56  BOOST_TEST(CheckSequence(graph.cbegin(), graph.cend(), &IsLayerOfType<armnn::InputLayer>, checkReshape,
57  &IsLayerOfType<armnn::OutputLayer>));
58 
59  // Check the new reshape layer has the other two reshapes as related layers
60  std::list<std::string> testRelatedLayers = { reshape2Name, reshape1Name };
61 
62  BOOST_TEST(CheckRelatedLayers<armnn::ReshapeLayer>(graph, testRelatedLayers));
63  }
64 
65  {
66  // Inserts a reshape to the input shape.
67  auto reshapeToIn = graph.InsertNewLayer<armnn::ReshapeLayer>(
68  output->GetInputSlot(0), armnn::ReshapeDescriptor{ info0.GetShape() }, "reshapeToIn");
69 
70  reshapeToIn->GetOutputHandler().SetTensorInfo(info0);
71 
73 
74  // The two reshapes are removed.
75  BOOST_TEST(CheckSequence(graph.cbegin(), graph.cend(), &IsLayerOfType<armnn::InputLayer>,
76  &IsLayerOfType<armnn::OutputLayer>));
77  }
78 }
Optimizer::Optimizations MakeOptimizations(Args &&... args)
Definition: Optimizer.hpp:43
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:402
ConstIterator cbegin() const
Returns const iterator pointing to the beginning of the list. Lowercase for range-based for loops...
Definition: Graph.hpp:172
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:225
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: InputLayer.hpp:13
bool CheckSequence(const armnn::Graph::ConstIterator first, const armnn::Graph::ConstIterator last)
Definition: TestUtils.hpp:21
ConstIterator cend() const
Returns const iterator pointing to the end of the list. Lowercase for range-based for loops...
Definition: Graph.hpp:174
LayerT * InsertNewLayer(InputSlot &insertBefore, Args &&... args)
Inserts a new layer between the output slot currently connected to insertBefore and insertBefore itse...
Definition: Graph.hpp:416