hls4ml.model package

Submodules

hls4ml.model.hls_layers module

class hls4ml.model.hls_layers.Activation(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.ArrayVariable(shape, dim_names, var_name='layer{index}', type_name='layer{index}_t', precision=None, pragma='partition', **kwargs)

Bases: hls4ml.model.hls_layers.Variable

definition_cpp()
get_shape()
size()
size_cpp()
class hls4ml.model.hls_layers.BatchNormalization(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.BiasAdd(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Merge

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.CompressedType(name, precision, index_precision, **kwargs)

Bases: hls4ml.model.hls_layers.HLSType

definition_cpp()
class hls4ml.model.hls_layers.CompressedWeightVariable(var_name, type_name, precision, data, reuse_factor, quantizer=None, **kwargs)

Bases: hls4ml.model.hls_layers.WeightVariable

next()
class hls4ml.model.hls_layers.Concatenate(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Merge

config_cpp()
initialize()
class hls4ml.model.hls_layers.Conv1D(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.Conv2D(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.Conv2DBatchnorm(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Conv2D

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.Dense(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.DepthwiseConv2D(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Conv2D

initialize()
class hls4ml.model.hls_layers.Dot(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Merge

config_cpp()
initialize()
class hls4ml.model.hls_layers.ExponentPrecisionType(width=16, signed=True)

Bases: hls4ml.model.hls_layers.IntegerPrecisionType

Convenience class to differentiate ‘regular’ integers from those which represent exponents, for QKeras po2 quantizers, for example.

class hls4ml.model.hls_layers.ExponentType(name, precision, **kwargs)

Bases: hls4ml.model.hls_layers.HLSType

definition_cpp()
class hls4ml.model.hls_layers.ExponentWeightVariable(var_name, type_name, precision, data, quantizer, **kwargs)

Bases: hls4ml.model.hls_layers.WeightVariable

next()
class hls4ml.model.hls_layers.FixedPrecisionType(width=16, integer=6, signed=True, rounding_mode=None, saturation_mode=None, saturation_bits=None)

Bases: object

class hls4ml.model.hls_layers.GarNet(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
ref_impl = False
class hls4ml.model.hls_layers.GarNetStack(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.GarNet

class hls4ml.model.hls_layers.GlobalPooling1D(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.GlobalPooling2D(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.HLSType(name, precision, **kwargs)

Bases: object

definition_cpp()
class hls4ml.model.hls_layers.InplaceVariable(shape, dim_names, proxy, **kwargs)

Bases: object

definition_cpp()
get_shape()
size_cpp()
class hls4ml.model.hls_layers.Input(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.IntegerPrecisionType(width=16, signed=True)

Bases: object

class hls4ml.model.hls_layers.Layer(model, name, attributes, inputs, outputs=None)

Bases: object

add_bias(quantizer=None)
add_output_variable(shape, dim_names, out_name=None, var_name='layer{index}_out', type_name='layer{index}_t', precision=None, pragma='auto')
add_weights(quantizer=None, compression=False)
add_weights_variable(name, var_name=None, type_name=None, precision=None, data=None, quantizer=None, compression=False)
config_cpp()
function_cpp()
get_attr(key, default=None)
get_input_node(input_name=None)
get_input_variable(input_name=None)
get_layer_precision()
get_numbers_cpp()
get_output_nodes(output_name=None)
get_output_variable(output_name=None)
get_variables()
get_weights(var_name=None)
initialize()
make_array_variable(shape, dim_names, var_name='layer{index}_out', type_name='layer{index}_t', precision=None, pragma='auto')
make_stream_variable(shape, dim_names, var_name='layer{index}_out', type_name='layer{index}_t', precision=None, depth=0)
precision_cpp()
set_attr(key, value)
class hls4ml.model.hls_layers.Merge(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.PReLU(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Activation

function_cpp()
initialize()
class hls4ml.model.hls_layers.PackedType(name, precision, n_elem, n_pack, **kwargs)

Bases: hls4ml.model.hls_layers.HLSType

definition_cpp()
class hls4ml.model.hls_layers.ParametrizedActivation(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Activation

function_cpp()
class hls4ml.model.hls_layers.Pooling1D(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.Pooling2D(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.Quantizer(bits, hls_type)

Bases: object

class hls4ml.model.hls_layers.Reshape(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.Resize(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.SeparableConv1D(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.SeparableConv2D(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.Softmax(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Activation

initialize()
class hls4ml.model.hls_layers.StreamVariable(shape, dim_names, var_name='layer{index}', type_name='layer{index}_t', precision=None, n_pack=1, depth=0, **kwargs)

Bases: hls4ml.model.hls_layers.Variable

get_shape()
size()
size_cpp()
class hls4ml.model.hls_layers.TernaryTanh(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Activation

initialize()
class hls4ml.model.hls_layers.Transpose(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.Variable(var_name, atype, **kwargs)

Bases: object

class hls4ml.model.hls_layers.WeightVariable(var_name, type_name, precision, data, quantizer=None, **kwargs)

Bases: hls4ml.model.hls_layers.Variable

definition_cpp()
next()
update_precision(new_precision)
class hls4ml.model.hls_layers.XnorPrecisionType

Bases: hls4ml.model.hls_layers.IntegerPrecisionType

Convenience class to differentiate ‘regular’ integers from BNN Xnor ones

class hls4ml.model.hls_layers.ZeroPadding1D(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
class hls4ml.model.hls_layers.ZeroPadding2D(model, name, attributes, inputs, outputs=None)

Bases: hls4ml.model.hls_layers.Layer

config_cpp()
function_cpp()
initialize()
hls4ml.model.hls_layers.find_minimum_width(data, signed=True)

Helper function to find the minimum integer width to express all entries in the data array without saturation / overflow

hls4ml.model.hls_layers.register_layer(name, clazz)

hls4ml.model.hls_model module

class hls4ml.model.hls_model.HLSConfig(config)

Bases: object

get_bram_size(layer)
get_compression(layer)
get_config_value(key, default=None)
get_conv_implementation(layer)
get_layer_config(layer)
get_layer_config_value(layer, key, default=None)
get_output_dir()
get_precision(layer, var='default')
get_project_name()
get_reuse_factor(layer)
get_strategy(layer)
get_target_cycles(layer)
is_resource_strategy(layer)
class hls4ml.model.hls_model.HLSModel(config, data_reader, layer_list, inputs=None, outputs=None)

Bases: object

build(reset=False, csim=True, synth=True, cosim=False, validation=False, export=False, vsynth=False)
compile()
get_bram_variables()
get_input_variables()
get_layer_output_variable(output_name)
get_layers()
get_output_variables()
get_weights_data(layer_name, var_name)
insert_node(node, before=None)

Insert a new node into the model graph.

The node to be inserted should be created with make_node() function. The optional parameter before can be used to specify the node that follows in case of ambiguities.

Parameters
  • node (Layer) – Node to insert

  • before (Layer, optional) – The next node in sequence before which a new node should be inserted.

Raises

Exception – If an attempt to insert a node with multiple inputs is made or if before does not specify a correct node in sequence.

make_node(kind, name, attributes, inputs, outputs=None)

Make a new node not connected to the model graph.

The ‘kind’ should be a valid layer registered with register_layer. If no outputs are specified, a default output named the same as the node will be created. The returned node should be added to the graph with insert_node or replace_node functions.

Parameters
  • kind (str) – Type of node to add

  • name (str) – Name of the node

  • attributes (dict) – Initial set of attributes required to construct the node (Layer)

  • inputs (list) – List of inputs to the layer

  • outputs (list, optional) – The optional list of named outputs of the node

Raises

Exception – If an attempt to insert a node with multiple inputs is made or if before does not specify a correct node in sequence.

Returns

The node created.

Return type

Layer

next_layer()
predict(x)
register_bram_variable(out_name, variable)
register_output_variable(out_name, variable)
remove_node(node, rewire=True)

Remove a node from a graph.

By default, this function can connect the outputs of previous node to the input of next one. Note that when removing a leaf node rewire should be set to False.

Parameters
  • node (Layer) – The node to remove

  • rewire (bool, optional) – If True, connects the outputs of the previous node to the inputs of the next node

Raises

Exception – If an attempt is made to rewire a leaf node or a node with multiple inputs/outpus.

replace_node(old_node, new_node)

Replace an existing node in the graph with a new one.

Parameters
  • old_node (Layer) – The node to replace

  • new_node (Layer) – The new node

trace(x)
write()

hls4ml.model.profiling module

hls4ml.model.profiling.activation_types_hlsmodel(model)
hls4ml.model.profiling.activations_hlsmodel(model, X, fmt='summary', plot='boxplot')
hls4ml.model.profiling.activations_keras(model, X, fmt='longform', plot='boxplot')
hls4ml.model.profiling.activations_torch(model, X, fmt='longform', plot='boxplot')
hls4ml.model.profiling.ap_fixed_WIF(dtype)
hls4ml.model.profiling.array_to_summary(x, fmt='boxplot')
hls4ml.model.profiling.boxplot(data, fmt='longform')
hls4ml.model.profiling.compare(keras_model, hls_model, X, plot_type='dist_diff')

Compare each layer’s output in keras and hls model. Note that the hls_model should not be compiled before using this.

Parameters
  • keras_model – original keras model

  • hls_model – converted HLSModel, with “Trace:True” in the configuration file.

  • X (array-like) – Input for the model.

  • plot_type (string) –

    different methods to visualize the y_model and y_sim differences. Possible options include:

    • ’norm_diff’ : square root of the sum of the squares of the differences between each output vectors

    • ’dist_diff’ : The normalized distribution of the differences of the elements between two output vectors

Returns

plot object of the histogram depicting the difference in each layer’s output

Return type

matplotlib figure

hls4ml.model.profiling.get_unoptimized_hlsmodel(model)
hls4ml.model.profiling.get_ymodel_keras(keras_model, X)

Calculate each layer’s ouput and put them into a dictionary

Parameters
  • keras_model – a keras model

  • X (array-like) – Test data on which to evaluate the model to profile activations. Must be formatted suitably for the model.predict(X) method.

Returns

A dictionary in the form {“layer_name”: ouput array of layer}

Return type

dictionary

hls4ml.model.profiling.histogram(data, fmt='longform')
hls4ml.model.profiling.numerical(model=None, hls_model=None, X=None, plot='boxplot')

Perform numerical profiling of a model

Parameters
  • model (keras or pytorch model) – The model to profile

  • hls_model (HLSModel) – The HLSModel to profile

  • X (array-like, optional) – Test data on which to evaluate the model to profile activations Must be formatted suitably for the model.predict(X) method

  • plot (str, optional) – The type of plot to produce. Options are: ‘boxplot’ (default), ‘violinplot’, ‘histogram’, ‘FacetGrid’

Returns

The quadruple of produced figures. First weights and biases for the pre- and post-optimization models respectively, then activations for the pre- and post-optimization models respectively. (Optimizations are applied to an HLSModel by hls4ml, a post-optimization HLSModel is a final model)

Return type

tuple

hls4ml.model.profiling.types_boxplot(data, fmt='longform')
hls4ml.model.profiling.types_histogram(data, fmt='longform')
hls4ml.model.profiling.types_hlsmodel(model)
hls4ml.model.profiling.weights_hlsmodel(model, fmt='longform', plot='boxplot')
hls4ml.model.profiling.weights_keras(model, fmt='longform', plot='boxplot')
hls4ml.model.profiling.weights_torch(model, fmt='longform', plot='boxplot')

Module contents