### hls4ml Convert Command Example Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/command.rst Example of using the 'hls4ml convert' command with a specified configuration file. ```bash hls4ml convert -c keras-config.yml ``` -------------------------------- ### Get Initial Vivado Configuration Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/configuration.rst Retrieves a starting configuration for the Vivado backend. This is useful for setting up a new HLS project. ```python config = hls4ml.templates.get_backend('Vivado').create_initial_config() ``` -------------------------------- ### hls4ml Build Command Example Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/command.rst Example of building an HLS project using the 'hls4ml build' command, specifying the project directory and enabling all build steps. ```bash hls4ml build -p my-hls-test -a ``` -------------------------------- ### Install hls4ml Source: https://github.com/fastmachinelearning/hls4ml/blob/main/README.md Install the hls4ml package using pip. Use the profiling extra for profiling dependencies. ```bash pip install hls4ml ``` ```bash pip install hls4ml[profiling] ``` -------------------------------- ### Basic hls4ml Workflow Example Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Demonstrates the initial steps of the hls4ml workflow: importing the library, constructing a Keras model, and generating an hls configuration. ```python import hls4ml from keras.models import Sequential from keras.layers import Dense, Activation # Construct a basic keras model model = Sequential() model.add(Dense(64, input_shape=(16,), activation='relu')) model.add(Dense(32, activation='relu')) # This is where you would train the model in a real-world scenario # Generate an hls configuration from the keras model ``` -------------------------------- ### Install hls4ml with QKeras frontend optional dependency Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Installs hls4ml with the 'qkeras' optional dependency group for the QKeras frontend. ```bash # For QKeras frontend pip install hls4ml[qkeras] ``` -------------------------------- ### YAML Configuration Example Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/configuration.rst An example of a YAML configuration file for hls4ml projects. It defines project, model, and backend settings, including output directory, model paths, FPGA part, and clock period. ```yaml # Project section OutputDir: my-hls-test ProjectName: myproject # Model section (Keras model) KerasJson: keras/KERAS_3layer.json KerasH5: keras/KERAS_3layer_weights.h5 #You can also use h5 file from Keras's model.save() without supplying json file. InputData: keras/KERAS_3layer_input_features.dat OutputPredictions: keras/KERAS_3layer_predictions.dat # Backend section (Vivado backend) Part: xcvu13p-flga2577-2-e ClockPeriod: 5 IOType: io_parallel # options: io_parallel/io_stream HLSConfig: Model: Precision: fixed<16,6> ReuseFactor: 1 Strategy: Latency LayerType: Dense: ReuseFactor: 2 Strategy: Resource Compression: True ``` -------------------------------- ### Create and Train HGQ Model Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/advanced/hgq1.rst Example of creating a sequential model using HGQ layers, compiling it, and fitting it with custom callbacks. This demonstrates the basic setup for using HGQ in a Keras model. ```Python import keras from HGQ.layers import HDense, HDenseBatchNorm, HQuantize from HGQ import ResetMinMax, FreeBOPs model = keras.models.Sequential([ HQuantize(beta=1.e-5), HDenseBatchNorm(32, beta=1.e-5, activation='relu'), HDenseBatchNorm(32, beta=1.e-5, activation='relu'), HDense(10, beta=1.e-5), ]) opt = keras.optimizers.Adam(learning_rate=0.001) loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True) model.compile(optimizer=opt, loss=loss, metrics=['accuracy']) callbacks = [ResetMinMax(), FreeBOPs()] model.fit(..., callbacks=callbacks) ``` -------------------------------- ### Install hls4ml with testing optional dependency Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Installs hls4ml with the 'testing' optional dependency group for testing (developers). ```bash # For testing (developers) pip install hls4ml[testing] ``` -------------------------------- ### Plugin Registration Function Example Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/advanced/plugins.rst This Python code demonstrates a minimal plugin registration function that registers a writer and a backend using provided helper functions. ```python # aie4ml/plugin.py from aie4ml.aie_backend import AIEBackend from aie4ml.writer import AIEWriter def register(*, register_backend, register_writer): register_writer('AIE', AIEWriter) register_backend('AIE', AIEBackend) ``` -------------------------------- ### Install Latest hls4ml Release Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Install the latest stable release of hls4ml using pip. This is the currently supported installation method. ```bash pip install hls4ml ``` -------------------------------- ### Create an HLS Project with Keras Model Source: https://github.com/fastmachinelearning/hls4ml/blob/main/README.md Fetch an example Keras model and convert it to an hls4ml project configuration. Lists available example models. ```Python import hls4ml # Fetch a keras model from our example repository # This will download our example model to your working directory and return an example configuration file config = hls4ml.utils.fetch_example_model('KERAS_3layer.json') # You can print the configuration to see some default parameters print(config) # Convert it to a hls project hls_model = hls4ml.converters.keras_v2_to_hls(config) # Print full list of example models if you want to explore more hls4ml.utils.fetch_example_list() ``` -------------------------------- ### Install hls4ml Development Version Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Install the development version of hls4ml directly from GitHub. This includes experimental features and bugfixes. ```bash pip install git+https://github.com/fastmachinelearning/hls4ml@main ``` -------------------------------- ### Install hls4ml with documentation building optional dependency Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Installs hls4ml with the 'doc' optional dependency group for documentation building (developers). ```bash # For documentation building (developers) pip install hls4ml[doc] ``` -------------------------------- ### YAML HLS Configuration with Multiple Layers Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/configuration.rst Example showing how to specify configurations for multiple layers within the HLSConfig. ```yaml HLSConfig: Model: ... LayerName: dense1: ... batchnormalization1: ... dense2: ... ``` -------------------------------- ### Install hls4ml with DSP-aware pruning optional dependency Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Installs hls4ml with the 'optimization' optional dependency group for DSP-aware pruning. ```bash # For DSP-aware pruning pip install hls4ml[optimization] ``` -------------------------------- ### Install hls4ml with weights and activation range visualization optional dependency Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Installs hls4ml with the 'profiling' optional dependency group for weights and activation range visualization. ```bash # For weights and activation range visualization pip install hls4ml[profiling] ``` -------------------------------- ### Install hls4ml with HGQ frontend optional dependency Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Installs hls4ml with the 'hgq' optional dependency group for the HGQ frontend. ```bash # For HGQ frontend pip install hls4ml[hgq] ``` -------------------------------- ### hls4ml CLI Help Overview Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/command.rst Displays the main usage and available commands for the hls4ml CLI. Use this to get a general overview of the tool's capabilities. ```bash usage: hls4ml [-h] [--version] {config,convert,build,report} ... HLS4ML - Machine learning inference in FPGAs positional arguments: {config,convert,build,report} config Create a conversion configuration file convert Convert Keras or ONNX model to HLS build Build generated HLS project report Show synthesis report of an HLS project optional arguments: -h, --help show this help message and exit --version show program's version number and exit ``` -------------------------------- ### hls4ml Report Command Example Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/command.rst Example of generating a full report for an HLS project using the 'hls4ml report' command. ```bash hls4ml report my-hls-test -f ``` -------------------------------- ### Install hls4ml with HGQ2 frontend optional dependency Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Installs hls4ml with the 'hgq2' optional dependency group for the HGQ2 frontend. ```bash # For HGQ2 frontend pip install hls4ml[hgq2] ``` -------------------------------- ### Install hls4ml with ONNX frontend optional dependency Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Installs hls4ml with the 'onnx' optional dependency group for the ONNX frontend. ```bash # For ONNX frontend pip install hls4ml[onnx] ``` -------------------------------- ### Run KL Layer Example Source: https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/contrib/kl_layer/README.md Execute the standalone Python script to demonstrate the usage of the custom KL divergence layer. Ensure you have the necessary Python environment set up. ```bash python kl_layer.py ``` -------------------------------- ### Defining Plugin Entry Point in pyproject.toml Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/advanced/plugins.rst A minimal plugin registers both a backend and an accompanying writer. This example shows how the 'aie4ml' package exposes its backend via pyproject.toml. ```toml [project.entry-points."hls4ml.backends"] AIE = "aie4ml.plugin:register" ``` -------------------------------- ### Install hls4ml with symbolic regression optional dependency Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Installs hls4ml with the 'sr' optional dependency group for symbolic regression. ```bash # For symbolic regression pip install hls4ml[sr] ``` -------------------------------- ### Install hls4ml with Quartus report parsing optional dependency Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Installs hls4ml with the 'quartus-report' optional dependency group for Quartus report parsing. ```bash # For Quartus report parsing pip install hls4ml[quartus-report] ``` -------------------------------- ### Install hls4ml with Distributed Arithmetic optional dependency Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Installs hls4ml with the 'da' optional dependency group for distributed arithmetic. ```bash # For distributed arithmetic pip install hls4ml[da] ``` -------------------------------- ### HGQ2 Model Setup with Quantizer and Layer Configuration Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/advanced/hgq.rst This snippet demonstrates setting up a Keras Sequential model using HGQ2's quantized layers (QConv2D, QDense). It shows how to configure quantization types, overflow modes, and enable Effective Bit-Operations (EBOPs) using nested QuantizerConfigScope and LayerConfigScope contexts. The configuration values shown are defaults for illustrative purposes. ```python import keras from hgq.layers import QDense, QConv2D from hgq.config import LayerConfigScope, QuantizerConfigScope # Setup quantization configuration # These values are the defaults, just for demonstration purposes here with ( # Configuration scope for setting the default quantization type and overflow mode # The second configuration scope overrides the first one for the 'datalane' place QuantizerConfigScope(place='all', default_q_type='kbi', overflow_mode='SAT_SYM'), # Configuration scope for enabling EBOPs and setting the beta0 value QuantizerConfigScope(place='datalane', default_q_type='kif', overflow_mode='WRAP'), LayerConfigScope(enable_ebops=True, beta0=1e-5), ): model = keras.Sequential([ QConv2D(32, (3, 3), activation='relu'), keras.layers.MaxPooling2D((2, 2)), keras.layers.Flatten(), QDense(10) ]) ... # Training, evaluation, and anything else you want to do with the model model_hls = hls4ml.converters.convert_from_keras(model, ...) # Model-wise precision propagation is done automatically for HGQ models for bit-exactness # Do NOT pass precision config if you don't know what you are doing model_hls.compile() ``` -------------------------------- ### Install hls4ml with Keras v3 frontend optional dependency Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Installs hls4ml with the 'keras-v3' optional dependency group for the Keras v3 frontend. ```bash # For Keras v3 frontend pip install hls4ml[keras-v3] ``` -------------------------------- ### hls4ml Command Help Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/command.rst Get detailed help for any specific hls4ml command. This is useful for understanding the options available for a particular subcommand. ```bash hls4ml command -h ``` -------------------------------- ### Structured Pruning with GPU FLOPs Objective Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/advanced/model_optimization.rst Shows how to optimize a model for GPU FLOPs using the hls4ml Optimization API. This example replaces the ParameterEstimator with GPUFLOPEstimator to target FLOP reduction, suitable for structured pruning. ```Python from hls4ml.optimization.dsp_aware_pruning.objectives.gpu_objectives import GPUFLOPEstimator # Optimize model # Note the change from ParameterEstimator to GPUFLOPEstimator optimized_model = optimize_model( baseline_model, model_attributes, GPUFLOPEstimator, scheduler, X_train, y_train, X_val, y_val, batch_size, epochs, optimizer, loss_fn, metric, increasing, rtol ) ``` -------------------------------- ### Package Bitfile and Handoff Files Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/backend/accelerator.rst Packages the generated bitfile, hardware handoff file, and Python driver into a tarball for deployment to the PYNQ board's PS. This prepares the necessary files for runtime execution. ```Bash mkdir -p package cp hls4ml_prj_pynq/myproject_vivado_accelerator/project_1.runs/impl_1/design_1_wrapper.bit package/hls4ml_nn.bit cp hls4ml_prj_pynq/myproject_vivado_accelerator/project_1.srcs/sources_1/bd/design_1/hw_handoff/design_1.hwh package/hls4ml_nn.hwh cp hls4ml_prj_pynq/axi_stream_driver.py package/ tar -czvf package.tar.gz -C package/ . ``` -------------------------------- ### hls4ml Config Command Help Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/command.rst Shows the specific arguments and options for the 'config' command, used to create conversion configuration files. ```bash hls4ml config -h ``` -------------------------------- ### Uninstall hls4ml using conda Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Removes the hls4ml package from your environment when installed via conda. ```bash conda remove hls4ml ``` -------------------------------- ### Uninstall hls4ml using pip Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Removes the hls4ml package from your environment when installed via pip. ```bash pip uninstall hls4ml ``` -------------------------------- ### Project and Build Variable Configuration Source: https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/templates/oneapi/CMakeLists.txt Sets up essential project variables including source files, library files, library stamp, target name, and library name. Also configures output directories for archives, libraries, and runtimes. ```cmake cmake_minimum_required (VERSION 3.7.2) project(myproject CXX) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) ``` ```cmake set(SOURCE_FILES src/firmware/myproject.cpp src/myproject_test.cpp) set(LIBRARY_FILES src/firmware/myproject.cpp src/myproject_bridge.cpp) set(LIB_STAMP mystamp) set(TARGET_NAME myproject) set(LIBRARY_NAME myproject-${LIB_STAMP}) ``` -------------------------------- ### Load Overlay and Predict with Hardware Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/backend/accelerator.rst Loads the neural network overlay onto the PYNQ board's PL and performs inference. Input and output data shapes are required to allocate buffers for data transfer. The predict method returns inference results, latency, and throughput. ```Python from axi_stream_driver import NeuralNetworkOverlay nn = NeuralNetworkOverlay('hls4ml_nn.bit', X_test.shape, y_test.shape) y_hw, latency, throughput = nn.predict(X_test, profile=True) ``` -------------------------------- ### hls4ml Config Command Usage Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/command.rst Demonstrates the basic usage of the 'hls4ml config' command to create a configuration file. Specify the model and output file. ```bash hls4ml config [-h] [-m MODEL] [-w WEIGHTS] [-o OUTPUT] ``` -------------------------------- ### Include and Link Path Configuration Source: https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/templates/oneapi/CMakeLists.txt Configures include directories, link directories, and link libraries based on user-defined paths and libraries. Includes messages to display the configured paths. ```cmake include_directories(${USER_INCLUDE_PATHS}) message(STATUS "Additional USER_INCLUDE_PATHS=${USER_INCLUDE_PATHS}") link_directories(${USER_LIB_PATHS}) message(STATUS "Additional USER_LIB_PATHS=${USER_LIB_PATHS}") link_libraries(${USER_LIBS}) message(STATUS "Additional USER_LIBS=${USER_LIBS}") ``` -------------------------------- ### Trace Model Outputs Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/ir/modelgraph.rst Performs an advanced prediction to trace outputs from individual layers of the ModelGraph. Returns both predictions and layer-wise trace outputs. Also shows how to get Keras model traces. ```python predict_ouputs, trace_outputs = hls_model.trace(X) #We also support a similar function for keras keras_trace = hls4ml.model.profiling.get_ymodel_keras(keras_model, X) ``` -------------------------------- ### C++ HLS Layer Implementation and Configuration Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/configuration.rst Illustrates the C++ HLS code for layer computation and activation, showing where layer-specific configurations are applied. ```cpp layer2_t layer2_out[N_LAYER_2]; #pragma HLS ARRAY_PARTITION variable=layer2_out complete dim=0 nnet::dense_latency(input_1, layer2_out, w2, b2); layer3_t layer3_out[N_LAYER_2]; #pragma HLS ARRAY_PARTITION variable=layer3_out complete dim=0 nnet::relu(layer2_out, layer3_out); layer4_t layer4_out[N_LAYER_4]; #pragma HLS ARRAY_PARTITION variable=layer4_out complete dim=0 nnet::dense_latency(layer3_out, layer4_out, w4, b4); nnet::sigmoid(layer4_out, layer5_out); ``` -------------------------------- ### Convert HGQ Model to hls4ml Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/advanced/hgq1.rst Demonstrates tracing min-max values, converting an HGQ model to a proxy model, and then converting the proxy model to an hls4ml compatible model using the Vivado backend. ```Python from HGQ import trace_minmax, to_proxy_model from hls4ml.converters import convert_from_keras_model trace_minmax(model, x_train, cover_factor=1.0) proxy = to_proxy_model(model, aggressive=True) model_hls = convert_from_keras_model(proxy, backend='vivado',output_dir=... ,part=...) ``` -------------------------------- ### Build HLS Project with Vivado Source: https://github.com/fastmachinelearning/hls4ml/blob/main/README.md Build the hls4ml project using Vivado HLS. This process may take several minutes. The report can be read afterwards. ```Python # Use Vivado HLS to synthesize the model # This might take several minutes hls_model.build() # Print out the report if you want hls4ml.report.read_vivado_report('my-hls-test') ``` -------------------------------- ### Convert Model to HLS Project Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/configuration.rst Create an HLS model using the generated configuration. Specify the output directory, IO type, and backend. Similar functions exist for ONNX and PyTorch models. ```python hls_model = hls4ml.converters.convert_from_keras_model( model, hls_config=config, output_dir="my_project_dir", io_type='io_stream', backend='Vitis' ) ``` -------------------------------- ### hls4ml Convert Command Usage Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/command.rst Shows the basic usage of the 'hls4ml convert' command, which requires a configuration file to convert models to HLS. ```bash hls4ml convert [-h] [-c CONFIG] ``` -------------------------------- ### hls4ml Build Command Usage Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/command.rst Displays the arguments for the 'hls4ml build' command, used to build HLS projects. Various flags control simulation, synthesis, and export. ```bash hls4ml build [-h] [-p PROJECT] [-c] [-s] [-r] [-v] [-e] [-l] [-a] [--reset] ``` -------------------------------- ### Build Target and Output Naming Source: https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/templates/oneapi/CMakeLists.txt Defines the names for various build targets (emulator, simulator, report, FPGA, IP export, library) and their corresponding output file names. ```cmake set(EMULATOR_TARGET fpga_emu) set(SIMULATOR_TARGET fpga_sim) set(REPORT_TARGET report) set(FPGA_TARGET fpga) set(IP_EXPORT_TARGET fpga_ip_export) set(LIBRARY_TARGET lib) set(EMULATOR_OUTPUT_NAME ${TARGET_NAME}.${EMULATOR_TARGET}) set(SIMULATOR_OUTPUT_NAME ${TARGET_NAME}.${SIMULATOR_TARGET}) set(REPORT_OUTPUT_NAME ${TARGET_NAME}.${REPORT_TARGET}) set(FPGA_OUTPUT_NAME ${TARGET_NAME}.${FPGA_TARGET}) set(IP_EXPORT_OUTPUT_NAME ${TARGET_NAME}.${IP_EXPORT_TARGET}) ``` -------------------------------- ### Basic Model Configuration (YAML) Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/configuration.rst Sets the default precision and reuse factor for all layers in the model. Use this for a uniform configuration across the entire network. ```yaml HLSConfig: Model: Precision: fixed<16,6> ReuseFactor: 1 ``` -------------------------------- ### Build HLS Model for Synthesis Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/intro/setup.rst Synthesizes the HLS model using Vitis HLS. This process can take several minutes. Optionally, read the Vivado report after building. ```python # Use Vitis HLS to synthesize the model # This might take several minutes hls_model.build() # Optional: print out the report hls4ml.report.read_vivado_report('my-hls-test') ``` -------------------------------- ### Generate ONNX Model Configuration Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/configuration.rst Generate configuration for an ONNX model with specified granularity, default precision, and backend. Granularity 'name' is recommended for more tuning options. ```python import hls4ml config = hls4ml.utils.config_from_onnx_model( model, granularity='name', default_precision='fixed<16,6>', backend='Vitis') ``` -------------------------------- ### FPGA Device and Compilation Flag Configuration Source: https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/templates/oneapi/CMakeLists.txt Configures the target FPGA device and customizes compilation flags for both general compilation and FPGA backend compilation. Allows setting user-defined flags and include paths. ```cmake if(NOT DEFINED FPGA_DEVICE) set(FPGA_DEVICE "Agilex7") endif() # Use cmake -DUSER_FPGA_FLAGS= to set extra flags for FPGA backend # compilation. set(USER_FPGA_FLAGS -Wno-unused-label;${USER_FPGA_FLAGS}) # Use cmake -DUSER_FLAGS= to set extra flags for general compilation. set(USER_FLAGS -Wno-unused-label -fconstexpr-steps=134217728 ${USER_FLAGS}) # Use cmake -DUSER_INCLUDE_PATHS= to set extra paths for general # compilation. set(USER_INCLUDE_PATHS src;src/firmware;${USER_INCLUDE_PATHS}) ``` -------------------------------- ### Generate Keras Model Configuration with Specific Backend Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/configuration.rst Generate configuration for a Keras model, specifying granularity, default precision, and backend. Passing the backend is recommended as some options depend on it. ```python import hls4ml config = hls4ml.utils.config_from_keras_model( model, granularity='name', default_precision='fixed<16,6>', backend='oneAPI') ``` -------------------------------- ### Generate Keras Model Configuration Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/api/configuration.rst Quickly generate a basic configuration dictionary from a Keras model. The granularity can be set to 'model' or 'name'. ```python import hls4ml config = hls4ml.utils.config_from_keras_model(model, granularity='model') ``` -------------------------------- ### Command-line ONNX Model Cleaning and Conversion Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/frontend/qonnx.rst Use these bash commands for cleaning ONNX models, converting them to channels-last format, and performing a final clean. ```bash $ qonnx_clean filename.onnx $ qonnx_to_channels_last filename_clean.onnx $ qonnx_clean filename_clean_channels_last.onnx # good to do a clean again as a last step ``` -------------------------------- ### Define FPGA Compile and Link Flags Source: https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/templates/oneapi/CMakeLists.txt Sets compile and link flags for FPGA IP export. These are temporary until native host pipes are fully supported. ```cmake set(IP_EXPORT_COMPILE_FLAGS -DFPGA_HARDWARE) set(IP_EXPORT_LINK_FLAGS -Xshardware -Xstarget=${FPGA_DEVICE} ${USER_FPGA_FLAGS} -fsycl-link=early -fsycl-device-code-split=per_kernel) ``` -------------------------------- ### Convert Keras Model and Build Bitfile Source: https://github.com/fastmachinelearning/hls4ml/blob/main/docs/backend/accelerator.rst Converts a Keras model to an hls4ml model using the VivadoAccelerator backend and builds the bitfile for a PYNQ-Z2 board. Ensure the Keras model and configuration are defined before running. ```Python import hls4ml config = hls4ml.utils.config_from_keras_model(model, granularity='name') hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config, output_dir='hls4ml_prj_pynq', backend='VivadoAccelerator', board='pynq-z2') hlsml.build(bitfile=True) ```