hgq.quantizer.internal package

Submodules

hgq.quantizer.internal.base module

class hgq.quantizer.internal.base.BitwidthMapperBase

Bases: object

Abstract base class for mapping bitwidth tensor to input tensors for HG quantizers.

bw_to_x(bw, x_shape)
classmethod from_config(config)
get_config()
inference_weight_shape(input_shape) tuple[int, ...]
x_to_bw_absmax(x)
class hgq.quantizer.internal.base.DefaultBitwidthMapper(heterogeneous_axis: Sequence[int] | None = None, homogeneous_axis: Sequence[int] | None = None, **kwargs)

Bases: BitwidthMapperBase

Default bitwidth mapper for HG quantizers.

bw_to_x(bw, x_shape)
get_config()
inference_weight_shape(input_shape)
x_to_bw_absmax(x)
x_to_bw_sign(x)
class hgq.quantizer.internal.base.DummyQuantizer(*args, **kwargs)

Bases: TrainableQuantizerBase

property bits
call(inputs, training=None)
property epsilon
property max
property min
class hgq.quantizer.internal.base.TrainableQuantizerBase(*args, **kwargs)

Bases: Layer

Abstract base class for all quantizers.

property bits
call(inputs, training=None)
compute_output_shape(input_shape)
property epsilon
property max
property min
quantize(mode)
hgq.quantizer.internal.base.check_axis(axis: Sequence[int], ndim: int)

Given a list of axis, check that they are valid for a tensor of ndim dimensions. If valid, return the axis as a list of positive integers.

Parameters:
  • axis (Sequence[int]) – List of axis to check.

  • ndim (int) – Number of dimensions of the tensor.

Returns:

axis – List of positive integers representing the axis.

Return type:

list[int]

hgq.quantizer.internal.fixed_point_quantizer module

class hgq.quantizer.internal.fixed_point_quantizer.FixedPointQuantizerBase(*args, **kwargs)

Bases: TrainableQuantizerBase

Abstract base class for all fixed-point quantizers.

property b
property bits
build(input_shape)
call(inputs, training=None)
property epsilon
property f
get_any_k(inputs)
get_minimum_i(inputs)
property i
property i_decay_speed
property k
property kif
property max
property min
property overflow_mode: str
property round_mode: str
property seed_gen
property symmetric
class hgq.quantizer.internal.fixed_point_quantizer.FixedPointQuantizerKBI(*args, **kwargs)

Bases: FixedPointQuantizerBase

Internal quantizer for fixed-point quantization parameterized by keep_negative, bits, and integer bits. Can be used as a quantizer in Keras layers, but is usually wrapped by a Quantizer class to provide a consistent interface.

Parameters:
  • k0 (numbers | bool | Initializer) – Initial value for the keep_negative parameter. Not trained, but can be manually updated.

  • b0 (numbers | Initializer) – Initial value for the number of bits. Trainable.

  • i0 (numbers | Initializer) – Initial value for the number of integer bits. Trainable.

  • round_mode (str) – Rounding mode, one of ‘RND’, ‘TRN’, ‘RND_CONV’, ‘S_RND’, ‘S_RND_CONV’.

  • overflow_mode (str) – Overflow mode, one of ‘WRAP’, ‘SAT’, ‘SYM’, ‘SAT_SYM’.

  • bc (Constraint | None) – Constraint for the number of bits.

  • ic (Constraint | None) – Constraint for the number of integer bits.

  • br (Regularizer | None) – Regularizer for the number of bits.

  • ir (Regularizer | None) – Regularizer for the number of integer bits.

  • i_decay_speed (numbers) – Speed of decay for the integer bits in WRAP mode, per step. If set to negative, enable tracing of maximum number even not in training mode.

property b
build(input_shape)
call(inputs, training=None)
property f
get_minimal_i(inputs)
property i
property k
property kif
validate_config()
class hgq.quantizer.internal.fixed_point_quantizer.FixedPointQuantizerKIF(*args, **kwargs)

Bases: FixedPointQuantizerBase

Internal quantizer for fixed-point quantization parameterized by keep_negative, integer bits, and fractional bits. Can be used as a quantizer in Keras layers, but is usually wrapped by a Quantizer class to provide a consistent interface.

