hls4ml.utils package

Submodules

hls4ml.utils.config module

hls4ml.utils.config.config_from_keras_model(model, granularity='model', backend=None, default_precision='fixed<16,6>', default_reuse_factor=1)

Create an HLS conversion config given the Keras model.

This function serves as the initial step in creating the custom conversion configuration. Users are advised to inspect the returned object to tweak the conversion configuration. The return object can be passed as hls_config parameter to convert_from_keras_model.

Parameters:
  • model – Keras model

  • granularity (str, optional) –

    Granularity of the created config. Defaults to ‘model’. Can be set to ‘model’, ‘type’ and ‘layer’.

    Granularity can be used to generate a more verbose config that can be fine-tuned. The default granulrity (‘model’) will generate config keys that apply to the whole model, so changes to the keys will affect the entire model. ‘type’ granularity will generate config keys that affect all layers of a given type, while the ‘name’ granularity will generate config keys for every layer separately, allowing for highly specific configuration tweaks.

  • backend (str, optional) – Name of the backend to use

  • default_precision (str, optional) – Default precision to use. Defaults to ‘fixed<16,6>’.

  • default_reuse_factor (int, optional) – Default reuse factor. Defaults to 1.

Raises:

Exception – If Keras model has layers not supported by hls4ml.

Returns:

The created config.

Return type:

[dict]

hls4ml.utils.config.config_from_onnx_model(model, granularity='model', backend=None, default_precision='ap_fixed<16,6>', default_reuse_factor=1)

Create an HLS conversion config given the ONNX model.

This function serves as the initial step in creating the custom conversion configuration. Users are advised to inspect the returned object to tweak the conversion configuration. The return object can be passed as hls_config parameter to convert_from_onnx_model.

Parameters:
  • model – ONNX model

  • granularity (str, optional) –

    Granularity of the created config. Defaults to ‘model’. Can be set to ‘model’, ‘type’ and ‘layer’.

    Granularity can be used to generate a more verbose config that can be fine-tuned. The default granulrity (‘model’) will generate config keys that apply to the whole model, so changes to the keys will affect the entire model. ‘type’ granularity will generate config keys that affect all layers of a given type, while the ‘name’ granularity will generate config keys for every layer separately, allowing for highly specific configuration tweaks.

  • backend (str, optional) – Name of the backend to use

  • default_precision (str, optional) – Default precision to use. Defaults to ‘fixed<16,6>’.

  • default_reuse_factor (int, optional) – Default reuse factor. Defaults to 1.

Raises:

Exception – If ONNX model has layers not supported by hls4ml.

Returns:

The created config.

Return type:

[dict]

hls4ml.utils.config.config_from_pytorch_model(model, granularity='model', backend=None, default_precision='ap_fixed<16,6>', default_reuse_factor=1)

Create an HLS conversion config given the PyTorch model.

This function serves as the initial step in creating the custom conversion configuration. Users are advised to inspect the returned object to tweak the conversion configuration. The return object can be passed as hls_config parameter to convert_from_pytorch_model.

Parameters:
  • model – PyTorch model

  • granularity (str, optional) –

    Granularity of the created config. Defaults to ‘model’. Can be set to ‘model’, ‘type’ and ‘layer’.

    Granularity can be used to generate a more verbose config that can be fine-tuned. The default granulrity (‘model’) will generate config keys that apply to the whole model, so changes to the keys will affect the entire model. ‘type’ granularity will generate config keys that affect all layers of a given type, while the ‘name’ granularity will generate config keys for every layer separately, allowing for highly specific configuration tweaks.

  • backend (str, optional) – Name of the backend to use

  • default_precision (str, optional) – Default precision to use. Defaults to ‘fixed<16,6>’.

  • default_reuse_factor (int, optional) – Default reuse factor. Defaults to 1.

Raises:

Exception – If PyTorch model has layers not supported by hls4ml.

Returns:

The created config.

