qkeras.bn_folding_utils

Utility functions for folding batchnorm with qconv/qdense layers.

Functions

convert_folded_layer_to_unfolded(layer)

Replace a source batchnorm folded layer with a non-folded layer.

populate_bias_quantizer_from_accumulator(...)

Populate the bias quantizer from accumulator type.

unfold_model(model)

Convert a model with batchnorm folded layer to a normal model.

qkeras.bn_folding_utils.convert_folded_layer_to_unfolded(layer)[source]

Replace a source batchnorm folded layer with a non-folded layer.

Parameters:

layer – keras/qkeras layer type. Source layer to be replaced with

Returns:

new layer instance

qkeras.bn_folding_utils.populate_bias_quantizer_from_accumulator(model, source_quantizers)[source]

Populate the bias quantizer from accumulator type.

When user set bias_quantizer=None for layers(e.g., QConv2DBatchnorm), this function generates the accumulator type of the layer MAC op and set it as the bias quantizer. Such step is skipped if user provided a specific bias quantizer type.

Parameters:
  • model – keras/qkeras model object. If the model doesn’t contain any batchnorm folded layer or if the bias quanizer type in the folded layer is already given, no operation needed. Else we generate the bias quantizer type and set it in model.

  • source_quantizers – list of qkeras quantizers. A list of quantizer types for model inputs.

Returns:

keras model object

qkeras.bn_folding_utils.unfold_model(model)[source]

Convert a model with batchnorm folded layer to a normal model.

“Normal” here refers to a model without composite folded layer such as QConv2DBatchnorm layer. This function replace the folded layers with a normal QConv/QDense layer. It aslo sets the weights in the normal layer with the folded weights in the folded layer. Model architecture could be either sequential or non-sequential.

Parameters:

model – keras object, model with folded layers.

Returns:

A model that replaces folded layers (e.g., QConv2DBatchnorm) with normal

qkeras layers (e.g., QConv2D). This model can be passed on to hardware generator so that hardware doesn’t see batch normalization parameters.