Skip to main content

Overview

The hls4ml API provides tools for converting machine learning models to FPGA firmware. This reference covers all major modules and functions.

Main Modules

Converters

Convert models from Keras, PyTorch, and ONNX

Model

Core model graph and layer management

Backends

FPGA backend implementations

Utils

Configuration and utility functions

Quick Start

Here’s a basic example of converting a Keras model:
import hls4ml

# Create configuration
config = hls4ml.utils.config.create_config(
    output_dir='my-hls-test',
    project_name='myproject',
    backend='Vivado'
)

# Convert Keras model
hls_model = hls4ml.converters.convert_from_keras_model(
    model,
    output_dir='my-hls-test',
    project_name='myproject',
    backend='Vivado'
)

# Compile and predict
hls_model.compile()
predictions = hls_model.predict(test_data)

Core Functions

Model Conversion

  • convert_from_keras_model() - Convert Keras models
  • convert_from_pytorch_model() - Convert PyTorch models
  • convert_from_onnx_model() - Convert ONNX models
  • convert_from_config() - Convert from YAML configuration

Configuration

  • create_config() - Create backend configuration
  • config_from_keras_model() - Generate config from Keras model
  • config_from_pytorch_model() - Generate config from PyTorch model
  • config_from_onnx_model() - Generate config from ONNX model

Model Operations

  • model.compile() - Compile the generated project
  • model.predict() - Run inference
  • model.build() - Synthesize with HLS tools
  • model.write() - Write project to disk

Module Structure

hls4ml/
├── converters/          # Model conversion modules
│   ├── keras/          # Keras converters
│   ├── pytorch/        # PyTorch converters
│   └── onnx/           # ONNX converters
├── model/              # Model graph representation
│   ├── graph.py        # ModelGraph class
│   └── optimizer/      # Optimization passes
├── backends/           # FPGA backends
│   ├── vivado/         # Xilinx Vivado backend
│   ├── vitis/          # Xilinx Vitis backend
│   └── quartus/        # Intel Quartus backend
└── utils/              # Utilities
    ├── config.py       # Configuration helpers
    └── report.py       # Report parsing

Architecture

Conversion Flow

  1. Parse - Extract model architecture and weights
  2. Transform - Convert to internal representation (ModelGraph)
  3. Optimize - Apply optimization passes
  4. Generate - Produce HLS C++ code
  5. Build - Synthesize with backend tools

ModelGraph

The ModelGraph is the central data structure representing your model:
  • Stores layers as nodes in a directed graph
  • Manages data flow between layers
  • Applies optimization transformations
  • Generates backend-specific code

Type System

hls4ml uses a sophisticated type system for precision:
  • FixedPrecisionType - Fixed-point types (e.g., ap_fixed<16,6>)
  • IntegerPrecisionType - Integer types (e.g., ap_int<8>)
  • FloatPrecisionType - Floating-point types
  • UnspecifiedPrecisionType - Auto-inferred precision

Next Steps

Keras Conversion

Learn about Keras model conversion

PyTorch Conversion

Learn about PyTorch model conversion

Model API

Explore the ModelGraph API

Configuration

Configure your conversion

Build docs developers (and LLMs) love