hls4ml.converters package

Submodules

hls4ml.converters.keras_to_hls module

class hls4ml.converters.keras_to_hls.KerasFileReader(config)

Bases: object

get_weights_data(layer_name, var_name)
get_weights_shape(layer_name, var_name)
class hls4ml.converters.keras_to_hls.KerasModelReader(keras_model)

Bases: object

get_weights_data(layer_name, var_name)
get_weights_shape(layer_name, var_name)
hls4ml.converters.keras_to_hls.compute_padding_1d(pad_type, in_size, stride, filt_size)
hls4ml.converters.keras_to_hls.compute_padding_2d(pad_type, in_height, in_width, stride_height, stride_width, filt_height, filt_width)
hls4ml.converters.keras_to_hls.get_qkeras_quantization(layer, keras_layer)
hls4ml.converters.keras_to_hls.get_supported_keras_layers()
hls4ml.converters.keras_to_hls.keras_handler(*args)
hls4ml.converters.keras_to_hls.keras_to_hls(config)
hls4ml.converters.keras_to_hls.parse_data_format(input_shape, data_format='channels_last')
hls4ml.converters.keras_to_hls.parse_default_keras_layer(keras_layer, input_names)
hls4ml.converters.keras_to_hls.register_keras_layer_handler(layer_name, handler_func)

hls4ml.converters.onnx_to_hls module

class hls4ml.converters.onnx_to_hls.ONNXDataReader(model)

Bases: object

ONNX data reader to be used for extracting relevant information during conversion.

add_input(layer_name, inputs, transpose=True, perm=None)
get_weights_data(layer_name, var_name)

Extract weights data from ONNX model.

Parameters
  • layer_name (string) – layer’s name in the ONNX model

  • var_name (string) – variable to be extracted

Returns

data – extracted weights data

Return type

numpy array

hls4ml.converters.onnx_to_hls.compute_pads_1d(operation, layer)
hls4ml.converters.onnx_to_hls.compute_pads_2d(operation, layer)
hls4ml.converters.onnx_to_hls.get_input_shape(model, operation, input_idx=0)
hls4ml.converters.onnx_to_hls.get_onnx_attribute(operation, name, default=None)
hls4ml.converters.onnx_to_hls.get_onnx_input_name(node, graph)

In ONNX, when calling node.input, it returns the node input’s index in the graph instead of the input’s name. However, the input’s name is used for indexing in HLSModel’s graph. This function return the input node’s name instead.

hls4ml.converters.onnx_to_hls.get_out_layer_name(graph)

Get the output layer’s name for the model. graph.output only returns the output’s node index

hls4ml.converters.onnx_to_hls.get_supported_onnx_layers()
hls4ml.converters.onnx_to_hls.onnx_handler(*args)
hls4ml.converters.onnx_to_hls.onnx_to_hls(config)

Convert onnx model to hls model from configuration.

Parameters

config (dict) – onnx configuration from yaml file or passed through API.

Returns

hls_model

Return type

hls4ml model object

hls4ml.converters.onnx_to_hls.register_onnx_layer_handler(layer_name, handler_func)
hls4ml.converters.onnx_to_hls.replace_char_inconsitency(name)

Replace some inconsistent characters that cause issues when writing into HLS.

hls4ml.converters.onnx_to_hls.sanitize_layer_name(layer)

hls4ml.converters.pytorch_to_hls module

hls4ml.converters.tf_to_hls module

hls4ml.converters.utils module

hls4ml.converters.utils.compute_padding_1d(pad_type, in_size, stride, filt_size)
hls4ml.converters.utils.compute_padding_2d(pad_type, in_height, in_width, stride_height, stride_width, filt_height, filt_width)
hls4ml.converters.utils.parse_data_format(input_shape, data_format='channels_last')

Module contents

hls4ml.converters.convert_from_config(config)

Convert to hls4ml model based on the provided configuration.

Parameters

config – A string containing the path to the YAML configuration file on the filesystem or a dict containig the parsed configuration.

Returns

hls4ml model.

Return type

HLSModel

hls4ml.converters.convert_from_keras_model(model, output_dir='my-hls-test', project_name='myproject', input_data_tb=None, output_data_tb=None, backend='Vivado', board=None, part=None, clock_period=5, io_type='io_parallel', hls_config={}, **kwargs)

Convert to hls4ml model based on the provided configuration. :param model: Keras model to convert :param output_dir: Output directory of the generated HLS

project. Defaults to ‘my-hls-test’.

