pattern_optimization.h#

namespace onnx_light

Alias that makes onnx-light headers compatible with code that references ONNX_LIGHT_NAMESPACE (the macro used in the standard onnx package).

Set to ONNX_LIGHT_NAMESPACE so both names resolve to the same namespace.

Symbol-visibility attribute for the public onnx-light C++ API.

Maps the upstream compatibility macro to onnx-light’s explicit proto ABI annotation. This keeps declarations from vendored ONNX headers visible when lib_onnx_proto uses hidden visibility by default.

Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal onnx namespace — rather than the ONNX_NAMESPACE macro — resolves to the onnx-light namespace. The standard onnx package lives in namespace onnx; onnx-light uses onnx_light (via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs from onnx.

namespace core
namespace builder
struct LocalRewriting#
#include <pattern_optimization.h>

Persistent description of one applied local graph rewrite.

Public Functions

std::string ToString() const#

Returns a concise summary of this rewrite.

Public Members

std::shared_ptr<const PatternOptimization> pattern#

Shared link to the pattern that produced the rewrite.

std::vector<std::string> graph_path#

Nested builder path from the root graph; empty means the root graph.

std::vector<std::size_t> matched_nodes#

Positions of the nodes selected by the match.

Positions refer to the graph at the start of the rewrite batch identified by iteration, not to the original model: a later batch may match nodes added by an earlier one. This batch-local coordinate system is also used by :cpp:var:insert_at.

utils::RepeatedProtoField<NodeProto> added_nodes#

Replacement nodes owned by this record.

std::vector<std::size_t> added_nodes_positions#

Positions occupied by added nodes after this rewrite batch.

utils::RepeatedProtoField<TensorProto> added_initializers#

Initializers created while applying the pattern.

std::vector<std::size_t> added_initializer_positions#

Positions occupied by added initializers after this rewrite batch.

std::vector<std::size_t> removed_initializers#

Positions of initializers removed by this rewrite.

std::vector<std::pair<std::string, std::string>> value_renames#

Value-name replacements applied to node inputs and nested captures.

std::size_t iteration = 0#

Ordered rewrite batch; records with the same value are applied together.

int64_t match_time_ns = 0#

Time spent recognizing this match, in nanoseconds.

int64_t apply_time_ns = 0#

Time spent building its replacement nodes, in nanoseconds.

struct MatchResult#
#include <pattern_optimization.h>

Describes one subgraph recognized by an optimization pattern.

Public Functions

std::string ToString() const#

Returns a concise summary of this match.

Public Members

const PatternOptimization *pattern = nullptr#

Pattern that produced this match.

std::vector<const NodeProto*> nodes#

Nodes involved in the rewrite, in the order expected by Apply.

A null pointer is an ignored positional placeholder for an optional role. It is not positioned, marked, removed, or persisted in the rewrite.

const NodeProto *insert_at = nullptr#

Optional node before which the replacement should be inserted.

std::optional<PatternNoMatch> no_match = std::nullopt#

Diagnostic attached to an empty result, when the pattern supplied one.

struct OptimizationReport#
#include <pattern_optimization.h>

Optional timing report populated by :cpp:func:GraphGraph::Optimize.

Public Functions

int64_t TotalTimeNs() const noexcept#

Returns the sum of all phase durations, in nanoseconds.

std::string ToString() const#

Returns a concise, printable optimization report.

Public Members

std::size_t iterations = 0#

Number of optimization iterations executed.

std::size_t rewrites = 0#

Number of local rewrites applied.

int64_t matching_time_ns = 0#

Time spent matching candidates in this graph, excluding subgraphs.

int64_t rewriting_time_ns = 0#

Time spent applying matches and rebuilding this graph, excluding subgraphs.

int64_t cleanup_time_ns = 0#

Time spent in cleanup passes for this graph, excluding subgraphs.

int64_t constant_folding_time_ns = 0#

Time spent folding replacements in this graph, excluding subgraphs.

int64_t subgraph_optimization_time_ns = 0#

Total wall-clock time spent recursively optimizing subgraphs.

std::vector<PatternOptimizationStatistics> patterns#

Aggregated root and subgraph counters in pattern evaluation order.

std::vector<SubgraphOptimizationStatistics> subgraphs#

Flat, deterministic list of recursively optimized subgraphs.

struct PatternNoMatch#
#include <pattern_optimization.h>

Explains why one candidate did not match a pattern.

Public Functions

std::string ToString() const#

Returns a concise summary of this rejection.

Public Members

const NodeProto *candidate = nullptr#

Candidate inspected by the matcher.

std::string source_file#

Source file containing the rejection condition.

std::uint_least32_t source_line = 0#

Source line containing the rejection condition.

std::string reason#

Human-readable reason why the candidate did not match.

struct PatternNoMatchStatistics#
#include <pattern_optimization.h>

Aggregated reason and source location for rejected pattern candidates.

Public Functions

std::string ToString() const#

Returns a concise summary of this rejection.

Public Members

std::string source_file#

Source file containing the rejection condition.

std::uint_least32_t source_line = 0#

Source line containing the rejection condition.

std::string reason#

Human-readable reason why the candidate did not match.

std::size_t occurrences = 0#

Number of match attempts rejected at this condition.

class PatternOptimization#
#include <pattern_optimization.h>

Stateless interface implemented by graph-rewriting patterns.

Public Functions

inline explicit PatternOptimization(int priority = 1, std::string name = {})#

Creates a pattern with the given optimization priority.

virtual ~PatternOptimization() = default#
inline const std::string &Name() const noexcept#

Returns the stable diagnostic name of this pattern.

void SetRegisteredName(const std::string &name)#

Assigns the registry name, rejecting a conflicting intrinsic name.

std::string ToString() const#

Returns a concise summary of this pattern.

inline virtual std::set<std::string> FastOpType() const#

Returns the invariant operator types from which this pattern can start. The optimization driver caches this value once per optimization.

virtual MatchResult Match(GraphGraph &graph, const NodeProto &candidate) const = 0#

Returns the match rooted at candidate. A null pattern means no match.

virtual utils::RepeatedProtoField<NodeProto> Apply(GraphGraph &graph, const std::vector<const NodeProto*> &nodes) const = 0#

Builds the replacement nodes for one match.

Public Members

int priority#

Priority used by the optimization driver.

Protected Functions

template<std::size_t N>
inline MatchResult NoMatch(const NodeProto &candidate, const char (&reason)[N], std::source_location location = std::source_location::current()) const#

Returns an empty match with a rejection reason and its call-site location.

This is the normal result for a candidate that does not satisfy a pattern. Apply is only called for successful matches; invalid arguments passed directly to it remain contract violations.

Private Functions

MatchResult NoMatchImpl(const NodeProto &candidate, std::string_view reason, std::source_location location) const#

Private Members

std::string name_#
struct PatternOptimizationStatistics#
#include <pattern_optimization.h>

Aggregated timing and activity counters for one optimization pattern.

Public Functions

std::string ToString() const#

Returns a concise summary of these statistics.

Public Members

std::string pattern_name#

Stable diagnostic pattern name.

std::size_t attempts = 0#

Number of candidate nodes passed to the pattern matcher.

std::size_t matches = 0#

Number of accepted, disjoint matches.

int64_t match_time_ns = 0#

Total time spent matching candidates, in nanoseconds.

int64_t apply_time_ns = 0#

Total time spent building replacement nodes, in nanoseconds.

std::vector<PatternNoMatchStatistics> no_matches = {}#

Rejection conditions aggregated by source location and reason.

struct SubgraphOptimizationStatistics#
#include <pattern_optimization.h>

Activity and elapsed time for one recursively optimized subgraph.

Public Functions

std::string ToString() const#

Returns a concise summary of these statistics.

Public Members

std::vector<std::string> graph_path#

Nested builder path from the root graph.

std::size_t iterations = 0#

Number of optimization iterations executed in this subgraph and its children.

std::size_t rewrites = 0#

Number of rewrites applied in this subgraph and its children.

int64_t elapsed_time_ns = 0#

Total elapsed optimization time for this subgraph, in nanoseconds.