hls4ml.converters package
Subpackages
- hls4ml.converters.keras package
- Submodules
- hls4ml.converters.keras.convolution module
- hls4ml.converters.keras.core module
- hls4ml.converters.keras.graph module
- hls4ml.converters.keras.merge module
- hls4ml.converters.keras.pooling module
- hls4ml.converters.keras.qkeras module
- hls4ml.converters.keras.qkeras_layers module
- hls4ml.converters.keras.recurrent module
- hls4ml.converters.keras.reshape module
- hls4ml.converters.keras.reshaping module
- Module contents
- hls4ml.converters.onnx package
- hls4ml.converters.pytorch 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_model_arch(config)
- 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.parse_keras_model(model_arch, reader)
- hls4ml.converters.keras_to_hls.register_keras_layer_handler(layer_cname, handler_func)
Register a handler function for the given layer class name.
- The handler function should have the following signature:
parse_func(keras_layer, input_names, input_shapes, data_reader, config):
- Parameters
layer_cname (str) – The name of Keras layer (the ‘class_name’ property in the layer’s config)
handler_func (callable) – The handler function
- Raises
Exception – If the layer class has already been registered.
hls4ml.converters.onnx_to_hls module
- class hls4ml.converters.onnx_to_hls.ONNXDataReader(model)
Bases:
objectONNX 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 ModelGraph’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
ModelGraph
- 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
- class hls4ml.converters.pytorch_to_hls.PyTorchFileReader(config)
Bases:
PyTorchModelReader
- class hls4ml.converters.pytorch_to_hls.PyTorchModelReader(config)
Bases:
objectPytorch data reader to be used for extracting relevant information during conversion.
- get_weights_data(layer_name, var_name)
Get weights data from layers.
The hls layer classes are based on Keras’s default parameters. Thus, this function will also need to account for some differences between Keras and Pytorch terminology.
- 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.pytorch_to_hls.get_supported_pytorch_layers()
- hls4ml.converters.pytorch_to_hls.pytorch_handler(*args)
- hls4ml.converters.pytorch_to_hls.pytorch_to_hls(config)
Convert Pytorch model to hls model from configuration.
- Parameters
config (dict) – pytorch configuration from yaml file or passed through API.
- Returns
ModelGraph
- Return type
hls4ml model object.
Notes
Only sequential pytorch models are supported for now.
- hls4ml.converters.pytorch_to_hls.register_pytorch_layer_handler(layer_name, handler_func)
hls4ml.converters.tf_to_hls module
- class hls4ml.converters.tf_to_hls.TFDataReader(graph)
Bases:
object- get_weights_data(layer_name, var_name)
- read_variable_data(tensor)
- hls4ml.converters.tf_to_hls.tf_to_hls(yamlConfig)
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 containing the parsed configuration.
- Returns
hls4ml model.
- Return type
- 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', hls_config=None, **kwargs)
Convert to hls4ml model based on the provided configuration.
- Parameters
model – Keras model to convert
output_dir (str, optional) – Output directory of the generated HLS project. Defaults to ‘my-hls-test’.
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_stream’. 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
- 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', hls_config=None, **kwargs)
Convert an ONNX model to a hls model.
- Parameters
model (ONNX model object.) – Model to be converted to hls model object.
(str (io_type) – project. Defaults to ‘my-hls-test’.
optional) (Additional parameters that will be used to create the config of the specified backend) – project. Defaults to ‘my-hls-test’.
(str – Defaults to ‘myproject’.
optional) – Defaults to ‘myproject’.
(str – used during csim and cosim.
optional) – used during csim and cosim.
(str – used during csim and cosim.
optional) – used during csim and cosim.
(str – or ‘Quartus’.
optional) – or ‘Quartus’.
(str – device of a backend will be used. See documentation of the backend used.
optional) – device of a backend will be used. See documentation of the backend used.
(str – 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.
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 (clock_period) – Defaults to 5.
optional) – Defaults to 5.
(str – ‘io_parallel’ or ‘io_stream’. Defaults to ‘io_parallel’.
optional) – ‘io_parallel’ or ‘io_stream’. Defaults to ‘io_parallel’.
(dict (kwargs**) –
optional) –
(dict –
optional) –
- Returns
ModelGraph
- Return type
hls4ml model object.
See also
hls4ml.convert_from_keras_model,hls4ml.convert_from_pytorch_modelExamples
>>> 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', hls_config=None, **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 (io_type) – project. Defaults to ‘my-hls-test’.
optional) (Additional parameters that will be used to create the config of the specified backend) – project. Defaults to ‘my-hls-test’.
(str – Defaults to ‘myproject’.
optional) – Defaults to ‘myproject’.
(str – used during csim and cosim.
optional) – used during csim and cosim.
(str – used during csim and cosim.
optional) – used during csim and cosim.
(str – or ‘Quartus’.
optional) – or ‘Quartus’.
(str – device of a backend will be used. See documentation of the backend used.
optional) – device of a backend will be used. See documentation of the backend used.
(str – 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.
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 (clock_period) – Defaults to 5.
optional) – Defaults to 5.
(str – ‘io_parallel’ or ‘io_stream’. Defaults to ‘io_parallel’.
optional) – ‘io_parallel’ or ‘io_stream’. Defaults to ‘io_parallel’.
(dict (kwargs**) –
optional) –
(dict –
optional) –
- Returns
ModelGraph
- Return type
hls4ml model object.
See also
hls4ml.convert_from_keras_model,hls4ml.convert_from_onnx_modelExamples
>>> 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 Part: 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