concrete.ml.quantization.quantized_ops

module concrete.ml.quantization.quantized_ops

Quantized versions of the ONNX operators for post training quantization.


class QuantizedSigmoid

Quantized sigmoid op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedHardSigmoid

Quantized HardSigmoid op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedRelu

Quantized Relu op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedPRelu

Quantized PRelu op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedLeakyRelu

Quantized LeakyRelu op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedHardSwish

Quantized Hardswish op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedElu

Quantized Elu op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedSelu

Quantized Selu op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedCelu

Quantized Celu op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedClip

Quantized clip op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedRound

Quantized round op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedPow

Quantized pow op.

Only works for a float constant power. This operation will be fused to a (potentially larger) TLU.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedGemm

Quantized Gemm op.

method __init__

__init__(
    n_bits_output: int,
    int_input_names: Set[str] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: QuantizationOptions = None,
    **attrs
)None

property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method can_fuse

can_fuse()

Determine if this op can be fused.

Gemm operation can not be fused since it must be performed over integer tensors and it combines different values of the input tensors.

Returns:

  • bool: False, this operation can not be fused as it adds different encrypted integers


method q_impl

q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray

class QuantizedMatMul

Quantized MatMul op.

method __init__

__init__(
    n_bits_output: int,
    int_input_names: Set[str] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: QuantizationOptions = None,
    **attrs
)None

property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method can_fuse

can_fuse()

Determine if this op can be fused.

Gemm operation can not be fused since it must be performed over integer tensors and it combines different values of the input tensors.

Returns:

  • bool: False, this operation can not be fused as it adds different encrypted integers


method q_impl

q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray

class QuantizedAdd

Quantized Addition operator.

Can add either two variables (both encrypted) or a variable and a constant


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method can_fuse

can_fuse()bool

Determine if this op can be fused.

Add operation can be computed in float and fused if it operates over inputs produced by a single integer tensor. For example the expression x + x * 1.75, where x is an encrypted tensor, can be computed with a single TLU.

Returns:

  • bool: Whether the number of integer input tensors allows computing this op as a TLU


method q_impl

q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray

class QuantizedTanh

Quantized Tanh op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedSoftplus

Quantized Softplus op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedExp

Quantized Exp op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedLog

Quantized Log op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedAbs

Quantized Abs op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedIdentity

Quantized Identity op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method q_impl

q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray

class QuantizedReshape

Quantized Reshape op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method q_impl

q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray

Reshape the input integer encrypted tensor.

Args:

  • q_inputs: an encrypted integer tensor at index 0 and one constant shape at index 1

  • attrs: additional optional reshape options

Returns:

  • result (QuantizedArray): reshaped encrypted integer tensor


class QuantizedConv

Quantized Conv op.

method __init__

__init__(
    n_bits_output: int,
    int_input_names: Set[str] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: QuantizationOptions = None,
    **attrs
)None

Construct the quantized convolution operator and retrieve parameters.

Args:

  • n_bits_output: number of bits for the quantization of the outputs of this operator

  • int_input_names: names of integer tensors that are taken as input for this operation

  • constant_inputs: the weights and activations

  • input_quant_opts: options for the input quantizer

  • attrs: convolution options

  • dilations (Tuple[int]): dilation of the kernel, default 1 on all dimensions.

  • group (int): number of convolution groups, default 1

  • kernel_shape (Tuple[int]): shape of the kernel. Should have 2 elements for 2d conv

  • pads (Tuple[int]): padding in ONNX format (begin, end) on each axis

  • strides (Tuple[int]): stride of the convolution on each axis


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method can_fuse

can_fuse()bool

Determine if this op can be fused.

Conv operation can not be fused since it must be performed over integer tensors and it combines different elements of the input tensors.

Returns:

  • bool: False, this operation can not be fused as it adds different encrypted integers


method q_impl

q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray

Compute the quantized convolution between two quantized tensors.

Allows an optional quantized bias.

Args:

  • q_inputs: input tuple, contains

  • x (numpy.ndarray): input data. Shape is N x C x H x W for 2d

  • w (numpy.ndarray): weights tensor. Shape is (O x I x Kh x Kw) for 2d

  • b (numpy.ndarray, Optional): bias tensor, Shape is (O,)

  • attrs: convolution options handled in constructor

