nodeclassifier#
Node classification module for AutoCast.
This module provides classes for classifying ONNX nodes based on various rules to determine which nodes should be converted to lower precision and which should remain in high precision. It includes rules for handling node names, operation types, initializer ranges, and I/O value ranges.
Classes
Rule for keeping nodes with high depth of reduction in high precision. |
|
Rule for keeping nodes with matching names in high precision. |
|
Rule for keeping nodes with specific operation types in high precision. |
|
Rule for keeping nodes with out-of-range inputs/outputs in high precision. |
|
Rule for force-including nodes with matching names in low precision. |
|
Rule for force-including specific operation types in low precision. |
|
Rule for keeping nodes with out-of-range initializers in high precision. |
|
Main class for classifying nodes into high and low precision groups. |
|
Base class for node classification rules. |
|
Rule for keeping the variance of decomposed RMSNorm in high precision. |
- class DepthOfReductionRule#
Bases:
NodeRuleBaseRule for keeping nodes with high depth of reduction in high precision.
- __init__(max_depth_of_reduction, reference_data, node_to_init_map, initializer_map)#
Initialize the rule.
- Parameters:
max_depth_of_reduction – Maximum depth of reduction allowed in low precision.
reference_data – Reference data for checking I/O ranges.
node_to_init_map – Mapping from node names to their initializers.
initializer_map – Mapping from initializer names to initializers.
- class DisabledNodeNameRegexRule#
Bases:
NodeRuleBaseRule for keeping nodes with matching names in high precision.
- __init__(disabled_node_name_regex)#
Initialize the rule.
- Parameters:
disabled_node_name_regex – List of regex patterns for node names to keep in high precision.
- class DisabledOpTypes#
Bases:
NodeRuleBaseRule for keeping nodes with specific operation types in high precision.
- __init__(op_types_to_exclude)#
Initialize the rule.
- Parameters:
op_types_to_exclude – List of operation types to keep in high precision.
- class IORangeRule#
Bases:
NodeRuleBaseRule for keeping nodes with out-of-range inputs/outputs in high precision.
Supports both single-batch (raw numpy arrays) and multi-batch (TensorStats objects) reference data for flexible precision conversion decisions.
- __init__(data_max, reference_data, node_to_init_map)#
Initialize the rule.
- Parameters:
data_max – Maximum absolute value allowed for node I/O.
reference_data – Reference data for checking I/O ranges. Can contain either raw numpy arrays (single batch) or TensorStats objects (multi-batch aggregated).
node_to_init_map – Mapping from node names to their initializers.
- class IncludeNodeNameRegexRule#
Bases:
DisabledNodeNameRegexRuleRule for force-including nodes with matching names in low precision.
Inherits matching behavior from DisabledNodeNameRegexRule but overrides logging.
- class IncludeOpTypes#
Bases:
DisabledOpTypesRule for force-including specific operation types in low precision.
Inherits matching behavior from DisabledOpTypes but overrides logging.
- class InitializerRangeRule#
Bases:
NodeRuleBaseRule for keeping nodes with out-of-range initializers in high precision.
- __init__(init_max, node_to_init_map)#
Initialize the rule.
- Parameters:
init_max – Maximum absolute value allowed for initializers.
node_to_init_map – Mapping from node names to their initializers.
- class NodeClassifier#
Bases:
objectMain class for classifying nodes into high and low precision groups.
- __init__(model, node_to_init_map=None, initializer_map=None, nodes_to_exclude=None, op_types_to_exclude=None, nodes_to_include=None, op_types_to_include=None, custom_rule=None, data_max=1000.0, init_max=np.float16(6.55e+04), max_depth_of_reduction=None, custom_ops_low_precision_nodes=None)#
Initialize the node classifier.
- Parameters:
model (ModelProto) – The ONNX model to classify nodes for.
node_to_init_map (dict[str, list[TensorProto]] | None) – Mapping from node names to their initializers.
initializer_map (dict[str, TensorProto] | None) – Mapping from initializer names to their tensors.
nodes_to_exclude (list[str] | None) – List of regex patterns for node names to keep in high precision.
op_types_to_exclude (list[str] | None) – List of operation types to keep in high precision.
nodes_to_include (list[str] | None) – List of regex patterns for node names to force-include in low precision.
op_types_to_include (list[str] | None) – List of operation types to force-include in low precision.
custom_rule (NodeRuleBase | None) – Optional custom classification rule.
data_max (float | None) – Maximum absolute value allowed for node I/O.
init_max (float | None) – Maximum absolute value allowed for initializers.
max_depth_of_reduction (int | None) – Maximum depth of reduction allowed in low precision.
custom_ops_low_precision_nodes (list[str] | None) – List of custom op node names to convert to low precision.
- run(ref_outputs_dict=None)#
Run node classification.
- Parameters:
ref_outputs_dict – Optional tensors’ reference data.
- Returns:
Lists of node names (low_precision_nodes, high_precision_nodes).
- Return type:
tuple
- class NodeRuleBase#
Bases:
objectBase class for node classification rules.
This class defines the interface for rules that determine whether a node should be kept in high precision or converted to low precision.
- check(node)#
Check if a node should be skipped based on the rule.
- Parameters:
node – The ONNX node to check.
- Returns:
True if the node should be kept in high precision, False otherwise.
- Return type:
bool
- class NormVarianceRule#
Bases:
NodeRuleBaseRule for keeping the variance of decomposed RMSNorm in high precision.
It also covers decomposed LayerNorm that
GraphSanitizerdid not fuse intoLayerNormalization.x * xoverflows fp16 once|x|exceeds about 256, which LLM residual streams with outlier channels routinely reach. Random reference inputs rarely produce such values, so the I/O range rule alone can leavePow -> ReduceMean -> Add -> Sqrtin fp16. Low-precision EPs then normalize those rows with an infinite variance and zero them; the CPU EP hides the problem because it computes these ops in fp32. The finalDiv/Mulonly sees values within fp16 range, so it stays eligible for low precision. The rule also runs for bf16 and with calibration data, for precision.nodes_to_includeopts specific nodes out.- __init__(model)#
Initialize the rule.
- Parameters:
model – The ONNX model to scan for variance subgraphs.