Parameters:
  • k0 (numbers | bool | Initializer) – Initial value for the keep_negative parameter. Not trained, but can be manually updated.

  • i0 (numbers | Initializer) – Initial value for the number of integer bits. Trainable.

  • f0 (numbers | Initializer) – Initial value for the number of fractional bits. Trainable.

  • round_mode (str) – Rounding mode, one of ‘RND’, ‘TRN’, ‘RND_CONV’, ‘S_RND’, ‘S_RND_CONV’.

  • overflow_mode (str) – Overflow mode, one of ‘WRAP’, ‘SAT’, ‘SYM’, ‘SAT_SYM’.

  • ic (Constraint | None) – Constraint for the number of integer bits.

  • fc (Constraint | None) – Constraint for the number of fractional bits.

  • ir (Regularizer | None) – Regularizer for the number of integer bits.

  • fr (Regularizer | None) – Regularizer for the number of fractional bits.

  • i_decay_speed (numbers) – Speed of decay for the integer bits in WRAP mode, per step. If set to negative, enable tracing of maximum number even not in training mode.

property b
build(input_shape)
call(inputs, training=None)
property f
get_minimal_i(inputs)
property i
property k
property kif
validate_config()
hgq.quantizer.internal.fixed_point_quantizer.minimal_i_given_xb(x, b, symmetric=False)
hgq.quantizer.internal.fixed_point_quantizer.minimal_i_given_xf(x, f, symmetric=False)

hgq.quantizer.internal.float_point_quantizer module

class hgq.quantizer.internal.float_point_quantizer.FloatPointQuantizer(*args, **kwargs)

Bases: TrainableQuantizerBase

Internal class for float-point quantizer. Follows IEEE 754 standard with subnormal numbers, but without NaNs and infinities. The quantizer is defined by three parameters: mantissa, exponent, and exponent offset. Mantissa bits are excluding the sign bit. Exponent bits are including the sign bit. Exponent offset is added to the signed exponent.

The sign bit always presents in this quantizer. However, as the number of mantissa bits reaches -1, the quantizer will always produce zero.

Can be used as a quantizer in Keras layers, but is usually wrapped by a Quantizer class to provide a consistent interface.

Parameters:
  • m0 (numbers | Initializer) – Initial value of the number of mantissa bits. Trainable.

  • e0 (numbers | Initializer) – Initial value of the number of exponent bits. Trainable.

  • e00 (numbers | Initializer, optional) – Initial value of the exponent offset. Default is 0. Trainable.

  • mc (Constraint, optional) – Constraint for the number of mantissa bits. Default is Min(-1).

  • ec (Constraint, optional) – Constraint for the number of exponent bits. Default is keras.constraints.NonNeg().

  • e0c (Constraint, optional) – Constraint for the exponent offset. Default is None.

  • mr (Regularizer, optional) – Regularizer for the number of mantissa bits. Default is None.

  • er (Regularizer, optional) – Regularizer for the number of exponent bits. Default is None.

  • e0r (Regularizer, optional) – Regularizer for the exponent offset. Default is None.

property bits
build(input_shape)
call(inputs, training=None)
property e
property e0
property epsilon
property m
property max
property min

Module contents

class hgq.quantizer.internal.BitwidthMapperBase

Bases: object

Abstract base class for mapping bitwidth tensor to input tensors for HG quantizers.

bw_to_x(bw, x_shape)
classmethod from_config(config)
get_config()
inference_weight_shape(input_shape) tuple[int, ...]
x_to_bw_absmax(x)
class hgq.quantizer.internal.DefaultBitwidthMapper(heterogeneous_axis: Sequence[int] | None = None, homogeneous_axis: Sequence[int] | None = None, **kwargs)

Bases: BitwidthMapperBase

Default bitwidth mapper for HG quantizers.

bw_to_x(bw, x_shape)
get_config()
inference_weight_shape(input_shape)
x_to_bw_absmax(x)
x_to_bw_sign(x)
class hgq.quantizer.internal.DummyQuantizer(*args, **kwargs)

Bases: TrainableQuantizerBase

property bits
call(inputs, training=None)
property epsilon
property max
property min
class hgq.quantizer.internal.FixedPointQuantizerBase(*args, **kwargs)

Bases: TrainableQuantizerBase

Abstract base class for all fixed-point quantizers.

property b
property bits
build(input_shape)
call(inputs, training=None)
property epsilon
property f
get_any_k(inputs)
get_minimum_i(inputs)
property i
property i_decay_speed
property k
property kif
property max
property min
property overflow_mode: str
property round_mode: str
property seed_gen
property symmetric
class hgq.quantizer.internal.FixedPointQuantizerKBI(*args, **kwargs)

Bases: FixedPointQuantizerBase

Internal quantizer for fixed-point quantization parameterized by keep_negative, bits, and integer bits. Can be used as a quantizer in Keras layers, but is usually wrapped by a Quantizer class to provide a consistent interface.

