hls4ml.model.optimizer.passes package
Submodules
hls4ml.model.optimizer.passes.bn_fuse module
- class hls4ml.model.optimizer.passes.bn_fuse.FuseBatchNormalization
Bases:
OptimizerPass
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
hls4ml.model.optimizer.passes.convert_to_channels_last module
- class hls4ml.model.optimizer.passes.convert_to_channels_last.ChannelsLastConverter
Bases:
OptimizerPass
Converts a model from channels_first to channels_last data format by transposing the weights of relevant layers and adding a transpose layer for the inputs and outputs, if necessary
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
- class hls4ml.model.optimizer.passes.convert_to_channels_last.RemoveTransposeBeforeFlatten
Bases:
OptimizerPass
After the channels last conversion, model may have a sequence: Transpose -> Flatten -> Dense. In this case we can remove the expensive transpose and instead transpose the weights of the Dense layer.
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
hls4ml.model.optimizer.passes.expand_layer_group module
- class hls4ml.model.optimizer.passes.expand_layer_group.ExpandLayerGroup
Bases:
OptimizerPass
Expands LayerGroup (a nested model) into the parent model.
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
hls4ml.model.optimizer.passes.fuse_biasadd module
- class hls4ml.model.optimizer.passes.fuse_biasadd.FuseBiasAdd
Bases:
OptimizerPass
Fuses BiasAdd into Dense/Conv2D layer (common in TF models).
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
hls4ml.model.optimizer.passes.hgq_proxy_model module
- class hls4ml.model.optimizer.passes.hgq_proxy_model.EnforceProxyModelEmbeddedConfig
Bases:
OptimizerPass
- match(node: Layer)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node: FixedPointQuantizer)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
- class hls4ml.model.optimizer.passes.hgq_proxy_model.FixedPointQuantizer(model, name, attributes, inputs, outputs=None)
Bases:
Layer
- initialize()
- class hls4ml.model.optimizer.passes.hgq_proxy_model.UnaryLUT(model, name, attributes, inputs, outputs=None)
Bases:
Layer
- initialize()
- hls4ml.model.optimizer.passes.hgq_proxy_model.register_hgq_proxy_model()
- hls4ml.model.optimizer.passes.hgq_proxy_model.to_hls4ml_fixed(fixed: str)
- hls4ml.model.optimizer.passes.hgq_proxy_model.userconf_ifdef(key: str, layer_name: str, model)
hls4ml.model.optimizer.passes.infer_precision module
- class hls4ml.model.optimizer.passes.infer_precision.InferPrecisionTypes
Bases:
ConfigurableOptimizerPass
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
hls4ml.model.optimizer.passes.multi_dense module
- class hls4ml.model.optimizer.passes.multi_dense.ReplaceMultidimensionalDenseWithConv
Bases:
OptimizerPass
This matches all multidimensional Dense layers and changes them to a convolution. Note: the convolution may subsequently be changed to a pointwise convolution for bakends that implement special pointwise convolutions.
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
hls4ml.model.optimizer.passes.nop module
- class hls4ml.model.optimizer.passes.nop.EliminateLinearActivation
Bases:
OptimizerPass
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
hls4ml.model.optimizer.passes.qkeras module
- class hls4ml.model.optimizer.passes.qkeras.ApplyAlpha(model, name, attributes, inputs, outputs=None)
Bases:
BatchNormalization
A custom layer to scale the output of a QDense layer which used ‘alpha != 1’ Inference computation uses BatchNormalization methods
- add_bias(bias, quantizer=None)
- add_weights(scale, quantizer=None)
- initialize()
- class hls4ml.model.optimizer.passes.qkeras.ExtractTernaryThreshold
Bases:
OptimizerPass
The input value (threshold) at which the output of a a ternary activation changes is configurable. This pass extracts that threshold point, inserting a BatchNormalization layer to execute the scaling. That BatchNormalization layer is then expected to be fused into a BatchNormalizationQuantizedTanh layer configured with the correct threshold.
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
- class hls4ml.model.optimizer.passes.qkeras.FuseConsecutiveBatchNormalization
Bases:
OptimizerPass
OptimizerPass to merge consecutive BatchNormalization layers. These may exist in a model after QKerasFactorizeAlpha layer. Scale and Bias of each layer are combined into scale and bias of a single layer.
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
- class hls4ml.model.optimizer.passes.qkeras.OutputRoundingSaturationMode
Bases:
ConfigurableOptimizerPass
Set the Rounding and Saturation mode of the output (and accumulator, if applicable) of the layers specific in layer list. The layer list is empty by default. To specify which layer to apply this pass to, perform e.g.: hls4ml.model.optimizer.get_optimizer(‘output_rounding_saturation_mode’).configure(layers=[‘Dense’, ‘Activation’]) The Rounding and Saturation modes are ‘None’ by default (so use the compiler defaults) To set which mode to use: hls4ml.model.optimizer.get_optimizer(‘output_rounding_saturation_mode’).configure(rounding_mode=’AP_RND_CONV’) hls4ml.model.optimizer.get_optimizer(‘output_rounding_saturation_mode’).configure(saturation_mode=’AP_SAT’)
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- precision_string_modify(pstr)
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
- class hls4ml.model.optimizer.passes.qkeras.QKerasFactorizeAlpha
Bases:
OptimizerPass
OptimizerPass for extracting alpha “scale” from QKeras quantized layer. The weights of the Q{Dense, Conv} layer are scaled to the common data type, and an ‘ApplyAlpha’ layer is inserted to reapply the scale.
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
- hls4ml.model.optimizer.passes.qkeras.register_qkeras()
hls4ml.model.optimizer.passes.seperable_to_dw_conv module
This optimizer converts a seperable convolution to a depthwise followed by a regular convolution. For backends with a custom pointwise implementations the regular convolution will subsequently be converted to a pointwise convolution by a different optimizer.
- class hls4ml.model.optimizer.passes.seperable_to_dw_conv.SeperableToDepthwiseAndConv
Bases:
OptimizerPass
Convert Seperable to DepthwiseConv + Conv (potentially later Pointwise)
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
hls4ml.model.optimizer.passes.stamp module
- class hls4ml.model.optimizer.passes.stamp.MakeStamp
Bases:
ModelOptimizerPass
- transform(model)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
hls4ml.model.optimizer.passes.transpose_opt module
- class hls4ml.model.optimizer.passes.transpose_opt.RemoveNopTranspose
Bases:
OptimizerPass
Remove a transpose layer if it doesn’t do anything to a 1D array. i.e, 1D input and perm = [0]
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.
- class hls4ml.model.optimizer.passes.transpose_opt.RemoveSingleChannelTranspose
Bases:
OptimizerPass
Remove transpose of inputs if the number of channels is 1 as for io_parallel this doesn’t affect the array representation used
- match(node)
Predicate to match on a given node.
- Parameters:
node (Layer) – Node in the model graph to try matching the optimizer on.
- transform(model, node)
Transformation to apply if matching was successful.
Transform should return a boolean value indicating if the model graph was altered (by adding/removing nodes).
- Parameters:
model (ModelGraph) – Model to optimize
node (Layer) – The matched node in the model graph.