2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
8 #include "LayersFwd.hpp"
16 virtual void Run(Graph& graph, Layer& base) const = 0;
18 ~Optimization() = default;
22 // The implementation of the following wrappers make use of the CRTP C++ idiom
23 // (curiously recurring template pattern).
24 // For details, see https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
26 /// Wrapper Optimization base class that calls Wrapped::Run() for every layer of type BaseType.
27 /// - Wrapped class mustn't remove the base layer. The optimizer will remove it if left unconnected
28 /// after applying each optimization.
29 template <typename BaseType, typename Wrapped>
30 class OptimizeForTypeImpl : public armnn::Optimization, public Wrapped
33 using Wrapped::Wrapped;
35 void Run(Graph& graph, Layer& base) const override
37 if (base.GetType() == LayerEnumOf<BaseType>())
39 Wrapped::Run(graph, *boost::polymorphic_downcast<BaseType*>(&base));
44 ~OptimizeForTypeImpl() = default;
47 /// Specialization that calls Wrapped::Run() for any layer type
48 template <typename Wrapped>
49 class OptimizeForTypeImpl<Layer, Wrapped> : public armnn::Optimization, public Wrapped
52 using Wrapped::Wrapped;
54 void Run(Graph& graph, Layer& base) const override
56 Wrapped::Run(graph, base);
60 ~OptimizeForTypeImpl() = default;
63 template <typename BaseType, typename Wrapped>
64 class OptimizeForType final : public OptimizeForTypeImpl<BaseType, Wrapped>
67 using OptimizeForTypeImpl<BaseType, Wrapped>::OptimizeForTypeImpl;
70 /// Wrapper Optimization class that calls Wrapped::Run for every connection BaseType -> ChildType.
71 /// - Wrapped class mustn't remove the base layer. The optimizer will remove it if left unconnected
72 /// after applying each optimization.
73 /// - Wrapped class mustn't affect existing connections in the same output. It might add new ones.
74 /// - Children layers are removed if left unconnected after applying the wrapped optimization.
75 template <typename BaseType, typename ChildType, typename Wrapped>
76 class OptimizeForConnectionImpl : public Wrapped
79 using Wrapped::Wrapped;
81 void Run(Graph& graph, BaseType& base) const
83 for (auto output = base.BeginOutputSlots(); output != base.EndOutputSlots(); ++output)
85 for (auto&& childInput : output->GetConnections())
87 if (childInput->GetOwningLayer().GetType() == LayerEnumOf<ChildType>())
89 Wrapped::Run(graph, *childInput);
93 // Remove unconnected children
94 for (unsigned int i = 0; i < output->GetNumConnections();)
96 Layer* child = &output->GetConnection(i)->GetOwningLayer();
98 if (child->IsOutputUnconnected())
100 graph.EraseLayer(child);
111 ~OptimizeForConnectionImpl() = default;
114 template <typename BaseType, typename ChildType, typename Wrapped>
115 class OptimizeForConnection final
116 : public OptimizeForTypeImpl<BaseType, OptimizeForConnectionImpl<BaseType, ChildType, Wrapped>>
119 using OptimizeForTypeImpl<BaseType, OptimizeForConnectionImpl<BaseType, ChildType, Wrapped>>::OptimizeForTypeImpl;