ONE - On-device Neural Engine
Loading...
Searching...
No Matches
logo::RemoveDeadNodePass Struct Referencefinal

#include <RemoveDeadNodePass.h>

Collaboration diagram for logo::RemoveDeadNodePass:

Public Member Functions

const char * name (void) const final
 
bool run (loco::Graph *g)
 Run the pass.
 
- Public Member Functions inherited from logo::Pass
virtual ~Pass ()=default
 

Detailed Description

Definition at line 25 of file RemoveDeadNodePass.h.

Member Function Documentation

◆ name()

const char * logo::RemoveDeadNodePass::name ( void  ) const
inlinefinalvirtual

Reimplemented from logo::Pass.

Definition at line 27 of file RemoveDeadNodePass.h.

27{ return "RemoveDeadNodePass"; }

◆ run()

bool logo::RemoveDeadNodePass::run ( loco::Graph graph)
virtual

Run the pass.

Returns
false if there was nothing changed

Implements logo::Pass.

Definition at line 28 of file RemoveDeadNodePass.cpp.

29{
30 // Let's enumerate nodes required to compute output nodes
32
33 // Find dead(= non-active) nodes
34 std::set<loco::Node *> candidates;
35
36 for (auto node : loco::all_nodes(g))
37 {
38 if (active_nodes.find(node) == active_nodes.end())
39 {
40 candidates.insert(node);
41 }
42 }
43
44 // Let's drop the references from each dead node first and then remove these dead nodes
45 //
46 // Why?
47 //
48 // Let us consider the following example:
49 // %0 = Pull(...)
50 // %1 = ConstGen(...)
51 // %2 = Forward(input: %1)
52 // %3 = Push(from: %0) <- OUTPUT
53 //
54 // Forward (%2) is dead as it does not contribute to the final result (%3). However, it
55 // refers to another dead node (%1).
56 //
57 // This example indicates that naive implementation results in dangling references.
58 //
59 // There are two possible solutions:
60 // 1. Destroy nodes in topological order
61 // 2. Drop the reference first and then destroy them
62 //
63 // The current implementation takes the latter approach for the simplicity of implementation.
64 for (auto node : candidates)
65 {
66 node->drop();
67 }
68
69 for (auto node : candidates)
70 {
71 g->nodes()->destroy(node);
72 }
73
74 return candidates.size() > 0;
75}
std::set< Node * > all_nodes(Graph *)
Enumerate all the nodes in a given graph.
Definition Graph.cpp:59
std::set< loco::Node * > active_nodes(const std::vector< loco::Node * > &roots)
Enumerate all the nodes required to compute "roots".
std::vector< Node * > output_nodes(Graph *)
Definition Graph.cpp:101

References loco::active_nodes(), loco::all_nodes(), and loco::output_nodes().

Referenced by package.infer.session::inference().


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