ABC interface which encapsulates the logic of how to quantize tensors.

Used in the notebooks

Used in the guide

This is an experimental API not subject to backward compatibility.

A Quantizer is used by the library code to apply the mathematical transformations which actually quantize a tensor, hence allowing the user precise control over the algorithm with which tensors are quantized. When used in conjunction with QuantizeConfig it controls how a layer is quantized.

Create a custom quantizer:

class FixedRangeQuantizer(Quantizer):
  # Example quantizer which clips tensors in a fixed range.

  def build(self, tensor_shape, name, layer):
    range_var = layer.add_weight(
      name + '_range',

    return {
      'range_var': range_var,

  def __call__(self, inputs, training, weights, **kwargs):
    return tf.keras.backend.clip(
        inputs, 0.0, weights['range_var'])

  def get_config(self):
    # Not needed. No __init__ parameters to serialize.
    return {}

For a full example, see



View source

Construct the weights required by the quantizer.

A quantizer may need to construct variables to hold the state for its algorithm. This function is invoked during the build stage of the layer that the quantizer is used for. Any variables constructed are under the scope of the layer and serialized as part of the layer.

tensor_shape Shape of tensor which needs to be quantized.
name Name of tensor.
layer Keras layer which is quantizing the tensors. The layer is needed to construct the weights, and is also the owner of the weights.

Returns: Dictionary of constructed weights. This dictionary will be passed to the quantizer's call function as a weights dictionary.


View source

Instantiates a Quantizer from its config.

config Output of get_config().

A Quantizer instance.


View source

Returns the config used to serialize the Quantizer.


View source

Apply quantization to the input tensor.

This is the main function of the Quantizer which implements the core logic to quantize the tensor. It is invoked during the call stage of the layer, and allows modifying the tensors used in graph construction.

inputs Input tensor to be quantized.
training Whether the graph is currently training.
weights Dictionary of weights the quantizer can use to quantize the tensor. This contains the weights created in the build function.
**kwargs Additional variables which may be passed to the quantizer.

Returns: quantized tensor.