Compute Library
 21.08
GroupedConvolutionMutator Class Referencefinal

Mutation pass to implement/optimize grouped convolutions. More...

#include <GroupedConvolutionMutator.h>

Collaboration diagram for GroupedConvolutionMutator:
[legend]

Public Member Functions

virtual void mutate (Graph &g) override
 Walk the graph and perform a specific mutation. More...
 
MutationType type () const override
 Returns mutation type. More...
 
const char * name () override
 Returns mutator name. More...
 
- Public Member Functions inherited from IGraphMutator
virtual ~IGraphMutator ()=default
 Virtual Destructor. More...
 

Additional Inherited Members

- Public Types inherited from IGraphMutator
enum  MutationType { IR, Backend }
 Mutation type. More...
 

Detailed Description

Mutation pass to implement/optimize grouped convolutions.

Warning
This is compulsory to run in case of grouped convolutions

Definition at line 37 of file GroupedConvolutionMutator.h.

Member Function Documentation

◆ mutate()

void mutate ( Graph g)
overridevirtual

Walk the graph and perform a specific mutation.

Parameters
[in,out]gGraph to walk and mutate

Implements IGraphMutator.

Definition at line 113 of file GroupedConvolutionMutator.cpp.

References Graph::add_connection(), ARM_COMPUTE_ERROR_ON, INode::assigned_target(), arm_compute::graph::configure_tensor(), arm_compute::test::validation::conv_info, arm_compute::graph::ConvolutionLayer, arm_compute::graph::EmptyNodeID, arm_compute::utility::for_each(), BackendRegistry::get(), BackendRegistry::get_backend(), arm_compute::graph::get_driving_nodes(), INode::id(), Graph::node(), Graph::nodes(), arm_compute::test::validation::num_groups, INode::output(), Graph::remove_node(), Tensor::set_accessor(), tf_frozen_model_extractor::t, Graph::tensors(), INode::type(), and IDeviceBackend::validate_node().

114 {
115  // Early exit if no Convolution layers exist in graph
116  if(g.nodes(NodeType::ConvolutionLayer).empty())
117  {
118  return;
119  }
120 
121  // Total nodes
122  size_t total_nodes = g.nodes().size();
123 
124  // Iterate over convolution nodes
125  for(unsigned int i = 0; i < total_nodes; ++i)
126  {
127  INode *node = g.node(i);
128  if(node != nullptr && node->type() == NodeType::ConvolutionLayer && arm_compute::utils::cast::polymorphic_downcast<ConvolutionLayerNode *>(node)->num_groups() != 1)
129  {
130  // Validate node
131  backends::IDeviceBackend &backend = backends::BackendRegistry::get().get_backend(node->assigned_target());
132  Status status = backend.validate_node(*node);
133 
134  // If grouped convolution is not supported
135  if(!bool(status))
136  {
137  // Down-cast node
138  auto *conv_node = arm_compute::utils::cast::polymorphic_downcast<ConvolutionLayerNode *>(node);
139 
140  // Get internal convolution info
141  // TODO (geopin01) : Create a descriptor or a clone interface
142  const PadStrideInfo conv_info = conv_node->convolution_info();
143  const ConvolutionMethod conv_method = conv_node->convolution_method();
144  const ActivationLayerInfo fused_act_info = conv_node->fused_activation();
145  const FastMathHint fast_math_hint = conv_node->fast_math_hint();
146  const unsigned int num_groups = conv_node->num_groups();
147  const NodeParams params = conv_node->common_node_params();
148  const Target assigned_target = conv_node->assigned_target();
149 
150  // Extract node ids
151  ARM_COMPUTE_ERROR_ON(conv_node->input_edge(0) == nullptr || conv_node->input_edge(1) == nullptr);
152  const NodeID input_id = conv_node->input_edge(0)->producer()->id();
153  const NodeID weights_id = conv_node->input_edge(1)->producer()->id();
154  const NodeID bias_id = (conv_node->input_edge(2) != nullptr) ? conv_node->input_edge(2)->producer()->id() : EmptyNodeID;
155 
156  // Get driving nodes
157  std::vector<NodeIdxPair> driving_nodes = get_driving_nodes(*node);
158 
159  // Extract activation node accessor if any
160  auto node_accessor = conv_node->output(0)->extract_accessor();
161 
162  // Current max tensor and node id
163  TensorID latest_tid = g.tensors().size();
164  NodeID latest_nid = g.nodes().size();
165 
166  // Create grouped convolution node
167  NodeID grouped_conv_id = create_grouped_convolution(g, params, { input_id, 0 }, weights_id, bias_id,
168  conv_info, conv_method, fused_act_info, fast_math_hint, num_groups);
169 
170  // Remove convolution node
171  g.remove_node(node->id());
172 
173  // Update batch normalization node outputs
174  for(auto &driving_node : driving_nodes)
175  {
176  g.add_connection(grouped_conv_id, 0, driving_node.node_id, driving_node.index);
177  }
178 
179  // Update accessor to batch normalization node
180  g.node(grouped_conv_id)->output(0)->set_accessor(std::move(node_accessor));
181 
182  // Configure new tensors and nodes
183  std::for_each(g.tensors().begin() + latest_tid, g.tensors().end(), [](std::unique_ptr<Tensor> &t)
184  {
185  configure_tensor(t.get());
186  });
187  std::for_each(g.nodes().begin() + latest_nid, g.nodes().end(), [&assigned_target](std::unique_ptr<INode> &n)
188  {
189  if(n != nullptr)
190  {
191  n->set_assigned_target(assigned_target);
192  }
193  });
194  }
195  }
196  }
197 }
void configure_tensor(Tensor *tensor)
Configures tensor.
Definition: Utils.cpp:186
std::vector< NodeIdxPair > get_driving_nodes(const INode &node)
Get the list of driving nodes of a given node.
Definition: Utils.cpp:166
IDeviceBackend & get_backend(Target target)
Get a backend from the registry.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
const unsigned int num_groups
Definition: Im2Col.cpp:153
static BackendRegistry & get()
Gets backend registry instance.
void for_each(F &&)
Base case of for_each.
Definition: Utility.h:110
FastMathHint
Enable or disable fast math for Convolution layer.
Definition: Types.h:142
virtual Status validate_node(INode &node)=0
Validate a node.
unsigned int NodeID
Definition: Types.h:68
constexpr NodeID EmptyNodeID
Constant EdgeID specifying an equivalent of null edge.
Definition: Types.h:75
ConvolutionMethod
Supported Convolution layer methods.
Definition: Types.h:125
unsigned int TensorID
Definition: Types.h:67

◆ name()

const char * name ( )
overridevirtual

Returns mutator name.

Returns
Mutator name

Implements IGraphMutator.

Definition at line 103 of file GroupedConvolutionMutator.cpp.

104 {
105  return "GroupedConvolutionMutator";
106 }

◆ type()

IGraphMutator::MutationType type ( ) const
overridevirtual

Returns mutation type.

Returns
Mutation type enumeration

Implements IGraphMutator.

Definition at line 108 of file GroupedConvolutionMutator.cpp.

References IGraphMutator::Backend.


The documentation for this class was generated from the following files: