yobx.helpers.stats_helper#
ModelStatistics#
- class yobx.helpers.ModelStatistics(model: ModelProto | GraphBuilderExtendedProtocol, verbose: int = 0)[source]#
Computes statistics on an ONNX model, including node counts per op_type and estimated FLOPs.
model may be either an
onnx.ModelProtoor aGraphBuilderExtendedProtocolinstance. When a graph builder is provided its already-computed shape information is used directly (no second shape-inference pass is run) and the ONNX model is obtained viato_onnx().- Parameters:
model – ONNX model or graph builder
verbose – verbosity level passed to
BasicShapeBuilder(ignored when model is a graph builder)
Usage:
stats = ModelStatistics(model).compute()
- compute() Dict[str, Any][source]#
Runs the full analysis and returns a statistics dictionary.
- Returns:
dictionary with the following keys:
"n_nodes"– total number of nodes"node_count_per_op_type"– dict mapping op_type → count"total_estimated_flops"– total estimated FLOPs (int or None)"flops_per_op_type"– dict mapping op_type → estimated FLOPs (None means the cost could not be estimated for some nodes)"node_stats"– list of per-node dicts with keysop_type,name,inputs,outputs,estimated_flops
model_statistics#
- yobx.helpers.model_statistics(model: ModelProto | GraphBuilderExtendedProtocol, verbose: int = 0) Dict[str, Any][source]#
Computes statistics on an ONNX model.
This is a convenience wrapper around
ModelStatistics.- Parameters:
model – ONNX model or graph builder
verbose – verbosity level
- Returns:
statistics dictionary — see
ModelStatistics.compute()for details
NodeStatistics#
- class yobx.helpers.NodeStatistics(parent: GraphProto | FunctionProto, node: NodeProto)[source]#
Stores per-node statistics for a
onnx.NodeProto.- Parameters:
parent – the
GraphProtoorFunctionProtothat contains nodenode – the ONNX node being described
TreeStatistics#
HistTreeStatistics#
- class yobx.helpers.HistTreeStatistics(node: NodeProto, featureid: int, values: ndarray, bins: int = 20)[source]#
Stores threshold-distribution statistics for a single feature across all trees in a
TreeEnsemble*node.- Parameters:
node – the
TreeEnsembleClassifierorTreeEnsembleRegressornodefeatureid – zero-based feature index
values – array of threshold values for featureid
bins – number of histogram bins (default
20)
HistStatistics#
- class yobx.helpers.HistStatistics(parent: GraphProto | FunctionProto, node: NodeProto | TensorProto | SparseTensorProto, bins: int = 20)[source]#
Stores distribution statistics for a constant tensor (initializer or
Constantnode).- Parameters:
parent – the
GraphProtoorFunctionProtothat contains nodenode – a
NodeProto(Constantop),TensorProto, orSparseTensorProtobins – number of histogram bins (default
20)
extract_attributes#
- yobx.helpers.extract_attributes(node: NodeProto) Dict[str, Any][source]#
Extracts all attributes of a node into a plain Python/NumPy dictionary.
Delegates to
attr_proto_to_python()for scalar and tensor attribute types. List-typed attributes (INTS,FLOATS,STRINGS) are returned as NumPy arrays so that callers can use boolean-mask indexing directly.GRAPHand ref-attribute entries are stored asNone.- Parameters:
node – node to inspect
- Returns:
dictionary mapping attribute name to a Python/NumPy value, or
Nonefor graph and ref-attribute entries.
stats_tree_ensemble#
- yobx.helpers.stats_tree_ensemble(parent: GraphProto | FunctionProto, node: NodeProto) NodeStatistics[source]#
Computes statistics on every tree of a
TreeEnsembleClassifier,TreeEnsembleRegressor, orTreeEnsemble(ai.onnx.mlopset 5) node.The returned
NodeStatisticsinstance contains the following entries:"kind"–"Classifier","Regressor", or"TreeEnsemble""n_trees"– total number of trees"n_outputs"– number of outputs / classes"max_featureid"– maximum feature index used across all nodes"n_features"– number of distinct features used across all nodes"n_rules"– number of distinct node modes (split types) used"rules"–setof node mode strings (e.g.{"BRANCH_LEQ", "LEAF"})"hist_rules"–collections.Counterof node mode frequencies"features"– list ofHistTreeStatistics, one per feature"trees"– list ofTreeStatistics, one per tree
Each
TreeStatisticsin"trees"contains:"n_nodes"– total nodes in the tree"n_leaves"– leaf nodes"max_featureid"– maximum feature index"n_features"– distinct feature count"n_rules"– distinct split-mode count"rules"–setof mode strings"hist_rules"–collections.Counterof mode frequencies
For
TreeEnsembleClassifier/TreeEnsembleRegressor(ai.onnx.mlopset ≤ 4) the legacy flatnodes_treeids/nodes_values/ stringnodes_modesattributes are used. For the unifiedTreeEnsembleoperator (ai.onnx.mlopset ≥ 5) thetree_roots/nodes_splits/nodes_modes(UINT8 tensor) attributes are used instead.- Parameters:
parent – the
GraphProtoorFunctionProtothat contains nodenode – a
TreeEnsembleClassifier,TreeEnsembleRegressor, orTreeEnsemblenode
- Returns:
NodeStatisticspopulated with the statistics listed above- Raises:
KeyError – if required tree-structure attributes are missing from node
enumerate_nodes#
- yobx.helpers.enumerate_nodes(onx: FunctionProto | GraphProto | ModelProto, recursive: bool = True) Iterable[Tuple[Tuple[str, ...], GraphProto | FunctionProto, NodeProto | TensorProto | SparseTensorProto]][source]#
Enumerates all nodes in a model.
- Parameters:
onx – the model, graph, or function to traverse
recursive – if
True, recurse into sub-graphs (e.g. insideIf/Loop/Scan)
- Returns:
yields tuples
(path, parent, node)where path is a tuple of name strings identifying the location of node in the model, parent is the containingGraphProtoorFunctionProto, and node is aNodeProto,TensorProto, orSparseTensorProto.
enumerate_stats_nodes#
- yobx.helpers.enumerate_stats_nodes(onx: FunctionProto | GraphProto | ModelProto, recursive: bool = True, stats_fcts: Dict[Tuple[str, str], Callable[[GraphProto | FunctionProto, NodeProto | TensorProto | SparseTensorProto], NodeStatistics | HistStatistics]] | None = None) Iterable[Tuple[Tuple[str, ...], GraphProto | FunctionProto, NodeStatistics | HistStatistics]][source]#
Iterates over nodes in onx, yielding statistics for those that match entries in stats_fcts.
By default the function handles both
TreeEnsembleClassifierandTreeEnsembleRegressornodes in the"ai.onnx.ml"domain viastats_tree_ensemble().- Parameters:
onx – the model, graph, or function to traverse
recursive – if
True, recurse into sub-graphsstats_fcts – mapping of
(domain, op_type)to a callable that accepts(parent, node)and returns a statistics object. WhenNonethe default handlers for tree-ensemble operators are used.
- Returns:
yields tuples
(path, parent, statistics)for every matched node
Module#
Functions to compute statistics on an ONNX model such as number of nodes
per op_type and estimation of computational cost. Also provides classes
and helpers for computing per-tree statistics on TreeEnsemble* operators.
- yobx.helpers.stats_helper.enumerate_nodes(onx: FunctionProto | GraphProto | ModelProto, recursive: bool = True) Iterable[Tuple[Tuple[str, ...], GraphProto | FunctionProto, NodeProto | TensorProto | SparseTensorProto]][source]#
Enumerates all nodes in a model.
- Parameters:
onx – the model, graph, or function to traverse
recursive – if
True, recurse into sub-graphs (e.g. insideIf/Loop/Scan)
- Returns:
yields tuples
(path, parent, node)where path is a tuple of name strings identifying the location of node in the model, parent is the containingGraphProtoorFunctionProto, and node is aNodeProto,TensorProto, orSparseTensorProto.
- yobx.helpers.stats_helper.enumerate_stats_nodes(onx: FunctionProto | GraphProto | ModelProto, recursive: bool = True, stats_fcts: Dict[Tuple[str, str], Callable[[GraphProto | FunctionProto, NodeProto | TensorProto | SparseTensorProto], NodeStatistics | HistStatistics]] | None = None) Iterable[Tuple[Tuple[str, ...], GraphProto | FunctionProto, NodeStatistics | HistStatistics]][source]#
Iterates over nodes in onx, yielding statistics for those that match entries in stats_fcts.
By default the function handles both
TreeEnsembleClassifierandTreeEnsembleRegressornodes in the"ai.onnx.ml"domain viastats_tree_ensemble().- Parameters:
onx – the model, graph, or function to traverse
recursive – if
True, recurse into sub-graphsstats_fcts – mapping of
(domain, op_type)to a callable that accepts(parent, node)and returns a statistics object. WhenNonethe default handlers for tree-ensemble operators are used.
- Returns:
yields tuples
(path, parent, statistics)for every matched node
- yobx.helpers.stats_helper.extract_attributes(node: NodeProto) Dict[str, Any][source]#
Extracts all attributes of a node into a plain Python/NumPy dictionary.
Delegates to
attr_proto_to_python()for scalar and tensor attribute types. List-typed attributes (INTS,FLOATS,STRINGS) are returned as NumPy arrays so that callers can use boolean-mask indexing directly.GRAPHand ref-attribute entries are stored asNone.- Parameters:
node – node to inspect
- Returns:
dictionary mapping attribute name to a Python/NumPy value, or
Nonefor graph and ref-attribute entries.
- yobx.helpers.stats_helper.model_statistics(model: ModelProto | GraphBuilderExtendedProtocol, verbose: int = 0) Dict[str, Any][source]#
Computes statistics on an ONNX model.
This is a convenience wrapper around
ModelStatistics.- Parameters:
model – ONNX model or graph builder
verbose – verbosity level
- Returns:
statistics dictionary — see
ModelStatistics.compute()for details
- yobx.helpers.stats_helper.stats_tree_ensemble(parent: GraphProto | FunctionProto, node: NodeProto) NodeStatistics[source]#
Computes statistics on every tree of a
TreeEnsembleClassifier,TreeEnsembleRegressor, orTreeEnsemble(ai.onnx.mlopset 5) node.The returned
NodeStatisticsinstance contains the following entries:"kind"–"Classifier","Regressor", or"TreeEnsemble""n_trees"– total number of trees"n_outputs"– number of outputs / classes"max_featureid"– maximum feature index used across all nodes"n_features"– number of distinct features used across all nodes"n_rules"– number of distinct node modes (split types) used"rules"–setof node mode strings (e.g.{"BRANCH_LEQ", "LEAF"})"hist_rules"–collections.Counterof node mode frequencies"features"– list ofHistTreeStatistics, one per feature"trees"– list ofTreeStatistics, one per tree
Each
TreeStatisticsin"trees"contains:"n_nodes"– total nodes in the tree"n_leaves"– leaf nodes"max_featureid"– maximum feature index"n_features"– distinct feature count"n_rules"– distinct split-mode count"rules"–setof mode strings"hist_rules"–collections.Counterof mode frequencies
For
TreeEnsembleClassifier/TreeEnsembleRegressor(ai.onnx.mlopset ≤ 4) the legacy flatnodes_treeids/nodes_values/ stringnodes_modesattributes are used. For the unifiedTreeEnsembleoperator (ai.onnx.mlopset ≥ 5) thetree_roots/nodes_splits/nodes_modes(UINT8 tensor) attributes are used instead.- Parameters:
parent – the
GraphProtoorFunctionProtothat contains nodenode – a
TreeEnsembleClassifier,TreeEnsembleRegressor, orTreeEnsemblenode
- Returns:
NodeStatisticspopulated with the statistics listed above- Raises:
KeyError – if required tree-structure attributes are missing from node