Return type:

[dict]

hls4ml.utils.config.create_config(output_dir='my-hls-test', project_name='myproject', backend='Vivado', **kwargs)

hls4ml.utils.example_models module

hls4ml.utils.example_models.fetch_example_list()
hls4ml.utils.example_models.fetch_example_model(model_name, backend='Vivado')

Download an example model (and example data & configuration if available) from github repo to working directory, and return the corresponding configuration:

https://github.com/hls-fpga-machine-learning/example-models

Use fetch_example_list() to see all the available models.

Parameters:
  • model_name (str) – Name of the example model in the repo. Example: fetch_example_model(‘KERAS_3layer.json’)

  • backend (str, optional) – Name of the backend to use for model conversion.

Returns:

Dictionary that stores the configuration to the model

Return type:

dict

hls4ml.utils.fixed_point_utils module

class hls4ml.utils.fixed_point_utils.FixedPointEmulator(N, I, signed=True, integer_bits=None, decimal_bits=None)

Bases: object

Default constructor :param - N: Total number of bits in the fixed point number :param - I: Integer bits in the fixed point number :param - F = N-I: Fixed point bits in the number :param - signed: True/False - If True, use 2’s complement when converting to float :param - self.integer_bits: Bits corresponding to the integer part of the number :param - self.decimal_bits: Bits corresponding to the decimal part of the number

exp_float(sig_figs=12)
inv_float(sig_figs=12)
set_msb_bits(bits)
to_float()
hls4ml.utils.fixed_point_utils.ceil_log2(i)
hls4ml.utils.fixed_point_utils.uint_to_binary(i, N)

hls4ml.utils.plot module

Utilities related to model visualization.

hls4ml.utils.plot.add_edge(dot, src, dst)
hls4ml.utils.plot.check_pydot()

Returns True if PyDot and Graphviz are available.

hls4ml.utils.plot.model_to_dot(model, show_shapes=False, show_layer_names=True, show_precision=False, rankdir='TB', dpi=96, subgraph=False)

Convert a HLS model to dot format.

Parameters:
  • model – A HLS model instance.

  • show_shapes – whether to display shape information.

  • show_layer_names – whether to display layer names.

  • show_precision – whether to display precision of layer’s variables.

  • rankdirrankdir argument passed to PyDot, a string specifying the format of the plot: ‘TB’ creates a vertical plot; ‘LR’ creates a horizontal plot.

  • dpi – Dots per inch.

  • subgraph – whether to return a pydot.Cluster instance.

Returns:

A pydot.Dot instance representing the HLS model or a pydot.Cluster instance representing nested model if subgraph=True.

Raises:

ImportError – if graphviz or pydot are not available.

hls4ml.utils.plot.plot_model(model, to_file='model.png', show_shapes=False, show_layer_names=True, show_precision=False, rankdir='TB', dpi=96)

Converts a HLS model to dot format and save to a file.

Parameters:
  • model – A HLS model instance

  • to_file – File name of the plot image.

  • show_shapes – whether to display shape information.

  • show_layer_names – whether to display layer names.

  • show_precision – whether to display precision of layer’s variables.

  • rankdirrankdir argument passed to PyDot, a string specifying the format of the plot: ‘TB’ creates a vertical plot; ‘LR’ creates a horizontal plot.

  • dpi – Dots per inch.

Returns:

A Jupyter notebook Image object if Jupyter is installed. This enables in-line display of the model plots in notebooks.

hls4ml.utils.string_utils module

hls4ml.utils.string_utils.convert_to_pascal_case(snake_case)

Convert string in snake_case to PascalCase

Parameters:

snake_case (str) – string to convert

Returns:

converted string

Return type:

str

hls4ml.utils.string_utils.convert_to_snake_case(pascal_case)

Convert string in PascalCase to snake_case

Parameters:

pascal_case (str) – string to convert

Returns:

converted string

Return type:

str

Module contents