Returns:

  • res (QuantizedArray): result of the quantized integer convolution


class QuantizedAvgPool

Quantized Average Pooling op.

method __init__

__init__(
    n_bits_output: int,
    int_input_names: Set[str] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: QuantizationOptions = None,
    **attrs
)None

property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method can_fuse

can_fuse()bool

Determine if this op can be fused.

Avg Pooling operation can not be fused since it must be performed over integer tensors and it combines different elements of the input tensors.

Returns:

  • bool: False, this operation can not be fused as it adds different encrypted integers


method q_impl

q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray

class QuantizedPad

Quantized Padding op.

method __init__

__init__(
    n_bits_output: int,
    int_input_names: Set[str] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: QuantizationOptions = None,
    **attrs
)None

property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method can_fuse

can_fuse()bool

Determine if this op can be fused.

Pad operation can not be fused since it must be performed over integer tensors.

Returns:

  • bool: False, this operation can not be fused as it is manipulates integer tensors


class QuantizedWhere

Where operator on quantized arrays.

Supports only constants for the results produced on the True/False branches.

method __init__

__init__(
    n_bits_output: int,
    int_input_names: Set[str] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: QuantizationOptions = None,
    **attrs
)None

property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedCast

Cast the input to the required data type.

In FHE we only support a limited number of output types. Booleans are cast to integers.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedGreater

Comparison operator >.

Only supports comparison with a constant.

method __init__

__init__(
    n_bits_output: int,
    int_input_names: Set[str] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: QuantizationOptions = None,
    **attrs
)None

property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedGreaterOrEqual

Comparison operator >=.

Only supports comparison with a constant.

method __init__

__init__(
    n_bits_output: int,
    int_input_names: Set[str] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: QuantizationOptions = None,
    **attrs
)None

property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedLess

Comparison operator <.

Only supports comparison with a constant.

method __init__

__init__(
    n_bits_output: int,
    int_input_names: Set[str] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: QuantizationOptions = None,
    **attrs
)None

property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedLessOrEqual

Comparison operator <=.

Only supports comparison with a constant.

method __init__

__init__(
    n_bits_output: int,
    int_input_names: Set[str] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: QuantizationOptions = None,
    **attrs
)None

property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedOr

Or operator ||.

This operation is not really working as a quantized operation. It just works when things got fused, as in e.g. Act(x) = x || (x + 42))


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedDiv

Div operator /.

This operation is not really working as a quantized operation. It just works when things got fused, as in e.g. Act(x) = 1000 / (x + 42))


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedMul

Multiplication operator.

Only multiplies an encrypted tensor with a float constant for now. This operation will be fused to a (potentially larger) TLU.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedSub

Subtraction operator.

This works the same as addition on both encrypted - encrypted and on encrypted - constant.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method can_fuse

can_fuse()bool

Determine if this op can be fused.

Add operation can be computed in float and fused if it operates over inputs produced by a single integer tensor. For example the expression x + x * 1.75, where x is an encrypted tensor, can be computed with a single TLU.

Returns:

  • bool: Whether the number of integer input tensors allows computing this op as a TLU


method q_impl

q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray

class QuantizedBatchNormalization

Quantized Batch normalization with encrypted input and in-the-clear normalization params.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedFlatten

Quantized flatten for encrypted inputs.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method can_fuse

can_fuse()bool

Determine if this op can be fused.

Flatten operation can not be fused since it must be performed over integer tensors.

Returns:

  • bool: False, this operation can not be fused as it is manipulates integer tensors.


method q_impl

q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray

Flatten the input integer encrypted tensor.

Args:

  • q_inputs: an encrypted integer tensor at index 0

  • attrs: contains axis attribute

Returns:

  • result (QuantizedArray): reshaped encrypted integer tensor


class QuantizedReduceSum

ReduceSum with encrypted input.

This operator is currently an experimental feature.

method __init__

__init__(
    n_bits_output: int,
    int_input_names: Set[str] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: Optional[QuantizationOptions] = None,
    **attrs
)None

Construct the quantized ReduceSum operator and retrieve parameters.

