Optimization Passes
Timber runs 6 domain-specific optimization passes on the IR. Each pass is designed for tree-based models and operates on the IR in-place.
Pass 1: Dead Leaf Elimination
File: timber/optimizer/dead_leaf.py
Prunes leaves whose contribution is negligible relative to the maximum leaf value in the tree.
Algorithm:
- Compute
max_leaf_valueacross all leaves in the tree - If
|leaf_value| / |max_leaf_value| < threshold(default: 1e-6), mark the leaf as dead - If both children of a node are dead, collapse the node to a leaf with the weighted average of children
Effect: Reduces tree depth and generated code size. A 50-tree model may lose 2-5% of nodes.
Pass 2: Constant Feature Detection
File: timber/optimizer/constant_feature.py
Identifies internal nodes where both children have identical leaf values — the split is redundant regardless of the input.
Algorithm:
- Post-order traversal of each tree
- If
left_child.leaf_value == right_child.leaf_valuefor a node, replace the node with a leaf
Effect: Eliminates unnecessary comparisons. Common in models trained with early stopping where some splits are noise.
Pass 3: Threshold Quantization
File: timber/optimizer/threshold_quant.py
Analyzes all split thresholds per feature to determine the minimum precision required.
Algorithm:
- Collect all thresholds for each feature across all trees
- Compute the minimum gap between consecutive thresholds
- Determine if int8, int16, float16, or float32 is sufficient
- Store precision metadata as
QuantizationHintannotations
Effect: Produces metadata for potential future narrow-type SIMD backends. Currently informational.
Pass 4: Frequency-Ordered Branch Sorting
File: timber/optimizer/branch_sort.py
Reorders tree children so the most frequently taken branch is the fall-through path.
Algorithm:
- Feed calibration data through each tree
- Count left/right branch frequencies at each node
- If
right_count > left_count, swap children and invert the comparison - The "fall-through" (more common) branch becomes the first clause in generated
if/else
Effect: Improves CPU branch prediction hit rate. Requires calibration data (--calibration-data). When no data is provided, the pass is a no-op.
Pass 5: Pipeline Fusion
File: timber/optimizer/pipeline_fusion.py
Absorbs a preceding ScalerStage into tree thresholds.
Algorithm:
- If the IR has
[ScalerStage(μ, σ), TreeEnsembleStage] - For each split on feature
i:θ' = θ × σ_i + μ_i - Remove the
ScalerStagefrom the pipeline
Effect: Eliminates the entire preprocessing step at inference time. The compiled code operates directly on raw features. This is the key optimization for sklearn Pipeline deployments.
Mathematical guarantee: The transformation (x - μ) / σ < θ is equivalent to x < θ × σ + μ, so predictions are numerically identical.
Pass 6: Vectorization Analysis
File: timber/optimizer/vectorize.py
Analyzes tree structure to identify SIMD batching opportunities.
Algorithm:
- Compute depth profile of each tree
- Analyze feature access patterns (sequential vs. random)
- Identify groups of structurally identical trees
- Produce
VectorizationHintannotations
Effect: Produces metadata for future SIMD code generation. Trees with identical structure can share a single control flow and process multiple inputs via SIMD lanes.
Pass Execution Order
The passes run in the order listed above. This order matters:
- Dead leaf first — reduces tree size before other passes analyze it
- Constant feature second — further simplification
- Threshold quant third — needs final threshold values
- Branch sort fourth — needs final tree structure
- Pipeline fusion fifth — modifies thresholds, must run before quant analysis is consumed
- Vectorization last — needs the final, fully optimized tree structure
Audit Integration
Each pass logs its result to the audit trail:
{
"name": "dead_leaf_elimination",
"changed": true,
"nodes_before": 1550,
"nodes_after": 1523,
"duration_ms": 1.2
}