ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnc::opt_util Namespace Reference

Functions

void swapAdjacent (mir::Graph *g, mir::Operation *top, mir::Operation *bottom)
 Swap adjacent nodes in Graph. Creates new nodes and replaces the old ones with new.
 
void removeNodeIfUnused (mir::Graph *g, mir::Operation *op)
 

Function Documentation

◆ removeNodeIfUnused()

void nnc::opt_util::removeNodeIfUnused ( mir::Graph g,
mir::Operation op 
)

Definition at line 48 of file OptimizationUtils.cpp.

49{
50 if (op->getOutput(0)->getUses().empty())
51 g->removeNode(op);
52}
const std::vector< Use > & getUses() const
Returns the inputs that consume this output.
Definition Operation.h:79
Output * getOutput(std::size_t index)
Definition Operation.h:149

References mir::Operation::getOutput(), and mir::Operation::Output::getUses().

Referenced by nnc::ConstantFoldTranspose::run(), nnc::SinkRelu::run(), and nnc::SinkTranspose::run().

◆ swapAdjacent()

void nnc::opt_util::swapAdjacent ( mir::Graph g,
mir::Operation top,
mir::Operation bottom 
)

Swap adjacent nodes in Graph. Creates new nodes and replaces the old ones with new.

Parameters
gMIR Graph
topNode
bottomNode

Definition at line 23 of file OptimizationUtils.cpp.

24{
25 assert(top->getNumInputs() == bottom->getNumInputs() && top->getNumInputs() == 1 &&
26 top->getNumInputs() == top->getNumOutputs() &&
27 top->getNumInputs() == bottom->getNumOutputs() && "incompatible ops");
28 const auto &ins = top->getInputs();
29 std::vector<mir::Operation::Output *> prods;
30 prods.reserve(top->getNumInputs());
31 for (mir::Operation::Output *in : ins)
32 {
33 prods.emplace_back(in);
34 }
35 mir::Operation *new_bottom = g->copyOpWithInputs(bottom, prods);
36 prods.clear();
37 prods.reserve(new_bottom->getNumOutputs());
38 for (mir::Operation::Output &out : new_bottom->getOutputs())
39 {
40 prods.emplace_back(&out);
41 }
42 mir::Operation *new_top = g->copyOpWithInputs(top, prods);
43 g->replaceNode(bottom, new_top);
44 g->replaceNode(top, new_bottom);
45}
Represents an output of a node.
Definition Operation.h:60
std::size_t getNumInputs() const
Definition Operation.h:128
std::deque< Output * > & getInputs()
Definition Operation.h:131
std::size_t getNumOutputs() const
Definition Operation.h:129

References mir::Operation::getInputs(), mir::Operation::getNumInputs(), mir::Operation::getNumOutputs(), and mir::Operation::getOutputs().

Referenced by nnc::SinkRelu::run(), and nnc::SinkTranspose::run().