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 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.

std::shared_ptr<const PatternApplication> application = nullptr#

Optional replacement construction, used only from a freshly revalidated match.

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 move_shape_and_size_time_ns = 0#

Time spent in the final Shape/Size hoisting stage, including nested scopes.

std::size_t moved_shape_and_size_nodes = 0#

Number of Shape/Size readers relocated in this graph and its children.

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.

class PatternApplication#
#include <pattern_optimization.h>

Owns replacement construction prepared by Match.

Only the optimizer may invoke it, immediately after revalidating the match. Direct Apply calls must still validate their inputs against the current graph. Captured analysis belongs to this match, never to the shared pattern object.

Public Functions

inline explicit PatternApplication(std::function<utils::RepeatedProtoField<NodeProto>()> apply)#

Creates replacement construction for one match.

Private Members

std::function<utils::RepeatedProtoField<NodeProto>()> apply_#

Friends

friend class GraphGraph
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.

Subclassed by onnx_light::onnx_patterns::AttentionGQAPattern, onnx_light::onnx_patterns::BatchNormalizationPattern, onnx_light::onnx_patterns::BatchNormalizationTrainingPattern, onnx_light::onnx_patterns::CastCastBinaryPattern, onnx_light::onnx_patterns::CastCastPattern, onnx_light::onnx_patterns::CastLayerNormalizationCastPattern, onnx_light::onnx_patterns::CastOpCastPattern, onnx_light::onnx_patterns::CastPattern, onnx_light::onnx_patterns::ClipClipPattern, onnx_light::onnx_patterns::ConcatEmptyPattern, onnx_light::onnx_patterns::ConcatGatherPattern, onnx_light::onnx_patterns::ConcatReshapePattern, onnx_light::onnx_patterns::ConcatSliceEliminationPattern, onnx_light::onnx_patterns::ConcatTwiceUnaryPattern, onnx_light::onnx_patterns::ConstantToInitializerPattern, onnx_light::onnx_patterns::ConvAddFusionPattern, onnx_light::onnx_patterns::ConvBatchNormalizationFusionPattern, onnx_light::onnx_patterns::ConvBiasNullPattern, onnx_light::onnx_patterns::ConvMulFusionPattern, onnx_light::onnx_patterns::DivMulPattern, onnx_light::onnx_patterns::DropoutPattern, onnx_light::onnx_patterns::ExpandBroadcastPattern, onnx_light::onnx_patterns::ExpandPattern, onnx_light::onnx_patterns::ExpandSwapPattern, onnx_light::onnx_patterns::ExpandUnsqueezeExpandPattern, onnx_light::onnx_patterns::FunctionAttentionGQAPattern, onnx_light::onnx_patterns::FunctionAttentionPattern, onnx_light::onnx_patterns::FunctionCausalMaskMulAddPattern, onnx_light::onnx_patterns::FunctionCausalMaskPattern, onnx_light::onnx_patterns::FunctionCosSinCachePattern, onnx_light::onnx_patterns::FunctionHalfRotaryEmbeddingPattern, onnx_light::onnx_patterns::FunctionPattern, onnx_light::onnx_patterns::GatherConcatPattern, onnx_light::onnx_patterns::GatherGatherPattern, onnx_light::onnx_patterns::GatherShapePattern, onnx_light::onnx_patterns::GatherSliceToSplitPattern, onnx_light::onnx_patterns::GatherToSlicePattern, onnx_light::onnx_patterns::GatherUpstreamPropagationPattern, onnx_light::onnx_patterns::GathersSplitPattern, onnx_light::onnx_patterns::GeluPattern, onnx_light::onnx_patterns::GemmOutputTransposePattern, onnx_light::onnx_patterns::GemmSumFusionPattern, onnx_light::onnx_patterns::GemmTransposePattern, onnx_light::onnx_patterns::GroupedMatMulPattern, onnx_light::onnx_patterns::IdentityPattern, onnx_light::onnx_patterns::IfIdenticalBranchesPattern, onnx_light::onnx_patterns::InitializerUnsqueezeCastPattern, onnx_light::onnx_patterns::LabelEncoderFusionPattern, onnx_light::onnx_patterns::LayerNormalizationPattern, onnx_light::onnx_patterns::LayerNormalizationScalePattern, onnx_light::onnx_patterns::LeakyReluPattern, onnx_light::onnx_patterns::LinearAttentionPattern, onnx_light::onnx_patterns::MatMulAddPattern, onnx_light::onnx_patterns::MatMulBatchNormalizationFusionPattern, onnx_light::onnx_patterns::MatMulReshape2Of3Pattern, onnx_light::onnx_patterns::MatMulScaleFusionPattern, onnx_light::onnx_patterns::MaxReluPattern, onnx_light::onnx_patterns::MulMulMatMulPattern, onnx_light::onnx_patterns::MulMulMulScalarPattern, onnx_light::onnx_patterns::MulUnsqueezeUnsqueezePattern, onnx_light::onnx_patterns::NotNotPattern, onnx_light::onnx_patterns::NotWherePattern, onnx_light::onnx_patterns::PadConvPattern, onnx_light::onnx_patterns::PadPadFusionPattern, onnx_light::onnx_patterns::PadPoolPattern, onnx_light::onnx_patterns::PartitionGroupPattern, onnx_light::onnx_patterns::PreShapeNodeEliminationPattern, onnx_light::onnx_patterns::RMSNormalizationMulPattern, onnx_light::onnx_patterns::RMSNormalizationPattern, onnx_light::onnx_patterns::ReduceArgTopKPattern, onnx_light::onnx_patterns::ReduceReshapePattern, onnx_light::onnx_patterns::ReduceSumNormalizePattern, onnx_light::onnx_patterns::ReluClipFusionPattern, onnx_light::onnx_patterns::RepeatedLayerPattern, onnx_light::onnx_patterns::Reshape2Of3Pattern, onnx_light::onnx_patterns::ReshapeMatMulReshapePattern, onnx_light::onnx_patterns::ReshapePattern, onnx_light::onnx_patterns::ReshapeReshapeBinaryPattern, onnx_light::onnx_patterns::ReshapeReshapePattern, onnx_light::onnx_patterns::ReshapeSqueezePattern, onnx_light::onnx_patterns::RotaryConcatPartPattern, onnx_light::onnx_patterns::RotaryEmbeddingPattern, onnx_light::onnx_patterns::STFTFusionPattern, onnx_light::onnx_patterns::SameChildrenPattern, onnx_light::onnx_patterns::SequenceConstructAtPattern, onnx_light::onnx_patterns::ShapeBasedConcatExpandPattern, onnx_light::onnx_patterns::ShapeBasedEditDistanceReshapePattern, onnx_light::onnx_patterns::ShapeBasedExpandBroadcastMatMulPattern, onnx_light::onnx_patterns::ShapeBasedExpandBroadcastPattern, onnx_light::onnx_patterns::ShapeBasedExpandCastWhereSwapPattern, onnx_light::onnx_patterns::ShapeBasedExpandSwapPattern, onnx_light::onnx_patterns::ShapeBasedIdentityPattern, onnx_light::onnx_patterns::ShapeBasedMatMulToMulPattern, onnx_light::onnx_patterns::ShapeBasedReshapeIsSqueezePattern, onnx_light::onnx_patterns::ShapeBasedSameChildrenPattern, onnx_light::onnx_patterns::ShapeBasedShapeShapeAddPattern, onnx_light::onnx_patterns::ShapeBasedStaticExpandPattern, onnx_light::onnx_patterns::ShapeTransposePattern, onnx_light::onnx_patterns::ShapedBasedReshapePattern, onnx_light::onnx_patterns::SliceConcatToSpaceToDepthPattern, onnx_light::onnx_patterns::SliceEliminationPattern, onnx_light::onnx_patterns::SliceSlicePattern, onnx_light::onnx_patterns::SlicesSplitPattern, onnx_light::onnx_patterns::SoftmaxCrossEntropyLossCastPattern, onnx_light::onnx_patterns::SplitConcatPattern, onnx_light::onnx_patterns::SplitToSequenceSequenceAtPattern, onnx_light::onnx_patterns::SqueezeAddPattern, onnx_light::onnx_patterns::SqueezeBinaryUnsqueezePattern, onnx_light::onnx_patterns::SqueezeUnsqueezePattern, onnx_light::onnx_patterns::StaticConcatReshapePattern, onnx_light::onnx_patterns::Sub1MulPattern, onnx_light::onnx_patterns::SwapExpandReshapePattern, onnx_light::onnx_patterns::SwapExpandUnsqueezePattern, onnx_light::onnx_patterns::SwapRangeAddScalarPattern, onnx_light::onnx_patterns::SwapUnaryPattern, onnx_light::onnx_patterns::SwapUnsqueezeTransposePattern, onnx_light::onnx_patterns::SwitchOrderBinaryPattern, onnx_light::onnx_patterns::SwitchReshapeActivationPattern, onnx_light::onnx_patterns::TransposeEqualReshapePattern, onnx_light::onnx_patterns::TransposeGatherPattern, onnx_light::onnx_patterns::TransposeMatMulPattern, onnx_light::onnx_patterns::TransposeReshapeMatMulPattern, onnx_light::onnx_patterns::TransposeReshapeTransposePattern, onnx_light::onnx_patterns::TransposeTransposePattern, onnx_light::onnx_patterns::TreeEnsemblePattern, onnx_light::onnx_patterns::UnsqueezeEqualPattern, onnx_light::onnx_patterns::UnsqueezeOrSqueezeReshapePattern, onnx_light::onnx_patterns::UnsqueezeReshapePattern, onnx_light::onnx_patterns::UnsqueezeShapePattern, onnx_light::onnx_patterns::UnsqueezeUnsqueezePattern, onnx_light::onnx_patterns::WhereAddPattern

Public Functions

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

Creates a pattern; kUndefined marks device-independent patterns.

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.

inline virtual bool CanMatchInParallel() const noexcept#

Returns whether Match may run concurrently on worker threads.

Opt-in requires an audited, reentrant Match implementation: it must not mutate pattern state, the graph, its builder, or shared caches. Every graph query it uses must support concurrent reads of the frozen matching state; a const-qualified method alone does not establish thread safety.

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.

symbolic::Device device#

Exact target device, or kUndefined for a device-independent pattern.

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.