Parameters
  • project_name (str, optional) – Name of the HLS project. Defaults to ‘myproject’.

  • input_data_tb (str, optional) – String representing the path of input data in .npy or .dat format that will be used during csim and cosim.

  • output_data_tb (str, optional) – String representing the path of output data in .npy or .dat format that will be used during csim and cosim.

  • backend (str, optional) – Name of the backend to use, e.g., ‘Vivado’ or ‘Quartus’.

  • board (str, optional) – One of target boards specified in supported_board.json file. If set to None a default device of a backend will be used. See documentation of the backend used.

  • part (str, optional) – The FPGA part. If set to None a default part of a backend will be used. See documentation of the backend used. Note that if board is specified, the part associated to that board will overwrite any part passed as a parameter.

  • clock_period (int, optional) – Clock period of the design. Defaults to 5.

  • io_type (str, optional) – Type of implementation used. One of ‘io_parallel’ or ‘io_serial’. Defaults to ‘io_parallel’.

  • hls_config (dict, optional) – The HLS config.

  • kwargs** (dict, optional) – Additional parameters that will be used to create the config of the specified backend

Raises

Exception – If precision and reuse factor are not present in ‘hls_config’

Returns

hls4ml model.

Return type

HLSModel

hls4ml.converters.convert_from_onnx_model(model, output_dir='my-hls-test', project_name='myproject', input_data_tb=None, output_data_tb=None, backend='Vivado', board=None, part=None, clock_period=5, io_type='io_parallel', hls_config={}, **kwargs)

Convert an ONNX model to a hls model.

Parameters
  • model (ONNX model object.) – Model to be converted to hls model object.

  • (str, optional) (io_type) – project. Defaults to ‘my-hls-test’.

  • (str, optional) – Defaults to ‘myproject’.

  • (str, optional) – used during csim and cosim.

  • (str, optional) – used during csim and cosim.

  • (str, optional) – or ‘Quartus’.

  • (str, optional) – device of a backend will be used. See documentation of the backend used.

  • (str, optional) – See documentation of the backend used. Note that if board is specified, the part associated to that board will overwrite any part passed as a parameter.

  • (int, optional) (clock_period) – Defaults to 5.

  • (str, optional) – ‘io_parallel’ or ‘io_serial’. Defaults to ‘io_parallel’.

  • (dict, optional) (kwargs**) –

  • (dict, optional)

Returns

hls_model

Return type

hls4ml model object.

See also

hls4ml.convert_from_keras_model(), hls4ml.convert_from_pytorch_model()

Examples

>>> import hls4ml
>>> config = hls4ml.utils.config_from_onnx_model(model, granularity='model')
>>> hls_model = hls4ml.converters.convert_from_onnx_model(model, hls_config=config)
hls4ml.converters.convert_from_pytorch_model(model, input_shape, output_dir='my-hls-test', project_name='myproject', input_data_tb=None, output_data_tb=None, backend='Vivado', board=None, part=None, clock_period=5, io_type='io_parallel', hls_config={}, **kwargs)

Convert a Pytorch model to a hls model.

Parameters
  • model (Pytorch model object.) – Model to be converted to hls model object.

  • input_shape (@todo: to be filled) –

  • (str, optional) (io_type) – project. Defaults to ‘my-hls-test’.

  • (str, optional) – Defaults to ‘myproject’.

  • (str, optional) – used during csim and cosim.

  • (str, optional) – used during csim and cosim.

  • (str, optional) – or ‘Quartus’.

  • (str, optional) – device of a backend will be used. See documentation of the backend used.

  • (str, optional) – See documentation of the backend used. Note that if board is specified, the part associated to that board will overwrite any part passed as a parameter.

  • (int, optional) (clock_period) – Defaults to 5.

  • (str, optional) – ‘io_parallel’ or ‘io_serial’. Defaults to ‘io_parallel’.

  • (dict, optional) (kwargs**) –

  • (dict, optional)

Returns

hls_model

Return type

hls4ml model object.

See also

hls4ml.convert_from_keras_model(), hls4ml.convert_from_onnx_model()

Examples

>>> import hls4ml
>>> config = hls4ml.utils.config_from_pytorch_model(model, granularity='model')
>>> hls_model = hls4ml.converters.convert_from_pytorch_model(model, hls_config=config)

Notes

Only sequential Pytorch models are supported for now.

hls4ml.converters.parse_yaml_config(config_file)

Parse conversion configuration from the provided YAML file.

This function parses the conversion configuration contained in the YAML file provided as an argument. It ensures proper serialization of hls4ml objects and should be called on YAML files created by hls4ml. A minimal valid YAML file may look like this:

KerasH5: my_keras_model.h5
OutputDir: my-hls-test
ProjectName: myproject
XilinxPart: xcku115-flvb2104-2-i
ClockPeriod: 5
IOType: io_stream
HLSConfig:
    Model:
    Precision: ap_fixed<16,6>
    ReuseFactor: 10

Please refer to the docs for more examples of valid YAML configurations.

Parameters

config_file (str) – Location of the file on the filesystem.

Returns

Parsed configuration.

Return type

dict