What is PQuantML?
mainhls4ml.repository·main·Indexed 24 days ago
https://github.com/fastmachinelearning/hls4mlA package for machine learning inference on FPGAs that translates models (such as Keras) into High-Level Synthesis (HLS) code for ultra-low-latency inference. It supports multiple backends including Xilinx Vivado HLS, Vitis HLS, Intel HLS, and Catapult HLS. Key features include automatic precision inference, Distributed Arithmetic (DA) optimization via da4ml, external BRAM weight loading using BramFactor, and an Extension API for implementing custom layers.
hls4ml.hls4ml is a tool designed to translate machine learning models from open-source frameworks (such as Keras and PyTorch) into High Level Synthesis (HLS) code. This HLS code can then be transpiled to run on FPGAs.
Key capabilities include:
hls4ml supports several machine learning frameworks (Frontends) and neural network architectures.
HGQ2 (High Granularity Quantization 2) is a quantization-aware training (QAT) framework built on Keras v3. It is designed for real-time deep learning applications on edge devices like FPGAs and integrates directly with hls4ml for hardware synthesis.
Key capabilities include:
TensorFlow, JAX, and PyTorch.einsum, einsum dense, and multi-head attention with quantization and hardware synthesis support.A hls4ml.model.flow.flow.Flow is an ordered sequence of optimizers representing a single stage in the conversion process.
Execution Logic:
transform method returns True, the flow is re-run until no more changes are detected.Key API Methods for Customization:
hls4ml.model.optimizer.optimizer.register_pass: Use this to register a new optimizer pass.hls4ml.model.flow.flow.register_flow: Use this to register a new flow.hls4ml.model.graph.ModelGraph.apply_flow: Use this to apply a registered flow to a model graph.Flow Types:
IP flow which requires several sub-flows to run first).When targeting AMD/Xilinx FPGAs, hls4ml provides two backends:
Vitis HLS compiler. This is the recommended backend for new designs. While the full Vitis accelerator flow is not directly supported, the HLS produced can be easily incorporated into a Vitis kernel.Vivado HLS compiler. This backend is primarily for legacy support; new hls4ml developments may not be backported to it.Both backends are designed to produce IP that can be incorporated into Vivado designs.
To prepare a model for code generation, hls4ml performs a series of transformations called optimization passes. These passes modify the internal model graph or layer attributes.
All optimizer passes must derive from the hls4ml.model.optimizer.optimizer.OptimizerPass class. They operate using two main functions:
match: A function that returns a boolean indicating if the pass should be applied to a specific node/layer.transform: Performs the actual transformation. It returns a boolean indicating if the model graph was modified. If transform returns True, the current flow of optimizers may be re-run to ensure all dependencies are satisfied.Types of Optimizer Passes:
hls4ml.model.optimizer.passes.hls4ml.backends.vivado.passes or hls4ml.backends.quartus.passes.hls4ml.model.optimizer.passes.stamp.MakeStamp).When using the BitExact pass, quantizers should be placed immediately after the inputs to ensure input precision is properly inferred.
QActivation with quantized_bits is placed right after the input layer so that the input precision can be derived.Convolutional layers (Conv1D/2D) are implemented based on the chosen io_type:
Best for small models requiring low latency.
ParallelizationFactor. Use the Resource strategy for larger models.Quartus, oneAPI, and Catapult backends by setting implementation to Winograd or combination. This can be faster but is less concerned with bit accuracy and overflow.Processes data serially (one pixel at a time).
Catapult and Vivado backends, configurable via the ConvImplementation option.Latency strategy is available.io_parallel/Latency strategy where the reuse_factor (RF) splits execution and limits multipliers. Note: in_width must be divisible by the RF.hls4ml supports both Keras v2 (tf.keras) and Keras v3. The mechanism for conversion differs by version:
Supported quantization frameworks include:
QEinsum, QMultiHeadAttention, and QUnaryFunctionLUT (arbitrary unary function as a 1-d lookup table).The MultiModelGraph class allows you to divide a large neural network into multiple smaller subgraphs (each a ModelGraph) by specifying split points at certain layers. This is useful for managing very large models, enabling step-wise optimization, or implementing modular design flows.
Key benefits include:
hls_multigraph_model = hls4ml.model.to_multi_model_graph(hls_model, ['layer3', 'layer7'])The SNN flow in hls4ml has the following characteristics:
Vitis backend.