HGQ.layers package

Subpackages

Submodules

HGQ.layers.base module

class HGQ.layers.base.ABSBaseLayer(*args, **kwargs)

Bases: Layer

property input_bw
property input_bw_exact
property last_layer
class HGQ.layers.base.HLayerBase(*args, **kwargs)

Bases: ABSBaseLayer

Abstract base class for all layers in the library. Child classes should call post_build() after calling their build() method.

property act_bw

Returns the bitwidth of the pre-activation values. Differentiable.

property act_bw_exact: ndarray

Returns the exact bitwidth of the pre-activation values. Non-differentiable. For post-training use.

beta

BOPs-regularization strength

build(input_shape)

Creates the variables of the layer (for subclass implementers).

This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().

This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).

Parameters:

input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).

call(x, training=None, record_minmax=None)

This is where the layer’s logic lives.

The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

in __init__(), or in the build() method that is

called automatically before call() executes for the first time.

Parameters:
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.

    • NumPy array or Python scalar values in inputs get cast as tensors.

    • Keras mask metadata is only collected from inputs.

    • Layers are built (build(input_shape) method) using shape info from inputs only.

    • input_spec compatibility is only checked against inputs.

    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.

    • The SavedModel input specification is generated using inputs only.

    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.

  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.

  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.

    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).

Returns:

A tensor or list/tuple of tensors.

property can_bias_cover_rnd
property compute_exact_bops

Computes the exact bops for the layer. Non-differentiable. For post-training use.

forward(x, training=None, record_minmax=None)
property fused_bias
property fused_kernel
property fused_qbias

Returns the final, quantized bias for deployment. non-differentiable, should not be used for training. When using rounding to nearest and the bias can cover the rounding error, bias is pre-biased to cover the rounding shift 2^-fbw, and then TRN can be used instead RND without any loss of accuracy.

property fused_qkernel

Returns the final, quantized kernel for deployment. non-differentiable, should not be used for training.

get_config()

Returns the config of the layer.

A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.

The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).

Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.

Returns:

Python dictionary.

get_keras_config()
init_quantizers(input_shape)

Initializes the High Granularity Quantizers for the kernel and the pre-activation values. This method is called by post_build() method.

property kernel_bw

Returns (over) approximated bitwidth of the kernel. Differentiable.

property kernel_bw_exact

Returns exact bitwidth of the kernel. Non-differentiable. For post-training use.

kq_config

kernel quantizer config

paq_config

pre-activation quantizer config

post_build(input_shape)

This method should be called after calling build() method of the child class. It initializes the quantizers and sets the bops variable, and set a few flags (_has_kernel, _has_bias, _relu_act) for convenience.)

reset_minmax()

Resets the recorded minmax values for the pre-activation quantizer.

HGQ.layers.base.scale_grad(x, scale)

HGQ.layers.batchnorm_base module

class HGQ.layers.batchnorm_base.FakeObj(**kwargs)

Bases: object

class HGQ.layers.batchnorm_base.HBatchNormBase(*args, **kwargs)

Bases: HLayerBase

adapt_fused_bn_kernel_bw_bits(x: Tensor)

Adapt the bitwidth of the kernel quantizer to the input tensor, such that each input is represented with approximately the same number of bits after fused batchnormalization.

property fused_bias
property fused_kernel
post_build(input_shape)

This method should be called after calling build() method of the child class. It initializes the quantizers and sets the bops variable, and set a few flags (_has_kernel, _has_bias, _relu_act) for convenience.)

class HGQ.layers.batchnorm_base.HBatchNormalization(*args, **kwargs)

Bases: HBatchNormBase

property compute_exact_bops

Computes the exact bops for the layer. Non-differentiable. For post-training use.

compute_output_shape(input_shape)

Computes the output shape of the layer.

This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.

Parameters:

input_shape – Shape tuple (tuple of integers) or tf.TensorShape, or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.

Returns:

A tf.TensorShape instance or structure of tf.TensorShape instances.

forward(x, training=None, record_minmax=False)
post_build(input_shape)

This method should be called after calling build() method of the child class. It initializes the quantizers and sets the bops variable, and set a few flags (_has_kernel, _has_bias, _relu_act) for convenience.)

HGQ.layers.conv module

class HGQ.layers.conv.HConv(*args, **kwargs)

Bases: HLayerBase, Conv

property compute_exact_bops

Computes the exact bops for the layer. Non-differentiable. For post-training use.

convolution_op(inputs, kernel)
forward(x, training=None, record_minmax=None)
jit_forward(x, training=None, record_minmax=None)
post_build(input_shape)

This method should be called after calling build() method of the child class. It initializes the quantizers and sets the bops variable, and set a few flags (_has_kernel, _has_bias, _relu_act) for convenience.)

class HGQ.layers.conv.HConv1D(*args, **kwargs)

Bases: HConv

class HGQ.layers.conv.HConv1DBatchNorm(*args, **kwargs)

Bases: HConvBatchNorm

class HGQ.layers.conv.HConv2D(*args, **kwargs)

Bases: HConv

class HGQ.layers.conv.HConv2DBatchNorm(*args, **kwargs)

Bases: HConvBatchNorm

class HGQ.layers.conv.HConvBatchNorm(*args, **kwargs)

Bases: HConv, HBatchNormBase

bn_train_jit_forward(x, training, record_minmax=None)
forward(x, training=None, record_minmax=None)

HGQ.layers.dense module

class HGQ.layers.dense.HDense(*args, **kwargs)

