ArmNN
 22.05.01
PermuteAsReshapeTests.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include <TestUtils.hpp>
7 
8 #include <Optimizer.hpp>
9 
10 #include <doctest/doctest.h>
11 
12 using namespace armnn;
13 
14 TEST_SUITE("Optimizer")
15 {
16 using namespace armnn::optimizations;
17 
18 TEST_CASE("PermuteAsReshapeTest")
19 {
20  armnn::Graph graph;
21 
22  std::string permuteLayerName = "permute";
23 
24  const armnn::TensorInfo infoIn({ 1, 2, 3, 1 }, armnn::DataType::Float32);
25  const armnn::TensorInfo infoOut({ 1, 1, 2, 3 }, armnn::DataType::Float32);
26 
27  auto output = graph.AddLayer<armnn::OutputLayer>(0, "output");
28 
29  graph.InsertNewLayer<armnn::InputLayer>(output->GetInputSlot(0), 0, "input")
30  ->GetOutputHandler()
31  .SetTensorInfo(infoIn);
32 
33  // Inserts permute.
34  graph
35  .InsertNewLayer<armnn::PermuteLayer>(output->GetInputSlot(0), armnn::PermuteDescriptor({ 0, 2, 3, 1 }),
36  permuteLayerName.c_str())
37  ->GetOutputHandler()
38  .SetTensorInfo(infoOut);
39 
40  CHECK(CheckSequence(graph.cbegin(), graph.cend(), &IsLayerOfType<armnn::InputLayer>,
41  &IsLayerOfType<armnn::PermuteLayer>, &IsLayerOfType<armnn::OutputLayer>));
42 
44 
45  // The permute is replaced by an equivalent reshape.
46 
47  auto checkReshape = [&infoOut](const armnn::Layer* const layer) -> bool {
48  const auto reshapeLayer = static_cast<const armnn::ReshapeLayer*>(layer);
49  return IsLayerOfType<armnn::ReshapeLayer>(layer) &&
50  (reshapeLayer->GetParameters().m_TargetShape == infoOut.GetShape()) &&
51  (reshapeLayer->GetOutputHandler().GetTensorInfo().GetShape() == infoOut.GetShape());
52  };
53 
54  CHECK(CheckSequence(graph.cbegin(), graph.cend(), &IsLayerOfType<armnn::InputLayer>, checkReshape,
55  &IsLayerOfType<armnn::OutputLayer>));
56 
57  std::list<std::string> testRelatedLayers = { permuteLayerName };
58  CHECK(CheckRelatedLayers<armnn::ReshapeLayer>(graph, testRelatedLayers));
59 }
60 
61 }
TEST_SUITE("TestConstTensorLayerVisitor")
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
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.
Copyright (c) 2021 ARM Limited and Contributors.
This layer represents a permutation operation.
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: OutputLayer.hpp:13
OptimizeForType< PermuteLayer, PermuteAsReshapeImpl > PermuteAsReshape
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
A PermuteDescriptor for the PermuteLayer.