network_editor Module

The model can be edited by a list of EditRules to apply on nodes in a graph that represents the model during the model quantization. Each EditRule is a tuple of a filter and an action, where we apply the action on each node the filter matches

EditRule

class model_compression_toolkit.core.network_editor.EditRule(filter, action)

A tuple of a node filter and an action. The filter matches nodes in the graph which represents the model, and the action is applied on these nodes during the quantization process.

Examples

Create an EditRule to quantize all Conv2D kernel attribute weights using 9 bits:

>>> import model_compression_toolkit as mct
>>> from model_compression_toolkit.core.keras.constants import KERNEL
>>> from tensorflow.keras.layers import Conv2D
>>> er_list = [mct.core.network_editor.EditRule(filter=mct.core.network_editor.NodeTypeFilter(Conv2D), action=mct.core.network_editor.ChangeCandidatesWeightsQuantConfigAttr(attr_name=KERNEL, weights_n_bits=9))]

Then the rules list can be passed to keras_post_training_quantization() to modify the network during the quantization process.

Create new instance of EditRule(filter, action)

Filters

class model_compression_toolkit.core.network_editor.NodeTypeFilter(node_type)

Class NodeNameFilter to check if a node is of a specific type.

Init a NodeTypeFilter object.

Parameters:

node_type – Node type to check.


class model_compression_toolkit.core.network_editor.NodeNameFilter(node_name)

Class NodeNameFilter to check if a node’s name has a specific value.

Init a NodeNameFilter object.

Parameters:

node_name – Node name to check.


class model_compression_toolkit.core.network_editor.NodeNameScopeFilter(node_name_scope)

Class NodeNameFilter to check if a string is in a node’s name.

Init a NodeNameScopeFilter object.

Parameters:

node_name_scope – String to check if exists in node name.

Actions

class model_compression_toolkit.core.network_editor.ChangeFinalWeightsQuantConfigAttr(attr_name=None, **kwargs)

Change attributes in a layer’s final weights quantization config.

Parameters:
  • attr_name – The weights attribute’s name to set the weights quantization params function for.

  • kwargs – Dictionary of attr_name and attr_value to change layer’s final weights quantization config.


class model_compression_toolkit.core.network_editor.ChangeCandidatesWeightsQuantConfigAttr(attr_name=None, **kwargs)

Change attributes in a layer’s weights quantization configuration candidates.

Parameters:
  • attr_name – The weights attribute’s name to set the weights quantization params function for.

  • kwargs – Dictionary of attr_name and attr_value to change layer’s weights quantization configuration candidates.


class model_compression_toolkit.core.network_editor.ChangeFinalActivationQuantConfigAttr(**kwargs)

Change attributes in a layer’s final activation quantization config.

Parameters:

kwargs – Dictionary of attr_name and attr_value to change layer’s final activation quantization config.


class model_compression_toolkit.core.network_editor.ChangeCandidatesActivationQuantConfigAttr(**kwargs)

Change attributes in a layer’s activation quantization configuration candidates.

Parameters:

kwargs – Dictionary of attr_name and attr_value to change in the layer’s activation quantization configuration candidates.


class model_compression_toolkit.core.network_editor.ChangeQuantizationParamFunction(attr_name=None, activation_quantization_params_fn=None, weights_quantization_params_fn=None)

Class ChangeQuantizationParamFunction to change a node’s weights/activations quantization params function.

Init a ChangeQuantizationParamFunction object.

Parameters:
  • attr_name – The weights attribute’s name to set the weights quantization params function for (if setting weights params).

  • activation_quantization_params_fn – a params function for a node’s activations.

  • weights_quantization_params_fn – a params function for a node’s weights.


class model_compression_toolkit.core.network_editor.ChangeFinalWeightsQuantizationMethod(attr_name, weights_quantization_method=None)

Class ChangeFinalWeightsQuantizationMethod to change a node’s weights/activations quantizer function.

Init a ChangeFinalWeightsQuantizationMethod object.

Parameters:
  • attr_name – The weights attribute’s name to set the weights quantization method for.

  • weights_quantization_method – a quantization method for a node’s weights.


class model_compression_toolkit.core.network_editor.ChangeCandidatesWeightsQuantizationMethod(attr_name, weights_quantization_method=None)

Class ChangeCandidatesWeightsQuantizationMethod to change a node’s weights quantizer function.

Init a ChangeCandidatesWeightsQuantizationMethod object.

Parameters:
  • weights_quantization_method – a quantization method for a node’s weights.

  • attr_name – The weights attribute’s name to set the weights quantization params function for.


class model_compression_toolkit.core.network_editor.ChangeCandidatesActivationQuantizationMethod(activation_quantization_method=None)

Class ChangeQuantizationMethod to change a node’s activations quantizer function.

Init a ChangeCandidatesActivationQuantizationMethod object.

Parameters:

activation_quantization_method – a quantization method for a node’s activations.