Bases: HLayerBase, Dense

property compute_exact_bops

Computes the exact bops for the layer. Non-differentiable. For post-training use.

forward(x, training=None, record_minmax=None)
jit_forward(x, training=None, record_minmax=None)
parallel_factor

Acting like parallelization_factor in conv, but for multi-dimension dense layer (matmul operation).

post_build(input_shape)

This method should be called after calling build() method of the child class. It initializes the quantizers and sets the bops variable, and set a few flags (_has_kernel, _has_bias, _relu_act) for convenience.)

class HGQ.layers.dense.HDenseBatchNorm(*args, **kwargs)

Bases: HDense, HBatchNormBase

bn_train_jit_forward(x, training, record_minmax=None)
forward(x, training=None, record_minmax=None)

HGQ.layers.misc module

class HGQ.layers.misc.HActivation(*args, **kwargs)

Bases: HLayerBase, Activation

compute_output_shape(input_shape)

Computes the output shape of the layer.

This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.

Parameters:

input_shape – Shape tuple (tuple of integers) or tf.TensorShape, or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.

Returns:

A tf.TensorShape instance or structure of tf.TensorShape instances.

post_build(input_shape)

This method should be called after calling build() method of the child class. It initializes the quantizers and sets the bops variable, and set a few flags (_has_kernel, _has_bias, _relu_act) for convenience.)

class HGQ.layers.misc.HAdd(*args, **kwargs)

Bases: HLayerBase, _Merge

compute_output_shape(input_shape)

Computes the output shape of the layer.

This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.

Parameters:

input_shape – Shape tuple (tuple of integers) or tf.TensorShape, or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.

Returns:

A tf.TensorShape instance or structure of tf.TensorShape instances.

forward(inputs, training=None, record_minmax=None)
class HGQ.layers.misc.HQuantize(*args, **kwargs)

Bases: HLayerBase

compute_output_shape(input_shape)

Computes the output shape of the layer.

This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.

Parameters:

input_shape – Shape tuple (tuple of integers) or tf.TensorShape, or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.

Returns:

A tf.TensorShape instance or structure of tf.TensorShape instances.

forward(x, training=None, record_minmax=None)

HGQ.layers.passive_layers module

class HGQ.layers.passive_layers.PAveragePooling1D(*args, **kwargs)

Bases: PPool1D, AveragePooling1D

class HGQ.layers.passive_layers.PAveragePooling2D(*args, **kwargs)

Bases: PPool2D, AveragePooling2D

HGQ.layers.passive_layers.PAvgPool1D

alias of PAveragePooling1D

HGQ.layers.passive_layers.PAvgPool2D

alias of PAveragePooling2D

class HGQ.layers.passive_layers.PConcatenate(*args, **kwargs)

Bases: Concatenate, PLayerBase

property act_bw

Returns the bitwidth of the pre-activation values. Differentiable.

property act_bw_exact: ndarray

Returns the bitwidth of the pre-activation values. Differentiable.

property input_bw
class HGQ.layers.passive_layers.PDropout(*args, **kwargs)

Bases: PLayerBase, Dropout

class HGQ.layers.passive_layers.PFlatten(*args, **kwargs)

Bases: Flatten, PLayerBase

class HGQ.layers.passive_layers.PLayerBase(*args, **kwargs)

Bases: ABSBaseLayer

property act_bw

Returns the bitwidth of the pre-activation values. Differentiable.

property act_bw_exact: ndarray

Returns the bitwidth of the pre-activation values. Differentiable.

HGQ.layers.passive_layers.PMaxPool1D

alias of PMaxPooling1D

HGQ.layers.passive_layers.PMaxPool2D

alias of PMaxPooling2D

class HGQ.layers.passive_layers.PMaxPooling1D(*args, **kwargs)

Bases: PPool1D, MaxPooling1D

class HGQ.layers.passive_layers.PMaxPooling2D(*args, **kwargs)

Bases: PPool2D, MaxPooling2D

class HGQ.layers.passive_layers.PPermute(*args, **kwargs)

Bases: PLayerBase, Permute

class HGQ.layers.passive_layers.PPool1D(*args, **kwargs)

Bases: PLayerBase, Pooling1D

property act_bw

Returns the bitwidth of the pre-activation values. Differentiable.

property act_bw_exact: ndarray

Returns the bitwidth of the pre-activation values. Differentiable.

build(input_shape)

Creates the variables of the layer (for subclass implementers).

This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().

This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).

Parameters:

input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).

class HGQ.layers.passive_layers.PPool2D(*args, **kwargs)

Bases: PLayerBase, Pooling2D

property act_bw

Returns the bitwidth of the pre-activation values. Differentiable.

property act_bw_exact: ndarray

Returns the bitwidth of the pre-activation values. Differentiable.

build(input_shape)

Creates the variables of the layer (for subclass implementers).

This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().

This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).

Parameters:

input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).

class HGQ.layers.passive_layers.PReshape(*args, **kwargs)

Bases: Reshape, PLayerBase

class HGQ.layers.passive_layers.Signature(*args, **kwargs)

Bases: PLayerBase

property act_bw
property act_bw_exact: ndarray

Returns the bitwidth of the pre-activation values. Differentiable.

build(input_shape)

Creates the variables of the layer (for subclass implementers).

This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().

This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).

Parameters:

input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).

call(x, training=None, record_minmax=None)
get_config()

Returns the config of the layer.

A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.

The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).

Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.

Returns:

Python dictionary.

property input_bw

Module contents