Args:

  • n_bits_output (int): Number of bits for the operator's quantization of outputs.

  • int_input_names (Optional[Set[str]]): Names of input integer tensors. Default to None.

  • constant_inputs (Optional[Dict]): Input constant tensor.

  • axes (Optional[numpy.ndarray]): Array of integers along which to reduce. The default is to reduce over all the dimensions of the input tensor if 'noop_with_empty_axes' is false, else act as an Identity op when 'noop_with_empty_axes' is true. Accepted range is [-r, r-1] where r = rank(data). Default to None.

  • input_quant_opts (Optional[QuantizationOptions]): Options for the input quantizer. Default to None.

  • attrs (dict): RecuseSum options.

  • keepdims (int): Keep the reduced dimension or not, 1 means keeping the input dimension, 0 will reduce it along the given axis. Default to 1.

  • noop_with_empty_axes (int): Defines behavior if 'axes' is empty or set to None. Default behavior with 0 is to reduce all axes. When axes is empty and this attribute is set to true 1, input tensor will not be reduced, and the output tensor would be equivalent to input tensor. Default to 0.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method calibrate

calibrate(*inputs: ndarray) → ndarray

Create corresponding QuantizedArray for the output of the activation function.

Args:

  • *inputs (numpy.ndarray): Calibration sample inputs.

Returns:

  • numpy.ndarray: the output values for the provided calibration samples.


method q_impl

q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray

Sum the encrypted tensor's values over axis 1.

Args:

  • q_inputs (QuantizedArray): An encrypted integer tensor at index 0.

  • attrs (Dict): Contains axis attribute.

Returns:

  • (QuantizedArray): The sum of all values along axis 1 as an encrypted integer tensor.


method tree_sum

tree_sum(input_qarray, is_calibration=False)

Large sum without overflow (only MSB remains).

Args:

  • input_qarray: Enctyped integer tensor.

  • is_calibration: Whether we are calibrating the tree sum. If so, it will create all the quantizers for the downscaling.

Returns:

  • (numpy.ndarray): The MSB (based on the precision self.n_bits) of the integers sum.


class QuantizedErf

Quantized erf op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedNot

Quantized Not op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedBrevitasQuant

Brevitas uniform quantization with encrypted input.

method __init__

__init__(
    n_bits_output: int,
    int_input_names: Set[str] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: Optional[QuantizationOptions] = None,
    **attrs
)None

Construct the Brevitas quantization operator.

Args:

  • n_bits_output (int): Number of bits for the operator's quantization of outputs. Not used, will be overridden by the bit_width in ONNX

  • int_input_names (Optional[Set[str]]): Names of input integer tensors. Default to None.

  • constant_inputs (Optional[Dict]): Input constant tensor.

  • scale (float): Quantizer scale

  • zero_point (float): Quantizer zero-point

  • bit_width (int): Number of bits of the integer representation

  • input_quant_opts (Optional[QuantizationOptions]): Options for the input quantizer. Default to None. attrs (dict):

  • rounding_mode (str): Rounding mode (default and only accepted option is "ROUND")

  • signed (int): Whether this op quantizes to signed integers (default 1),

  • narrow (int): Whether this op quantizes to a narrow range of integers e.g. [-2n_bits-1 .. 2n_bits-1] (default 0),


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method q_impl

q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray

Quantize values.

Args:

  • q_inputs: an encrypted integer tensor at index 0 and one constant shape at index 1

  • attrs: additional optional reshape options

Returns:

  • result (QuantizedArray): reshaped encrypted integer tensor


class QuantizedTranspose

Transpose operator for quantized inputs.

This operator performs quantization, transposes the encrypted data, then dequantizes again.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


method q_impl

q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray

Reshape the input integer encrypted tensor.

Args:

  • q_inputs: an encrypted integer tensor at index 0 and one constant shape at index 1

  • attrs: additional optional reshape options

Returns:

  • result (QuantizedArray): reshaped encrypted integer tensor


class QuantizedFloor

Quantized Floor op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedMax

Quantized Max op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedMin

Quantized Min op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedNeg

Quantized Neg op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential


class QuantizedSign

Quantized Neg op.


property op_type

Get the type of this operation.

Returns:

  • op_type (str): The type of this operation, in the ONNX referential

Last updated