HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
onnxruntime::RewriteRule Class Referenceabstract

#include <rewrite_rule.h>

Public Types

enum  RewriteRuleEffect : uint8_t { RewriteRuleEffect::kNone, RewriteRuleEffect::kUpdatedCurrentNode, RewriteRuleEffect::kRemovedCurrentNode, RewriteRuleEffect::kModifiedRestOfGraph }
 

Public Member Functions

 RewriteRule (const std::string &name)
 
virtual ~RewriteRule ()=default
 
const std::stringName () const noexcept
 
virtual std::vector< std::stringTargetOpTypes () const noexcept=0
 
common::Status CheckConditionAndApply (Graph &graph, Node &node, RewriteRuleEffect &rule_effect, const logging::Logger &logger) const
 

Detailed Description

The base class for a rewrite rule. A rewrite rule represents a semantics-preserving transformation of a computation graph. It can be used to represent, for example, the elimination of operators that serve as no-ops (e.g., dropout during inference), as well as inlining of "function" definitions or the dual operation of replacing a complex expression by an equivalent function-call). Unlike the more general GraphTransformer, a rewrite rule is a more local transformation that is triggered on a particular node of the graph.

Each rule has a set of conditions and a body. The conditions have to be satisfied for the body of the rule to be triggered. Therefore, when creating a new rewrite rule, two main functions have to be implemented:

  • SatisfyCondition defines the condition checks. It is advisable to add the more selective checks first, because those will lead to discarding fast rules that cannot be applied on a node.
  • Apply is the actual body of the rule that will be executed if SatisfyCondition returns true for a particular node. Note that additional, more complex checks can be included in the Apply if putting them in the SatisfyCondition would lead to duplicate work (e.g., when we make a check on a Node attribute but we need that attribute to execute the rule too). In general, simple fast checks are a better fit for SatisfyCondition, whereas more complex ones can be added in the Apply.

In order to avoid evaluating the SatisfyCondition for each rule and each node of the graph, each rewrite rule should specify the target op types for which a rule will be evaluated, by overriding the TargetOpTypes() function. If the op type of a node is not included in the target op types of a rule, that rule would not be considered at all. If the list of op types is left empty, that rule will be triggered for every op type.

Definition at line 36 of file rewrite_rule.h.

Member Enumeration Documentation

Enumerator
kNone 
kUpdatedCurrentNode 
kRemovedCurrentNode 
kModifiedRestOfGraph 

Definition at line 43 of file rewrite_rule.h.

Constructor & Destructor Documentation

onnxruntime::RewriteRule::RewriteRule ( const std::string name)
inline

Definition at line 50 of file rewrite_rule.h.

virtual onnxruntime::RewriteRule::~RewriteRule ( )
virtualdefault

Member Function Documentation

common::Status onnxruntime::RewriteRule::CheckConditionAndApply ( Graph graph,
Node node,
RewriteRuleEffect rule_effect,
const logging::Logger logger 
) const
inline

Checks if the condition of the rule is satisfied, and if so applies the body of the rule.

Parameters
[in]graphThe Graph.
[in]nodeThe Node to apply the rewrite to.
[out]rule_effectEnum to indicate if and how the graph was modified as a result of the rule application.
Returns
Status indicating success or providing error information

Definition at line 69 of file rewrite_rule.h.

const std::string& onnxruntime::RewriteRule::Name ( ) const
inlinenoexcept

Gets the name of this rewrite rule.

Definition at line 55 of file rewrite_rule.h.

virtual std::vector<std::string> onnxruntime::RewriteRule::TargetOpTypes ( ) const
pure virtualnoexcept

Returns the node op types for which this rule will be triggered. If the op type of a node is not included in the target op types of a rule, that rule would not be considered at all. Returning an empty list indicates that we will attempt to trigger the rule for every op type.


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