View source on GitHub
|
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',
initializer=keras.initializers.Constant(6.0),
trainable=False)
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 https://www.tensorflow.org/model_optimization/guide/quantization/training_comprehensive_guide.md
Methods
build
@abc.abstractmethodbuild( tensor_shape, name, layer )
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.
| Args | |
|---|---|
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.
from_config
@classmethodfrom_config( config )
Instantiates a Quantizer from its config.
| Args | |
|---|---|
config
|
Output of get_config().
|
| Returns | |
|---|---|
A Quantizer instance.
|
get_config
@abc.abstractmethodget_config()
Returns the config used to serialize the Quantizer.
__call__
@abc.abstractmethod__call__( inputs, training, weights, **kwargs )
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.
| Args | |
|---|---|
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.
View source on GitHub