Parameters:
  • k0 (numbers | bool | Initializer) – Initial value for the keep_negative parameter. Not trained, but can be manually updated.

  • b0 (numbers | Initializer) – Initial value for the number of bits. Trainable.

  • i0 (numbers | Initializer) – Initial value for the number of integer bits. Trainable.

  • round_mode (str) – Rounding mode, one of ‘RND’, ‘TRN’, ‘RND_CONV’, ‘S_RND’, ‘S_RND_CONV’.

  • overflow_mode (str) – Overflow mode, one of ‘WRAP’, ‘SAT’, ‘SYM’, ‘SAT_SYM’.

  • bc (Constraint | None) – Constraint for the number of bits.

  • ic (Constraint | None) – Constraint for the number of integer bits.

  • br (Regularizer | None) – Regularizer for the number of bits.

  • ir (Regularizer | None) – Regularizer for the number of integer bits.

  • i_decay_speed (numbers) – Speed of decay for the integer bits in WRAP mode, per step. If set to negative, enable tracing of maximum number even not in training mode.

property b
build(input_shape)
call(inputs, training=None)
property f
get_minimal_i(inputs)
property i
property k
property kif
validate_config()
class hgq.quantizer.internal.FixedPointQuantizerKIF(*args, **kwargs)

Bases: FixedPointQuantizerBase

Internal quantizer for fixed-point quantization parameterized by keep_negative, integer bits, and fractional bits. Can be used as a quantizer in Keras layers, but is usually wrapped by a Quantizer class to provide a consistent interface.

Parameters:
  • k0 (numbers | bool | Initializer) – Initial value for the keep_negative parameter. Not trained, but can be manually updated.

  • i0 (numbers | Initializer) – Initial value for the number of integer bits. Trainable.

  • f0 (numbers | Initializer) – Initial value for the number of fractional bits. Trainable.

  • round_mode (str) – Rounding mode, one of ‘RND’, ‘TRN’, ‘RND_CONV’, ‘S_RND’, ‘S_RND_CONV’.

  • overflow_mode (str) – Overflow mode, one of ‘WRAP’, ‘SAT’, ‘SYM’, ‘SAT_SYM’.

  • ic (Constraint | None) – Constraint for the number of integer bits.

  • fc (Constraint | None) – Constraint for the number of fractional bits.

  • ir (Regularizer | None) – Regularizer for the number of integer bits.

  • fr (Regularizer | None) – Regularizer for the number of fractional bits.

  • i_decay_speed (numbers) – Speed of decay for the integer bits in WRAP mode, per step. If set to negative, enable tracing of maximum number even not in training mode.

property b
build(input_shape)
call(inputs, training=None)
property f
get_minimal_i(inputs)
property i
property k
property kif
validate_config()
class hgq.quantizer.internal.FloatPointQuantizer(*args, **kwargs)

Bases: TrainableQuantizerBase

Internal class for float-point quantizer. Follows IEEE 754 standard with subnormal numbers, but without NaNs and infinities. The quantizer is defined by three parameters: mantissa, exponent, and exponent offset. Mantissa bits are excluding the sign bit. Exponent bits are including the sign bit. Exponent offset is added to the signed exponent.

The sign bit always presents in this quantizer. However, as the number of mantissa bits reaches -1, the quantizer will always produce zero.

Can be used as a quantizer in Keras layers, but is usually wrapped by a Quantizer class to provide a consistent interface.

Parameters:
  • m0 (numbers | Initializer) – Initial value of the number of mantissa bits. Trainable.

  • e0 (numbers | Initializer) – Initial value of the number of exponent bits. Trainable.

  • e00 (numbers | Initializer, optional) – Initial value of the exponent offset. Default is 0. Trainable.

  • mc (Constraint, optional) – Constraint for the number of mantissa bits. Default is Min(-1).

  • ec (Constraint, optional) – Constraint for the number of exponent bits. Default is keras.constraints.NonNeg().

  • e0c (Constraint, optional) – Constraint for the exponent offset. Default is None.

  • mr (Regularizer, optional) – Regularizer for the number of mantissa bits. Default is None.

  • er (Regularizer, optional) – Regularizer for the number of exponent bits. Default is None.

  • e0r (Regularizer, optional) – Regularizer for the exponent offset. Default is None.

property bits
build(input_shape)
call(inputs, training=None)
property e
property e0
property epsilon
property m
property max
property min
class hgq.quantizer.internal.TrainableQuantizerBase(*args, **kwargs)

Bases: Layer

Abstract base class for all quantizers.

property bits
call(inputs, training=None)
compute_output_shape(input_shape)
property epsilon
property max
property min
quantize(mode)