hls4ml.model.profiling module provides tools to analyze and visualize model weights and activations, helping you choose appropriate precision settings and validate model conversions.
Installation
Profiling requires additional dependencies. Install them with:Numerical Profiling
Thenumerical() function profiles weights and activations to help determine appropriate fixed-point precision:
Return Values
Thenumerical() function returns four matplotlib figures:
- wp: Weights distribution (before optimization)
- wph: Weights distribution (after optimization)
- ap: Activations distribution (before optimization)
- aph: Activations distribution (after optimization)
Plot Types
You can specify different visualization styles:Boxplot (Default)
Shows the median and quartiles of weight/activation distributions with box-and-whisker diagrams:- Box: Contains the middle 50% of values (25th to 75th percentile)
- Whiskers: Extend to minimum and maximum values
- Grey boxes: Show the range representable with current precision settings
Histogram
Displays frequency distributions on a log scale:Usage Patterns
Different input combinations produce different outputs:- Model Only
- HLS Model Only
- Model + Data
- Full Profiling
Interpreting Results
The grey boxes in profiling plots indicate the range representable by your configured precision:Good Configuration: Grey box covers the full range of the box-and-whisker plot
Optimization Impact
Notice differences between “before” and “after” optimization plots:- BatchNormalization layers may be fused into preceding layers
- Layer names may change after graph optimizations
- Weight distributions change due to layer folding
When tuning precision, always work with the “after optimization” (final) model since that’s what gets synthesized.
Layer-by-Layer Comparison
Compare outputs between the original model and hls4ml conversion:Comparison Methods
- dist_diff
- norm_diff
Weights and Activations Utilities
For custom analysis, you can extract raw data:For Keras Models
For PyTorch Models
Visualization Tools
Thehls4ml.utils.plot module provides model visualization:
Visualization Options
- show_shapes: Display tensor shapes for inputs/outputs
- show_layer_names: Show layer names in the diagram
- show_precision: Display precision types for all variables
- rankdir: Graph orientation (
'TB'vertical or'LR'horizontal) - dpi: Image resolution
Visualization requires
pydot and graphviz installed: pip install pydot graphvizBest Practices
Profile with representative data
Use a sufficiently large and diverse test dataset that covers the full range of expected inputs.
Check both weights and activations
Both weight and activation profiles are important for choosing precision. Activations may have different ranges than weights.
Iterate on precision configuration
Start with profiling results, adjust precision, re-profile, and verify with C simulation.
Compare optimized model
Always use the “after optimization” plots when tuning precision, as layer fusion and other optimizations affect value ranges.
API Reference
numerical()
model: Keras or PyTorch model (optional)hls_model: hls4ml ModelGraph (optional)X: Test data for activation profiling (optional)plot: Plot style -'boxplot','histogram', or'violinplot'
compare()
keras_model: Original Keras modelhls_model: Converted hls4ml ModelGraph withTrace: TrueX: Input test dataplot_type:'dist_diff'or'norm_diff'
plot_model()
model: hls4ml ModelGraphto_file: Output filename (PNG, PDF, etc.)show_shapes: Display tensor shapesshow_layer_names: Display layer namesshow_precision: Display precision informationrankdir: Graph direction ('TB'or'LR')dpi: Image resolution
