Package: conifer
Conifer translates trained Boosted Decision Trees to FPGA firmware for extreme low latency inference.InstallationConifer is on the Python Package Index, so install it like:pip install coniferBuilding FPGA firmware requires tools from Xilinx - a High Level Synthesis tool and Vivado, depending on the choice of backend. We recommend the most recent version Vivado ML and Vitis HLS 2022.2.Using the C++ backends requires JSON header files fromhere. Clone or download the repository, and set the environment variableJSON_ROOTto the JSON include path. Xilinx's arbitrary precision header files (e.g.ap_fixed.h) are required to use these type for emulation. They are automatically found if you have source the Xilinx HLS toolchain, but you can also find themhere. If using the C++ backend without a Vivado installation, clone or download Xilinx's repository, and set the environment variableXILINX_HLSto the include path.AboutConifer converts from popular BDT training frameworks, and can emit code projects in different FPGA languages.Available converters:scikit-learnxgboostONNX - giving access to other training libraries such as lightGBM and CatBoost with ONNXMLToolsTMVATensorflow Decision Forest (tf_df)Available backends:Xilinx HLS - for best results use latest Vitis HLS, but Vivado HLS is also supported (conifer uses whichever is on your$PATH)VHDL - a direct-to-VHDL implementation, deeply pipelined for high clock frequenciesFPU - Forest Processing Unit reusable IP core for flexible BDT inferenceC++ - intended for bit-accurate emulation on CPU with a single include header filePython - intended for validation of model conversion and to allow inspection of a model without a configurationSee our paper in JINST: "Fast inference of Boosted Decision Trees in FPGAs for particle physics".Conifer originated as a development forhls4ml, and is developed under theFast Machine Learning Lab.UsageView the API reference at theconifer homepagefrom sklearn.ensemble import GradientBoostingClassifier
# Train a BDT
clf = GradientBoostingClassifier().fit(X_train, y_train)

# Create a conifer config dictionary
cfg = conifer.backends.xilinxhls.auto_config()
# Change the bit precision (print the config to see everything modifiable)
cfg['Precision'] = 'ap_fixed<12,4>' 

# Convert the sklearn model to a conifer model
model = conifer.converters.convert_from_sklearn(clf, cfg)
# Write the HLS project and compile the C++-Python bridge                      
model.compile()

# Run bit-accurate prediction on the CPU
y_hls = model.decision_function(X)
y_skl = clf.decision_function(X)

# Synthesize the model for the target FPGA
model.build()Check the examples directory for examples to get started with, and the BDT part of thehls4ml tutorial.Forest Processing UnitThe conifer Forest Processing Unit (FPU) is a flexible IP for fast BDT inference on FPGAs. One FPU can be configured to perform inference of different BDTs without rebuilding the IP or bitstream.<summary>More information</summary>FPUs comprise multiple Tree Engines (TEs) that operate in parallel. Each TE navigates a Decision Tree from root to leaf and outputs the leaf score. A summing network then combines the class scores to make the BDT prediction. TEs are programmed by the conifer compiler, allowing you to map different BDTs - for example with different numbers of nodes and maximum tree depth - onto the same FPU.Downloading the FPUPremade binaries for select boards are available fordownload here. Navigate to the conifer version, board and configuration and download the bitfile.Building the FPUIf you would like to build the FPU yourself, for example if you need a custom configuration or to target a different board, you can use theFPUBuilders inconifer.backends.fpu. Check thebuild_fpu.pyexample for ideas. You can change the number of tree engines, the number of nodes per engine, as well as the bitwidth allocated to each variable. All of this configuration is carried out through a configuration dictionary.Running the FPUThe coniferfpubackend maps your trained BDT onto a specific FPU configuration, and provides the driver to interact with the FPU - to load (and read) a BDT, and to perform inference.For a pynq-z2 board the first step is to copy thefpu_driver.pyand bitstream to the pynq-z2 SD card, then load it like this:from fpu_driver import ZynqDriver
fpu = ZynqDriver('fpu.bit')The FPU stores the configuration settings it was built with, which we can query like this:print(fpu.get_info())# model = json.load(open('prj_fpu_.../nodes.json'))
# fpu.load(model['nodes'], model['scales'])
# X = np.zeros(16, dtype='int32')
# fpu.predict(X)LicenseApart from the source code and binaries of the Forest Processing Unit (FPU),coniferis licensed underApache v2. The FPU source code and binaries are licensed under theCERN-OHL-P v2or later.