afe.ir.transform.quantization_transforms

Classes

UpdateQuantizationConfigs

Update quantization configs to each node in AwesomeNet

QuantizeState

Data used by the quantization algorithm during traversal of a network's nodes.

Quantize

Quantizes an AwesomeNet

Functions

add_external_nodes_to_float_node_list(→ None)

Adding names of external nodes to the net.float_node_list attributes.

traverse_network(→ _A)

Traverse a network in topological order and process each node using visit_function.

quantized_operation_execution(→ List[numpy.ndarray])

Module Contents

class afe.ir.transform.quantization_transforms.UpdateQuantizationConfigs(quant_configs: afe.core.configs.QuantizationConfigs, custom_quantization_configs: Dict[afe.ir.defines.NodeName, Dict[str, Any]] | None = None)[source]

Update quantization configs to each node in AwesomeNet

Attribute asymmetry:

bool. Set to True if users want to do asymmetry quantization for all layers or False for symmetric quantization unless some layers are specific configured in the custom_quantization_configs.

Attribute per_channel:

bool. Set to True if users want to do per-channel quantization for all layers or False for per-tensor quantization unless some layers are specific configured in the custom_quantization_configs.

Attribute leaky_relu_uses_udf:

Default is True. If Ture, AFE will use UDF for LeakyRelu operation. Else AFE will breakdown the LeakyRelu into multiple elementwise operationis unless some layers are specific configured in the custom_quantization_configs.

Attribute custom_quantization_configs:

Optional[Dict[NodeName, Dict[str, Any]]]. A dictionary using NodeName as keys. The value to each key is a dictionary of the AwesomeQuantAttr’s field names and sets target configuration.

Example

The example shows how a custom_quantization_configs looks like to config the output_int32 field in a Conv2DQuantAttrs in a output conv2d_add node to True.

custom_quantization_configs = {“MLA_1/conv2d_add_84”: {“output_int32”: True}}

afe.ir.transform.quantization_transforms.add_external_nodes_to_float_node_list(net: afe.ir.net.AwesomeNet) None[source]

Adding names of external nodes to the net.float_node_list attributes. net.float_node_list is used to decide whether to apply quantization transforms on a node.

Parameters

param net:

AwesomeNet.

afe.ir.transform.quantization_transforms.traverse_network(visit_node: Callable[[_State, Dict[afe.ir.defines.InputName, _A], _A | None, afe.ir.node.AwesomeNode], _A], state: _State, inputs: Dict[afe.ir.defines.InputName, _A], net: afe.ir.net.AwesomeNet) _A[source]

Traverse a network in topological order and process each node using visit_function. Values are propagated along graph edges.

The network’s topological order must have been computed. The topological order upon entry to the function is used.

Parameters:
  • visit_node – Processing to do on a network node. Processing can access shared state and can read results from processing the node’s inputs. Processing returns a result to be passed to other nodes. It may modify the node and the network.

  • net – Network to traverse.

  • state – Mutable data used by visit_node.

  • inputs – An ordered dict of values for the net’s inputs.

Returns:

Result of processing the network’s output node.

class afe.ir.transform.quantization_transforms.QuantizeState[source]

Data used by the quantization algorithm during traversal of a network’s nodes.

casts: afe.ir.defines.QuantizationCasts[source]
disallowed_quantization_nodes: Set[afe.ir.defines.NodeName][source]
num_calibration_inputs: int[source]
classmethod initialize(num_calibration_inputs: int) QuantizeState[source]

Initialize a new QuantizeState for the beginning of the algorithm.

enter_awesome_net_scope(net: afe.ir.net.AwesomeNet, num_calibration_inputs: int) QuantizeState[source]

Prepare to process the nodes in net. Returns a new QuantizeState with information about the nodes in net. Some mutable data is shared with the original QuantizeState.

afe.ir.transform.quantization_transforms.quantized_operation_execution(state: QuantizeState, inputs: Dict[afe.ir.defines.InputName, afe.ir.operations.QuantizationTensorData], placeholder_value: afe.ir.operations.QuantizationTensorData | None, node: afe.ir.node.AwesomeNode, node_casts: afe.ir.defines.InputsQuantCast) List[numpy.ndarray][source]
class afe.ir.transform.quantization_transforms.Quantize[source]

Quantizes an AwesomeNet