Install hls4ml
Install hls4ml using pip:For profiling capabilities, install with the profiling extra:
hls4ml supports Python 3.8+. You’ll need an HLS tool (Vivado HLS, Vitis HLS, Quartus, etc.) installed for synthesis, but you can explore conversion without it.
Fetch an Example Model
hls4ml provides a collection of example models to get you started quickly. Let’s fetch a simple 3-layer Keras model:This downloads:
KERAS_3layer.json- Model architectureKERAS_3layer_weights.h5- Trained weightsKERAS_3layer_config.yml- Conversion configuration (if available)- Example input/output data for testing
Browse Available Example Models
Browse Available Example Models
See all available example models:This prints a list of all available models including Keras, PyTorch, ONNX, and TensorFlow formats.
Convert to HLS
Now convert the Keras model to HLS firmware:
Convert Your Own Model
Convert Your Own Model
Build the HLS Project
Synthesize the model using your HLS tool. This example uses Vivado HLS:The build process:
- Generates optimized C++ firmware code
- Runs C simulation (csim) to verify correctness
- Performs HLS synthesis to generate RTL
- Produces resource utilization and timing reports
Build Options
Build Options
Control which stages of the build to run:
View Results
After synthesis completes, examine the reports to understand resource usage and performance:The report shows:
- Latency: Clock cycles required for inference
- Interval: Throughput (how often you can process new inputs)
- Resource Usage: DSPs, BRAMs, LUTs, and FFs consumed
- Clock Period: Achieved timing
Understanding the Report
Understanding the Report
Key metrics in the synthesis report:
- Latency: Total time (in clock cycles) from input to output. Lower is better for real-time applications.
- Interval (II): Initiation interval - how many cycles between processing consecutive inputs. II=1 means full throughput.
- DSPs: Digital signal processing blocks used for multiplication/accumulation.
- BRAMs: Block RAM used for weights and intermediate values.
- LUTs: Look-up tables for logic implementation.
- FFs: Flip-flops for registers.
Complete Example
Here’s a complete end-to-end example you can copy and run:Next Steps
Now that you’ve converted your first model, explore more advanced features:Model Conversion
Learn about the conversion process and configuration options
Precision Optimization
Optimize resource usage with mixed precision and quantization
HLS Backends
Configure different HLS tools (Vivado, Vitis, Quartus)
Profiling
Profile your model to identify optimization opportunities
Different Model Frameworks
hls4ml supports multiple ML frameworks. Here’s how to convert models from each:- Keras
- PyTorch
- ONNX
- Config File
Common Issues
HLS tool not found
HLS tool not found
Make sure your HLS tool (Vivado HLS, Vitis HLS, etc.) is installed and available in your PATH:
Model conversion fails
Model conversion fails
Check that your layer types are supported:
Resource usage too high
Resource usage too high
Accuracy degradation
Accuracy degradation
The model accuracy may differ from the original due to quantization. Use profiling to identify problematic layers:See Profiling for more details.
Get Help
FAQ
Common questions and synthesis issues
GitHub Discussions
Ask questions and share your projects
Video Tutorial
Introduction to FPGAs, HLS, and ML inference
Full Tutorials
Detailed hands-on tutorials
