Overview
The flow system consists of two main components:- Optimization Passes - Individual transformations applied to the model
- Flows - Collections of passes with dependency management
Key Concepts
OptimizerPass
A single transformation that matches and modifies nodes
Flow
An ordered collection of optimization passes
Match
Predicate to determine if pass applies to a node
Transform
The actual modification to the model graph
Optimization Passes
OptimizerPass Base Class
All optimizer passes inherit from theOptimizerPass class:
hls4ml/model/optimizer/optimizer.py:8-33
Pass Types
Standard OptimizerPass
The standard pass matches specific nodes and transforms them:hls4ml/model/optimizer/passes/bn_fuse.py:8-99
GlobalOptimizerPass
Matches every node in the graph:hls4ml/model/optimizer/optimizer.py:43-47
LayerOptimizerPass
Optimizes a specific layer type:hls4ml/model/optimizer/optimizer.py:72-80
ModelOptimizerPass
Operates on the entire model:hls4ml/model/optimizer/optimizer.py:83-95
ConfigurableOptimizerPass
Pass with configurable parameters:hls4ml/model/optimizer/optimizer.py:98-111
Decorator-Based Passes
Passes can be defined using decorators:hls4ml/model/optimizer/optimizer.py:114-150
Flow System
Flow Class
A flow is a collection of optimizer passes with optional dependencies:hls4ml/model/flow/flow.py:1-24
DynamicFlow
A flow that dynamically determines its optimizers:hls4ml/model/flow/flow.py:33-62
Registering Flows
hls4ml/model/flow/flow.py:81-109
Applying Flows
Basic Application
hls4ml/model/graph.py:485-519
Flow Execution
hls4ml/model/graph.py:521-534
Optimization Loop
hls4ml/model/optimizer/optimizer.py:294-329
Common Optimization Patterns
Layer Fusion
Combining multiple layers into one:Layer Expansion
Splitting one layer into multiple:Type Inference
Automatically determining precisions:Backend-Specific Optimization
Pass Registration
Registering a Pass
hls4ml/model/optimizer/optimizer.py:225-252
Automatic Registration
Passes in thepasses/ directory are automatically discovered:
hls4ml/model/optimizer/optimizer.py:156-192
Example: Custom Flow
Defining a Custom Flow
Best Practices
Return True Only When Graph Changes
Return True Only When Graph Changes
Return
True from transform() only when you modify the graph structure (add/remove/replace nodes). Return False for attribute-only changes to allow other passes to continue.Check Parent/Child Usage
Check Parent/Child Usage
Before removing or modifying a node, check if it has multiple consumers using
get_output_use_map() to avoid breaking the graph.Validate Transformations
Validate Transformations
Always validate shape compatibility and type compatibility when replacing or removing nodes.
Use Flow Dependencies
Use Flow Dependencies
Specify flow dependencies with
requires to ensure passes run in the correct order.Make Passes Idempotent
Make Passes Idempotent
Design passes to be safely re-applied. The optimization loop may run passes multiple times.
Related Documentation
Intermediate Representation
Learn about Layer classes and attributes
Model Graph
Understand graph operations and structure
