quark.onnx.finetuning.create_torch.create_model_ops#

Module Contents#

Classes#

Functions#

quark.onnx.finetuning.create_torch.create_model_ops.extract_attr_values(attr: onnx.AttributeProto) Any#

Extract onnx attribute values.

quark.onnx.finetuning.create_torch.create_model_ops.param_is_symmetric(params: List[Any]) bool#

Check if parameters are symmetric, all values [2,2,2,2]. Then we can use only [2,2].

quark.onnx.finetuning.create_torch.create_model_ops.extract_padding_params(params: List[Any]) Any#

Extract padding parameters for Pad layers.

quark.onnx.finetuning.create_torch.create_model_ops.extract_padding_params_for_conv(params: List[Any]) Any#

Padding params in onnx are different than in pytorch. That is why we need to check if they are symmetric and cut half or return a padding layer.

quark.onnx.finetuning.create_torch.create_model_ops.extract_weight_and_bias(params: List[Any]) Tuple[numpy.typing.NDArray[Any], Union[numpy.typing.NDArray[Any], None]]#

Extract weights and biases.

quark.onnx.finetuning.create_torch.create_model_ops.load_weight_and_bias(layer: torch.nn.Module, weight: numpy.typing.NDArray[Any], bias: Union[numpy.typing.NDArray[Any], None]) None#

Load weight and bias to a given layer from onnx format.

quark.onnx.finetuning.create_torch.create_model_ops.convert_conv(node: onnx.NodeProto, layer_params: List[Any], layer_qinfos: List[Any]) Tuple[quark.onnx.finetuning.create_torch.quant_base_ops.QuantizeWrapper, Union[quark.onnx.finetuning.create_torch.quant_base_ops.QuantizeWrapper, None]]#

Use to convert Conv ONNX node to Torch module (or called layer). This function supports onnx’s Conv and ConvTranspose from 1 to 11.

:param node : ONNX node. :param layer_params : Layer weight and bias parameters. :param layer_qinfos : Layer quantization informations. :return: Converted conv layer, perhaps it has a pad layer.

quark.onnx.finetuning.create_torch.create_model_ops.convert_gemm(node: onnx.NodeProto, layer_params: List[Any], layer_qinfos: List[Any]) Tuple[quark.onnx.finetuning.create_torch.quant_gemm_ops.QGemm, None]#

Use to convert Gemm ONNX node to Torch module. This function supports onnx’s Instance Norm from 6.

:param node : ONNX node. :param layer_params : Layer weight and bias parameters. :param layer_qinfos : Layer quantization informations. :return: Converted Gemm layer.

quark.onnx.finetuning.create_torch.create_model_ops.convert_norm(node: onnx.NodeProto, layer_params: List[Any], layer_qinfos: List[Any]) Tuple[quark.onnx.finetuning.create_torch.quant_norm_ops.QInstanceNorm2d, None]#

Use to convert norm (Instance Norm) ONNX node to Torch module. This function supports onnx’s Instance Norm from 6.

:param node : ONNX node. :param layer_params : Layer weight and bias parameters. :param layer_qinfos : Layer quantization informations. :return: Converted norm (Instance Norm) layer.

class quark.onnx.finetuning.create_torch.create_model_ops.Clip(min: Optional[float] = None, max: Optional[float] = None)#

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:

import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:`to`, etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

quark.onnx.finetuning.create_torch.create_model_ops.convert_act(node: onnx.NodeProto) Union[torch.nn.Module, None]#

Use to convert Activation ONNX node to Torch module (or called layer). :param node : ONNX node. :return: Converted act layer.

quark.onnx.finetuning.create_torch.create_model_ops.convert_ops_to_modules(onnx_model: onnx.ModelProto) Tuple[Optional[torch.nn.Module], Optional[torch.nn.Module], Optional[torch.nn.Module], Optional[quark.onnx.finetuning.create_torch.quant_base_ops.QDQModule]]#

Convert ONNX operations to Torch modules.

quark.onnx.finetuning.create_torch.create_model_ops.set_modules_original_weight(module: torch.nn.Module, weight: numpy.typing.NDArray[Any]) None#

For setting original float weight

quark.onnx.finetuning.create_torch.create_model_ops.get_modules_optimized_weight(module: torch.nn.Module) Any#

For getting optimized quantized weight

quark.onnx.finetuning.create_torch.create_model_ops.set_modules_original_bias(module: torch.nn.Module, bias: numpy.typing.NDArray[Any]) None#

For setting original float bias

quark.onnx.finetuning.create_torch.create_model_ops.get_modules_optimized_bias(module: torch.nn.Module) Any#

For getting optimized quantized bias