concrete.ml.quantization.quantizers.md
module concrete.ml.quantization.quantizers
concrete.ml.quantization.quantizers
Quantization utilities for a numpy array/tensor.
Global Variables
STABILITY_CONST
function fill_from_kwargs
fill_from_kwargs
Fill a parameter set structure from kwargs parameters.
Args:
obj
: an object of type klass, if None the object is created if any of the type's members appear in the kwargsklass
: the type of object to fillkwargs
: parameter names and values to fill into an instance of the klass type
Returns:
obj
: an object of type klasskwargs
: remaining parameter names and values that were not filled into obj
Raises:
TypeError
: if the types of the parameters in kwargs could not be converted to the corresponding types of members of klass
class QuantizationOptions
QuantizationOptions
Options for quantization.
Determines the number of bits for quantization and the method of quantization of the values. Signed quantization allows negative quantized values. Symmetric quantization assumes the float values are distributed symmetrically around x=0 and assigns signed values around 0 to the float values. QAT (quantization aware training) quantization assumes the values are already quantized, taking a discrete set of values, and assigns these values to integers, computing only the scale.
method __init__
__init__
property quant_options
Get a copy of the quantization parameters.
Returns:
UniformQuantizationParameters
: a copy of the current quantization parameters
method copy_opts
copy_opts
Copy the options from a different structure.
Args:
opts
(QuantizationOptions): structure to copy parameters from.
method dump
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
method dump_dict
dump_dict
Dump itself to a dict.
Returns:
metadata
(Dict): Dict of serialized objects.
method dumps
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
method is_equal
is_equal
Compare two quantization options sets.
Args:
opts
(QuantizationOptions): options to compare this instance toignore_sign_qat
(bool): ignore sign comparison for QAT options
Returns:
bool
: whether the two quantization options compared are equivalent
method load_dict
load_dict
Load itself from a string.
Args:
metadata
(Dict): Dict of serialized objects.
Returns:
QuantizationOptions
: The loaded object.
class MinMaxQuantizationStats
MinMaxQuantizationStats
Calibration set statistics.
This class stores the statistics for the calibration set or for a calibration data batch. Currently we only store min/max to determine the quantization range. The min/max are computed from the calibration set.
method __init__
__init__
property quant_stats
Get a copy of the calibration set statistics.
Returns:
MinMaxQuantizationStats
: a copy of the current quantization stats
method check_is_uniform_quantized
check_is_uniform_quantized
Check if these statistics correspond to uniformly quantized values.
Determines whether the values represented by this QuantizedArray show a quantized structure that allows to infer the scale of quantization.
Args:
options
(QuantizationOptions): used to quantize the values in the QuantizedArray
Returns:
bool
: check result.
method compute_quantization_stats
compute_quantization_stats
Compute the calibration set quantization statistics.
Args:
values
(numpy.ndarray): Calibration set on which to compute statistics.
method copy_stats
copy_stats
Copy the statistics from a different structure.
Args:
stats
(MinMaxQuantizationStats): structure to copy statistics from.
method dump
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
method dump_dict
dump_dict
Dump itself to a dict.
Returns:
metadata
(Dict): Dict of serialized objects.
method dumps
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
method load_dict
load_dict
Load itself from a string.
Args:
metadata
(Dict): Dict of serialized objects.
Returns:
QuantizationOptions
: The loaded object.
class UniformQuantizationParameters
UniformQuantizationParameters
Quantization parameters for uniform quantization.
This class stores the parameters used for quantizing real values to discrete integer values. The parameters are computed from quantization options and quantization statistics.
method __init__
__init__
property quant_params
Get a copy of the quantization parameters.
Returns:
UniformQuantizationParameters
: a copy of the current quantization parameters
method compute_quantization_parameters
compute_quantization_parameters
Compute the quantization parameters.
Args:
options
(QuantizationOptions): quantization options setstats
(MinMaxQuantizationStats): calibrated statistics for quantization
method copy_params
copy_params
Copy the parameters from a different structure.
Args:
params
(UniformQuantizationParameters): parameter structure to copy
method dump
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
method dump_dict
dump_dict
Dump itself to a dict.
Returns:
metadata
(Dict): Dict of serialized objects.
method dumps
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
method load_dict
load_dict
Load itself from a string.
Args:
metadata
(Dict): Dict of serialized objects.
Returns:
UniformQuantizationParameters
: The loaded object.
class UniformQuantizer
UniformQuantizer
Uniform quantizer.
Contains all information necessary for uniform quantization and provides quantization/de-quantization functionality on numpy arrays.
Args:
options
(QuantizationOptions): Quantization options setstats
(Optional[MinMaxQuantizationStats]): Quantization batch statistics setparams
(Optional[UniformQuantizationParameters]): Quantization parameters set (scale, zero-point)
method __init__
__init__
property quant_options
Get a copy of the quantization parameters.
Returns:
UniformQuantizationParameters
: a copy of the current quantization parameters
property quant_params
Get a copy of the quantization parameters.
Returns:
UniformQuantizationParameters
: a copy of the current quantization parameters
property quant_stats
Get a copy of the calibration set statistics.
Returns:
MinMaxQuantizationStats
: a copy of the current quantization stats
method check_is_uniform_quantized
check_is_uniform_quantized
Check if these statistics correspond to uniformly quantized values.
Determines whether the values represented by this QuantizedArray show a quantized structure that allows to infer the scale of quantization.
Args:
options
(QuantizationOptions): used to quantize the values in the QuantizedArray
Returns:
bool
: check result.
method compute_quantization_parameters
compute_quantization_parameters
Compute the quantization parameters.
Args:
options
(QuantizationOptions): quantization options setstats
(MinMaxQuantizationStats): calibrated statistics for quantization
method compute_quantization_stats
compute_quantization_stats
Compute the calibration set quantization statistics.
Args:
values
(numpy.ndarray): Calibration set on which to compute statistics.
method copy_opts
copy_opts
Copy the options from a different structure.
Args:
opts
(QuantizationOptions): structure to copy parameters from.
method copy_params
copy_params
Copy the parameters from a different structure.
Args:
params
(UniformQuantizationParameters): parameter structure to copy
method copy_stats
copy_stats
Copy the statistics from a different structure.
Args:
stats
(MinMaxQuantizationStats): structure to copy statistics from.
method dequant
dequant
De-quantize values.
Args:
qvalues
(numpy.ndarray): integer values to de-quantize
Returns:
Union[Any, numpy.ndarray]
: De-quantized float values.
method dump
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
method dump_dict
dump_dict
Dump itself to a dict.
Returns:
metadata
(Dict): Dict of serialized objects.
method dumps
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
method is_equal
is_equal
Compare two quantization options sets.
Args:
opts
(QuantizationOptions): options to compare this instance toignore_sign_qat
(bool): ignore sign comparison for QAT options
Returns:
bool
: whether the two quantization options compared are equivalent
method load_dict
load_dict
Load itself from a string.
Args:
metadata
(Dict): Dict of serialized objects.
Returns:
UniformQuantizer
: The loaded object.
method quant
quant
Quantize values.
Args:
values
(numpy.ndarray): float values to quantize
Returns:
numpy.ndarray
: Integer quantized values.
class QuantizedArray
QuantizedArray
Abstraction of quantized array.
Contains float values and their quantized integer counter-parts. Quantization is performed by the quantizer member object. Float and int values are kept in sync. Having both types of values is useful since quantized operators in Concrete ML graphs might need one or the other depending on how the operator works (in float or in int). Moreover, when the encrypted function needs to return a value, it must return integer values.
See https://arxiv.org/abs/1712.05877.
Args:
values
(numpy.ndarray): Values to be quantized.n_bits
(int): The number of bits to use for quantization.value_is_float
(bool, optional): Whether the passed values are real (float) values or not. If False, the values will be quantized according to the passed scale and zero_point. Defaults to True.options
(QuantizationOptions): Quantization options setstats
(Optional[MinMaxQuantizationStats]): Quantization batch statistics setparams
(Optional[UniformQuantizationParameters]): Quantization parameters set (scale, zero-point)kwargs
: Any member of the options, stats, params sets as a key-value pair. The parameter sets need to be completely parametrized if their members appear in kwargs.
method __init__
__init__
method dequant
dequant
De-quantize self.qvalues.
Returns:
numpy.ndarray
: De-quantized values.
method dump
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
method dump_dict
dump_dict
Dump itself to a dict.
Returns:
metadata
(Dict): Dict of serialized objects.
method dumps
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
method load_dict
load_dict
Load itself from a string.
Args:
metadata
(Dict): Dict of serialized objects.
Returns:
QuantizedArray
: The loaded object.
method quant
quant
Quantize self.values.
Returns:
numpy.ndarray
: Quantized values.
method update_quantized_values
update_quantized_values
Update qvalues to get their corresponding values using the related quantized parameters.
Args:
qvalues
(numpy.ndarray): Values to replace self.qvalues
Returns:
values
(numpy.ndarray): Corresponding values
method update_values
update_values
Update values to get their corresponding qvalues using the related quantized parameters.
Args:
values
(numpy.ndarray): Values to replace self.values
Returns:
qvalues
(numpy.ndarray): Corresponding qvalues
Last updated