GraphBuilder#
yobx.xbuilder.GraphBuilder simplifies the programmatic construction
and optimization of ONNX graphs. It is the primary tool used to convert a
torch.fx.Graph into a onnx.ModelProto, but it can equally
be used standalone to build or transform any ONNX graph from scratch.
Class Hierarchy#
GraphBuilder is composed of three
cooperative base classes:
_BuilderRuntime— evaluates small constant sub-expressions (e.g. the[0, 0, -1]passed to aReshapenode) so the builder can resolve-1to the correct symbolic formula and fold constants early._ShapeRuntime— handles value-as-shape tracking needed by operators such asShape,Gather,Concat, andSlicewhen their outputs feed directly into aReshape._InferenceRuntime— walks the graph node by node, dispatching each node to the matching per-operator handler inyobx.xshape.shape_type_computeso that shapes and types are tracked for every intermediate result.
Two helper classes round out the public API:
FunctionOptions— controls whether (and how) a sub-graph is exported as a reusable ONNX local function.OptimizationOptions— selects which optimization passes run insideto_onnx.
Protocol API#
GraphBuilder satisfies a hierarchy of
protocols defined in yobx.typing. Callers that do not need the
full concrete class should type-annotate against the narrowest protocol that
covers the methods they actually use:
Protocol |
Scope |
|---|---|
Core construction API: inputs, outputs, initializers, nodes,
opset registration, type/shape/sequence accessors, and
|
|
Extends the core protocol with |
|
Extends |
|
The read-only view of the graph exposed to pattern-optimization authors
inside |
The op property returns an
object that satisfies OpsetProtocol,
which resolves g.op.Add(x, y)-style attribute-access dispatch to ONNX
node creation.
Building a graph from scratch#
The simplest workflow is:
Construct a
GraphBuilderwith an opset version.Call
make_tensor_inputto declare each graph input.Call
make_node(or the short-handg.op.<OpType>(…)syntax) to add operators.Call
make_tensor_outputto declare each graph output.Call
to_onnxto obtain aonnx.ModelProto.
<<<
import numpy as np
import onnx
from yobx.helpers.onnx_helper import pretty_onnx
from yobx.xbuilder import GraphBuilder
TFLOAT = onnx.TensorProto.FLOAT
# 1. create builder targeting opset 18
g = GraphBuilder(18, ir_version=10)
# 2. declare inputs
g.make_tensor_input("X", TFLOAT, ("batch", "seq", 64))
g.make_tensor_input("W", TFLOAT, (64, 32))
# 3. add a MatMul node via the short-hand op accessor
result = g.op.MatMul("X", "W")
# 4. declare the output and export
g.make_tensor_output(
result, elem_type=TFLOAT, shape=("batch", "seq", 32), indexed=False
)
model = g.to_onnx()
print(f"nodes : {len(model.graph.node)}")
print(f"opset : {model.opset_import[0].version}")
print(f"output : {model.graph.output[0].name}")
print(pretty_onnx(model))
>>>
nodes : 1
opset : 18
output : _onx_matmul_X
opset: domain='' version=18
input: name='X' type=dtype('float32') shape=['batch', 'seq', 64]
input: name='W' type=dtype('float32') shape=[64, 32]
MatMul(X, W) -> _onx_matmul_X
output: name='_onx_matmul_X' type=dtype('float32') shape=['batch', 'seq', 32]
Loading an existing model#
Passing an existing onnx.ModelProto to the constructor loads it into
the builder so its nodes and initializers can be inspected, modified, or
re-optimized.
<<<
import onnx
import onnx.helper as oh
from yobx.xbuilder import GraphBuilder
TFLOAT = onnx.TensorProto.FLOAT
model = oh.make_model(
oh.make_graph(
[
oh.make_node("Add", ["X", "Y"], ["T"]),
oh.make_node("Relu", ["T"], ["Z"]),
],
"add_relu",
[
oh.make_tensor_value_info("X", TFLOAT, ["batch", 4]),
oh.make_tensor_value_info("Y", TFLOAT, ["batch", 4]),
],
[oh.make_tensor_value_info("Z", TFLOAT, ["batch", 4])],
),
opset_imports=[oh.make_opsetid("", 18)],
ir_version=10,
)
g = GraphBuilder(model)
print("input shapes:", {n: g.get_shape(n) for n in g.input_names})
print("nodes :", [node.op_type for node in g.nodes])
>>>
input shapes: {'X': ('batch', 4), 'Y': ('batch', 4)}
nodes : ['Add', 'Relu']
Initializers#
Initializers (model weights and constants) are added with
make_initializer.
The builder deduplicates small integer arrays automatically: if the same
value is added twice it returns the name of the first occurrence rather than
creating a duplicate node.
<<<
import numpy as np
import onnx
from yobx.xbuilder import GraphBuilder
TFLOAT = onnx.TensorProto.FLOAT
g = GraphBuilder(18, ir_version=10)
g.make_tensor_input("X", TFLOAT, ("batch", 64))
# Add a weight matrix as an initializer
W = np.random.randn(64, 32).astype(np.float32)
w_name = g.make_initializer("W", W, source="example")
result = g.op.MatMul("X", w_name)
g.make_tensor_output(result, elem_type=TFLOAT, shape=("batch", 32), indexed=False)
model = g.to_onnx()
print("initializer name :", list(g.initializers_dict)[0])
print("initializer shape:", list(g.initializers_dict.values())[0].shape)
>>>
initializer name : W
initializer shape: (64, 32)
Shape and type tracking#
GraphBuilder inherits the full
ShapeBuilder interface. Shapes and types
are registered for every intermediate result as nodes are added, and are used
during optimization and for populating value_info in the exported proto.
See Expected API.
Dynamic shapes#
When some input dimensions are unknown at graph-construction time, they are
represented as strings (e.g. "batch", "seq"). For graphs that are
later exported for dynamic-shape inference with torch.export, the builder
accepts a dynamic_shapes dictionary that maps input names to per-axis
dimension objects (torch.export.Dim or WrapDim).
register_dynamic_objects_from_shape
registers any string dimension names encountered in a shape so that they are
tracked as symbolic dimensions.
<<<
import onnx
from yobx.xbuilder import GraphBuilder
TFLOAT = onnx.TensorProto.FLOAT
g = GraphBuilder(18, ir_version=10)
g.make_tensor_input("X", TFLOAT, ("batch", "seq", 64))
g.make_tensor_input("Y", TFLOAT, ("batch", "seq", 64))
# symbolic dimensions are tracked automatically once shapes are set
result = g.op.Add("X", "Y")
g.make_tensor_output(
result, elem_type=TFLOAT, shape=("batch", "seq", 64), indexed=False
)
model = g.to_onnx()
out = model.graph.output[0]
dims = [
d.dim_param if d.dim_param else d.dim_value for d in out.type.tensor_type.shape.dim
]
print("output shape:", dims)
>>>
output shape: ['batch', 'seq', 64]
Optimizations#
to_onnx runs a sequence of
optimization passes by default. The set of passes is controlled by
OptimizationOptions.
Default passes (in order):
Pass |
Effect |
|---|---|
|
Remove nodes whose outputs are never consumed. |
|
Evaluate operators such as |
|
Remove |
|
Merge identical constant initializers into a single tensor, removing redundant copies. |
|
Apply user-supplied or built-in fusion patterns (e.g.
|
|
Reorder nodes to reduce peak memory by moving each |
<<<
import onnx
import onnx.helper as oh
from yobx.xbuilder import GraphBuilder, OptimizationOptions
TFLOAT = onnx.TensorProto.FLOAT
model = oh.make_model(
oh.make_graph(
[
oh.make_node("Identity", ["X"], ["X2"]),
oh.make_node("Relu", ["X2"], ["Z"]),
],
"id_relu",
[oh.make_tensor_value_info("X", TFLOAT, [None, 4])],
[oh.make_tensor_value_info("Z", TFLOAT, [None, 4])],
),
opset_imports=[oh.make_opsetid("", 18)],
ir_version=10,
)
opts = OptimizationOptions(remove_identity=True)
g = GraphBuilder(model, optimization_options=opts)
optimized = g.to_onnx()
print("nodes before:", len(model.graph.node))
print("nodes after :", len(optimized.graph.node))
>>>
nodes before: 2
nodes after : 1
Optimization report#
Passing return_optimize_report=True to
to_onnx makes the method return
a (model, stats) tuple instead of just the model. stats is a list of
dictionaries — one entry per optimization pass — that records how many nodes
were added or removed and how long each pass took.
Key |
Description |
|---|---|
|
Name of the optimization pass (e.g. |
|
Number of nodes added by this pass. |
|
Number of nodes removed by this pass. |
|
Wall-clock time spent in this pass (seconds). |
|
Iteration number (only for pattern-based passes). |
|
Sequential index of the match within the iteration (pattern passes). |
|
Number of times the pattern was matched (pattern passes). |
The list can be converted to a pandas.DataFrame for quick
exploration:
<<<
import pandas
import onnx
import onnx.helper as oh
from yobx.xbuilder import GraphBuilder, OptimizationOptions
TFLOAT = onnx.TensorProto.FLOAT
model = oh.make_model(
oh.make_graph(
[
oh.make_node("Identity", ["X"], ["X2"]),
oh.make_node("Transpose", ["X2"], ["T"], perm=[1, 0]),
oh.make_node("Transpose", ["T"], ["Z"], perm=[1, 0]),
],
"demo",
[oh.make_tensor_value_info("X", TFLOAT, [3, 4])],
[oh.make_tensor_value_info("Z", TFLOAT, [3, 4])],
),
opset_imports=[oh.make_opsetid("", 18)],
ir_version=10,
)
opts = OptimizationOptions(patterns="default")
g = GraphBuilder(model, infer_shapes_options=True, optimization_options=opts)
optimized = g.to_onnx(return_optimize_report=True)
df = pandas.DataFrame(optimized.report.stats)
# keep only rows that have numeric added/removed counts
df["added"] = df["added"].fillna(0).astype(int)
df["removed"] = df["removed"].fillna(0).astype(int)
print(df[["pattern", "added", "removed", "time_in"]].to_string(index=False))
print(f"\nnodes before: {len(model.graph.node)}")
print(f"nodes after : {len(optimized.graph.node)}")
>>>
pattern added removed time_in
dynamic_dimension_naming 0 0 1.891400e-05
check_A-dynamic_dimension_naming 0 0 9.532000e-06
check_A-opt-sub 0 0 8.691000e-06
remove_identity 1 2 3.146100e-05
check_remove_identity-0 0 0 6.796000e-06
remove_unused 0 0 1.464400e-05
check_remove_unused-1 0 0 6.328000e-06
constant_folding 0 0 8.158000e-06
apply_constant_folding_new_inits 0 0 NaN
check_constant_folding-2 0 0 6.126000e-06
remove_unused 0 0 1.129500e-05
check_remove_unused-3 0 0 6.079000e-06
patterns 0 1 4.719274e-03
check_pattern_00 0 0 1.475300e-05
match_BatchNormalizationPattern 0 0 7.755000e-06
match_BatchNormalizationTrainingPattern 0 0 3.644000e-06
match_CastPattern 0 0 3.991000e-06
match_CastCastPattern 0 0 3.441000e-06
match_ConcatGatherPattern 0 0 2.829000e-06
match_ConcatReshapePattern 0 0 4.023000e-06
match_ConvBiasNullPattern 0 0 3.436000e-06
match_PadConvPattern 0 0 2.696000e-06
match_ExpandPattern 0 0 2.977000e-06
match_ExpandUnsqueezeExpandPattern 0 0 2.955000e-06
match_GatherConcatPattern 0 0 3.235000e-06
match_GatherGatherPattern 0 0 2.801000e-06
match_GatherShapePattern 0 0 3.023000e-06
match_GeluPattern 0 0 9.510000e-07
match_IdentityPattern 0 0 1.589740e-04
match_LeakyReluPattern 0 0 9.602850e-04
match_MulUnsqueezeUnsqueezePattern 0 0 5.474000e-06
match_ReshapePattern 0 0 4.124000e-06
match_ReshapeSqueezePattern 0 0 3.675000e-06
match_ShapeBasedReshapeIsSqueezePattern 0 0 5.277000e-06
match_ShapeBasedStaticExpandPattern 0 0 2.979000e-06
match_ShapeBasedEditDistanceReshapePattern 0 0 3.975000e-06
match_ShapeBasedIdentityPattern 0 0 7.727000e-06
match_ShapedBasedReshapePattern 0 0 3.239000e-06
match_ShapeBasedSameChildrenPattern 0 0 3.045000e-06
match_ShapeBasedShapeShapeAddPattern 0 0 3.282000e-06
match_ShapeTransposePattern 0 0 2.720000e-06
match_UnsqueezeShapePattern 0 0 2.978000e-06
match_ReshapeReshapePattern 0 0 3.091000e-06
match_SameChildrenPattern 0 0 7.902000e-06
match_SameChildrenFromInputPattern 0 0 6.903000e-06
match_SoftmaxCrossEntropyLossCastPattern 0 0 1.511209e-03
match_SqueezeAddPattern 0 0 4.385000e-06
match_SqueezeBinaryUnsqueezePattern 0 0 3.169000e-06
match_SqueezeUnsqueezePattern 0 0 3.381000e-06
match_StaticConcatReshapePattern 0 0 3.655000e-06
match_SwapExpandReshapePattern 0 0 3.133000e-06
match_SwapExpandUnsqueezePattern 0 0 2.542000e-06
match_SwapUnaryPattern 0 0 1.897300e-05
match_SwapUnsqueezeTransposePattern 0 0 9.016000e-06
match_TransposeGatherPattern 0 0 3.102000e-06
match_TransposeReshapeTransposePattern 0 0 6.806000e-06
match_TransposeTransposePattern 0 0 4.225000e-05
match_UnsqueezeOrSqueezeReshapePattern 0 0 3.695000e-06
match_UnsqueezeReshapePattern 0 0 3.352000e-06
match_UnsqueezeUnsqueezePattern 0 0 3.205000e-06
match_FunctionAttentionPattern 0 0 3.356000e-06
match_FunctionAttentionGQAPattern 0 0 4.026000e-06
insert_and_remove_nodes 0 0 7.311900e-05
apply_TransposeTransposePattern 1 2 1.302970e-04
check_pattern_A10 0 0 1.094000e-06
check_pattern_A20 0 0 8.833000e-06
remove_duplicated_shape 0 0 2.183000e-06
check_pattern_BD0 0 0 5.575000e-06
remove_identity_nodes 0 0 2.260000e-05
check_pattern_BI0 0 0 7.299000e-06
remove_unused 0 0 1.505700e-05
check_pattern_BUS0 0 0 6.644000e-06
build_graph_for_pattern 0 0 1.191700e-05
iteration_0 0 0 3.179560e-03
match_BatchNormalizationPattern 0 0 3.706000e-06
match_BatchNormalizationTrainingPattern 0 0 2.605000e-06
match_CastPattern 0 0 2.355000e-06
match_CastCastPattern 0 0 2.062000e-06
match_ConcatGatherPattern 0 0 2.019000e-06
match_ConcatReshapePattern 0 0 2.909000e-06
match_ConvBiasNullPattern 0 0 1.881000e-06
match_PadConvPattern 0 0 1.878000e-06
match_ExpandPattern 0 0 1.936000e-06
match_ExpandUnsqueezeExpandPattern 0 0 1.835000e-06
match_GatherConcatPattern 0 0 1.857000e-06
match_GatherGatherPattern 0 0 2.255000e-06
match_GatherShapePattern 0 0 2.240000e-06
match_GeluPattern 0 0 9.070000e-07
match_IdentityPattern 0 0 2.370000e-06
match_LeakyReluPattern 0 0 5.458000e-06
match_MulUnsqueezeUnsqueezePattern 0 0 2.017000e-06
match_ReshapePattern 0 0 2.289000e-06
match_ReshapeSqueezePattern 0 0 2.782000e-06
match_ShapeBasedReshapeIsSqueezePattern 0 0 2.480000e-06
match_ShapeBasedStaticExpandPattern 0 0 1.707000e-06
match_ShapeBasedEditDistanceReshapePattern 0 0 2.001000e-06
match_ShapeBasedIdentityPattern 0 0 2.148000e-06
match_ShapedBasedReshapePattern 0 0 1.621000e-06
match_ShapeBasedSameChildrenPattern 0 0 2.284000e-06
match_ShapeBasedShapeShapeAddPattern 0 0 2.073000e-06
match_ShapeTransposePattern 0 0 2.177000e-06
match_UnsqueezeShapePattern 0 0 2.110000e-06
match_ReshapeReshapePattern 0 0 1.919000e-06
match_SameChildrenPattern 0 0 3.823000e-06
match_SameChildrenFromInputPattern 0 0 4.332000e-06
match_SoftmaxCrossEntropyLossCastPattern 0 0 3.950000e-06
match_SqueezeAddPattern 0 0 1.836000e-06
match_SqueezeBinaryUnsqueezePattern 0 0 1.878000e-06
match_SqueezeUnsqueezePattern 0 0 2.339000e-06
match_StaticConcatReshapePattern 0 0 1.718000e-06
match_SwapExpandReshapePattern 0 0 1.613000e-06
match_SwapExpandUnsqueezePattern 0 0 1.625000e-06
match_SwapUnaryPattern 0 0 1.763000e-06
match_SwapUnsqueezeTransposePattern 0 0 1.632000e-06
match_TransposeGatherPattern 0 0 1.420000e-06
match_TransposeReshapeTransposePattern 0 0 1.462000e-06
match_TransposeTransposePattern 0 0 1.539000e-06
match_UnsqueezeOrSqueezeReshapePattern 0 0 1.845000e-06
match_UnsqueezeReshapePattern 0 0 1.991000e-06
match_UnsqueezeUnsqueezePattern 0 0 1.901000e-06
match_FunctionAttentionPattern 0 0 1.763000e-06
match_FunctionAttentionGQAPattern 0 0 2.130000e-06
check_pattern_A20 0 0 6.910000e-06
remove_duplicated_shape 0 0 1.780000e-06
check_pattern_BD0 0 0 5.172000e-06
remove_identity_nodes 0 0 1.306000e-05
check_pattern_BI0 0 0 4.654000e-06
remove_unused 0 0 9.280000e-06
check_pattern_BUS0 0 0 4.181000e-06
build_graph_for_pattern 0 0 7.711000e-06
iteration_1 0 0 2.457040e-04
match_BatchNormalizationPattern 0 0 2.548000e-06
match_BatchNormalizationTrainingPattern 0 0 1.956000e-06
match_CastLayerNormalizationCastPattern 0 0 2.860000e-06
match_CastPattern 0 0 2.029000e-06
match_CastCastBinaryPattern 0 0 2.756000e-06
match_CastCastPattern 0 0 1.565000e-06
match_CastOpCastPattern 0 0 3.107000e-06
match_ClipClipPattern 0 0 2.141000e-06
match_ConcatEmptyPattern 0 0 2.769000e-06
match_ConcatGatherPattern 0 0 1.530000e-06
match_ConcatReshapePattern 0 0 1.921000e-06
match_ConcatTwiceUnaryPattern 0 0 2.746000e-06
match_ConstantToInitializerPattern 0 0 2.073000e-06
match_ConvBiasNullPattern 0 0 1.355000e-06
match_PadConvPattern 0 0 1.488000e-06
match_DropoutPattern 0 0 2.367000e-06
match_ExpandPattern 0 0 1.616000e-06
match_ExpandBroadcastPattern 0 0 1.943000e-06
match_ExpandSwapPattern 0 0 2.258000e-06
match_ExpandUnsqueezeExpandPattern 0 0 1.490000e-06
match_GatherConcatPattern 0 0 1.709000e-06
match_GatherGatherPattern 0 0 1.557000e-06
match_GathersSplitPattern 0 0 2.453000e-06
match_GatherShapePattern 0 0 1.712000e-06
match_GeluPattern 0 0 7.920000e-07
match_IdentityPattern 0 0 1.881000e-06
match_LayerNormalizationPattern 0 0 3.113000e-06
match_LayerNormalizationScalePattern 0 0 2.572000e-06
match_LeakyReluPattern 0 0 3.674000e-06
match_MaxReluPattern 0 0 2.328000e-06
match_MulMulMulScalarPattern 0 0 3.075000e-06
match_MulUnsqueezeUnsqueezePattern 0 0 1.551000e-06
match_NotNotPattern 0 0 2.246000e-06
match_NotWherePattern 0 0 2.125000e-06
match_ReduceArgTopKPattern 0 0 2.861000e-06
match_ReduceReshapePattern 0 0 2.541000e-06
match_ReduceSumNormalizePattern 0 0 2.207000e-06
match_ReshapePattern 0 0 1.809000e-06
match_ReshapeMatMulReshapePattern 0 0 2.656000e-06
match_Reshape2Of3Pattern 0 0 2.587000e-06
match_ReshapeReshapeBinaryPattern 0 0 2.594000e-06
match_ReshapeSqueezePattern 0 0 2.278000e-06
match_GemmTransposePattern 0 0 2.677000e-06
match_MatMulReshape2Of3Pattern 0 0 2.446000e-06
match_MulMulMatMulPattern 0 0 2.167000e-06
match_ShapeBasedReshapeIsSqueezePattern 0 0 1.687000e-06
match_ShapeBasedStaticExpandPattern 0 0 1.456000e-06
match_ShapeBasedConcatExpandPattern 0 0 2.281000e-06
match_ShapeBasedEditDistanceReshapePattern 0 0 1.577000e-06
match_ShapeBasedIdentityPattern 0 0 1.784000e-06
match_ShapeBasedExpandBroadcastPattern 0 0 2.221000e-06
match_ShapeBasedExpandBroadcastMatMulPattern 0 0 2.012000e-06
match_ShapeBasedExpandCastWhereSwapPattern 0 0 2.270000e-06
match_ShapeBasedExpandSwapPattern 0 0 2.172000e-06
match_ShapeBasedMatMulToMulPattern 0 0 2.145000e-06
match_ShapedBasedReshapePattern 0 0 1.758000e-06
match_ShapeBasedSameChildrenPattern 0 0 2.226000e-06
match_ShapeBasedShapeShapeAddPattern 0 0 1.432000e-06
match_ShapeTransposePattern 0 0 1.504000e-06
match_UnsqueezeShapePattern 0 0 1.527000e-06
match_ReshapeReshapePattern 0 0 1.696000e-06
match_RotaryEmbeddingPattern 0 0 2.594000e-06
match_SameChildrenPattern 0 0 3.184000e-06
match_SameChildrenFromInputPattern 0 0 3.595000e-06
match_SequenceConstructAtPattern 0 0 2.428000e-06
match_SplitToSequenceSequenceAtPattern 0 0 2.338000e-06
match_SliceSlicePattern 0 0 2.422000e-06
match_SlicesSplitPattern 0 0 2.394000e-06
match_SoftmaxCrossEntropyLossCastPattern 0 0 3.392000e-06
match_SplitConcatPattern 0 0 2.228000e-06
match_SqueezeAddPattern 0 0 1.878000e-06
match_SqueezeBinaryUnsqueezePattern 0 0 1.388000e-06
match_SqueezeUnsqueezePattern 0 0 1.757000e-06
match_StaticConcatReshapePattern 0 0 1.851000e-06
match_Sub1MulPattern 0 0 2.109000e-06
match_SwapExpandReshapePattern 0 0 1.828000e-06
match_SwapExpandUnsqueezePattern 0 0 1.686000e-06
match_SwapRangeAddScalarPattern 0 0 2.624000e-06
match_SwapUnaryPattern 0 0 1.545000e-06
match_SwapUnsqueezeTransposePattern 0 0 1.538000e-06
match_SwitchOrderBinaryPattern 0 0 2.676000e-06
match_SwitchReshapeActivationPattern 0 0 2.825000e-06
match_TransposeEqualReshapePattern 0 0 2.123000e-06
match_TransposeGatherPattern 0 0 1.538000e-06
match_TransposeMatMulPattern 0 0 2.125000e-06
match_TransposeReshapeMatMulPattern 0 0 2.232000e-06
match_TransposeReshapeTransposePattern 0 0 1.599000e-06
match_TransposeTransposePattern 0 0 1.689000e-06
match_UnsqueezeEqualPattern 0 0 2.319000e-06
match_UnsqueezeOrSqueezeReshapePattern 0 0 1.856000e-06
match_UnsqueezeReshapePattern 0 0 1.696000e-06
match_UnsqueezeUnsqueezePattern 0 0 1.548000e-06
match_WhereAddPattern 0 0 2.371000e-06
match_RotaryConcatPartPattern 0 0 2.442000e-06
match_FunctionAttentionPattern 0 0 1.699000e-06
match_FunctionAttentionGQAPattern 0 0 1.806000e-06
match_FunctionCausalMaskPattern 0 0 2.633000e-06
match_FunctionCausalMaskMulAddPattern 0 0 2.395000e-06
match_FunctionCosSinCachePattern 0 0 2.556000e-06
match_FunctionHalfRotaryEmbeddingPattern 0 0 2.312000e-06
match_RMSNormalizationPattern 0 0 2.609000e-06
match_RMSNormalizationMulPattern 0 0 1.976000e-06
check_pattern_A20 0 0 6.139000e-06
remove_duplicated_shape 0 0 1.436000e-06
check_pattern_BD0 0 0 4.425000e-06
remove_identity_nodes 0 0 1.130200e-05
check_pattern_BI0 0 0 4.364000e-06
remove_unused 0 0 8.718000e-06
check_pattern_BUS0 0 0 3.990000e-06
build_graph_for_pattern 0 0 6.705000e-06
iteration_2 0 0 3.917570e-04
match_BatchNormalizationPattern 0 0 2.554000e-06
match_BatchNormalizationTrainingPattern 0 0 1.864000e-06
match_CastLayerNormalizationCastPattern 0 0 1.970000e-06
match_CastPattern 0 0 1.470000e-06
match_CastCastBinaryPattern 0 0 1.545000e-06
match_CastCastPattern 0 0 1.498000e-06
match_CastOpCastPattern 0 0 1.995000e-06
match_ClipClipPattern 0 0 1.822000e-06
match_ConcatEmptyPattern 0 0 1.386000e-06
match_ConcatGatherPattern 0 0 1.571000e-06
match_ConcatReshapePattern 0 0 1.951000e-06
match_ConcatTwiceUnaryPattern 0 0 1.841000e-06
match_ConstantToInitializerPattern 0 0 1.701000e-06
match_ConvBiasNullPattern 0 0 1.721000e-06
match_PadConvPattern 0 0 1.509000e-06
match_DropoutPattern 0 0 1.265000e-06
match_ExpandPattern 0 0 1.402000e-06
match_ExpandBroadcastPattern 0 0 1.387000e-06
match_ExpandSwapPattern 0 0 1.407000e-06
match_ExpandUnsqueezeExpandPattern 0 0 1.486000e-06
match_GatherConcatPattern 0 0 1.690000e-06
match_GatherGatherPattern 0 0 1.549000e-06
match_GathersSplitPattern 0 0 1.597000e-06
match_GatherShapePattern 0 0 1.591000e-06
match_GeluPattern 0 0 7.270000e-07
match_IdentityPattern 0 0 1.646000e-06
match_LayerNormalizationPattern 0 0 1.441000e-06
match_LayerNormalizationScalePattern 0 0 1.681000e-06
match_LeakyReluPattern 0 0 3.749000e-06
match_MaxReluPattern 0 0 1.488000e-06
match_MulMulMulScalarPattern 0 0 1.713000e-06
match_MulUnsqueezeUnsqueezePattern 0 0 1.290700e-05
match_NotNotPattern 0 0 1.954000e-06
match_NotWherePattern 0 0 1.608000e-06
match_ReduceArgTopKPattern 0 0 2.086000e-06
match_ReduceReshapePattern 0 0 1.712000e-06
match_ReduceSumNormalizePattern 0 0 1.544000e-06
match_ReshapePattern 0 0 1.914000e-06
match_ReshapeMatMulReshapePattern 0 0 1.459000e-06
match_Reshape2Of3Pattern 0 0 1.552000e-06
match_ReshapeReshapeBinaryPattern 0 0 1.898000e-06
match_ReshapeSqueezePattern 0 0 1.891000e-06
match_GemmTransposePattern 0 0 1.574000e-06
match_MatMulReshape2Of3Pattern 0 0 1.611000e-06
match_MulMulMatMulPattern 0 0 1.586000e-06
match_ShapeBasedReshapeIsSqueezePattern 0 0 2.082000e-06
match_ShapeBasedStaticExpandPattern 0 0 1.528000e-06
match_ShapeBasedConcatExpandPattern 0 0 1.612000e-06
match_ShapeBasedEditDistanceReshapePattern 0 0 2.010000e-06
match_ShapeBasedIdentityPattern 0 0 1.596000e-06
match_ShapeBasedExpandBroadcastPattern 0 0 1.590000e-06
match_ShapeBasedExpandBroadcastMatMulPattern 0 0 1.477000e-06
match_ShapeBasedExpandCastWhereSwapPattern 0 0 1.419000e-06
match_ShapeBasedExpandSwapPattern 0 0 1.503000e-06
match_ShapeBasedMatMulToMulPattern 0 0 1.575000e-06
match_ShapedBasedReshapePattern 0 0 1.842000e-06
match_ShapeBasedSameChildrenPattern 0 0 1.591000e-06
match_ShapeBasedShapeShapeAddPattern 0 0 1.530000e-06
match_ShapeTransposePattern 0 0 1.660000e-06
match_UnsqueezeShapePattern 0 0 1.490000e-06
match_ReshapeReshapePattern 0 0 2.098000e-06
match_RotaryEmbeddingPattern 0 0 1.780000e-06
match_SameChildrenPattern 0 0 2.884000e-06
match_SameChildrenFromInputPattern 0 0 3.231000e-06
match_SequenceConstructAtPattern 0 0 1.927000e-06
match_SplitToSequenceSequenceAtPattern 0 0 1.965000e-06
match_SliceSlicePattern 0 0 1.578000e-06
match_SlicesSplitPattern 0 0 1.468000e-06
match_SoftmaxCrossEntropyLossCastPattern 0 0 3.222000e-06
match_SplitConcatPattern 0 0 1.607000e-06
match_SqueezeAddPattern 0 0 1.510000e-06
match_SqueezeBinaryUnsqueezePattern 0 0 1.526000e-06
match_SqueezeUnsqueezePattern 0 0 1.949000e-06
match_StaticConcatReshapePattern 0 0 1.754000e-06
match_Sub1MulPattern 0 0 1.437000e-06
match_SwapExpandReshapePattern 0 0 1.502000e-06
match_SwapExpandUnsqueezePattern 0 0 1.435000e-06
match_SwapRangeAddScalarPattern 0 0 1.511000e-06
match_SwapUnaryPattern 0 0 1.542000e-06
match_SwapUnsqueezeTransposePattern 0 0 1.425000e-06
match_SwitchOrderBinaryPattern 0 0 1.792000e-06
match_SwitchReshapeActivationPattern 0 0 1.657000e-06
match_TransposeEqualReshapePattern 0 0 1.486000e-06
match_TransposeGatherPattern 0 0 1.626000e-06
match_TransposeMatMulPattern 0 0 1.592000e-06
match_TransposeReshapeMatMulPattern 0 0 1.544000e-06
match_TransposeReshapeTransposePattern 0 0 1.561000e-06
match_TransposeTransposePattern 0 0 1.510000e-06
match_UnsqueezeEqualPattern 0 0 1.498000e-06
match_UnsqueezeOrSqueezeReshapePattern 0 0 1.647000e-06
match_UnsqueezeReshapePattern 0 0 1.706000e-06
match_UnsqueezeUnsqueezePattern 0 0 1.515000e-06
match_WhereAddPattern 0 0 1.530000e-06
match_RotaryConcatPartPattern 0 0 1.581000e-06
match_FunctionAttentionPattern 0 0 1.695000e-06
match_FunctionAttentionGQAPattern 0 0 1.854000e-06
match_FunctionCausalMaskPattern 0 0 1.667000e-06
match_FunctionCausalMaskMulAddPattern 0 0 1.372000e-06
match_FunctionCosSinCachePattern 0 0 1.707000e-06
match_FunctionHalfRotaryEmbeddingPattern 0 0 1.436000e-06
match_RMSNormalizationPattern 0 0 1.599000e-06
match_RMSNormalizationMulPattern 0 0 1.592000e-06
match_AttentionGQAPattern 0 0 2.034000e-06
check_pattern_A20 0 0 6.218000e-06
remove_duplicated_shape 0 0 1.268000e-06
check_pattern_BD0 0 0 4.342000e-06
remove_identity_nodes 0 0 1.063900e-05
check_pattern_BI0 0 0 4.191000e-06
remove_unused 0 0 8.617000e-06
check_pattern_BUS0 0 0 4.044000e-06
build_graph_for_pattern 0 0 6.705000e-06
iteration_3 0 0 3.569490e-04
match_BatchNormalizationPattern 0 0 2.486000e-06
match_BatchNormalizationTrainingPattern 0 0 1.885000e-06
match_CastLayerNormalizationCastPattern 0 0 1.796000e-06
match_CastPattern 0 0 1.433000e-06
match_CastCastBinaryPattern 0 0 1.482000e-06
match_CastCastPattern 0 0 1.466000e-06
match_CastOpCastPattern 0 0 1.719000e-06
match_ClipClipPattern 0 0 1.423000e-06
match_ConcatEmptyPattern 0 0 1.384000e-06
match_ConcatGatherPattern 0 0 1.560000e-06
match_ConcatReshapePattern 0 0 2.122000e-06
match_ConcatTwiceUnaryPattern 0 0 1.743000e-06
match_ConstantToInitializerPattern 0 0 1.526000e-06
match_ConvBiasNullPattern 0 0 1.426000e-06
match_PadConvPattern 0 0 1.473000e-06
match_DropoutPattern 0 0 1.281000e-06
match_ExpandPattern 0 0 1.408000e-06
match_ExpandBroadcastPattern 0 0 1.397000e-06
match_ExpandSwapPattern 0 0 1.473000e-06
match_ExpandUnsqueezeExpandPattern 0 0 1.416000e-06
match_GatherConcatPattern 0 0 1.479000e-06
match_GatherGatherPattern 0 0 1.531000e-06
match_GathersSplitPattern 0 0 1.567000e-06
match_GatherShapePattern 0 0 1.559000e-06
match_GeluPattern 0 0 6.800000e-07
match_IdentityPattern 0 0 1.666000e-06
match_LayerNormalizationPattern 0 0 1.491000e-06
match_LayerNormalizationScalePattern 0 0 1.467000e-06
match_LeakyReluPattern 0 0 3.572000e-06
match_MaxReluPattern 0 0 1.615000e-06
match_MulMulMulScalarPattern 0 0 1.603000e-06
match_MulUnsqueezeUnsqueezePattern 0 0 1.455000e-06
match_NotNotPattern 0 0 1.383000e-06
match_NotWherePattern 0 0 1.571000e-06
match_ReduceArgTopKPattern 0 0 1.771000e-06
match_ReduceReshapePattern 0 0 1.681000e-06
match_ReduceSumNormalizePattern 0 0 1.411000e-06
match_ReshapePattern 0 0 1.758000e-06
match_ReshapeMatMulReshapePattern 0 0 1.431000e-06
match_Reshape2Of3Pattern 0 0 1.485000e-06
match_ReshapeReshapeBinaryPattern 0 0 1.553000e-06
match_ReshapeSqueezePattern 0 0 1.801000e-06
match_MatMulAddPattern 0 0 2.496000e-06
match_GemmTransposePattern 0 0 1.618000e-06
match_MatMulReshape2Of3Pattern 0 0 1.596000e-06
match_MulMulMatMulPattern 0 0 1.555000e-06
match_ShapeBasedReshapeIsSqueezePattern 0 0 1.813000e-06
match_ShapeBasedStaticExpandPattern 0 0 1.473000e-06
match_ShapeBasedConcatExpandPattern 0 0 1.513000e-06
match_ShapeBasedEditDistanceReshapePattern 0 0 1.719000e-06
match_ShapeBasedIdentityPattern 0 0 1.542000e-06
match_ShapeBasedExpandBroadcastPattern 0 0 1.437000e-06
match_ShapeBasedExpandBroadcastMatMulPattern 0 0 1.553000e-06
match_ShapeBasedExpandCastWhereSwapPattern 0 0 1.413000e-06
match_ShapeBasedExpandSwapPattern 0 0 1.567000e-06
match_ShapeBasedMatMulToMulPattern 0 0 1.525000e-06
match_ShapedBasedReshapePattern 0 0 1.726000e-06
match_ShapeBasedSameChildrenPattern 0 0 1.570000e-06
match_ShapeBasedShapeShapeAddPattern 0 0 1.531000e-06
match_ShapeTransposePattern 0 0 1.480000e-06
match_UnsqueezeShapePattern 0 0 1.513000e-06
match_ReshapeReshapePattern 0 0 1.823000e-06
match_RotaryEmbeddingPattern 0 0 1.716000e-06
match_SameChildrenPattern 0 0 3.180000e-06
match_SameChildrenFromInputPattern 0 0 3.221000e-06
match_SequenceConstructAtPattern 0 0 1.695000e-06
match_SplitToSequenceSequenceAtPattern 0 0 1.609000e-06
match_SliceSlicePattern 0 0 1.539000e-06
match_SlicesSplitPattern 0 0 1.617000e-06
match_SoftmaxCrossEntropyLossCastPattern 0 0 3.342000e-06
match_SplitConcatPattern 0 0 1.499000e-06
match_SqueezeAddPattern 0 0 1.586000e-06
match_SqueezeBinaryUnsqueezePattern 0 0 1.747000e-06
match_SqueezeUnsqueezePattern 0 0 2.114000e-06
match_StaticConcatReshapePattern 0 0 1.840000e-06
match_Sub1MulPattern 0 0 1.454000e-06
match_SwapExpandReshapePattern 0 0 1.416000e-06
match_SwapExpandUnsqueezePattern 0 0 5.591200e-05
match_SwapRangeAddScalarPattern 0 0 1.787000e-06
match_SwapUnaryPattern 0 0 1.672000e-06
match_SwapUnsqueezeTransposePattern 0 0 1.581000e-06
match_SwitchOrderBinaryPattern 0 0 2.391000e-06
match_SwitchReshapeActivationPattern 0 0 1.700000e-06
match_TransposeEqualReshapePattern 0 0 1.578000e-06
match_TransposeGatherPattern 0 0 1.476000e-06
match_TransposeMatMulPattern 0 0 1.816000e-06
match_TransposeReshapeMatMulPattern 0 0 1.655000e-06
match_TransposeReshapeTransposePattern 0 0 1.520000e-06
match_TransposeTransposePattern 0 0 1.500000e-06
match_UnsqueezeEqualPattern 0 0 1.609000e-06
match_UnsqueezeOrSqueezeReshapePattern 0 0 1.780000e-06
match_UnsqueezeReshapePattern 0 0 1.766000e-06
match_UnsqueezeUnsqueezePattern 0 0 1.629000e-06
match_WhereAddPattern 0 0 1.581000e-06
match_RotaryConcatPartPattern 0 0 1.628000e-06
match_FunctionAttentionPattern 0 0 1.701000e-06
match_FunctionAttentionGQAPattern 0 0 1.759000e-06
match_FunctionCausalMaskPattern 0 0 1.578000e-06
match_FunctionCausalMaskMulAddPattern 0 0 1.515000e-06
match_FunctionCosSinCachePattern 0 0 1.606000e-06
match_FunctionHalfRotaryEmbeddingPattern 0 0 1.560000e-06
match_RMSNormalizationPattern 0 0 1.617000e-06
match_RMSNormalizationMulPattern 0 0 1.464000e-06
match_AttentionGQAPattern 0 0 1.534000e-06
check_pattern_A20 0 0 6.058000e-06
remove_duplicated_shape 0 0 1.313000e-06
check_pattern_BD0 0 0 4.356000e-06
remove_identity_nodes 0 0 1.115500e-05
check_pattern_BI0 0 0 4.295000e-06
remove_unused 0 0 8.548000e-06
check_pattern_BUS0 0 0 4.056000e-06
build_graph_for_pattern 0 0 8.367000e-06
check_patterns-4 0 0 8.073000e-06
remove_unused 0 0 9.245000e-06
check_remove_unused-5 0 0 4.391000e-06
remove_identity 0 0 1.047700e-05
check_remove_identity-6 0 0 4.229000e-06
constant_folding 0 0 9.303000e-06
apply_constant_folding_new_inits 0 0 NaN
check_constant_folding-7 0 0 3.992000e-06
remove_unused 0 0 7.871000e-06
check_remove_unused-8 0 0 3.878000e-06
remove_duplicated_initializer 0 0 2.346000e-06
check_remove_duplicated_initializer-9 0 0 3.717000e-06
remove_identity 0 0 9.540000e-06
check_remove_identity-10 0 0 3.588000e-06
remove_unused 0 0 7.053000e-06
check_remove_unused-11 0 0 3.780000e-06
order 0 0 3.570200e-05
check_orderA 0 0 5.943000e-06
check_orderL 0 0 4.236000e-06
shape_order 0 0 1.723700e-05
order 0 0 NaN
check_order-12 0 0 4.440000e-06
optimization 0 2 5.022880e-03
nodes before: 3
nodes after : 1
The report can be aggregated by pass name:
<<<
import pandas
import onnx
import onnx.helper as oh
from yobx.xbuilder import GraphBuilder, OptimizationOptions
TFLOAT = onnx.TensorProto.FLOAT
model = oh.make_model(
oh.make_graph(
[
oh.make_node("Identity", ["X"], ["X2"]),
oh.make_node("Transpose", ["X2"], ["T"], perm=[1, 0]),
oh.make_node("Transpose", ["T"], ["Z"], perm=[1, 0]),
],
"demo",
[oh.make_tensor_value_info("X", TFLOAT, [3, 4])],
[oh.make_tensor_value_info("Z", TFLOAT, [3, 4])],
),
opset_imports=[oh.make_opsetid("", 18)],
ir_version=10,
)
opts = OptimizationOptions(patterns="default")
g = GraphBuilder(model, infer_shapes_options=True, optimization_options=opts)
art = g.to_onnx(return_optimize_report=True)
df = pandas.DataFrame(art.report.stats)
for c in ["added", "removed"]:
df[c] = df[c].fillna(0).astype(int)
agg = df.groupby("pattern")[["added", "removed", "time_in"]].sum()
agg = agg[(agg["added"] > 0) | (agg["removed"] > 0)].sort_values(
"removed", ascending=False
)
print(agg.to_string())
>>>
added removed time_in
pattern
apply_TransposeTransposePattern 1 2 0.000126
optimization 0 2 0.004769
remove_identity 1 2 0.000053
patterns 0 1 0.004466
Local functions#
A sub-graph can be exported as a reusable ONNX local function (a
FunctionProto) by passing a FunctionOptions instance to
to_onnx.
<<<
import onnx
from yobx.xbuilder import GraphBuilder, FunctionOptions
TFLOAT = onnx.TensorProto.FLOAT
g = GraphBuilder(18, ir_version=10, as_function=True)
g.make_tensor_input("X", TFLOAT, ("batch", 64))
r = g.op.Relu("X")
g.make_tensor_output(r, indexed=False)
func = g.to_onnx(
function_options=FunctionOptions(
export_as_function=True,
name="MyRelu",
domain="my.domain",
),
inline=False,
)
proto = func.proto
print(type(proto).__name__)
print("function name :", proto.name)
print("function domain:", proto.domain)
>>>
FunctionProto
function name : MyRelu
function domain: my.domain
Debugging GraphBuilder with Environment Variables#
GraphBuilder respects several
environment variables that help narrow down construction or optimization
problems:
Environment variable |
Effect |
|---|---|
|
Raises an exception the moment result |
|
Raises an exception the moment result |
|
Raises an exception the moment result |
|
Raises an exception the moment a node produces output |
|
Prints extra information for shape-as-value tracking (e.g. inputs
to |
|
Prints which constant is being evaluated. |
|
Prints details when nodes from a local function domain are added. |
|
Raises an exception when a shape is missing for a result that should have one. |
|
Raises an exception as soon as a null/empty shape is encountered. |
|
Prints a message every time dynamic dimension |
|
Prints a message every time a node producing |
In addition,
get_debug_msg
returns a detailed text dump of the builder’s internal state (known shapes,
types, ranks, constants, and node list) which can be printed or logged whenever
an assertion fails.
pretty_text returns a
human-readable representation of the whole graph (inputs, initializers, nodes,
outputs) and is useful for quick visual inspection:
<<<
import onnx
import onnx.helper as oh
from yobx.xbuilder import GraphBuilder
TFLOAT = onnx.TensorProto.FLOAT
model = oh.make_model(
oh.make_graph(
[
oh.make_node("Add", ["X", "Y"], ["T"]),
oh.make_node("Relu", ["T"], ["Z"]),
],
"add_relu",
[
oh.make_tensor_value_info("X", TFLOAT, ["batch", 4]),
oh.make_tensor_value_info("Y", TFLOAT, ["batch", 4]),
],
[oh.make_tensor_value_info("Z", TFLOAT, ["batch", 4])],
),
opset_imports=[oh.make_opsetid("", 18)],
ir_version=10,
)
g = GraphBuilder(model)
print(g.pretty_text())
>>>
dyn---: batch -> WrapSym(batch)
dynrev: batch -> [('batch', SymInt(batch))]
dynsrc: batch -> [{batch:('input_name', 'X'), batch:('axis', 0)}, {batch:('input_name', 'Y'), batch:('axis', 0)}, {batch:('input_name', 'Z'), batch:('axis', 0)}]
opset: : 18
input:: X |T1: batch x 4
input:: Y |T1: batch x 4
Add: X, Y -> T |T1: batch x 4
Relu: T -> Z |T1: batch x 4
output:: Z |T1: batch x 4
See also
GraphBuilderProtocol,
GraphBuilderExtendedProtocol,
GraphBuilderTorchProtocol,
OpsetProtocol —
formal protocol interfaces satisfied by
GraphBuilder.
GraphBuilderPatternOptimizationProtocol
— the read-only API exposed to pattern authors; satisfied by
GraphBuilderPatternOptimization.
Expected API — the full list of methods and attributes
every graph builder must expose for use with to_onnx().
Pattern Optimizer — how the pattern optimizer uses
GraphBuilderPatternOptimization.