qkeras.qtools.qtools_util

utility functions.

Functions

adjust_accumulator_for_auto_po2(layer, ...)

Adjust accumulator when weight quantizer is auto_po2 type.

adjust_multiplier_for_auto_po2(multiplier, ...)

Adjust multiplier when weight quantizer is auto_po2 type.

find_divisors(num)

get_input_quantizers(graph, node_id, ...[, ...])

get the current layer's input quantizer.

get_input_quantizers_advanced(graph, ...[, ...])

get input quantizer, deal with keras layer or lack of input quantizer in qkeras layer.

get_layer_info(layer, attr_name)

get_operation_count(layer, input_shape)

Determines number of multiplier operations in a qkeras layer.

get_scale_from_quantized_bits_with_auto_po2(...)

Get scale from quantized_bits with alpha=auto_po2.

get_val(feature, key[, default_val])

get_weights(layer[, ...])

Get layer weights.

is_merge_layers(layer)

is_shape_alternation_layers(layer)

is_upsampled(layer)

qkeras.qtools.qtools_util.adjust_accumulator_for_auto_po2(layer, multiplier, qkeras_weight_quantizer, bias_quantizer)[source]

Adjust accumulator when weight quantizer is auto_po2 type.

qkeras.qtools.qtools_util.adjust_multiplier_for_auto_po2(multiplier, qkeras_weight_quantizer)[source]

Adjust multiplier when weight quantizer is auto_po2 type.

Multiplier_bits = bits_x + bits_w Multiplier_intbits = log2(scale) + intbits_x + intbits_w

Because we might have different scale for auto_po2 quantizer at different output channels, multiplier will have different integer bits at different output channel accordingly, which is not desirable in hardware implementation. Therefore we set a general multiplier quantizers so that it provides enough fractional bits and integer bits for all output channels.

qkeras.qtools.qtools_util.find_divisors(num)[source]
qkeras.qtools.qtools_util.get_input_quantizers(graph, node_id, quantizer_factory, debug=False)[source]

get the current layer’s input quantizer.

qkeras.qtools.qtools_util.get_input_quantizers_advanced(graph, node_id, is_input_layer, quantizer_factory, cfg, debug=False)[source]

get input quantizer, deal with keras layer or lack of input quantizer in qkeras layer.

qkeras.qtools.qtools_util.get_layer_info(layer, attr_name)[source]
Parameters:
  • layer (Layer)

  • attr_name (str)

qkeras.qtools.qtools_util.get_operation_count(layer, input_shape)[source]

Determines number of multiplier operations in a qkeras layer.

qkeras.qtools.qtools_util.get_scale_from_quantized_bits_with_auto_po2(quantizer)[source]

Get scale from quantized_bits with alpha=auto_po2.

qkeras.qtools.qtools_util.get_val(feature, key, default_val=None)[source]
qkeras.qtools.qtools_util.get_weights(layer, model_weights_already_quantized=True)[source]

Get layer weights.

Parameters:
  • layer – given qkeras/keras layer

  • model_weights_already_quantized – bool. whether the given layer’s weights are already quantized. This is necessary because with certain quantizers, eg., quantized_bits(alpha=”auto_po2”), we cannot quantize the same weights more than once, as it will lead to different results.

Returns:

Quantized layer weights.

qkeras.qtools.qtools_util.is_merge_layers(layer)[source]
qkeras.qtools.qtools_util.is_shape_alternation_layers(layer)[source]
qkeras.qtools.qtools_util.is_upsampled(layer)[source]
Parameters:

layer (Layer)